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