janja 2.0.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/index.cjs.js +1386 -1306
- package/dist/index.d.ts +80 -109
- package/dist/index.esm.js +1386 -1306
- package/dist/index.js +1386 -1306
- package/package.json +24 -4
package/dist/index.esm.js
CHANGED
|
@@ -1,193 +1,459 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
2
|
+
const output = [];
|
|
3
|
+
const caretLines = new Set();
|
|
4
|
+
const addCaretLine = (index) => {
|
|
5
|
+
caretLines.add(index - 2);
|
|
6
|
+
caretLines.add(index - 1);
|
|
7
|
+
caretLines.add(index);
|
|
8
|
+
caretLines.add(index + 1);
|
|
9
|
+
caretLines.add(index + 2);
|
|
10
|
+
};
|
|
11
|
+
const lines = source.split('\n');
|
|
12
|
+
const indentWidth = String(lines.length).length + 2;
|
|
13
|
+
let caretLinesCount = 0;
|
|
14
|
+
lines.forEach((line, index) => {
|
|
15
|
+
if (!line) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
output.push(`${`${index + 1}| `.padStart(indentWidth, ' ')}${line}`);
|
|
19
|
+
if (l1 === index + 1) {
|
|
20
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c1 - 1)}^${l1 === l2 && c2 > c1 + 1 ? `${' '.repeat(c2 - c1 - 2)}^` : ''}`;
|
|
21
|
+
if (/\S/.test(caretLine)) {
|
|
22
|
+
output.push(caretLine);
|
|
23
|
+
}
|
|
24
|
+
addCaretLine(++caretLinesCount + index);
|
|
25
|
+
}
|
|
26
|
+
if (l1 !== l2 && l2 === index + 1) {
|
|
27
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c2 - 1)}${'^'.repeat(1)}`;
|
|
28
|
+
if (/\S/.test(caretLine)) {
|
|
29
|
+
output.push(caretLine);
|
|
30
|
+
}
|
|
31
|
+
addCaretLine(++caretLinesCount + index);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
35
|
+
return `${message}
|
|
36
|
+
|
|
37
|
+
${output
|
|
38
|
+
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
39
|
+
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
40
|
+
.join('\n')}
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class CompileError extends SyntaxError {
|
|
45
|
+
constructor(message, src, loc) {
|
|
46
|
+
super(message);
|
|
47
|
+
Object.defineProperty(this, "src", {
|
|
28
48
|
enumerable: true,
|
|
29
49
|
configurable: true,
|
|
30
50
|
writable: true,
|
|
31
|
-
value:
|
|
51
|
+
value: src
|
|
32
52
|
});
|
|
33
53
|
Object.defineProperty(this, "loc", {
|
|
34
54
|
enumerable: true,
|
|
35
55
|
configurable: true,
|
|
36
56
|
writable: true,
|
|
37
|
-
value:
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "body", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: void 0
|
|
57
|
+
value: loc
|
|
44
58
|
});
|
|
59
|
+
this.name = 'CompileError';
|
|
60
|
+
CompileError.captureStackTrace?.(this, this.constructor);
|
|
45
61
|
}
|
|
46
|
-
|
|
47
|
-
this.
|
|
62
|
+
get details() {
|
|
63
|
+
return this.loc
|
|
64
|
+
? highlightSource(this.message, this.src, this.loc)
|
|
65
|
+
: this.message;
|
|
48
66
|
}
|
|
49
67
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
|
|
69
|
+
const CONTEXT = 'c';
|
|
70
|
+
const ESCAPE = 'e';
|
|
71
|
+
const FILTERS = 'f';
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Contains the source map information.
|
|
75
|
+
* @example
|
|
76
|
+
* - template:
|
|
77
|
+
* {{ foo }}
|
|
78
|
+
*
|
|
79
|
+
* - compiled:
|
|
80
|
+
* s+=e(foo);
|
|
81
|
+
*
|
|
82
|
+
* - sourceMap:
|
|
83
|
+
* Mapping {
|
|
84
|
+
* source: {
|
|
85
|
+
* start: {
|
|
86
|
+
* line: 2,
|
|
87
|
+
* column: 4,
|
|
88
|
+
* },
|
|
89
|
+
* end: {
|
|
90
|
+
* line: 3,
|
|
91
|
+
* column: 5,
|
|
92
|
+
* },
|
|
93
|
+
* },
|
|
94
|
+
* target: {
|
|
95
|
+
* start: {
|
|
96
|
+
* line: 2,
|
|
97
|
+
* column: 3,
|
|
98
|
+
* },
|
|
99
|
+
* end: {
|
|
100
|
+
* line: 3,
|
|
101
|
+
* column: 8,
|
|
102
|
+
* },
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
*/
|
|
106
|
+
class SourceMap {
|
|
107
|
+
constructor() {
|
|
108
|
+
Object.defineProperty(this, "mappings", {
|
|
60
109
|
enumerable: true,
|
|
61
110
|
configurable: true,
|
|
62
111
|
writable: true,
|
|
63
|
-
value:
|
|
112
|
+
value: []
|
|
64
113
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
114
|
+
}
|
|
115
|
+
addMapping(source, target) {
|
|
116
|
+
this.mappings.push({
|
|
117
|
+
source,
|
|
118
|
+
target,
|
|
70
119
|
});
|
|
71
120
|
}
|
|
121
|
+
getSourceLoc({ line, column }) {
|
|
122
|
+
return this.mappings
|
|
123
|
+
.filter(({ target: { start, end } }) => start.line <= line &&
|
|
124
|
+
end.line >= line &&
|
|
125
|
+
(start.line < line || start.column <= column) &&
|
|
126
|
+
(end.line > line || end.column >= column))
|
|
127
|
+
.map(({ source }) => source);
|
|
128
|
+
}
|
|
72
129
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
configurable: true,
|
|
79
|
-
writable: true,
|
|
80
|
-
value: val
|
|
81
|
-
});
|
|
82
|
-
Object.defineProperty(this, "loc", {
|
|
83
|
-
enumerable: true,
|
|
84
|
-
configurable: true,
|
|
85
|
-
writable: true,
|
|
86
|
-
value: loc
|
|
87
|
-
});
|
|
88
|
-
Object.defineProperty(this, "strip", {
|
|
89
|
-
enumerable: true,
|
|
90
|
-
configurable: true,
|
|
91
|
-
writable: true,
|
|
92
|
-
value: strip
|
|
93
|
-
});
|
|
94
|
-
Object.defineProperty(this, "exp", {
|
|
130
|
+
|
|
131
|
+
class OutScript extends SourceMap {
|
|
132
|
+
constructor() {
|
|
133
|
+
super(...arguments);
|
|
134
|
+
Object.defineProperty(this, "code", {
|
|
95
135
|
enumerable: true,
|
|
96
136
|
configurable: true,
|
|
97
137
|
writable: true,
|
|
98
|
-
value:
|
|
138
|
+
value: ''
|
|
99
139
|
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
140
|
+
}
|
|
141
|
+
get script() {
|
|
142
|
+
// eslint-disable-next-line no-new-func
|
|
143
|
+
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
144
|
+
}
|
|
145
|
+
start() {
|
|
146
|
+
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
147
|
+
}
|
|
148
|
+
end() {
|
|
149
|
+
this.pushRaw(null, 'return s;', '})();');
|
|
150
|
+
}
|
|
151
|
+
pushRaw(loc, ...lines) {
|
|
152
|
+
const start = this.code.length;
|
|
153
|
+
for (const line of lines) {
|
|
154
|
+
this.code += line;
|
|
155
|
+
}
|
|
156
|
+
if (!loc) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.addMapping(loc, {
|
|
160
|
+
start: {
|
|
161
|
+
line: 1,
|
|
162
|
+
column: start,
|
|
163
|
+
},
|
|
164
|
+
end: {
|
|
165
|
+
line: 1,
|
|
166
|
+
column: this.code.length,
|
|
167
|
+
},
|
|
105
168
|
});
|
|
106
169
|
}
|
|
170
|
+
pushStr(loc, s) {
|
|
171
|
+
if (s) {
|
|
172
|
+
this.pushRaw(loc, `s+="${s
|
|
173
|
+
.replace(/\\/g, '\\\\')
|
|
174
|
+
.replace(/"/g, '\\"')
|
|
175
|
+
.replace(/[\n\r]/g, '\\n')}";`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
pushVar(loc, v) {
|
|
179
|
+
this.pushRaw(loc, `s+=${ESCAPE}(${v});`);
|
|
180
|
+
}
|
|
107
181
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
configurable: true,
|
|
114
|
-
writable: true,
|
|
115
|
-
value: val
|
|
116
|
-
});
|
|
117
|
-
Object.defineProperty(this, "loc", {
|
|
182
|
+
|
|
183
|
+
class Context extends OutScript {
|
|
184
|
+
constructor() {
|
|
185
|
+
super(...arguments);
|
|
186
|
+
Object.defineProperty(this, "context", {
|
|
118
187
|
enumerable: true,
|
|
119
188
|
configurable: true,
|
|
120
189
|
writable: true,
|
|
121
|
-
value:
|
|
190
|
+
value: CONTEXT
|
|
122
191
|
});
|
|
123
|
-
Object.defineProperty(this, "
|
|
192
|
+
Object.defineProperty(this, "contexts", {
|
|
124
193
|
enumerable: true,
|
|
125
194
|
configurable: true,
|
|
126
195
|
writable: true,
|
|
127
|
-
value:
|
|
196
|
+
value: [CONTEXT]
|
|
128
197
|
});
|
|
129
|
-
Object.defineProperty(this, "
|
|
198
|
+
Object.defineProperty(this, "index", {
|
|
130
199
|
enumerable: true,
|
|
131
200
|
configurable: true,
|
|
132
201
|
writable: true,
|
|
133
|
-
value:
|
|
202
|
+
value: 0
|
|
134
203
|
});
|
|
135
204
|
}
|
|
205
|
+
in() {
|
|
206
|
+
this.context = `${this.context}_${this.index++}`;
|
|
207
|
+
this.contexts.push(this.context);
|
|
208
|
+
return this.context;
|
|
209
|
+
}
|
|
210
|
+
out() {
|
|
211
|
+
if (this.context === CONTEXT) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
this.contexts.pop();
|
|
215
|
+
this.context = this.contexts.at(-1);
|
|
216
|
+
}
|
|
136
217
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
218
|
+
|
|
219
|
+
class Safe extends String {
|
|
220
|
+
constructor(value) {
|
|
221
|
+
super(`${value}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function escape(v) {
|
|
226
|
+
return `${v}`
|
|
227
|
+
.replace(/&/g, '&')
|
|
228
|
+
.replace(/"/g, '"')
|
|
229
|
+
.replace(/'/g, ''')
|
|
230
|
+
.replace(/</g, '<')
|
|
231
|
+
.replace(/>/g, '>');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function abs(value = 0) {
|
|
235
|
+
return Math.abs(value);
|
|
236
|
+
}
|
|
237
|
+
function add(value = 0, addend = 0) {
|
|
238
|
+
return value + addend;
|
|
239
|
+
}
|
|
240
|
+
function capitalize(value = '') {
|
|
241
|
+
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
242
|
+
}
|
|
243
|
+
function ceil(value = 0) {
|
|
244
|
+
return Math.ceil(value);
|
|
245
|
+
}
|
|
246
|
+
function compact(value = []) {
|
|
247
|
+
return value.filter((v) => v != null);
|
|
248
|
+
}
|
|
249
|
+
function div(value = 0, divisor = 1) {
|
|
250
|
+
return value / divisor;
|
|
251
|
+
}
|
|
252
|
+
function entries(value = {}) {
|
|
253
|
+
return Object.entries(value);
|
|
254
|
+
}
|
|
255
|
+
function even(value = 0) {
|
|
256
|
+
return +value % 2 === 0;
|
|
257
|
+
}
|
|
258
|
+
function fallback(value, defaultValue, anyFalsy = false) {
|
|
259
|
+
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
260
|
+
}
|
|
261
|
+
function first(value = []) {
|
|
262
|
+
return [...value][0];
|
|
263
|
+
}
|
|
264
|
+
function get(value, ...path) {
|
|
265
|
+
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
266
|
+
}
|
|
267
|
+
function groupby(value = [], key) {
|
|
268
|
+
return value.reduce((o, v) => {
|
|
269
|
+
const k = v[key];
|
|
270
|
+
return {
|
|
271
|
+
...o,
|
|
272
|
+
[k]: [...(o[k] || []), v],
|
|
273
|
+
};
|
|
274
|
+
}, {});
|
|
275
|
+
}
|
|
276
|
+
function join(value = [], separator = '') {
|
|
277
|
+
return [...value].join(separator);
|
|
278
|
+
}
|
|
279
|
+
function json(value = null, indent = 0) {
|
|
280
|
+
return new Safe(JSON.stringify(value, null, indent));
|
|
281
|
+
}
|
|
282
|
+
function keys(value = {}) {
|
|
283
|
+
return Object.keys(value);
|
|
284
|
+
}
|
|
285
|
+
function last(value = []) {
|
|
286
|
+
return [...value].reverse()[0];
|
|
287
|
+
}
|
|
288
|
+
function length(value = '') {
|
|
289
|
+
return value.length;
|
|
290
|
+
}
|
|
291
|
+
function lower(value = '') {
|
|
292
|
+
return value.toLowerCase();
|
|
293
|
+
}
|
|
294
|
+
function map(value = [], key, defaultValue) {
|
|
295
|
+
return value.map((o) => o[key] ?? defaultValue);
|
|
296
|
+
}
|
|
297
|
+
function max(value = 0, ...rest) {
|
|
298
|
+
return Math.max(value, ...rest);
|
|
299
|
+
}
|
|
300
|
+
function min(value = 0, ...rest) {
|
|
301
|
+
return Math.min(value, ...rest);
|
|
302
|
+
}
|
|
303
|
+
function mul(value = 0, multiplier = 1) {
|
|
304
|
+
return value * multiplier;
|
|
305
|
+
}
|
|
306
|
+
function odd(value = 0) {
|
|
307
|
+
return +value % 2 === 1;
|
|
308
|
+
}
|
|
309
|
+
function omit(value = {}, ...omitKeys) {
|
|
310
|
+
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
311
|
+
}
|
|
312
|
+
function pick(value = {}, ...pickKeys) {
|
|
313
|
+
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
314
|
+
}
|
|
315
|
+
function repeat(value = '', count = 0) {
|
|
316
|
+
return value.repeat(count);
|
|
317
|
+
}
|
|
318
|
+
function replace(value = '', search, replacement) {
|
|
319
|
+
return value.replace(new RegExp(search, 'g'), replacement);
|
|
320
|
+
}
|
|
321
|
+
function reverse(value = []) {
|
|
322
|
+
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
323
|
+
}
|
|
324
|
+
function safe(value = '') {
|
|
325
|
+
return new Safe(value);
|
|
326
|
+
}
|
|
327
|
+
function slice(value = '', start = 0, end) {
|
|
328
|
+
return value.slice(start, end);
|
|
329
|
+
}
|
|
330
|
+
function sort(value = []) {
|
|
331
|
+
return Array.isArray(value) ? value.sort() : [...value].sort().join('');
|
|
332
|
+
}
|
|
333
|
+
function split(value = '', separator = '') {
|
|
334
|
+
return value.split(separator);
|
|
335
|
+
}
|
|
336
|
+
function sub(value = 0, subtrahend = 0) {
|
|
337
|
+
return value - subtrahend;
|
|
338
|
+
}
|
|
339
|
+
function sum(value = []) {
|
|
340
|
+
return value.reduce((a, b) => a + b, 0);
|
|
341
|
+
}
|
|
342
|
+
function trim(value = '') {
|
|
343
|
+
return value.trim();
|
|
344
|
+
}
|
|
345
|
+
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
346
|
+
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
347
|
+
}
|
|
348
|
+
function unique(value = '') {
|
|
349
|
+
return Array.isArray(value)
|
|
350
|
+
? Array.from(new Set(value))
|
|
351
|
+
: Array.from(new Set(value)).join('');
|
|
352
|
+
}
|
|
353
|
+
function upper(value = '') {
|
|
354
|
+
return value.toUpperCase();
|
|
355
|
+
}
|
|
356
|
+
function urldecode(value = '') {
|
|
357
|
+
return decodeURIComponent(value);
|
|
358
|
+
}
|
|
359
|
+
function urlencode(value = '') {
|
|
360
|
+
return encodeURIComponent(value);
|
|
361
|
+
}
|
|
362
|
+
function values(value = {}) {
|
|
363
|
+
return Object.values(value);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
var _filters = /*#__PURE__*/Object.freeze({
|
|
367
|
+
__proto__: null,
|
|
368
|
+
abs: abs,
|
|
369
|
+
add: add,
|
|
370
|
+
capitalize: capitalize,
|
|
371
|
+
ceil: ceil,
|
|
372
|
+
compact: compact,
|
|
373
|
+
div: div,
|
|
374
|
+
entries: entries,
|
|
375
|
+
escape: escape,
|
|
376
|
+
even: even,
|
|
377
|
+
fallback: fallback,
|
|
378
|
+
first: first,
|
|
379
|
+
get: get,
|
|
380
|
+
groupby: groupby,
|
|
381
|
+
join: join,
|
|
382
|
+
json: json,
|
|
383
|
+
keys: keys,
|
|
384
|
+
last: last,
|
|
385
|
+
length: length,
|
|
386
|
+
lower: lower,
|
|
387
|
+
map: map,
|
|
388
|
+
max: max,
|
|
389
|
+
min: min,
|
|
390
|
+
mul: mul,
|
|
391
|
+
odd: odd,
|
|
392
|
+
omit: omit,
|
|
393
|
+
pick: pick,
|
|
394
|
+
repeat: repeat,
|
|
395
|
+
replace: replace,
|
|
396
|
+
reverse: reverse,
|
|
397
|
+
safe: safe,
|
|
398
|
+
slice: slice,
|
|
399
|
+
sort: sort,
|
|
400
|
+
split: split,
|
|
401
|
+
sub: sub,
|
|
402
|
+
sum: sum,
|
|
403
|
+
trim: trim,
|
|
404
|
+
truncate: truncate,
|
|
405
|
+
unique: unique,
|
|
406
|
+
upper: upper,
|
|
407
|
+
urldecode: urldecode,
|
|
408
|
+
urlencode: urlencode,
|
|
409
|
+
values: values
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
413
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
414
|
+
}
|
|
415
|
+
async function compileSuper({ loc }, compiler) {
|
|
416
|
+
if (!compiler.state.block) {
|
|
417
|
+
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
421
|
+
}
|
|
422
|
+
const compilers$a = {
|
|
423
|
+
BLOCK: compileBlock,
|
|
424
|
+
SUPER: compileSuper,
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const createUnexpected = (token, parser) => parser.createUnexpectedNode(token);
|
|
428
|
+
|
|
429
|
+
class Traversal {
|
|
430
|
+
constructor() {
|
|
431
|
+
Object.defineProperty(this, "type", {
|
|
159
432
|
enumerable: true,
|
|
160
433
|
configurable: true,
|
|
161
434
|
writable: true,
|
|
162
|
-
value:
|
|
435
|
+
value: null
|
|
163
436
|
});
|
|
164
|
-
Object.defineProperty(this, "
|
|
437
|
+
Object.defineProperty(this, "loc", {
|
|
165
438
|
enumerable: true,
|
|
166
439
|
configurable: true,
|
|
167
440
|
writable: true,
|
|
168
|
-
value:
|
|
441
|
+
value: null
|
|
169
442
|
});
|
|
170
|
-
Object.defineProperty(this, "
|
|
443
|
+
Object.defineProperty(this, "body", {
|
|
171
444
|
enumerable: true,
|
|
172
445
|
configurable: true,
|
|
173
446
|
writable: true,
|
|
174
|
-
value:
|
|
447
|
+
value: void 0
|
|
175
448
|
});
|
|
176
449
|
}
|
|
177
450
|
traverse(cb) {
|
|
178
451
|
this.body?.forEach(cb);
|
|
179
|
-
this.alternatives?.forEach(node => node.body?.forEach(cb));
|
|
180
452
|
}
|
|
181
453
|
}
|
|
182
|
-
class
|
|
183
|
-
constructor(
|
|
454
|
+
class RootNode extends Traversal {
|
|
455
|
+
constructor(body, loc) {
|
|
184
456
|
super();
|
|
185
|
-
Object.defineProperty(this, "test", {
|
|
186
|
-
enumerable: true,
|
|
187
|
-
configurable: true,
|
|
188
|
-
writable: true,
|
|
189
|
-
value: test
|
|
190
|
-
});
|
|
191
457
|
Object.defineProperty(this, "body", {
|
|
192
458
|
enumerable: true,
|
|
193
459
|
configurable: true,
|
|
@@ -200,28 +466,28 @@ class ElseIfNode extends Traversal {
|
|
|
200
466
|
writable: true,
|
|
201
467
|
value: loc
|
|
202
468
|
});
|
|
203
|
-
Object.defineProperty(this, "strip", {
|
|
204
|
-
enumerable: true,
|
|
205
|
-
configurable: true,
|
|
206
|
-
writable: true,
|
|
207
|
-
value: strip
|
|
208
|
-
});
|
|
209
469
|
Object.defineProperty(this, "type", {
|
|
210
470
|
enumerable: true,
|
|
211
471
|
configurable: true,
|
|
212
472
|
writable: true,
|
|
213
|
-
value:
|
|
473
|
+
value: 'TEMPLATE'
|
|
214
474
|
});
|
|
215
475
|
}
|
|
216
476
|
}
|
|
217
|
-
class
|
|
218
|
-
constructor(
|
|
477
|
+
class UnknownNode extends Traversal {
|
|
478
|
+
constructor(name, val, loc, strip) {
|
|
219
479
|
super();
|
|
220
|
-
Object.defineProperty(this, "
|
|
480
|
+
Object.defineProperty(this, "name", {
|
|
221
481
|
enumerable: true,
|
|
222
482
|
configurable: true,
|
|
223
483
|
writable: true,
|
|
224
|
-
value:
|
|
484
|
+
value: name
|
|
485
|
+
});
|
|
486
|
+
Object.defineProperty(this, "val", {
|
|
487
|
+
enumerable: true,
|
|
488
|
+
configurable: true,
|
|
489
|
+
writable: true,
|
|
490
|
+
value: val
|
|
225
491
|
});
|
|
226
492
|
Object.defineProperty(this, "loc", {
|
|
227
493
|
enumerable: true,
|
|
@@ -239,24 +505,24 @@ class ElseNode extends Traversal {
|
|
|
239
505
|
enumerable: true,
|
|
240
506
|
configurable: true,
|
|
241
507
|
writable: true,
|
|
242
|
-
value:
|
|
508
|
+
value: 'UNKNOWN'
|
|
243
509
|
});
|
|
244
510
|
}
|
|
245
511
|
}
|
|
246
|
-
class
|
|
247
|
-
constructor(
|
|
512
|
+
class UnexpectedNode extends Traversal {
|
|
513
|
+
constructor(name, val, loc, strip) {
|
|
248
514
|
super();
|
|
249
|
-
Object.defineProperty(this, "
|
|
515
|
+
Object.defineProperty(this, "name", {
|
|
250
516
|
enumerable: true,
|
|
251
517
|
configurable: true,
|
|
252
518
|
writable: true,
|
|
253
|
-
value:
|
|
519
|
+
value: name
|
|
254
520
|
});
|
|
255
|
-
Object.defineProperty(this, "
|
|
521
|
+
Object.defineProperty(this, "val", {
|
|
256
522
|
enumerable: true,
|
|
257
523
|
configurable: true,
|
|
258
524
|
writable: true,
|
|
259
|
-
value:
|
|
525
|
+
value: val
|
|
260
526
|
});
|
|
261
527
|
Object.defineProperty(this, "loc", {
|
|
262
528
|
enumerable: true,
|
|
@@ -274,12 +540,13 @@ class ForNode extends Traversal {
|
|
|
274
540
|
enumerable: true,
|
|
275
541
|
configurable: true,
|
|
276
542
|
writable: true,
|
|
277
|
-
value:
|
|
543
|
+
value: 'UNEXPECTED'
|
|
278
544
|
});
|
|
279
545
|
}
|
|
280
546
|
}
|
|
281
|
-
|
|
282
|
-
|
|
547
|
+
|
|
548
|
+
class BlockNode extends Traversal {
|
|
549
|
+
constructor(val, body, loc, strip) {
|
|
283
550
|
super();
|
|
284
551
|
Object.defineProperty(this, "val", {
|
|
285
552
|
enumerable: true,
|
|
@@ -287,13 +554,19 @@ class BreakNode extends Traversal {
|
|
|
287
554
|
writable: true,
|
|
288
555
|
value: val
|
|
289
556
|
});
|
|
290
|
-
Object.defineProperty(this, "
|
|
557
|
+
Object.defineProperty(this, "body", {
|
|
291
558
|
enumerable: true,
|
|
292
559
|
configurable: true,
|
|
293
560
|
writable: true,
|
|
294
|
-
value:
|
|
561
|
+
value: body
|
|
295
562
|
});
|
|
296
|
-
Object.defineProperty(this, "
|
|
563
|
+
Object.defineProperty(this, "loc", {
|
|
564
|
+
enumerable: true,
|
|
565
|
+
configurable: true,
|
|
566
|
+
writable: true,
|
|
567
|
+
value: loc
|
|
568
|
+
});
|
|
569
|
+
Object.defineProperty(this, "strip", {
|
|
297
570
|
enumerable: true,
|
|
298
571
|
configurable: true,
|
|
299
572
|
writable: true,
|
|
@@ -303,11 +576,11 @@ class BreakNode extends Traversal {
|
|
|
303
576
|
enumerable: true,
|
|
304
577
|
configurable: true,
|
|
305
578
|
writable: true,
|
|
306
|
-
value:
|
|
579
|
+
value: 'BLOCK'
|
|
307
580
|
});
|
|
308
581
|
}
|
|
309
582
|
}
|
|
310
|
-
class
|
|
583
|
+
class SuperNode extends Traversal {
|
|
311
584
|
constructor(val, loc, strip) {
|
|
312
585
|
super();
|
|
313
586
|
Object.defineProperty(this, "val", {
|
|
@@ -332,25 +605,207 @@ class ContinueNode extends Traversal {
|
|
|
332
605
|
enumerable: true,
|
|
333
606
|
configurable: true,
|
|
334
607
|
writable: true,
|
|
335
|
-
value:
|
|
608
|
+
value: 'SUPER'
|
|
336
609
|
});
|
|
337
610
|
}
|
|
338
611
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
612
|
+
|
|
613
|
+
function parseBlock(token, parser) {
|
|
614
|
+
if (!token.expression) {
|
|
615
|
+
parser.emitExpErr(token);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
parser.advance();
|
|
619
|
+
const body = parser.parseUntil(['endblock']);
|
|
620
|
+
if (parser.match(['endblock'])) {
|
|
621
|
+
parser.advance();
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
625
|
+
}
|
|
626
|
+
return new BlockNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
627
|
+
}
|
|
628
|
+
function parseSuper(token, parser) {
|
|
629
|
+
if (token.expression) {
|
|
630
|
+
parser.emitExpErr(token, false);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
parser.advance();
|
|
634
|
+
return new SuperNode(token.val, token.loc, token.strip);
|
|
635
|
+
}
|
|
636
|
+
const parsers$a = {
|
|
637
|
+
block: parseBlock,
|
|
638
|
+
super: parseSuper,
|
|
639
|
+
endblock: createUnexpected,
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
const plugin$a = {
|
|
643
|
+
compilers: compilers$a,
|
|
644
|
+
parsers: parsers$a,
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
const expTokenOperators = {
|
|
648
|
+
AND: '&&',
|
|
649
|
+
OR: '||',
|
|
650
|
+
IS: '',
|
|
651
|
+
NOT: '!',
|
|
652
|
+
EQ: '===',
|
|
653
|
+
NE: '!==',
|
|
654
|
+
GT: '>',
|
|
655
|
+
LT: '<',
|
|
656
|
+
GE: '>=',
|
|
657
|
+
LE: '<=',
|
|
658
|
+
IN: ' in ',
|
|
659
|
+
NI: ' in ',
|
|
660
|
+
OF: ' of ',
|
|
661
|
+
ADD: '+',
|
|
662
|
+
SUB: '-',
|
|
663
|
+
MUL: '*',
|
|
664
|
+
DIV: '/',
|
|
665
|
+
MOD: '%',
|
|
666
|
+
PIPE: '',
|
|
667
|
+
ASSIGN: '=',
|
|
668
|
+
LP: '(',
|
|
669
|
+
RP: ')',
|
|
670
|
+
COMMA: ',',
|
|
671
|
+
DOT: '.',
|
|
672
|
+
ID: '',
|
|
673
|
+
LIT: '',
|
|
674
|
+
IF: '',
|
|
675
|
+
ELSE: '',
|
|
676
|
+
};
|
|
677
|
+
class ExpCompiler {
|
|
678
|
+
constructor() {
|
|
679
|
+
Object.defineProperty(this, "context", {
|
|
343
680
|
enumerable: true,
|
|
344
681
|
configurable: true,
|
|
345
682
|
writable: true,
|
|
346
|
-
value:
|
|
683
|
+
value: void 0
|
|
684
|
+
});
|
|
685
|
+
Object.defineProperty(this, "filters", {
|
|
686
|
+
enumerable: true,
|
|
687
|
+
configurable: true,
|
|
688
|
+
writable: true,
|
|
689
|
+
value: void 0
|
|
347
690
|
});
|
|
691
|
+
}
|
|
692
|
+
compile(expression, context = CONTEXT, filters = FILTERS) {
|
|
693
|
+
this.context = context;
|
|
694
|
+
this.filters = filters;
|
|
695
|
+
return expression ? this.compileExp(expression) : '""';
|
|
696
|
+
}
|
|
697
|
+
compileExp(expression) {
|
|
698
|
+
switch (expression.type) {
|
|
699
|
+
case 'NOT':
|
|
700
|
+
return this.compileNot(expression);
|
|
701
|
+
case 'ID':
|
|
702
|
+
return this.compileId(expression);
|
|
703
|
+
case 'LIT':
|
|
704
|
+
return this.compileLit(expression);
|
|
705
|
+
case 'PIPE':
|
|
706
|
+
return this.compilePipe(expression);
|
|
707
|
+
case 'OF':
|
|
708
|
+
return this.compileOf(expression);
|
|
709
|
+
case 'ASSIGN':
|
|
710
|
+
return this.compileAssign(expression);
|
|
711
|
+
case 'IS':
|
|
712
|
+
return this.compileIs(expression);
|
|
713
|
+
case 'AND':
|
|
714
|
+
case 'OR':
|
|
715
|
+
case 'EQ':
|
|
716
|
+
case 'NE':
|
|
717
|
+
case 'GT':
|
|
718
|
+
case 'LT':
|
|
719
|
+
case 'GE':
|
|
720
|
+
case 'LE':
|
|
721
|
+
case 'IN':
|
|
722
|
+
case 'NI':
|
|
723
|
+
case 'ADD':
|
|
724
|
+
case 'SUB':
|
|
725
|
+
case 'MUL':
|
|
726
|
+
case 'DIV':
|
|
727
|
+
case 'MOD':
|
|
728
|
+
return this.compileBinary(expression);
|
|
729
|
+
case 'IF':
|
|
730
|
+
return this.compileTernary(expression);
|
|
731
|
+
case 'SEQ':
|
|
732
|
+
return this.compileSeq(expression);
|
|
733
|
+
// no default
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
compileNot({ argument }) {
|
|
737
|
+
return `!${this.compileExp(argument)}`;
|
|
738
|
+
}
|
|
739
|
+
compileId({ value, path, args }) {
|
|
740
|
+
let s = `${this.context}.${value}`;
|
|
741
|
+
if (path) {
|
|
742
|
+
s += path.map((p) => `.${p.value}`).join('');
|
|
743
|
+
}
|
|
744
|
+
if (args) {
|
|
745
|
+
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
746
|
+
}
|
|
747
|
+
return s;
|
|
748
|
+
}
|
|
749
|
+
compileLit({ value }) {
|
|
750
|
+
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
751
|
+
}
|
|
752
|
+
compileSeq({ elements }) {
|
|
753
|
+
return elements.map((element) => this.compileExp(element)).toString();
|
|
754
|
+
}
|
|
755
|
+
compilePipe({ left, right }) {
|
|
756
|
+
const { value: name, args = [] } = right;
|
|
757
|
+
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map((arg) => (arg.type === 'ASSIGN' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg)))].join(',')}))`;
|
|
758
|
+
}
|
|
759
|
+
compileOf({ left, right }) {
|
|
760
|
+
// destructuring
|
|
761
|
+
if (left.type === 'SEQ') {
|
|
762
|
+
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
763
|
+
}
|
|
764
|
+
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
765
|
+
}
|
|
766
|
+
compileAssign({ left, right }) {
|
|
767
|
+
if (left.type === 'ID') {
|
|
768
|
+
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
769
|
+
}
|
|
770
|
+
// destructuring
|
|
771
|
+
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
772
|
+
}
|
|
773
|
+
compileIs({ left, right }) {
|
|
774
|
+
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
775
|
+
}
|
|
776
|
+
compileBinary({ type, left, right }) {
|
|
777
|
+
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
778
|
+
return type === 'NI' ? `!${ret}` : ret;
|
|
779
|
+
}
|
|
780
|
+
compileTernary({ test, consequent, alternative }) {
|
|
781
|
+
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
async function compileCall({ val, body, loc }, compiler) {
|
|
786
|
+
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
787
|
+
await compiler.compileNodes(body);
|
|
788
|
+
compiler.pushRaw(null, '});');
|
|
789
|
+
}
|
|
790
|
+
const compilers$9 = {
|
|
791
|
+
CALL: compileCall,
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
class CallNode extends Traversal {
|
|
795
|
+
constructor(val, body, loc, strip) {
|
|
796
|
+
super();
|
|
348
797
|
Object.defineProperty(this, "val", {
|
|
349
798
|
enumerable: true,
|
|
350
799
|
configurable: true,
|
|
351
800
|
writable: true,
|
|
352
801
|
value: val
|
|
353
802
|
});
|
|
803
|
+
Object.defineProperty(this, "body", {
|
|
804
|
+
enumerable: true,
|
|
805
|
+
configurable: true,
|
|
806
|
+
writable: true,
|
|
807
|
+
value: body
|
|
808
|
+
});
|
|
354
809
|
Object.defineProperty(this, "loc", {
|
|
355
810
|
enumerable: true,
|
|
356
811
|
configurable: true,
|
|
@@ -367,24 +822,59 @@ class UnknownDirectiveNode extends Traversal {
|
|
|
367
822
|
enumerable: true,
|
|
368
823
|
configurable: true,
|
|
369
824
|
writable: true,
|
|
370
|
-
value:
|
|
825
|
+
value: 'CALL'
|
|
371
826
|
});
|
|
372
827
|
}
|
|
373
828
|
}
|
|
374
|
-
|
|
375
|
-
|
|
829
|
+
|
|
830
|
+
function parseCall(token, parser) {
|
|
831
|
+
if (!token.expression) {
|
|
832
|
+
parser.emitExpErr(token);
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
parser.advance();
|
|
836
|
+
const body = parser.parseUntil(['endcall']);
|
|
837
|
+
if (parser.match(['endcall'])) {
|
|
838
|
+
parser.advance();
|
|
839
|
+
}
|
|
840
|
+
else {
|
|
841
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
842
|
+
}
|
|
843
|
+
return new CallNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
844
|
+
}
|
|
845
|
+
const parsers$9 = {
|
|
846
|
+
call: parseCall,
|
|
847
|
+
endcall: createUnexpected,
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
const plugin$9 = {
|
|
851
|
+
compilers: compilers$9,
|
|
852
|
+
parsers: parsers$9,
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
async function compileCapture({ val, body, loc }, compiler) {
|
|
856
|
+
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
857
|
+
await compiler.compileNodes(body);
|
|
858
|
+
compiler.pushRaw(null, 'return s;})("");');
|
|
859
|
+
}
|
|
860
|
+
const compilers$8 = {
|
|
861
|
+
CAPTURE: compileCapture,
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
class CaptureNode extends Traversal {
|
|
865
|
+
constructor(val, body, loc, strip) {
|
|
376
866
|
super();
|
|
377
|
-
Object.defineProperty(this, "
|
|
867
|
+
Object.defineProperty(this, "val", {
|
|
378
868
|
enumerable: true,
|
|
379
869
|
configurable: true,
|
|
380
870
|
writable: true,
|
|
381
|
-
value:
|
|
871
|
+
value: val
|
|
382
872
|
});
|
|
383
|
-
Object.defineProperty(this, "
|
|
873
|
+
Object.defineProperty(this, "body", {
|
|
384
874
|
enumerable: true,
|
|
385
875
|
configurable: true,
|
|
386
876
|
writable: true,
|
|
387
|
-
value:
|
|
877
|
+
value: body
|
|
388
878
|
});
|
|
389
879
|
Object.defineProperty(this, "loc", {
|
|
390
880
|
enumerable: true,
|
|
@@ -402,10 +892,45 @@ class UnexpectedDirectiveNode extends Traversal {
|
|
|
402
892
|
enumerable: true,
|
|
403
893
|
configurable: true,
|
|
404
894
|
writable: true,
|
|
405
|
-
value:
|
|
895
|
+
value: 'CAPTURE'
|
|
406
896
|
});
|
|
407
897
|
}
|
|
408
898
|
}
|
|
899
|
+
|
|
900
|
+
function parseCapture(token, parser) {
|
|
901
|
+
if (!token.expression) {
|
|
902
|
+
parser.emitExpErr(token);
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
parser.advance();
|
|
906
|
+
const body = parser.parseUntil(['endcapture']);
|
|
907
|
+
if (parser.match(['endcapture'])) {
|
|
908
|
+
parser.advance();
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
912
|
+
}
|
|
913
|
+
return new CaptureNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
914
|
+
}
|
|
915
|
+
const parsers$8 = {
|
|
916
|
+
capture: parseCapture,
|
|
917
|
+
endcapture: createUnexpected,
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
const plugin$8 = {
|
|
921
|
+
compilers: compilers$8,
|
|
922
|
+
parsers: parsers$8,
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
async function compileComment({ val, loc }, compiler) {
|
|
926
|
+
if (!compiler.options.stripComments) {
|
|
927
|
+
compiler.pushStr(loc, `<!--${val}-->`);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
const compilers$7 = {
|
|
931
|
+
COMMENT: compileComment,
|
|
932
|
+
};
|
|
933
|
+
|
|
409
934
|
class CommentNode extends Traversal {
|
|
410
935
|
constructor(val, loc, strip) {
|
|
411
936
|
super();
|
|
@@ -431,24 +956,68 @@ class CommentNode extends Traversal {
|
|
|
431
956
|
enumerable: true,
|
|
432
957
|
configurable: true,
|
|
433
958
|
writable: true,
|
|
434
|
-
value:
|
|
959
|
+
value: 'COMMENT'
|
|
435
960
|
});
|
|
436
961
|
}
|
|
437
962
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
963
|
+
|
|
964
|
+
function parseComment({ val, loc, strip }, parser) {
|
|
965
|
+
parser.advance();
|
|
966
|
+
return new CommentNode(val, loc, strip);
|
|
967
|
+
}
|
|
968
|
+
const parsers$7 = {
|
|
969
|
+
comment: parseComment,
|
|
970
|
+
};
|
|
971
|
+
|
|
972
|
+
const plugin$7 = {
|
|
973
|
+
compilers: compilers$7,
|
|
974
|
+
parsers: parsers$7,
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
async function compileFor({ loop, body }, compiler) {
|
|
978
|
+
const { context } = compiler;
|
|
979
|
+
if (loop.left.type === 'SEQ') {
|
|
980
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
981
|
+
}
|
|
982
|
+
else {
|
|
983
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
|
|
984
|
+
}
|
|
985
|
+
await compiler.compileNodes(body);
|
|
986
|
+
compiler.out();
|
|
987
|
+
compiler.pushRaw(null, '}');
|
|
988
|
+
}
|
|
989
|
+
function compileBreak({ loc }, compiler) {
|
|
990
|
+
compiler.pushRaw(loc, 'break;');
|
|
991
|
+
}
|
|
992
|
+
function compileContinue({ loc }, compiler) {
|
|
993
|
+
compiler.pushRaw(loc, 'continue;');
|
|
994
|
+
}
|
|
995
|
+
const compilers$6 = {
|
|
996
|
+
FOR: compileFor,
|
|
997
|
+
BREAK: compileBreak,
|
|
998
|
+
CONTINUE: compileContinue,
|
|
999
|
+
};
|
|
1000
|
+
|
|
1001
|
+
class ForNode extends Traversal {
|
|
1002
|
+
constructor(loop, body, loc, strip) {
|
|
1003
|
+
super();
|
|
1004
|
+
Object.defineProperty(this, "loop", {
|
|
1005
|
+
enumerable: true,
|
|
1006
|
+
configurable: true,
|
|
1007
|
+
writable: true,
|
|
1008
|
+
value: loop
|
|
1009
|
+
});
|
|
1010
|
+
Object.defineProperty(this, "body", {
|
|
1011
|
+
enumerable: true,
|
|
1012
|
+
configurable: true,
|
|
1013
|
+
writable: true,
|
|
1014
|
+
value: body
|
|
1015
|
+
});
|
|
1016
|
+
Object.defineProperty(this, "loc", {
|
|
1017
|
+
enumerable: true,
|
|
1018
|
+
configurable: true,
|
|
1019
|
+
writable: true,
|
|
1020
|
+
value: loc
|
|
452
1021
|
});
|
|
453
1022
|
Object.defineProperty(this, "strip", {
|
|
454
1023
|
enumerable: true,
|
|
@@ -460,12 +1029,12 @@ class IncludeNode extends Traversal {
|
|
|
460
1029
|
enumerable: true,
|
|
461
1030
|
configurable: true,
|
|
462
1031
|
writable: true,
|
|
463
|
-
value:
|
|
1032
|
+
value: 'FOR'
|
|
464
1033
|
});
|
|
465
1034
|
}
|
|
466
1035
|
}
|
|
467
|
-
class
|
|
468
|
-
constructor(val,
|
|
1036
|
+
class BreakNode extends Traversal {
|
|
1037
|
+
constructor(val, loc, strip) {
|
|
469
1038
|
super();
|
|
470
1039
|
Object.defineProperty(this, "val", {
|
|
471
1040
|
enumerable: true,
|
|
@@ -473,12 +1042,6 @@ class BlockNode extends Traversal {
|
|
|
473
1042
|
writable: true,
|
|
474
1043
|
value: val
|
|
475
1044
|
});
|
|
476
|
-
Object.defineProperty(this, "body", {
|
|
477
|
-
enumerable: true,
|
|
478
|
-
configurable: true,
|
|
479
|
-
writable: true,
|
|
480
|
-
value: body
|
|
481
|
-
});
|
|
482
1045
|
Object.defineProperty(this, "loc", {
|
|
483
1046
|
enumerable: true,
|
|
484
1047
|
configurable: true,
|
|
@@ -495,11 +1058,11 @@ class BlockNode extends Traversal {
|
|
|
495
1058
|
enumerable: true,
|
|
496
1059
|
configurable: true,
|
|
497
1060
|
writable: true,
|
|
498
|
-
value:
|
|
1061
|
+
value: 'BREAK'
|
|
499
1062
|
});
|
|
500
1063
|
}
|
|
501
1064
|
}
|
|
502
|
-
class
|
|
1065
|
+
class ContinueNode extends Traversal {
|
|
503
1066
|
constructor(val, loc, strip) {
|
|
504
1067
|
super();
|
|
505
1068
|
Object.defineProperty(this, "val", {
|
|
@@ -524,18 +1087,92 @@ class SuperNode extends Traversal {
|
|
|
524
1087
|
enumerable: true,
|
|
525
1088
|
configurable: true,
|
|
526
1089
|
writable: true,
|
|
527
|
-
value:
|
|
1090
|
+
value: 'CONTINUE'
|
|
528
1091
|
});
|
|
529
1092
|
}
|
|
530
1093
|
}
|
|
531
|
-
|
|
532
|
-
|
|
1094
|
+
|
|
1095
|
+
const wm$1 = new WeakMap();
|
|
1096
|
+
function parseFor(token, parser) {
|
|
1097
|
+
if (!token.expression) {
|
|
1098
|
+
parser.emitExpErr(token);
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
wm$1.set(parser, true);
|
|
1102
|
+
parser.advance();
|
|
1103
|
+
const body = parser.parseUntil(['endfor']);
|
|
1104
|
+
wm$1.set(parser, false);
|
|
1105
|
+
if (parser.match(['endfor'])) {
|
|
1106
|
+
parser.advance();
|
|
1107
|
+
}
|
|
1108
|
+
else {
|
|
1109
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1110
|
+
}
|
|
1111
|
+
return new ForNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
1112
|
+
}
|
|
1113
|
+
function parseBreak(token, parser) {
|
|
1114
|
+
if (token.expression) {
|
|
1115
|
+
parser.emitExpErr(token, false);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (!wm$1.get(parser)) {
|
|
1119
|
+
parser.options.debug?.(new CompileError('"break" directive used outside of a loop', parser.template, token.loc));
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
parser.advance();
|
|
1123
|
+
return new BreakNode(token.val, token.loc, token.strip);
|
|
1124
|
+
}
|
|
1125
|
+
function parseContinue(token, parser) {
|
|
1126
|
+
if (token.expression) {
|
|
1127
|
+
parser.emitExpErr(token, false);
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
if (!wm$1.get(parser)) {
|
|
1131
|
+
parser.options.debug?.(new CompileError('"continue" directive used outside of a loop', parser.template, token.loc));
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
parser.advance();
|
|
1135
|
+
return new ContinueNode(token.val, token.loc, token.strip);
|
|
1136
|
+
}
|
|
1137
|
+
const parsers$6 = {
|
|
1138
|
+
for: parseFor,
|
|
1139
|
+
break: parseBreak,
|
|
1140
|
+
continue: parseContinue,
|
|
1141
|
+
endfor: createUnexpected,
|
|
1142
|
+
};
|
|
1143
|
+
|
|
1144
|
+
const plugin$6 = {
|
|
1145
|
+
compilers: compilers$6,
|
|
1146
|
+
parsers: parsers$6,
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
async function compileIf({ test, body, alternatives }, compiler) {
|
|
1150
|
+
compiler.pushRaw(test.loc, `if(${new ExpCompiler().compile(test, compiler.context)}){`);
|
|
1151
|
+
await compiler.compileNodes(body);
|
|
1152
|
+
compiler.pushRaw(null, '}');
|
|
1153
|
+
for (const branch of alternatives) {
|
|
1154
|
+
if (branch.type === 'ELSE_IF') {
|
|
1155
|
+
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1156
|
+
}
|
|
1157
|
+
else {
|
|
1158
|
+
compiler.pushRaw(null, 'else{');
|
|
1159
|
+
}
|
|
1160
|
+
await compiler.compileNodes(branch.body);
|
|
1161
|
+
compiler.pushRaw(null, '}');
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
const compilers$5 = {
|
|
1165
|
+
IF: compileIf,
|
|
1166
|
+
};
|
|
1167
|
+
|
|
1168
|
+
class IfNode extends Traversal {
|
|
1169
|
+
constructor(test, body, alternatives, loc, strip) {
|
|
533
1170
|
super();
|
|
534
|
-
Object.defineProperty(this, "
|
|
1171
|
+
Object.defineProperty(this, "test", {
|
|
535
1172
|
enumerable: true,
|
|
536
1173
|
configurable: true,
|
|
537
1174
|
writable: true,
|
|
538
|
-
value:
|
|
1175
|
+
value: test
|
|
539
1176
|
});
|
|
540
1177
|
Object.defineProperty(this, "body", {
|
|
541
1178
|
enumerable: true,
|
|
@@ -543,6 +1180,12 @@ class MacroNode extends Traversal {
|
|
|
543
1180
|
writable: true,
|
|
544
1181
|
value: body
|
|
545
1182
|
});
|
|
1183
|
+
Object.defineProperty(this, "alternatives", {
|
|
1184
|
+
enumerable: true,
|
|
1185
|
+
configurable: true,
|
|
1186
|
+
writable: true,
|
|
1187
|
+
value: alternatives
|
|
1188
|
+
});
|
|
546
1189
|
Object.defineProperty(this, "loc", {
|
|
547
1190
|
enumerable: true,
|
|
548
1191
|
configurable: true,
|
|
@@ -559,18 +1202,28 @@ class MacroNode extends Traversal {
|
|
|
559
1202
|
enumerable: true,
|
|
560
1203
|
configurable: true,
|
|
561
1204
|
writable: true,
|
|
562
|
-
value:
|
|
1205
|
+
value: 'IF'
|
|
563
1206
|
});
|
|
564
1207
|
}
|
|
1208
|
+
traverse(cb) {
|
|
1209
|
+
this.body?.forEach(cb);
|
|
1210
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
1211
|
+
}
|
|
565
1212
|
}
|
|
566
|
-
class
|
|
567
|
-
constructor(
|
|
1213
|
+
class ElseIfNode extends Traversal {
|
|
1214
|
+
constructor(test, body, loc, strip) {
|
|
568
1215
|
super();
|
|
569
|
-
Object.defineProperty(this, "
|
|
1216
|
+
Object.defineProperty(this, "test", {
|
|
570
1217
|
enumerable: true,
|
|
571
1218
|
configurable: true,
|
|
572
1219
|
writable: true,
|
|
573
|
-
value:
|
|
1220
|
+
value: test
|
|
1221
|
+
});
|
|
1222
|
+
Object.defineProperty(this, "body", {
|
|
1223
|
+
enumerable: true,
|
|
1224
|
+
configurable: true,
|
|
1225
|
+
writable: true,
|
|
1226
|
+
value: body
|
|
574
1227
|
});
|
|
575
1228
|
Object.defineProperty(this, "loc", {
|
|
576
1229
|
enumerable: true,
|
|
@@ -588,19 +1241,13 @@ class CallerNode extends Traversal {
|
|
|
588
1241
|
enumerable: true,
|
|
589
1242
|
configurable: true,
|
|
590
1243
|
writable: true,
|
|
591
|
-
value:
|
|
1244
|
+
value: 'ELSE_IF'
|
|
592
1245
|
});
|
|
593
1246
|
}
|
|
594
1247
|
}
|
|
595
|
-
class
|
|
596
|
-
constructor(
|
|
1248
|
+
class ElseNode extends Traversal {
|
|
1249
|
+
constructor(body, loc, strip) {
|
|
597
1250
|
super();
|
|
598
|
-
Object.defineProperty(this, "val", {
|
|
599
|
-
enumerable: true,
|
|
600
|
-
configurable: true,
|
|
601
|
-
writable: true,
|
|
602
|
-
value: val
|
|
603
|
-
});
|
|
604
1251
|
Object.defineProperty(this, "body", {
|
|
605
1252
|
enumerable: true,
|
|
606
1253
|
configurable: true,
|
|
@@ -623,11 +1270,79 @@ class CallNode extends Traversal {
|
|
|
623
1270
|
enumerable: true,
|
|
624
1271
|
configurable: true,
|
|
625
1272
|
writable: true,
|
|
626
|
-
value:
|
|
1273
|
+
value: 'ELSE'
|
|
627
1274
|
});
|
|
628
1275
|
}
|
|
629
1276
|
}
|
|
630
|
-
|
|
1277
|
+
|
|
1278
|
+
function parseIf(token, parser) {
|
|
1279
|
+
if (!token.expression) {
|
|
1280
|
+
parser.emitExpErr(token);
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
parser.advance();
|
|
1284
|
+
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
1285
|
+
const alternatives = [];
|
|
1286
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
1287
|
+
const branchToken = parser.peek();
|
|
1288
|
+
if (branchToken.name.toLowerCase() === 'else') {
|
|
1289
|
+
const elseNode = parseElse(branchToken, parser);
|
|
1290
|
+
if (elseNode) {
|
|
1291
|
+
alternatives.push(elseNode);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
else {
|
|
1295
|
+
const elseIfNode = parseElseIf(branchToken, parser);
|
|
1296
|
+
if (elseIfNode) {
|
|
1297
|
+
alternatives.push(elseIfNode);
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
if (parser.match(['endif'])) {
|
|
1302
|
+
parser.advance();
|
|
1303
|
+
}
|
|
1304
|
+
else {
|
|
1305
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1306
|
+
}
|
|
1307
|
+
return new IfNode(parser.parseExp(token.expression), body, alternatives, token.loc, token.strip);
|
|
1308
|
+
}
|
|
1309
|
+
function parseElseIf(token, parser) {
|
|
1310
|
+
if (!token.expression) {
|
|
1311
|
+
parser.emitExpErr(token);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
parser.advance();
|
|
1315
|
+
return new ElseIfNode(parser.parseExp(token.expression), parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']), token.loc, token.strip);
|
|
1316
|
+
}
|
|
1317
|
+
function parseElse(token, parser) {
|
|
1318
|
+
if (token.expression) {
|
|
1319
|
+
parser.emitExpErr(token, false);
|
|
1320
|
+
return;
|
|
1321
|
+
}
|
|
1322
|
+
parser.advance();
|
|
1323
|
+
return new ElseNode(parser.parseUntil(['endif']), token.loc, token.strip);
|
|
1324
|
+
}
|
|
1325
|
+
const parsers$5 = {
|
|
1326
|
+
if: parseIf,
|
|
1327
|
+
elseif: createUnexpected,
|
|
1328
|
+
elsif: createUnexpected,
|
|
1329
|
+
elif: createUnexpected,
|
|
1330
|
+
endif: createUnexpected,
|
|
1331
|
+
};
|
|
1332
|
+
|
|
1333
|
+
const plugin$5 = {
|
|
1334
|
+
compilers: compilers$5,
|
|
1335
|
+
parsers: parsers$5,
|
|
1336
|
+
};
|
|
1337
|
+
|
|
1338
|
+
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
1339
|
+
compiler.pushRaw(loc, `s+=await p["${value}"]?.()??"";`);
|
|
1340
|
+
}
|
|
1341
|
+
const compilers$4 = {
|
|
1342
|
+
INCLUDE: compileInclude,
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
class IncludeNode extends Traversal {
|
|
631
1346
|
constructor(val, loc, strip) {
|
|
632
1347
|
super();
|
|
633
1348
|
Object.defineProperty(this, "val", {
|
|
@@ -652,11 +1367,52 @@ class SetNode extends Traversal {
|
|
|
652
1367
|
enumerable: true,
|
|
653
1368
|
configurable: true,
|
|
654
1369
|
writable: true,
|
|
655
|
-
value:
|
|
1370
|
+
value: 'INCLUDE'
|
|
656
1371
|
});
|
|
657
1372
|
}
|
|
658
1373
|
}
|
|
659
|
-
|
|
1374
|
+
|
|
1375
|
+
function parseInclude(token, parser) {
|
|
1376
|
+
if (!token.expression) {
|
|
1377
|
+
parser.emitExpErr(token);
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
parser.advance();
|
|
1381
|
+
return new IncludeNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
1382
|
+
}
|
|
1383
|
+
const parsers$4 = {
|
|
1384
|
+
include: parseInclude,
|
|
1385
|
+
};
|
|
1386
|
+
|
|
1387
|
+
const plugin$4 = {
|
|
1388
|
+
compilers: compilers$4,
|
|
1389
|
+
parsers: parsers$4,
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
1393
|
+
const { context } = compiler;
|
|
1394
|
+
const { elements } = right;
|
|
1395
|
+
const expCompiler = new ExpCompiler();
|
|
1396
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1397
|
+
.map((el) => el.type === 'ASSIGN'
|
|
1398
|
+
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1399
|
+
: el.value)
|
|
1400
|
+
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1401
|
+
.map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
|
|
1402
|
+
.join(','), '};');
|
|
1403
|
+
await compiler.compileNodes(body);
|
|
1404
|
+
compiler.out();
|
|
1405
|
+
compiler.pushRaw(null, '};');
|
|
1406
|
+
}
|
|
1407
|
+
function compileCaller({ loc }, compiler) {
|
|
1408
|
+
compiler.pushRaw(loc, 'await _c?.();');
|
|
1409
|
+
}
|
|
1410
|
+
const compilers$3 = {
|
|
1411
|
+
MACRO: compileMacro,
|
|
1412
|
+
CALLER: compileCaller,
|
|
1413
|
+
};
|
|
1414
|
+
|
|
1415
|
+
class MacroNode extends Traversal {
|
|
660
1416
|
constructor(val, body, loc, strip) {
|
|
661
1417
|
super();
|
|
662
1418
|
Object.defineProperty(this, "val", {
|
|
@@ -687,989 +1443,306 @@ class CaptureNode extends Traversal {
|
|
|
687
1443
|
enumerable: true,
|
|
688
1444
|
configurable: true,
|
|
689
1445
|
writable: true,
|
|
690
|
-
value:
|
|
1446
|
+
value: 'MACRO'
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
class CallerNode extends Traversal {
|
|
1451
|
+
constructor(val, loc, strip) {
|
|
1452
|
+
super();
|
|
1453
|
+
Object.defineProperty(this, "val", {
|
|
1454
|
+
enumerable: true,
|
|
1455
|
+
configurable: true,
|
|
1456
|
+
writable: true,
|
|
1457
|
+
value: val
|
|
1458
|
+
});
|
|
1459
|
+
Object.defineProperty(this, "loc", {
|
|
1460
|
+
enumerable: true,
|
|
1461
|
+
configurable: true,
|
|
1462
|
+
writable: true,
|
|
1463
|
+
value: loc
|
|
1464
|
+
});
|
|
1465
|
+
Object.defineProperty(this, "strip", {
|
|
1466
|
+
enumerable: true,
|
|
1467
|
+
configurable: true,
|
|
1468
|
+
writable: true,
|
|
1469
|
+
value: strip
|
|
1470
|
+
});
|
|
1471
|
+
Object.defineProperty(this, "type", {
|
|
1472
|
+
enumerable: true,
|
|
1473
|
+
configurable: true,
|
|
1474
|
+
writable: true,
|
|
1475
|
+
value: 'CALLER'
|
|
691
1476
|
});
|
|
692
1477
|
}
|
|
693
1478
|
}
|
|
694
1479
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
caretLines.add(index - 1);
|
|
701
|
-
caretLines.add(index);
|
|
702
|
-
caretLines.add(index + 1);
|
|
703
|
-
caretLines.add(index + 2);
|
|
704
|
-
};
|
|
705
|
-
const lines = source.split('\n');
|
|
706
|
-
const indentWidth = String(lines.length).length + 2;
|
|
707
|
-
let caretLinesCount = 0;
|
|
708
|
-
lines.forEach((line, index) => {
|
|
709
|
-
if (!line) {
|
|
710
|
-
return;
|
|
711
|
-
}
|
|
712
|
-
output.push(`${`${index + 1}| `.padStart(indentWidth, ' ')}${line}`);
|
|
713
|
-
if (l1 === index + 1) {
|
|
714
|
-
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c1 - 1)}^${l1 === l2 && c2 > c1 + 1 ? `${' '.repeat(c2 - c1 - 2)}^` : ''}`;
|
|
715
|
-
if (/\S/.test(caretLine)) {
|
|
716
|
-
output.push(caretLine);
|
|
717
|
-
}
|
|
718
|
-
addCaretLine(++caretLinesCount + index);
|
|
719
|
-
}
|
|
720
|
-
if (l1 !== l2 && l2 === index + 1) {
|
|
721
|
-
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c2 - 1)}${'^'.repeat(1)}`;
|
|
722
|
-
if (/\S/.test(caretLine)) {
|
|
723
|
-
output.push(caretLine);
|
|
724
|
-
}
|
|
725
|
-
addCaretLine(++caretLinesCount + index);
|
|
726
|
-
}
|
|
727
|
-
});
|
|
728
|
-
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
729
|
-
return `${message}
|
|
730
|
-
|
|
731
|
-
${output
|
|
732
|
-
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
733
|
-
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
734
|
-
.join('\n')}
|
|
735
|
-
`;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
class CompileError extends SyntaxError {
|
|
739
|
-
constructor(message, src, loc) {
|
|
740
|
-
super(message);
|
|
741
|
-
Object.defineProperty(this, "src", {
|
|
742
|
-
enumerable: true,
|
|
743
|
-
configurable: true,
|
|
744
|
-
writable: true,
|
|
745
|
-
value: src
|
|
746
|
-
});
|
|
747
|
-
Object.defineProperty(this, "loc", {
|
|
748
|
-
enumerable: true,
|
|
749
|
-
configurable: true,
|
|
750
|
-
writable: true,
|
|
751
|
-
value: loc
|
|
752
|
-
});
|
|
753
|
-
this.name = 'CompileError';
|
|
754
|
-
CompileError.captureStackTrace?.(this, this.constructor);
|
|
755
|
-
}
|
|
756
|
-
get details() {
|
|
757
|
-
return this.loc
|
|
758
|
-
? highlightSource(this.message, this.src, this.loc)
|
|
759
|
-
: this.message;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
const parseUnexpected = (token, parser) => parser.createUnexpectedDirective(token);
|
|
764
|
-
|
|
765
|
-
function parseBlock(token, parser) {
|
|
766
|
-
parser.requireExpression(token);
|
|
767
|
-
const val = parser.parseExp(token.expression);
|
|
768
|
-
parser.advance();
|
|
769
|
-
const body = parser.parseUntil(['endblock']);
|
|
770
|
-
if (parser.match(['endblock'])) {
|
|
771
|
-
parser.advance();
|
|
772
|
-
}
|
|
773
|
-
else {
|
|
774
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
775
|
-
}
|
|
776
|
-
return new BlockNode(val, body, token.loc, token.strip);
|
|
777
|
-
}
|
|
778
|
-
function parseSuper(token, parser) {
|
|
779
|
-
parser.requireNoExpression(token);
|
|
780
|
-
parser.advance();
|
|
781
|
-
return new SuperNode(token.val, token.loc, token.strip);
|
|
782
|
-
}
|
|
783
|
-
const parsers$8 = {
|
|
784
|
-
block: parseBlock,
|
|
785
|
-
super: parseSuper,
|
|
786
|
-
endblock: parseUnexpected,
|
|
787
|
-
};
|
|
788
|
-
|
|
789
|
-
function parseCall(token, parser) {
|
|
790
|
-
parser.requireExpression(token);
|
|
791
|
-
const val = parser.parseExp(token.expression);
|
|
792
|
-
parser.advance();
|
|
793
|
-
const body = parser.parseUntil(['endcall']);
|
|
794
|
-
if (parser.match(['endcall'])) {
|
|
795
|
-
parser.advance();
|
|
796
|
-
}
|
|
797
|
-
else {
|
|
798
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1480
|
+
const wm = new WeakMap();
|
|
1481
|
+
function parseMacro(token, parser) {
|
|
1482
|
+
if (!token.expression) {
|
|
1483
|
+
parser.emitExpErr(token);
|
|
1484
|
+
return;
|
|
799
1485
|
}
|
|
800
|
-
|
|
801
|
-
}
|
|
802
|
-
const parsers$7 = {
|
|
803
|
-
call: parseCall,
|
|
804
|
-
endcall: parseUnexpected,
|
|
805
|
-
};
|
|
806
|
-
|
|
807
|
-
function parseCapture(token, parser) {
|
|
808
|
-
parser.requireExpression(token);
|
|
809
|
-
const val = parser.parseExp(token.expression);
|
|
1486
|
+
wm.set(parser, true);
|
|
810
1487
|
parser.advance();
|
|
811
|
-
const body = parser.parseUntil(['
|
|
812
|
-
|
|
1488
|
+
const body = parser.parseUntil(['endmacro']);
|
|
1489
|
+
wm.set(parser, false);
|
|
1490
|
+
if (parser.match(['endmacro'])) {
|
|
813
1491
|
parser.advance();
|
|
814
1492
|
}
|
|
815
1493
|
else {
|
|
816
1494
|
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
817
1495
|
}
|
|
818
|
-
return new
|
|
1496
|
+
return new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
819
1497
|
}
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
function parseFor(token, parser) {
|
|
826
|
-
parser.requireExpression(token);
|
|
827
|
-
const loop = parser.parseExp(token.expression);
|
|
828
|
-
parser.advance();
|
|
829
|
-
const body = parser.parseUntil(['endfor']);
|
|
830
|
-
if (parser.match(['endfor'])) {
|
|
831
|
-
parser.advance();
|
|
1498
|
+
function parseCaller(token, parser) {
|
|
1499
|
+
if (token.expression) {
|
|
1500
|
+
parser.emitExpErr(token, false);
|
|
1501
|
+
return;
|
|
832
1502
|
}
|
|
833
|
-
|
|
834
|
-
|
|
1503
|
+
if (!wm.get(parser)) {
|
|
1504
|
+
parser.options.debug?.(new CompileError('"caller" directive used outside of a macro', parser.template, token.loc));
|
|
1505
|
+
return;
|
|
835
1506
|
}
|
|
836
|
-
return new ForNode(loop, body, token.loc, token.strip);
|
|
837
|
-
}
|
|
838
|
-
function parseBreak(token, parser) {
|
|
839
|
-
parser.requireNoExpression(token);
|
|
840
|
-
parser.advance();
|
|
841
|
-
return new BreakNode(token.val, token.loc, token.strip);
|
|
842
|
-
}
|
|
843
|
-
function parseContinue(token, parser) {
|
|
844
|
-
parser.requireNoExpression(token);
|
|
845
1507
|
parser.advance();
|
|
846
|
-
return new
|
|
1508
|
+
return new CallerNode(token.val, token.loc, token.strip);
|
|
847
1509
|
}
|
|
848
|
-
const parsers$
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
endfor: parseUnexpected,
|
|
1510
|
+
const parsers$3 = {
|
|
1511
|
+
macro: parseMacro,
|
|
1512
|
+
caller: parseCaller,
|
|
1513
|
+
endmacro: createUnexpected,
|
|
853
1514
|
};
|
|
854
1515
|
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
859
|
-
const alternatives = [];
|
|
860
|
-
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
861
|
-
const branchToken = parser.peek();
|
|
862
|
-
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
863
|
-
? parseElse(branchToken, parser)
|
|
864
|
-
: parseElseIf(branchToken, parser));
|
|
865
|
-
}
|
|
866
|
-
if (parser.match(['endif'])) {
|
|
867
|
-
parser.advance();
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
871
|
-
}
|
|
872
|
-
return new IfNode(parser.parseExp(token.expression), body, alternatives, token.loc, token.strip);
|
|
873
|
-
}
|
|
874
|
-
function parseElseIf(token, parser) {
|
|
875
|
-
parser.requireExpression(token);
|
|
876
|
-
parser.advance();
|
|
877
|
-
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
878
|
-
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
879
|
-
}
|
|
880
|
-
function parseElse(token, parser) {
|
|
881
|
-
parser.requireNoExpression(token);
|
|
882
|
-
parser.advance();
|
|
883
|
-
const body = parser.parseUntil(['endif']);
|
|
884
|
-
return new ElseNode(body, token.loc, token.strip);
|
|
885
|
-
}
|
|
886
|
-
const parsers$4 = {
|
|
887
|
-
if: parseIf,
|
|
888
|
-
elseif: parseUnexpected,
|
|
889
|
-
elsif: parseUnexpected,
|
|
890
|
-
elif: parseUnexpected,
|
|
891
|
-
endif: parseUnexpected,
|
|
1516
|
+
const plugin$3 = {
|
|
1517
|
+
compilers: compilers$3,
|
|
1518
|
+
parsers: parsers$3,
|
|
892
1519
|
};
|
|
893
1520
|
|
|
894
|
-
function
|
|
895
|
-
|
|
896
|
-
const val = parser.parseExp(token.expression);
|
|
897
|
-
parser.advance();
|
|
898
|
-
return new IncludeNode(val, token.loc, token.strip);
|
|
1521
|
+
async function compileOutput({ exp }, compiler) {
|
|
1522
|
+
compiler.pushVar(exp.loc, new ExpCompiler().compile(exp, compiler.context));
|
|
899
1523
|
}
|
|
900
|
-
const
|
|
901
|
-
|
|
1524
|
+
const compilers$2 = {
|
|
1525
|
+
OUTPUT: compileOutput,
|
|
902
1526
|
};
|
|
903
1527
|
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1528
|
+
class OutputNode extends Traversal {
|
|
1529
|
+
constructor(val, loc, strip, exp) {
|
|
1530
|
+
super();
|
|
1531
|
+
Object.defineProperty(this, "val", {
|
|
1532
|
+
enumerable: true,
|
|
1533
|
+
configurable: true,
|
|
1534
|
+
writable: true,
|
|
1535
|
+
value: val
|
|
1536
|
+
});
|
|
1537
|
+
Object.defineProperty(this, "loc", {
|
|
1538
|
+
enumerable: true,
|
|
1539
|
+
configurable: true,
|
|
1540
|
+
writable: true,
|
|
1541
|
+
value: loc
|
|
1542
|
+
});
|
|
1543
|
+
Object.defineProperty(this, "strip", {
|
|
1544
|
+
enumerable: true,
|
|
1545
|
+
configurable: true,
|
|
1546
|
+
writable: true,
|
|
1547
|
+
value: strip
|
|
1548
|
+
});
|
|
1549
|
+
Object.defineProperty(this, "exp", {
|
|
1550
|
+
enumerable: true,
|
|
1551
|
+
configurable: true,
|
|
1552
|
+
writable: true,
|
|
1553
|
+
value: exp
|
|
1554
|
+
});
|
|
1555
|
+
Object.defineProperty(this, "type", {
|
|
1556
|
+
enumerable: true,
|
|
1557
|
+
configurable: true,
|
|
1558
|
+
writable: true,
|
|
1559
|
+
value: 'OUTPUT'
|
|
1560
|
+
});
|
|
914
1561
|
}
|
|
915
|
-
return new MacroNode(name, body, token.loc, token.strip);
|
|
916
|
-
}
|
|
917
|
-
function parseCaller(token, parser) {
|
|
918
|
-
parser.requireNoExpression(token);
|
|
919
|
-
parser.advance();
|
|
920
|
-
return new CallerNode(token.val, token.loc, token.strip);
|
|
921
1562
|
}
|
|
922
|
-
const parsers$2 = {
|
|
923
|
-
macro: parseMacro,
|
|
924
|
-
caller: parseCaller,
|
|
925
|
-
endmacro: parseUnexpected,
|
|
926
|
-
};
|
|
927
1563
|
|
|
928
|
-
function
|
|
929
|
-
parser.requireExpression(token);
|
|
930
|
-
const val = parser.parseExp(token.expression);
|
|
1564
|
+
function parseOutput(token, parser) {
|
|
931
1565
|
parser.advance();
|
|
932
|
-
return new
|
|
1566
|
+
return new OutputNode(token.val, token.loc, token.strip, parser.parseExp(token));
|
|
933
1567
|
}
|
|
934
|
-
const parsers$
|
|
935
|
-
|
|
1568
|
+
const parsers$2 = {
|
|
1569
|
+
output: parseOutput,
|
|
936
1570
|
};
|
|
937
1571
|
|
|
938
|
-
const
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
...parsers$3,
|
|
942
|
-
...parsers$8,
|
|
943
|
-
...parsers$2,
|
|
944
|
-
...parsers$7,
|
|
945
|
-
...parsers$1,
|
|
946
|
-
...parsers$6,
|
|
1572
|
+
const plugin$2 = {
|
|
1573
|
+
compilers: compilers$2,
|
|
1574
|
+
parsers: parsers$2,
|
|
947
1575
|
};
|
|
948
1576
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
super(`${value}`);
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
function escape(v) {
|
|
956
|
-
return `${v}`
|
|
957
|
-
.replace(/&/g, '&')
|
|
958
|
-
.replace(/"/g, '"')
|
|
959
|
-
.replace(/'/g, ''')
|
|
960
|
-
.replace(/</g, '<')
|
|
961
|
-
.replace(/>/g, '>');
|
|
1577
|
+
async function compileSet({ val, loc }, compiler) {
|
|
1578
|
+
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
962
1579
|
}
|
|
1580
|
+
const compilers$1 = {
|
|
1581
|
+
SET: compileSet,
|
|
1582
|
+
};
|
|
963
1583
|
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
return value + addend;
|
|
969
|
-
}
|
|
970
|
-
function capitalize(value = '') {
|
|
971
|
-
return value.replace(/\b\w/g, match => match.toUpperCase());
|
|
972
|
-
}
|
|
973
|
-
function ceil(value = 0) {
|
|
974
|
-
return Math.ceil(value);
|
|
975
|
-
}
|
|
976
|
-
function compact(value = []) {
|
|
977
|
-
return value.filter(v => v != null);
|
|
978
|
-
}
|
|
979
|
-
function div(value = 0, divisor = 1) {
|
|
980
|
-
return value / divisor;
|
|
981
|
-
}
|
|
982
|
-
function entries(value = {}) {
|
|
983
|
-
return Object.entries(value);
|
|
984
|
-
}
|
|
985
|
-
function even(value = 0) {
|
|
986
|
-
return +value % 2 === 0;
|
|
987
|
-
}
|
|
988
|
-
function fallback(value, defaultValue, anyFalsy = false) {
|
|
989
|
-
return anyFalsy ? (value || defaultValue) : (value ?? defaultValue);
|
|
990
|
-
}
|
|
991
|
-
function first(value = []) {
|
|
992
|
-
return [...value][0];
|
|
993
|
-
}
|
|
994
|
-
function get(value, ...path) {
|
|
995
|
-
return path.reduce((acc, cur) => (acc && typeof acc === 'object' && (cur in acc)) ? acc[cur] : undefined, value);
|
|
996
|
-
}
|
|
997
|
-
function groupby(value = [], key) {
|
|
998
|
-
return value.reduce((o, v) => {
|
|
999
|
-
const k = v[key];
|
|
1000
|
-
return {
|
|
1001
|
-
...o,
|
|
1002
|
-
[k]: [...(o[k] || []), v],
|
|
1003
|
-
};
|
|
1004
|
-
}, {});
|
|
1005
|
-
}
|
|
1006
|
-
function join(value = [], separator = '') {
|
|
1007
|
-
return [...value].join(separator);
|
|
1008
|
-
}
|
|
1009
|
-
function json(value = null, indent = 0) {
|
|
1010
|
-
return new Safe(JSON.stringify(value, null, indent));
|
|
1011
|
-
}
|
|
1012
|
-
function keys(value = {}) {
|
|
1013
|
-
return Object.keys(value);
|
|
1014
|
-
}
|
|
1015
|
-
function last(value = []) {
|
|
1016
|
-
return [...value].reverse()[0];
|
|
1017
|
-
}
|
|
1018
|
-
function length(value = '') {
|
|
1019
|
-
return value.length;
|
|
1020
|
-
}
|
|
1021
|
-
function lower(value = '') {
|
|
1022
|
-
return value.toLowerCase();
|
|
1023
|
-
}
|
|
1024
|
-
function map(value = [], key, defaultValue) {
|
|
1025
|
-
return value.map(o => o[key] ?? defaultValue);
|
|
1026
|
-
}
|
|
1027
|
-
function max(value = 0, ...values) {
|
|
1028
|
-
return Math.max(value, ...values);
|
|
1029
|
-
}
|
|
1030
|
-
function min(value = 0, ...values) {
|
|
1031
|
-
return Math.min(value, ...values);
|
|
1032
|
-
}
|
|
1033
|
-
function mul(value = 0, multiplier = 1) {
|
|
1034
|
-
return value * multiplier;
|
|
1035
|
-
}
|
|
1036
|
-
function odd(value = 0) {
|
|
1037
|
-
return +value % 2 === 1;
|
|
1038
|
-
}
|
|
1039
|
-
function omit(value = {}, ...keys) {
|
|
1040
|
-
return Object.entries(value).reduce((o, [k, v]) => (keys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
1041
|
-
}
|
|
1042
|
-
function pick(value = {}, ...keys) {
|
|
1043
|
-
return keys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
1044
|
-
}
|
|
1045
|
-
function repeat(value = '', count = 0) {
|
|
1046
|
-
return value.repeat(count);
|
|
1047
|
-
}
|
|
1048
|
-
function replace(value = '', search, replace) {
|
|
1049
|
-
return value.replace(new RegExp(search, 'g'), replace);
|
|
1050
|
-
}
|
|
1051
|
-
function reverse(value = []) {
|
|
1052
|
-
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
1053
|
-
}
|
|
1054
|
-
function safe(value = '') {
|
|
1055
|
-
return new Safe(value);
|
|
1056
|
-
}
|
|
1057
|
-
function slice(value = '', start = 0, end) {
|
|
1058
|
-
return value.slice(start, end);
|
|
1059
|
-
}
|
|
1060
|
-
function sort(value = []) {
|
|
1061
|
-
return Array.isArray(value) ? value.sort() : [...value].sort().join('');
|
|
1062
|
-
}
|
|
1063
|
-
function split(value = '', separator = '') {
|
|
1064
|
-
return value.split(separator);
|
|
1065
|
-
}
|
|
1066
|
-
function sub(value = 0, subtrahend = 0) {
|
|
1067
|
-
return value - subtrahend;
|
|
1068
|
-
}
|
|
1069
|
-
function sum(value = []) {
|
|
1070
|
-
return value.reduce((a, b) => a + b, 0);
|
|
1071
|
-
}
|
|
1072
|
-
function trim(value = '') {
|
|
1073
|
-
return value.trim();
|
|
1074
|
-
}
|
|
1075
|
-
function truncate(value = '', length = 0, truncateStr = '...') {
|
|
1076
|
-
return value.slice(0, length) + (value.length > length ? truncateStr : '');
|
|
1077
|
-
}
|
|
1078
|
-
function unique(value = '') {
|
|
1079
|
-
return Array.isArray(value)
|
|
1080
|
-
? Array.from(new Set(value))
|
|
1081
|
-
: Array.from(new Set(value)).join('');
|
|
1082
|
-
}
|
|
1083
|
-
function upper(value = '') {
|
|
1084
|
-
return value.toUpperCase();
|
|
1085
|
-
}
|
|
1086
|
-
function urldecode(value = '') {
|
|
1087
|
-
return decodeURIComponent(value);
|
|
1088
|
-
}
|
|
1089
|
-
function urlencode(value = '') {
|
|
1090
|
-
return encodeURIComponent(value);
|
|
1091
|
-
}
|
|
1092
|
-
function values(value = {}) {
|
|
1093
|
-
return Object.values(value);
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
var filters = /*#__PURE__*/Object.freeze({
|
|
1097
|
-
__proto__: null,
|
|
1098
|
-
abs: abs,
|
|
1099
|
-
add: add,
|
|
1100
|
-
capitalize: capitalize,
|
|
1101
|
-
ceil: ceil,
|
|
1102
|
-
compact: compact,
|
|
1103
|
-
div: div,
|
|
1104
|
-
entries: entries,
|
|
1105
|
-
escape: escape,
|
|
1106
|
-
even: even,
|
|
1107
|
-
fallback: fallback,
|
|
1108
|
-
first: first,
|
|
1109
|
-
get: get,
|
|
1110
|
-
groupby: groupby,
|
|
1111
|
-
join: join,
|
|
1112
|
-
json: json,
|
|
1113
|
-
keys: keys,
|
|
1114
|
-
last: last,
|
|
1115
|
-
length: length,
|
|
1116
|
-
lower: lower,
|
|
1117
|
-
map: map,
|
|
1118
|
-
max: max,
|
|
1119
|
-
min: min,
|
|
1120
|
-
mul: mul,
|
|
1121
|
-
odd: odd,
|
|
1122
|
-
omit: omit,
|
|
1123
|
-
pick: pick,
|
|
1124
|
-
repeat: repeat,
|
|
1125
|
-
replace: replace,
|
|
1126
|
-
reverse: reverse,
|
|
1127
|
-
safe: safe,
|
|
1128
|
-
slice: slice,
|
|
1129
|
-
sort: sort,
|
|
1130
|
-
split: split,
|
|
1131
|
-
sub: sub,
|
|
1132
|
-
sum: sum,
|
|
1133
|
-
trim: trim,
|
|
1134
|
-
truncate: truncate,
|
|
1135
|
-
unique: unique,
|
|
1136
|
-
upper: upper,
|
|
1137
|
-
urldecode: urldecode,
|
|
1138
|
-
urlencode: urlencode,
|
|
1139
|
-
values: values
|
|
1140
|
-
});
|
|
1141
|
-
|
|
1142
|
-
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1143
|
-
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, `}`);
|
|
1144
|
-
}
|
|
1145
|
-
async function compileSuper({ loc }, compiler) {
|
|
1146
|
-
if (!compiler.state.block) {
|
|
1147
|
-
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1148
|
-
return;
|
|
1149
|
-
}
|
|
1150
|
-
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1151
|
-
}
|
|
1152
|
-
const compilers$b = {
|
|
1153
|
-
[NodeType.BLOCK]: compileBlock,
|
|
1154
|
-
[NodeType.SUPER]: compileSuper,
|
|
1155
|
-
};
|
|
1156
|
-
|
|
1157
|
-
const expTokenOperators = {
|
|
1158
|
-
AND: '&&',
|
|
1159
|
-
OR: '||',
|
|
1160
|
-
IS: '',
|
|
1161
|
-
NOT: '!',
|
|
1162
|
-
EQ: '===',
|
|
1163
|
-
NE: '!==',
|
|
1164
|
-
GT: '>',
|
|
1165
|
-
LT: '<',
|
|
1166
|
-
GE: '>=',
|
|
1167
|
-
LE: '<=',
|
|
1168
|
-
IN: ' in ',
|
|
1169
|
-
NI: ' in ',
|
|
1170
|
-
OF: ' of ',
|
|
1171
|
-
ADD: '+',
|
|
1172
|
-
SUB: '-',
|
|
1173
|
-
MUL: '*',
|
|
1174
|
-
DIV: '/',
|
|
1175
|
-
MOD: '%',
|
|
1176
|
-
PIPE: '',
|
|
1177
|
-
SET: '=',
|
|
1178
|
-
LP: '(',
|
|
1179
|
-
RP: ')',
|
|
1180
|
-
COMMA: ',',
|
|
1181
|
-
DOT: '.',
|
|
1182
|
-
ID: '',
|
|
1183
|
-
LIT: '',
|
|
1184
|
-
IF: '',
|
|
1185
|
-
ELSE: '',
|
|
1186
|
-
};
|
|
1187
|
-
|
|
1188
|
-
const CONTEXT = 'c';
|
|
1189
|
-
const ESCAPE = 'e';
|
|
1190
|
-
const FILTERS = 'f';
|
|
1191
|
-
|
|
1192
|
-
class ExpCompiler {
|
|
1193
|
-
constructor() {
|
|
1194
|
-
Object.defineProperty(this, "context", {
|
|
1584
|
+
class SetNode extends Traversal {
|
|
1585
|
+
constructor(val, loc, strip) {
|
|
1586
|
+
super();
|
|
1587
|
+
Object.defineProperty(this, "val", {
|
|
1195
1588
|
enumerable: true,
|
|
1196
1589
|
configurable: true,
|
|
1197
1590
|
writable: true,
|
|
1198
|
-
value:
|
|
1591
|
+
value: val
|
|
1199
1592
|
});
|
|
1200
|
-
Object.defineProperty(this, "
|
|
1593
|
+
Object.defineProperty(this, "loc", {
|
|
1201
1594
|
enumerable: true,
|
|
1202
1595
|
configurable: true,
|
|
1203
1596
|
writable: true,
|
|
1204
|
-
value:
|
|
1597
|
+
value: loc
|
|
1205
1598
|
});
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
switch (expression.type) {
|
|
1214
|
-
case 'NOT':
|
|
1215
|
-
return this.compileNot(expression);
|
|
1216
|
-
case 'ID':
|
|
1217
|
-
return this.compileId(expression);
|
|
1218
|
-
case 'LIT':
|
|
1219
|
-
return this.compileLit(expression);
|
|
1220
|
-
case 'PIPE':
|
|
1221
|
-
return this.compilePipe(expression);
|
|
1222
|
-
case 'OF':
|
|
1223
|
-
return this.compileOf(expression);
|
|
1224
|
-
case 'SET':
|
|
1225
|
-
return this.compileSet(expression);
|
|
1226
|
-
case 'IS':
|
|
1227
|
-
return this.compileIs(expression);
|
|
1228
|
-
case 'AND':
|
|
1229
|
-
case 'OR':
|
|
1230
|
-
case 'EQ':
|
|
1231
|
-
case 'NE':
|
|
1232
|
-
case 'GT':
|
|
1233
|
-
case 'LT':
|
|
1234
|
-
case 'GE':
|
|
1235
|
-
case 'LE':
|
|
1236
|
-
case 'IN':
|
|
1237
|
-
case 'NI':
|
|
1238
|
-
case 'ADD':
|
|
1239
|
-
case 'SUB':
|
|
1240
|
-
case 'MUL':
|
|
1241
|
-
case 'DIV':
|
|
1242
|
-
case 'MOD':
|
|
1243
|
-
return this.compileBinary(expression);
|
|
1244
|
-
case 'IF':
|
|
1245
|
-
return this.compileTernary(expression);
|
|
1246
|
-
case 'SEQ':
|
|
1247
|
-
return this.compileSeq(expression);
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
compileNot({ argument }) {
|
|
1251
|
-
return `!${this.compileExp(argument)}`;
|
|
1252
|
-
}
|
|
1253
|
-
compileId({ value, path, args }) {
|
|
1254
|
-
let s = `${this.context}.${value}`;
|
|
1255
|
-
if (path) {
|
|
1256
|
-
s += path.map(p => `.${p.value}`).join('');
|
|
1257
|
-
}
|
|
1258
|
-
if (args) {
|
|
1259
|
-
s += `(${args.map(arg => this.compileExp(arg)).join(',')})`;
|
|
1260
|
-
}
|
|
1261
|
-
return s;
|
|
1262
|
-
}
|
|
1263
|
-
compileLit({ value }) {
|
|
1264
|
-
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1265
|
-
}
|
|
1266
|
-
compileSeq({ elements }) {
|
|
1267
|
-
return elements.map(element => this.compileExp(element)).toString();
|
|
1268
|
-
}
|
|
1269
|
-
compilePipe({ left, right }) {
|
|
1270
|
-
const { value: name, args = [] } = right;
|
|
1271
|
-
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map(arg => arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg))].join(',')}))`;
|
|
1272
|
-
}
|
|
1273
|
-
compileOf({ left, right }) {
|
|
1274
|
-
// destructuring
|
|
1275
|
-
if (left.type === 'SEQ') {
|
|
1276
|
-
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
1277
|
-
}
|
|
1278
|
-
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
1279
|
-
}
|
|
1280
|
-
compileSet({ left, right }) {
|
|
1281
|
-
if (left.type === 'ID') {
|
|
1282
|
-
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
1283
|
-
}
|
|
1284
|
-
// destructuring
|
|
1285
|
-
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
1286
|
-
}
|
|
1287
|
-
compileIs({ left, right }) {
|
|
1288
|
-
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
1289
|
-
}
|
|
1290
|
-
compileBinary({ type, left, right }) {
|
|
1291
|
-
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
1292
|
-
return type === 'NI' ? `!${ret}` : ret;
|
|
1293
|
-
}
|
|
1294
|
-
compileTernary({ test, consequent, alternative }) {
|
|
1295
|
-
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
async function compileCall({ val, body, loc }, compiler) {
|
|
1300
|
-
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1301
|
-
await compiler.compileNodes(body);
|
|
1302
|
-
compiler.pushRaw(null, `});`);
|
|
1303
|
-
}
|
|
1304
|
-
const compilers$a = {
|
|
1305
|
-
[NodeType.CALL]: compileCall,
|
|
1306
|
-
};
|
|
1307
|
-
|
|
1308
|
-
async function compileCapture({ val, body, loc }, compiler) {
|
|
1309
|
-
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1310
|
-
await compiler.compileNodes(body);
|
|
1311
|
-
compiler.pushRaw(null, `return s;})("");`);
|
|
1312
|
-
}
|
|
1313
|
-
const compilers$9 = {
|
|
1314
|
-
[NodeType.CAPTURE]: compileCapture,
|
|
1315
|
-
};
|
|
1316
|
-
|
|
1317
|
-
async function compileComment({ val, loc }, compiler) {
|
|
1318
|
-
if (!compiler.options.stripComments) {
|
|
1319
|
-
compiler.pushStr(loc, `<!--${val}-->`);
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
const compilers$8 = {
|
|
1323
|
-
[NodeType.COMMENT]: compileComment,
|
|
1324
|
-
};
|
|
1325
|
-
|
|
1326
|
-
async function compileFor({ loop, body }, compiler) {
|
|
1327
|
-
const { context } = compiler;
|
|
1328
|
-
if (loop.left.type === 'SEQ') {
|
|
1329
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, `};`);
|
|
1330
|
-
}
|
|
1331
|
-
else {
|
|
1332
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, `};`);
|
|
1333
|
-
}
|
|
1334
|
-
await compiler.compileNodes(body);
|
|
1335
|
-
compiler.out();
|
|
1336
|
-
compiler.pushRaw(null, '}');
|
|
1337
|
-
}
|
|
1338
|
-
function compileBreak({ loc }, compiler) {
|
|
1339
|
-
compiler.pushRaw(loc, `break;`);
|
|
1340
|
-
}
|
|
1341
|
-
function compileContinue({ loc }, compiler) {
|
|
1342
|
-
compiler.pushRaw(loc, `continue;`);
|
|
1343
|
-
}
|
|
1344
|
-
const compilers$7 = {
|
|
1345
|
-
[NodeType.FOR]: compileFor,
|
|
1346
|
-
[NodeType.BREAK]: compileBreak,
|
|
1347
|
-
[NodeType.CONTINUE]: compileContinue,
|
|
1348
|
-
};
|
|
1349
|
-
|
|
1350
|
-
async function compileIf({ test, body, alternatives }, compiler) {
|
|
1351
|
-
compiler.pushRaw(test.loc, `if(${new ExpCompiler().compile(test, compiler.context)}){`);
|
|
1352
|
-
await compiler.compileNodes(body);
|
|
1353
|
-
compiler.pushRaw(null, '}');
|
|
1354
|
-
for (const branch of alternatives) {
|
|
1355
|
-
if (branch.type === NodeType.ELSE_IF) {
|
|
1356
|
-
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1357
|
-
}
|
|
1358
|
-
else {
|
|
1359
|
-
compiler.pushRaw(null, 'else{');
|
|
1360
|
-
}
|
|
1361
|
-
await compiler.compileNodes(branch.body);
|
|
1362
|
-
compiler.pushRaw(null, '}');
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
const compilers$6 = {
|
|
1366
|
-
[NodeType.IF]: compileIf,
|
|
1367
|
-
};
|
|
1368
|
-
|
|
1369
|
-
async function compileInclude({ val: path, loc }, compiler) {
|
|
1370
|
-
compiler.pushRaw(loc, `s+=await p["${path.value}"]?.()??"";`);
|
|
1371
|
-
}
|
|
1372
|
-
const compilers$5 = {
|
|
1373
|
-
[NodeType.INCLUDE]: compileInclude,
|
|
1374
|
-
};
|
|
1375
|
-
|
|
1376
|
-
async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
1377
|
-
const { context } = compiler;
|
|
1378
|
-
const { elements } = right;
|
|
1379
|
-
const expCompiler = new ExpCompiler();
|
|
1380
|
-
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${(elements).map(el => el.type === 'SET' ? `${el.left.value}=${expCompiler.compile((el.right))}` : el.value).join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements.map(el => el.type === 'SET'
|
|
1381
|
-
? el.left.value
|
|
1382
|
-
: el.value).join(','), `};`);
|
|
1383
|
-
await compiler.compileNodes(body);
|
|
1384
|
-
compiler.out();
|
|
1385
|
-
compiler.pushRaw(null, '};');
|
|
1386
|
-
}
|
|
1387
|
-
function compileCaller({ loc }, compiler) {
|
|
1388
|
-
compiler.pushRaw(loc, `await _c?.();`);
|
|
1389
|
-
}
|
|
1390
|
-
const compilers$4 = {
|
|
1391
|
-
[NodeType.MACRO]: compileMacro,
|
|
1392
|
-
[NodeType.CALLER]: compileCaller,
|
|
1393
|
-
};
|
|
1394
|
-
|
|
1395
|
-
async function compileOutput({ exp }, compiler) {
|
|
1396
|
-
compiler.pushVar(exp.loc, new ExpCompiler().compile(exp, compiler.context));
|
|
1397
|
-
}
|
|
1398
|
-
const compilers$3 = {
|
|
1399
|
-
[NodeType.OUTPUT]: compileOutput,
|
|
1400
|
-
};
|
|
1401
|
-
|
|
1402
|
-
async function compileSet({ val, loc }, compiler) {
|
|
1403
|
-
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1404
|
-
}
|
|
1405
|
-
const compilers$2 = {
|
|
1406
|
-
[NodeType.SET]: compileSet,
|
|
1407
|
-
};
|
|
1408
|
-
|
|
1409
|
-
async function compileText({ val, loc, strip: { start, end } }, compiler) {
|
|
1410
|
-
if (start || compiler.options.trimWhitespace) {
|
|
1411
|
-
val = val.replace(/^\s+/, '');
|
|
1412
|
-
}
|
|
1413
|
-
if (end || compiler.options.trimWhitespace) {
|
|
1414
|
-
val = val.replace(/\s+$/, '');
|
|
1415
|
-
}
|
|
1416
|
-
compiler.pushStr(loc, val);
|
|
1417
|
-
}
|
|
1418
|
-
const compilers$1 = {
|
|
1419
|
-
[NodeType.TEXT]: compileText,
|
|
1420
|
-
};
|
|
1421
|
-
|
|
1422
|
-
const compilers = {
|
|
1423
|
-
...compilers$6,
|
|
1424
|
-
...compilers$7,
|
|
1425
|
-
...compilers$5,
|
|
1426
|
-
...compilers$b,
|
|
1427
|
-
...compilers$1,
|
|
1428
|
-
...compilers$3,
|
|
1429
|
-
...compilers$8,
|
|
1430
|
-
...compilers$4,
|
|
1431
|
-
...compilers$a,
|
|
1432
|
-
...compilers$2,
|
|
1433
|
-
...compilers$9,
|
|
1434
|
-
};
|
|
1435
|
-
|
|
1436
|
-
const parserOptions = {
|
|
1437
|
-
commentOpen: '{{#',
|
|
1438
|
-
commentClose: '}}',
|
|
1439
|
-
directiveOpen: '{{',
|
|
1440
|
-
directiveClose: '}}',
|
|
1441
|
-
outputOpen: '{{=',
|
|
1442
|
-
outputClose: '}}',
|
|
1443
|
-
parsers,
|
|
1444
|
-
debug: () => { },
|
|
1445
|
-
};
|
|
1446
|
-
const compilerOptions = {
|
|
1447
|
-
...parserOptions,
|
|
1448
|
-
trimWhitespace: false,
|
|
1449
|
-
stripComments: false,
|
|
1450
|
-
compilers,
|
|
1451
|
-
loader: async () => '',
|
|
1452
|
-
};
|
|
1453
|
-
const renderOptions = {
|
|
1454
|
-
...compilerOptions,
|
|
1455
|
-
globals: {},
|
|
1456
|
-
filters,
|
|
1457
|
-
autoEscape: true,
|
|
1458
|
-
};
|
|
1459
|
-
|
|
1460
|
-
/**
|
|
1461
|
-
* Contains the source map information.
|
|
1462
|
-
* @example
|
|
1463
|
-
* - template:
|
|
1464
|
-
* {{ foo }}
|
|
1465
|
-
*
|
|
1466
|
-
* - compiled:
|
|
1467
|
-
* s+=e(foo);
|
|
1468
|
-
*
|
|
1469
|
-
* - sourceMap:
|
|
1470
|
-
* Mapping {
|
|
1471
|
-
* source: {
|
|
1472
|
-
* start: {
|
|
1473
|
-
* line: 2,
|
|
1474
|
-
* column: 4,
|
|
1475
|
-
* },
|
|
1476
|
-
* end: {
|
|
1477
|
-
* line: 3,
|
|
1478
|
-
* column: 5,
|
|
1479
|
-
* },
|
|
1480
|
-
* },
|
|
1481
|
-
* target: {
|
|
1482
|
-
* start: {
|
|
1483
|
-
* line: 2,
|
|
1484
|
-
* column: 3,
|
|
1485
|
-
* },
|
|
1486
|
-
* end: {
|
|
1487
|
-
* line: 3,
|
|
1488
|
-
* column: 8,
|
|
1489
|
-
* },
|
|
1490
|
-
* }
|
|
1491
|
-
* }
|
|
1492
|
-
*/
|
|
1493
|
-
class SourceMap {
|
|
1494
|
-
constructor() {
|
|
1495
|
-
Object.defineProperty(this, "mappings", {
|
|
1599
|
+
Object.defineProperty(this, "strip", {
|
|
1600
|
+
enumerable: true,
|
|
1601
|
+
configurable: true,
|
|
1602
|
+
writable: true,
|
|
1603
|
+
value: strip
|
|
1604
|
+
});
|
|
1605
|
+
Object.defineProperty(this, "type", {
|
|
1496
1606
|
enumerable: true,
|
|
1497
1607
|
configurable: true,
|
|
1498
1608
|
writable: true,
|
|
1499
|
-
value:
|
|
1500
|
-
});
|
|
1501
|
-
}
|
|
1502
|
-
addMapping(source, target) {
|
|
1503
|
-
this.mappings.push({
|
|
1504
|
-
source,
|
|
1505
|
-
target,
|
|
1609
|
+
value: 'SET'
|
|
1506
1610
|
});
|
|
1507
1611
|
}
|
|
1508
|
-
getSourceLoc({ line, column }) {
|
|
1509
|
-
return this.mappings
|
|
1510
|
-
.filter(({ target: { start, end } }) => start.line <= line
|
|
1511
|
-
&& end.line >= line
|
|
1512
|
-
&& (start.line < line || start.column <= column)
|
|
1513
|
-
&& (end.line > line || end.column >= column))
|
|
1514
|
-
.map(({ source }) => source);
|
|
1515
|
-
}
|
|
1516
1612
|
}
|
|
1517
1613
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
enumerable: true,
|
|
1523
|
-
configurable: true,
|
|
1524
|
-
writable: true,
|
|
1525
|
-
value: ''
|
|
1526
|
-
});
|
|
1527
|
-
}
|
|
1528
|
-
get script() {
|
|
1529
|
-
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
1530
|
-
}
|
|
1531
|
-
start() {
|
|
1532
|
-
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
1533
|
-
}
|
|
1534
|
-
end() {
|
|
1535
|
-
this.pushRaw(null, 'return s;', '})();');
|
|
1536
|
-
}
|
|
1537
|
-
pushRaw(loc, ...lines) {
|
|
1538
|
-
const start = this.code.length;
|
|
1539
|
-
for (const line of lines) {
|
|
1540
|
-
this.code += line;
|
|
1541
|
-
}
|
|
1542
|
-
if (!loc) {
|
|
1543
|
-
return;
|
|
1544
|
-
}
|
|
1545
|
-
this.addMapping(loc, {
|
|
1546
|
-
start: {
|
|
1547
|
-
line: 1,
|
|
1548
|
-
column: start,
|
|
1549
|
-
},
|
|
1550
|
-
end: {
|
|
1551
|
-
line: 1,
|
|
1552
|
-
column: this.code.length,
|
|
1553
|
-
},
|
|
1554
|
-
});
|
|
1614
|
+
function parseSet(token, parser) {
|
|
1615
|
+
if (!token.expression) {
|
|
1616
|
+
parser.emitExpErr(token);
|
|
1617
|
+
return;
|
|
1555
1618
|
}
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1619
|
+
parser.advance();
|
|
1620
|
+
return new SetNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
1621
|
+
}
|
|
1622
|
+
const parsers$1 = {
|
|
1623
|
+
set: parseSet,
|
|
1624
|
+
};
|
|
1625
|
+
|
|
1626
|
+
const plugin$1 = {
|
|
1627
|
+
compilers: compilers$1,
|
|
1628
|
+
parsers: parsers$1,
|
|
1629
|
+
};
|
|
1630
|
+
|
|
1631
|
+
async function compileText({ val, loc, strip: { start, end } }, compiler) {
|
|
1632
|
+
if (start || compiler.options.trimWhitespace) {
|
|
1633
|
+
val = val.replace(/^\s+/, '');
|
|
1563
1634
|
}
|
|
1564
|
-
|
|
1565
|
-
|
|
1635
|
+
if (end || compiler.options.trimWhitespace) {
|
|
1636
|
+
val = val.replace(/\s+$/, '');
|
|
1566
1637
|
}
|
|
1638
|
+
compiler.pushStr(loc, val);
|
|
1567
1639
|
}
|
|
1640
|
+
const compilers = {
|
|
1641
|
+
TEXT: compileText,
|
|
1642
|
+
};
|
|
1568
1643
|
|
|
1569
|
-
class
|
|
1570
|
-
constructor() {
|
|
1644
|
+
class TextNode extends Traversal {
|
|
1645
|
+
constructor(val, loc, strip) {
|
|
1571
1646
|
super();
|
|
1572
|
-
Object.defineProperty(this, "
|
|
1647
|
+
Object.defineProperty(this, "val", {
|
|
1573
1648
|
enumerable: true,
|
|
1574
1649
|
configurable: true,
|
|
1575
1650
|
writable: true,
|
|
1576
|
-
value:
|
|
1651
|
+
value: val
|
|
1577
1652
|
});
|
|
1578
|
-
Object.defineProperty(this, "
|
|
1653
|
+
Object.defineProperty(this, "loc", {
|
|
1579
1654
|
enumerable: true,
|
|
1580
1655
|
configurable: true,
|
|
1581
1656
|
writable: true,
|
|
1582
|
-
value:
|
|
1657
|
+
value: loc
|
|
1583
1658
|
});
|
|
1584
|
-
Object.defineProperty(this, "
|
|
1659
|
+
Object.defineProperty(this, "strip", {
|
|
1585
1660
|
enumerable: true,
|
|
1586
1661
|
configurable: true,
|
|
1587
1662
|
writable: true,
|
|
1588
|
-
value:
|
|
1663
|
+
value: strip
|
|
1664
|
+
});
|
|
1665
|
+
Object.defineProperty(this, "type", {
|
|
1666
|
+
enumerable: true,
|
|
1667
|
+
configurable: true,
|
|
1668
|
+
writable: true,
|
|
1669
|
+
value: 'TEXT'
|
|
1589
1670
|
});
|
|
1590
|
-
}
|
|
1591
|
-
in() {
|
|
1592
|
-
this.context = `${this.context}_${this.index++}`;
|
|
1593
|
-
this.contexts.push(this.context);
|
|
1594
|
-
return this.context;
|
|
1595
|
-
}
|
|
1596
|
-
out() {
|
|
1597
|
-
if (this.context === CONTEXT) {
|
|
1598
|
-
return;
|
|
1599
|
-
}
|
|
1600
|
-
this.contexts.pop();
|
|
1601
|
-
this.context = this.contexts.at(-1) ?? CONTEXT;
|
|
1602
1671
|
}
|
|
1603
1672
|
}
|
|
1604
1673
|
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
RP: 20,
|
|
1612
|
-
OR: 25,
|
|
1613
|
-
AND: 30,
|
|
1614
|
-
IS: 40,
|
|
1615
|
-
EQ: 40,
|
|
1616
|
-
NE: 40,
|
|
1617
|
-
GT: 40,
|
|
1618
|
-
LT: 40,
|
|
1619
|
-
GE: 40,
|
|
1620
|
-
LE: 40,
|
|
1621
|
-
IN: 40,
|
|
1622
|
-
NI: 40,
|
|
1623
|
-
OF: 40,
|
|
1624
|
-
ADD: 50,
|
|
1625
|
-
SUB: 50,
|
|
1626
|
-
MUL: 60,
|
|
1627
|
-
DIV: 60,
|
|
1628
|
-
MOD: 60,
|
|
1629
|
-
NOT: 70,
|
|
1630
|
-
PIPE: 80,
|
|
1631
|
-
DOT: 90,
|
|
1632
|
-
ID: 90,
|
|
1633
|
-
LIT: 90,
|
|
1674
|
+
function parseText(token, parser) {
|
|
1675
|
+
parser.advance();
|
|
1676
|
+
return new TextNode(token.val, token.loc, token.strip);
|
|
1677
|
+
}
|
|
1678
|
+
const parsers = {
|
|
1679
|
+
text: parseText,
|
|
1634
1680
|
};
|
|
1635
1681
|
|
|
1636
|
-
const
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1682
|
+
const plugin = {
|
|
1683
|
+
compilers,
|
|
1684
|
+
parsers,
|
|
1685
|
+
};
|
|
1686
|
+
|
|
1687
|
+
const plugins = [
|
|
1688
|
+
plugin$a,
|
|
1689
|
+
plugin$9,
|
|
1690
|
+
plugin$7,
|
|
1691
|
+
plugin$8,
|
|
1692
|
+
plugin$6,
|
|
1693
|
+
plugin$5,
|
|
1694
|
+
plugin$4,
|
|
1695
|
+
plugin$3,
|
|
1696
|
+
plugin$1,
|
|
1697
|
+
plugin,
|
|
1698
|
+
plugin$2,
|
|
1699
|
+
];
|
|
1700
|
+
|
|
1701
|
+
const baseOptions = {
|
|
1702
|
+
filters: _filters,
|
|
1703
|
+
parsers: {},
|
|
1704
|
+
compilers: {},
|
|
1705
|
+
plugins,
|
|
1706
|
+
debug: () => { },
|
|
1707
|
+
};
|
|
1708
|
+
const parserOptions = {
|
|
1709
|
+
...baseOptions,
|
|
1710
|
+
commentOpen: '{{#',
|
|
1711
|
+
commentClose: '}}',
|
|
1712
|
+
directiveOpen: '{{',
|
|
1713
|
+
directiveClose: '}}',
|
|
1714
|
+
outputOpen: '{{=',
|
|
1715
|
+
outputClose: '}}',
|
|
1716
|
+
};
|
|
1717
|
+
const compilerOptions = {
|
|
1718
|
+
...parserOptions,
|
|
1719
|
+
trimWhitespace: false,
|
|
1720
|
+
stripComments: false,
|
|
1721
|
+
loader: async () => '',
|
|
1722
|
+
};
|
|
1723
|
+
const renderOptions = {
|
|
1724
|
+
...compilerOptions,
|
|
1725
|
+
globals: {},
|
|
1726
|
+
autoEscape: true,
|
|
1727
|
+
};
|
|
1728
|
+
const mergeOptions = (defaultOptions, options) => {
|
|
1729
|
+
const mergedOptions = {
|
|
1730
|
+
...defaultOptions,
|
|
1731
|
+
...options,
|
|
1732
|
+
};
|
|
1733
|
+
defaultOptions.plugins?.forEach((plugin) => {
|
|
1734
|
+
Object.assign(mergedOptions.filters, plugin.filters ?? {});
|
|
1735
|
+
Object.assign(mergedOptions.parsers, plugin.parsers ?? {});
|
|
1736
|
+
Object.assign(mergedOptions.compilers, plugin.compilers ?? {});
|
|
1737
|
+
});
|
|
1738
|
+
options?.plugins?.forEach((plugin) => {
|
|
1739
|
+
Object.assign(mergedOptions.filters, plugin.filters ?? {});
|
|
1740
|
+
Object.assign(mergedOptions.parsers, plugin.parsers ?? {});
|
|
1741
|
+
Object.assign(mergedOptions.compilers, plugin.compilers ?? {});
|
|
1742
|
+
});
|
|
1743
|
+
return mergedOptions;
|
|
1667
1744
|
};
|
|
1668
1745
|
|
|
1669
|
-
/**
|
|
1670
|
-
* Update position by a given string, and return a new position with the
|
|
1671
|
-
* updated values.
|
|
1672
|
-
*/
|
|
1673
1746
|
function updatePosition(str, pos) {
|
|
1674
1747
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1675
1748
|
if (newlines > 0) {
|
|
@@ -1686,6 +1759,38 @@ function updatePosition(str, pos) {
|
|
|
1686
1759
|
};
|
|
1687
1760
|
}
|
|
1688
1761
|
|
|
1762
|
+
const expTokenTypes = {
|
|
1763
|
+
and: 'AND',
|
|
1764
|
+
or: 'OR',
|
|
1765
|
+
not: 'NOT',
|
|
1766
|
+
is: 'IS',
|
|
1767
|
+
eq: 'EQ',
|
|
1768
|
+
ne: 'NE',
|
|
1769
|
+
gt: 'GT',
|
|
1770
|
+
lt: 'LT',
|
|
1771
|
+
ge: 'GE',
|
|
1772
|
+
le: 'LE',
|
|
1773
|
+
in: 'IN',
|
|
1774
|
+
ni: 'NI',
|
|
1775
|
+
of: 'OF',
|
|
1776
|
+
'+': 'ADD',
|
|
1777
|
+
'-': 'SUB',
|
|
1778
|
+
'*': 'MUL',
|
|
1779
|
+
'/': 'DIV',
|
|
1780
|
+
'%': 'MOD',
|
|
1781
|
+
if: 'IF',
|
|
1782
|
+
else: 'ELSE',
|
|
1783
|
+
true: 'LIT',
|
|
1784
|
+
false: 'LIT',
|
|
1785
|
+
null: 'LIT',
|
|
1786
|
+
undefined: 'LIT',
|
|
1787
|
+
'|': 'PIPE',
|
|
1788
|
+
'=': 'ASSIGN',
|
|
1789
|
+
'(': 'LP',
|
|
1790
|
+
')': 'RP',
|
|
1791
|
+
',': 'COMMA',
|
|
1792
|
+
'.': 'DOT',
|
|
1793
|
+
};
|
|
1689
1794
|
class ExpTokenizer {
|
|
1690
1795
|
constructor(template) {
|
|
1691
1796
|
Object.defineProperty(this, "template", {
|
|
@@ -1744,7 +1849,7 @@ class ExpTokenizer {
|
|
|
1744
1849
|
this.index++;
|
|
1745
1850
|
continue;
|
|
1746
1851
|
}
|
|
1747
|
-
if (char === '
|
|
1852
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
1748
1853
|
this.readString(char);
|
|
1749
1854
|
continue;
|
|
1750
1855
|
}
|
|
@@ -1843,7 +1948,9 @@ class ExpTokenizer {
|
|
|
1843
1948
|
}
|
|
1844
1949
|
break;
|
|
1845
1950
|
}
|
|
1846
|
-
const type = value in expTokenTypes
|
|
1951
|
+
const type = value in expTokenTypes
|
|
1952
|
+
? expTokenTypes[value]
|
|
1953
|
+
: 'ID';
|
|
1847
1954
|
this.tokens.push({
|
|
1848
1955
|
type,
|
|
1849
1956
|
value: type === 'LIT'
|
|
@@ -1894,6 +2001,36 @@ function isIdentifierChar(char) {
|
|
|
1894
2001
|
return /[\w$]/.test(char);
|
|
1895
2002
|
}
|
|
1896
2003
|
|
|
2004
|
+
const expTokenPrecedences = {
|
|
2005
|
+
COMMA: 5,
|
|
2006
|
+
IF: 10,
|
|
2007
|
+
ELSE: 10,
|
|
2008
|
+
ASSIGN: 15,
|
|
2009
|
+
LP: 20,
|
|
2010
|
+
RP: 20,
|
|
2011
|
+
OR: 25,
|
|
2012
|
+
AND: 30,
|
|
2013
|
+
IS: 40,
|
|
2014
|
+
EQ: 40,
|
|
2015
|
+
NE: 40,
|
|
2016
|
+
GT: 40,
|
|
2017
|
+
LT: 40,
|
|
2018
|
+
GE: 40,
|
|
2019
|
+
LE: 40,
|
|
2020
|
+
IN: 40,
|
|
2021
|
+
NI: 40,
|
|
2022
|
+
OF: 40,
|
|
2023
|
+
ADD: 50,
|
|
2024
|
+
SUB: 50,
|
|
2025
|
+
MUL: 60,
|
|
2026
|
+
DIV: 60,
|
|
2027
|
+
MOD: 60,
|
|
2028
|
+
NOT: 70,
|
|
2029
|
+
PIPE: 80,
|
|
2030
|
+
DOT: 90,
|
|
2031
|
+
ID: 90,
|
|
2032
|
+
LIT: 90,
|
|
2033
|
+
};
|
|
1897
2034
|
class ExpParser {
|
|
1898
2035
|
constructor(template) {
|
|
1899
2036
|
Object.defineProperty(this, "template", {
|
|
@@ -1958,12 +2095,12 @@ class ExpParser {
|
|
|
1958
2095
|
}
|
|
1959
2096
|
if (token.type === 'LP') {
|
|
1960
2097
|
if (!this.peek()) {
|
|
1961
|
-
throw new CompileError(
|
|
2098
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1962
2099
|
}
|
|
1963
2100
|
const elements = this.parseSequence();
|
|
1964
2101
|
const rp = this.consume('RP');
|
|
1965
2102
|
if (!rp) {
|
|
1966
|
-
throw new CompileError(
|
|
2103
|
+
throw new CompileError('expected "RP" after "LP"', this.template, token.loc);
|
|
1967
2104
|
}
|
|
1968
2105
|
left = {
|
|
1969
2106
|
...token,
|
|
@@ -1978,12 +2115,12 @@ class ExpParser {
|
|
|
1978
2115
|
}
|
|
1979
2116
|
if (token.type === 'NOT') {
|
|
1980
2117
|
if (!this.peek()) {
|
|
1981
|
-
throw new CompileError(
|
|
2118
|
+
throw new CompileError('unexpected end of expression', this.template, token.loc);
|
|
1982
2119
|
}
|
|
1983
2120
|
left = {
|
|
1984
2121
|
...token,
|
|
1985
2122
|
type: 'NOT',
|
|
1986
|
-
argument: this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined),
|
|
2123
|
+
argument: this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined),
|
|
1987
2124
|
};
|
|
1988
2125
|
continue;
|
|
1989
2126
|
}
|
|
@@ -1993,14 +2130,14 @@ class ExpParser {
|
|
|
1993
2130
|
type: 'ID',
|
|
1994
2131
|
value: token.value,
|
|
1995
2132
|
};
|
|
1996
|
-
if (this.
|
|
2133
|
+
if (this.match('DOT')) {
|
|
1997
2134
|
left.path = this.parsePath();
|
|
1998
2135
|
}
|
|
1999
2136
|
const lp = this.consume('LP');
|
|
2000
2137
|
if (lp) {
|
|
2001
2138
|
left.args = this.parseSequence();
|
|
2002
2139
|
if (!this.consume('RP')) {
|
|
2003
|
-
throw new CompileError(
|
|
2140
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2004
2141
|
}
|
|
2005
2142
|
continue;
|
|
2006
2143
|
}
|
|
@@ -2014,7 +2151,7 @@ class ExpParser {
|
|
|
2014
2151
|
continue;
|
|
2015
2152
|
}
|
|
2016
2153
|
if (token.type === 'IF') {
|
|
2017
|
-
const test = this.parseExp(t => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2154
|
+
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2018
2155
|
if (!test) {
|
|
2019
2156
|
throw new CompileError('expected test expression', this.template, token.loc);
|
|
2020
2157
|
}
|
|
@@ -2025,7 +2162,7 @@ class ExpParser {
|
|
|
2025
2162
|
consequent: left,
|
|
2026
2163
|
};
|
|
2027
2164
|
if (this.consume('ELSE')) {
|
|
2028
|
-
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2165
|
+
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2029
2166
|
if (!alternative) {
|
|
2030
2167
|
throw new CompileError('expected alternative expression', this.template, token.loc);
|
|
2031
2168
|
}
|
|
@@ -2033,28 +2170,28 @@ class ExpParser {
|
|
|
2033
2170
|
}
|
|
2034
2171
|
continue;
|
|
2035
2172
|
}
|
|
2036
|
-
if (token.type === 'AND'
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2173
|
+
if (token.type === 'AND' ||
|
|
2174
|
+
token.type === 'OR' ||
|
|
2175
|
+
token.type === 'IS' ||
|
|
2176
|
+
token.type === 'EQ' ||
|
|
2177
|
+
token.type === 'NE' ||
|
|
2178
|
+
token.type === 'GT' ||
|
|
2179
|
+
token.type === 'LT' ||
|
|
2180
|
+
token.type === 'GE' ||
|
|
2181
|
+
token.type === 'LE' ||
|
|
2182
|
+
token.type === 'IN' ||
|
|
2183
|
+
token.type === 'NI' ||
|
|
2184
|
+
token.type === 'OF' ||
|
|
2185
|
+
token.type === 'ADD' ||
|
|
2186
|
+
token.type === 'SUB' ||
|
|
2187
|
+
token.type === 'MUL' ||
|
|
2188
|
+
token.type === 'DIV' ||
|
|
2189
|
+
token.type === 'MOD' ||
|
|
2190
|
+
token.type === 'ASSIGN') {
|
|
2054
2191
|
if (!left) {
|
|
2055
2192
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2056
2193
|
}
|
|
2057
|
-
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
2194
|
+
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2058
2195
|
if (!right) {
|
|
2059
2196
|
throw new CompileError(`no right operand for "${token.type}"`, this.template, token.loc);
|
|
2060
2197
|
}
|
|
@@ -2070,8 +2207,8 @@ class ExpParser {
|
|
|
2070
2207
|
if (!left) {
|
|
2071
2208
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2072
2209
|
}
|
|
2073
|
-
if (!this.
|
|
2074
|
-
throw new CompileError(
|
|
2210
|
+
if (!this.match('ID')) {
|
|
2211
|
+
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2075
2212
|
}
|
|
2076
2213
|
left = {
|
|
2077
2214
|
...token,
|
|
@@ -2095,10 +2232,10 @@ class ExpParser {
|
|
|
2095
2232
|
}
|
|
2096
2233
|
const token = this.next();
|
|
2097
2234
|
if (!token) {
|
|
2098
|
-
throw new CompileError(
|
|
2235
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2099
2236
|
}
|
|
2100
2237
|
if (token.type !== 'ID') {
|
|
2101
|
-
throw new CompileError(
|
|
2238
|
+
throw new CompileError('expected "ID" after "DOT"', this.template, dot.loc);
|
|
2102
2239
|
}
|
|
2103
2240
|
ids.push({
|
|
2104
2241
|
...token,
|
|
@@ -2110,8 +2247,8 @@ class ExpParser {
|
|
|
2110
2247
|
}
|
|
2111
2248
|
parseSequence() {
|
|
2112
2249
|
const elements = [];
|
|
2113
|
-
while (!this.
|
|
2114
|
-
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2250
|
+
while (!this.match('RP')) {
|
|
2251
|
+
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2115
2252
|
if (element) {
|
|
2116
2253
|
elements.push(element);
|
|
2117
2254
|
}
|
|
@@ -2135,7 +2272,7 @@ class ExpParser {
|
|
|
2135
2272
|
right.args = args;
|
|
2136
2273
|
}
|
|
2137
2274
|
if (!this.consume('RP')) {
|
|
2138
|
-
throw new CompileError(
|
|
2275
|
+
throw new CompileError('expected "RP" after "LP"', this.template, lp.loc);
|
|
2139
2276
|
}
|
|
2140
2277
|
}
|
|
2141
2278
|
return right;
|
|
@@ -2149,11 +2286,11 @@ class ExpParser {
|
|
|
2149
2286
|
peek() {
|
|
2150
2287
|
return this.tokens.at(this.index) ?? null;
|
|
2151
2288
|
}
|
|
2152
|
-
|
|
2289
|
+
match(type) {
|
|
2153
2290
|
return this.peek()?.type === type;
|
|
2154
2291
|
}
|
|
2155
2292
|
consume(type) {
|
|
2156
|
-
if (this.
|
|
2293
|
+
if (this.match(type)) {
|
|
2157
2294
|
return this.next();
|
|
2158
2295
|
}
|
|
2159
2296
|
}
|
|
@@ -2224,28 +2361,13 @@ class Tokenizer {
|
|
|
2224
2361
|
writable: true,
|
|
2225
2362
|
value: 1
|
|
2226
2363
|
});
|
|
2227
|
-
this.options =
|
|
2228
|
-
...parserOptions,
|
|
2229
|
-
...options,
|
|
2230
|
-
};
|
|
2364
|
+
this.options = mergeOptions(parserOptions, options);
|
|
2231
2365
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2232
|
-
this.tokenPatterns =
|
|
2233
|
-
[
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
],
|
|
2238
|
-
[
|
|
2239
|
-
TokenType.DIRECTIVE,
|
|
2240
|
-
directiveOpen,
|
|
2241
|
-
directiveClose,
|
|
2242
|
-
],
|
|
2243
|
-
[
|
|
2244
|
-
TokenType.OUTPUT,
|
|
2245
|
-
outputOpen,
|
|
2246
|
-
outputClose,
|
|
2247
|
-
],
|
|
2248
|
-
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1));
|
|
2366
|
+
this.tokenPatterns = [
|
|
2367
|
+
[TokenType.COMMENT, commentOpen, commentClose],
|
|
2368
|
+
[TokenType.DIRECTIVE, directiveOpen, directiveClose],
|
|
2369
|
+
[TokenType.OUTPUT, outputOpen, outputClose],
|
|
2370
|
+
].sort(([, open1], [, open2]) => open1.length > open2.length ? -1 : 1);
|
|
2249
2371
|
}
|
|
2250
2372
|
tokenize(template) {
|
|
2251
2373
|
this.template = template;
|
|
@@ -2314,7 +2436,9 @@ class Tokenizer {
|
|
|
2314
2436
|
: {
|
|
2315
2437
|
val: val.slice(i + 1),
|
|
2316
2438
|
loc: {
|
|
2317
|
-
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2439
|
+
start: updatePosition(val.slice(0, i + 1), updatePosition(`${openMarker}${stripBefore ? '-' : ''}`, {
|
|
2440
|
+
...start,
|
|
2441
|
+
})),
|
|
2318
2442
|
end: {
|
|
2319
2443
|
line: this.line,
|
|
2320
2444
|
column: this.column - closeMarker.length - +stripAfter,
|
|
@@ -2329,41 +2453,32 @@ class Tokenizer {
|
|
|
2329
2453
|
}
|
|
2330
2454
|
consumeTextToken() {
|
|
2331
2455
|
const nextSpecialIndex = this.findNextSpecialIndex();
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
this.index = nextSpecialIndex;
|
|
2348
|
-
}
|
|
2349
|
-
else {
|
|
2350
|
-
updatePosition(this.template[this.index++], this);
|
|
2351
|
-
}
|
|
2456
|
+
const start = {
|
|
2457
|
+
line: this.line,
|
|
2458
|
+
column: this.column,
|
|
2459
|
+
};
|
|
2460
|
+
const val = this.template.slice(this.index, nextSpecialIndex);
|
|
2461
|
+
this.tokens.push({
|
|
2462
|
+
type: TokenType.TEXT,
|
|
2463
|
+
val: unescapeTag(val),
|
|
2464
|
+
loc: {
|
|
2465
|
+
start,
|
|
2466
|
+
end: updatePosition(val, this),
|
|
2467
|
+
},
|
|
2468
|
+
strip: {},
|
|
2469
|
+
});
|
|
2470
|
+
this.index = nextSpecialIndex;
|
|
2352
2471
|
}
|
|
2353
2472
|
lookahead(marker, offset = 0) {
|
|
2354
2473
|
return this.template.startsWith(marker, this.index + offset);
|
|
2355
2474
|
}
|
|
2356
2475
|
findNextSpecialIndex() {
|
|
2357
|
-
const { outputOpen, directiveOpen, commentOpen
|
|
2476
|
+
const { outputOpen, directiveOpen, commentOpen } = this.options;
|
|
2358
2477
|
const commentIndex = this.template.indexOf(commentOpen, this.index);
|
|
2359
2478
|
const directiveIndex = this.template.indexOf(directiveOpen, this.index);
|
|
2360
2479
|
const outputIndex = this.template.indexOf(outputOpen, this.index);
|
|
2361
|
-
const indexes = [
|
|
2362
|
-
|
|
2363
|
-
directiveIndex,
|
|
2364
|
-
outputIndex,
|
|
2365
|
-
]
|
|
2366
|
-
.filter(index => index !== -1)
|
|
2480
|
+
const indexes = [commentIndex, directiveIndex, outputIndex]
|
|
2481
|
+
.filter((index) => index !== -1)
|
|
2367
2482
|
.sort((a, b) => a - b);
|
|
2368
2483
|
return indexes.length ? indexes[0] : this.length;
|
|
2369
2484
|
}
|
|
@@ -2398,10 +2513,10 @@ class Parser extends Tokenizer {
|
|
|
2398
2513
|
let prevToken = null;
|
|
2399
2514
|
while (true) {
|
|
2400
2515
|
const token = this.peek();
|
|
2401
|
-
if (!token
|
|
2402
|
-
|
|
2403
|
-
&&
|
|
2404
|
-
|
|
2516
|
+
if (!token ||
|
|
2517
|
+
(token.type === TokenType.DIRECTIVE &&
|
|
2518
|
+
names &&
|
|
2519
|
+
this.match(names, token))) {
|
|
2405
2520
|
break;
|
|
2406
2521
|
}
|
|
2407
2522
|
if (prevToken?.strip?.after && token.type === TokenType.TEXT) {
|
|
@@ -2410,49 +2525,23 @@ class Parser extends Tokenizer {
|
|
|
2410
2525
|
if (token.strip?.before && prevToken?.type === TokenType.TEXT) {
|
|
2411
2526
|
prevToken.strip.end = true;
|
|
2412
2527
|
}
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
nodes.push(this.createComment(token));
|
|
2416
|
-
break;
|
|
2417
|
-
case TokenType.DIRECTIVE:
|
|
2418
|
-
nodes.push(this.createDirective(token));
|
|
2419
|
-
break;
|
|
2420
|
-
case TokenType.OUTPUT:
|
|
2421
|
-
nodes.push(this.createOutput(token));
|
|
2422
|
-
break;
|
|
2423
|
-
case TokenType.TEXT:
|
|
2424
|
-
nodes.push(this.createText(token));
|
|
2425
|
-
break;
|
|
2426
|
-
}
|
|
2528
|
+
nodes.push(this.options.parsers[token.name?.toLowerCase() ??
|
|
2529
|
+
token.type?.toLowerCase()]?.(token, this) ?? this.createUnknownNode(token));
|
|
2427
2530
|
prevToken = token;
|
|
2428
2531
|
}
|
|
2429
2532
|
return nodes;
|
|
2430
2533
|
}
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
}
|
|
2435
|
-
createOutput({ val, loc, strip }) {
|
|
2436
|
-
this.advance();
|
|
2437
|
-
return new OutputNode(val, loc, strip, this.parseExp({ val, loc }));
|
|
2438
|
-
}
|
|
2439
|
-
createText({ val, loc, strip }) {
|
|
2440
|
-
this.advance();
|
|
2441
|
-
return new TextNode(val, loc, strip);
|
|
2442
|
-
}
|
|
2443
|
-
createDirective(token) {
|
|
2444
|
-
return this.options.parsers[token.name.toLowerCase()]?.(token, this)
|
|
2445
|
-
?? this.createUnknownDirective(token);
|
|
2446
|
-
}
|
|
2447
|
-
createUnexpectedDirective(token) {
|
|
2448
|
-
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
2534
|
+
createUnexpectedNode(token) {
|
|
2535
|
+
const name = token.name ?? token.type;
|
|
2536
|
+
this.options.debug?.(new CompileError(`Unexpected "${name}" directive`, this.template, token.loc));
|
|
2449
2537
|
this.advance();
|
|
2450
|
-
return new
|
|
2538
|
+
return new UnexpectedNode(name, token.val, token.loc, token.strip);
|
|
2451
2539
|
}
|
|
2452
|
-
|
|
2453
|
-
|
|
2540
|
+
createUnknownNode(token) {
|
|
2541
|
+
const name = token.name ?? token.type;
|
|
2542
|
+
this.options.debug?.(new CompileError(`Unknown "${name}" node`, this.template, token.loc));
|
|
2454
2543
|
this.advance();
|
|
2455
|
-
return new
|
|
2544
|
+
return new UnknownNode(name, token.val, token.loc, token.strip);
|
|
2456
2545
|
}
|
|
2457
2546
|
parseExp({ val, loc }) {
|
|
2458
2547
|
return this.expParser.parse(val, loc);
|
|
@@ -2466,15 +2555,10 @@ class Parser extends Tokenizer {
|
|
|
2466
2555
|
match(names, token = this.peek()) {
|
|
2467
2556
|
return names.includes(token?.name);
|
|
2468
2557
|
}
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
}
|
|
2474
|
-
requireNoExpression({ expression, name, loc }) {
|
|
2475
|
-
if (expression) {
|
|
2476
|
-
throw this.options.debug?.(new CompileError(`"${name}" should not have expression`, this.template, loc));
|
|
2477
|
-
}
|
|
2558
|
+
emitExpErr({ name, loc }, required = true) {
|
|
2559
|
+
this.options.debug?.(new CompileError(required
|
|
2560
|
+
? `"${name}" requires expression`
|
|
2561
|
+
: `"${name}" should not have expression`, this.template, loc));
|
|
2478
2562
|
}
|
|
2479
2563
|
}
|
|
2480
2564
|
|
|
@@ -2523,10 +2607,7 @@ class Compiler extends Context {
|
|
|
2523
2607
|
writable: true,
|
|
2524
2608
|
value: void 0
|
|
2525
2609
|
});
|
|
2526
|
-
this.options =
|
|
2527
|
-
...compilerOptions,
|
|
2528
|
-
...options,
|
|
2529
|
-
};
|
|
2610
|
+
this.options = mergeOptions(compilerOptions, options);
|
|
2530
2611
|
}
|
|
2531
2612
|
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2532
2613
|
this.template = template;
|
|
@@ -2539,26 +2620,26 @@ class Compiler extends Context {
|
|
|
2539
2620
|
await this.parseTemplates();
|
|
2540
2621
|
if (this.isRoot) {
|
|
2541
2622
|
if (this.partials.size) {
|
|
2542
|
-
this.pushRaw(null,
|
|
2623
|
+
this.pushRaw(null, 'const p={');
|
|
2543
2624
|
for (const [path, code] of this.partials) {
|
|
2544
|
-
this.pushRaw(null, `"${path}":async()=>{`, code,
|
|
2625
|
+
this.pushRaw(null, `"${path}":async()=>{`, code, '},');
|
|
2545
2626
|
}
|
|
2546
|
-
this.pushRaw(null,
|
|
2627
|
+
this.pushRaw(null, '};');
|
|
2547
2628
|
}
|
|
2548
2629
|
if (this.blocks.size) {
|
|
2549
|
-
this.pushRaw(null,
|
|
2550
|
-
for (const [name,
|
|
2551
|
-
this.pushRaw(null, `"${name}":{`,
|
|
2630
|
+
this.pushRaw(null, 'const b={');
|
|
2631
|
+
for (const [name, _blocks] of this.blocks) {
|
|
2632
|
+
this.pushRaw(null, `"${name}":{`, 'u:0,', 's:[');
|
|
2552
2633
|
this.state.block = name;
|
|
2553
|
-
for (const block of
|
|
2554
|
-
this.pushRaw(null,
|
|
2634
|
+
for (const block of _blocks) {
|
|
2635
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
2555
2636
|
await this.compileNodes(block.body);
|
|
2556
|
-
this.pushRaw(null,
|
|
2637
|
+
this.pushRaw(null, 'return s;', '},');
|
|
2557
2638
|
}
|
|
2558
2639
|
this.state.block = undefined;
|
|
2559
|
-
this.pushRaw(null,
|
|
2640
|
+
this.pushRaw(null, '],', '},');
|
|
2560
2641
|
}
|
|
2561
|
-
this.pushRaw(null,
|
|
2642
|
+
this.pushRaw(null, '};');
|
|
2562
2643
|
}
|
|
2563
2644
|
}
|
|
2564
2645
|
await this.compileNode(this.rootNode);
|
|
@@ -2583,7 +2664,7 @@ class Compiler extends Context {
|
|
|
2583
2664
|
}
|
|
2584
2665
|
}
|
|
2585
2666
|
for (const block of blocks) {
|
|
2586
|
-
const { val: { value } } = block;
|
|
2667
|
+
const { val: { value }, } = block;
|
|
2587
2668
|
if (!this.blocks.has(value)) {
|
|
2588
2669
|
this.blocks.set(value, []);
|
|
2589
2670
|
}
|
|
@@ -2594,12 +2675,12 @@ class Compiler extends Context {
|
|
|
2594
2675
|
const partials = new Map();
|
|
2595
2676
|
const blocks = [];
|
|
2596
2677
|
const traverse = (node) => {
|
|
2597
|
-
if (node.type ===
|
|
2678
|
+
if (node.type === 'INCLUDE') {
|
|
2598
2679
|
if (!partials.has(node.val.value)) {
|
|
2599
2680
|
partials.set(node.val.value, node);
|
|
2600
2681
|
}
|
|
2601
2682
|
}
|
|
2602
|
-
if (node.type ===
|
|
2683
|
+
if (node.type === 'BLOCK') {
|
|
2603
2684
|
blocks.push(node);
|
|
2604
2685
|
}
|
|
2605
2686
|
node.traverse(traverse);
|
|
@@ -2611,7 +2692,7 @@ class Compiler extends Context {
|
|
|
2611
2692
|
};
|
|
2612
2693
|
}
|
|
2613
2694
|
async compileNode(node) {
|
|
2614
|
-
if (node.type ===
|
|
2695
|
+
if (node.type === 'TEMPLATE') {
|
|
2615
2696
|
await this.compileNodes(node.body);
|
|
2616
2697
|
return;
|
|
2617
2698
|
}
|
|
@@ -2619,7 +2700,7 @@ class Compiler extends Context {
|
|
|
2619
2700
|
await this.options.compilers[node.type](node, this);
|
|
2620
2701
|
return;
|
|
2621
2702
|
}
|
|
2622
|
-
this.options.debug?.(new CompileError(`Unknown "${node.name}"
|
|
2703
|
+
this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
|
|
2623
2704
|
}
|
|
2624
2705
|
async compileNodes(nodes) {
|
|
2625
2706
|
for (const child of nodes) {
|
|
@@ -2632,7 +2713,7 @@ class Compiler extends Context {
|
|
|
2632
2713
|
this.partials.set(path, code);
|
|
2633
2714
|
}
|
|
2634
2715
|
catch {
|
|
2635
|
-
this.options.debug?.(new CompileError(
|
|
2716
|
+
this.options.debug?.(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2636
2717
|
}
|
|
2637
2718
|
}
|
|
2638
2719
|
}
|
|
@@ -2662,18 +2743,14 @@ class RenderError extends CompileError {
|
|
|
2662
2743
|
}
|
|
2663
2744
|
|
|
2664
2745
|
class Renderer {
|
|
2665
|
-
constructor(
|
|
2746
|
+
constructor(options) {
|
|
2666
2747
|
Object.defineProperty(this, "options", {
|
|
2667
2748
|
enumerable: true,
|
|
2668
2749
|
configurable: true,
|
|
2669
2750
|
writable: true,
|
|
2670
2751
|
value: void 0
|
|
2671
2752
|
});
|
|
2672
|
-
this.options =
|
|
2673
|
-
Object.assign(this.options.globals, globals);
|
|
2674
|
-
Object.assign(this.options.filters, filters);
|
|
2675
|
-
Object.assign(this.options.parsers, parsers);
|
|
2676
|
-
Object.assign(this.options.compilers, compilers);
|
|
2753
|
+
this.options = mergeOptions(renderOptions, options);
|
|
2677
2754
|
}
|
|
2678
2755
|
async render(template, globals) {
|
|
2679
2756
|
const compiler = new Compiler(this.options);
|
|
@@ -2684,24 +2761,27 @@ class Renderer {
|
|
|
2684
2761
|
return `${v}`;
|
|
2685
2762
|
}
|
|
2686
2763
|
return this.options.autoEscape ? escape(v) : v;
|
|
2687
|
-
}, { ...this.options.filters, ...
|
|
2764
|
+
}, { ...this.options.filters, ..._filters });
|
|
2688
2765
|
}
|
|
2689
2766
|
catch (error) {
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
throw new RenderError(error.message, template, () => {
|
|
2694
|
-
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2695
|
-
if (!matched) {
|
|
2696
|
-
return error.stack ?? '';
|
|
2767
|
+
this.options.debug((() => {
|
|
2768
|
+
if (error.name === 'CompileError') {
|
|
2769
|
+
return error;
|
|
2697
2770
|
}
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2771
|
+
return new RenderError(error.message, template, () => {
|
|
2772
|
+
const matched = error.stack.match(/<anonymous>:(\d+):(\d+)\)/);
|
|
2773
|
+
if (!matched) {
|
|
2774
|
+
return error.stack ?? '';
|
|
2775
|
+
}
|
|
2776
|
+
const [loc] = compiler.getSourceLoc({
|
|
2777
|
+
line: +matched[1] - 2,
|
|
2778
|
+
column: +matched[2],
|
|
2779
|
+
});
|
|
2780
|
+
return loc;
|
|
2701
2781
|
});
|
|
2702
|
-
|
|
2703
|
-
});
|
|
2782
|
+
})());
|
|
2704
2783
|
}
|
|
2784
|
+
return '';
|
|
2705
2785
|
}
|
|
2706
2786
|
async renderFile(filepath, globals) {
|
|
2707
2787
|
return this.render(await this.options.loader(filepath), globals);
|