janja 2.1.0 → 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 +1304 -1223
- package/dist/index.d.ts +70 -101
- package/dist/index.esm.js +1304 -1223
- package/dist/index.js +1304 -1223
- package/package.json +21 -2
package/dist/index.cjs.js
CHANGED
|
@@ -1,200 +1,495 @@
|
|
|
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
|
-
|
|
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", {
|
|
149
434
|
enumerable: true,
|
|
150
435
|
configurable: true,
|
|
151
436
|
writable: true,
|
|
152
|
-
value:
|
|
437
|
+
value: null
|
|
153
438
|
});
|
|
154
|
-
Object.defineProperty(this, "
|
|
439
|
+
Object.defineProperty(this, "loc", {
|
|
155
440
|
enumerable: true,
|
|
156
441
|
configurable: true,
|
|
157
442
|
writable: true,
|
|
158
|
-
value:
|
|
443
|
+
value: null
|
|
159
444
|
});
|
|
160
|
-
Object.defineProperty(this, "
|
|
445
|
+
Object.defineProperty(this, "body", {
|
|
161
446
|
enumerable: true,
|
|
162
447
|
configurable: true,
|
|
163
448
|
writable: true,
|
|
164
|
-
value:
|
|
449
|
+
value: void 0
|
|
165
450
|
});
|
|
166
|
-
|
|
451
|
+
}
|
|
452
|
+
traverse(cb) {
|
|
453
|
+
this.body?.forEach(cb);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
class RootNode extends Traversal {
|
|
457
|
+
constructor(body, loc) {
|
|
458
|
+
super();
|
|
459
|
+
Object.defineProperty(this, "body", {
|
|
167
460
|
enumerable: true,
|
|
168
461
|
configurable: true,
|
|
169
462
|
writable: true,
|
|
170
|
-
value:
|
|
463
|
+
value: body
|
|
464
|
+
});
|
|
465
|
+
Object.defineProperty(this, "loc", {
|
|
466
|
+
enumerable: true,
|
|
467
|
+
configurable: true,
|
|
468
|
+
writable: true,
|
|
469
|
+
value: loc
|
|
171
470
|
});
|
|
172
471
|
Object.defineProperty(this, "type", {
|
|
173
472
|
enumerable: true,
|
|
174
473
|
configurable: true,
|
|
175
474
|
writable: true,
|
|
176
|
-
value:
|
|
475
|
+
value: 'TEMPLATE'
|
|
177
476
|
});
|
|
178
477
|
}
|
|
179
|
-
traverse(cb) {
|
|
180
|
-
this.body?.forEach(cb);
|
|
181
|
-
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
182
|
-
}
|
|
183
478
|
}
|
|
184
|
-
class
|
|
185
|
-
constructor(
|
|
479
|
+
class UnknownNode extends Traversal {
|
|
480
|
+
constructor(name, val, loc, strip) {
|
|
186
481
|
super();
|
|
187
|
-
Object.defineProperty(this, "
|
|
482
|
+
Object.defineProperty(this, "name", {
|
|
188
483
|
enumerable: true,
|
|
189
484
|
configurable: true,
|
|
190
485
|
writable: true,
|
|
191
|
-
value:
|
|
486
|
+
value: name
|
|
192
487
|
});
|
|
193
|
-
Object.defineProperty(this, "
|
|
488
|
+
Object.defineProperty(this, "val", {
|
|
194
489
|
enumerable: true,
|
|
195
490
|
configurable: true,
|
|
196
491
|
writable: true,
|
|
197
|
-
value:
|
|
492
|
+
value: val
|
|
198
493
|
});
|
|
199
494
|
Object.defineProperty(this, "loc", {
|
|
200
495
|
enumerable: true,
|
|
@@ -212,18 +507,24 @@ class ElseIfNode extends Traversal {
|
|
|
212
507
|
enumerable: true,
|
|
213
508
|
configurable: true,
|
|
214
509
|
writable: true,
|
|
215
|
-
value:
|
|
510
|
+
value: 'UNKNOWN'
|
|
216
511
|
});
|
|
217
512
|
}
|
|
218
513
|
}
|
|
219
|
-
class
|
|
220
|
-
constructor(
|
|
514
|
+
class UnexpectedNode extends Traversal {
|
|
515
|
+
constructor(name, val, loc, strip) {
|
|
221
516
|
super();
|
|
222
|
-
Object.defineProperty(this, "
|
|
517
|
+
Object.defineProperty(this, "name", {
|
|
223
518
|
enumerable: true,
|
|
224
519
|
configurable: true,
|
|
225
520
|
writable: true,
|
|
226
|
-
value:
|
|
521
|
+
value: name
|
|
522
|
+
});
|
|
523
|
+
Object.defineProperty(this, "val", {
|
|
524
|
+
enumerable: true,
|
|
525
|
+
configurable: true,
|
|
526
|
+
writable: true,
|
|
527
|
+
value: val
|
|
227
528
|
});
|
|
228
529
|
Object.defineProperty(this, "loc", {
|
|
229
530
|
enumerable: true,
|
|
@@ -241,18 +542,19 @@ class ElseNode extends Traversal {
|
|
|
241
542
|
enumerable: true,
|
|
242
543
|
configurable: true,
|
|
243
544
|
writable: true,
|
|
244
|
-
value:
|
|
545
|
+
value: 'UNEXPECTED'
|
|
245
546
|
});
|
|
246
547
|
}
|
|
247
548
|
}
|
|
248
|
-
|
|
249
|
-
|
|
549
|
+
|
|
550
|
+
class BlockNode extends Traversal {
|
|
551
|
+
constructor(val, body, loc, strip) {
|
|
250
552
|
super();
|
|
251
|
-
Object.defineProperty(this, "
|
|
553
|
+
Object.defineProperty(this, "val", {
|
|
252
554
|
enumerable: true,
|
|
253
555
|
configurable: true,
|
|
254
556
|
writable: true,
|
|
255
|
-
value:
|
|
557
|
+
value: val
|
|
256
558
|
});
|
|
257
559
|
Object.defineProperty(this, "body", {
|
|
258
560
|
enumerable: true,
|
|
@@ -276,11 +578,11 @@ class ForNode extends Traversal {
|
|
|
276
578
|
enumerable: true,
|
|
277
579
|
configurable: true,
|
|
278
580
|
writable: true,
|
|
279
|
-
value:
|
|
581
|
+
value: 'BLOCK'
|
|
280
582
|
});
|
|
281
583
|
}
|
|
282
584
|
}
|
|
283
|
-
class
|
|
585
|
+
class SuperNode extends Traversal {
|
|
284
586
|
constructor(val, loc, strip) {
|
|
285
587
|
super();
|
|
286
588
|
Object.defineProperty(this, "val", {
|
|
@@ -305,53 +607,206 @@ class BreakNode extends Traversal {
|
|
|
305
607
|
enumerable: true,
|
|
306
608
|
configurable: true,
|
|
307
609
|
writable: true,
|
|
308
|
-
value:
|
|
610
|
+
value: 'SUPER'
|
|
309
611
|
});
|
|
310
612
|
}
|
|
311
613
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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", {
|
|
328
682
|
enumerable: true,
|
|
329
683
|
configurable: true,
|
|
330
684
|
writable: true,
|
|
331
|
-
value:
|
|
685
|
+
value: void 0
|
|
332
686
|
});
|
|
333
|
-
Object.defineProperty(this, "
|
|
687
|
+
Object.defineProperty(this, "filters", {
|
|
334
688
|
enumerable: true,
|
|
335
689
|
configurable: true,
|
|
336
690
|
writable: true,
|
|
337
|
-
value:
|
|
691
|
+
value: void 0
|
|
338
692
|
});
|
|
339
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
|
+
}
|
|
340
785
|
}
|
|
341
|
-
|
|
342
|
-
|
|
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) {
|
|
343
798
|
super();
|
|
344
|
-
Object.defineProperty(this, "
|
|
799
|
+
Object.defineProperty(this, "val", {
|
|
345
800
|
enumerable: true,
|
|
346
801
|
configurable: true,
|
|
347
802
|
writable: true,
|
|
348
|
-
value:
|
|
803
|
+
value: val
|
|
349
804
|
});
|
|
350
|
-
Object.defineProperty(this, "
|
|
805
|
+
Object.defineProperty(this, "body", {
|
|
351
806
|
enumerable: true,
|
|
352
807
|
configurable: true,
|
|
353
808
|
writable: true,
|
|
354
|
-
value:
|
|
809
|
+
value: body
|
|
355
810
|
});
|
|
356
811
|
Object.defineProperty(this, "loc", {
|
|
357
812
|
enumerable: 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,40 +894,46 @@ 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
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
configurable: true,
|
|
417
|
-
writable: true,
|
|
418
|
-
value: val
|
|
419
|
-
});
|
|
420
|
-
Object.defineProperty(this, "loc", {
|
|
421
|
-
enumerable: true,
|
|
422
|
-
configurable: true,
|
|
423
|
-
writable: true,
|
|
424
|
-
value: loc
|
|
425
|
-
});
|
|
426
|
-
Object.defineProperty(this, "strip", {
|
|
427
|
-
enumerable: true,
|
|
428
|
-
configurable: true,
|
|
429
|
-
writable: true,
|
|
430
|
-
value: strip
|
|
431
|
-
});
|
|
432
|
-
Object.defineProperty(this, "type", {
|
|
433
|
-
enumerable: true,
|
|
434
|
-
configurable: true,
|
|
435
|
-
writable: true,
|
|
436
|
-
value: NodeType.COMMENT
|
|
437
|
-
});
|
|
901
|
+
|
|
902
|
+
function parseCapture(token, parser) {
|
|
903
|
+
if (!token.expression) {
|
|
904
|
+
parser.emitExpErr(token);
|
|
905
|
+
return;
|
|
438
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);
|
|
439
916
|
}
|
|
440
|
-
|
|
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
|
+
|
|
936
|
+
class CommentNode extends Traversal {
|
|
441
937
|
constructor(val, loc, strip) {
|
|
442
938
|
super();
|
|
443
939
|
Object.defineProperty(this, "val", {
|
|
@@ -462,18 +958,56 @@ class IncludeNode extends Traversal {
|
|
|
462
958
|
enumerable: true,
|
|
463
959
|
configurable: true,
|
|
464
960
|
writable: true,
|
|
465
|
-
value:
|
|
961
|
+
value: 'COMMENT'
|
|
466
962
|
});
|
|
467
963
|
}
|
|
468
964
|
}
|
|
469
|
-
|
|
470
|
-
|
|
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) {
|
|
471
1005
|
super();
|
|
472
|
-
Object.defineProperty(this, "
|
|
1006
|
+
Object.defineProperty(this, "loop", {
|
|
473
1007
|
enumerable: true,
|
|
474
1008
|
configurable: true,
|
|
475
1009
|
writable: true,
|
|
476
|
-
value:
|
|
1010
|
+
value: loop
|
|
477
1011
|
});
|
|
478
1012
|
Object.defineProperty(this, "body", {
|
|
479
1013
|
enumerable: true,
|
|
@@ -497,11 +1031,11 @@ class BlockNode extends Traversal {
|
|
|
497
1031
|
enumerable: true,
|
|
498
1032
|
configurable: true,
|
|
499
1033
|
writable: true,
|
|
500
|
-
value:
|
|
1034
|
+
value: 'FOR'
|
|
501
1035
|
});
|
|
502
1036
|
}
|
|
503
1037
|
}
|
|
504
|
-
class
|
|
1038
|
+
class BreakNode extends Traversal {
|
|
505
1039
|
constructor(val, loc, strip) {
|
|
506
1040
|
super();
|
|
507
1041
|
Object.defineProperty(this, "val", {
|
|
@@ -526,12 +1060,12 @@ class SuperNode extends Traversal {
|
|
|
526
1060
|
enumerable: true,
|
|
527
1061
|
configurable: true,
|
|
528
1062
|
writable: true,
|
|
529
|
-
value:
|
|
1063
|
+
value: 'BREAK'
|
|
530
1064
|
});
|
|
531
1065
|
}
|
|
532
1066
|
}
|
|
533
|
-
class
|
|
534
|
-
constructor(val,
|
|
1067
|
+
class ContinueNode extends Traversal {
|
|
1068
|
+
constructor(val, loc, strip) {
|
|
535
1069
|
super();
|
|
536
1070
|
Object.defineProperty(this, "val", {
|
|
537
1071
|
enumerable: true,
|
|
@@ -539,12 +1073,6 @@ class MacroNode extends Traversal {
|
|
|
539
1073
|
writable: true,
|
|
540
1074
|
value: val
|
|
541
1075
|
});
|
|
542
|
-
Object.defineProperty(this, "body", {
|
|
543
|
-
enumerable: true,
|
|
544
|
-
configurable: true,
|
|
545
|
-
writable: true,
|
|
546
|
-
value: body
|
|
547
|
-
});
|
|
548
1076
|
Object.defineProperty(this, "loc", {
|
|
549
1077
|
enumerable: true,
|
|
550
1078
|
configurable: true,
|
|
@@ -561,18 +1089,104 @@ class MacroNode extends Traversal {
|
|
|
561
1089
|
enumerable: true,
|
|
562
1090
|
configurable: true,
|
|
563
1091
|
writable: true,
|
|
564
|
-
value:
|
|
1092
|
+
value: 'CONTINUE'
|
|
565
1093
|
});
|
|
566
1094
|
}
|
|
567
1095
|
}
|
|
568
|
-
|
|
569
|
-
|
|
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) {
|
|
570
1172
|
super();
|
|
571
|
-
Object.defineProperty(this, "
|
|
1173
|
+
Object.defineProperty(this, "test", {
|
|
572
1174
|
enumerable: true,
|
|
573
1175
|
configurable: true,
|
|
574
1176
|
writable: true,
|
|
575
|
-
value:
|
|
1177
|
+
value: test
|
|
1178
|
+
});
|
|
1179
|
+
Object.defineProperty(this, "body", {
|
|
1180
|
+
enumerable: true,
|
|
1181
|
+
configurable: true,
|
|
1182
|
+
writable: true,
|
|
1183
|
+
value: body
|
|
1184
|
+
});
|
|
1185
|
+
Object.defineProperty(this, "alternatives", {
|
|
1186
|
+
enumerable: true,
|
|
1187
|
+
configurable: true,
|
|
1188
|
+
writable: true,
|
|
1189
|
+
value: alternatives
|
|
576
1190
|
});
|
|
577
1191
|
Object.defineProperty(this, "loc", {
|
|
578
1192
|
enumerable: true,
|
|
@@ -590,18 +1204,22 @@ class CallerNode extends Traversal {
|
|
|
590
1204
|
enumerable: true,
|
|
591
1205
|
configurable: true,
|
|
592
1206
|
writable: true,
|
|
593
|
-
value:
|
|
1207
|
+
value: 'IF'
|
|
594
1208
|
});
|
|
595
1209
|
}
|
|
1210
|
+
traverse(cb) {
|
|
1211
|
+
this.body?.forEach(cb);
|
|
1212
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
1213
|
+
}
|
|
596
1214
|
}
|
|
597
|
-
class
|
|
598
|
-
constructor(
|
|
1215
|
+
class ElseIfNode extends Traversal {
|
|
1216
|
+
constructor(test, body, loc, strip) {
|
|
599
1217
|
super();
|
|
600
|
-
Object.defineProperty(this, "
|
|
1218
|
+
Object.defineProperty(this, "test", {
|
|
601
1219
|
enumerable: true,
|
|
602
1220
|
configurable: true,
|
|
603
1221
|
writable: true,
|
|
604
|
-
value:
|
|
1222
|
+
value: test
|
|
605
1223
|
});
|
|
606
1224
|
Object.defineProperty(this, "body", {
|
|
607
1225
|
enumerable: true,
|
|
@@ -625,18 +1243,18 @@ class CallNode extends Traversal {
|
|
|
625
1243
|
enumerable: true,
|
|
626
1244
|
configurable: true,
|
|
627
1245
|
writable: true,
|
|
628
|
-
value:
|
|
1246
|
+
value: 'ELSE_IF'
|
|
629
1247
|
});
|
|
630
1248
|
}
|
|
631
1249
|
}
|
|
632
|
-
class
|
|
633
|
-
constructor(
|
|
1250
|
+
class ElseNode extends Traversal {
|
|
1251
|
+
constructor(body, loc, strip) {
|
|
634
1252
|
super();
|
|
635
|
-
Object.defineProperty(this, "
|
|
1253
|
+
Object.defineProperty(this, "body", {
|
|
636
1254
|
enumerable: true,
|
|
637
1255
|
configurable: true,
|
|
638
1256
|
writable: true,
|
|
639
|
-
value:
|
|
1257
|
+
value: body
|
|
640
1258
|
});
|
|
641
1259
|
Object.defineProperty(this, "loc", {
|
|
642
1260
|
enumerable: true,
|
|
@@ -654,12 +1272,80 @@ class SetNode extends Traversal {
|
|
|
654
1272
|
enumerable: true,
|
|
655
1273
|
configurable: true,
|
|
656
1274
|
writable: true,
|
|
657
|
-
value:
|
|
1275
|
+
value: 'ELSE'
|
|
658
1276
|
});
|
|
659
1277
|
}
|
|
660
1278
|
}
|
|
661
|
-
|
|
662
|
-
|
|
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 {
|
|
1348
|
+
constructor(val, loc, strip) {
|
|
663
1349
|
super();
|
|
664
1350
|
Object.defineProperty(this, "val", {
|
|
665
1351
|
enumerable: true,
|
|
@@ -667,12 +1353,6 @@ class CaptureNode extends Traversal {
|
|
|
667
1353
|
writable: true,
|
|
668
1354
|
value: val
|
|
669
1355
|
});
|
|
670
|
-
Object.defineProperty(this, "body", {
|
|
671
|
-
enumerable: true,
|
|
672
|
-
configurable: true,
|
|
673
|
-
writable: true,
|
|
674
|
-
value: body
|
|
675
|
-
});
|
|
676
1356
|
Object.defineProperty(this, "loc", {
|
|
677
1357
|
enumerable: true,
|
|
678
1358
|
configurable: true,
|
|
@@ -683,698 +1363,32 @@ class CaptureNode extends Traversal {
|
|
|
683
1363
|
enumerable: true,
|
|
684
1364
|
configurable: true,
|
|
685
1365
|
writable: true,
|
|
686
|
-
value: strip
|
|
687
|
-
});
|
|
688
|
-
Object.defineProperty(this, "type", {
|
|
689
|
-
enumerable: true,
|
|
690
|
-
configurable: true,
|
|
691
|
-
writable: true,
|
|
692
|
-
value:
|
|
693
|
-
});
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
698
|
-
const output = [];
|
|
699
|
-
const caretLines = new Set();
|
|
700
|
-
const addCaretLine = (index) => {
|
|
701
|
-
caretLines.add(index - 2);
|
|
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));
|
|
801
|
-
}
|
|
802
|
-
return new CallNode(val, body, token.loc, token.strip);
|
|
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);
|
|
812
|
-
parser.advance();
|
|
813
|
-
const body = parser.parseUntil(['endcapture']);
|
|
814
|
-
if (parser.match(['endcapture'])) {
|
|
815
|
-
parser.advance();
|
|
816
|
-
}
|
|
817
|
-
else {
|
|
818
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
819
|
-
}
|
|
820
|
-
return new CaptureNode(val, body, token.loc, token.strip);
|
|
821
|
-
}
|
|
822
|
-
const parsers$6 = {
|
|
823
|
-
capture: parseCapture,
|
|
824
|
-
endcapture: parseUnexpected,
|
|
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();
|
|
834
|
-
}
|
|
835
|
-
else {
|
|
836
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
837
|
-
}
|
|
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
|
-
parser.advance();
|
|
848
|
-
return new ContinueNode(token.val, token.loc, token.strip);
|
|
849
|
-
}
|
|
850
|
-
const parsers$5 = {
|
|
851
|
-
for: parseFor,
|
|
852
|
-
break: parseBreak,
|
|
853
|
-
continue: parseContinue,
|
|
854
|
-
endfor: parseUnexpected,
|
|
855
|
-
};
|
|
856
|
-
|
|
857
|
-
function parseIf(token, parser) {
|
|
858
|
-
parser.requireExpression(token);
|
|
859
|
-
parser.advance();
|
|
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,
|
|
894
|
-
};
|
|
895
|
-
|
|
896
|
-
function parseInclude(token, parser) {
|
|
897
|
-
parser.requireExpression(token);
|
|
898
|
-
const val = parser.parseExp(token.expression);
|
|
899
|
-
parser.advance();
|
|
900
|
-
return new IncludeNode(val, token.loc, token.strip);
|
|
901
|
-
}
|
|
902
|
-
const parsers$3 = {
|
|
903
|
-
include: parseInclude,
|
|
904
|
-
};
|
|
905
|
-
|
|
906
|
-
function parseMacro(token, parser) {
|
|
907
|
-
parser.requireExpression(token);
|
|
908
|
-
const name = parser.parseExp(token.expression);
|
|
909
|
-
parser.advance();
|
|
910
|
-
const body = parser.parseUntil(['endmacro']);
|
|
911
|
-
if (parser.match(['endmacro'])) {
|
|
912
|
-
parser.advance();
|
|
913
|
-
}
|
|
914
|
-
else {
|
|
915
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
916
|
-
}
|
|
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
|
-
}
|
|
924
|
-
const parsers$2 = {
|
|
925
|
-
macro: parseMacro,
|
|
926
|
-
caller: parseCaller,
|
|
927
|
-
endmacro: parseUnexpected,
|
|
928
|
-
};
|
|
929
|
-
|
|
930
|
-
function parseSet(token, parser) {
|
|
931
|
-
parser.requireExpression(token);
|
|
932
|
-
const val = parser.parseExp(token.expression);
|
|
933
|
-
parser.advance();
|
|
934
|
-
return new SetNode(val, token.loc, token.strip);
|
|
935
|
-
}
|
|
936
|
-
const parsers$1 = {
|
|
937
|
-
set: parseSet,
|
|
938
|
-
};
|
|
939
|
-
|
|
940
|
-
const parsers = {
|
|
941
|
-
...parsers$4,
|
|
942
|
-
...parsers$5,
|
|
943
|
-
...parsers$3,
|
|
944
|
-
...parsers$8,
|
|
945
|
-
...parsers$2,
|
|
946
|
-
...parsers$7,
|
|
947
|
-
...parsers$1,
|
|
948
|
-
...parsers$6,
|
|
949
|
-
};
|
|
950
|
-
|
|
951
|
-
class Safe extends String {
|
|
952
|
-
constructor(value) {
|
|
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, '>');
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
function abs(value = 0) {
|
|
967
|
-
return Math.abs(value);
|
|
968
|
-
}
|
|
969
|
-
function add(value = 0, addend = 0) {
|
|
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, ...rest) {
|
|
1030
|
-
return Math.max(value, ...rest);
|
|
1031
|
-
}
|
|
1032
|
-
function min(value = 0, ...rest) {
|
|
1033
|
-
return Math.min(value, ...rest);
|
|
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 = {}, ...omitKeys) {
|
|
1042
|
-
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
1043
|
-
}
|
|
1044
|
-
function pick(value = {}, ...pickKeys) {
|
|
1045
|
-
return pickKeys.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, replacement) {
|
|
1051
|
-
return value.replace(new RegExp(search, 'g'), replacement);
|
|
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 = '', maxLength = 0, truncateStr = '...') {
|
|
1078
|
-
return (value.slice(0, maxLength) + (value.length > maxLength ? 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", {
|
|
1197
|
-
enumerable: true,
|
|
1198
|
-
configurable: true,
|
|
1199
|
-
writable: true,
|
|
1200
|
-
value: void 0
|
|
1201
|
-
});
|
|
1202
|
-
Object.defineProperty(this, "filters", {
|
|
1203
|
-
enumerable: true,
|
|
1204
|
-
configurable: true,
|
|
1205
|
-
writable: true,
|
|
1206
|
-
value: void 0
|
|
1207
|
-
});
|
|
1208
|
-
}
|
|
1209
|
-
compile(expression, context = CONTEXT, filters = FILTERS) {
|
|
1210
|
-
this.context = context;
|
|
1211
|
-
this.filters = filters;
|
|
1212
|
-
return expression ? this.compileExp(expression) : '""';
|
|
1213
|
-
}
|
|
1214
|
-
compileExp(expression) {
|
|
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
|
-
default:
|
|
1251
|
-
return '';
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
compileNot({ argument }) {
|
|
1255
|
-
return `!${this.compileExp(argument)}`;
|
|
1256
|
-
}
|
|
1257
|
-
compileId({ value, path, args }) {
|
|
1258
|
-
let s = `${this.context}.${value}`;
|
|
1259
|
-
if (path) {
|
|
1260
|
-
s += path.map((p) => `.${p.value}`).join('');
|
|
1261
|
-
}
|
|
1262
|
-
if (args) {
|
|
1263
|
-
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
1264
|
-
}
|
|
1265
|
-
return s;
|
|
1266
|
-
}
|
|
1267
|
-
compileLit({ value }) {
|
|
1268
|
-
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1269
|
-
}
|
|
1270
|
-
compileSeq({ elements }) {
|
|
1271
|
-
return elements.map((element) => this.compileExp(element)).toString();
|
|
1272
|
-
}
|
|
1273
|
-
compilePipe({ left, right }) {
|
|
1274
|
-
const { value: name, args = [] } = right;
|
|
1275
|
-
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(',')}))`;
|
|
1276
|
-
}
|
|
1277
|
-
compileOf({ left, right }) {
|
|
1278
|
-
// destructuring
|
|
1279
|
-
if (left.type === 'SEQ') {
|
|
1280
|
-
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
1281
|
-
}
|
|
1282
|
-
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
1283
|
-
}
|
|
1284
|
-
compileSet({ left, right }) {
|
|
1285
|
-
if (left.type === 'ID') {
|
|
1286
|
-
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
1287
|
-
}
|
|
1288
|
-
// destructuring
|
|
1289
|
-
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
1290
|
-
}
|
|
1291
|
-
compileIs({ left, right }) {
|
|
1292
|
-
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
1293
|
-
}
|
|
1294
|
-
compileBinary({ type, left, right }) {
|
|
1295
|
-
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
1296
|
-
return type === 'NI' ? `!${ret}` : ret;
|
|
1297
|
-
}
|
|
1298
|
-
compileTernary({ test, consequent, alternative }) {
|
|
1299
|
-
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
1300
|
-
}
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
async function compileCall({ val, body, loc }, compiler) {
|
|
1304
|
-
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1305
|
-
await compiler.compileNodes(body);
|
|
1306
|
-
compiler.pushRaw(null, '});');
|
|
1307
|
-
}
|
|
1308
|
-
const compilers$a = {
|
|
1309
|
-
[NodeType.CALL]: compileCall,
|
|
1310
|
-
};
|
|
1311
|
-
|
|
1312
|
-
async function compileCapture({ val, body, loc }, compiler) {
|
|
1313
|
-
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1314
|
-
await compiler.compileNodes(body);
|
|
1315
|
-
compiler.pushRaw(null, 'return s;})("");');
|
|
1316
|
-
}
|
|
1317
|
-
const compilers$9 = {
|
|
1318
|
-
[NodeType.CAPTURE]: compileCapture,
|
|
1319
|
-
};
|
|
1320
|
-
|
|
1321
|
-
async function compileComment({ val, loc }, compiler) {
|
|
1322
|
-
if (!compiler.options.stripComments) {
|
|
1323
|
-
compiler.pushStr(loc, `<!--${val}-->`);
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
const compilers$8 = {
|
|
1327
|
-
[NodeType.COMMENT]: compileComment,
|
|
1328
|
-
};
|
|
1329
|
-
|
|
1330
|
-
async function compileFor({ loop, body }, compiler) {
|
|
1331
|
-
const { context } = compiler;
|
|
1332
|
-
if (loop.left.type === 'SEQ') {
|
|
1333
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
1334
|
-
}
|
|
1335
|
-
else {
|
|
1336
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
|
|
1337
|
-
}
|
|
1338
|
-
await compiler.compileNodes(body);
|
|
1339
|
-
compiler.out();
|
|
1340
|
-
compiler.pushRaw(null, '}');
|
|
1341
|
-
}
|
|
1342
|
-
function compileBreak({ loc }, compiler) {
|
|
1343
|
-
compiler.pushRaw(loc, 'break;');
|
|
1344
|
-
}
|
|
1345
|
-
function compileContinue({ loc }, compiler) {
|
|
1346
|
-
compiler.pushRaw(loc, 'continue;');
|
|
1366
|
+
value: strip
|
|
1367
|
+
});
|
|
1368
|
+
Object.defineProperty(this, "type", {
|
|
1369
|
+
enumerable: true,
|
|
1370
|
+
configurable: true,
|
|
1371
|
+
writable: true,
|
|
1372
|
+
value: 'INCLUDE'
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1347
1375
|
}
|
|
1348
|
-
const compilers$7 = {
|
|
1349
|
-
[NodeType.FOR]: compileFor,
|
|
1350
|
-
[NodeType.BREAK]: compileBreak,
|
|
1351
|
-
[NodeType.CONTINUE]: compileContinue,
|
|
1352
|
-
};
|
|
1353
1376
|
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
for (const branch of alternatives) {
|
|
1359
|
-
if (branch.type === NodeType.ELSE_IF) {
|
|
1360
|
-
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1361
|
-
}
|
|
1362
|
-
else {
|
|
1363
|
-
compiler.pushRaw(null, 'else{');
|
|
1364
|
-
}
|
|
1365
|
-
await compiler.compileNodes(branch.body);
|
|
1366
|
-
compiler.pushRaw(null, '}');
|
|
1377
|
+
function parseInclude(token, parser) {
|
|
1378
|
+
if (!token.expression) {
|
|
1379
|
+
parser.emitExpErr(token);
|
|
1380
|
+
return;
|
|
1367
1381
|
}
|
|
1382
|
+
parser.advance();
|
|
1383
|
+
return new IncludeNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
1368
1384
|
}
|
|
1369
|
-
const
|
|
1370
|
-
|
|
1385
|
+
const parsers$4 = {
|
|
1386
|
+
include: parseInclude,
|
|
1371
1387
|
};
|
|
1372
1388
|
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
const compilers$5 = {
|
|
1377
|
-
[NodeType.INCLUDE]: compileInclude,
|
|
1389
|
+
const plugin$4 = {
|
|
1390
|
+
compilers: compilers$4,
|
|
1391
|
+
parsers: parsers$4,
|
|
1378
1392
|
};
|
|
1379
1393
|
|
|
1380
1394
|
async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
@@ -1382,11 +1396,11 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
|
1382
1396
|
const { elements } = right;
|
|
1383
1397
|
const expCompiler = new ExpCompiler();
|
|
1384
1398
|
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1385
|
-
.map((el) => el.type === '
|
|
1399
|
+
.map((el) => el.type === 'ASSIGN'
|
|
1386
1400
|
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1387
1401
|
: el.value)
|
|
1388
1402
|
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1389
|
-
.map((el) => el.type === '
|
|
1403
|
+
.map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
|
|
1390
1404
|
.join(','), '};');
|
|
1391
1405
|
await compiler.compileNodes(body);
|
|
1392
1406
|
compiler.out();
|
|
@@ -1395,254 +1409,358 @@ async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
|
1395
1409
|
function compileCaller({ loc }, compiler) {
|
|
1396
1410
|
compiler.pushRaw(loc, 'await _c?.();');
|
|
1397
1411
|
}
|
|
1398
|
-
const compilers$
|
|
1399
|
-
|
|
1400
|
-
|
|
1412
|
+
const compilers$3 = {
|
|
1413
|
+
MACRO: compileMacro,
|
|
1414
|
+
CALLER: compileCaller,
|
|
1401
1415
|
};
|
|
1402
1416
|
|
|
1403
|
-
|
|
1404
|
-
|
|
1417
|
+
class MacroNode extends Traversal {
|
|
1418
|
+
constructor(val, body, loc, strip) {
|
|
1419
|
+
super();
|
|
1420
|
+
Object.defineProperty(this, "val", {
|
|
1421
|
+
enumerable: true,
|
|
1422
|
+
configurable: true,
|
|
1423
|
+
writable: true,
|
|
1424
|
+
value: val
|
|
1425
|
+
});
|
|
1426
|
+
Object.defineProperty(this, "body", {
|
|
1427
|
+
enumerable: true,
|
|
1428
|
+
configurable: true,
|
|
1429
|
+
writable: true,
|
|
1430
|
+
value: body
|
|
1431
|
+
});
|
|
1432
|
+
Object.defineProperty(this, "loc", {
|
|
1433
|
+
enumerable: true,
|
|
1434
|
+
configurable: true,
|
|
1435
|
+
writable: true,
|
|
1436
|
+
value: loc
|
|
1437
|
+
});
|
|
1438
|
+
Object.defineProperty(this, "strip", {
|
|
1439
|
+
enumerable: true,
|
|
1440
|
+
configurable: true,
|
|
1441
|
+
writable: true,
|
|
1442
|
+
value: strip
|
|
1443
|
+
});
|
|
1444
|
+
Object.defineProperty(this, "type", {
|
|
1445
|
+
enumerable: true,
|
|
1446
|
+
configurable: true,
|
|
1447
|
+
writable: true,
|
|
1448
|
+
value: 'MACRO'
|
|
1449
|
+
});
|
|
1450
|
+
}
|
|
1405
1451
|
}
|
|
1406
|
-
|
|
1407
|
-
|
|
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'
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
const wm = new WeakMap();
|
|
1483
|
+
function parseMacro(token, parser) {
|
|
1484
|
+
if (!token.expression) {
|
|
1485
|
+
parser.emitExpErr(token);
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
wm.set(parser, true);
|
|
1489
|
+
parser.advance();
|
|
1490
|
+
const body = parser.parseUntil(['endmacro']);
|
|
1491
|
+
wm.set(parser, false);
|
|
1492
|
+
if (parser.match(['endmacro'])) {
|
|
1493
|
+
parser.advance();
|
|
1494
|
+
}
|
|
1495
|
+
else {
|
|
1496
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1497
|
+
}
|
|
1498
|
+
return new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
1499
|
+
}
|
|
1500
|
+
function parseCaller(token, parser) {
|
|
1501
|
+
if (token.expression) {
|
|
1502
|
+
parser.emitExpErr(token, false);
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
if (!wm.get(parser)) {
|
|
1506
|
+
parser.options.debug?.(new CompileError('"caller" directive used outside of a macro', parser.template, token.loc));
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
parser.advance();
|
|
1510
|
+
return new CallerNode(token.val, token.loc, token.strip);
|
|
1511
|
+
}
|
|
1512
|
+
const parsers$3 = {
|
|
1513
|
+
macro: parseMacro,
|
|
1514
|
+
caller: parseCaller,
|
|
1515
|
+
endmacro: createUnexpected,
|
|
1408
1516
|
};
|
|
1409
1517
|
|
|
1410
|
-
|
|
1411
|
-
|
|
1518
|
+
const plugin$3 = {
|
|
1519
|
+
compilers: compilers$3,
|
|
1520
|
+
parsers: parsers$3,
|
|
1521
|
+
};
|
|
1522
|
+
|
|
1523
|
+
async function compileOutput({ exp }, compiler) {
|
|
1524
|
+
compiler.pushVar(exp.loc, new ExpCompiler().compile(exp, compiler.context));
|
|
1412
1525
|
}
|
|
1413
1526
|
const compilers$2 = {
|
|
1414
|
-
|
|
1527
|
+
OUTPUT: compileOutput,
|
|
1415
1528
|
};
|
|
1416
1529
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
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
|
+
});
|
|
1423
1563
|
}
|
|
1424
|
-
compiler.pushStr(loc, val);
|
|
1425
1564
|
}
|
|
1426
|
-
const compilers$1 = {
|
|
1427
|
-
[NodeType.TEXT]: compileText,
|
|
1428
|
-
};
|
|
1429
1565
|
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
...compilers$3,
|
|
1437
|
-
...compilers$8,
|
|
1438
|
-
...compilers$4,
|
|
1439
|
-
...compilers$a,
|
|
1440
|
-
...compilers$2,
|
|
1441
|
-
...compilers$9,
|
|
1566
|
+
function parseOutput(token, parser) {
|
|
1567
|
+
parser.advance();
|
|
1568
|
+
return new OutputNode(token.val, token.loc, token.strip, parser.parseExp(token));
|
|
1569
|
+
}
|
|
1570
|
+
const parsers$2 = {
|
|
1571
|
+
output: parseOutput,
|
|
1442
1572
|
};
|
|
1443
1573
|
|
|
1444
|
-
const
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
directiveOpen: '{{',
|
|
1448
|
-
directiveClose: '}}',
|
|
1449
|
-
outputOpen: '{{=',
|
|
1450
|
-
outputClose: '}}',
|
|
1451
|
-
parsers,
|
|
1452
|
-
debug: () => { },
|
|
1453
|
-
};
|
|
1454
|
-
const compilerOptions = {
|
|
1455
|
-
...parserOptions,
|
|
1456
|
-
trimWhitespace: false,
|
|
1457
|
-
stripComments: false,
|
|
1458
|
-
compilers,
|
|
1459
|
-
loader: async () => '',
|
|
1574
|
+
const plugin$2 = {
|
|
1575
|
+
compilers: compilers$2,
|
|
1576
|
+
parsers: parsers$2,
|
|
1460
1577
|
};
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1578
|
+
|
|
1579
|
+
async function compileSet({ val, loc }, compiler) {
|
|
1580
|
+
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1581
|
+
}
|
|
1582
|
+
const compilers$1 = {
|
|
1583
|
+
SET: compileSet,
|
|
1467
1584
|
};
|
|
1468
1585
|
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
* start: {
|
|
1492
|
-
* line: 2,
|
|
1493
|
-
* column: 3,
|
|
1494
|
-
* },
|
|
1495
|
-
* end: {
|
|
1496
|
-
* line: 3,
|
|
1497
|
-
* column: 8,
|
|
1498
|
-
* },
|
|
1499
|
-
* }
|
|
1500
|
-
* }
|
|
1501
|
-
*/
|
|
1502
|
-
class SourceMap {
|
|
1503
|
-
constructor() {
|
|
1504
|
-
Object.defineProperty(this, "mappings", {
|
|
1586
|
+
class SetNode extends Traversal {
|
|
1587
|
+
constructor(val, loc, strip) {
|
|
1588
|
+
super();
|
|
1589
|
+
Object.defineProperty(this, "val", {
|
|
1590
|
+
enumerable: true,
|
|
1591
|
+
configurable: true,
|
|
1592
|
+
writable: true,
|
|
1593
|
+
value: val
|
|
1594
|
+
});
|
|
1595
|
+
Object.defineProperty(this, "loc", {
|
|
1596
|
+
enumerable: true,
|
|
1597
|
+
configurable: true,
|
|
1598
|
+
writable: true,
|
|
1599
|
+
value: loc
|
|
1600
|
+
});
|
|
1601
|
+
Object.defineProperty(this, "strip", {
|
|
1602
|
+
enumerable: true,
|
|
1603
|
+
configurable: true,
|
|
1604
|
+
writable: true,
|
|
1605
|
+
value: strip
|
|
1606
|
+
});
|
|
1607
|
+
Object.defineProperty(this, "type", {
|
|
1505
1608
|
enumerable: true,
|
|
1506
1609
|
configurable: true,
|
|
1507
1610
|
writable: true,
|
|
1508
|
-
value:
|
|
1611
|
+
value: 'SET'
|
|
1509
1612
|
});
|
|
1510
1613
|
}
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
function parseSet(token, parser) {
|
|
1617
|
+
if (!token.expression) {
|
|
1618
|
+
parser.emitExpErr(token);
|
|
1619
|
+
return;
|
|
1516
1620
|
}
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
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+/, '');
|
|
1636
|
+
}
|
|
1637
|
+
if (end || compiler.options.trimWhitespace) {
|
|
1638
|
+
val = val.replace(/\s+$/, '');
|
|
1524
1639
|
}
|
|
1640
|
+
compiler.pushStr(loc, val);
|
|
1525
1641
|
}
|
|
1642
|
+
const compilers = {
|
|
1643
|
+
TEXT: compileText,
|
|
1644
|
+
};
|
|
1526
1645
|
|
|
1527
|
-
class
|
|
1528
|
-
constructor() {
|
|
1529
|
-
super(
|
|
1530
|
-
Object.defineProperty(this, "
|
|
1646
|
+
class TextNode extends Traversal {
|
|
1647
|
+
constructor(val, loc, strip) {
|
|
1648
|
+
super();
|
|
1649
|
+
Object.defineProperty(this, "val", {
|
|
1531
1650
|
enumerable: true,
|
|
1532
1651
|
configurable: true,
|
|
1533
1652
|
writable: true,
|
|
1534
|
-
value:
|
|
1535
|
-
});
|
|
1536
|
-
}
|
|
1537
|
-
get script() {
|
|
1538
|
-
// eslint-disable-next-line no-new-func
|
|
1539
|
-
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
1540
|
-
}
|
|
1541
|
-
start() {
|
|
1542
|
-
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
1543
|
-
}
|
|
1544
|
-
end() {
|
|
1545
|
-
this.pushRaw(null, 'return s;', '})();');
|
|
1546
|
-
}
|
|
1547
|
-
pushRaw(loc, ...lines) {
|
|
1548
|
-
const start = this.code.length;
|
|
1549
|
-
for (const line of lines) {
|
|
1550
|
-
this.code += line;
|
|
1551
|
-
}
|
|
1552
|
-
if (!loc) {
|
|
1553
|
-
return;
|
|
1554
|
-
}
|
|
1555
|
-
this.addMapping(loc, {
|
|
1556
|
-
start: {
|
|
1557
|
-
line: 1,
|
|
1558
|
-
column: start,
|
|
1559
|
-
},
|
|
1560
|
-
end: {
|
|
1561
|
-
line: 1,
|
|
1562
|
-
column: this.code.length,
|
|
1563
|
-
},
|
|
1653
|
+
value: val
|
|
1564
1654
|
});
|
|
1565
|
-
|
|
1566
|
-
pushStr(loc, s) {
|
|
1567
|
-
if (s) {
|
|
1568
|
-
this.pushRaw(loc, `s+="${s
|
|
1569
|
-
.replace(/\\/g, '\\\\')
|
|
1570
|
-
.replace(/"/g, '\\"')
|
|
1571
|
-
.replace(/[\n\r]/g, '\\n')}";`);
|
|
1572
|
-
}
|
|
1573
|
-
}
|
|
1574
|
-
pushVar(loc, v) {
|
|
1575
|
-
this.pushRaw(loc, `s+=${ESCAPE}(${v});`);
|
|
1576
|
-
}
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1579
|
-
class Context extends OutScript {
|
|
1580
|
-
constructor() {
|
|
1581
|
-
super(...arguments);
|
|
1582
|
-
Object.defineProperty(this, "context", {
|
|
1655
|
+
Object.defineProperty(this, "loc", {
|
|
1583
1656
|
enumerable: true,
|
|
1584
1657
|
configurable: true,
|
|
1585
1658
|
writable: true,
|
|
1586
|
-
value:
|
|
1659
|
+
value: loc
|
|
1587
1660
|
});
|
|
1588
|
-
Object.defineProperty(this, "
|
|
1661
|
+
Object.defineProperty(this, "strip", {
|
|
1589
1662
|
enumerable: true,
|
|
1590
1663
|
configurable: true,
|
|
1591
1664
|
writable: true,
|
|
1592
|
-
value:
|
|
1665
|
+
value: strip
|
|
1593
1666
|
});
|
|
1594
|
-
Object.defineProperty(this, "
|
|
1667
|
+
Object.defineProperty(this, "type", {
|
|
1595
1668
|
enumerable: true,
|
|
1596
1669
|
configurable: true,
|
|
1597
1670
|
writable: true,
|
|
1598
|
-
value:
|
|
1671
|
+
value: 'TEXT'
|
|
1599
1672
|
});
|
|
1600
1673
|
}
|
|
1601
|
-
in() {
|
|
1602
|
-
this.context = `${this.context}_${this.index++}`;
|
|
1603
|
-
this.contexts.push(this.context);
|
|
1604
|
-
return this.context;
|
|
1605
|
-
}
|
|
1606
|
-
out() {
|
|
1607
|
-
if (this.context === CONTEXT) {
|
|
1608
|
-
return;
|
|
1609
|
-
}
|
|
1610
|
-
this.contexts.pop();
|
|
1611
|
-
this.context = this.contexts.at(-1);
|
|
1612
|
-
}
|
|
1613
1674
|
}
|
|
1614
1675
|
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
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,
|
|
1682
|
+
};
|
|
1683
|
+
|
|
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;
|
|
1644
1746
|
};
|
|
1645
1747
|
|
|
1748
|
+
function updatePosition(str, pos) {
|
|
1749
|
+
const newlines = (str.match(/\n/g) || []).length;
|
|
1750
|
+
if (newlines > 0) {
|
|
1751
|
+
pos.line += newlines;
|
|
1752
|
+
const lastNewline = str.lastIndexOf('\n');
|
|
1753
|
+
pos.column = str.length - lastNewline;
|
|
1754
|
+
}
|
|
1755
|
+
else {
|
|
1756
|
+
pos.column += str.length;
|
|
1757
|
+
}
|
|
1758
|
+
return {
|
|
1759
|
+
line: pos.line,
|
|
1760
|
+
column: pos.column,
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1646
1764
|
const expTokenTypes = {
|
|
1647
1765
|
and: 'AND',
|
|
1648
1766
|
or: 'OR',
|
|
@@ -1669,33 +1787,12 @@ const expTokenTypes = {
|
|
|
1669
1787
|
null: 'LIT',
|
|
1670
1788
|
undefined: 'LIT',
|
|
1671
1789
|
'|': 'PIPE',
|
|
1672
|
-
'=': '
|
|
1790
|
+
'=': 'ASSIGN',
|
|
1673
1791
|
'(': 'LP',
|
|
1674
1792
|
')': 'RP',
|
|
1675
1793
|
',': 'COMMA',
|
|
1676
1794
|
'.': 'DOT',
|
|
1677
1795
|
};
|
|
1678
|
-
|
|
1679
|
-
/**
|
|
1680
|
-
* Update position by a given string, and return a new position with the
|
|
1681
|
-
* updated values.
|
|
1682
|
-
*/
|
|
1683
|
-
function updatePosition(str, pos) {
|
|
1684
|
-
const newlines = (str.match(/\n/g) || []).length;
|
|
1685
|
-
if (newlines > 0) {
|
|
1686
|
-
pos.line += newlines;
|
|
1687
|
-
const lastNewline = str.lastIndexOf('\n');
|
|
1688
|
-
pos.column = str.length - lastNewline;
|
|
1689
|
-
}
|
|
1690
|
-
else {
|
|
1691
|
-
pos.column += str.length;
|
|
1692
|
-
}
|
|
1693
|
-
return {
|
|
1694
|
-
line: pos.line,
|
|
1695
|
-
column: pos.column,
|
|
1696
|
-
};
|
|
1697
|
-
}
|
|
1698
|
-
|
|
1699
1796
|
class ExpTokenizer {
|
|
1700
1797
|
constructor(template) {
|
|
1701
1798
|
Object.defineProperty(this, "template", {
|
|
@@ -1906,6 +2003,36 @@ function isIdentifierChar(char) {
|
|
|
1906
2003
|
return /[\w$]/.test(char);
|
|
1907
2004
|
}
|
|
1908
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
|
+
};
|
|
1909
2036
|
class ExpParser {
|
|
1910
2037
|
constructor(template) {
|
|
1911
2038
|
Object.defineProperty(this, "template", {
|
|
@@ -2005,7 +2132,7 @@ class ExpParser {
|
|
|
2005
2132
|
type: 'ID',
|
|
2006
2133
|
value: token.value,
|
|
2007
2134
|
};
|
|
2008
|
-
if (this.
|
|
2135
|
+
if (this.match('DOT')) {
|
|
2009
2136
|
left.path = this.parsePath();
|
|
2010
2137
|
}
|
|
2011
2138
|
const lp = this.consume('LP');
|
|
@@ -2062,7 +2189,7 @@ class ExpParser {
|
|
|
2062
2189
|
token.type === 'MUL' ||
|
|
2063
2190
|
token.type === 'DIV' ||
|
|
2064
2191
|
token.type === 'MOD' ||
|
|
2065
|
-
token.type === '
|
|
2192
|
+
token.type === 'ASSIGN') {
|
|
2066
2193
|
if (!left) {
|
|
2067
2194
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2068
2195
|
}
|
|
@@ -2082,7 +2209,7 @@ class ExpParser {
|
|
|
2082
2209
|
if (!left) {
|
|
2083
2210
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2084
2211
|
}
|
|
2085
|
-
if (!this.
|
|
2212
|
+
if (!this.match('ID')) {
|
|
2086
2213
|
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2087
2214
|
}
|
|
2088
2215
|
left = {
|
|
@@ -2122,7 +2249,7 @@ class ExpParser {
|
|
|
2122
2249
|
}
|
|
2123
2250
|
parseSequence() {
|
|
2124
2251
|
const elements = [];
|
|
2125
|
-
while (!this.
|
|
2252
|
+
while (!this.match('RP')) {
|
|
2126
2253
|
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2127
2254
|
if (element) {
|
|
2128
2255
|
elements.push(element);
|
|
@@ -2161,11 +2288,11 @@ class ExpParser {
|
|
|
2161
2288
|
peek() {
|
|
2162
2289
|
return this.tokens.at(this.index) ?? null;
|
|
2163
2290
|
}
|
|
2164
|
-
|
|
2291
|
+
match(type) {
|
|
2165
2292
|
return this.peek()?.type === type;
|
|
2166
2293
|
}
|
|
2167
2294
|
consume(type) {
|
|
2168
|
-
if (this.
|
|
2295
|
+
if (this.match(type)) {
|
|
2169
2296
|
return this.next();
|
|
2170
2297
|
}
|
|
2171
2298
|
}
|
|
@@ -2236,10 +2363,7 @@ class Tokenizer {
|
|
|
2236
2363
|
writable: true,
|
|
2237
2364
|
value: 1
|
|
2238
2365
|
});
|
|
2239
|
-
this.options =
|
|
2240
|
-
...parserOptions,
|
|
2241
|
-
...options,
|
|
2242
|
-
};
|
|
2366
|
+
this.options = mergeOptions(parserOptions, options);
|
|
2243
2367
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2244
2368
|
this.tokenPatterns = [
|
|
2245
2369
|
[TokenType.COMMENT, commentOpen, commentClose],
|
|
@@ -2403,49 +2527,23 @@ class Parser extends Tokenizer {
|
|
|
2403
2527
|
if (token.strip?.before && prevToken?.type === TokenType.TEXT) {
|
|
2404
2528
|
prevToken.strip.end = true;
|
|
2405
2529
|
}
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
nodes.push(this.createComment(token));
|
|
2409
|
-
break;
|
|
2410
|
-
case TokenType.DIRECTIVE:
|
|
2411
|
-
nodes.push(this.createDirective(token));
|
|
2412
|
-
break;
|
|
2413
|
-
case TokenType.OUTPUT:
|
|
2414
|
-
nodes.push(this.createOutput(token));
|
|
2415
|
-
break;
|
|
2416
|
-
case TokenType.TEXT:
|
|
2417
|
-
nodes.push(this.createText(token));
|
|
2418
|
-
break;
|
|
2419
|
-
}
|
|
2530
|
+
nodes.push(this.options.parsers[token.name?.toLowerCase() ??
|
|
2531
|
+
token.type?.toLowerCase()]?.(token, this) ?? this.createUnknownNode(token));
|
|
2420
2532
|
prevToken = token;
|
|
2421
2533
|
}
|
|
2422
2534
|
return nodes;
|
|
2423
2535
|
}
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
}
|
|
2428
|
-
createOutput({ val, loc, strip }) {
|
|
2429
|
-
this.advance();
|
|
2430
|
-
return new OutputNode(val, loc, strip, this.parseExp({ val, loc }));
|
|
2431
|
-
}
|
|
2432
|
-
createText({ val, loc, strip }) {
|
|
2433
|
-
this.advance();
|
|
2434
|
-
return new TextNode(val, loc, strip);
|
|
2435
|
-
}
|
|
2436
|
-
createDirective(token) {
|
|
2437
|
-
return (this.options.parsers[token.name.toLowerCase()]?.(token, this) ??
|
|
2438
|
-
this.createUnknownDirective(token));
|
|
2439
|
-
}
|
|
2440
|
-
createUnexpectedDirective(token) {
|
|
2441
|
-
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));
|
|
2442
2539
|
this.advance();
|
|
2443
|
-
return new
|
|
2540
|
+
return new UnexpectedNode(name, token.val, token.loc, token.strip);
|
|
2444
2541
|
}
|
|
2445
|
-
|
|
2446
|
-
|
|
2542
|
+
createUnknownNode(token) {
|
|
2543
|
+
const name = token.name ?? token.type;
|
|
2544
|
+
this.options.debug?.(new CompileError(`Unknown "${name}" node`, this.template, token.loc));
|
|
2447
2545
|
this.advance();
|
|
2448
|
-
return new
|
|
2546
|
+
return new UnknownNode(name, token.val, token.loc, token.strip);
|
|
2449
2547
|
}
|
|
2450
2548
|
parseExp({ val, loc }) {
|
|
2451
2549
|
return this.expParser.parse(val, loc);
|
|
@@ -2459,15 +2557,10 @@ class Parser extends Tokenizer {
|
|
|
2459
2557
|
match(names, token = this.peek()) {
|
|
2460
2558
|
return names.includes(token?.name);
|
|
2461
2559
|
}
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
}
|
|
2467
|
-
requireNoExpression({ expression, name, loc }) {
|
|
2468
|
-
if (expression) {
|
|
2469
|
-
throw this.options.debug?.(new CompileError(`"${name}" should not have expression`, this.template, loc));
|
|
2470
|
-
}
|
|
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));
|
|
2471
2564
|
}
|
|
2472
2565
|
}
|
|
2473
2566
|
|
|
@@ -2516,10 +2609,7 @@ class Compiler extends Context {
|
|
|
2516
2609
|
writable: true,
|
|
2517
2610
|
value: void 0
|
|
2518
2611
|
});
|
|
2519
|
-
this.options =
|
|
2520
|
-
...compilerOptions,
|
|
2521
|
-
...options,
|
|
2522
|
-
};
|
|
2612
|
+
this.options = mergeOptions(compilerOptions, options);
|
|
2523
2613
|
}
|
|
2524
2614
|
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2525
2615
|
this.template = template;
|
|
@@ -2587,12 +2677,12 @@ class Compiler extends Context {
|
|
|
2587
2677
|
const partials = new Map();
|
|
2588
2678
|
const blocks = [];
|
|
2589
2679
|
const traverse = (node) => {
|
|
2590
|
-
if (node.type ===
|
|
2680
|
+
if (node.type === 'INCLUDE') {
|
|
2591
2681
|
if (!partials.has(node.val.value)) {
|
|
2592
2682
|
partials.set(node.val.value, node);
|
|
2593
2683
|
}
|
|
2594
2684
|
}
|
|
2595
|
-
if (node.type ===
|
|
2685
|
+
if (node.type === 'BLOCK') {
|
|
2596
2686
|
blocks.push(node);
|
|
2597
2687
|
}
|
|
2598
2688
|
node.traverse(traverse);
|
|
@@ -2604,7 +2694,7 @@ class Compiler extends Context {
|
|
|
2604
2694
|
};
|
|
2605
2695
|
}
|
|
2606
2696
|
async compileNode(node) {
|
|
2607
|
-
if (node.type ===
|
|
2697
|
+
if (node.type === 'TEMPLATE') {
|
|
2608
2698
|
await this.compileNodes(node.body);
|
|
2609
2699
|
return;
|
|
2610
2700
|
}
|
|
@@ -2612,7 +2702,7 @@ class Compiler extends Context {
|
|
|
2612
2702
|
await this.options.compilers[node.type](node, this);
|
|
2613
2703
|
return;
|
|
2614
2704
|
}
|
|
2615
|
-
this.options.debug?.(new CompileError(`Unknown "${node.name}"
|
|
2705
|
+
this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
|
|
2616
2706
|
}
|
|
2617
2707
|
async compileNodes(nodes) {
|
|
2618
2708
|
for (const child of nodes) {
|
|
@@ -2655,23 +2745,14 @@ class RenderError extends CompileError {
|
|
|
2655
2745
|
}
|
|
2656
2746
|
|
|
2657
2747
|
class Renderer {
|
|
2658
|
-
constructor(
|
|
2748
|
+
constructor(options) {
|
|
2659
2749
|
Object.defineProperty(this, "options", {
|
|
2660
2750
|
enumerable: true,
|
|
2661
2751
|
configurable: true,
|
|
2662
2752
|
writable: true,
|
|
2663
2753
|
value: void 0
|
|
2664
2754
|
});
|
|
2665
|
-
this.options =
|
|
2666
|
-
Object.assign(this.options.globals, globals);
|
|
2667
|
-
Object.assign(this.options.filters, filters);
|
|
2668
|
-
Object.assign(this.options.parsers, parsers);
|
|
2669
|
-
Object.assign(this.options.compilers, compilers);
|
|
2670
|
-
plugins?.forEach((plugin) => {
|
|
2671
|
-
Object.assign(this.options.filters, plugin.filters);
|
|
2672
|
-
Object.assign(this.options.parsers, plugin.parsers);
|
|
2673
|
-
Object.assign(this.options.compilers, plugin.compilers);
|
|
2674
|
-
});
|
|
2755
|
+
this.options = mergeOptions(renderOptions, options);
|
|
2675
2756
|
}
|
|
2676
2757
|
async render(template, globals) {
|
|
2677
2758
|
const compiler = new Compiler(this.options);
|