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