janja 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1829 -1006
- package/dist/index.d.ts +235 -126
- package/dist/index.esm.js +1829 -1002
- package/dist/index.js +1829 -1006
- package/package.json +4 -4
package/dist/index.cjs.js
CHANGED
|
@@ -1,334 +1,904 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const count = end - tag.start;
|
|
35
|
-
const caretLine = `${' '.repeat(offset)}${'^'.repeat(count)}`;
|
|
36
|
-
if (/\S/.test(caretLine)) {
|
|
37
|
-
output.push(caretLine);
|
|
38
|
-
}
|
|
39
|
-
if (end < tag.end) {
|
|
40
|
-
tag.start += count + 1;
|
|
41
|
-
}
|
|
42
|
-
addCaretLine(++caretLinesCount + index);
|
|
3
|
+
var NodeType;
|
|
4
|
+
(function (NodeType) {
|
|
5
|
+
NodeType["TEMPLATE"] = "TEMPLATE";
|
|
6
|
+
NodeType["COMMENT"] = "COMMENT";
|
|
7
|
+
NodeType["OUTPUT"] = "OUTPUT";
|
|
8
|
+
NodeType["TEXT"] = "TEXT";
|
|
9
|
+
NodeType["IF"] = "IF";
|
|
10
|
+
NodeType["ELSE"] = "ELSE";
|
|
11
|
+
NodeType["ELSE_IF"] = "ELSE_IF";
|
|
12
|
+
NodeType["FOR"] = "FOR";
|
|
13
|
+
NodeType["BREAK"] = "BREAK";
|
|
14
|
+
NodeType["CONTINUE"] = "CONTINUE";
|
|
15
|
+
NodeType["EXP"] = "EXP";
|
|
16
|
+
NodeType["INCLUDE"] = "INCLUDE";
|
|
17
|
+
NodeType["BLOCK"] = "BLOCK";
|
|
18
|
+
NodeType["SUPER"] = "SUPER";
|
|
19
|
+
NodeType["MACRO"] = "MACRO";
|
|
20
|
+
NodeType["CALLER"] = "CALLER";
|
|
21
|
+
NodeType["CALL"] = "CALL";
|
|
22
|
+
NodeType["SET"] = "SET";
|
|
23
|
+
NodeType["CAPTURE"] = "CAPTURE";
|
|
24
|
+
NodeType["UNKNOWN"] = "UNKNOWN";
|
|
25
|
+
NodeType["UNEXPECTED"] = "UNEXPECTED";
|
|
26
|
+
})(NodeType || (NodeType = {}));
|
|
27
|
+
class TemplateNode {
|
|
28
|
+
constructor(children, loc) {
|
|
29
|
+
Object.defineProperty(this, "children", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: children
|
|
43
34
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
35
|
+
Object.defineProperty(this, "loc", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: loc
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "type", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: NodeType.TEMPLATE
|
|
46
|
+
});
|
|
47
|
+
}
|
|
54
48
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
super(filepath ? `${message} at ${filepath}` : message);
|
|
59
|
-
Object.defineProperty(this, "token", {
|
|
49
|
+
class OutputNode {
|
|
50
|
+
constructor(val, loc, strip, exp) {
|
|
51
|
+
Object.defineProperty(this, "val", {
|
|
60
52
|
enumerable: true,
|
|
61
53
|
configurable: true,
|
|
62
54
|
writable: true,
|
|
63
|
-
value:
|
|
55
|
+
value: val
|
|
56
|
+
});
|
|
57
|
+
Object.defineProperty(this, "loc", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: loc
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(this, "strip", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: strip
|
|
68
|
+
});
|
|
69
|
+
Object.defineProperty(this, "exp", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: exp
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(this, "type", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: NodeType.OUTPUT
|
|
64
80
|
});
|
|
65
|
-
this.name = 'CompileError';
|
|
66
|
-
Error.captureStackTrace?.(this, this.constructor);
|
|
67
81
|
}
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
}
|
|
83
|
+
class TextNode {
|
|
84
|
+
constructor(val, loc, strip) {
|
|
85
|
+
Object.defineProperty(this, "val", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: val
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(this, "loc", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: loc
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "strip", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: strip
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, "type", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: NodeType.TEXT
|
|
108
|
+
});
|
|
70
109
|
}
|
|
71
110
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const ESCAPE = 'e';
|
|
76
|
-
const RAW$1 = 'raw';
|
|
77
|
-
const LAYOUT = 'layout';
|
|
78
|
-
const INCLUDE = 'include';
|
|
79
|
-
const BLOCK = 'block';
|
|
80
|
-
const SUPER = 'super';
|
|
81
|
-
const ENDBLOCK = 'endblock';
|
|
82
|
-
|
|
83
|
-
class Context {
|
|
84
|
-
constructor(options) {
|
|
85
|
-
Object.defineProperty(this, "options", {
|
|
111
|
+
class IfNode {
|
|
112
|
+
constructor(test, body, alternatives, loc, strip) {
|
|
113
|
+
Object.defineProperty(this, "test", {
|
|
86
114
|
enumerable: true,
|
|
87
115
|
configurable: true,
|
|
88
116
|
writable: true,
|
|
89
|
-
value:
|
|
117
|
+
value: test
|
|
90
118
|
});
|
|
91
|
-
Object.defineProperty(this, "
|
|
119
|
+
Object.defineProperty(this, "body", {
|
|
92
120
|
enumerable: true,
|
|
93
121
|
configurable: true,
|
|
94
122
|
writable: true,
|
|
95
|
-
value:
|
|
123
|
+
value: body
|
|
96
124
|
});
|
|
97
|
-
Object.defineProperty(this, "
|
|
125
|
+
Object.defineProperty(this, "alternatives", {
|
|
98
126
|
enumerable: true,
|
|
99
127
|
configurable: true,
|
|
100
128
|
writable: true,
|
|
101
|
-
value:
|
|
129
|
+
value: alternatives
|
|
102
130
|
});
|
|
103
|
-
Object.defineProperty(this, "
|
|
131
|
+
Object.defineProperty(this, "loc", {
|
|
104
132
|
enumerable: true,
|
|
105
133
|
configurable: true,
|
|
106
134
|
writable: true,
|
|
107
|
-
value:
|
|
135
|
+
value: loc
|
|
108
136
|
});
|
|
109
|
-
Object.defineProperty(this, "
|
|
137
|
+
Object.defineProperty(this, "strip", {
|
|
110
138
|
enumerable: true,
|
|
111
139
|
configurable: true,
|
|
112
140
|
writable: true,
|
|
113
|
-
value:
|
|
141
|
+
value: strip
|
|
142
|
+
});
|
|
143
|
+
Object.defineProperty(this, "type", {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
value: NodeType.IF
|
|
114
148
|
});
|
|
115
149
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
150
|
+
}
|
|
151
|
+
class ElseIfNode {
|
|
152
|
+
constructor(test, body, loc, strip) {
|
|
153
|
+
Object.defineProperty(this, "test", {
|
|
154
|
+
enumerable: true,
|
|
155
|
+
configurable: true,
|
|
156
|
+
writable: true,
|
|
157
|
+
value: test
|
|
158
|
+
});
|
|
159
|
+
Object.defineProperty(this, "body", {
|
|
160
|
+
enumerable: true,
|
|
161
|
+
configurable: true,
|
|
162
|
+
writable: true,
|
|
163
|
+
value: body
|
|
164
|
+
});
|
|
165
|
+
Object.defineProperty(this, "loc", {
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: true,
|
|
168
|
+
writable: true,
|
|
169
|
+
value: loc
|
|
170
|
+
});
|
|
171
|
+
Object.defineProperty(this, "strip", {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: true,
|
|
174
|
+
writable: true,
|
|
175
|
+
value: strip
|
|
176
|
+
});
|
|
177
|
+
Object.defineProperty(this, "type", {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
configurable: true,
|
|
180
|
+
writable: true,
|
|
181
|
+
value: NodeType.ELSE_IF
|
|
182
|
+
});
|
|
120
183
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
184
|
+
}
|
|
185
|
+
class ElseNode {
|
|
186
|
+
constructor(body, loc, strip) {
|
|
187
|
+
Object.defineProperty(this, "body", {
|
|
188
|
+
enumerable: true,
|
|
189
|
+
configurable: true,
|
|
190
|
+
writable: true,
|
|
191
|
+
value: body
|
|
192
|
+
});
|
|
193
|
+
Object.defineProperty(this, "loc", {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
configurable: true,
|
|
196
|
+
writable: true,
|
|
197
|
+
value: loc
|
|
198
|
+
});
|
|
199
|
+
Object.defineProperty(this, "strip", {
|
|
200
|
+
enumerable: true,
|
|
201
|
+
configurable: true,
|
|
202
|
+
writable: true,
|
|
203
|
+
value: strip
|
|
204
|
+
});
|
|
205
|
+
Object.defineProperty(this, "type", {
|
|
206
|
+
enumerable: true,
|
|
207
|
+
configurable: true,
|
|
208
|
+
writable: true,
|
|
209
|
+
value: NodeType.ELSE
|
|
210
|
+
});
|
|
127
211
|
}
|
|
128
|
-
|
|
129
|
-
|
|
212
|
+
}
|
|
213
|
+
class ForNode {
|
|
214
|
+
constructor(loop, body, loc, strip) {
|
|
215
|
+
Object.defineProperty(this, "loop", {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
configurable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
value: loop
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(this, "body", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: body
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(this, "loc", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
configurable: true,
|
|
230
|
+
writable: true,
|
|
231
|
+
value: loc
|
|
232
|
+
});
|
|
233
|
+
Object.defineProperty(this, "strip", {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
configurable: true,
|
|
236
|
+
writable: true,
|
|
237
|
+
value: strip
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(this, "type", {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
configurable: true,
|
|
242
|
+
writable: true,
|
|
243
|
+
value: NodeType.FOR
|
|
244
|
+
});
|
|
130
245
|
}
|
|
131
|
-
|
|
132
|
-
|
|
246
|
+
}
|
|
247
|
+
class BreakNode {
|
|
248
|
+
constructor(val, loc, strip) {
|
|
249
|
+
Object.defineProperty(this, "val", {
|
|
250
|
+
enumerable: true,
|
|
251
|
+
configurable: true,
|
|
252
|
+
writable: true,
|
|
253
|
+
value: val
|
|
254
|
+
});
|
|
255
|
+
Object.defineProperty(this, "loc", {
|
|
256
|
+
enumerable: true,
|
|
257
|
+
configurable: true,
|
|
258
|
+
writable: true,
|
|
259
|
+
value: loc
|
|
260
|
+
});
|
|
261
|
+
Object.defineProperty(this, "strip", {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
configurable: true,
|
|
264
|
+
writable: true,
|
|
265
|
+
value: strip
|
|
266
|
+
});
|
|
267
|
+
Object.defineProperty(this, "type", {
|
|
268
|
+
enumerable: true,
|
|
269
|
+
configurable: true,
|
|
270
|
+
writable: true,
|
|
271
|
+
value: NodeType.BREAK
|
|
272
|
+
});
|
|
133
273
|
}
|
|
134
|
-
|
|
135
|
-
|
|
274
|
+
}
|
|
275
|
+
class ContinueNode {
|
|
276
|
+
constructor(val, loc, strip) {
|
|
277
|
+
Object.defineProperty(this, "val", {
|
|
278
|
+
enumerable: true,
|
|
279
|
+
configurable: true,
|
|
280
|
+
writable: true,
|
|
281
|
+
value: val
|
|
282
|
+
});
|
|
283
|
+
Object.defineProperty(this, "loc", {
|
|
284
|
+
enumerable: true,
|
|
285
|
+
configurable: true,
|
|
286
|
+
writable: true,
|
|
287
|
+
value: loc
|
|
288
|
+
});
|
|
289
|
+
Object.defineProperty(this, "strip", {
|
|
290
|
+
enumerable: true,
|
|
291
|
+
configurable: true,
|
|
292
|
+
writable: true,
|
|
293
|
+
value: strip
|
|
294
|
+
});
|
|
295
|
+
Object.defineProperty(this, "type", {
|
|
296
|
+
enumerable: true,
|
|
297
|
+
configurable: true,
|
|
298
|
+
writable: true,
|
|
299
|
+
value: NodeType.CONTINUE
|
|
300
|
+
});
|
|
136
301
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
302
|
+
}
|
|
303
|
+
class UnknownDirectiveNode {
|
|
304
|
+
constructor(name, val, loc, strip) {
|
|
305
|
+
Object.defineProperty(this, "name", {
|
|
306
|
+
enumerable: true,
|
|
307
|
+
configurable: true,
|
|
308
|
+
writable: true,
|
|
309
|
+
value: name
|
|
310
|
+
});
|
|
311
|
+
Object.defineProperty(this, "val", {
|
|
312
|
+
enumerable: true,
|
|
313
|
+
configurable: true,
|
|
314
|
+
writable: true,
|
|
315
|
+
value: val
|
|
316
|
+
});
|
|
317
|
+
Object.defineProperty(this, "loc", {
|
|
318
|
+
enumerable: true,
|
|
319
|
+
configurable: true,
|
|
320
|
+
writable: true,
|
|
321
|
+
value: loc
|
|
322
|
+
});
|
|
323
|
+
Object.defineProperty(this, "strip", {
|
|
324
|
+
enumerable: true,
|
|
325
|
+
configurable: true,
|
|
326
|
+
writable: true,
|
|
327
|
+
value: strip
|
|
328
|
+
});
|
|
329
|
+
Object.defineProperty(this, "type", {
|
|
330
|
+
enumerable: true,
|
|
331
|
+
configurable: true,
|
|
332
|
+
writable: true,
|
|
333
|
+
value: NodeType.UNKNOWN
|
|
334
|
+
});
|
|
143
335
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
336
|
+
}
|
|
337
|
+
class UnexpectedDirectiveNode {
|
|
338
|
+
constructor(name, val, loc, strip) {
|
|
339
|
+
Object.defineProperty(this, "name", {
|
|
340
|
+
enumerable: true,
|
|
341
|
+
configurable: true,
|
|
342
|
+
writable: true,
|
|
343
|
+
value: name
|
|
344
|
+
});
|
|
345
|
+
Object.defineProperty(this, "val", {
|
|
346
|
+
enumerable: true,
|
|
347
|
+
configurable: true,
|
|
348
|
+
writable: true,
|
|
349
|
+
value: val
|
|
350
|
+
});
|
|
351
|
+
Object.defineProperty(this, "loc", {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
configurable: true,
|
|
354
|
+
writable: true,
|
|
355
|
+
value: loc
|
|
356
|
+
});
|
|
357
|
+
Object.defineProperty(this, "strip", {
|
|
358
|
+
enumerable: true,
|
|
359
|
+
configurable: true,
|
|
360
|
+
writable: true,
|
|
361
|
+
value: strip
|
|
362
|
+
});
|
|
363
|
+
Object.defineProperty(this, "type", {
|
|
364
|
+
enumerable: true,
|
|
365
|
+
configurable: true,
|
|
366
|
+
writable: true,
|
|
367
|
+
value: NodeType.UNEXPECTED
|
|
368
|
+
});
|
|
148
369
|
}
|
|
149
370
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
371
|
+
class CommentNode {
|
|
372
|
+
constructor(val, loc, strip) {
|
|
373
|
+
Object.defineProperty(this, "val", {
|
|
374
|
+
enumerable: true,
|
|
375
|
+
configurable: true,
|
|
376
|
+
writable: true,
|
|
377
|
+
value: val
|
|
378
|
+
});
|
|
379
|
+
Object.defineProperty(this, "loc", {
|
|
380
|
+
enumerable: true,
|
|
381
|
+
configurable: true,
|
|
382
|
+
writable: true,
|
|
383
|
+
value: loc
|
|
384
|
+
});
|
|
385
|
+
Object.defineProperty(this, "strip", {
|
|
386
|
+
enumerable: true,
|
|
387
|
+
configurable: true,
|
|
388
|
+
writable: true,
|
|
389
|
+
value: strip
|
|
390
|
+
});
|
|
391
|
+
Object.defineProperty(this, "type", {
|
|
392
|
+
enumerable: true,
|
|
393
|
+
configurable: true,
|
|
394
|
+
writable: true,
|
|
395
|
+
value: NodeType.COMMENT
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
class IncludeNode {
|
|
400
|
+
constructor(val, loc, strip) {
|
|
401
|
+
Object.defineProperty(this, "val", {
|
|
402
|
+
enumerable: true,
|
|
403
|
+
configurable: true,
|
|
404
|
+
writable: true,
|
|
405
|
+
value: val
|
|
406
|
+
});
|
|
407
|
+
Object.defineProperty(this, "loc", {
|
|
408
|
+
enumerable: true,
|
|
409
|
+
configurable: true,
|
|
410
|
+
writable: true,
|
|
411
|
+
value: loc
|
|
412
|
+
});
|
|
413
|
+
Object.defineProperty(this, "strip", {
|
|
414
|
+
enumerable: true,
|
|
415
|
+
configurable: true,
|
|
416
|
+
writable: true,
|
|
417
|
+
value: strip
|
|
418
|
+
});
|
|
419
|
+
Object.defineProperty(this, "type", {
|
|
420
|
+
enumerable: true,
|
|
421
|
+
configurable: true,
|
|
422
|
+
writable: true,
|
|
423
|
+
value: NodeType.INCLUDE
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
class BlockNode {
|
|
428
|
+
constructor(val, body, loc, strip) {
|
|
429
|
+
Object.defineProperty(this, "val", {
|
|
430
|
+
enumerable: true,
|
|
431
|
+
configurable: true,
|
|
432
|
+
writable: true,
|
|
433
|
+
value: val
|
|
434
|
+
});
|
|
435
|
+
Object.defineProperty(this, "body", {
|
|
436
|
+
enumerable: true,
|
|
437
|
+
configurable: true,
|
|
438
|
+
writable: true,
|
|
439
|
+
value: body
|
|
440
|
+
});
|
|
441
|
+
Object.defineProperty(this, "loc", {
|
|
442
|
+
enumerable: true,
|
|
443
|
+
configurable: true,
|
|
444
|
+
writable: true,
|
|
445
|
+
value: loc
|
|
446
|
+
});
|
|
447
|
+
Object.defineProperty(this, "strip", {
|
|
448
|
+
enumerable: true,
|
|
449
|
+
configurable: true,
|
|
450
|
+
writable: true,
|
|
451
|
+
value: strip
|
|
452
|
+
});
|
|
453
|
+
Object.defineProperty(this, "type", {
|
|
454
|
+
enumerable: true,
|
|
455
|
+
configurable: true,
|
|
456
|
+
writable: true,
|
|
457
|
+
value: NodeType.BLOCK
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
class SuperNode {
|
|
462
|
+
constructor(val, loc, strip) {
|
|
463
|
+
Object.defineProperty(this, "val", {
|
|
464
|
+
enumerable: true,
|
|
465
|
+
configurable: true,
|
|
466
|
+
writable: true,
|
|
467
|
+
value: val
|
|
468
|
+
});
|
|
469
|
+
Object.defineProperty(this, "loc", {
|
|
470
|
+
enumerable: true,
|
|
471
|
+
configurable: true,
|
|
472
|
+
writable: true,
|
|
473
|
+
value: loc
|
|
474
|
+
});
|
|
475
|
+
Object.defineProperty(this, "strip", {
|
|
476
|
+
enumerable: true,
|
|
477
|
+
configurable: true,
|
|
478
|
+
writable: true,
|
|
479
|
+
value: strip
|
|
480
|
+
});
|
|
481
|
+
Object.defineProperty(this, "type", {
|
|
482
|
+
enumerable: true,
|
|
483
|
+
configurable: true,
|
|
484
|
+
writable: true,
|
|
485
|
+
value: NodeType.SUPER
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
class MacroNode {
|
|
490
|
+
constructor(val, body, loc, strip) {
|
|
491
|
+
Object.defineProperty(this, "val", {
|
|
492
|
+
enumerable: true,
|
|
493
|
+
configurable: true,
|
|
494
|
+
writable: true,
|
|
495
|
+
value: val
|
|
496
|
+
});
|
|
497
|
+
Object.defineProperty(this, "body", {
|
|
498
|
+
enumerable: true,
|
|
499
|
+
configurable: true,
|
|
500
|
+
writable: true,
|
|
501
|
+
value: body
|
|
502
|
+
});
|
|
503
|
+
Object.defineProperty(this, "loc", {
|
|
504
|
+
enumerable: true,
|
|
505
|
+
configurable: true,
|
|
506
|
+
writable: true,
|
|
507
|
+
value: loc
|
|
508
|
+
});
|
|
509
|
+
Object.defineProperty(this, "strip", {
|
|
510
|
+
enumerable: true,
|
|
511
|
+
configurable: true,
|
|
512
|
+
writable: true,
|
|
513
|
+
value: strip
|
|
514
|
+
});
|
|
515
|
+
Object.defineProperty(this, "type", {
|
|
516
|
+
enumerable: true,
|
|
517
|
+
configurable: true,
|
|
518
|
+
writable: true,
|
|
519
|
+
value: NodeType.MACRO
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
class CallerNode {
|
|
524
|
+
constructor(val, loc, strip) {
|
|
525
|
+
Object.defineProperty(this, "val", {
|
|
526
|
+
enumerable: true,
|
|
527
|
+
configurable: true,
|
|
528
|
+
writable: true,
|
|
529
|
+
value: val
|
|
530
|
+
});
|
|
531
|
+
Object.defineProperty(this, "loc", {
|
|
532
|
+
enumerable: true,
|
|
533
|
+
configurable: true,
|
|
534
|
+
writable: true,
|
|
535
|
+
value: loc
|
|
536
|
+
});
|
|
537
|
+
Object.defineProperty(this, "strip", {
|
|
538
|
+
enumerable: true,
|
|
539
|
+
configurable: true,
|
|
540
|
+
writable: true,
|
|
541
|
+
value: strip
|
|
542
|
+
});
|
|
543
|
+
Object.defineProperty(this, "type", {
|
|
544
|
+
enumerable: true,
|
|
545
|
+
configurable: true,
|
|
546
|
+
writable: true,
|
|
547
|
+
value: NodeType.CALLER
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
class CallNode {
|
|
552
|
+
constructor(val, body, loc, strip) {
|
|
553
|
+
Object.defineProperty(this, "val", {
|
|
554
|
+
enumerable: true,
|
|
555
|
+
configurable: true,
|
|
556
|
+
writable: true,
|
|
557
|
+
value: val
|
|
558
|
+
});
|
|
559
|
+
Object.defineProperty(this, "body", {
|
|
560
|
+
enumerable: true,
|
|
561
|
+
configurable: true,
|
|
562
|
+
writable: true,
|
|
563
|
+
value: body
|
|
564
|
+
});
|
|
565
|
+
Object.defineProperty(this, "loc", {
|
|
566
|
+
enumerable: true,
|
|
567
|
+
configurable: true,
|
|
568
|
+
writable: true,
|
|
569
|
+
value: loc
|
|
570
|
+
});
|
|
571
|
+
Object.defineProperty(this, "strip", {
|
|
572
|
+
enumerable: true,
|
|
573
|
+
configurable: true,
|
|
574
|
+
writable: true,
|
|
575
|
+
value: strip
|
|
576
|
+
});
|
|
577
|
+
Object.defineProperty(this, "type", {
|
|
578
|
+
enumerable: true,
|
|
579
|
+
configurable: true,
|
|
580
|
+
writable: true,
|
|
581
|
+
value: NodeType.CALL
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
class SetNode {
|
|
586
|
+
constructor(val, loc, strip) {
|
|
587
|
+
Object.defineProperty(this, "val", {
|
|
588
|
+
enumerable: true,
|
|
589
|
+
configurable: true,
|
|
590
|
+
writable: true,
|
|
591
|
+
value: val
|
|
592
|
+
});
|
|
593
|
+
Object.defineProperty(this, "loc", {
|
|
594
|
+
enumerable: true,
|
|
595
|
+
configurable: true,
|
|
596
|
+
writable: true,
|
|
597
|
+
value: loc
|
|
598
|
+
});
|
|
599
|
+
Object.defineProperty(this, "strip", {
|
|
600
|
+
enumerable: true,
|
|
601
|
+
configurable: true,
|
|
602
|
+
writable: true,
|
|
603
|
+
value: strip
|
|
604
|
+
});
|
|
605
|
+
Object.defineProperty(this, "type", {
|
|
606
|
+
enumerable: true,
|
|
607
|
+
configurable: true,
|
|
608
|
+
writable: true,
|
|
609
|
+
value: NodeType.SET
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
class CaptureNode {
|
|
614
|
+
constructor(val, body, loc, strip) {
|
|
615
|
+
Object.defineProperty(this, "val", {
|
|
154
616
|
enumerable: true,
|
|
155
617
|
configurable: true,
|
|
156
618
|
writable: true,
|
|
157
|
-
value:
|
|
619
|
+
value: val
|
|
158
620
|
});
|
|
159
|
-
Object.defineProperty(this, "
|
|
621
|
+
Object.defineProperty(this, "body", {
|
|
160
622
|
enumerable: true,
|
|
161
623
|
configurable: true,
|
|
162
624
|
writable: true,
|
|
163
|
-
value:
|
|
625
|
+
value: body
|
|
164
626
|
});
|
|
165
|
-
Object.defineProperty(this, "
|
|
627
|
+
Object.defineProperty(this, "loc", {
|
|
166
628
|
enumerable: true,
|
|
167
629
|
configurable: true,
|
|
168
630
|
writable: true,
|
|
169
|
-
value:
|
|
631
|
+
value: loc
|
|
170
632
|
});
|
|
171
|
-
Object.defineProperty(this, "
|
|
633
|
+
Object.defineProperty(this, "strip", {
|
|
172
634
|
enumerable: true,
|
|
173
635
|
configurable: true,
|
|
174
636
|
writable: true,
|
|
175
|
-
value:
|
|
637
|
+
value: strip
|
|
638
|
+
});
|
|
639
|
+
Object.defineProperty(this, "type", {
|
|
640
|
+
enumerable: true,
|
|
641
|
+
configurable: true,
|
|
642
|
+
writable: true,
|
|
643
|
+
value: NodeType.CAPTURE
|
|
176
644
|
});
|
|
177
645
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
for (const line of lines) {
|
|
197
|
-
this.content += line;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function highlightSource(message, source, { start: { line: l1, column: c1, }, end: { line: l2, column: c2, }, }) {
|
|
649
|
+
const output = [];
|
|
650
|
+
const caretLines = new Set();
|
|
651
|
+
const addCaretLine = (index) => {
|
|
652
|
+
caretLines.add(index - 2);
|
|
653
|
+
caretLines.add(index - 1);
|
|
654
|
+
caretLines.add(index);
|
|
655
|
+
caretLines.add(index + 1);
|
|
656
|
+
caretLines.add(index + 2);
|
|
657
|
+
};
|
|
658
|
+
const lines = source.split('\n');
|
|
659
|
+
const indentWidth = String(lines.length).length + 2;
|
|
660
|
+
let caretLinesCount = 0;
|
|
661
|
+
lines.forEach((line, index) => {
|
|
662
|
+
if (!line) {
|
|
663
|
+
return;
|
|
198
664
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
pushStr(s, o) {
|
|
205
|
-
if (s) {
|
|
206
|
-
if (o?.trimStart || this.options.trimWhitespace) {
|
|
207
|
-
s = s.trimStart();
|
|
208
|
-
}
|
|
209
|
-
if (o?.trimEnd || this.options.trimWhitespace) {
|
|
210
|
-
s = s.trimEnd();
|
|
665
|
+
output.push(`${`${index + 1}| `.padStart(indentWidth, ' ')}${line}`);
|
|
666
|
+
if (l1 === index + 1) {
|
|
667
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c1 - 1)}^${l1 === l2 && c2 > c1 + 1 ? `${' '.repeat(c2 - c1 - 2)}^` : ''}`;
|
|
668
|
+
if (/\S/.test(caretLine)) {
|
|
669
|
+
output.push(caretLine);
|
|
211
670
|
}
|
|
671
|
+
addCaretLine(++caretLinesCount + index);
|
|
212
672
|
}
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
.
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
end: this.content.length - 2,
|
|
220
|
-
};
|
|
673
|
+
if (l1 !== l2 && l2 === index + 1) {
|
|
674
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c2 - 1)}${'^'.repeat(1)}`;
|
|
675
|
+
if (/\S/.test(caretLine)) {
|
|
676
|
+
output.push(caretLine);
|
|
677
|
+
}
|
|
678
|
+
addCaretLine(++caretLinesCount + index);
|
|
221
679
|
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
|
|
680
|
+
});
|
|
681
|
+
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
682
|
+
return `${message}
|
|
683
|
+
|
|
684
|
+
${output
|
|
685
|
+
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
686
|
+
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
687
|
+
.join('\n')}
|
|
688
|
+
`;
|
|
231
689
|
}
|
|
232
690
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
* {{ foo }}
|
|
238
|
-
*
|
|
239
|
-
* - compiled:
|
|
240
|
-
* s+=e(foo);
|
|
241
|
-
*
|
|
242
|
-
* - sourceMap:
|
|
243
|
-
* Mapping {
|
|
244
|
-
* source: {
|
|
245
|
-
* start: 3,
|
|
246
|
-
* end: 6,
|
|
247
|
-
* },
|
|
248
|
-
* target: {
|
|
249
|
-
* start: 3,
|
|
250
|
-
* end: 8,
|
|
251
|
-
* }
|
|
252
|
-
* }
|
|
253
|
-
*/
|
|
254
|
-
class SourceMap {
|
|
255
|
-
constructor(options) {
|
|
256
|
-
Object.defineProperty(this, "options", {
|
|
691
|
+
class CompileError extends SyntaxError {
|
|
692
|
+
constructor(message, src, loc) {
|
|
693
|
+
super(message);
|
|
694
|
+
Object.defineProperty(this, "src", {
|
|
257
695
|
enumerable: true,
|
|
258
696
|
configurable: true,
|
|
259
697
|
writable: true,
|
|
260
|
-
value:
|
|
698
|
+
value: src
|
|
261
699
|
});
|
|
262
|
-
Object.defineProperty(this, "
|
|
700
|
+
Object.defineProperty(this, "loc", {
|
|
263
701
|
enumerable: true,
|
|
264
702
|
configurable: true,
|
|
265
703
|
writable: true,
|
|
266
|
-
value:
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
addMapping(source, target) {
|
|
270
|
-
this.mappings.push({
|
|
271
|
-
source,
|
|
272
|
-
target,
|
|
704
|
+
value: loc
|
|
273
705
|
});
|
|
706
|
+
this.name = 'CompileError';
|
|
707
|
+
CompileError.captureStackTrace?.(this, this.constructor);
|
|
274
708
|
}
|
|
275
|
-
|
|
276
|
-
return this.
|
|
277
|
-
.
|
|
278
|
-
.
|
|
709
|
+
get details() {
|
|
710
|
+
return this.loc
|
|
711
|
+
? highlightSource(this.message, this.src, this.loc)
|
|
712
|
+
: this.message;
|
|
279
713
|
}
|
|
280
714
|
}
|
|
281
715
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
716
|
+
const parseUnexpected = (token, parser) => parser.createUnexpectedDirective(token);
|
|
717
|
+
|
|
718
|
+
function parseBlock(token, parser) {
|
|
719
|
+
parser.requireExpression(token);
|
|
720
|
+
const val = parser.parseExp(token.expression);
|
|
721
|
+
parser.advance();
|
|
722
|
+
const body = parser.parseUntil(['endblock']);
|
|
723
|
+
if (parser.match('endblock')) {
|
|
724
|
+
parser.advance();
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
728
|
+
}
|
|
729
|
+
return new BlockNode(val, body, token.loc, token.strip);
|
|
730
|
+
}
|
|
731
|
+
function parseSuper(token, parser) {
|
|
732
|
+
parser.requireNoExpression(token);
|
|
733
|
+
parser.advance();
|
|
734
|
+
return new SuperNode(token.val, token.loc, token.strip);
|
|
735
|
+
}
|
|
736
|
+
const parsers$8 = {
|
|
737
|
+
block: parseBlock,
|
|
738
|
+
super: parseSuper,
|
|
739
|
+
endblock: parseUnexpected,
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
function parseCall(token, parser) {
|
|
743
|
+
parser.requireExpression(token);
|
|
744
|
+
const val = parser.parseExp(token.expression);
|
|
745
|
+
parser.advance();
|
|
746
|
+
const body = parser.parseUntil(['endcall']);
|
|
747
|
+
if (parser.match('endcall')) {
|
|
748
|
+
parser.advance();
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
752
|
+
}
|
|
753
|
+
return new CallNode(val, body, token.loc, token.strip);
|
|
754
|
+
}
|
|
755
|
+
const parsers$7 = {
|
|
756
|
+
call: parseCall,
|
|
757
|
+
endcall: parseUnexpected,
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
function parseCapture(token, parser) {
|
|
761
|
+
parser.requireExpression(token);
|
|
762
|
+
const val = parser.parseExp(token.expression);
|
|
763
|
+
parser.advance();
|
|
764
|
+
const body = parser.parseUntil(['endcapture']);
|
|
765
|
+
if (parser.match('endcapture')) {
|
|
766
|
+
parser.advance();
|
|
767
|
+
}
|
|
768
|
+
else {
|
|
769
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
770
|
+
}
|
|
771
|
+
return new CaptureNode(val, body, token.loc, token.strip);
|
|
772
|
+
}
|
|
773
|
+
const parsers$6 = {
|
|
774
|
+
capture: parseCapture,
|
|
775
|
+
endcapture: parseUnexpected,
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
function parseFor(token, parser) {
|
|
779
|
+
parser.requireExpression(token);
|
|
780
|
+
const loop = parser.parseExp(token.expression);
|
|
781
|
+
parser.advance();
|
|
782
|
+
const body = parser.parseUntil(['endfor']);
|
|
783
|
+
if (parser.match('endfor')) {
|
|
784
|
+
parser.advance();
|
|
785
|
+
}
|
|
786
|
+
else {
|
|
787
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
788
|
+
}
|
|
789
|
+
return new ForNode(loop, body, token.loc, token.strip);
|
|
790
|
+
}
|
|
791
|
+
function parseBreak(token, parser) {
|
|
792
|
+
parser.requireNoExpression(token);
|
|
793
|
+
parser.advance();
|
|
794
|
+
return new BreakNode(token.val, token.loc, token.strip);
|
|
795
|
+
}
|
|
796
|
+
function parseContinue(token, parser) {
|
|
797
|
+
parser.requireNoExpression(token);
|
|
798
|
+
parser.advance();
|
|
799
|
+
return new ContinueNode(token.val, token.loc, token.strip);
|
|
800
|
+
}
|
|
801
|
+
const parsers$5 = {
|
|
802
|
+
for: parseFor,
|
|
803
|
+
break: parseBreak,
|
|
804
|
+
continue: parseContinue,
|
|
805
|
+
endfor: parseUnexpected,
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
function parseIf(token, parser) {
|
|
809
|
+
parser.requireExpression(token);
|
|
810
|
+
parser.advance();
|
|
811
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
812
|
+
const alternatives = [];
|
|
813
|
+
while (parser.isDirective(['else', 'elseif', 'elsif', 'elif'])) {
|
|
814
|
+
const branchToken = parser.peek();
|
|
815
|
+
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
816
|
+
? parseElse(branchToken, parser)
|
|
817
|
+
: parseElseIf(branchToken, parser));
|
|
818
|
+
}
|
|
819
|
+
if (parser.match('endif')) {
|
|
820
|
+
parser.advance();
|
|
821
|
+
}
|
|
822
|
+
else {
|
|
823
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
824
|
+
}
|
|
825
|
+
return new IfNode(parser.parseExp(token.expression), body, alternatives, token.loc, token.strip);
|
|
826
|
+
}
|
|
827
|
+
function parseElseIf(token, parser) {
|
|
828
|
+
parser.requireExpression(token);
|
|
829
|
+
const test = parser.parseExp(token.expression);
|
|
830
|
+
parser.advance();
|
|
831
|
+
const body = parser.parseUntil(['else', 'elseif', 'elif', 'endif']);
|
|
832
|
+
return new ElseIfNode(test, body, token.loc, token.strip);
|
|
833
|
+
}
|
|
834
|
+
function parseElse(token, parser) {
|
|
835
|
+
parser.requireNoExpression(token);
|
|
836
|
+
parser.advance();
|
|
837
|
+
const body = parser.parseUntil(['endif']);
|
|
838
|
+
return new ElseNode(body, token.loc, token.strip);
|
|
839
|
+
}
|
|
840
|
+
const parsers$4 = {
|
|
841
|
+
if: parseIf,
|
|
842
|
+
elseif: parseUnexpected,
|
|
843
|
+
elsif: parseUnexpected,
|
|
844
|
+
elif: parseUnexpected,
|
|
845
|
+
endif: parseUnexpected,
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
function parseInclude(token, parser) {
|
|
849
|
+
parser.requireExpression(token);
|
|
850
|
+
const val = parser.parseExp(token.expression);
|
|
851
|
+
parser.advance();
|
|
852
|
+
return new IncludeNode(val, token.loc, token.strip);
|
|
853
|
+
}
|
|
854
|
+
const parsers$3 = {
|
|
855
|
+
include: parseInclude,
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
function parseMacro(token, parser) {
|
|
859
|
+
parser.requireExpression(token);
|
|
860
|
+
const name = parser.parseExp(token.expression);
|
|
861
|
+
parser.advance();
|
|
862
|
+
const body = parser.parseUntil(['endmacro']);
|
|
863
|
+
if (parser.match('endmacro')) {
|
|
864
|
+
parser.advance();
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
868
|
+
}
|
|
869
|
+
return new MacroNode(name, body, token.loc, token.strip);
|
|
870
|
+
}
|
|
871
|
+
function parseCaller(token, parser) {
|
|
872
|
+
parser.requireNoExpression(token);
|
|
873
|
+
parser.advance();
|
|
874
|
+
return new CallerNode(token.val, token.loc, token.strip);
|
|
875
|
+
}
|
|
876
|
+
const parsers$2 = {
|
|
877
|
+
macro: parseMacro,
|
|
878
|
+
caller: parseCaller,
|
|
879
|
+
endmacro: parseUnexpected,
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
function parseSet(token, parser) {
|
|
883
|
+
parser.requireExpression(token);
|
|
884
|
+
const val = parser.parseExp(token.expression);
|
|
885
|
+
parser.advance();
|
|
886
|
+
return new SetNode(val, token.loc, token.strip);
|
|
887
|
+
}
|
|
888
|
+
const parsers$1 = {
|
|
889
|
+
set: parseSet,
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
const parsers = {
|
|
893
|
+
...parsers$4,
|
|
894
|
+
...parsers$5,
|
|
895
|
+
...parsers$3,
|
|
896
|
+
...parsers$8,
|
|
897
|
+
...parsers$2,
|
|
898
|
+
...parsers$7,
|
|
899
|
+
...parsers$1,
|
|
900
|
+
...parsers$6,
|
|
901
|
+
};
|
|
332
902
|
|
|
333
903
|
class Safe extends String {
|
|
334
904
|
constructor(value) {
|
|
@@ -523,6 +1093,31 @@ var filters = /*#__PURE__*/Object.freeze({
|
|
|
523
1093
|
values: values
|
|
524
1094
|
});
|
|
525
1095
|
|
|
1096
|
+
async function compileBlock({ val: { value } }, compiler) {
|
|
1097
|
+
if (!compiler.blocks.has(value)) {
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
compiler.state.block = value;
|
|
1101
|
+
const blockNode = compiler.blocks.get(value).pop();
|
|
1102
|
+
await compiler.compileNodes(blockNode.body);
|
|
1103
|
+
compiler.blocks.delete(value);
|
|
1104
|
+
compiler.state.block = undefined;
|
|
1105
|
+
}
|
|
1106
|
+
async function compileSuper({ loc }, compiler) {
|
|
1107
|
+
if (!compiler.state.block) {
|
|
1108
|
+
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
const block = compiler.blocks.get(compiler.state.block)?.pop();
|
|
1112
|
+
if (block) {
|
|
1113
|
+
await compiler.compileNodes(block.body);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
const compilers$b = {
|
|
1117
|
+
[NodeType.BLOCK]: compileBlock,
|
|
1118
|
+
[NodeType.SUPER]: compileSuper,
|
|
1119
|
+
};
|
|
1120
|
+
|
|
526
1121
|
const expTokenOperators = {
|
|
527
1122
|
AND: '&&',
|
|
528
1123
|
OR: '||',
|
|
@@ -554,6 +1149,10 @@ const expTokenOperators = {
|
|
|
554
1149
|
ELSE: '',
|
|
555
1150
|
};
|
|
556
1151
|
|
|
1152
|
+
const CONTEXT = 'c';
|
|
1153
|
+
const ESCAPE = 'e';
|
|
1154
|
+
const FILTERS = 'f';
|
|
1155
|
+
|
|
557
1156
|
class ExpCompiler {
|
|
558
1157
|
constructor() {
|
|
559
1158
|
Object.defineProperty(this, "context", {
|
|
@@ -569,7 +1168,7 @@ class ExpCompiler {
|
|
|
569
1168
|
value: void 0
|
|
570
1169
|
});
|
|
571
1170
|
}
|
|
572
|
-
compile(expression, context, filters) {
|
|
1171
|
+
compile(expression, context = CONTEXT, filters = FILTERS) {
|
|
573
1172
|
this.context = context;
|
|
574
1173
|
this.filters = filters;
|
|
575
1174
|
return expression ? this.compileExp(expression) : '""';
|
|
@@ -625,43 +1224,346 @@ class ExpCompiler {
|
|
|
625
1224
|
}
|
|
626
1225
|
return s;
|
|
627
1226
|
}
|
|
628
|
-
compileLit({ value }) {
|
|
629
|
-
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1227
|
+
compileLit({ value }) {
|
|
1228
|
+
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1229
|
+
}
|
|
1230
|
+
compileSeq({ elements }) {
|
|
1231
|
+
return elements.map(element => this.compileExp(element)).toString();
|
|
1232
|
+
}
|
|
1233
|
+
compilePipe({ left, right }) {
|
|
1234
|
+
const { value: name, args = [] } = right;
|
|
1235
|
+
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map(arg => arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg))].join(',')}))`;
|
|
1236
|
+
}
|
|
1237
|
+
compileOf({ left, right }) {
|
|
1238
|
+
// destructuring
|
|
1239
|
+
if (left.type === 'SEQ') {
|
|
1240
|
+
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
1241
|
+
}
|
|
1242
|
+
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
1243
|
+
}
|
|
1244
|
+
compileSet({ left, right }) {
|
|
1245
|
+
if (left.type === 'ID') {
|
|
1246
|
+
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
1247
|
+
}
|
|
1248
|
+
// destructuring
|
|
1249
|
+
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
1250
|
+
}
|
|
1251
|
+
compileIs({ left, right }) {
|
|
1252
|
+
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
1253
|
+
}
|
|
1254
|
+
compileBinary({ type, left, right }) {
|
|
1255
|
+
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
1256
|
+
return type === 'NI' ? `!${ret}` : ret;
|
|
1257
|
+
}
|
|
1258
|
+
compileTernary({ test, consequent, alternative }) {
|
|
1259
|
+
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
async function compileCall({ val, body, loc }, compiler) {
|
|
1264
|
+
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1265
|
+
await compiler.compileNodes(body);
|
|
1266
|
+
compiler.pushRaw(null, `});`);
|
|
1267
|
+
}
|
|
1268
|
+
const compilers$a = {
|
|
1269
|
+
[NodeType.CALL]: compileCall,
|
|
1270
|
+
};
|
|
1271
|
+
|
|
1272
|
+
async function compileCapture({ val, body, loc }, compiler) {
|
|
1273
|
+
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1274
|
+
await compiler.compileNodes(body);
|
|
1275
|
+
compiler.pushRaw(null, `return s;})("");`);
|
|
1276
|
+
}
|
|
1277
|
+
const compilers$9 = {
|
|
1278
|
+
[NodeType.CAPTURE]: compileCapture,
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1281
|
+
async function compileComment({ val, loc }, compiler) {
|
|
1282
|
+
if (!compiler.options.stripComments) {
|
|
1283
|
+
compiler.pushStr(loc, `<!--${val}-->`);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
const compilers$8 = {
|
|
1287
|
+
[NodeType.COMMENT]: compileComment,
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
async function compileFor({ loop, body }, compiler) {
|
|
1291
|
+
const { context } = compiler;
|
|
1292
|
+
if (loop.left.type === 'SEQ') {
|
|
1293
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, `};`);
|
|
1294
|
+
}
|
|
1295
|
+
else {
|
|
1296
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, `};`);
|
|
1297
|
+
}
|
|
1298
|
+
await compiler.compileNodes(body);
|
|
1299
|
+
compiler.out();
|
|
1300
|
+
compiler.pushRaw(null, '}');
|
|
1301
|
+
}
|
|
1302
|
+
function compileBreak({ loc }, compiler) {
|
|
1303
|
+
compiler.pushRaw(loc, `break;`);
|
|
1304
|
+
}
|
|
1305
|
+
function compileContinue({ loc }, compiler) {
|
|
1306
|
+
compiler.pushRaw(loc, `continue;`);
|
|
1307
|
+
}
|
|
1308
|
+
const compilers$7 = {
|
|
1309
|
+
[NodeType.FOR]: compileFor,
|
|
1310
|
+
[NodeType.BREAK]: compileBreak,
|
|
1311
|
+
[NodeType.CONTINUE]: compileContinue,
|
|
1312
|
+
};
|
|
1313
|
+
|
|
1314
|
+
async function compileIf({ test, body, alternatives }, compiler) {
|
|
1315
|
+
compiler.pushRaw(test.loc, `if(${new ExpCompiler().compile(test, compiler.context)}){`);
|
|
1316
|
+
await compiler.compileNodes(body);
|
|
1317
|
+
compiler.pushRaw(null, '}');
|
|
1318
|
+
for (const branch of alternatives) {
|
|
1319
|
+
if (branch.type === NodeType.ELSE_IF) {
|
|
1320
|
+
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1321
|
+
}
|
|
1322
|
+
else {
|
|
1323
|
+
compiler.pushRaw(null, 'else{');
|
|
1324
|
+
}
|
|
1325
|
+
await compiler.compileNodes(branch.body);
|
|
1326
|
+
compiler.pushRaw(null, '}');
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
const compilers$6 = {
|
|
1330
|
+
[NodeType.IF]: compileIf,
|
|
1331
|
+
};
|
|
1332
|
+
|
|
1333
|
+
async function compileInclude({ val: path, loc }, compiler) {
|
|
1334
|
+
compiler.pushRaw(loc, `{`, `const partial=partials["${path.value}"];`, `if(partial){`, `s+=await partial(c,e,f);`, `}`, `}`);
|
|
1335
|
+
}
|
|
1336
|
+
const compilers$5 = {
|
|
1337
|
+
[NodeType.INCLUDE]: compileInclude,
|
|
1338
|
+
};
|
|
1339
|
+
|
|
1340
|
+
async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
1341
|
+
const { context } = compiler;
|
|
1342
|
+
const { elements } = right;
|
|
1343
|
+
const expCompiler = new ExpCompiler();
|
|
1344
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${(elements).map(el => el.type === 'SET' ? `${el.left.value}=${expCompiler.compile((el.right))}` : el.value).join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements.map(el => el.type === 'SET'
|
|
1345
|
+
? el.left.value
|
|
1346
|
+
: el.value).join(','), `};`);
|
|
1347
|
+
await compiler.compileNodes(body);
|
|
1348
|
+
compiler.out();
|
|
1349
|
+
compiler.pushRaw(null, '};');
|
|
1350
|
+
}
|
|
1351
|
+
function compileCaller({ loc }, compiler) {
|
|
1352
|
+
compiler.pushRaw(loc, `await _c?.();`);
|
|
1353
|
+
}
|
|
1354
|
+
const compilers$4 = {
|
|
1355
|
+
[NodeType.MACRO]: compileMacro,
|
|
1356
|
+
[NodeType.CALLER]: compileCaller,
|
|
1357
|
+
};
|
|
1358
|
+
|
|
1359
|
+
async function compileOutput({ exp }, compiler) {
|
|
1360
|
+
compiler.pushVar(exp.loc, new ExpCompiler().compile(exp, compiler.context));
|
|
1361
|
+
}
|
|
1362
|
+
const compilers$3 = {
|
|
1363
|
+
[NodeType.OUTPUT]: compileOutput,
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
async function compileSet({ val, loc }, compiler) {
|
|
1367
|
+
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1368
|
+
compiler.pushRaw(null, `});`);
|
|
1369
|
+
}
|
|
1370
|
+
const compilers$2 = {
|
|
1371
|
+
[NodeType.SET]: compileSet,
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
async function compileText({ val, loc, strip: { start, end } }, compiler) {
|
|
1375
|
+
if (start || compiler.options.trimWhitespace) {
|
|
1376
|
+
val = val.replace(/^\s+/, '');
|
|
1377
|
+
}
|
|
1378
|
+
if (end || compiler.options.trimWhitespace) {
|
|
1379
|
+
val = val.replace(/\s+$/, '');
|
|
1380
|
+
}
|
|
1381
|
+
compiler.pushStr(loc, val);
|
|
1382
|
+
}
|
|
1383
|
+
const compilers$1 = {
|
|
1384
|
+
[NodeType.TEXT]: compileText,
|
|
1385
|
+
};
|
|
1386
|
+
|
|
1387
|
+
const compilers = {
|
|
1388
|
+
...compilers$6,
|
|
1389
|
+
...compilers$7,
|
|
1390
|
+
...compilers$5,
|
|
1391
|
+
...compilers$b,
|
|
1392
|
+
...compilers$1,
|
|
1393
|
+
...compilers$3,
|
|
1394
|
+
...compilers$8,
|
|
1395
|
+
...compilers$4,
|
|
1396
|
+
...compilers$a,
|
|
1397
|
+
...compilers$2,
|
|
1398
|
+
...compilers$9,
|
|
1399
|
+
};
|
|
1400
|
+
|
|
1401
|
+
const parserOptions = {
|
|
1402
|
+
commentOpen: '{{#',
|
|
1403
|
+
commentClose: '}}',
|
|
1404
|
+
directiveOpen: '{{',
|
|
1405
|
+
directiveClose: '}}',
|
|
1406
|
+
outputOpen: '{{=',
|
|
1407
|
+
outputClose: '}}',
|
|
1408
|
+
parsers,
|
|
1409
|
+
debug: () => { },
|
|
1410
|
+
};
|
|
1411
|
+
const compilerOptions = {
|
|
1412
|
+
...parserOptions,
|
|
1413
|
+
trimWhitespace: false,
|
|
1414
|
+
stripComments: false,
|
|
1415
|
+
compilers,
|
|
1416
|
+
loader: async () => '',
|
|
1417
|
+
};
|
|
1418
|
+
const renderOptions = {
|
|
1419
|
+
...compilerOptions,
|
|
1420
|
+
globals: {},
|
|
1421
|
+
filters: { ...filters },
|
|
1422
|
+
autoEscape: true,
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
/**
|
|
1426
|
+
* Contains the source map information.
|
|
1427
|
+
* @example
|
|
1428
|
+
* - template:
|
|
1429
|
+
* {{ foo }}
|
|
1430
|
+
*
|
|
1431
|
+
* - compiled:
|
|
1432
|
+
* s+=e(foo);
|
|
1433
|
+
*
|
|
1434
|
+
* - sourceMap:
|
|
1435
|
+
* Mapping {
|
|
1436
|
+
* source: {
|
|
1437
|
+
* start: {
|
|
1438
|
+
* line: 2,
|
|
1439
|
+
* column: 4,
|
|
1440
|
+
* },
|
|
1441
|
+
* end: {
|
|
1442
|
+
* line: 3,
|
|
1443
|
+
* column: 5,
|
|
1444
|
+
* },
|
|
1445
|
+
* },
|
|
1446
|
+
* target: {
|
|
1447
|
+
* start: {
|
|
1448
|
+
* line: 2,
|
|
1449
|
+
* column: 3,
|
|
1450
|
+
* },
|
|
1451
|
+
* end: {
|
|
1452
|
+
* line: 3,
|
|
1453
|
+
* column: 8,
|
|
1454
|
+
* },
|
|
1455
|
+
* }
|
|
1456
|
+
* }
|
|
1457
|
+
*/
|
|
1458
|
+
class SourceMap {
|
|
1459
|
+
constructor() {
|
|
1460
|
+
Object.defineProperty(this, "mappings", {
|
|
1461
|
+
enumerable: true,
|
|
1462
|
+
configurable: true,
|
|
1463
|
+
writable: true,
|
|
1464
|
+
value: []
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
addMapping(source, target) {
|
|
1468
|
+
this.mappings.push({
|
|
1469
|
+
source,
|
|
1470
|
+
target,
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
getSourceLoc({ line, column }) {
|
|
1474
|
+
return this.mappings
|
|
1475
|
+
.filter(({ target: { start, end } }) => start.line <= line
|
|
1476
|
+
&& end.line >= line
|
|
1477
|
+
&& (start.line < line || start.column <= column)
|
|
1478
|
+
&& (end.line > line || end.column >= column))
|
|
1479
|
+
.map(({ source }) => source);
|
|
630
1480
|
}
|
|
631
|
-
|
|
632
|
-
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
class OutScript extends SourceMap {
|
|
1484
|
+
constructor() {
|
|
1485
|
+
super(...arguments);
|
|
1486
|
+
Object.defineProperty(this, "code", {
|
|
1487
|
+
enumerable: true,
|
|
1488
|
+
configurable: true,
|
|
1489
|
+
writable: true,
|
|
1490
|
+
value: ''
|
|
1491
|
+
});
|
|
633
1492
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map(arg => arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg))].join(',')}))`;
|
|
1493
|
+
get script() {
|
|
1494
|
+
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
637
1495
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
1496
|
+
start() {
|
|
1497
|
+
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
1498
|
+
}
|
|
1499
|
+
end() {
|
|
1500
|
+
this.pushRaw(null, 'return s;', '})();');
|
|
1501
|
+
}
|
|
1502
|
+
pushRaw(loc, ...lines) {
|
|
1503
|
+
const start = this.code.length;
|
|
1504
|
+
for (const line of lines) {
|
|
1505
|
+
this.code += line;
|
|
642
1506
|
}
|
|
643
|
-
|
|
1507
|
+
if (!loc) {
|
|
1508
|
+
return;
|
|
1509
|
+
}
|
|
1510
|
+
this.addMapping(loc, {
|
|
1511
|
+
start: {
|
|
1512
|
+
line: 1,
|
|
1513
|
+
column: start,
|
|
1514
|
+
},
|
|
1515
|
+
end: {
|
|
1516
|
+
line: 1,
|
|
1517
|
+
column: this.code.length,
|
|
1518
|
+
},
|
|
1519
|
+
});
|
|
644
1520
|
}
|
|
645
|
-
|
|
646
|
-
if (
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
1521
|
+
pushStr(loc, s) {
|
|
1522
|
+
if (s) {
|
|
1523
|
+
this.pushRaw(loc, `s+="${s
|
|
1524
|
+
.replace(/\\/g, '\\\\')
|
|
1525
|
+
.replace(/"/g, '\\"')
|
|
1526
|
+
.replace(/[\n\r]/g, '\\n')}";`);
|
|
652
1527
|
}
|
|
653
|
-
// destructuring
|
|
654
|
-
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
655
1528
|
}
|
|
656
|
-
|
|
657
|
-
|
|
1529
|
+
pushVar(loc, v) {
|
|
1530
|
+
this.pushRaw(loc, `s+=${ESCAPE}(${v});`);
|
|
658
1531
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
class Context extends OutScript {
|
|
1535
|
+
constructor() {
|
|
1536
|
+
super();
|
|
1537
|
+
Object.defineProperty(this, "context", {
|
|
1538
|
+
enumerable: true,
|
|
1539
|
+
configurable: true,
|
|
1540
|
+
writable: true,
|
|
1541
|
+
value: CONTEXT
|
|
1542
|
+
});
|
|
1543
|
+
Object.defineProperty(this, "contexts", {
|
|
1544
|
+
enumerable: true,
|
|
1545
|
+
configurable: true,
|
|
1546
|
+
writable: true,
|
|
1547
|
+
value: [CONTEXT]
|
|
1548
|
+
});
|
|
1549
|
+
Object.defineProperty(this, "index", {
|
|
1550
|
+
enumerable: true,
|
|
1551
|
+
configurable: true,
|
|
1552
|
+
writable: true,
|
|
1553
|
+
value: 0
|
|
1554
|
+
});
|
|
662
1555
|
}
|
|
663
|
-
|
|
664
|
-
|
|
1556
|
+
in() {
|
|
1557
|
+
this.context = `${this.context}_${this.index++}`;
|
|
1558
|
+
this.contexts.push(this.context);
|
|
1559
|
+
return this.context;
|
|
1560
|
+
}
|
|
1561
|
+
out() {
|
|
1562
|
+
if (this.context === CONTEXT) {
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
this.contexts.pop();
|
|
1566
|
+
this.context = this.contexts.at(-1) ?? CONTEXT;
|
|
665
1567
|
}
|
|
666
1568
|
}
|
|
667
1569
|
|
|
@@ -729,36 +1631,39 @@ const expTokenTypes = {
|
|
|
729
1631
|
'.': 'DOT',
|
|
730
1632
|
};
|
|
731
1633
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
1634
|
+
/**
|
|
1635
|
+
* Update position by a given string, and return a new position with the
|
|
1636
|
+
* updated values.
|
|
1637
|
+
*/
|
|
1638
|
+
function updatePosition(str, pos) {
|
|
1639
|
+
const newlines = (str.match(/\n/g) || []).length;
|
|
1640
|
+
if (newlines > 0) {
|
|
1641
|
+
pos.line += newlines;
|
|
1642
|
+
const lastNewline = str.lastIndexOf('\n');
|
|
1643
|
+
pos.column = str.length - lastNewline;
|
|
1644
|
+
}
|
|
1645
|
+
else {
|
|
1646
|
+
pos.column += str.length;
|
|
1647
|
+
}
|
|
1648
|
+
return {
|
|
1649
|
+
line: pos.line,
|
|
1650
|
+
column: pos.column,
|
|
1651
|
+
};
|
|
747
1652
|
}
|
|
748
1653
|
|
|
749
1654
|
class ExpTokenizer {
|
|
750
|
-
constructor() {
|
|
1655
|
+
constructor(template) {
|
|
751
1656
|
Object.defineProperty(this, "template", {
|
|
752
1657
|
enumerable: true,
|
|
753
1658
|
configurable: true,
|
|
754
1659
|
writable: true,
|
|
755
|
-
value:
|
|
1660
|
+
value: template
|
|
756
1661
|
});
|
|
757
|
-
Object.defineProperty(this, "
|
|
1662
|
+
Object.defineProperty(this, "val", {
|
|
758
1663
|
enumerable: true,
|
|
759
1664
|
configurable: true,
|
|
760
1665
|
writable: true,
|
|
761
|
-
value:
|
|
1666
|
+
value: ''
|
|
762
1667
|
});
|
|
763
1668
|
Object.defineProperty(this, "tokens", {
|
|
764
1669
|
enumerable: true,
|
|
@@ -766,22 +1671,42 @@ class ExpTokenizer {
|
|
|
766
1671
|
writable: true,
|
|
767
1672
|
value: []
|
|
768
1673
|
});
|
|
769
|
-
Object.defineProperty(this, "
|
|
1674
|
+
Object.defineProperty(this, "index", {
|
|
770
1675
|
enumerable: true,
|
|
771
1676
|
configurable: true,
|
|
772
1677
|
writable: true,
|
|
773
1678
|
value: 0
|
|
774
1679
|
});
|
|
1680
|
+
Object.defineProperty(this, "line", {
|
|
1681
|
+
enumerable: true,
|
|
1682
|
+
configurable: true,
|
|
1683
|
+
writable: true,
|
|
1684
|
+
value: 1
|
|
1685
|
+
});
|
|
1686
|
+
Object.defineProperty(this, "column", {
|
|
1687
|
+
enumerable: true,
|
|
1688
|
+
configurable: true,
|
|
1689
|
+
writable: true,
|
|
1690
|
+
value: 1
|
|
1691
|
+
});
|
|
775
1692
|
}
|
|
776
|
-
tokenize(
|
|
777
|
-
this.
|
|
778
|
-
this.
|
|
1693
|
+
tokenize(val, loc) {
|
|
1694
|
+
this.val = val;
|
|
1695
|
+
this.index = 0;
|
|
1696
|
+
this.line = loc.start.line;
|
|
1697
|
+
this.column = loc.start.column;
|
|
779
1698
|
this.tokens = [];
|
|
780
|
-
this.
|
|
781
|
-
|
|
782
|
-
const char = this.template[this.cursor];
|
|
1699
|
+
while (this.index < this.val.length) {
|
|
1700
|
+
const char = this.val[this.index];
|
|
783
1701
|
if (isWhitespace(char)) {
|
|
784
|
-
|
|
1702
|
+
if (char === '\n') {
|
|
1703
|
+
this.line++;
|
|
1704
|
+
this.column = 0;
|
|
1705
|
+
}
|
|
1706
|
+
else {
|
|
1707
|
+
this.column++;
|
|
1708
|
+
}
|
|
1709
|
+
this.index++;
|
|
785
1710
|
continue;
|
|
786
1711
|
}
|
|
787
1712
|
if (char === '\'' || char === '"' || char === '`') {
|
|
@@ -800,21 +1725,21 @@ class ExpTokenizer {
|
|
|
800
1725
|
this.readSymbol(char);
|
|
801
1726
|
continue;
|
|
802
1727
|
}
|
|
803
|
-
throw new
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1728
|
+
throw new CompileError(`Unexpect "${char}"`, this.template, {
|
|
1729
|
+
start: { line: this.line, column: this.column },
|
|
1730
|
+
end: { line: this.line, column: this.column + 1 },
|
|
1731
|
+
});
|
|
807
1732
|
}
|
|
808
1733
|
return this.tokens;
|
|
809
1734
|
}
|
|
810
1735
|
readString(quoteChar) {
|
|
811
1736
|
let value = '';
|
|
812
1737
|
let escaped = false;
|
|
813
|
-
const start = this.
|
|
1738
|
+
const start = { line: this.line, column: this.column };
|
|
814
1739
|
// Skip opening quote char
|
|
815
|
-
this.
|
|
816
|
-
while (this.
|
|
817
|
-
const char = this.
|
|
1740
|
+
this.index++;
|
|
1741
|
+
while (this.index < this.val.length) {
|
|
1742
|
+
const char = this.val[this.index];
|
|
818
1743
|
if (escaped) {
|
|
819
1744
|
value += char;
|
|
820
1745
|
escaped = false;
|
|
@@ -829,24 +1754,26 @@ class ExpTokenizer {
|
|
|
829
1754
|
else {
|
|
830
1755
|
value += char;
|
|
831
1756
|
}
|
|
832
|
-
this.
|
|
1757
|
+
this.index++;
|
|
833
1758
|
}
|
|
834
1759
|
// skip closing quote char
|
|
835
|
-
this.
|
|
1760
|
+
this.index++;
|
|
836
1761
|
this.tokens.push({
|
|
837
1762
|
type: 'LIT',
|
|
838
1763
|
value,
|
|
839
|
-
|
|
840
|
-
|
|
1764
|
+
loc: {
|
|
1765
|
+
start,
|
|
1766
|
+
end: updatePosition(value, this),
|
|
1767
|
+
},
|
|
841
1768
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
842
1769
|
});
|
|
843
1770
|
}
|
|
844
1771
|
readNumber() {
|
|
845
1772
|
let value = '';
|
|
846
1773
|
let hasDot = false;
|
|
847
|
-
const start = this.
|
|
848
|
-
while (this.
|
|
849
|
-
const char = this.
|
|
1774
|
+
const start = { line: this.line, column: this.column };
|
|
1775
|
+
while (this.index < this.val.length) {
|
|
1776
|
+
const char = this.val[this.index];
|
|
850
1777
|
if (isDigit(char)) {
|
|
851
1778
|
value += char;
|
|
852
1779
|
}
|
|
@@ -857,24 +1784,26 @@ class ExpTokenizer {
|
|
|
857
1784
|
else {
|
|
858
1785
|
break;
|
|
859
1786
|
}
|
|
860
|
-
this.
|
|
1787
|
+
this.index++;
|
|
861
1788
|
}
|
|
862
1789
|
this.tokens.push({
|
|
863
1790
|
type: 'LIT',
|
|
864
1791
|
value: Number.parseFloat(value),
|
|
865
|
-
|
|
866
|
-
|
|
1792
|
+
loc: {
|
|
1793
|
+
start,
|
|
1794
|
+
end: updatePosition(value, this),
|
|
1795
|
+
},
|
|
867
1796
|
raw: value,
|
|
868
1797
|
});
|
|
869
1798
|
}
|
|
870
1799
|
readIdentifier() {
|
|
871
1800
|
let value = '';
|
|
872
|
-
const start = this.
|
|
873
|
-
while (this.
|
|
874
|
-
const char = this.
|
|
1801
|
+
const start = { line: this.line, column: this.column };
|
|
1802
|
+
while (this.index < this.val.length) {
|
|
1803
|
+
const char = this.val[this.index];
|
|
875
1804
|
if (isIdentifierChar(char)) {
|
|
876
1805
|
value += char;
|
|
877
|
-
this.
|
|
1806
|
+
this.index++;
|
|
878
1807
|
continue;
|
|
879
1808
|
}
|
|
880
1809
|
break;
|
|
@@ -893,21 +1822,25 @@ class ExpTokenizer {
|
|
|
893
1822
|
? undefined
|
|
894
1823
|
: value
|
|
895
1824
|
: value,
|
|
896
|
-
|
|
897
|
-
|
|
1825
|
+
loc: {
|
|
1826
|
+
start,
|
|
1827
|
+
end: updatePosition(value, this),
|
|
1828
|
+
},
|
|
898
1829
|
raw: value,
|
|
899
1830
|
});
|
|
900
1831
|
}
|
|
901
1832
|
readSymbol(char) {
|
|
902
|
-
const start = this.
|
|
1833
|
+
const start = { line: this.line, column: this.column };
|
|
1834
|
+
this.index++;
|
|
903
1835
|
this.tokens.push({
|
|
904
1836
|
type: expTokenTypes[char],
|
|
905
1837
|
value: char,
|
|
906
|
-
|
|
907
|
-
|
|
1838
|
+
loc: {
|
|
1839
|
+
start,
|
|
1840
|
+
end: updatePosition(char, this),
|
|
1841
|
+
},
|
|
908
1842
|
raw: char,
|
|
909
1843
|
});
|
|
910
|
-
this.cursor++;
|
|
911
1844
|
}
|
|
912
1845
|
}
|
|
913
1846
|
function isWhitespace(char) {
|
|
@@ -927,8 +1860,14 @@ function isIdentifierChar(char) {
|
|
|
927
1860
|
}
|
|
928
1861
|
|
|
929
1862
|
class ExpParser {
|
|
930
|
-
constructor() {
|
|
1863
|
+
constructor(template) {
|
|
931
1864
|
Object.defineProperty(this, "template", {
|
|
1865
|
+
enumerable: true,
|
|
1866
|
+
configurable: true,
|
|
1867
|
+
writable: true,
|
|
1868
|
+
value: template
|
|
1869
|
+
});
|
|
1870
|
+
Object.defineProperty(this, "val", {
|
|
932
1871
|
enumerable: true,
|
|
933
1872
|
configurable: true,
|
|
934
1873
|
writable: true,
|
|
@@ -940,25 +1879,32 @@ class ExpParser {
|
|
|
940
1879
|
writable: true,
|
|
941
1880
|
value: []
|
|
942
1881
|
});
|
|
943
|
-
Object.defineProperty(this, "
|
|
1882
|
+
Object.defineProperty(this, "index", {
|
|
944
1883
|
enumerable: true,
|
|
945
1884
|
configurable: true,
|
|
946
1885
|
writable: true,
|
|
947
1886
|
value: 0
|
|
948
1887
|
});
|
|
949
|
-
Object.defineProperty(this, "
|
|
1888
|
+
Object.defineProperty(this, "line", {
|
|
950
1889
|
enumerable: true,
|
|
951
1890
|
configurable: true,
|
|
952
1891
|
writable: true,
|
|
953
|
-
value:
|
|
1892
|
+
value: 1
|
|
1893
|
+
});
|
|
1894
|
+
Object.defineProperty(this, "column", {
|
|
1895
|
+
enumerable: true,
|
|
1896
|
+
configurable: true,
|
|
1897
|
+
writable: true,
|
|
1898
|
+
value: 1
|
|
954
1899
|
});
|
|
955
1900
|
}
|
|
956
|
-
parse(
|
|
957
|
-
this.
|
|
958
|
-
this.
|
|
959
|
-
this.
|
|
960
|
-
this.
|
|
961
|
-
|
|
1901
|
+
parse(val, loc) {
|
|
1902
|
+
this.val = val;
|
|
1903
|
+
this.index = 0;
|
|
1904
|
+
this.line = loc.start.line;
|
|
1905
|
+
this.column = loc.start.column;
|
|
1906
|
+
this.tokens = new ExpTokenizer(this.template).tokenize(val, loc);
|
|
1907
|
+
return this.val ? this.parseExp() : null;
|
|
962
1908
|
}
|
|
963
1909
|
parseExp(checker) {
|
|
964
1910
|
let left = null;
|
|
@@ -977,42 +1923,27 @@ class ExpParser {
|
|
|
977
1923
|
}
|
|
978
1924
|
if (token.type === 'LP') {
|
|
979
1925
|
if (!this.peek()) {
|
|
980
|
-
throw new
|
|
981
|
-
source: this.template,
|
|
982
|
-
range: {
|
|
983
|
-
start: token.start,
|
|
984
|
-
end: token.end,
|
|
985
|
-
},
|
|
986
|
-
});
|
|
1926
|
+
throw new CompileError(`unexpected end of expression`, this.template, token.loc);
|
|
987
1927
|
}
|
|
988
1928
|
const elements = this.parseSequence();
|
|
989
1929
|
const rp = this.consume('RP');
|
|
990
1930
|
if (!rp) {
|
|
991
|
-
throw new
|
|
992
|
-
source: this.template,
|
|
993
|
-
range: {
|
|
994
|
-
start: token.start,
|
|
995
|
-
end: token.end,
|
|
996
|
-
},
|
|
997
|
-
});
|
|
1931
|
+
throw new CompileError(`expected "RP" after "LP"`, this.template, token.loc);
|
|
998
1932
|
}
|
|
999
1933
|
left = {
|
|
1000
1934
|
...token,
|
|
1001
1935
|
type: 'SEQ',
|
|
1002
1936
|
elements,
|
|
1003
|
-
|
|
1937
|
+
loc: {
|
|
1938
|
+
start: token.loc.start,
|
|
1939
|
+
end: rp.loc.end,
|
|
1940
|
+
},
|
|
1004
1941
|
};
|
|
1005
1942
|
continue;
|
|
1006
1943
|
}
|
|
1007
1944
|
if (token.type === 'NOT') {
|
|
1008
1945
|
if (!this.peek()) {
|
|
1009
|
-
throw new
|
|
1010
|
-
source: this.template,
|
|
1011
|
-
range: {
|
|
1012
|
-
start: token.start,
|
|
1013
|
-
end: token.end,
|
|
1014
|
-
},
|
|
1015
|
-
});
|
|
1946
|
+
throw new CompileError(`unexpected end of expression`, this.template, token.loc);
|
|
1016
1947
|
}
|
|
1017
1948
|
left = {
|
|
1018
1949
|
...token,
|
|
@@ -1030,17 +1961,11 @@ class ExpParser {
|
|
|
1030
1961
|
if (this.check('DOT')) {
|
|
1031
1962
|
left.path = this.parsePath();
|
|
1032
1963
|
}
|
|
1033
|
-
|
|
1964
|
+
const lp = this.consume('LP');
|
|
1965
|
+
if (lp) {
|
|
1034
1966
|
left.args = this.parseSequence();
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
throw new ParseError(`expected "RP" after "LP"`, {
|
|
1038
|
-
source: this.template,
|
|
1039
|
-
range: {
|
|
1040
|
-
start: token.start,
|
|
1041
|
-
end: token.end,
|
|
1042
|
-
},
|
|
1043
|
-
});
|
|
1967
|
+
if (!this.consume('RP')) {
|
|
1968
|
+
throw new CompileError(`expected "RP" after "LP"`, this.template, lp.loc);
|
|
1044
1969
|
}
|
|
1045
1970
|
continue;
|
|
1046
1971
|
}
|
|
@@ -1056,13 +1981,7 @@ class ExpParser {
|
|
|
1056
1981
|
if (token.type === 'IF') {
|
|
1057
1982
|
const test = this.parseExp(t => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
1058
1983
|
if (!test) {
|
|
1059
|
-
throw new
|
|
1060
|
-
source: this.template,
|
|
1061
|
-
range: {
|
|
1062
|
-
start: token.start,
|
|
1063
|
-
end: token.end,
|
|
1064
|
-
},
|
|
1065
|
-
});
|
|
1984
|
+
throw new CompileError('expected test expression', this.template, token.loc);
|
|
1066
1985
|
}
|
|
1067
1986
|
left = {
|
|
1068
1987
|
...token,
|
|
@@ -1073,13 +1992,7 @@ class ExpParser {
|
|
|
1073
1992
|
if (this.consume('ELSE')) {
|
|
1074
1993
|
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
1075
1994
|
if (!alternative) {
|
|
1076
|
-
throw new
|
|
1077
|
-
source: this.template,
|
|
1078
|
-
range: {
|
|
1079
|
-
start: token.start,
|
|
1080
|
-
end: token.end,
|
|
1081
|
-
},
|
|
1082
|
-
});
|
|
1995
|
+
throw new CompileError('expected alternative expression', this.template, token.loc);
|
|
1083
1996
|
}
|
|
1084
1997
|
left.alternative = alternative;
|
|
1085
1998
|
}
|
|
@@ -1104,23 +2017,11 @@ class ExpParser {
|
|
|
1104
2017
|
|| token.type === 'MOD'
|
|
1105
2018
|
|| token.type === 'SET') {
|
|
1106
2019
|
if (!left) {
|
|
1107
|
-
throw new
|
|
1108
|
-
source: this.template,
|
|
1109
|
-
range: {
|
|
1110
|
-
start: token.start,
|
|
1111
|
-
end: token.end,
|
|
1112
|
-
},
|
|
1113
|
-
});
|
|
2020
|
+
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
1114
2021
|
}
|
|
1115
2022
|
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
1116
2023
|
if (!right) {
|
|
1117
|
-
throw new
|
|
1118
|
-
source: this.template,
|
|
1119
|
-
range: {
|
|
1120
|
-
start: token.start,
|
|
1121
|
-
end: token.end,
|
|
1122
|
-
},
|
|
1123
|
-
});
|
|
2024
|
+
throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
|
|
1124
2025
|
}
|
|
1125
2026
|
left = {
|
|
1126
2027
|
...token,
|
|
@@ -1132,22 +2033,10 @@ class ExpParser {
|
|
|
1132
2033
|
}
|
|
1133
2034
|
if (token.type === 'PIPE') {
|
|
1134
2035
|
if (!left) {
|
|
1135
|
-
throw new
|
|
1136
|
-
source: this.template,
|
|
1137
|
-
range: {
|
|
1138
|
-
start: token.start,
|
|
1139
|
-
end: token.end,
|
|
1140
|
-
},
|
|
1141
|
-
});
|
|
2036
|
+
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
1142
2037
|
}
|
|
1143
2038
|
if (!this.check('ID')) {
|
|
1144
|
-
throw new
|
|
1145
|
-
source: this.template,
|
|
1146
|
-
range: {
|
|
1147
|
-
start: token.start,
|
|
1148
|
-
end: token.end,
|
|
1149
|
-
},
|
|
1150
|
-
});
|
|
2039
|
+
throw new CompileError(`expected "ID" after "PIPE"`, this.template, token.loc);
|
|
1151
2040
|
}
|
|
1152
2041
|
left = {
|
|
1153
2042
|
...token,
|
|
@@ -1171,712 +2060,646 @@ class ExpParser {
|
|
|
1171
2060
|
}
|
|
1172
2061
|
const token = this.next();
|
|
1173
2062
|
if (!token) {
|
|
1174
|
-
throw new
|
|
1175
|
-
source: this.template,
|
|
1176
|
-
range: {
|
|
1177
|
-
start: dot.start,
|
|
1178
|
-
end: dot.end,
|
|
1179
|
-
},
|
|
1180
|
-
});
|
|
2063
|
+
throw new CompileError(`expected "ID" after "DOT"`, this.template, dot.loc);
|
|
1181
2064
|
}
|
|
1182
2065
|
if (token.type !== 'ID') {
|
|
1183
|
-
throw new
|
|
1184
|
-
source: this.template,
|
|
1185
|
-
range: {
|
|
1186
|
-
start: token.start,
|
|
1187
|
-
end: token.end,
|
|
1188
|
-
},
|
|
1189
|
-
});
|
|
2066
|
+
throw new CompileError(`expected "ID" after "DOT"`, this.template, dot.loc);
|
|
1190
2067
|
}
|
|
1191
2068
|
ids.push({
|
|
1192
|
-
...token,
|
|
1193
|
-
type: 'ID',
|
|
1194
|
-
value: token.value,
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1197
|
-
return ids;
|
|
1198
|
-
}
|
|
1199
|
-
parseSequence() {
|
|
1200
|
-
const elements = [];
|
|
1201
|
-
while (!this.check('RP')) {
|
|
1202
|
-
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
1203
|
-
if (element) {
|
|
1204
|
-
elements.push(element);
|
|
1205
|
-
}
|
|
1206
|
-
else {
|
|
1207
|
-
break;
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
return elements;
|
|
1211
|
-
}
|
|
1212
|
-
parsePipe() {
|
|
1213
|
-
const token = this.next();
|
|
1214
|
-
const right = {
|
|
1215
|
-
...token,
|
|
1216
|
-
type: 'ID',
|
|
1217
|
-
value: token.value,
|
|
1218
|
-
};
|
|
1219
|
-
if (this.consume('LP')) {
|
|
1220
|
-
const args = this.parseSequence();
|
|
1221
|
-
if (args.length) {
|
|
1222
|
-
right.args = args;
|
|
1223
|
-
}
|
|
1224
|
-
const rp = this.consume('RP');
|
|
1225
|
-
if (!rp) {
|
|
1226
|
-
throw new ParseError(`expected "RP" after "LP"`, {
|
|
1227
|
-
source: this.template,
|
|
1228
|
-
range: {
|
|
1229
|
-
start: token.start,
|
|
1230
|
-
end: token.end,
|
|
1231
|
-
},
|
|
1232
|
-
});
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
return right;
|
|
1236
|
-
}
|
|
1237
|
-
next() {
|
|
1238
|
-
return this.tokens[this.cursor++] ?? null;
|
|
1239
|
-
}
|
|
1240
|
-
back() {
|
|
1241
|
-
return this.tokens[this.cursor--];
|
|
1242
|
-
}
|
|
1243
|
-
peek() {
|
|
1244
|
-
return this.tokens[this.cursor] ?? null;
|
|
1245
|
-
}
|
|
1246
|
-
check(type) {
|
|
1247
|
-
return this.peek()?.type === type;
|
|
1248
|
-
}
|
|
1249
|
-
consume(type) {
|
|
1250
|
-
if (this.check(type)) {
|
|
1251
|
-
return this.next();
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
function isHigher(a, b) {
|
|
1256
|
-
return expTokenPrecedences[a.type] > expTokenPrecedences[b.type];
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
const compiler = new ExpCompiler();
|
|
1260
|
-
const parser = new ExpParser();
|
|
1261
|
-
|
|
1262
|
-
const CALL = 'call';
|
|
1263
|
-
const ENDCALL = 'endcall';
|
|
1264
|
-
/**
|
|
1265
|
-
* @example {{ call my_macro(x, "a", 1) }}...{{ endcall }}
|
|
1266
|
-
*/
|
|
1267
|
-
const tag$7 = {
|
|
1268
|
-
names: [CALL, ENDCALL],
|
|
1269
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1270
|
-
if (name === CALL) {
|
|
1271
|
-
if (value?.type !== 'ID') {
|
|
1272
|
-
throw new Error(`"${CALL}" tag must specify a "macro" name`);
|
|
1273
|
-
}
|
|
1274
|
-
ctx.expect(ENDCALL);
|
|
1275
|
-
const { context } = ctx;
|
|
1276
|
-
return out.pushLine(`await ${compiler.compile(value, context, FILTERS)}(async()=>{`);
|
|
1277
|
-
}
|
|
1278
|
-
if (name === ENDCALL) {
|
|
1279
|
-
if (!ctx.consume(ENDCALL)) {
|
|
1280
|
-
throw new Error(`unexpected "${ENDCALL}"`);
|
|
1281
|
-
}
|
|
1282
|
-
return out.pushLine(`});`);
|
|
1283
|
-
}
|
|
1284
|
-
},
|
|
1285
|
-
};
|
|
1286
|
-
|
|
1287
|
-
const COMMENT = 'comment';
|
|
1288
|
-
const ENDCOMMENT = 'endcomment';
|
|
1289
|
-
/**
|
|
1290
|
-
* @example {{# This is a comment }}
|
|
1291
|
-
* @example {{ comment }} This is a comment {{ endcomment }}
|
|
1292
|
-
*/
|
|
1293
|
-
const tag$6 = {
|
|
1294
|
-
names: [COMMENT, '#', ENDCOMMENT],
|
|
1295
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1296
|
-
// inline comment
|
|
1297
|
-
if (name === '#') {
|
|
1298
|
-
if (!value.value || out.options.stripComments) {
|
|
1299
|
-
return;
|
|
1300
|
-
}
|
|
1301
|
-
return out.pushStr(`<!--${value.value}-->`);
|
|
1302
|
-
}
|
|
1303
|
-
// block comment
|
|
1304
|
-
if (name === COMMENT) {
|
|
1305
|
-
ctx.expect(ENDCOMMENT);
|
|
1306
|
-
return out.pushStr(`<!--`);
|
|
1307
|
-
}
|
|
1308
|
-
if (name === ENDCOMMENT) {
|
|
1309
|
-
if (!ctx.consume(ENDCOMMENT)) {
|
|
1310
|
-
throw new Error(`unexpected "${ENDCOMMENT}"`);
|
|
1311
|
-
}
|
|
1312
|
-
return out.pushStr(`-->`);
|
|
1313
|
-
}
|
|
1314
|
-
},
|
|
1315
|
-
};
|
|
1316
|
-
|
|
1317
|
-
const EXPRESSION = '=';
|
|
1318
|
-
/**
|
|
1319
|
-
* @example {{= x | f }}
|
|
1320
|
-
* @example {{= 'a' if x else 'b' }}
|
|
1321
|
-
*/
|
|
1322
|
-
const tag$5 = {
|
|
1323
|
-
names: [EXPRESSION],
|
|
1324
|
-
compile({ tag: { value }, ctx: { context }, out }) {
|
|
1325
|
-
return out.pushVar(compiler.compile(value, context, FILTERS));
|
|
1326
|
-
},
|
|
1327
|
-
};
|
|
1328
|
-
|
|
1329
|
-
const FOR = 'for';
|
|
1330
|
-
const BREAK = 'break';
|
|
1331
|
-
const CONTINUE = 'continue';
|
|
1332
|
-
const ENDFOR = 'endfor';
|
|
1333
|
-
/**
|
|
1334
|
-
* @example {{ for item in items }}{{= item }}{{ endfor }}
|
|
1335
|
-
* @example {{ for (x, y) in items }}{{= x }},{{= y }}{{ endfor }}
|
|
1336
|
-
*/
|
|
1337
|
-
const tag$4 = {
|
|
1338
|
-
names: [FOR, BREAK, CONTINUE, ENDFOR],
|
|
1339
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1340
|
-
if (name === FOR) {
|
|
1341
|
-
if (!value) {
|
|
1342
|
-
throw new Error(`"${FOR}" tag must have expression`);
|
|
1343
|
-
}
|
|
1344
|
-
ctx.expect(ENDFOR);
|
|
1345
|
-
const { context } = ctx;
|
|
1346
|
-
const { type, elements } = value.left;
|
|
1347
|
-
if (type === 'SEQ') {
|
|
1348
|
-
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, elements.map(({ value }) => value).join(','), `};`);
|
|
1349
|
-
}
|
|
1350
|
-
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.value},`, `};`);
|
|
1351
|
-
}
|
|
1352
|
-
if (name === BREAK) {
|
|
1353
|
-
if (!ctx.matchAny(ENDFOR)) {
|
|
1354
|
-
throw new Error(`"${BREAK}" tag must inside a "${FOR}" loop`);
|
|
1355
|
-
}
|
|
1356
|
-
return out.pushLine(`break;`);
|
|
1357
|
-
}
|
|
1358
|
-
if (name === CONTINUE) {
|
|
1359
|
-
if (!ctx.matchAny(ENDFOR)) {
|
|
1360
|
-
throw new Error(`"${CONTINUE}" tag must inside a "${FOR}" loop`);
|
|
1361
|
-
}
|
|
1362
|
-
return out.pushLine(`continue;`);
|
|
1363
|
-
}
|
|
1364
|
-
if (name === ENDFOR) {
|
|
1365
|
-
if (!ctx.consume(ENDFOR)) {
|
|
1366
|
-
throw new Error(`unexpected "${ENDFOR}"`);
|
|
1367
|
-
}
|
|
1368
|
-
ctx.out();
|
|
1369
|
-
return out.pushLine(`}}`);
|
|
1370
|
-
}
|
|
1371
|
-
},
|
|
1372
|
-
};
|
|
1373
|
-
|
|
1374
|
-
const IF = 'if';
|
|
1375
|
-
const ELIF = 'elif';
|
|
1376
|
-
const ELSE = 'else';
|
|
1377
|
-
const ENDIF = 'endif';
|
|
1378
|
-
/**
|
|
1379
|
-
* @example {{ if my_var | my_filter }}yes{{ else }}no{{ endif }}
|
|
1380
|
-
*/
|
|
1381
|
-
const tag$3 = {
|
|
1382
|
-
names: [IF, ELIF, ELSE, ENDIF],
|
|
1383
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1384
|
-
if (name === IF) {
|
|
1385
|
-
if (!value) {
|
|
1386
|
-
throw new Error(`"${IF}" tag must have expression`);
|
|
1387
|
-
}
|
|
1388
|
-
ctx.expect(ENDIF);
|
|
1389
|
-
const { context } = ctx;
|
|
1390
|
-
return out.pushLine(`if(${compiler.compile(value, context, FILTERS)}){`);
|
|
1391
|
-
}
|
|
1392
|
-
if (name === ELIF) {
|
|
1393
|
-
if (!value) {
|
|
1394
|
-
throw new Error(`"${ELIF}" tag must have expression`);
|
|
1395
|
-
}
|
|
1396
|
-
if (!ctx.match(ENDIF)) {
|
|
1397
|
-
throw new Error(`"${ELIF}" tag must follow "${IF}" tag`);
|
|
1398
|
-
}
|
|
1399
|
-
const { context } = ctx;
|
|
1400
|
-
return out.pushLine(`}else if(${compiler.compile(value, context, FILTERS)}){`);
|
|
1401
|
-
}
|
|
1402
|
-
if (name === ELSE) {
|
|
1403
|
-
if (!ctx.match(ENDIF)) {
|
|
1404
|
-
throw new Error(`"${ELSE}" tag must follow "${IF}" tag`);
|
|
1405
|
-
}
|
|
1406
|
-
return out.pushLine(`}else{`);
|
|
1407
|
-
}
|
|
1408
|
-
if (name === ENDIF) {
|
|
1409
|
-
if (!ctx.consume(ENDIF)) {
|
|
1410
|
-
throw new Error(`unexpected "${ENDIF}"`);
|
|
1411
|
-
}
|
|
1412
|
-
return out.pushLine(`}`);
|
|
1413
|
-
}
|
|
1414
|
-
},
|
|
1415
|
-
};
|
|
1416
|
-
|
|
1417
|
-
const MACRO = 'macro';
|
|
1418
|
-
const CALLER = 'caller';
|
|
1419
|
-
const ENDMACRO = 'endmacro';
|
|
1420
|
-
const CALLER_PN = '_c';
|
|
1421
|
-
/**
|
|
1422
|
-
* @example {{ macro my_macro = (x, y) }}...{{ caller }}{{ endmacro }}{{ my_macro("foo", 1) }}
|
|
1423
|
-
*/
|
|
1424
|
-
const tag$2 = {
|
|
1425
|
-
names: [MACRO, CALLER, ENDMACRO],
|
|
1426
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1427
|
-
if (name === MACRO) {
|
|
1428
|
-
if (value?.type !== 'SET') {
|
|
1429
|
-
throw new Error(`"${MACRO}" tag must have "SET" expression`);
|
|
1430
|
-
}
|
|
1431
|
-
ctx.expect(ENDMACRO);
|
|
1432
|
-
const { context } = ctx;
|
|
1433
|
-
const { elements } = value.right;
|
|
1434
|
-
return out.pushLine(`${compiler.compile(value, context, FILTERS)}=>async(${CALLER_PN})=>{`, `const ${ctx.in()}={`, `...${context},`, elements.map(el => el.type === 'SET'
|
|
1435
|
-
? el.left.value
|
|
1436
|
-
: el.value).join(','), `};`);
|
|
2069
|
+
...token,
|
|
2070
|
+
type: 'ID',
|
|
2071
|
+
value: token.value,
|
|
2072
|
+
});
|
|
1437
2073
|
}
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
2074
|
+
return ids;
|
|
2075
|
+
}
|
|
2076
|
+
parseSequence() {
|
|
2077
|
+
const elements = [];
|
|
2078
|
+
while (!this.check('RP')) {
|
|
2079
|
+
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2080
|
+
if (element) {
|
|
2081
|
+
elements.push(element);
|
|
1441
2082
|
}
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
if (name === ENDMACRO) {
|
|
1445
|
-
if (!ctx.consume(ENDMACRO)) {
|
|
1446
|
-
throw new Error(`unexpected "${ENDMACRO}"`);
|
|
2083
|
+
else {
|
|
2084
|
+
break;
|
|
1447
2085
|
}
|
|
1448
|
-
ctx.out();
|
|
1449
|
-
return out.pushLine(`};`);
|
|
1450
2086
|
}
|
|
1451
|
-
|
|
1452
|
-
}
|
|
1453
|
-
|
|
1454
|
-
const
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
},
|
|
1466
|
-
};
|
|
1467
|
-
|
|
1468
|
-
const SET = 'set';
|
|
1469
|
-
const ENDSET = 'endset';
|
|
1470
|
-
/**
|
|
1471
|
-
* @example {{ set left = right }}
|
|
1472
|
-
* @example {{ set variable }} this is {{= my_value }} {{ endset }}
|
|
1473
|
-
*/
|
|
1474
|
-
const tag = {
|
|
1475
|
-
names: [SET, ENDSET],
|
|
1476
|
-
async compile({ tag: { name, value }, ctx, out }) {
|
|
1477
|
-
if (name === SET) {
|
|
1478
|
-
if (!value) {
|
|
1479
|
-
throw new Error(`"${SET}" tag must have expression`);
|
|
2087
|
+
return elements;
|
|
2088
|
+
}
|
|
2089
|
+
parsePipe() {
|
|
2090
|
+
const token = this.next();
|
|
2091
|
+
const right = {
|
|
2092
|
+
...token,
|
|
2093
|
+
type: 'ID',
|
|
2094
|
+
value: token.value,
|
|
2095
|
+
};
|
|
2096
|
+
const lp = this.consume('LP');
|
|
2097
|
+
if (lp) {
|
|
2098
|
+
const args = this.parseSequence();
|
|
2099
|
+
if (args.length) {
|
|
2100
|
+
right.args = args;
|
|
1480
2101
|
}
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
if (value.right) {
|
|
1484
|
-
return out.pushLine(compiler.compile(value, CONTEXT, FILTERS));
|
|
2102
|
+
if (!this.consume('RP')) {
|
|
2103
|
+
throw new CompileError(`expected "RP" after "LP"`, this.template, lp.loc);
|
|
1485
2104
|
}
|
|
1486
|
-
ctx.expect(ENDSET);
|
|
1487
|
-
// block assignment
|
|
1488
|
-
return out.pushLine(`Object.assign(${context},{`, `${value.value}:await(async(s)=>{`);
|
|
1489
2105
|
}
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
2106
|
+
return right;
|
|
2107
|
+
}
|
|
2108
|
+
next() {
|
|
2109
|
+
return this.tokens[this.index++] ?? null;
|
|
2110
|
+
}
|
|
2111
|
+
back() {
|
|
2112
|
+
return this.tokens[this.index--];
|
|
2113
|
+
}
|
|
2114
|
+
peek() {
|
|
2115
|
+
return this.tokens.at(this.index) ?? null;
|
|
2116
|
+
}
|
|
2117
|
+
check(type) {
|
|
2118
|
+
return this.peek()?.type === type;
|
|
2119
|
+
}
|
|
2120
|
+
consume(type) {
|
|
2121
|
+
if (this.check(type)) {
|
|
2122
|
+
return this.next();
|
|
1495
2123
|
}
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
tag$7,
|
|
1502
|
-
tag$6,
|
|
1503
|
-
tag$5,
|
|
1504
|
-
tag$4,
|
|
1505
|
-
tag$3,
|
|
1506
|
-
tag$2,
|
|
1507
|
-
tag$1,
|
|
1508
|
-
].reduce((acc, compiler) => {
|
|
1509
|
-
for (const name of compiler.names) {
|
|
1510
|
-
acc[name] = [...(acc[name] || []), compiler];
|
|
1511
|
-
}
|
|
1512
|
-
return acc;
|
|
1513
|
-
}, {});
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
function isHigher(a, b) {
|
|
2127
|
+
return expTokenPrecedences[a.type] > expTokenPrecedences[b.type];
|
|
2128
|
+
}
|
|
1514
2129
|
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
strictMode: true,
|
|
1523
|
-
stripComments: false,
|
|
1524
|
-
trimWhitespace: false,
|
|
1525
|
-
};
|
|
2130
|
+
var TokenType;
|
|
2131
|
+
(function (TokenType) {
|
|
2132
|
+
TokenType["TEXT"] = "TEXT";
|
|
2133
|
+
TokenType["OUTPUT"] = "OUTPUT";
|
|
2134
|
+
TokenType["DIRECTIVE"] = "DIRECTIVE";
|
|
2135
|
+
TokenType["COMMENT"] = "COMMENT";
|
|
2136
|
+
})(TokenType || (TokenType = {}));
|
|
1526
2137
|
|
|
1527
2138
|
function unescapeTag(v) {
|
|
1528
|
-
return v.replace(/\\(
|
|
2139
|
+
return v.replace(/\\([{}])/g, '$1');
|
|
1529
2140
|
}
|
|
1530
2141
|
|
|
1531
|
-
class
|
|
2142
|
+
class Tokenizer {
|
|
1532
2143
|
constructor(options) {
|
|
1533
2144
|
Object.defineProperty(this, "options", {
|
|
1534
2145
|
enumerable: true,
|
|
1535
2146
|
configurable: true,
|
|
1536
2147
|
writable: true,
|
|
1537
|
-
value:
|
|
2148
|
+
value: void 0
|
|
1538
2149
|
});
|
|
1539
2150
|
Object.defineProperty(this, "template", {
|
|
1540
2151
|
enumerable: true,
|
|
1541
2152
|
configurable: true,
|
|
1542
2153
|
writable: true,
|
|
1543
|
-
value:
|
|
2154
|
+
value: void 0
|
|
2155
|
+
});
|
|
2156
|
+
Object.defineProperty(this, "length", {
|
|
2157
|
+
enumerable: true,
|
|
2158
|
+
configurable: true,
|
|
2159
|
+
writable: true,
|
|
2160
|
+
value: void 0
|
|
1544
2161
|
});
|
|
1545
2162
|
Object.defineProperty(this, "index", {
|
|
1546
2163
|
enumerable: true,
|
|
1547
2164
|
configurable: true,
|
|
1548
2165
|
writable: true,
|
|
1549
|
-
value: 0
|
|
2166
|
+
value: void 0
|
|
1550
2167
|
});
|
|
1551
|
-
Object.defineProperty(this, "
|
|
2168
|
+
Object.defineProperty(this, "tokens", {
|
|
1552
2169
|
enumerable: true,
|
|
1553
2170
|
configurable: true,
|
|
1554
2171
|
writable: true,
|
|
1555
|
-
value:
|
|
2172
|
+
value: void 0
|
|
1556
2173
|
});
|
|
1557
|
-
Object.defineProperty(this, "
|
|
2174
|
+
Object.defineProperty(this, "tokenPatterns", {
|
|
1558
2175
|
enumerable: true,
|
|
1559
2176
|
configurable: true,
|
|
1560
2177
|
writable: true,
|
|
1561
|
-
value:
|
|
2178
|
+
value: void 0
|
|
1562
2179
|
});
|
|
1563
|
-
Object.defineProperty(this, "
|
|
2180
|
+
Object.defineProperty(this, "line", {
|
|
1564
2181
|
enumerable: true,
|
|
1565
2182
|
configurable: true,
|
|
1566
2183
|
writable: true,
|
|
1567
|
-
value:
|
|
2184
|
+
value: 1
|
|
1568
2185
|
});
|
|
1569
|
-
Object.defineProperty(this, "
|
|
2186
|
+
Object.defineProperty(this, "column", {
|
|
1570
2187
|
enumerable: true,
|
|
1571
2188
|
configurable: true,
|
|
1572
2189
|
writable: true,
|
|
1573
|
-
value:
|
|
2190
|
+
value: 1
|
|
1574
2191
|
});
|
|
2192
|
+
this.options = {
|
|
2193
|
+
...parserOptions,
|
|
2194
|
+
...options,
|
|
2195
|
+
};
|
|
2196
|
+
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2197
|
+
this.tokenPatterns = ([
|
|
2198
|
+
[
|
|
2199
|
+
TokenType.COMMENT,
|
|
2200
|
+
commentOpen,
|
|
2201
|
+
commentClose,
|
|
2202
|
+
],
|
|
2203
|
+
[
|
|
2204
|
+
TokenType.DIRECTIVE,
|
|
2205
|
+
directiveOpen,
|
|
2206
|
+
directiveClose,
|
|
2207
|
+
],
|
|
2208
|
+
[
|
|
2209
|
+
TokenType.OUTPUT,
|
|
2210
|
+
outputOpen,
|
|
2211
|
+
outputClose,
|
|
2212
|
+
],
|
|
2213
|
+
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1));
|
|
1575
2214
|
}
|
|
1576
|
-
|
|
2215
|
+
tokenize(template) {
|
|
1577
2216
|
this.template = template;
|
|
2217
|
+
this.length = template.length;
|
|
1578
2218
|
this.index = 0;
|
|
1579
|
-
this.
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
while (
|
|
1583
|
-
if (
|
|
1584
|
-
|
|
2219
|
+
this.line = 1;
|
|
2220
|
+
this.column = 1;
|
|
2221
|
+
this.tokens = [];
|
|
2222
|
+
while (this.index < this.length) {
|
|
2223
|
+
if (this.consumeSpecialTokens()) {
|
|
2224
|
+
continue;
|
|
1585
2225
|
}
|
|
1586
|
-
|
|
1587
|
-
this.index = match.index + match[0].length;
|
|
1588
|
-
}
|
|
1589
|
-
if (this.index < template.length) {
|
|
1590
|
-
this.addRaw(this.index, template.length);
|
|
1591
|
-
}
|
|
1592
|
-
this.cursor = this.first;
|
|
1593
|
-
this.rebuildBlocks();
|
|
1594
|
-
return this.cursor;
|
|
1595
|
-
}
|
|
1596
|
-
addRaw(start, end) {
|
|
1597
|
-
this.push({
|
|
1598
|
-
name: RAW$1,
|
|
1599
|
-
raw: this.template.slice(start, end),
|
|
1600
|
-
previous: null,
|
|
1601
|
-
next: null,
|
|
1602
|
-
start,
|
|
1603
|
-
end,
|
|
1604
|
-
});
|
|
1605
|
-
}
|
|
1606
|
-
async addTag(match) {
|
|
1607
|
-
const start = match.index;
|
|
1608
|
-
const end = start + match[0].length;
|
|
1609
|
-
const base = {
|
|
1610
|
-
raw: match[0],
|
|
1611
|
-
previous: null,
|
|
1612
|
-
next: null,
|
|
1613
|
-
stripBefore: match[1] === '-',
|
|
1614
|
-
stripAfter: match[4] === '-',
|
|
1615
|
-
start,
|
|
1616
|
-
end,
|
|
1617
|
-
};
|
|
1618
|
-
if (match[2] === '#') {
|
|
1619
|
-
this.push({
|
|
1620
|
-
...base,
|
|
1621
|
-
name: match[2],
|
|
1622
|
-
value: {
|
|
1623
|
-
type: 'LIT',
|
|
1624
|
-
value: unescapeTag(match[3]),
|
|
1625
|
-
},
|
|
1626
|
-
});
|
|
1627
|
-
}
|
|
1628
|
-
else if (match[2] === '=') {
|
|
1629
|
-
this.push({
|
|
1630
|
-
...base,
|
|
1631
|
-
name: match[2],
|
|
1632
|
-
value: parser.parse(unescapeTag(match[3])),
|
|
1633
|
-
});
|
|
2226
|
+
this.consumeTextToken();
|
|
1634
2227
|
}
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
2228
|
+
return this.tokens;
|
|
2229
|
+
}
|
|
2230
|
+
consumeSpecialTokens() {
|
|
2231
|
+
return this.tokenPatterns.some(([type, openMarker, closeMarker]) => {
|
|
2232
|
+
if (!this.lookahead(openMarker)) {
|
|
2233
|
+
return false;
|
|
1639
2234
|
}
|
|
1640
|
-
|
|
1641
|
-
|
|
2235
|
+
const start = {
|
|
2236
|
+
line: this.line,
|
|
2237
|
+
column: this.column,
|
|
2238
|
+
};
|
|
2239
|
+
const endIndex = this.template.indexOf(closeMarker, this.index + openMarker.length);
|
|
2240
|
+
if (endIndex === -1) {
|
|
2241
|
+
throw new CompileError(`Unclosed "${openMarker}"`, this.template, {
|
|
2242
|
+
start,
|
|
2243
|
+
end: updatePosition(openMarker, { ...start }),
|
|
2244
|
+
});
|
|
1642
2245
|
}
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
2246
|
+
const stripBefore = this.lookahead('-', openMarker.length);
|
|
2247
|
+
const stripAfter = this.template[endIndex - 1] === '-';
|
|
2248
|
+
const val = this.template.slice(this.index + openMarker.length + +stripBefore, endIndex - +stripAfter);
|
|
2249
|
+
const end = updatePosition(this.template.slice(this.index, (this.index = endIndex + closeMarker.length)), this);
|
|
2250
|
+
const token = {
|
|
2251
|
+
type,
|
|
2252
|
+
val: unescapeTag(val),
|
|
2253
|
+
strip: {
|
|
2254
|
+
before: stripBefore,
|
|
2255
|
+
after: stripAfter,
|
|
2256
|
+
},
|
|
2257
|
+
loc: {
|
|
2258
|
+
start,
|
|
2259
|
+
end,
|
|
2260
|
+
},
|
|
2261
|
+
};
|
|
2262
|
+
if (type === TokenType.DIRECTIVE) {
|
|
2263
|
+
let i = 0;
|
|
2264
|
+
let name = '';
|
|
2265
|
+
while (i < val.length) {
|
|
2266
|
+
const char = val[i];
|
|
2267
|
+
if (/\S/.test(char)) {
|
|
2268
|
+
name += char;
|
|
2269
|
+
}
|
|
2270
|
+
else if (name) {
|
|
2271
|
+
break;
|
|
2272
|
+
}
|
|
2273
|
+
i++;
|
|
2274
|
+
}
|
|
2275
|
+
Object.assign(token, {
|
|
1646
2276
|
name,
|
|
1647
|
-
|
|
2277
|
+
expression: i === val.length || /^\s*$/m.test(val.slice(i + 1))
|
|
2278
|
+
? null
|
|
2279
|
+
: {
|
|
2280
|
+
val: val.slice(i + 1),
|
|
2281
|
+
loc: {
|
|
2282
|
+
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, { ...start })),
|
|
2283
|
+
end: {
|
|
2284
|
+
line: this.line,
|
|
2285
|
+
column: this.column - closeMarker.length - +stripAfter,
|
|
2286
|
+
},
|
|
2287
|
+
},
|
|
2288
|
+
},
|
|
1648
2289
|
});
|
|
1649
2290
|
}
|
|
1650
|
-
|
|
2291
|
+
this.tokens.push(token);
|
|
2292
|
+
return true;
|
|
2293
|
+
});
|
|
1651
2294
|
}
|
|
1652
|
-
|
|
1653
|
-
const
|
|
1654
|
-
if (
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
2295
|
+
consumeTextToken() {
|
|
2296
|
+
const nextSpecialIndex = this.findNextSpecialIndex();
|
|
2297
|
+
if (nextSpecialIndex > this.index) {
|
|
2298
|
+
const start = {
|
|
2299
|
+
line: this.line,
|
|
2300
|
+
column: this.column,
|
|
2301
|
+
};
|
|
2302
|
+
const val = this.template.slice(this.index, nextSpecialIndex);
|
|
2303
|
+
this.tokens.push({
|
|
2304
|
+
type: TokenType.TEXT,
|
|
2305
|
+
val: unescapeTag(val),
|
|
2306
|
+
loc: {
|
|
2307
|
+
start,
|
|
2308
|
+
end: updatePosition(val, this),
|
|
1660
2309
|
},
|
|
2310
|
+
strip: {},
|
|
1661
2311
|
});
|
|
2312
|
+
this.index = nextSpecialIndex;
|
|
1662
2313
|
}
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
async include(value) {
|
|
1666
|
-
const exp = parser.parse(value.replace(/\?$/, ''));
|
|
1667
|
-
if (exp?.type !== 'LIT' || typeof exp.value !== 'string' || !exp.value) {
|
|
1668
|
-
throw new ParseError(`"include" tag must have a file path`, {
|
|
1669
|
-
source: this.template,
|
|
1670
|
-
range: {
|
|
1671
|
-
start: this.index,
|
|
1672
|
-
end: this.index + value.length,
|
|
1673
|
-
},
|
|
1674
|
-
});
|
|
2314
|
+
else {
|
|
2315
|
+
updatePosition(this.template[this.index++], this);
|
|
1675
2316
|
}
|
|
1676
|
-
await this.child(`partials/${exp.value}.janja`, value.endsWith('?'));
|
|
1677
2317
|
}
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
try {
|
|
1681
|
-
content = await this.options.loader(path);
|
|
1682
|
-
}
|
|
1683
|
-
catch (error) {
|
|
1684
|
-
if (optional) {
|
|
1685
|
-
return;
|
|
1686
|
-
}
|
|
1687
|
-
throw error;
|
|
1688
|
-
}
|
|
1689
|
-
if (!content) {
|
|
1690
|
-
return;
|
|
1691
|
-
}
|
|
1692
|
-
let cursor = await new Parser(this.options).parse(content);
|
|
1693
|
-
while (cursor) {
|
|
1694
|
-
this.push(cursor);
|
|
1695
|
-
cursor = cursor.next;
|
|
1696
|
-
}
|
|
2318
|
+
lookahead(marker, offset = 0) {
|
|
2319
|
+
return this.template.startsWith(marker, this.index + offset);
|
|
1697
2320
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
2321
|
+
findNextSpecialIndex() {
|
|
2322
|
+
const { outputOpen, directiveOpen, commentOpen, } = this.options;
|
|
2323
|
+
const commentIndex = this.template.indexOf(commentOpen, this.index);
|
|
2324
|
+
const directiveIndex = this.template.indexOf(directiveOpen, this.index);
|
|
2325
|
+
const outputIndex = this.template.indexOf(outputOpen, this.index);
|
|
2326
|
+
const indexes = [
|
|
2327
|
+
commentIndex,
|
|
2328
|
+
directiveIndex,
|
|
2329
|
+
outputIndex,
|
|
2330
|
+
]
|
|
2331
|
+
.filter(index => index !== -1)
|
|
2332
|
+
.sort((a, b) => a - b);
|
|
2333
|
+
return indexes.length ? indexes[0] : this.length;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
class Parser extends Tokenizer {
|
|
2338
|
+
constructor() {
|
|
2339
|
+
super(...arguments);
|
|
2340
|
+
Object.defineProperty(this, "expParser", {
|
|
2341
|
+
enumerable: true,
|
|
2342
|
+
configurable: true,
|
|
2343
|
+
writable: true,
|
|
2344
|
+
value: void 0
|
|
2345
|
+
});
|
|
2346
|
+
Object.defineProperty(this, "cursor", {
|
|
2347
|
+
enumerable: true,
|
|
2348
|
+
configurable: true,
|
|
2349
|
+
writable: true,
|
|
2350
|
+
value: void 0
|
|
2351
|
+
});
|
|
2352
|
+
}
|
|
2353
|
+
parse(template) {
|
|
2354
|
+
this.tokenize(template);
|
|
2355
|
+
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2356
|
+
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2357
|
+
this.expParser = new ExpParser(template);
|
|
2358
|
+
this.cursor = 0;
|
|
2359
|
+
return new TemplateNode(this.parseUntil(), { start, end });
|
|
2360
|
+
}
|
|
2361
|
+
parseUntil(names) {
|
|
2362
|
+
const nodes = [];
|
|
2363
|
+
let prevToken = null;
|
|
2364
|
+
while (true) {
|
|
2365
|
+
const token = this.peek();
|
|
2366
|
+
if (!token
|
|
2367
|
+
|| (token.type === TokenType.DIRECTIVE
|
|
2368
|
+
&& names?.includes(token.name.toLowerCase()))) {
|
|
2369
|
+
break;
|
|
1709
2370
|
}
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
if (this.blocks[value]) {
|
|
1713
|
-
this.blocks[value].push([tag]);
|
|
1714
|
-
return;
|
|
2371
|
+
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
2372
|
+
token.strip.start = true;
|
|
1715
2373
|
}
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
}
|
|
1719
|
-
else if (this.group) {
|
|
1720
|
-
const chunks = this.blocks[this.group];
|
|
1721
|
-
if (tag.name === ENDBLOCK) {
|
|
1722
|
-
this.group = '';
|
|
2374
|
+
if (token.strip?.before && prevToken?.type === TokenType.TEXT) {
|
|
2375
|
+
prevToken.strip.end = true;
|
|
1723
2376
|
}
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
2377
|
+
switch (token.type) {
|
|
2378
|
+
case TokenType.COMMENT:
|
|
2379
|
+
nodes.push(this.createComment(token));
|
|
2380
|
+
break;
|
|
2381
|
+
case TokenType.DIRECTIVE:
|
|
2382
|
+
nodes.push(this.createDirective(token));
|
|
2383
|
+
break;
|
|
2384
|
+
case TokenType.OUTPUT:
|
|
2385
|
+
nodes.push(this.createOutput(token));
|
|
2386
|
+
break;
|
|
2387
|
+
case TokenType.TEXT:
|
|
2388
|
+
nodes.push(this.createText(token));
|
|
2389
|
+
break;
|
|
1728
2390
|
}
|
|
2391
|
+
prevToken = token;
|
|
2392
|
+
}
|
|
2393
|
+
return nodes;
|
|
2394
|
+
}
|
|
2395
|
+
createComment({ val, loc, strip }) {
|
|
2396
|
+
this.advance();
|
|
2397
|
+
return new CommentNode(val, loc, strip);
|
|
2398
|
+
}
|
|
2399
|
+
createOutput({ val, loc, strip }) {
|
|
2400
|
+
this.advance();
|
|
2401
|
+
return new OutputNode(val, loc, strip, this.parseExp({ val, loc }));
|
|
2402
|
+
}
|
|
2403
|
+
createText({ val, loc, strip }) {
|
|
2404
|
+
this.advance();
|
|
2405
|
+
return new TextNode(val, loc, strip);
|
|
2406
|
+
}
|
|
2407
|
+
createDirective(token) {
|
|
2408
|
+
return this.options.parsers[token.name.toLowerCase()]?.(token, this)
|
|
2409
|
+
?? this.createUnknownDirective(token);
|
|
2410
|
+
}
|
|
2411
|
+
createUnexpectedDirective(token) {
|
|
2412
|
+
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
2413
|
+
this.advance();
|
|
2414
|
+
return new UnexpectedDirectiveNode(token.name, token.val, token.loc, token.strip);
|
|
2415
|
+
}
|
|
2416
|
+
createUnknownDirective(token) {
|
|
2417
|
+
this.options.debug?.(new CompileError(`Unknown "${token.name}" directive`, this.template, token.loc));
|
|
2418
|
+
this.advance();
|
|
2419
|
+
return new UnknownDirectiveNode(token.name, token.val, token.loc, token.strip);
|
|
2420
|
+
}
|
|
2421
|
+
parseExp({ val, loc }) {
|
|
2422
|
+
return this.expParser.parse(val, loc);
|
|
2423
|
+
}
|
|
2424
|
+
peek() {
|
|
2425
|
+
return this.tokens.at(this.cursor) ?? null;
|
|
2426
|
+
}
|
|
2427
|
+
advance() {
|
|
2428
|
+
this.cursor++;
|
|
2429
|
+
}
|
|
2430
|
+
match(name) {
|
|
2431
|
+
return this.peek()?.name === name;
|
|
2432
|
+
}
|
|
2433
|
+
isDirective(names) {
|
|
2434
|
+
const token = this.peek();
|
|
2435
|
+
if (token?.type !== TokenType.DIRECTIVE) {
|
|
2436
|
+
return false;
|
|
1729
2437
|
}
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
2438
|
+
return names.includes(token.name.toLowerCase());
|
|
2439
|
+
}
|
|
2440
|
+
requireExpression({ expression, name, loc }) {
|
|
2441
|
+
if (!expression) {
|
|
2442
|
+
throw this.options.debug?.(new CompileError(`"${name}" requires expression`, this.template, loc));
|
|
1733
2443
|
}
|
|
1734
|
-
|
|
1735
|
-
|
|
2444
|
+
}
|
|
2445
|
+
requireNoExpression({ expression, name, loc }) {
|
|
2446
|
+
if (expression) {
|
|
2447
|
+
throw this.options.debug?.(new CompileError(`"${name}" should not have expression`, this.template, loc));
|
|
1736
2448
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
class Compiler extends Context {
|
|
2453
|
+
constructor(options) {
|
|
2454
|
+
super();
|
|
2455
|
+
Object.defineProperty(this, "options", {
|
|
2456
|
+
enumerable: true,
|
|
2457
|
+
configurable: true,
|
|
2458
|
+
writable: true,
|
|
2459
|
+
value: void 0
|
|
2460
|
+
});
|
|
2461
|
+
Object.defineProperty(this, "template", {
|
|
2462
|
+
enumerable: true,
|
|
2463
|
+
configurable: true,
|
|
2464
|
+
writable: true,
|
|
2465
|
+
value: void 0
|
|
2466
|
+
});
|
|
2467
|
+
Object.defineProperty(this, "root", {
|
|
2468
|
+
enumerable: true,
|
|
2469
|
+
configurable: true,
|
|
2470
|
+
writable: true,
|
|
2471
|
+
value: void 0
|
|
2472
|
+
});
|
|
2473
|
+
Object.defineProperty(this, "partials", {
|
|
2474
|
+
enumerable: true,
|
|
2475
|
+
configurable: true,
|
|
2476
|
+
writable: true,
|
|
2477
|
+
value: void 0
|
|
2478
|
+
});
|
|
2479
|
+
Object.defineProperty(this, "blocks", {
|
|
2480
|
+
enumerable: true,
|
|
2481
|
+
configurable: true,
|
|
2482
|
+
writable: true,
|
|
2483
|
+
value: void 0
|
|
2484
|
+
});
|
|
2485
|
+
Object.defineProperty(this, "templateNode", {
|
|
2486
|
+
enumerable: true,
|
|
2487
|
+
configurable: true,
|
|
2488
|
+
writable: true,
|
|
2489
|
+
value: void 0
|
|
2490
|
+
});
|
|
2491
|
+
Object.defineProperty(this, "state", {
|
|
2492
|
+
enumerable: true,
|
|
2493
|
+
configurable: true,
|
|
2494
|
+
writable: true,
|
|
2495
|
+
value: void 0
|
|
2496
|
+
});
|
|
2497
|
+
this.options = {
|
|
2498
|
+
...compilerOptions,
|
|
2499
|
+
...options,
|
|
2500
|
+
};
|
|
2501
|
+
}
|
|
2502
|
+
async compile(template, root = true, blocks = new Map()) {
|
|
2503
|
+
this.template = template;
|
|
2504
|
+
this.root = root;
|
|
2505
|
+
this.partials = new Map();
|
|
2506
|
+
this.blocks = blocks;
|
|
2507
|
+
this.templateNode = new Parser(this.options).parse(template);
|
|
2508
|
+
this.state = {};
|
|
2509
|
+
this.start();
|
|
2510
|
+
if (this.root) {
|
|
2511
|
+
await this.parseTemplates();
|
|
2512
|
+
if (this.partials.size > 0) {
|
|
2513
|
+
this.pushRaw(null, `const partials = {`);
|
|
2514
|
+
for (const [path, code] of this.partials) {
|
|
2515
|
+
this.pushRaw(null, `"${path}":async function r(c,e,f){`, code, `},`);
|
|
2516
|
+
}
|
|
2517
|
+
this.pushRaw(null, `};`);
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
await this.compileNode(this.templateNode);
|
|
2521
|
+
this.end();
|
|
2522
|
+
return this;
|
|
2523
|
+
}
|
|
2524
|
+
async parseTemplates() {
|
|
2525
|
+
const astMap = new Map();
|
|
2526
|
+
const queue = [{ path: '$$', node: null, template: this.template }];
|
|
2527
|
+
while (queue.length > 0) {
|
|
2528
|
+
const { path, node, template } = queue.shift();
|
|
2529
|
+
if (!astMap.has(path)) {
|
|
2530
|
+
astMap.set(path, [node, template]);
|
|
2531
|
+
}
|
|
2532
|
+
const { includes, blocks } = this.collectIncludesAndBlocks();
|
|
2533
|
+
for (const [path, node] of includes) {
|
|
2534
|
+
if (!astMap.has(path)) {
|
|
2535
|
+
try {
|
|
2536
|
+
queue.push({
|
|
2537
|
+
path,
|
|
2538
|
+
node,
|
|
2539
|
+
template: await this.options.loader(path),
|
|
2540
|
+
});
|
|
2541
|
+
}
|
|
2542
|
+
catch {
|
|
2543
|
+
this.options.debug?.(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2544
|
+
return;
|
|
2545
|
+
}
|
|
1753
2546
|
}
|
|
1754
|
-
previous.next = null;
|
|
1755
|
-
}
|
|
1756
|
-
else {
|
|
1757
|
-
this.first = null;
|
|
1758
|
-
this.cursor = null;
|
|
1759
2547
|
}
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
2548
|
+
for (const block of blocks) {
|
|
2549
|
+
const { val: { value } } = block;
|
|
2550
|
+
if (!this.blocks.has(value)) {
|
|
2551
|
+
this.blocks.set(value, []);
|
|
1763
2552
|
}
|
|
1764
|
-
|
|
2553
|
+
this.blocks.get(value).push(block);
|
|
1765
2554
|
}
|
|
1766
|
-
const body = this.rebuildBlockBody(chunks.pop(), chunks);
|
|
1767
|
-
this.rebuildBlockChain(body, previous, next);
|
|
1768
2555
|
}
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
2556
|
+
astMap.delete('$$');
|
|
2557
|
+
for (const [path, [node, template]] of astMap) {
|
|
2558
|
+
if (!this.partials.has(path)) {
|
|
2559
|
+
await this.compilePartial(path, node, template, this.blocks);
|
|
2560
|
+
}
|
|
1773
2561
|
}
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
2562
|
+
}
|
|
2563
|
+
collectIncludesAndBlocks() {
|
|
2564
|
+
const includes = new Map();
|
|
2565
|
+
const blocks = [];
|
|
2566
|
+
const traverse = (node) => {
|
|
2567
|
+
if (node.type === NodeType.INCLUDE) {
|
|
2568
|
+
if (!includes.has(node.val.value)) {
|
|
2569
|
+
includes.set(node.val.value, node);
|
|
2570
|
+
}
|
|
1778
2571
|
}
|
|
1779
|
-
|
|
1780
|
-
|
|
2572
|
+
if (node.type === NodeType.BLOCK) {
|
|
2573
|
+
blocks.push(node);
|
|
1781
2574
|
}
|
|
1782
|
-
|
|
1783
|
-
|
|
2575
|
+
if (node.type === NodeType.TEMPLATE) {
|
|
2576
|
+
node.children.forEach(traverse);
|
|
2577
|
+
}
|
|
2578
|
+
if (node.type === NodeType.IF) {
|
|
2579
|
+
const ifNode = node;
|
|
2580
|
+
ifNode.body.forEach(traverse);
|
|
2581
|
+
ifNode.alternatives.forEach((alt) => {
|
|
2582
|
+
alt.body.forEach(traverse);
|
|
2583
|
+
});
|
|
2584
|
+
}
|
|
2585
|
+
if (node.type === NodeType.FOR) {
|
|
2586
|
+
node.body.forEach(traverse);
|
|
2587
|
+
}
|
|
2588
|
+
if (node.type === NodeType.BLOCK) {
|
|
2589
|
+
node.body.forEach(traverse);
|
|
2590
|
+
}
|
|
2591
|
+
};
|
|
2592
|
+
traverse(this.templateNode);
|
|
2593
|
+
return {
|
|
2594
|
+
includes,
|
|
2595
|
+
blocks,
|
|
2596
|
+
};
|
|
1784
2597
|
}
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
2598
|
+
async compileNode(node) {
|
|
2599
|
+
if (node.type === NodeType.TEMPLATE) {
|
|
2600
|
+
await this.compileNodes(node.children);
|
|
2601
|
+
return;
|
|
1789
2602
|
}
|
|
1790
|
-
if (
|
|
1791
|
-
[
|
|
2603
|
+
if (this.options.compilers[node.type]) {
|
|
2604
|
+
await this.options.compilers[node.type](node, this);
|
|
2605
|
+
return;
|
|
2606
|
+
}
|
|
2607
|
+
this.options.debug?.(new CompileError(`Unknown "${node.name}" directive`, this.template, node.loc));
|
|
2608
|
+
}
|
|
2609
|
+
async compileNodes(nodes) {
|
|
2610
|
+
for (const child of nodes) {
|
|
2611
|
+
await this.compileNode(child);
|
|
1792
2612
|
}
|
|
1793
|
-
|
|
1794
|
-
|
|
2613
|
+
}
|
|
2614
|
+
async compilePartial(path, node, template, blocks) {
|
|
2615
|
+
try {
|
|
2616
|
+
const { code } = await new Compiler(this.options).compile(template, false, blocks);
|
|
2617
|
+
this.partials.set(path, code);
|
|
1795
2618
|
}
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
this.cursor = this.first;
|
|
2619
|
+
catch {
|
|
2620
|
+
this.options.debug?.(new CompileError(`Failed to compile template`, this.template, node.loc));
|
|
1799
2621
|
}
|
|
1800
2622
|
}
|
|
1801
2623
|
}
|
|
1802
2624
|
|
|
1803
|
-
class RenderError extends
|
|
1804
|
-
constructor(message,
|
|
1805
|
-
super(message);
|
|
1806
|
-
Object.defineProperty(this, "
|
|
2625
|
+
class RenderError extends CompileError {
|
|
2626
|
+
constructor(message, src, getLoc) {
|
|
2627
|
+
super(message, src);
|
|
2628
|
+
Object.defineProperty(this, "src", {
|
|
2629
|
+
enumerable: true,
|
|
2630
|
+
configurable: true,
|
|
2631
|
+
writable: true,
|
|
2632
|
+
value: src
|
|
2633
|
+
});
|
|
2634
|
+
Object.defineProperty(this, "getLoc", {
|
|
1807
2635
|
enumerable: true,
|
|
1808
2636
|
configurable: true,
|
|
1809
2637
|
writable: true,
|
|
1810
|
-
value:
|
|
2638
|
+
value: getLoc
|
|
1811
2639
|
});
|
|
1812
|
-
this.name = '
|
|
1813
|
-
|
|
2640
|
+
this.name = 'CompileError';
|
|
2641
|
+
RenderError.captureStackTrace?.(this, this.constructor);
|
|
1814
2642
|
}
|
|
1815
2643
|
get details() {
|
|
1816
|
-
|
|
1817
|
-
return
|
|
2644
|
+
this.loc = this.getLoc();
|
|
2645
|
+
return super.details;
|
|
1818
2646
|
}
|
|
1819
2647
|
}
|
|
1820
2648
|
|
|
1821
|
-
class
|
|
1822
|
-
constructor({ globals, filters, compilers, ...options } = {}) {
|
|
2649
|
+
class Renderer {
|
|
2650
|
+
constructor({ globals, filters, parsers, compilers, ...options } = {}) {
|
|
1823
2651
|
Object.defineProperty(this, "options", {
|
|
1824
2652
|
enumerable: true,
|
|
1825
2653
|
configurable: true,
|
|
1826
2654
|
writable: true,
|
|
1827
2655
|
value: void 0
|
|
1828
2656
|
});
|
|
1829
|
-
this.options = { ...
|
|
2657
|
+
this.options = { ...renderOptions, ...options };
|
|
1830
2658
|
Object.assign(this.options.globals, globals);
|
|
1831
2659
|
Object.assign(this.options.filters, filters);
|
|
2660
|
+
Object.assign(this.options.parsers, parsers);
|
|
1832
2661
|
Object.assign(this.options.compilers, compilers);
|
|
1833
2662
|
}
|
|
1834
2663
|
async render(template, globals) {
|
|
1835
|
-
const
|
|
1836
|
-
return this._render(template, globals, script, sourcemap);
|
|
1837
|
-
}
|
|
1838
|
-
async renderFile(filepath, globals) {
|
|
1839
|
-
if (this.options.cache?.has(filepath)) {
|
|
1840
|
-
const { template, script, sourcemap } = this.options.cache.get(filepath);
|
|
1841
|
-
return this._render(template, globals, script, sourcemap);
|
|
1842
|
-
}
|
|
1843
|
-
const template = await this.options.loader(filepath);
|
|
1844
|
-
const { script, sourcemap } = await this._compile(template);
|
|
1845
|
-
this.options.cache?.set(filepath, { template, script, sourcemap });
|
|
1846
|
-
return this._render(template, globals, script, sourcemap);
|
|
1847
|
-
}
|
|
1848
|
-
async _compile(template, filepath) {
|
|
1849
|
-
return new Compiler(this.options).compile(await new Parser(this.options).parse(template), filepath);
|
|
1850
|
-
}
|
|
1851
|
-
async _render(template, globals, script, sourcemap) {
|
|
2664
|
+
const compiler = new Compiler(this.options);
|
|
1852
2665
|
try {
|
|
1853
|
-
|
|
2666
|
+
await compiler.compile(template);
|
|
2667
|
+
return await compiler.script({ ...this.options.globals, ...globals }, (v) => {
|
|
1854
2668
|
if (v instanceof Safe) {
|
|
1855
2669
|
return `${v}`;
|
|
1856
2670
|
}
|
|
1857
2671
|
return this.options.autoEscape ? escape(v) : v;
|
|
1858
|
-
});
|
|
2672
|
+
}, { ...this.options.filters, ...filters });
|
|
1859
2673
|
}
|
|
1860
2674
|
catch (error) {
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
2675
|
+
if (error.name === 'CompileError') {
|
|
2676
|
+
throw error;
|
|
2677
|
+
}
|
|
2678
|
+
throw new RenderError(error.message, template, () => {
|
|
2679
|
+
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2680
|
+
if (!matched) {
|
|
2681
|
+
return error.stack ?? '';
|
|
2682
|
+
}
|
|
2683
|
+
const [loc] = compiler.getSourceLoc({
|
|
2684
|
+
line: +matched[1] - 2,
|
|
2685
|
+
column: +matched[2],
|
|
2686
|
+
});
|
|
2687
|
+
return loc;
|
|
1865
2688
|
});
|
|
1866
2689
|
}
|
|
1867
2690
|
}
|
|
2691
|
+
async renderFile(filepath, globals) {
|
|
2692
|
+
return this.render(await this.options.loader(filepath), globals);
|
|
2693
|
+
}
|
|
1868
2694
|
}
|
|
2695
|
+
|
|
1869
2696
|
async function render(template, data, options) {
|
|
1870
|
-
return new
|
|
2697
|
+
return new Renderer(options).render(template, data);
|
|
1871
2698
|
}
|
|
1872
2699
|
async function renderFile(filepath, data, options) {
|
|
1873
|
-
return new
|
|
2700
|
+
return new Renderer(options).renderFile(filepath, data);
|
|
1874
2701
|
}
|
|
1875
2702
|
|
|
1876
|
-
exports.
|
|
1877
|
-
exports.ExpCompiler = ExpCompiler;
|
|
1878
|
-
exports.ExpParser = ExpParser;
|
|
1879
|
-
exports.compiler = compiler;
|
|
1880
|
-
exports.parser = parser;
|
|
2703
|
+
exports.Renderer = Renderer;
|
|
1881
2704
|
exports.render = render;
|
|
1882
2705
|
exports.renderFile = renderFile;
|