janja 2.1.0 → 2.1.2
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 +1390 -1227
- package/dist/index.d.ts +65 -105
- package/dist/index.esm.js +1390 -1227
- package/dist/index.js +1390 -1227
- package/package.json +23 -4
package/dist/index.js
CHANGED
|
@@ -1,196 +1,460 @@
|
|
|
1
1
|
(function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
4
|
+
function highlightSource(message, source, { start: { line: l1, column: c1 }, end: { line: l2, column: c2 } }) {
|
|
5
|
+
const output = [];
|
|
6
|
+
const caretLines = new Set();
|
|
7
|
+
const addCaretLine = (index) => {
|
|
8
|
+
caretLines.add(index - 2);
|
|
9
|
+
caretLines.add(index - 1);
|
|
10
|
+
caretLines.add(index);
|
|
11
|
+
caretLines.add(index + 1);
|
|
12
|
+
caretLines.add(index + 2);
|
|
13
|
+
};
|
|
14
|
+
const lines = source.split('\n');
|
|
15
|
+
const indentWidth = String(lines.length).length + 2;
|
|
16
|
+
let caretLinesCount = 0;
|
|
17
|
+
lines.forEach((line, index) => {
|
|
18
|
+
if (!line) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
output.push(`${`${index + 1}| `.padStart(indentWidth, ' ')}${line}`);
|
|
22
|
+
if (l1 === index + 1) {
|
|
23
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c1 - 1)}^${l1 === l2 && c2 > c1 + 1 ? `${' '.repeat(c2 - c1 - 2)}^` : ''}`;
|
|
24
|
+
if (/\S/.test(caretLine)) {
|
|
25
|
+
output.push(caretLine);
|
|
26
|
+
}
|
|
27
|
+
addCaretLine(++caretLinesCount + index);
|
|
28
|
+
}
|
|
29
|
+
if (l1 !== l2 && l2 === index + 1) {
|
|
30
|
+
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c2 - 1)}${'^'.repeat(1)}`;
|
|
31
|
+
if (/\S/.test(caretLine)) {
|
|
32
|
+
output.push(caretLine);
|
|
33
|
+
}
|
|
34
|
+
addCaretLine(++caretLinesCount + index);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
38
|
+
return `${message}
|
|
39
|
+
|
|
40
|
+
${output
|
|
41
|
+
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
42
|
+
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
43
|
+
.join('\n')}
|
|
44
|
+
`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class CompileError extends SyntaxError {
|
|
48
|
+
constructor(message, src, loc) {
|
|
49
|
+
super(message);
|
|
50
|
+
Object.defineProperty(this, "src", {
|
|
31
51
|
enumerable: true,
|
|
32
52
|
configurable: true,
|
|
33
53
|
writable: true,
|
|
34
|
-
value:
|
|
54
|
+
value: src
|
|
35
55
|
});
|
|
36
56
|
Object.defineProperty(this, "loc", {
|
|
37
57
|
enumerable: true,
|
|
38
58
|
configurable: true,
|
|
39
59
|
writable: true,
|
|
40
|
-
value:
|
|
41
|
-
});
|
|
42
|
-
Object.defineProperty(this, "body", {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true,
|
|
46
|
-
value: void 0
|
|
60
|
+
value: loc
|
|
47
61
|
});
|
|
62
|
+
this.name = 'CompileError';
|
|
63
|
+
CompileError.captureStackTrace?.(this, this.constructor);
|
|
48
64
|
}
|
|
49
|
-
|
|
50
|
-
this.
|
|
65
|
+
get details() {
|
|
66
|
+
return this.loc
|
|
67
|
+
? highlightSource(this.message, this.src, this.loc)
|
|
68
|
+
: this.message;
|
|
51
69
|
}
|
|
52
70
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
|
|
72
|
+
const CONTEXT = 'c';
|
|
73
|
+
const ESCAPE = 'e';
|
|
74
|
+
const FILTERS = 'f';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Contains the source map information.
|
|
78
|
+
* @example
|
|
79
|
+
* - template:
|
|
80
|
+
* {{ foo }}
|
|
81
|
+
*
|
|
82
|
+
* - compiled:
|
|
83
|
+
* s+=e(foo);
|
|
84
|
+
*
|
|
85
|
+
* - sourceMap:
|
|
86
|
+
* Mapping {
|
|
87
|
+
* source: {
|
|
88
|
+
* start: {
|
|
89
|
+
* line: 2,
|
|
90
|
+
* column: 4,
|
|
91
|
+
* },
|
|
92
|
+
* end: {
|
|
93
|
+
* line: 3,
|
|
94
|
+
* column: 5,
|
|
95
|
+
* },
|
|
96
|
+
* },
|
|
97
|
+
* target: {
|
|
98
|
+
* start: {
|
|
99
|
+
* line: 2,
|
|
100
|
+
* column: 3,
|
|
101
|
+
* },
|
|
102
|
+
* end: {
|
|
103
|
+
* line: 3,
|
|
104
|
+
* column: 8,
|
|
105
|
+
* },
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
*/
|
|
109
|
+
class SourceMap {
|
|
110
|
+
constructor() {
|
|
111
|
+
Object.defineProperty(this, "mappings", {
|
|
63
112
|
enumerable: true,
|
|
64
113
|
configurable: true,
|
|
65
114
|
writable: true,
|
|
66
|
-
value:
|
|
115
|
+
value: []
|
|
67
116
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
117
|
+
}
|
|
118
|
+
addMapping(source, target) {
|
|
119
|
+
this.mappings.push({
|
|
120
|
+
source,
|
|
121
|
+
target,
|
|
73
122
|
});
|
|
74
123
|
}
|
|
124
|
+
getSourceLoc({ line, column }) {
|
|
125
|
+
return this.mappings
|
|
126
|
+
.filter(({ target: { start, end } }) => start.line <= line &&
|
|
127
|
+
end.line >= line &&
|
|
128
|
+
(start.line < line || start.column <= column) &&
|
|
129
|
+
(end.line > line || end.column >= column))
|
|
130
|
+
.map(({ source }) => source);
|
|
131
|
+
}
|
|
75
132
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
configurable: true,
|
|
82
|
-
writable: true,
|
|
83
|
-
value: val
|
|
84
|
-
});
|
|
85
|
-
Object.defineProperty(this, "loc", {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
configurable: true,
|
|
88
|
-
writable: true,
|
|
89
|
-
value: loc
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(this, "strip", {
|
|
92
|
-
enumerable: true,
|
|
93
|
-
configurable: true,
|
|
94
|
-
writable: true,
|
|
95
|
-
value: strip
|
|
96
|
-
});
|
|
97
|
-
Object.defineProperty(this, "exp", {
|
|
133
|
+
|
|
134
|
+
class OutScript extends SourceMap {
|
|
135
|
+
constructor() {
|
|
136
|
+
super(...arguments);
|
|
137
|
+
Object.defineProperty(this, "code", {
|
|
98
138
|
enumerable: true,
|
|
99
139
|
configurable: true,
|
|
100
140
|
writable: true,
|
|
101
|
-
value:
|
|
141
|
+
value: ''
|
|
102
142
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
143
|
+
}
|
|
144
|
+
get script() {
|
|
145
|
+
// eslint-disable-next-line no-new-func
|
|
146
|
+
return new Function(CONTEXT, ESCAPE, FILTERS, this.code);
|
|
147
|
+
}
|
|
148
|
+
start() {
|
|
149
|
+
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
150
|
+
}
|
|
151
|
+
end() {
|
|
152
|
+
this.pushRaw(null, 'return s;', '})();');
|
|
153
|
+
}
|
|
154
|
+
pushRaw(loc, ...lines) {
|
|
155
|
+
const start = this.code.length;
|
|
156
|
+
for (const line of lines) {
|
|
157
|
+
this.code += line;
|
|
158
|
+
}
|
|
159
|
+
if (!loc) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this.addMapping(loc, {
|
|
163
|
+
start: {
|
|
164
|
+
line: 1,
|
|
165
|
+
column: start,
|
|
166
|
+
},
|
|
167
|
+
end: {
|
|
168
|
+
line: 1,
|
|
169
|
+
column: this.code.length,
|
|
170
|
+
},
|
|
108
171
|
});
|
|
109
172
|
}
|
|
173
|
+
pushStr(loc, s) {
|
|
174
|
+
if (s) {
|
|
175
|
+
this.pushRaw(loc, `s+="${s
|
|
176
|
+
.replace(/\\/g, '\\\\')
|
|
177
|
+
.replace(/"/g, '\\"')
|
|
178
|
+
.replace(/[\n\r]/g, '\\n')}";`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
pushVar(loc, v) {
|
|
182
|
+
this.pushRaw(loc, `s+=${ESCAPE}(${v});`);
|
|
183
|
+
}
|
|
110
184
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
configurable: true,
|
|
117
|
-
writable: true,
|
|
118
|
-
value: val
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(this, "loc", {
|
|
185
|
+
|
|
186
|
+
class Context extends OutScript {
|
|
187
|
+
constructor() {
|
|
188
|
+
super(...arguments);
|
|
189
|
+
Object.defineProperty(this, "context", {
|
|
121
190
|
enumerable: true,
|
|
122
191
|
configurable: true,
|
|
123
192
|
writable: true,
|
|
124
|
-
value:
|
|
193
|
+
value: CONTEXT
|
|
125
194
|
});
|
|
126
|
-
Object.defineProperty(this, "
|
|
195
|
+
Object.defineProperty(this, "contexts", {
|
|
127
196
|
enumerable: true,
|
|
128
197
|
configurable: true,
|
|
129
198
|
writable: true,
|
|
130
|
-
value:
|
|
199
|
+
value: [CONTEXT]
|
|
131
200
|
});
|
|
132
|
-
Object.defineProperty(this, "
|
|
201
|
+
Object.defineProperty(this, "index", {
|
|
133
202
|
enumerable: true,
|
|
134
203
|
configurable: true,
|
|
135
204
|
writable: true,
|
|
136
|
-
value:
|
|
205
|
+
value: 0
|
|
137
206
|
});
|
|
138
207
|
}
|
|
208
|
+
in() {
|
|
209
|
+
this.context = `${this.context}_${this.index++}`;
|
|
210
|
+
this.contexts.push(this.context);
|
|
211
|
+
return this.context;
|
|
212
|
+
}
|
|
213
|
+
out() {
|
|
214
|
+
if (this.context === CONTEXT) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
this.contexts.pop();
|
|
218
|
+
this.context = this.contexts.at(-1);
|
|
219
|
+
}
|
|
139
220
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
221
|
+
|
|
222
|
+
class Safe extends String {
|
|
223
|
+
constructor(value) {
|
|
224
|
+
super(`${value}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function escape(v) {
|
|
229
|
+
return `${v}`
|
|
230
|
+
.replace(/&/g, '&')
|
|
231
|
+
.replace(/"/g, '"')
|
|
232
|
+
.replace(/'/g, ''')
|
|
233
|
+
.replace(/</g, '<')
|
|
234
|
+
.replace(/>/g, '>');
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function abs(value = 0) {
|
|
238
|
+
return Math.abs(value);
|
|
239
|
+
}
|
|
240
|
+
function add(value = 0, addend = 0) {
|
|
241
|
+
return value + addend;
|
|
242
|
+
}
|
|
243
|
+
function capitalize(value = '') {
|
|
244
|
+
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
245
|
+
}
|
|
246
|
+
function ceil(value = 0) {
|
|
247
|
+
return Math.ceil(value);
|
|
248
|
+
}
|
|
249
|
+
function compact(value = []) {
|
|
250
|
+
return value.filter((v) => v != null);
|
|
251
|
+
}
|
|
252
|
+
function div(value = 0, divisor = 1) {
|
|
253
|
+
return value / divisor;
|
|
254
|
+
}
|
|
255
|
+
function entries(value = {}) {
|
|
256
|
+
return Object.entries(value);
|
|
257
|
+
}
|
|
258
|
+
function even(value = 0) {
|
|
259
|
+
return +value % 2 === 0;
|
|
260
|
+
}
|
|
261
|
+
function fallback(value, defaultValue, anyFalsy = false) {
|
|
262
|
+
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
263
|
+
}
|
|
264
|
+
function first(value = []) {
|
|
265
|
+
return [...value][0];
|
|
266
|
+
}
|
|
267
|
+
function get(value, ...path) {
|
|
268
|
+
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
269
|
+
}
|
|
270
|
+
function groupby(value = [], key) {
|
|
271
|
+
return value.reduce((o, v) => {
|
|
272
|
+
const k = v[key];
|
|
273
|
+
return {
|
|
274
|
+
...o,
|
|
275
|
+
[k]: [...(o[k] || []), v],
|
|
276
|
+
};
|
|
277
|
+
}, {});
|
|
278
|
+
}
|
|
279
|
+
function join(value = [], separator = '') {
|
|
280
|
+
return [...value].join(separator);
|
|
281
|
+
}
|
|
282
|
+
function json(value = null, indent = 0) {
|
|
283
|
+
return new Safe(JSON.stringify(value, null, indent));
|
|
284
|
+
}
|
|
285
|
+
function keys(value = {}) {
|
|
286
|
+
return Object.keys(value);
|
|
287
|
+
}
|
|
288
|
+
function last(value = []) {
|
|
289
|
+
return [...value].reverse()[0];
|
|
290
|
+
}
|
|
291
|
+
function length(value = '') {
|
|
292
|
+
return value.length;
|
|
293
|
+
}
|
|
294
|
+
function lower(value = '') {
|
|
295
|
+
return value.toLowerCase();
|
|
296
|
+
}
|
|
297
|
+
function map(value = [], key, defaultValue) {
|
|
298
|
+
return value.map((o) => o[key] ?? defaultValue);
|
|
299
|
+
}
|
|
300
|
+
function max(value = 0, ...rest) {
|
|
301
|
+
return Math.max(value, ...rest);
|
|
302
|
+
}
|
|
303
|
+
function min(value = 0, ...rest) {
|
|
304
|
+
return Math.min(value, ...rest);
|
|
305
|
+
}
|
|
306
|
+
function mul(value = 0, multiplier = 1) {
|
|
307
|
+
return value * multiplier;
|
|
308
|
+
}
|
|
309
|
+
function odd(value = 0) {
|
|
310
|
+
return +value % 2 === 1;
|
|
311
|
+
}
|
|
312
|
+
function omit(value = {}, ...omitKeys) {
|
|
313
|
+
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
314
|
+
}
|
|
315
|
+
function pick(value = {}, ...pickKeys) {
|
|
316
|
+
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
317
|
+
}
|
|
318
|
+
function repeat(value = '', count = 0) {
|
|
319
|
+
return value.repeat(count);
|
|
320
|
+
}
|
|
321
|
+
function replace(value = '', search, replacement) {
|
|
322
|
+
return value.replace(new RegExp(search, 'g'), replacement);
|
|
323
|
+
}
|
|
324
|
+
function reverse(value = []) {
|
|
325
|
+
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
326
|
+
}
|
|
327
|
+
function safe(value = '') {
|
|
328
|
+
return new Safe(value);
|
|
329
|
+
}
|
|
330
|
+
function slice(value = '', start = 0, end) {
|
|
331
|
+
return value.slice(start, end);
|
|
332
|
+
}
|
|
333
|
+
function sort(value = []) {
|
|
334
|
+
return Array.isArray(value) ? value.sort() : [...value].sort().join('');
|
|
335
|
+
}
|
|
336
|
+
function split(value = '', separator = '') {
|
|
337
|
+
return value.split(separator);
|
|
338
|
+
}
|
|
339
|
+
function sub(value = 0, subtrahend = 0) {
|
|
340
|
+
return value - subtrahend;
|
|
341
|
+
}
|
|
342
|
+
function sum(value = []) {
|
|
343
|
+
return value.reduce((a, b) => a + b, 0);
|
|
344
|
+
}
|
|
345
|
+
function trim(value = '') {
|
|
346
|
+
return value.trim();
|
|
347
|
+
}
|
|
348
|
+
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
349
|
+
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
350
|
+
}
|
|
351
|
+
function unique(value = '') {
|
|
352
|
+
return Array.isArray(value)
|
|
353
|
+
? Array.from(new Set(value))
|
|
354
|
+
: Array.from(new Set(value)).join('');
|
|
355
|
+
}
|
|
356
|
+
function upper(value = '') {
|
|
357
|
+
return value.toUpperCase();
|
|
358
|
+
}
|
|
359
|
+
function urldecode(value = '') {
|
|
360
|
+
return decodeURIComponent(value);
|
|
361
|
+
}
|
|
362
|
+
function urlencode(value = '') {
|
|
363
|
+
return encodeURIComponent(value);
|
|
364
|
+
}
|
|
365
|
+
function values(value = {}) {
|
|
366
|
+
return Object.values(value);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
var _filters = /*#__PURE__*/Object.freeze({
|
|
370
|
+
__proto__: null,
|
|
371
|
+
abs: abs,
|
|
372
|
+
add: add,
|
|
373
|
+
capitalize: capitalize,
|
|
374
|
+
ceil: ceil,
|
|
375
|
+
compact: compact,
|
|
376
|
+
div: div,
|
|
377
|
+
entries: entries,
|
|
378
|
+
escape: escape,
|
|
379
|
+
even: even,
|
|
380
|
+
fallback: fallback,
|
|
381
|
+
first: first,
|
|
382
|
+
get: get,
|
|
383
|
+
groupby: groupby,
|
|
384
|
+
join: join,
|
|
385
|
+
json: json,
|
|
386
|
+
keys: keys,
|
|
387
|
+
last: last,
|
|
388
|
+
length: length,
|
|
389
|
+
lower: lower,
|
|
390
|
+
map: map,
|
|
391
|
+
max: max,
|
|
392
|
+
min: min,
|
|
393
|
+
mul: mul,
|
|
394
|
+
odd: odd,
|
|
395
|
+
omit: omit,
|
|
396
|
+
pick: pick,
|
|
397
|
+
repeat: repeat,
|
|
398
|
+
replace: replace,
|
|
399
|
+
reverse: reverse,
|
|
400
|
+
safe: safe,
|
|
401
|
+
slice: slice,
|
|
402
|
+
sort: sort,
|
|
403
|
+
split: split,
|
|
404
|
+
sub: sub,
|
|
405
|
+
sum: sum,
|
|
406
|
+
trim: trim,
|
|
407
|
+
truncate: truncate,
|
|
408
|
+
unique: unique,
|
|
409
|
+
upper: upper,
|
|
410
|
+
urldecode: urldecode,
|
|
411
|
+
urlencode: urlencode,
|
|
412
|
+
values: values
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
416
|
+
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
417
|
+
}
|
|
418
|
+
async function compileSuper({ loc }, compiler) {
|
|
419
|
+
if (!compiler.state.block) {
|
|
420
|
+
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
424
|
+
}
|
|
425
|
+
const compilers$c = {
|
|
426
|
+
BLOCK: compileBlock,
|
|
427
|
+
SUPER: compileSuper,
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
class Traversal {
|
|
431
|
+
constructor() {
|
|
432
|
+
Object.defineProperty(this, "type", {
|
|
156
433
|
enumerable: true,
|
|
157
434
|
configurable: true,
|
|
158
435
|
writable: true,
|
|
159
|
-
value:
|
|
436
|
+
value: null
|
|
160
437
|
});
|
|
161
438
|
Object.defineProperty(this, "loc", {
|
|
162
439
|
enumerable: true,
|
|
163
440
|
configurable: true,
|
|
164
441
|
writable: true,
|
|
165
|
-
value:
|
|
166
|
-
});
|
|
167
|
-
Object.defineProperty(this, "strip", {
|
|
168
|
-
enumerable: true,
|
|
169
|
-
configurable: true,
|
|
170
|
-
writable: true,
|
|
171
|
-
value: strip
|
|
442
|
+
value: null
|
|
172
443
|
});
|
|
173
|
-
Object.defineProperty(this, "
|
|
444
|
+
Object.defineProperty(this, "body", {
|
|
174
445
|
enumerable: true,
|
|
175
446
|
configurable: true,
|
|
176
447
|
writable: true,
|
|
177
|
-
value:
|
|
448
|
+
value: void 0
|
|
178
449
|
});
|
|
179
450
|
}
|
|
180
451
|
traverse(cb) {
|
|
181
452
|
this.body?.forEach(cb);
|
|
182
|
-
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
183
453
|
}
|
|
184
454
|
}
|
|
185
|
-
class
|
|
186
|
-
constructor(
|
|
455
|
+
class RootNode extends Traversal {
|
|
456
|
+
constructor(body, loc) {
|
|
187
457
|
super();
|
|
188
|
-
Object.defineProperty(this, "test", {
|
|
189
|
-
enumerable: true,
|
|
190
|
-
configurable: true,
|
|
191
|
-
writable: true,
|
|
192
|
-
value: test
|
|
193
|
-
});
|
|
194
458
|
Object.defineProperty(this, "body", {
|
|
195
459
|
enumerable: true,
|
|
196
460
|
configurable: true,
|
|
@@ -203,23 +467,24 @@
|
|
|
203
467
|
writable: true,
|
|
204
468
|
value: loc
|
|
205
469
|
});
|
|
206
|
-
Object.defineProperty(this, "strip", {
|
|
207
|
-
enumerable: true,
|
|
208
|
-
configurable: true,
|
|
209
|
-
writable: true,
|
|
210
|
-
value: strip
|
|
211
|
-
});
|
|
212
470
|
Object.defineProperty(this, "type", {
|
|
213
471
|
enumerable: true,
|
|
214
472
|
configurable: true,
|
|
215
473
|
writable: true,
|
|
216
|
-
value:
|
|
474
|
+
value: 'TEMPLATE'
|
|
217
475
|
});
|
|
218
476
|
}
|
|
219
477
|
}
|
|
220
|
-
|
|
221
|
-
|
|
478
|
+
|
|
479
|
+
class BlockNode extends Traversal {
|
|
480
|
+
constructor(val, body, loc, strip) {
|
|
222
481
|
super();
|
|
482
|
+
Object.defineProperty(this, "val", {
|
|
483
|
+
enumerable: true,
|
|
484
|
+
configurable: true,
|
|
485
|
+
writable: true,
|
|
486
|
+
value: val
|
|
487
|
+
});
|
|
223
488
|
Object.defineProperty(this, "body", {
|
|
224
489
|
enumerable: true,
|
|
225
490
|
configurable: true,
|
|
@@ -242,24 +507,18 @@
|
|
|
242
507
|
enumerable: true,
|
|
243
508
|
configurable: true,
|
|
244
509
|
writable: true,
|
|
245
|
-
value:
|
|
510
|
+
value: 'BLOCK'
|
|
246
511
|
});
|
|
247
512
|
}
|
|
248
513
|
}
|
|
249
|
-
class
|
|
250
|
-
constructor(
|
|
514
|
+
class SuperNode extends Traversal {
|
|
515
|
+
constructor(val, loc, strip) {
|
|
251
516
|
super();
|
|
252
|
-
Object.defineProperty(this, "
|
|
253
|
-
enumerable: true,
|
|
254
|
-
configurable: true,
|
|
255
|
-
writable: true,
|
|
256
|
-
value: loop
|
|
257
|
-
});
|
|
258
|
-
Object.defineProperty(this, "body", {
|
|
517
|
+
Object.defineProperty(this, "val", {
|
|
259
518
|
enumerable: true,
|
|
260
519
|
configurable: true,
|
|
261
520
|
writable: true,
|
|
262
|
-
value:
|
|
521
|
+
value: val
|
|
263
522
|
});
|
|
264
523
|
Object.defineProperty(this, "loc", {
|
|
265
524
|
enumerable: true,
|
|
@@ -277,41 +536,194 @@
|
|
|
277
536
|
enumerable: true,
|
|
278
537
|
configurable: true,
|
|
279
538
|
writable: true,
|
|
280
|
-
value:
|
|
539
|
+
value: 'SUPER'
|
|
281
540
|
});
|
|
282
541
|
}
|
|
283
542
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
543
|
+
|
|
544
|
+
async function* parseBlock(token, parser) {
|
|
545
|
+
if (!token.expression) {
|
|
546
|
+
parser.emitExpErr(token);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
yield 'NEXT';
|
|
550
|
+
const body = await parser.parseUntil(['endblock']);
|
|
551
|
+
if (parser.match(['endblock'])) {
|
|
552
|
+
yield 'NEXT';
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
556
|
+
}
|
|
557
|
+
yield new BlockNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
558
|
+
}
|
|
559
|
+
async function* parseSuper(token, parser) {
|
|
560
|
+
if (token.expression) {
|
|
561
|
+
parser.emitExpErr(token, false);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
yield 'NEXT';
|
|
565
|
+
yield new SuperNode(token.val, token.loc, token.strip);
|
|
566
|
+
}
|
|
567
|
+
const parsers$c = {
|
|
568
|
+
block: parseBlock,
|
|
569
|
+
super: parseSuper,
|
|
570
|
+
endblock: 'unexpected',
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const plugin$c = {
|
|
574
|
+
compilers: compilers$c,
|
|
575
|
+
parsers: parsers$c,
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
const expTokenOperators = {
|
|
579
|
+
AND: '&&',
|
|
580
|
+
OR: '||',
|
|
581
|
+
IS: '',
|
|
582
|
+
NOT: '!',
|
|
583
|
+
EQ: '===',
|
|
584
|
+
NE: '!==',
|
|
585
|
+
GT: '>',
|
|
586
|
+
LT: '<',
|
|
587
|
+
GE: '>=',
|
|
588
|
+
LE: '<=',
|
|
589
|
+
IN: ' in ',
|
|
590
|
+
NI: ' in ',
|
|
591
|
+
OF: ' of ',
|
|
592
|
+
ADD: '+',
|
|
593
|
+
SUB: '-',
|
|
594
|
+
MUL: '*',
|
|
595
|
+
DIV: '/',
|
|
596
|
+
MOD: '%',
|
|
597
|
+
PIPE: '',
|
|
598
|
+
ASSIGN: '=',
|
|
599
|
+
LP: '(',
|
|
600
|
+
RP: ')',
|
|
601
|
+
COMMA: ',',
|
|
602
|
+
DOT: '.',
|
|
603
|
+
ID: '',
|
|
604
|
+
LIT: '',
|
|
605
|
+
IF: '',
|
|
606
|
+
ELSE: '',
|
|
607
|
+
};
|
|
608
|
+
class ExpCompiler {
|
|
609
|
+
constructor() {
|
|
610
|
+
Object.defineProperty(this, "context", {
|
|
300
611
|
enumerable: true,
|
|
301
612
|
configurable: true,
|
|
302
613
|
writable: true,
|
|
303
|
-
value:
|
|
614
|
+
value: void 0
|
|
304
615
|
});
|
|
305
|
-
Object.defineProperty(this, "
|
|
616
|
+
Object.defineProperty(this, "filters", {
|
|
306
617
|
enumerable: true,
|
|
307
618
|
configurable: true,
|
|
308
619
|
writable: true,
|
|
309
|
-
value:
|
|
620
|
+
value: void 0
|
|
310
621
|
});
|
|
311
622
|
}
|
|
623
|
+
compile(expression, context = CONTEXT, filters = FILTERS) {
|
|
624
|
+
this.context = context;
|
|
625
|
+
this.filters = filters;
|
|
626
|
+
return expression ? this.compileExp(expression) : '""';
|
|
627
|
+
}
|
|
628
|
+
compileExp(expression) {
|
|
629
|
+
switch (expression.type) {
|
|
630
|
+
case 'NOT':
|
|
631
|
+
return this.compileNot(expression);
|
|
632
|
+
case 'ID':
|
|
633
|
+
return this.compileId(expression);
|
|
634
|
+
case 'LIT':
|
|
635
|
+
return this.compileLit(expression);
|
|
636
|
+
case 'PIPE':
|
|
637
|
+
return this.compilePipe(expression);
|
|
638
|
+
case 'OF':
|
|
639
|
+
return this.compileOf(expression);
|
|
640
|
+
case 'ASSIGN':
|
|
641
|
+
return this.compileAssign(expression);
|
|
642
|
+
case 'IS':
|
|
643
|
+
return this.compileIs(expression);
|
|
644
|
+
case 'AND':
|
|
645
|
+
case 'OR':
|
|
646
|
+
case 'EQ':
|
|
647
|
+
case 'NE':
|
|
648
|
+
case 'GT':
|
|
649
|
+
case 'LT':
|
|
650
|
+
case 'GE':
|
|
651
|
+
case 'LE':
|
|
652
|
+
case 'IN':
|
|
653
|
+
case 'NI':
|
|
654
|
+
case 'ADD':
|
|
655
|
+
case 'SUB':
|
|
656
|
+
case 'MUL':
|
|
657
|
+
case 'DIV':
|
|
658
|
+
case 'MOD':
|
|
659
|
+
return this.compileBinary(expression);
|
|
660
|
+
case 'IF':
|
|
661
|
+
return this.compileTernary(expression);
|
|
662
|
+
case 'SEQ':
|
|
663
|
+
return this.compileSeq(expression);
|
|
664
|
+
// no default
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
compileNot({ argument }) {
|
|
668
|
+
return `!${this.compileExp(argument)}`;
|
|
669
|
+
}
|
|
670
|
+
compileId({ value, path, args }) {
|
|
671
|
+
let s = `${this.context}.${value}`;
|
|
672
|
+
if (path) {
|
|
673
|
+
s += path.map((p) => `.${p.value}`).join('');
|
|
674
|
+
}
|
|
675
|
+
if (args) {
|
|
676
|
+
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
677
|
+
}
|
|
678
|
+
return s;
|
|
679
|
+
}
|
|
680
|
+
compileLit({ value }) {
|
|
681
|
+
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
682
|
+
}
|
|
683
|
+
compileSeq({ elements }) {
|
|
684
|
+
return elements.map((element) => this.compileExp(element)).toString();
|
|
685
|
+
}
|
|
686
|
+
compilePipe({ left, right }) {
|
|
687
|
+
const { value: name, args = [] } = right;
|
|
688
|
+
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(',')}))`;
|
|
689
|
+
}
|
|
690
|
+
compileOf({ left, right }) {
|
|
691
|
+
// destructuring
|
|
692
|
+
if (left.type === 'SEQ') {
|
|
693
|
+
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
694
|
+
}
|
|
695
|
+
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
696
|
+
}
|
|
697
|
+
compileAssign({ left, right }) {
|
|
698
|
+
if (left.type === 'ID') {
|
|
699
|
+
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
700
|
+
}
|
|
701
|
+
// destructuring
|
|
702
|
+
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
703
|
+
}
|
|
704
|
+
compileIs({ left, right }) {
|
|
705
|
+
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
706
|
+
}
|
|
707
|
+
compileBinary({ type, left, right }) {
|
|
708
|
+
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
709
|
+
return type === 'NI' ? `!${ret}` : ret;
|
|
710
|
+
}
|
|
711
|
+
compileTernary({ test, consequent, alternative }) {
|
|
712
|
+
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
713
|
+
}
|
|
312
714
|
}
|
|
313
|
-
|
|
314
|
-
|
|
715
|
+
|
|
716
|
+
async function compileCall({ val, body, loc }, compiler) {
|
|
717
|
+
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
718
|
+
await compiler.compileNodes(body);
|
|
719
|
+
compiler.pushRaw(null, '});');
|
|
720
|
+
}
|
|
721
|
+
const compilers$b = {
|
|
722
|
+
CALL: compileCall,
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
class CallNode extends Traversal {
|
|
726
|
+
constructor(val, body, loc, strip) {
|
|
315
727
|
super();
|
|
316
728
|
Object.defineProperty(this, "val", {
|
|
317
729
|
enumerable: true,
|
|
@@ -319,6 +731,12 @@
|
|
|
319
731
|
writable: true,
|
|
320
732
|
value: val
|
|
321
733
|
});
|
|
734
|
+
Object.defineProperty(this, "body", {
|
|
735
|
+
enumerable: true,
|
|
736
|
+
configurable: true,
|
|
737
|
+
writable: true,
|
|
738
|
+
value: body
|
|
739
|
+
});
|
|
322
740
|
Object.defineProperty(this, "loc", {
|
|
323
741
|
enumerable: true,
|
|
324
742
|
configurable: true,
|
|
@@ -335,24 +753,59 @@
|
|
|
335
753
|
enumerable: true,
|
|
336
754
|
configurable: true,
|
|
337
755
|
writable: true,
|
|
338
|
-
value:
|
|
756
|
+
value: 'CALL'
|
|
339
757
|
});
|
|
340
758
|
}
|
|
341
759
|
}
|
|
342
|
-
|
|
343
|
-
|
|
760
|
+
|
|
761
|
+
async function* parseCall(token, parser) {
|
|
762
|
+
if (!token.expression) {
|
|
763
|
+
parser.emitExpErr(token);
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
yield 'NEXT';
|
|
767
|
+
const body = await parser.parseUntil(['endcall']);
|
|
768
|
+
if (parser.match(['endcall'])) {
|
|
769
|
+
yield 'NEXT';
|
|
770
|
+
}
|
|
771
|
+
else {
|
|
772
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
773
|
+
}
|
|
774
|
+
yield new CallNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
775
|
+
}
|
|
776
|
+
const parsers$b = {
|
|
777
|
+
call: parseCall,
|
|
778
|
+
endcall: 'unexpected',
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const plugin$b = {
|
|
782
|
+
compilers: compilers$b,
|
|
783
|
+
parsers: parsers$b,
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
async function compileCapture({ val, body, loc }, compiler) {
|
|
787
|
+
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
788
|
+
await compiler.compileNodes(body);
|
|
789
|
+
compiler.pushRaw(null, 'return s;})("");');
|
|
790
|
+
}
|
|
791
|
+
const compilers$a = {
|
|
792
|
+
CAPTURE: compileCapture,
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
class CaptureNode extends Traversal {
|
|
796
|
+
constructor(val, body, loc, strip) {
|
|
344
797
|
super();
|
|
345
|
-
Object.defineProperty(this, "
|
|
798
|
+
Object.defineProperty(this, "val", {
|
|
346
799
|
enumerable: true,
|
|
347
800
|
configurable: true,
|
|
348
801
|
writable: true,
|
|
349
|
-
value:
|
|
802
|
+
value: val
|
|
350
803
|
});
|
|
351
|
-
Object.defineProperty(this, "
|
|
804
|
+
Object.defineProperty(this, "body", {
|
|
352
805
|
enumerable: true,
|
|
353
806
|
configurable: true,
|
|
354
807
|
writable: true,
|
|
355
|
-
value:
|
|
808
|
+
value: body
|
|
356
809
|
});
|
|
357
810
|
Object.defineProperty(this, "loc", {
|
|
358
811
|
enumerable: true,
|
|
@@ -370,19 +823,48 @@
|
|
|
370
823
|
enumerable: true,
|
|
371
824
|
configurable: true,
|
|
372
825
|
writable: true,
|
|
373
|
-
value:
|
|
826
|
+
value: 'CAPTURE'
|
|
374
827
|
});
|
|
375
828
|
}
|
|
376
829
|
}
|
|
377
|
-
|
|
378
|
-
|
|
830
|
+
|
|
831
|
+
async function* parseCapture(token, parser) {
|
|
832
|
+
if (!token.expression) {
|
|
833
|
+
parser.emitExpErr(token);
|
|
834
|
+
return;
|
|
835
|
+
}
|
|
836
|
+
yield 'NEXT';
|
|
837
|
+
const body = await parser.parseUntil(['endcapture']);
|
|
838
|
+
if (parser.match(['endcapture'])) {
|
|
839
|
+
yield 'NEXT';
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
843
|
+
}
|
|
844
|
+
yield new CaptureNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
845
|
+
}
|
|
846
|
+
const parsers$a = {
|
|
847
|
+
capture: parseCapture,
|
|
848
|
+
endcapture: 'unexpected',
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
const plugin$a = {
|
|
852
|
+
compilers: compilers$a,
|
|
853
|
+
parsers: parsers$a,
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
async function compileComment({ val, loc }, compiler) {
|
|
857
|
+
if (!compiler.options.stripComments) {
|
|
858
|
+
compiler.pushStr(loc, `<!--${val}-->`);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
const compilers$9 = {
|
|
862
|
+
COMMENT: compileComment,
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
class CommentNode extends Traversal {
|
|
866
|
+
constructor(val, loc, strip) {
|
|
379
867
|
super();
|
|
380
|
-
Object.defineProperty(this, "name", {
|
|
381
|
-
enumerable: true,
|
|
382
|
-
configurable: true,
|
|
383
|
-
writable: true,
|
|
384
|
-
value: name
|
|
385
|
-
});
|
|
386
868
|
Object.defineProperty(this, "val", {
|
|
387
869
|
enumerable: true,
|
|
388
870
|
configurable: true,
|
|
@@ -405,18 +887,62 @@
|
|
|
405
887
|
enumerable: true,
|
|
406
888
|
configurable: true,
|
|
407
889
|
writable: true,
|
|
408
|
-
value:
|
|
890
|
+
value: 'COMMENT'
|
|
409
891
|
});
|
|
410
892
|
}
|
|
411
893
|
}
|
|
412
|
-
|
|
413
|
-
|
|
894
|
+
|
|
895
|
+
async function* parseComment({ val, loc, strip }) {
|
|
896
|
+
yield 'NEXT';
|
|
897
|
+
yield new CommentNode(val, loc, strip);
|
|
898
|
+
}
|
|
899
|
+
const parsers$9 = {
|
|
900
|
+
comment: parseComment,
|
|
901
|
+
};
|
|
902
|
+
|
|
903
|
+
const plugin$9 = {
|
|
904
|
+
compilers: compilers$9,
|
|
905
|
+
parsers: parsers$9,
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
async function compileFor({ loop, body }, compiler) {
|
|
909
|
+
const { context } = compiler;
|
|
910
|
+
if (loop.left.type === 'SEQ') {
|
|
911
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.value},`, '};');
|
|
915
|
+
}
|
|
916
|
+
await compiler.compileNodes(body);
|
|
917
|
+
compiler.out();
|
|
918
|
+
compiler.pushRaw(null, '}');
|
|
919
|
+
}
|
|
920
|
+
function compileBreak({ loc }, compiler) {
|
|
921
|
+
compiler.pushRaw(loc, 'break;');
|
|
922
|
+
}
|
|
923
|
+
function compileContinue({ loc }, compiler) {
|
|
924
|
+
compiler.pushRaw(loc, 'continue;');
|
|
925
|
+
}
|
|
926
|
+
const compilers$8 = {
|
|
927
|
+
FOR: compileFor,
|
|
928
|
+
BREAK: compileBreak,
|
|
929
|
+
CONTINUE: compileContinue,
|
|
930
|
+
};
|
|
931
|
+
|
|
932
|
+
class ForNode extends Traversal {
|
|
933
|
+
constructor(loop, body, loc, strip) {
|
|
414
934
|
super();
|
|
415
|
-
Object.defineProperty(this, "
|
|
935
|
+
Object.defineProperty(this, "loop", {
|
|
416
936
|
enumerable: true,
|
|
417
937
|
configurable: true,
|
|
418
938
|
writable: true,
|
|
419
|
-
value:
|
|
939
|
+
value: loop
|
|
940
|
+
});
|
|
941
|
+
Object.defineProperty(this, "body", {
|
|
942
|
+
enumerable: true,
|
|
943
|
+
configurable: true,
|
|
944
|
+
writable: true,
|
|
945
|
+
value: body
|
|
420
946
|
});
|
|
421
947
|
Object.defineProperty(this, "loc", {
|
|
422
948
|
enumerable: true,
|
|
@@ -434,11 +960,11 @@
|
|
|
434
960
|
enumerable: true,
|
|
435
961
|
configurable: true,
|
|
436
962
|
writable: true,
|
|
437
|
-
value:
|
|
963
|
+
value: 'FOR'
|
|
438
964
|
});
|
|
439
965
|
}
|
|
440
966
|
}
|
|
441
|
-
class
|
|
967
|
+
class BreakNode extends Traversal {
|
|
442
968
|
constructor(val, loc, strip) {
|
|
443
969
|
super();
|
|
444
970
|
Object.defineProperty(this, "val", {
|
|
@@ -463,12 +989,12 @@
|
|
|
463
989
|
enumerable: true,
|
|
464
990
|
configurable: true,
|
|
465
991
|
writable: true,
|
|
466
|
-
value:
|
|
992
|
+
value: 'BREAK'
|
|
467
993
|
});
|
|
468
994
|
}
|
|
469
995
|
}
|
|
470
|
-
class
|
|
471
|
-
constructor(val,
|
|
996
|
+
class ContinueNode extends Traversal {
|
|
997
|
+
constructor(val, loc, strip) {
|
|
472
998
|
super();
|
|
473
999
|
Object.defineProperty(this, "val", {
|
|
474
1000
|
enumerable: true,
|
|
@@ -476,12 +1002,6 @@
|
|
|
476
1002
|
writable: true,
|
|
477
1003
|
value: val
|
|
478
1004
|
});
|
|
479
|
-
Object.defineProperty(this, "body", {
|
|
480
|
-
enumerable: true,
|
|
481
|
-
configurable: true,
|
|
482
|
-
writable: true,
|
|
483
|
-
value: body
|
|
484
|
-
});
|
|
485
1005
|
Object.defineProperty(this, "loc", {
|
|
486
1006
|
enumerable: true,
|
|
487
1007
|
configurable: true,
|
|
@@ -498,18 +1018,104 @@
|
|
|
498
1018
|
enumerable: true,
|
|
499
1019
|
configurable: true,
|
|
500
1020
|
writable: true,
|
|
501
|
-
value:
|
|
1021
|
+
value: 'CONTINUE'
|
|
502
1022
|
});
|
|
503
1023
|
}
|
|
504
1024
|
}
|
|
505
|
-
|
|
506
|
-
|
|
1025
|
+
|
|
1026
|
+
const wm$1 = new WeakMap();
|
|
1027
|
+
async function* parseFor(token, parser) {
|
|
1028
|
+
if (!token.expression) {
|
|
1029
|
+
parser.emitExpErr(token);
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
wm$1.set(parser, true);
|
|
1033
|
+
yield 'NEXT';
|
|
1034
|
+
const body = await parser.parseUntil(['endfor']);
|
|
1035
|
+
wm$1.set(parser, false);
|
|
1036
|
+
if (parser.match(['endfor'])) {
|
|
1037
|
+
yield 'NEXT';
|
|
1038
|
+
}
|
|
1039
|
+
else {
|
|
1040
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1041
|
+
}
|
|
1042
|
+
yield new ForNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
1043
|
+
}
|
|
1044
|
+
async function* parseBreak(token, parser) {
|
|
1045
|
+
if (token.expression) {
|
|
1046
|
+
parser.emitExpErr(token, false);
|
|
1047
|
+
return;
|
|
1048
|
+
}
|
|
1049
|
+
if (!wm$1.get(parser)) {
|
|
1050
|
+
parser.options.debug?.(new CompileError('"break" directive used outside of a loop', parser.template, token.loc));
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
yield 'NEXT';
|
|
1054
|
+
yield new BreakNode(token.val, token.loc, token.strip);
|
|
1055
|
+
}
|
|
1056
|
+
async function* parseContinue(token, parser) {
|
|
1057
|
+
if (token.expression) {
|
|
1058
|
+
parser.emitExpErr(token, false);
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
if (!wm$1.get(parser)) {
|
|
1062
|
+
parser.options.debug?.(new CompileError('"continue" directive used outside of a loop', parser.template, token.loc));
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
yield 'NEXT';
|
|
1066
|
+
yield new ContinueNode(token.val, token.loc, token.strip);
|
|
1067
|
+
}
|
|
1068
|
+
const parsers$8 = {
|
|
1069
|
+
for: parseFor,
|
|
1070
|
+
break: parseBreak,
|
|
1071
|
+
continue: parseContinue,
|
|
1072
|
+
endfor: 'unexpected',
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
const plugin$8 = {
|
|
1076
|
+
compilers: compilers$8,
|
|
1077
|
+
parsers: parsers$8,
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
async function compileIf({ test, body, alternatives }, compiler) {
|
|
1081
|
+
compiler.pushRaw(test.loc, `if(${new ExpCompiler().compile(test, compiler.context)}){`);
|
|
1082
|
+
await compiler.compileNodes(body);
|
|
1083
|
+
compiler.pushRaw(null, '}');
|
|
1084
|
+
for (const branch of alternatives) {
|
|
1085
|
+
if (branch.type === 'ELSE_IF') {
|
|
1086
|
+
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1087
|
+
}
|
|
1088
|
+
else {
|
|
1089
|
+
compiler.pushRaw(null, 'else{');
|
|
1090
|
+
}
|
|
1091
|
+
await compiler.compileNodes(branch.body);
|
|
1092
|
+
compiler.pushRaw(null, '}');
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
const compilers$7 = {
|
|
1096
|
+
IF: compileIf,
|
|
1097
|
+
};
|
|
1098
|
+
|
|
1099
|
+
class IfNode extends Traversal {
|
|
1100
|
+
constructor(test, body, alternatives, loc, strip) {
|
|
507
1101
|
super();
|
|
508
|
-
Object.defineProperty(this, "
|
|
1102
|
+
Object.defineProperty(this, "test", {
|
|
509
1103
|
enumerable: true,
|
|
510
1104
|
configurable: true,
|
|
511
1105
|
writable: true,
|
|
512
|
-
value:
|
|
1106
|
+
value: test
|
|
1107
|
+
});
|
|
1108
|
+
Object.defineProperty(this, "body", {
|
|
1109
|
+
enumerable: true,
|
|
1110
|
+
configurable: true,
|
|
1111
|
+
writable: true,
|
|
1112
|
+
value: body
|
|
1113
|
+
});
|
|
1114
|
+
Object.defineProperty(this, "alternatives", {
|
|
1115
|
+
enumerable: true,
|
|
1116
|
+
configurable: true,
|
|
1117
|
+
writable: true,
|
|
1118
|
+
value: alternatives
|
|
513
1119
|
});
|
|
514
1120
|
Object.defineProperty(this, "loc", {
|
|
515
1121
|
enumerable: true,
|
|
@@ -527,18 +1133,22 @@
|
|
|
527
1133
|
enumerable: true,
|
|
528
1134
|
configurable: true,
|
|
529
1135
|
writable: true,
|
|
530
|
-
value:
|
|
1136
|
+
value: 'IF'
|
|
531
1137
|
});
|
|
532
1138
|
}
|
|
1139
|
+
traverse(cb) {
|
|
1140
|
+
this.body?.forEach(cb);
|
|
1141
|
+
this.alternatives?.forEach((node) => node.body?.forEach(cb));
|
|
1142
|
+
}
|
|
533
1143
|
}
|
|
534
|
-
class
|
|
535
|
-
constructor(
|
|
1144
|
+
class ElseIfNode extends Traversal {
|
|
1145
|
+
constructor(test, body, loc, strip) {
|
|
536
1146
|
super();
|
|
537
|
-
Object.defineProperty(this, "
|
|
1147
|
+
Object.defineProperty(this, "test", {
|
|
538
1148
|
enumerable: true,
|
|
539
1149
|
configurable: true,
|
|
540
1150
|
writable: true,
|
|
541
|
-
value:
|
|
1151
|
+
value: test
|
|
542
1152
|
});
|
|
543
1153
|
Object.defineProperty(this, "body", {
|
|
544
1154
|
enumerable: true,
|
|
@@ -562,18 +1172,18 @@
|
|
|
562
1172
|
enumerable: true,
|
|
563
1173
|
configurable: true,
|
|
564
1174
|
writable: true,
|
|
565
|
-
value:
|
|
1175
|
+
value: 'ELSE_IF'
|
|
566
1176
|
});
|
|
567
1177
|
}
|
|
568
1178
|
}
|
|
569
|
-
class
|
|
570
|
-
constructor(
|
|
1179
|
+
class ElseNode extends Traversal {
|
|
1180
|
+
constructor(body, loc, strip) {
|
|
571
1181
|
super();
|
|
572
|
-
Object.defineProperty(this, "
|
|
1182
|
+
Object.defineProperty(this, "body", {
|
|
573
1183
|
enumerable: true,
|
|
574
1184
|
configurable: true,
|
|
575
1185
|
writable: true,
|
|
576
|
-
value:
|
|
1186
|
+
value: body
|
|
577
1187
|
});
|
|
578
1188
|
Object.defineProperty(this, "loc", {
|
|
579
1189
|
enumerable: true,
|
|
@@ -591,12 +1201,106 @@
|
|
|
591
1201
|
enumerable: true,
|
|
592
1202
|
configurable: true,
|
|
593
1203
|
writable: true,
|
|
594
|
-
value:
|
|
1204
|
+
value: 'ELSE'
|
|
595
1205
|
});
|
|
596
1206
|
}
|
|
597
1207
|
}
|
|
598
|
-
|
|
599
|
-
|
|
1208
|
+
|
|
1209
|
+
async function* parseIf(token, parser) {
|
|
1210
|
+
if (!token.expression) {
|
|
1211
|
+
parser.emitExpErr(token);
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
yield 'NEXT';
|
|
1215
|
+
const body = await parser.parseUntil([
|
|
1216
|
+
'else',
|
|
1217
|
+
'elseif',
|
|
1218
|
+
'elsif',
|
|
1219
|
+
'elif',
|
|
1220
|
+
'endif',
|
|
1221
|
+
]);
|
|
1222
|
+
const alternatives = [];
|
|
1223
|
+
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
1224
|
+
const branchToken = parser.peek();
|
|
1225
|
+
if (branchToken.name.toLowerCase() === 'else') {
|
|
1226
|
+
const g = parseElse(branchToken, parser);
|
|
1227
|
+
while (true) {
|
|
1228
|
+
const { value, done } = await g.next();
|
|
1229
|
+
if (done) {
|
|
1230
|
+
break;
|
|
1231
|
+
}
|
|
1232
|
+
if (value) {
|
|
1233
|
+
if (value === 'NEXT') {
|
|
1234
|
+
yield 'NEXT';
|
|
1235
|
+
continue;
|
|
1236
|
+
}
|
|
1237
|
+
alternatives.push(value);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
else {
|
|
1242
|
+
const g = parseElseIf(branchToken, parser);
|
|
1243
|
+
while (true) {
|
|
1244
|
+
const { value, done } = await g.next();
|
|
1245
|
+
if (done) {
|
|
1246
|
+
break;
|
|
1247
|
+
}
|
|
1248
|
+
if (value) {
|
|
1249
|
+
if (value === 'NEXT') {
|
|
1250
|
+
yield 'NEXT';
|
|
1251
|
+
continue;
|
|
1252
|
+
}
|
|
1253
|
+
alternatives.push(value);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
if (parser.match(['endif'])) {
|
|
1259
|
+
yield 'NEXT';
|
|
1260
|
+
}
|
|
1261
|
+
else {
|
|
1262
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1263
|
+
}
|
|
1264
|
+
yield new IfNode(parser.parseExp(token.expression), body, alternatives, token.loc, token.strip);
|
|
1265
|
+
}
|
|
1266
|
+
async function* parseElseIf(token, parser) {
|
|
1267
|
+
if (!token.expression) {
|
|
1268
|
+
parser.emitExpErr(token);
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
yield 'NEXT';
|
|
1272
|
+
yield new ElseIfNode(parser.parseExp(token.expression), await parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']), token.loc, token.strip);
|
|
1273
|
+
}
|
|
1274
|
+
async function* parseElse(token, parser) {
|
|
1275
|
+
if (token.expression) {
|
|
1276
|
+
parser.emitExpErr(token, false);
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
yield 'NEXT';
|
|
1280
|
+
yield new ElseNode(await parser.parseUntil(['endif']), token.loc, token.strip);
|
|
1281
|
+
}
|
|
1282
|
+
const parsers$7 = {
|
|
1283
|
+
if: parseIf,
|
|
1284
|
+
elseif: 'unexpected',
|
|
1285
|
+
elsif: 'unexpected',
|
|
1286
|
+
elif: 'unexpected',
|
|
1287
|
+
endif: 'unexpected',
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
const plugin$7 = {
|
|
1291
|
+
compilers: compilers$7,
|
|
1292
|
+
parsers: parsers$7,
|
|
1293
|
+
};
|
|
1294
|
+
|
|
1295
|
+
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
1296
|
+
compiler.pushRaw(loc, `s+=await p["${value}"]?.()??"";`);
|
|
1297
|
+
}
|
|
1298
|
+
const compilers$6 = {
|
|
1299
|
+
INCLUDE: compileInclude,
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
class IncludeNode extends Traversal {
|
|
1303
|
+
constructor(val, loc, strip) {
|
|
600
1304
|
super();
|
|
601
1305
|
Object.defineProperty(this, "val", {
|
|
602
1306
|
enumerable: true,
|
|
@@ -604,12 +1308,6 @@
|
|
|
604
1308
|
writable: true,
|
|
605
1309
|
value: val
|
|
606
1310
|
});
|
|
607
|
-
Object.defineProperty(this, "body", {
|
|
608
|
-
enumerable: true,
|
|
609
|
-
configurable: true,
|
|
610
|
-
writable: true,
|
|
611
|
-
value: body
|
|
612
|
-
});
|
|
613
1311
|
Object.defineProperty(this, "loc", {
|
|
614
1312
|
enumerable: true,
|
|
615
1313
|
configurable: true,
|
|
@@ -626,12 +1324,53 @@
|
|
|
626
1324
|
enumerable: true,
|
|
627
1325
|
configurable: true,
|
|
628
1326
|
writable: true,
|
|
629
|
-
value:
|
|
1327
|
+
value: 'INCLUDE'
|
|
630
1328
|
});
|
|
631
1329
|
}
|
|
632
1330
|
}
|
|
633
|
-
|
|
634
|
-
|
|
1331
|
+
|
|
1332
|
+
async function* parseInclude(token, parser) {
|
|
1333
|
+
if (!token.expression) {
|
|
1334
|
+
parser.emitExpErr(token);
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
yield 'NEXT';
|
|
1338
|
+
yield new IncludeNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
1339
|
+
}
|
|
1340
|
+
const parsers$6 = {
|
|
1341
|
+
include: parseInclude,
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1344
|
+
const plugin$6 = {
|
|
1345
|
+
compilers: compilers$6,
|
|
1346
|
+
parsers: parsers$6,
|
|
1347
|
+
};
|
|
1348
|
+
|
|
1349
|
+
async function compileMacro({ val: { left, right }, loc, body }, compiler) {
|
|
1350
|
+
const { context } = compiler;
|
|
1351
|
+
const { elements } = right;
|
|
1352
|
+
const expCompiler = new ExpCompiler();
|
|
1353
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1354
|
+
.map((el) => el.type === 'ASSIGN'
|
|
1355
|
+
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1356
|
+
: el.value)
|
|
1357
|
+
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1358
|
+
.map((el) => el.type === 'ASSIGN' ? el.left.value : el.value)
|
|
1359
|
+
.join(','), '};');
|
|
1360
|
+
await compiler.compileNodes(body);
|
|
1361
|
+
compiler.out();
|
|
1362
|
+
compiler.pushRaw(null, '};');
|
|
1363
|
+
}
|
|
1364
|
+
function compileCaller({ loc }, compiler) {
|
|
1365
|
+
compiler.pushRaw(loc, 'await _c?.();');
|
|
1366
|
+
}
|
|
1367
|
+
const compilers$5 = {
|
|
1368
|
+
MACRO: compileMacro,
|
|
1369
|
+
CALLER: compileCaller,
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1372
|
+
class MacroNode extends Traversal {
|
|
1373
|
+
constructor(val, body, loc, strip) {
|
|
635
1374
|
super();
|
|
636
1375
|
Object.defineProperty(this, "val", {
|
|
637
1376
|
enumerable: true,
|
|
@@ -639,6 +1378,12 @@
|
|
|
639
1378
|
writable: true,
|
|
640
1379
|
value: val
|
|
641
1380
|
});
|
|
1381
|
+
Object.defineProperty(this, "body", {
|
|
1382
|
+
enumerable: true,
|
|
1383
|
+
configurable: true,
|
|
1384
|
+
writable: true,
|
|
1385
|
+
value: body
|
|
1386
|
+
});
|
|
642
1387
|
Object.defineProperty(this, "loc", {
|
|
643
1388
|
enumerable: true,
|
|
644
1389
|
configurable: true,
|
|
@@ -655,12 +1400,12 @@
|
|
|
655
1400
|
enumerable: true,
|
|
656
1401
|
configurable: true,
|
|
657
1402
|
writable: true,
|
|
658
|
-
value:
|
|
1403
|
+
value: 'MACRO'
|
|
659
1404
|
});
|
|
660
1405
|
}
|
|
661
1406
|
}
|
|
662
|
-
class
|
|
663
|
-
constructor(val,
|
|
1407
|
+
class CallerNode extends Traversal {
|
|
1408
|
+
constructor(val, loc, strip) {
|
|
664
1409
|
super();
|
|
665
1410
|
Object.defineProperty(this, "val", {
|
|
666
1411
|
enumerable: true,
|
|
@@ -668,12 +1413,6 @@
|
|
|
668
1413
|
writable: true,
|
|
669
1414
|
value: val
|
|
670
1415
|
});
|
|
671
|
-
Object.defineProperty(this, "body", {
|
|
672
|
-
enumerable: true,
|
|
673
|
-
configurable: true,
|
|
674
|
-
writable: true,
|
|
675
|
-
value: body
|
|
676
|
-
});
|
|
677
1416
|
Object.defineProperty(this, "loc", {
|
|
678
1417
|
enumerable: true,
|
|
679
1418
|
configurable: true,
|
|
@@ -690,729 +1429,160 @@
|
|
|
690
1429
|
enumerable: true,
|
|
691
1430
|
configurable: true,
|
|
692
1431
|
writable: true,
|
|
693
|
-
value:
|
|
1432
|
+
value: 'CALLER'
|
|
694
1433
|
});
|
|
695
1434
|
}
|
|
696
1435
|
}
|
|
697
1436
|
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
let caretLinesCount = 0;
|
|
711
|
-
lines.forEach((line, index) => {
|
|
712
|
-
if (!line) {
|
|
713
|
-
return;
|
|
714
|
-
}
|
|
715
|
-
output.push(`${`${index + 1}| `.padStart(indentWidth, ' ')}${line}`);
|
|
716
|
-
if (l1 === index + 1) {
|
|
717
|
-
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c1 - 1)}^${l1 === l2 && c2 > c1 + 1 ? `${' '.repeat(c2 - c1 - 2)}^` : ''}`;
|
|
718
|
-
if (/\S/.test(caretLine)) {
|
|
719
|
-
output.push(caretLine);
|
|
720
|
-
}
|
|
721
|
-
addCaretLine(++caretLinesCount + index);
|
|
722
|
-
}
|
|
723
|
-
if (l1 !== l2 && l2 === index + 1) {
|
|
724
|
-
const caretLine = `${' '.repeat(indentWidth - 2)}| ${' '.repeat(c2 - 1)}${'^'.repeat(1)}`;
|
|
725
|
-
if (/\S/.test(caretLine)) {
|
|
726
|
-
output.push(caretLine);
|
|
727
|
-
}
|
|
728
|
-
addCaretLine(++caretLinesCount + index);
|
|
729
|
-
}
|
|
730
|
-
});
|
|
731
|
-
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
732
|
-
return `${message}
|
|
733
|
-
|
|
734
|
-
${output
|
|
735
|
-
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
736
|
-
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
737
|
-
.join('\n')}
|
|
738
|
-
`;
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
class CompileError extends SyntaxError {
|
|
742
|
-
constructor(message, src, loc) {
|
|
743
|
-
super(message);
|
|
744
|
-
Object.defineProperty(this, "src", {
|
|
745
|
-
enumerable: true,
|
|
746
|
-
configurable: true,
|
|
747
|
-
writable: true,
|
|
748
|
-
value: src
|
|
749
|
-
});
|
|
750
|
-
Object.defineProperty(this, "loc", {
|
|
751
|
-
enumerable: true,
|
|
752
|
-
configurable: true,
|
|
753
|
-
writable: true,
|
|
754
|
-
value: loc
|
|
755
|
-
});
|
|
756
|
-
this.name = 'CompileError';
|
|
757
|
-
CompileError.captureStackTrace?.(this, this.constructor);
|
|
758
|
-
}
|
|
759
|
-
get details() {
|
|
760
|
-
return this.loc
|
|
761
|
-
? highlightSource(this.message, this.src, this.loc)
|
|
762
|
-
: this.message;
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
const parseUnexpected = (token, parser) => parser.createUnexpectedDirective(token);
|
|
767
|
-
|
|
768
|
-
function parseBlock(token, parser) {
|
|
769
|
-
parser.requireExpression(token);
|
|
770
|
-
const val = parser.parseExp(token.expression);
|
|
771
|
-
parser.advance();
|
|
772
|
-
const body = parser.parseUntil(['endblock']);
|
|
773
|
-
if (parser.match(['endblock'])) {
|
|
774
|
-
parser.advance();
|
|
775
|
-
}
|
|
776
|
-
else {
|
|
777
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
778
|
-
}
|
|
779
|
-
return new BlockNode(val, body, token.loc, token.strip);
|
|
780
|
-
}
|
|
781
|
-
function parseSuper(token, parser) {
|
|
782
|
-
parser.requireNoExpression(token);
|
|
783
|
-
parser.advance();
|
|
784
|
-
return new SuperNode(token.val, token.loc, token.strip);
|
|
785
|
-
}
|
|
786
|
-
const parsers$8 = {
|
|
787
|
-
block: parseBlock,
|
|
788
|
-
super: parseSuper,
|
|
789
|
-
endblock: parseUnexpected,
|
|
790
|
-
};
|
|
791
|
-
|
|
792
|
-
function parseCall(token, parser) {
|
|
793
|
-
parser.requireExpression(token);
|
|
794
|
-
const val = parser.parseExp(token.expression);
|
|
795
|
-
parser.advance();
|
|
796
|
-
const body = parser.parseUntil(['endcall']);
|
|
797
|
-
if (parser.match(['endcall'])) {
|
|
798
|
-
parser.advance();
|
|
799
|
-
}
|
|
800
|
-
else {
|
|
801
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
802
|
-
}
|
|
803
|
-
return new CallNode(val, body, token.loc, token.strip);
|
|
804
|
-
}
|
|
805
|
-
const parsers$7 = {
|
|
806
|
-
call: parseCall,
|
|
807
|
-
endcall: parseUnexpected,
|
|
808
|
-
};
|
|
809
|
-
|
|
810
|
-
function parseCapture(token, parser) {
|
|
811
|
-
parser.requireExpression(token);
|
|
812
|
-
const val = parser.parseExp(token.expression);
|
|
813
|
-
parser.advance();
|
|
814
|
-
const body = parser.parseUntil(['endcapture']);
|
|
815
|
-
if (parser.match(['endcapture'])) {
|
|
816
|
-
parser.advance();
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
820
|
-
}
|
|
821
|
-
return new CaptureNode(val, body, token.loc, token.strip);
|
|
822
|
-
}
|
|
823
|
-
const parsers$6 = {
|
|
824
|
-
capture: parseCapture,
|
|
825
|
-
endcapture: parseUnexpected,
|
|
826
|
-
};
|
|
827
|
-
|
|
828
|
-
function parseFor(token, parser) {
|
|
829
|
-
parser.requireExpression(token);
|
|
830
|
-
const loop = parser.parseExp(token.expression);
|
|
831
|
-
parser.advance();
|
|
832
|
-
const body = parser.parseUntil(['endfor']);
|
|
833
|
-
if (parser.match(['endfor'])) {
|
|
834
|
-
parser.advance();
|
|
835
|
-
}
|
|
836
|
-
else {
|
|
837
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
838
|
-
}
|
|
839
|
-
return new ForNode(loop, body, token.loc, token.strip);
|
|
840
|
-
}
|
|
841
|
-
function parseBreak(token, parser) {
|
|
842
|
-
parser.requireNoExpression(token);
|
|
843
|
-
parser.advance();
|
|
844
|
-
return new BreakNode(token.val, token.loc, token.strip);
|
|
845
|
-
}
|
|
846
|
-
function parseContinue(token, parser) {
|
|
847
|
-
parser.requireNoExpression(token);
|
|
848
|
-
parser.advance();
|
|
849
|
-
return new ContinueNode(token.val, token.loc, token.strip);
|
|
850
|
-
}
|
|
851
|
-
const parsers$5 = {
|
|
852
|
-
for: parseFor,
|
|
853
|
-
break: parseBreak,
|
|
854
|
-
continue: parseContinue,
|
|
855
|
-
endfor: parseUnexpected,
|
|
856
|
-
};
|
|
857
|
-
|
|
858
|
-
function parseIf(token, parser) {
|
|
859
|
-
parser.requireExpression(token);
|
|
860
|
-
parser.advance();
|
|
861
|
-
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
862
|
-
const alternatives = [];
|
|
863
|
-
while (parser.match(['else', 'elseif', 'elsif', 'elif'])) {
|
|
864
|
-
const branchToken = parser.peek();
|
|
865
|
-
alternatives.push(branchToken.name.toLowerCase() === 'else'
|
|
866
|
-
? parseElse(branchToken, parser)
|
|
867
|
-
: parseElseIf(branchToken, parser));
|
|
868
|
-
}
|
|
869
|
-
if (parser.match(['endif'])) {
|
|
870
|
-
parser.advance();
|
|
871
|
-
}
|
|
872
|
-
else {
|
|
873
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
874
|
-
}
|
|
875
|
-
return new IfNode(parser.parseExp(token.expression), body, alternatives, token.loc, token.strip);
|
|
876
|
-
}
|
|
877
|
-
function parseElseIf(token, parser) {
|
|
878
|
-
parser.requireExpression(token);
|
|
879
|
-
parser.advance();
|
|
880
|
-
const body = parser.parseUntil(['else', 'elseif', 'elsif', 'elif', 'endif']);
|
|
881
|
-
return new ElseIfNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
882
|
-
}
|
|
883
|
-
function parseElse(token, parser) {
|
|
884
|
-
parser.requireNoExpression(token);
|
|
885
|
-
parser.advance();
|
|
886
|
-
const body = parser.parseUntil(['endif']);
|
|
887
|
-
return new ElseNode(body, token.loc, token.strip);
|
|
888
|
-
}
|
|
889
|
-
const parsers$4 = {
|
|
890
|
-
if: parseIf,
|
|
891
|
-
elseif: parseUnexpected,
|
|
892
|
-
elsif: parseUnexpected,
|
|
893
|
-
elif: parseUnexpected,
|
|
894
|
-
endif: parseUnexpected,
|
|
895
|
-
};
|
|
896
|
-
|
|
897
|
-
function parseInclude(token, parser) {
|
|
898
|
-
parser.requireExpression(token);
|
|
899
|
-
const val = parser.parseExp(token.expression);
|
|
900
|
-
parser.advance();
|
|
901
|
-
return new IncludeNode(val, token.loc, token.strip);
|
|
902
|
-
}
|
|
903
|
-
const parsers$3 = {
|
|
904
|
-
include: parseInclude,
|
|
905
|
-
};
|
|
906
|
-
|
|
907
|
-
function parseMacro(token, parser) {
|
|
908
|
-
parser.requireExpression(token);
|
|
909
|
-
const name = parser.parseExp(token.expression);
|
|
910
|
-
parser.advance();
|
|
911
|
-
const body = parser.parseUntil(['endmacro']);
|
|
912
|
-
if (parser.match(['endmacro'])) {
|
|
913
|
-
parser.advance();
|
|
914
|
-
}
|
|
915
|
-
else {
|
|
916
|
-
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
917
|
-
}
|
|
918
|
-
return new MacroNode(name, body, token.loc, token.strip);
|
|
919
|
-
}
|
|
920
|
-
function parseCaller(token, parser) {
|
|
921
|
-
parser.requireNoExpression(token);
|
|
922
|
-
parser.advance();
|
|
923
|
-
return new CallerNode(token.val, token.loc, token.strip);
|
|
924
|
-
}
|
|
925
|
-
const parsers$2 = {
|
|
926
|
-
macro: parseMacro,
|
|
927
|
-
caller: parseCaller,
|
|
928
|
-
endmacro: parseUnexpected,
|
|
929
|
-
};
|
|
930
|
-
|
|
931
|
-
function parseSet(token, parser) {
|
|
932
|
-
parser.requireExpression(token);
|
|
933
|
-
const val = parser.parseExp(token.expression);
|
|
934
|
-
parser.advance();
|
|
935
|
-
return new SetNode(val, token.loc, token.strip);
|
|
936
|
-
}
|
|
937
|
-
const parsers$1 = {
|
|
938
|
-
set: parseSet,
|
|
939
|
-
};
|
|
940
|
-
|
|
941
|
-
const parsers = {
|
|
942
|
-
...parsers$4,
|
|
943
|
-
...parsers$5,
|
|
944
|
-
...parsers$3,
|
|
945
|
-
...parsers$8,
|
|
946
|
-
...parsers$2,
|
|
947
|
-
...parsers$7,
|
|
948
|
-
...parsers$1,
|
|
949
|
-
...parsers$6,
|
|
950
|
-
};
|
|
951
|
-
|
|
952
|
-
class Safe extends String {
|
|
953
|
-
constructor(value) {
|
|
954
|
-
super(`${value}`);
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
function escape(v) {
|
|
959
|
-
return `${v}`
|
|
960
|
-
.replace(/&/g, '&')
|
|
961
|
-
.replace(/"/g, '"')
|
|
962
|
-
.replace(/'/g, ''')
|
|
963
|
-
.replace(/</g, '<')
|
|
964
|
-
.replace(/>/g, '>');
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
function abs(value = 0) {
|
|
968
|
-
return Math.abs(value);
|
|
969
|
-
}
|
|
970
|
-
function add(value = 0, addend = 0) {
|
|
971
|
-
return value + addend;
|
|
972
|
-
}
|
|
973
|
-
function capitalize(value = '') {
|
|
974
|
-
return value.replace(/\b\w/g, (match) => match.toUpperCase());
|
|
975
|
-
}
|
|
976
|
-
function ceil(value = 0) {
|
|
977
|
-
return Math.ceil(value);
|
|
978
|
-
}
|
|
979
|
-
function compact(value = []) {
|
|
980
|
-
return value.filter((v) => v != null);
|
|
981
|
-
}
|
|
982
|
-
function div(value = 0, divisor = 1) {
|
|
983
|
-
return value / divisor;
|
|
984
|
-
}
|
|
985
|
-
function entries(value = {}) {
|
|
986
|
-
return Object.entries(value);
|
|
987
|
-
}
|
|
988
|
-
function even(value = 0) {
|
|
989
|
-
return +value % 2 === 0;
|
|
990
|
-
}
|
|
991
|
-
function fallback(value, defaultValue, anyFalsy = false) {
|
|
992
|
-
return anyFalsy ? value || defaultValue : (value ?? defaultValue);
|
|
993
|
-
}
|
|
994
|
-
function first(value = []) {
|
|
995
|
-
return [...value][0];
|
|
996
|
-
}
|
|
997
|
-
function get(value, ...path) {
|
|
998
|
-
return path.reduce((acc, cur) => acc && typeof acc === 'object' && cur in acc ? acc[cur] : undefined, value);
|
|
999
|
-
}
|
|
1000
|
-
function groupby(value = [], key) {
|
|
1001
|
-
return value.reduce((o, v) => {
|
|
1002
|
-
const k = v[key];
|
|
1003
|
-
return {
|
|
1004
|
-
...o,
|
|
1005
|
-
[k]: [...(o[k] || []), v],
|
|
1006
|
-
};
|
|
1007
|
-
}, {});
|
|
1008
|
-
}
|
|
1009
|
-
function join(value = [], separator = '') {
|
|
1010
|
-
return [...value].join(separator);
|
|
1011
|
-
}
|
|
1012
|
-
function json(value = null, indent = 0) {
|
|
1013
|
-
return new Safe(JSON.stringify(value, null, indent));
|
|
1014
|
-
}
|
|
1015
|
-
function keys(value = {}) {
|
|
1016
|
-
return Object.keys(value);
|
|
1017
|
-
}
|
|
1018
|
-
function last(value = []) {
|
|
1019
|
-
return [...value].reverse()[0];
|
|
1020
|
-
}
|
|
1021
|
-
function length(value = '') {
|
|
1022
|
-
return value.length;
|
|
1023
|
-
}
|
|
1024
|
-
function lower(value = '') {
|
|
1025
|
-
return value.toLowerCase();
|
|
1026
|
-
}
|
|
1027
|
-
function map(value = [], key, defaultValue) {
|
|
1028
|
-
return value.map((o) => o[key] ?? defaultValue);
|
|
1029
|
-
}
|
|
1030
|
-
function max(value = 0, ...rest) {
|
|
1031
|
-
return Math.max(value, ...rest);
|
|
1032
|
-
}
|
|
1033
|
-
function min(value = 0, ...rest) {
|
|
1034
|
-
return Math.min(value, ...rest);
|
|
1035
|
-
}
|
|
1036
|
-
function mul(value = 0, multiplier = 1) {
|
|
1037
|
-
return value * multiplier;
|
|
1038
|
-
}
|
|
1039
|
-
function odd(value = 0) {
|
|
1040
|
-
return +value % 2 === 1;
|
|
1041
|
-
}
|
|
1042
|
-
function omit(value = {}, ...omitKeys) {
|
|
1043
|
-
return Object.entries(value).reduce((o, [k, v]) => (omitKeys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
1044
|
-
}
|
|
1045
|
-
function pick(value = {}, ...pickKeys) {
|
|
1046
|
-
return pickKeys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
1047
|
-
}
|
|
1048
|
-
function repeat(value = '', count = 0) {
|
|
1049
|
-
return value.repeat(count);
|
|
1050
|
-
}
|
|
1051
|
-
function replace(value = '', search, replacement) {
|
|
1052
|
-
return value.replace(new RegExp(search, 'g'), replacement);
|
|
1053
|
-
}
|
|
1054
|
-
function reverse(value = []) {
|
|
1055
|
-
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
1056
|
-
}
|
|
1057
|
-
function safe(value = '') {
|
|
1058
|
-
return new Safe(value);
|
|
1059
|
-
}
|
|
1060
|
-
function slice(value = '', start = 0, end) {
|
|
1061
|
-
return value.slice(start, end);
|
|
1062
|
-
}
|
|
1063
|
-
function sort(value = []) {
|
|
1064
|
-
return Array.isArray(value) ? value.sort() : [...value].sort().join('');
|
|
1065
|
-
}
|
|
1066
|
-
function split(value = '', separator = '') {
|
|
1067
|
-
return value.split(separator);
|
|
1068
|
-
}
|
|
1069
|
-
function sub(value = 0, subtrahend = 0) {
|
|
1070
|
-
return value - subtrahend;
|
|
1071
|
-
}
|
|
1072
|
-
function sum(value = []) {
|
|
1073
|
-
return value.reduce((a, b) => a + b, 0);
|
|
1074
|
-
}
|
|
1075
|
-
function trim(value = '') {
|
|
1076
|
-
return value.trim();
|
|
1077
|
-
}
|
|
1078
|
-
function truncate(value = '', maxLength = 0, truncateStr = '...') {
|
|
1079
|
-
return (value.slice(0, maxLength) + (value.length > maxLength ? truncateStr : ''));
|
|
1080
|
-
}
|
|
1081
|
-
function unique(value = '') {
|
|
1082
|
-
return Array.isArray(value)
|
|
1083
|
-
? Array.from(new Set(value))
|
|
1084
|
-
: Array.from(new Set(value)).join('');
|
|
1085
|
-
}
|
|
1086
|
-
function upper(value = '') {
|
|
1087
|
-
return value.toUpperCase();
|
|
1088
|
-
}
|
|
1089
|
-
function urldecode(value = '') {
|
|
1090
|
-
return decodeURIComponent(value);
|
|
1091
|
-
}
|
|
1092
|
-
function urlencode(value = '') {
|
|
1093
|
-
return encodeURIComponent(value);
|
|
1094
|
-
}
|
|
1095
|
-
function values(value = {}) {
|
|
1096
|
-
return Object.values(value);
|
|
1097
|
-
}
|
|
1098
|
-
|
|
1099
|
-
var _filters = /*#__PURE__*/Object.freeze({
|
|
1100
|
-
__proto__: null,
|
|
1101
|
-
abs: abs,
|
|
1102
|
-
add: add,
|
|
1103
|
-
capitalize: capitalize,
|
|
1104
|
-
ceil: ceil,
|
|
1105
|
-
compact: compact,
|
|
1106
|
-
div: div,
|
|
1107
|
-
entries: entries,
|
|
1108
|
-
escape: escape,
|
|
1109
|
-
even: even,
|
|
1110
|
-
fallback: fallback,
|
|
1111
|
-
first: first,
|
|
1112
|
-
get: get,
|
|
1113
|
-
groupby: groupby,
|
|
1114
|
-
join: join,
|
|
1115
|
-
json: json,
|
|
1116
|
-
keys: keys,
|
|
1117
|
-
last: last,
|
|
1118
|
-
length: length,
|
|
1119
|
-
lower: lower,
|
|
1120
|
-
map: map,
|
|
1121
|
-
max: max,
|
|
1122
|
-
min: min,
|
|
1123
|
-
mul: mul,
|
|
1124
|
-
odd: odd,
|
|
1125
|
-
omit: omit,
|
|
1126
|
-
pick: pick,
|
|
1127
|
-
repeat: repeat,
|
|
1128
|
-
replace: replace,
|
|
1129
|
-
reverse: reverse,
|
|
1130
|
-
safe: safe,
|
|
1131
|
-
slice: slice,
|
|
1132
|
-
sort: sort,
|
|
1133
|
-
split: split,
|
|
1134
|
-
sub: sub,
|
|
1135
|
-
sum: sum,
|
|
1136
|
-
trim: trim,
|
|
1137
|
-
truncate: truncate,
|
|
1138
|
-
unique: unique,
|
|
1139
|
-
upper: upper,
|
|
1140
|
-
urldecode: urldecode,
|
|
1141
|
-
urlencode: urlencode,
|
|
1142
|
-
values: values
|
|
1143
|
-
});
|
|
1144
|
-
|
|
1145
|
-
async function compileBlock({ val: { value }, loc }, compiler) {
|
|
1146
|
-
compiler.pushRaw(loc, `if(b["${value}"]&&!(b["${value}"].u++)){`, `s+=await b["${value}"].s?.pop()?.()??"";`, '}');
|
|
1147
|
-
}
|
|
1148
|
-
async function compileSuper({ loc }, compiler) {
|
|
1149
|
-
if (!compiler.state.block) {
|
|
1150
|
-
compiler.options.debug?.(new CompileError('"super" should be used inside a block', compiler.template, loc));
|
|
1151
|
-
return;
|
|
1152
|
-
}
|
|
1153
|
-
compiler.pushRaw(loc, `s+=await b["${compiler.state.block}"]?.s?.pop()?.()??"";`);
|
|
1154
|
-
}
|
|
1155
|
-
const compilers$b = {
|
|
1156
|
-
[NodeType.BLOCK]: compileBlock,
|
|
1157
|
-
[NodeType.SUPER]: compileSuper,
|
|
1158
|
-
};
|
|
1159
|
-
|
|
1160
|
-
const expTokenOperators = {
|
|
1161
|
-
AND: '&&',
|
|
1162
|
-
OR: '||',
|
|
1163
|
-
IS: '',
|
|
1164
|
-
NOT: '!',
|
|
1165
|
-
EQ: '===',
|
|
1166
|
-
NE: '!==',
|
|
1167
|
-
GT: '>',
|
|
1168
|
-
LT: '<',
|
|
1169
|
-
GE: '>=',
|
|
1170
|
-
LE: '<=',
|
|
1171
|
-
IN: ' in ',
|
|
1172
|
-
NI: ' in ',
|
|
1173
|
-
OF: ' of ',
|
|
1174
|
-
ADD: '+',
|
|
1175
|
-
SUB: '-',
|
|
1176
|
-
MUL: '*',
|
|
1177
|
-
DIV: '/',
|
|
1178
|
-
MOD: '%',
|
|
1179
|
-
PIPE: '',
|
|
1180
|
-
SET: '=',
|
|
1181
|
-
LP: '(',
|
|
1182
|
-
RP: ')',
|
|
1183
|
-
COMMA: ',',
|
|
1184
|
-
DOT: '.',
|
|
1185
|
-
ID: '',
|
|
1186
|
-
LIT: '',
|
|
1187
|
-
IF: '',
|
|
1188
|
-
ELSE: '',
|
|
1189
|
-
};
|
|
1190
|
-
|
|
1191
|
-
const CONTEXT = 'c';
|
|
1192
|
-
const ESCAPE = 'e';
|
|
1193
|
-
const FILTERS = 'f';
|
|
1194
|
-
|
|
1195
|
-
class ExpCompiler {
|
|
1196
|
-
constructor() {
|
|
1197
|
-
Object.defineProperty(this, "context", {
|
|
1198
|
-
enumerable: true,
|
|
1199
|
-
configurable: true,
|
|
1200
|
-
writable: true,
|
|
1201
|
-
value: void 0
|
|
1202
|
-
});
|
|
1203
|
-
Object.defineProperty(this, "filters", {
|
|
1204
|
-
enumerable: true,
|
|
1205
|
-
configurable: true,
|
|
1206
|
-
writable: true,
|
|
1207
|
-
value: void 0
|
|
1208
|
-
});
|
|
1209
|
-
}
|
|
1210
|
-
compile(expression, context = CONTEXT, filters = FILTERS) {
|
|
1211
|
-
this.context = context;
|
|
1212
|
-
this.filters = filters;
|
|
1213
|
-
return expression ? this.compileExp(expression) : '""';
|
|
1214
|
-
}
|
|
1215
|
-
compileExp(expression) {
|
|
1216
|
-
switch (expression.type) {
|
|
1217
|
-
case 'NOT':
|
|
1218
|
-
return this.compileNot(expression);
|
|
1219
|
-
case 'ID':
|
|
1220
|
-
return this.compileId(expression);
|
|
1221
|
-
case 'LIT':
|
|
1222
|
-
return this.compileLit(expression);
|
|
1223
|
-
case 'PIPE':
|
|
1224
|
-
return this.compilePipe(expression);
|
|
1225
|
-
case 'OF':
|
|
1226
|
-
return this.compileOf(expression);
|
|
1227
|
-
case 'SET':
|
|
1228
|
-
return this.compileSet(expression);
|
|
1229
|
-
case 'IS':
|
|
1230
|
-
return this.compileIs(expression);
|
|
1231
|
-
case 'AND':
|
|
1232
|
-
case 'OR':
|
|
1233
|
-
case 'EQ':
|
|
1234
|
-
case 'NE':
|
|
1235
|
-
case 'GT':
|
|
1236
|
-
case 'LT':
|
|
1237
|
-
case 'GE':
|
|
1238
|
-
case 'LE':
|
|
1239
|
-
case 'IN':
|
|
1240
|
-
case 'NI':
|
|
1241
|
-
case 'ADD':
|
|
1242
|
-
case 'SUB':
|
|
1243
|
-
case 'MUL':
|
|
1244
|
-
case 'DIV':
|
|
1245
|
-
case 'MOD':
|
|
1246
|
-
return this.compileBinary(expression);
|
|
1247
|
-
case 'IF':
|
|
1248
|
-
return this.compileTernary(expression);
|
|
1249
|
-
case 'SEQ':
|
|
1250
|
-
return this.compileSeq(expression);
|
|
1251
|
-
default:
|
|
1252
|
-
return '';
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
compileNot({ argument }) {
|
|
1256
|
-
return `!${this.compileExp(argument)}`;
|
|
1257
|
-
}
|
|
1258
|
-
compileId({ value, path, args }) {
|
|
1259
|
-
let s = `${this.context}.${value}`;
|
|
1260
|
-
if (path) {
|
|
1261
|
-
s += path.map((p) => `.${p.value}`).join('');
|
|
1262
|
-
}
|
|
1263
|
-
if (args) {
|
|
1264
|
-
s += `(${args.map((arg) => this.compileExp(arg)).join(',')})`;
|
|
1265
|
-
}
|
|
1266
|
-
return s;
|
|
1267
|
-
}
|
|
1268
|
-
compileLit({ value }) {
|
|
1269
|
-
return value === undefined ? 'undefined' : JSON.stringify(value);
|
|
1270
|
-
}
|
|
1271
|
-
compileSeq({ elements }) {
|
|
1272
|
-
return elements.map((element) => this.compileExp(element)).toString();
|
|
1273
|
-
}
|
|
1274
|
-
compilePipe({ left, right }) {
|
|
1275
|
-
const { value: name, args = [] } = right;
|
|
1276
|
-
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(',')}))`;
|
|
1277
|
-
}
|
|
1278
|
-
compileOf({ left, right }) {
|
|
1279
|
-
// destructuring
|
|
1280
|
-
if (left.type === 'SEQ') {
|
|
1281
|
-
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
1282
|
-
}
|
|
1283
|
-
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
1284
|
-
}
|
|
1285
|
-
compileSet({ left, right }) {
|
|
1286
|
-
if (left.type === 'ID') {
|
|
1287
|
-
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
1288
|
-
}
|
|
1289
|
-
// destructuring
|
|
1290
|
-
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
1291
|
-
}
|
|
1292
|
-
compileIs({ left, right }) {
|
|
1293
|
-
return `(typeof ${this.compileExp(left)}===${this.compileExp(right)})`;
|
|
1294
|
-
}
|
|
1295
|
-
compileBinary({ type, left, right }) {
|
|
1296
|
-
const ret = `${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)}`;
|
|
1297
|
-
return type === 'NI' ? `!${ret}` : ret;
|
|
1298
|
-
}
|
|
1299
|
-
compileTernary({ test, consequent, alternative }) {
|
|
1300
|
-
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
async function compileCall({ val, body, loc }, compiler) {
|
|
1305
|
-
compiler.pushRaw(loc, `await ${new ExpCompiler().compile(val, compiler.context)}(async()=>{`);
|
|
1306
|
-
await compiler.compileNodes(body);
|
|
1307
|
-
compiler.pushRaw(null, '});');
|
|
1308
|
-
}
|
|
1309
|
-
const compilers$a = {
|
|
1310
|
-
[NodeType.CALL]: compileCall,
|
|
1311
|
-
};
|
|
1312
|
-
|
|
1313
|
-
async function compileCapture({ val, body, loc }, compiler) {
|
|
1314
|
-
compiler.pushRaw(loc, `${compiler.context}.${val.value}=await(async(s)=>{`);
|
|
1315
|
-
await compiler.compileNodes(body);
|
|
1316
|
-
compiler.pushRaw(null, 'return s;})("");');
|
|
1317
|
-
}
|
|
1318
|
-
const compilers$9 = {
|
|
1319
|
-
[NodeType.CAPTURE]: compileCapture,
|
|
1320
|
-
};
|
|
1321
|
-
|
|
1322
|
-
async function compileComment({ val, loc }, compiler) {
|
|
1323
|
-
if (!compiler.options.stripComments) {
|
|
1324
|
-
compiler.pushStr(loc, `<!--${val}-->`);
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
const compilers$8 = {
|
|
1328
|
-
[NodeType.COMMENT]: compileComment,
|
|
1329
|
-
};
|
|
1330
|
-
|
|
1331
|
-
async function compileFor({ loop, body }, compiler) {
|
|
1332
|
-
const { context } = compiler;
|
|
1333
|
-
if (loop.left.type === 'SEQ') {
|
|
1334
|
-
compiler.pushRaw(loop.loc, `for(${new ExpCompiler().compile(loop, context)}){`, `const ${compiler.in()}={`, `...${context},`, `${loop.left.elements.map(({ value }) => value).join(',')},`, '};');
|
|
1437
|
+
const wm = new WeakMap();
|
|
1438
|
+
async function* parseMacro(token, parser) {
|
|
1439
|
+
if (!token.expression) {
|
|
1440
|
+
parser.emitExpErr(token);
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1443
|
+
wm.set(parser, true);
|
|
1444
|
+
yield 'NEXT';
|
|
1445
|
+
const body = await parser.parseUntil(['endmacro']);
|
|
1446
|
+
wm.set(parser, false);
|
|
1447
|
+
if (parser.match(['endmacro'])) {
|
|
1448
|
+
yield 'NEXT';
|
|
1335
1449
|
}
|
|
1336
1450
|
else {
|
|
1337
|
-
|
|
1451
|
+
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1338
1452
|
}
|
|
1339
|
-
|
|
1340
|
-
compiler.out();
|
|
1341
|
-
compiler.pushRaw(null, '}');
|
|
1453
|
+
yield new MacroNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
1342
1454
|
}
|
|
1343
|
-
function
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1455
|
+
async function* parseCaller(token, parser) {
|
|
1456
|
+
if (token.expression) {
|
|
1457
|
+
parser.emitExpErr(token, false);
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
if (!wm.get(parser)) {
|
|
1461
|
+
parser.options.debug?.(new CompileError('"caller" directive used outside of a macro', parser.template, token.loc));
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
yield 'NEXT';
|
|
1465
|
+
yield new CallerNode(token.val, token.loc, token.strip);
|
|
1348
1466
|
}
|
|
1349
|
-
const
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1467
|
+
const parsers$5 = {
|
|
1468
|
+
macro: parseMacro,
|
|
1469
|
+
caller: parseCaller,
|
|
1470
|
+
endmacro: 'unexpected',
|
|
1353
1471
|
};
|
|
1354
1472
|
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
compiler.pushRaw(null, '}');
|
|
1359
|
-
for (const branch of alternatives) {
|
|
1360
|
-
if (branch.type === NodeType.ELSE_IF) {
|
|
1361
|
-
compiler.pushRaw(branch.test.loc, `else if(${new ExpCompiler().compile(branch.test, compiler.context)}){`);
|
|
1362
|
-
}
|
|
1363
|
-
else {
|
|
1364
|
-
compiler.pushRaw(null, 'else{');
|
|
1365
|
-
}
|
|
1366
|
-
await compiler.compileNodes(branch.body);
|
|
1367
|
-
compiler.pushRaw(null, '}');
|
|
1368
|
-
}
|
|
1369
|
-
}
|
|
1370
|
-
const compilers$6 = {
|
|
1371
|
-
[NodeType.IF]: compileIf,
|
|
1473
|
+
const plugin$5 = {
|
|
1474
|
+
compilers: compilers$5,
|
|
1475
|
+
parsers: parsers$5,
|
|
1372
1476
|
};
|
|
1373
1477
|
|
|
1374
|
-
async function
|
|
1375
|
-
compiler.
|
|
1478
|
+
async function compileOutput({ exp }, compiler) {
|
|
1479
|
+
compiler.pushVar(exp.loc, new ExpCompiler().compile(exp, compiler.context));
|
|
1376
1480
|
}
|
|
1377
|
-
const compilers$
|
|
1378
|
-
|
|
1481
|
+
const compilers$4 = {
|
|
1482
|
+
OUTPUT: compileOutput,
|
|
1379
1483
|
};
|
|
1380
1484
|
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
.
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1485
|
+
class OutputNode extends Traversal {
|
|
1486
|
+
constructor(val, loc, strip, exp) {
|
|
1487
|
+
super();
|
|
1488
|
+
Object.defineProperty(this, "val", {
|
|
1489
|
+
enumerable: true,
|
|
1490
|
+
configurable: true,
|
|
1491
|
+
writable: true,
|
|
1492
|
+
value: val
|
|
1493
|
+
});
|
|
1494
|
+
Object.defineProperty(this, "loc", {
|
|
1495
|
+
enumerable: true,
|
|
1496
|
+
configurable: true,
|
|
1497
|
+
writable: true,
|
|
1498
|
+
value: loc
|
|
1499
|
+
});
|
|
1500
|
+
Object.defineProperty(this, "strip", {
|
|
1501
|
+
enumerable: true,
|
|
1502
|
+
configurable: true,
|
|
1503
|
+
writable: true,
|
|
1504
|
+
value: strip
|
|
1505
|
+
});
|
|
1506
|
+
Object.defineProperty(this, "exp", {
|
|
1507
|
+
enumerable: true,
|
|
1508
|
+
configurable: true,
|
|
1509
|
+
writable: true,
|
|
1510
|
+
value: exp
|
|
1511
|
+
});
|
|
1512
|
+
Object.defineProperty(this, "type", {
|
|
1513
|
+
enumerable: true,
|
|
1514
|
+
configurable: true,
|
|
1515
|
+
writable: true,
|
|
1516
|
+
value: 'OUTPUT'
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1395
1519
|
}
|
|
1396
|
-
|
|
1397
|
-
|
|
1520
|
+
|
|
1521
|
+
async function* parseOutput(token, parser) {
|
|
1522
|
+
yield 'NEXT';
|
|
1523
|
+
yield new OutputNode(token.val, token.loc, token.strip, parser.parseExp(token));
|
|
1398
1524
|
}
|
|
1399
|
-
const
|
|
1400
|
-
|
|
1401
|
-
[NodeType.CALLER]: compileCaller,
|
|
1525
|
+
const parsers$4 = {
|
|
1526
|
+
output: parseOutput,
|
|
1402
1527
|
};
|
|
1403
1528
|
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
const compilers$3 = {
|
|
1408
|
-
[NodeType.OUTPUT]: compileOutput,
|
|
1529
|
+
const plugin$4 = {
|
|
1530
|
+
compilers: compilers$4,
|
|
1531
|
+
parsers: parsers$4,
|
|
1409
1532
|
};
|
|
1410
1533
|
|
|
1411
1534
|
async function compileSet({ val, loc }, compiler) {
|
|
1412
1535
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
1413
1536
|
}
|
|
1414
|
-
const compilers$
|
|
1415
|
-
|
|
1537
|
+
const compilers$3 = {
|
|
1538
|
+
SET: compileSet,
|
|
1539
|
+
};
|
|
1540
|
+
|
|
1541
|
+
class SetNode extends Traversal {
|
|
1542
|
+
constructor(val, loc, strip) {
|
|
1543
|
+
super();
|
|
1544
|
+
Object.defineProperty(this, "val", {
|
|
1545
|
+
enumerable: true,
|
|
1546
|
+
configurable: true,
|
|
1547
|
+
writable: true,
|
|
1548
|
+
value: val
|
|
1549
|
+
});
|
|
1550
|
+
Object.defineProperty(this, "loc", {
|
|
1551
|
+
enumerable: true,
|
|
1552
|
+
configurable: true,
|
|
1553
|
+
writable: true,
|
|
1554
|
+
value: loc
|
|
1555
|
+
});
|
|
1556
|
+
Object.defineProperty(this, "strip", {
|
|
1557
|
+
enumerable: true,
|
|
1558
|
+
configurable: true,
|
|
1559
|
+
writable: true,
|
|
1560
|
+
value: strip
|
|
1561
|
+
});
|
|
1562
|
+
Object.defineProperty(this, "type", {
|
|
1563
|
+
enumerable: true,
|
|
1564
|
+
configurable: true,
|
|
1565
|
+
writable: true,
|
|
1566
|
+
value: 'SET'
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
async function* parseSet(token, parser) {
|
|
1572
|
+
if (!token.expression) {
|
|
1573
|
+
parser.emitExpErr(token);
|
|
1574
|
+
return;
|
|
1575
|
+
}
|
|
1576
|
+
yield 'NEXT';
|
|
1577
|
+
yield new SetNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
1578
|
+
}
|
|
1579
|
+
const parsers$3 = {
|
|
1580
|
+
set: parseSet,
|
|
1581
|
+
};
|
|
1582
|
+
|
|
1583
|
+
const plugin$3 = {
|
|
1584
|
+
compilers: compilers$3,
|
|
1585
|
+
parsers: parsers$3,
|
|
1416
1586
|
};
|
|
1417
1587
|
|
|
1418
1588
|
async function compileText({ val, loc, strip: { start, end } }, compiler) {
|
|
@@ -1424,226 +1594,246 @@ ${output
|
|
|
1424
1594
|
}
|
|
1425
1595
|
compiler.pushStr(loc, val);
|
|
1426
1596
|
}
|
|
1427
|
-
const compilers$
|
|
1428
|
-
|
|
1597
|
+
const compilers$2 = {
|
|
1598
|
+
TEXT: compileText,
|
|
1429
1599
|
};
|
|
1430
1600
|
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1601
|
+
class TextNode extends Traversal {
|
|
1602
|
+
constructor(val, loc, strip) {
|
|
1603
|
+
super();
|
|
1604
|
+
Object.defineProperty(this, "val", {
|
|
1605
|
+
enumerable: true,
|
|
1606
|
+
configurable: true,
|
|
1607
|
+
writable: true,
|
|
1608
|
+
value: val
|
|
1609
|
+
});
|
|
1610
|
+
Object.defineProperty(this, "loc", {
|
|
1611
|
+
enumerable: true,
|
|
1612
|
+
configurable: true,
|
|
1613
|
+
writable: true,
|
|
1614
|
+
value: loc
|
|
1615
|
+
});
|
|
1616
|
+
Object.defineProperty(this, "strip", {
|
|
1617
|
+
enumerable: true,
|
|
1618
|
+
configurable: true,
|
|
1619
|
+
writable: true,
|
|
1620
|
+
value: strip
|
|
1621
|
+
});
|
|
1622
|
+
Object.defineProperty(this, "type", {
|
|
1623
|
+
enumerable: true,
|
|
1624
|
+
configurable: true,
|
|
1625
|
+
writable: true,
|
|
1626
|
+
value: 'TEXT'
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1444
1630
|
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
outputClose: '}}',
|
|
1452
|
-
parsers,
|
|
1453
|
-
debug: () => { },
|
|
1631
|
+
async function* parseText(token) {
|
|
1632
|
+
yield 'NEXT';
|
|
1633
|
+
yield new TextNode(token.val, token.loc, token.strip);
|
|
1634
|
+
}
|
|
1635
|
+
const parsers$2 = {
|
|
1636
|
+
text: parseText,
|
|
1454
1637
|
};
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
compilers,
|
|
1460
|
-
loader: async () => '',
|
|
1638
|
+
|
|
1639
|
+
const plugin$2 = {
|
|
1640
|
+
compilers: compilers$2,
|
|
1641
|
+
parsers: parsers$2,
|
|
1461
1642
|
};
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1643
|
+
|
|
1644
|
+
async function compileUnexpected$1({ name, val, loc }, compiler) {
|
|
1645
|
+
compiler.pushRaw(loc, `<!-- unexpected "${name}" with value "${val}" -->`);
|
|
1646
|
+
}
|
|
1647
|
+
const compilers$1 = {
|
|
1648
|
+
UNEXPECTED: compileUnexpected$1,
|
|
1468
1649
|
};
|
|
1469
1650
|
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
* {{ foo }}
|
|
1475
|
-
*
|
|
1476
|
-
* - compiled:
|
|
1477
|
-
* s+=e(foo);
|
|
1478
|
-
*
|
|
1479
|
-
* - sourceMap:
|
|
1480
|
-
* Mapping {
|
|
1481
|
-
* source: {
|
|
1482
|
-
* start: {
|
|
1483
|
-
* line: 2,
|
|
1484
|
-
* column: 4,
|
|
1485
|
-
* },
|
|
1486
|
-
* end: {
|
|
1487
|
-
* line: 3,
|
|
1488
|
-
* column: 5,
|
|
1489
|
-
* },
|
|
1490
|
-
* },
|
|
1491
|
-
* target: {
|
|
1492
|
-
* start: {
|
|
1493
|
-
* line: 2,
|
|
1494
|
-
* column: 3,
|
|
1495
|
-
* },
|
|
1496
|
-
* end: {
|
|
1497
|
-
* line: 3,
|
|
1498
|
-
* column: 8,
|
|
1499
|
-
* },
|
|
1500
|
-
* }
|
|
1501
|
-
* }
|
|
1502
|
-
*/
|
|
1503
|
-
class SourceMap {
|
|
1504
|
-
constructor() {
|
|
1505
|
-
Object.defineProperty(this, "mappings", {
|
|
1651
|
+
class UnexpectedNode extends Traversal {
|
|
1652
|
+
constructor(name, val, loc, strip) {
|
|
1653
|
+
super();
|
|
1654
|
+
Object.defineProperty(this, "name", {
|
|
1506
1655
|
enumerable: true,
|
|
1507
1656
|
configurable: true,
|
|
1508
1657
|
writable: true,
|
|
1509
|
-
value:
|
|
1658
|
+
value: name
|
|
1510
1659
|
});
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1660
|
+
Object.defineProperty(this, "val", {
|
|
1661
|
+
enumerable: true,
|
|
1662
|
+
configurable: true,
|
|
1663
|
+
writable: true,
|
|
1664
|
+
value: val
|
|
1665
|
+
});
|
|
1666
|
+
Object.defineProperty(this, "loc", {
|
|
1667
|
+
enumerable: true,
|
|
1668
|
+
configurable: true,
|
|
1669
|
+
writable: true,
|
|
1670
|
+
value: loc
|
|
1671
|
+
});
|
|
1672
|
+
Object.defineProperty(this, "strip", {
|
|
1673
|
+
enumerable: true,
|
|
1674
|
+
configurable: true,
|
|
1675
|
+
writable: true,
|
|
1676
|
+
value: strip
|
|
1677
|
+
});
|
|
1678
|
+
Object.defineProperty(this, "type", {
|
|
1679
|
+
enumerable: true,
|
|
1680
|
+
configurable: true,
|
|
1681
|
+
writable: true,
|
|
1682
|
+
value: 'unexpected'
|
|
1516
1683
|
});
|
|
1517
|
-
}
|
|
1518
|
-
getSourceLoc({ line, column }) {
|
|
1519
|
-
return this.mappings
|
|
1520
|
-
.filter(({ target: { start, end } }) => start.line <= line &&
|
|
1521
|
-
end.line >= line &&
|
|
1522
|
-
(start.line < line || start.column <= column) &&
|
|
1523
|
-
(end.line > line || end.column >= column))
|
|
1524
|
-
.map(({ source }) => source);
|
|
1525
1684
|
}
|
|
1526
1685
|
}
|
|
1527
1686
|
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1687
|
+
async function* parseUnexpected(token, parser) {
|
|
1688
|
+
const name = token.name ?? token.type;
|
|
1689
|
+
parser.options.debug?.(new CompileError(`Unexpected "${name}" node`, parser.template, token.loc));
|
|
1690
|
+
yield 'NEXT';
|
|
1691
|
+
yield new UnexpectedNode(name, token.val, token.loc, token.strip);
|
|
1692
|
+
}
|
|
1693
|
+
const parsers$1 = {
|
|
1694
|
+
unexpected: parseUnexpected,
|
|
1695
|
+
};
|
|
1696
|
+
|
|
1697
|
+
const plugin$1 = {
|
|
1698
|
+
compilers: compilers$1,
|
|
1699
|
+
parsers: parsers$1,
|
|
1700
|
+
};
|
|
1701
|
+
|
|
1702
|
+
async function compileUnexpected({ name, val, loc }, compiler) {
|
|
1703
|
+
compiler.pushRaw(loc, `<!-- unexpected "${name}" with value "${val}" -->`);
|
|
1704
|
+
}
|
|
1705
|
+
const compilers = {
|
|
1706
|
+
UNEXPECTED: compileUnexpected,
|
|
1707
|
+
};
|
|
1708
|
+
|
|
1709
|
+
class UnknownNode extends Traversal {
|
|
1710
|
+
constructor(name, val, loc, strip) {
|
|
1711
|
+
super();
|
|
1712
|
+
Object.defineProperty(this, "name", {
|
|
1532
1713
|
enumerable: true,
|
|
1533
1714
|
configurable: true,
|
|
1534
1715
|
writable: true,
|
|
1535
|
-
value:
|
|
1716
|
+
value: name
|
|
1536
1717
|
});
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
start() {
|
|
1543
|
-
this.pushRaw(null, 'return(async()=>{', 'let s="";');
|
|
1544
|
-
}
|
|
1545
|
-
end() {
|
|
1546
|
-
this.pushRaw(null, 'return s;', '})();');
|
|
1547
|
-
}
|
|
1548
|
-
pushRaw(loc, ...lines) {
|
|
1549
|
-
const start = this.code.length;
|
|
1550
|
-
for (const line of lines) {
|
|
1551
|
-
this.code += line;
|
|
1552
|
-
}
|
|
1553
|
-
if (!loc) {
|
|
1554
|
-
return;
|
|
1555
|
-
}
|
|
1556
|
-
this.addMapping(loc, {
|
|
1557
|
-
start: {
|
|
1558
|
-
line: 1,
|
|
1559
|
-
column: start,
|
|
1560
|
-
},
|
|
1561
|
-
end: {
|
|
1562
|
-
line: 1,
|
|
1563
|
-
column: this.code.length,
|
|
1564
|
-
},
|
|
1718
|
+
Object.defineProperty(this, "val", {
|
|
1719
|
+
enumerable: true,
|
|
1720
|
+
configurable: true,
|
|
1721
|
+
writable: true,
|
|
1722
|
+
value: val
|
|
1565
1723
|
});
|
|
1566
|
-
|
|
1567
|
-
pushStr(loc, s) {
|
|
1568
|
-
if (s) {
|
|
1569
|
-
this.pushRaw(loc, `s+="${s
|
|
1570
|
-
.replace(/\\/g, '\\\\')
|
|
1571
|
-
.replace(/"/g, '\\"')
|
|
1572
|
-
.replace(/[\n\r]/g, '\\n')}";`);
|
|
1573
|
-
}
|
|
1574
|
-
}
|
|
1575
|
-
pushVar(loc, v) {
|
|
1576
|
-
this.pushRaw(loc, `s+=${ESCAPE}(${v});`);
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
class Context extends OutScript {
|
|
1581
|
-
constructor() {
|
|
1582
|
-
super(...arguments);
|
|
1583
|
-
Object.defineProperty(this, "context", {
|
|
1724
|
+
Object.defineProperty(this, "loc", {
|
|
1584
1725
|
enumerable: true,
|
|
1585
1726
|
configurable: true,
|
|
1586
1727
|
writable: true,
|
|
1587
|
-
value:
|
|
1728
|
+
value: loc
|
|
1588
1729
|
});
|
|
1589
|
-
Object.defineProperty(this, "
|
|
1730
|
+
Object.defineProperty(this, "strip", {
|
|
1590
1731
|
enumerable: true,
|
|
1591
1732
|
configurable: true,
|
|
1592
1733
|
writable: true,
|
|
1593
|
-
value:
|
|
1734
|
+
value: strip
|
|
1594
1735
|
});
|
|
1595
|
-
Object.defineProperty(this, "
|
|
1736
|
+
Object.defineProperty(this, "type", {
|
|
1596
1737
|
enumerable: true,
|
|
1597
1738
|
configurable: true,
|
|
1598
1739
|
writable: true,
|
|
1599
|
-
value:
|
|
1740
|
+
value: 'UNKNOWN'
|
|
1600
1741
|
});
|
|
1601
1742
|
}
|
|
1602
|
-
in() {
|
|
1603
|
-
this.context = `${this.context}_${this.index++}`;
|
|
1604
|
-
this.contexts.push(this.context);
|
|
1605
|
-
return this.context;
|
|
1606
|
-
}
|
|
1607
|
-
out() {
|
|
1608
|
-
if (this.context === CONTEXT) {
|
|
1609
|
-
return;
|
|
1610
|
-
}
|
|
1611
|
-
this.contexts.pop();
|
|
1612
|
-
this.context = this.contexts.at(-1);
|
|
1613
|
-
}
|
|
1614
1743
|
}
|
|
1615
1744
|
|
|
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
|
-
|
|
1644
|
-
|
|
1745
|
+
async function* parseUnknown(token, parser) {
|
|
1746
|
+
const name = token.name ?? token.type;
|
|
1747
|
+
parser.options.debug?.(new CompileError(`Unknown "${name}" node`, parser.template, token.loc));
|
|
1748
|
+
yield 'NEXT';
|
|
1749
|
+
yield new UnknownNode(name, token.val, token.loc, token.strip);
|
|
1750
|
+
}
|
|
1751
|
+
const parsers = {
|
|
1752
|
+
unknown: parseUnknown,
|
|
1753
|
+
};
|
|
1754
|
+
|
|
1755
|
+
const plugin = {
|
|
1756
|
+
compilers,
|
|
1757
|
+
parsers,
|
|
1758
|
+
};
|
|
1759
|
+
|
|
1760
|
+
const plugins = [
|
|
1761
|
+
plugin$c,
|
|
1762
|
+
plugin$b,
|
|
1763
|
+
plugin$9,
|
|
1764
|
+
plugin$a,
|
|
1765
|
+
plugin$8,
|
|
1766
|
+
plugin$7,
|
|
1767
|
+
plugin$6,
|
|
1768
|
+
plugin$5,
|
|
1769
|
+
plugin$3,
|
|
1770
|
+
plugin$2,
|
|
1771
|
+
plugin$4,
|
|
1772
|
+
plugin$1,
|
|
1773
|
+
plugin,
|
|
1774
|
+
];
|
|
1775
|
+
|
|
1776
|
+
const baseOptions = {
|
|
1777
|
+
filters: _filters,
|
|
1778
|
+
parsers: {},
|
|
1779
|
+
compilers: {},
|
|
1780
|
+
plugins,
|
|
1781
|
+
debug: () => { },
|
|
1782
|
+
};
|
|
1783
|
+
const parserOptions = {
|
|
1784
|
+
...baseOptions,
|
|
1785
|
+
commentOpen: '{{#',
|
|
1786
|
+
commentClose: '}}',
|
|
1787
|
+
directiveOpen: '{{',
|
|
1788
|
+
directiveClose: '}}',
|
|
1789
|
+
outputOpen: '{{=',
|
|
1790
|
+
outputClose: '}}',
|
|
1791
|
+
};
|
|
1792
|
+
const compilerOptions = {
|
|
1793
|
+
...parserOptions,
|
|
1794
|
+
trimWhitespace: false,
|
|
1795
|
+
stripComments: false,
|
|
1796
|
+
loader: async () => '',
|
|
1797
|
+
};
|
|
1798
|
+
const renderOptions = {
|
|
1799
|
+
...compilerOptions,
|
|
1800
|
+
globals: {},
|
|
1801
|
+
autoEscape: true,
|
|
1802
|
+
};
|
|
1803
|
+
const mergeOptions = (defaultOptions, options) => {
|
|
1804
|
+
const mergedOptions = {
|
|
1805
|
+
...defaultOptions,
|
|
1806
|
+
...options,
|
|
1807
|
+
};
|
|
1808
|
+
defaultOptions.plugins?.forEach((plugin) => {
|
|
1809
|
+
Object.assign(mergedOptions.filters, plugin.filters ?? {});
|
|
1810
|
+
Object.assign(mergedOptions.parsers, plugin.parsers ?? {});
|
|
1811
|
+
Object.assign(mergedOptions.compilers, plugin.compilers ?? {});
|
|
1812
|
+
});
|
|
1813
|
+
options?.plugins?.forEach((plugin) => {
|
|
1814
|
+
Object.assign(mergedOptions.filters, plugin.filters ?? {});
|
|
1815
|
+
Object.assign(mergedOptions.parsers, plugin.parsers ?? {});
|
|
1816
|
+
Object.assign(mergedOptions.compilers, plugin.compilers ?? {});
|
|
1817
|
+
});
|
|
1818
|
+
return mergedOptions;
|
|
1645
1819
|
};
|
|
1646
1820
|
|
|
1821
|
+
function updatePosition(str, pos) {
|
|
1822
|
+
const newlines = (str.match(/\n/g) || []).length;
|
|
1823
|
+
if (newlines > 0) {
|
|
1824
|
+
pos.line += newlines;
|
|
1825
|
+
const lastNewline = str.lastIndexOf('\n');
|
|
1826
|
+
pos.column = str.length - lastNewline;
|
|
1827
|
+
}
|
|
1828
|
+
else {
|
|
1829
|
+
pos.column += str.length;
|
|
1830
|
+
}
|
|
1831
|
+
return {
|
|
1832
|
+
line: pos.line,
|
|
1833
|
+
column: pos.column,
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1647
1837
|
const expTokenTypes = {
|
|
1648
1838
|
and: 'AND',
|
|
1649
1839
|
or: 'OR',
|
|
@@ -1670,33 +1860,12 @@ ${output
|
|
|
1670
1860
|
null: 'LIT',
|
|
1671
1861
|
undefined: 'LIT',
|
|
1672
1862
|
'|': 'PIPE',
|
|
1673
|
-
'=': '
|
|
1863
|
+
'=': 'ASSIGN',
|
|
1674
1864
|
'(': 'LP',
|
|
1675
1865
|
')': 'RP',
|
|
1676
1866
|
',': 'COMMA',
|
|
1677
1867
|
'.': 'DOT',
|
|
1678
1868
|
};
|
|
1679
|
-
|
|
1680
|
-
/**
|
|
1681
|
-
* Update position by a given string, and return a new position with the
|
|
1682
|
-
* updated values.
|
|
1683
|
-
*/
|
|
1684
|
-
function updatePosition(str, pos) {
|
|
1685
|
-
const newlines = (str.match(/\n/g) || []).length;
|
|
1686
|
-
if (newlines > 0) {
|
|
1687
|
-
pos.line += newlines;
|
|
1688
|
-
const lastNewline = str.lastIndexOf('\n');
|
|
1689
|
-
pos.column = str.length - lastNewline;
|
|
1690
|
-
}
|
|
1691
|
-
else {
|
|
1692
|
-
pos.column += str.length;
|
|
1693
|
-
}
|
|
1694
|
-
return {
|
|
1695
|
-
line: pos.line,
|
|
1696
|
-
column: pos.column,
|
|
1697
|
-
};
|
|
1698
|
-
}
|
|
1699
|
-
|
|
1700
1869
|
class ExpTokenizer {
|
|
1701
1870
|
constructor(template) {
|
|
1702
1871
|
Object.defineProperty(this, "template", {
|
|
@@ -1907,6 +2076,36 @@ ${output
|
|
|
1907
2076
|
return /[\w$]/.test(char);
|
|
1908
2077
|
}
|
|
1909
2078
|
|
|
2079
|
+
const expTokenPrecedences = {
|
|
2080
|
+
COMMA: 5,
|
|
2081
|
+
IF: 10,
|
|
2082
|
+
ELSE: 10,
|
|
2083
|
+
ASSIGN: 15,
|
|
2084
|
+
LP: 20,
|
|
2085
|
+
RP: 20,
|
|
2086
|
+
OR: 25,
|
|
2087
|
+
AND: 30,
|
|
2088
|
+
IS: 40,
|
|
2089
|
+
EQ: 40,
|
|
2090
|
+
NE: 40,
|
|
2091
|
+
GT: 40,
|
|
2092
|
+
LT: 40,
|
|
2093
|
+
GE: 40,
|
|
2094
|
+
LE: 40,
|
|
2095
|
+
IN: 40,
|
|
2096
|
+
NI: 40,
|
|
2097
|
+
OF: 40,
|
|
2098
|
+
ADD: 50,
|
|
2099
|
+
SUB: 50,
|
|
2100
|
+
MUL: 60,
|
|
2101
|
+
DIV: 60,
|
|
2102
|
+
MOD: 60,
|
|
2103
|
+
NOT: 70,
|
|
2104
|
+
PIPE: 80,
|
|
2105
|
+
DOT: 90,
|
|
2106
|
+
ID: 90,
|
|
2107
|
+
LIT: 90,
|
|
2108
|
+
};
|
|
1910
2109
|
class ExpParser {
|
|
1911
2110
|
constructor(template) {
|
|
1912
2111
|
Object.defineProperty(this, "template", {
|
|
@@ -2006,7 +2205,7 @@ ${output
|
|
|
2006
2205
|
type: 'ID',
|
|
2007
2206
|
value: token.value,
|
|
2008
2207
|
};
|
|
2009
|
-
if (this.
|
|
2208
|
+
if (this.match('DOT')) {
|
|
2010
2209
|
left.path = this.parsePath();
|
|
2011
2210
|
}
|
|
2012
2211
|
const lp = this.consume('LP');
|
|
@@ -2063,7 +2262,7 @@ ${output
|
|
|
2063
2262
|
token.type === 'MUL' ||
|
|
2064
2263
|
token.type === 'DIV' ||
|
|
2065
2264
|
token.type === 'MOD' ||
|
|
2066
|
-
token.type === '
|
|
2265
|
+
token.type === 'ASSIGN') {
|
|
2067
2266
|
if (!left) {
|
|
2068
2267
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2069
2268
|
}
|
|
@@ -2083,7 +2282,7 @@ ${output
|
|
|
2083
2282
|
if (!left) {
|
|
2084
2283
|
throw new CompileError(`no left operand for "${token.type}"`, this.template, token.loc);
|
|
2085
2284
|
}
|
|
2086
|
-
if (!this.
|
|
2285
|
+
if (!this.match('ID')) {
|
|
2087
2286
|
throw new CompileError('expected "ID" after "PIPE"', this.template, token.loc);
|
|
2088
2287
|
}
|
|
2089
2288
|
left = {
|
|
@@ -2123,7 +2322,7 @@ ${output
|
|
|
2123
2322
|
}
|
|
2124
2323
|
parseSequence() {
|
|
2125
2324
|
const elements = [];
|
|
2126
|
-
while (!this.
|
|
2325
|
+
while (!this.match('RP')) {
|
|
2127
2326
|
const element = this.parseExp((t) => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
2128
2327
|
if (element) {
|
|
2129
2328
|
elements.push(element);
|
|
@@ -2162,11 +2361,11 @@ ${output
|
|
|
2162
2361
|
peek() {
|
|
2163
2362
|
return this.tokens.at(this.index) ?? null;
|
|
2164
2363
|
}
|
|
2165
|
-
|
|
2364
|
+
match(type) {
|
|
2166
2365
|
return this.peek()?.type === type;
|
|
2167
2366
|
}
|
|
2168
2367
|
consume(type) {
|
|
2169
|
-
if (this.
|
|
2368
|
+
if (this.match(type)) {
|
|
2170
2369
|
return this.next();
|
|
2171
2370
|
}
|
|
2172
2371
|
}
|
|
@@ -2237,10 +2436,7 @@ ${output
|
|
|
2237
2436
|
writable: true,
|
|
2238
2437
|
value: 1
|
|
2239
2438
|
});
|
|
2240
|
-
this.options =
|
|
2241
|
-
...parserOptions,
|
|
2242
|
-
...options,
|
|
2243
|
-
};
|
|
2439
|
+
this.options = mergeOptions(parserOptions, options);
|
|
2244
2440
|
const { commentOpen, commentClose, directiveOpen, directiveClose, outputOpen, outputClose, } = this.options;
|
|
2245
2441
|
this.tokenPatterns = [
|
|
2246
2442
|
[TokenType.COMMENT, commentOpen, commentClose],
|
|
@@ -2379,15 +2575,15 @@ ${output
|
|
|
2379
2575
|
value: void 0
|
|
2380
2576
|
});
|
|
2381
2577
|
}
|
|
2382
|
-
parse(template) {
|
|
2578
|
+
async parse(template) {
|
|
2383
2579
|
this.tokenize(template);
|
|
2384
2580
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2385
2581
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2386
2582
|
this.expParser = new ExpParser(template);
|
|
2387
2583
|
this.cursor = 0;
|
|
2388
|
-
return new RootNode(this.parseUntil(), { start, end });
|
|
2584
|
+
return new RootNode(await this.parseUntil(), { start, end });
|
|
2389
2585
|
}
|
|
2390
|
-
parseUntil(names) {
|
|
2586
|
+
async parseUntil(names) {
|
|
2391
2587
|
const nodes = [];
|
|
2392
2588
|
let prevToken = null;
|
|
2393
2589
|
while (true) {
|
|
@@ -2404,50 +2600,33 @@ ${output
|
|
|
2404
2600
|
if (token.strip?.before && prevToken?.type === TokenType.TEXT) {
|
|
2405
2601
|
prevToken.strip.end = true;
|
|
2406
2602
|
}
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2603
|
+
let parser = this.options.parsers[token.name?.toLowerCase() ??
|
|
2604
|
+
token.type?.toLowerCase()];
|
|
2605
|
+
if (parser === 'unexpected') {
|
|
2606
|
+
parser = this.options.parsers['unexpected'];
|
|
2607
|
+
}
|
|
2608
|
+
if (!parser) {
|
|
2609
|
+
parser = this.options.parsers['unknown'];
|
|
2610
|
+
}
|
|
2611
|
+
const ag = parser?.(token, this);
|
|
2612
|
+
if (ag) {
|
|
2613
|
+
while (true) {
|
|
2614
|
+
const { value, done } = await ag.next();
|
|
2615
|
+
if (done) {
|
|
2616
|
+
break;
|
|
2617
|
+
}
|
|
2618
|
+
if (value === 'NEXT') {
|
|
2619
|
+
this.advance();
|
|
2620
|
+
}
|
|
2621
|
+
else if (value) {
|
|
2622
|
+
nodes.push(value);
|
|
2623
|
+
}
|
|
2624
|
+
}
|
|
2420
2625
|
}
|
|
2421
2626
|
prevToken = token;
|
|
2422
2627
|
}
|
|
2423
2628
|
return nodes;
|
|
2424
2629
|
}
|
|
2425
|
-
createComment({ val, loc, strip }) {
|
|
2426
|
-
this.advance();
|
|
2427
|
-
return new CommentNode(val, loc, strip);
|
|
2428
|
-
}
|
|
2429
|
-
createOutput({ val, loc, strip }) {
|
|
2430
|
-
this.advance();
|
|
2431
|
-
return new OutputNode(val, loc, strip, this.parseExp({ val, loc }));
|
|
2432
|
-
}
|
|
2433
|
-
createText({ val, loc, strip }) {
|
|
2434
|
-
this.advance();
|
|
2435
|
-
return new TextNode(val, loc, strip);
|
|
2436
|
-
}
|
|
2437
|
-
createDirective(token) {
|
|
2438
|
-
return (this.options.parsers[token.name.toLowerCase()]?.(token, this) ??
|
|
2439
|
-
this.createUnknownDirective(token));
|
|
2440
|
-
}
|
|
2441
|
-
createUnexpectedDirective(token) {
|
|
2442
|
-
this.options.debug?.(new CompileError(`Unexpected "${token.name}" directive`, this.template, token.loc));
|
|
2443
|
-
this.advance();
|
|
2444
|
-
return new UnexpectedDirectiveNode(token.name, token.val, token.loc, token.strip);
|
|
2445
|
-
}
|
|
2446
|
-
createUnknownDirective(token) {
|
|
2447
|
-
this.options.debug?.(new CompileError(`Unknown "${token.name}" directive`, this.template, token.loc));
|
|
2448
|
-
this.advance();
|
|
2449
|
-
return new UnknownDirectiveNode(token.name, token.val, token.loc, token.strip);
|
|
2450
|
-
}
|
|
2451
2630
|
parseExp({ val, loc }) {
|
|
2452
2631
|
return this.expParser.parse(val, loc);
|
|
2453
2632
|
}
|
|
@@ -2460,15 +2639,10 @@ ${output
|
|
|
2460
2639
|
match(names, token = this.peek()) {
|
|
2461
2640
|
return names.includes(token?.name);
|
|
2462
2641
|
}
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
}
|
|
2468
|
-
requireNoExpression({ expression, name, loc }) {
|
|
2469
|
-
if (expression) {
|
|
2470
|
-
throw this.options.debug?.(new CompileError(`"${name}" should not have expression`, this.template, loc));
|
|
2471
|
-
}
|
|
2642
|
+
emitExpErr({ name, loc }, required = true) {
|
|
2643
|
+
this.options.debug?.(new CompileError(required
|
|
2644
|
+
? `"${name}" requires expression`
|
|
2645
|
+
: `"${name}" should not have expression`, this.template, loc));
|
|
2472
2646
|
}
|
|
2473
2647
|
}
|
|
2474
2648
|
|
|
@@ -2517,17 +2691,14 @@ ${output
|
|
|
2517
2691
|
writable: true,
|
|
2518
2692
|
value: void 0
|
|
2519
2693
|
});
|
|
2520
|
-
this.options =
|
|
2521
|
-
...compilerOptions,
|
|
2522
|
-
...options,
|
|
2523
|
-
};
|
|
2694
|
+
this.options = mergeOptions(compilerOptions, options);
|
|
2524
2695
|
}
|
|
2525
2696
|
async compile(template, root = true, partials = new Map(), blocks = new Map()) {
|
|
2526
2697
|
this.template = template;
|
|
2527
2698
|
this.isRoot = root;
|
|
2528
2699
|
this.partials = partials;
|
|
2529
2700
|
this.blocks = blocks;
|
|
2530
|
-
this.rootNode = new Parser(this.options).parse(template);
|
|
2701
|
+
this.rootNode = await new Parser(this.options).parse(template);
|
|
2531
2702
|
this.state = {};
|
|
2532
2703
|
this.start();
|
|
2533
2704
|
await this.parseTemplates();
|
|
@@ -2588,12 +2759,12 @@ ${output
|
|
|
2588
2759
|
const partials = new Map();
|
|
2589
2760
|
const blocks = [];
|
|
2590
2761
|
const traverse = (node) => {
|
|
2591
|
-
if (node.type ===
|
|
2762
|
+
if (node.type === 'INCLUDE') {
|
|
2592
2763
|
if (!partials.has(node.val.value)) {
|
|
2593
2764
|
partials.set(node.val.value, node);
|
|
2594
2765
|
}
|
|
2595
2766
|
}
|
|
2596
|
-
if (node.type ===
|
|
2767
|
+
if (node.type === 'BLOCK') {
|
|
2597
2768
|
blocks.push(node);
|
|
2598
2769
|
}
|
|
2599
2770
|
node.traverse(traverse);
|
|
@@ -2605,15 +2776,16 @@ ${output
|
|
|
2605
2776
|
};
|
|
2606
2777
|
}
|
|
2607
2778
|
async compileNode(node) {
|
|
2608
|
-
if (node.type ===
|
|
2779
|
+
if (node.type === 'TEMPLATE') {
|
|
2609
2780
|
await this.compileNodes(node.body);
|
|
2610
2781
|
return;
|
|
2611
2782
|
}
|
|
2612
|
-
|
|
2613
|
-
|
|
2783
|
+
const compiler = this.options.compilers[node.type];
|
|
2784
|
+
if (compiler) {
|
|
2785
|
+
await compiler(node, this);
|
|
2614
2786
|
return;
|
|
2615
2787
|
}
|
|
2616
|
-
this.options.debug?.(new CompileError(`Unknown "${node.name}"
|
|
2788
|
+
this.options.debug?.(new CompileError(`Unknown "${node.name}" node`, this.template, node.loc));
|
|
2617
2789
|
}
|
|
2618
2790
|
async compileNodes(nodes) {
|
|
2619
2791
|
for (const child of nodes) {
|
|
@@ -2656,23 +2828,14 @@ ${output
|
|
|
2656
2828
|
}
|
|
2657
2829
|
|
|
2658
2830
|
class Renderer {
|
|
2659
|
-
constructor(
|
|
2831
|
+
constructor(options) {
|
|
2660
2832
|
Object.defineProperty(this, "options", {
|
|
2661
2833
|
enumerable: true,
|
|
2662
2834
|
configurable: true,
|
|
2663
2835
|
writable: true,
|
|
2664
2836
|
value: void 0
|
|
2665
2837
|
});
|
|
2666
|
-
this.options =
|
|
2667
|
-
Object.assign(this.options.globals, globals);
|
|
2668
|
-
Object.assign(this.options.filters, filters);
|
|
2669
|
-
Object.assign(this.options.parsers, parsers);
|
|
2670
|
-
Object.assign(this.options.compilers, compilers);
|
|
2671
|
-
plugins?.forEach((plugin) => {
|
|
2672
|
-
Object.assign(this.options.filters, plugin.filters);
|
|
2673
|
-
Object.assign(this.options.parsers, plugin.parsers);
|
|
2674
|
-
Object.assign(this.options.compilers, plugin.compilers);
|
|
2675
|
-
});
|
|
2838
|
+
this.options = mergeOptions(renderOptions, options);
|
|
2676
2839
|
}
|
|
2677
2840
|
async render(template, globals) {
|
|
2678
2841
|
const compiler = new Compiler(this.options);
|