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