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