janja 0.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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.cjs.js +1948 -0
- package/dist/index.d.ts +227 -0
- package/dist/index.esm.js +1940 -0
- package/dist/index.js +1951 -0
- package/package.json +52 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1951 @@
|
|
|
1
|
+
(function (exports) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
function highlightSource(message, source, ranges) {
|
|
5
|
+
const output = [];
|
|
6
|
+
const caretLines = new Set();
|
|
7
|
+
const addCaretLine = (index) => {
|
|
8
|
+
caretLines.add(index - 2);
|
|
9
|
+
caretLines.add(index - 1);
|
|
10
|
+
caretLines.add(index);
|
|
11
|
+
caretLines.add(index + 1);
|
|
12
|
+
caretLines.add(index + 2);
|
|
13
|
+
};
|
|
14
|
+
const lines = source.split('\n');
|
|
15
|
+
const indentWidth = String(lines.length).length + 2;
|
|
16
|
+
// Copy to avoid mutation
|
|
17
|
+
ranges = [...ranges];
|
|
18
|
+
let cursor = 0;
|
|
19
|
+
let caretLinesCount = 0;
|
|
20
|
+
lines.forEach((line, index) => {
|
|
21
|
+
output.push(`${`${`${index + 1}: `.padStart(indentWidth, ' ')}`}${line}`);
|
|
22
|
+
ranges.forEach((tag) => {
|
|
23
|
+
if (tag.start < cursor) {
|
|
24
|
+
ranges.splice(ranges.indexOf(tag), 1);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (tag.start >= cursor + line.length + 1) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const offset = tag.start - cursor + indentWidth;
|
|
31
|
+
const end = Math.min(tag.end, cursor + line.length);
|
|
32
|
+
const count = end - tag.start;
|
|
33
|
+
const caretLine = `${' '.repeat(offset)}${'^'.repeat(count)}`;
|
|
34
|
+
if (/\S/.test(caretLine)) {
|
|
35
|
+
output.push(caretLine);
|
|
36
|
+
}
|
|
37
|
+
if (end < tag.end) {
|
|
38
|
+
tag.start += count + 1;
|
|
39
|
+
}
|
|
40
|
+
addCaretLine(++caretLinesCount + index);
|
|
41
|
+
});
|
|
42
|
+
cursor += line.length + 1;
|
|
43
|
+
});
|
|
44
|
+
const emptyLine = `${' '.repeat(indentWidth)}...`;
|
|
45
|
+
return `${message}
|
|
46
|
+
|
|
47
|
+
${output
|
|
48
|
+
.map((line, index) => (caretLines.has(index) ? line : emptyLine))
|
|
49
|
+
.reduce((acc, line) => line === emptyLine && acc.at(-1) === line ? acc : [...acc, line], [])
|
|
50
|
+
.join('\n')}
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class CompileError extends Error {
|
|
55
|
+
constructor(message, token, filepath) {
|
|
56
|
+
super(filepath ? `${message} at ${filepath}` : message);
|
|
57
|
+
Object.defineProperty(this, "token", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: token
|
|
62
|
+
});
|
|
63
|
+
this.name = 'CompileError';
|
|
64
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
65
|
+
}
|
|
66
|
+
get details() {
|
|
67
|
+
return highlightSource(this.message, this.token.raw, [this.token]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const CONTEXT = 'c';
|
|
72
|
+
const FILTERS = 'f'; // Filters can be async
|
|
73
|
+
const ESCAPE = 'e';
|
|
74
|
+
const RAW$1 = 'raw';
|
|
75
|
+
const LAYOUT = 'layout';
|
|
76
|
+
const INCLUDE = 'include';
|
|
77
|
+
const BLOCK = 'block';
|
|
78
|
+
const SUPER = 'super';
|
|
79
|
+
const ENDBLOCK = 'endblock';
|
|
80
|
+
|
|
81
|
+
class Context {
|
|
82
|
+
constructor(options) {
|
|
83
|
+
Object.defineProperty(this, "options", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: options
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "context", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: CONTEXT
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "index", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: 0
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "contexts", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: [CONTEXT]
|
|
106
|
+
});
|
|
107
|
+
Object.defineProperty(this, "expected", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: []
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
in() {
|
|
115
|
+
const len = this.contexts.push(`${this.context}_${this.index++}`);
|
|
116
|
+
return (this.context = this.contexts[len - 1]);
|
|
117
|
+
}
|
|
118
|
+
out() {
|
|
119
|
+
if (this.context === CONTEXT) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.contexts.pop();
|
|
123
|
+
this.context = this.contexts[this.contexts.length - 1];
|
|
124
|
+
}
|
|
125
|
+
expect(name) {
|
|
126
|
+
this.expected.push(name);
|
|
127
|
+
}
|
|
128
|
+
matchAny(name) {
|
|
129
|
+
return this.expected.includes(name);
|
|
130
|
+
}
|
|
131
|
+
match(name) {
|
|
132
|
+
return this.expected.at(-1) === name;
|
|
133
|
+
}
|
|
134
|
+
consume(name) {
|
|
135
|
+
if (this.match(name)) {
|
|
136
|
+
this.expected.pop();
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
validate() {
|
|
142
|
+
if (this.expected.length) {
|
|
143
|
+
throw new Error(`expected tokens "${this.expected.join(', ')}", but got nothing`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
class OutScript {
|
|
149
|
+
constructor(options) {
|
|
150
|
+
Object.defineProperty(this, "options", {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
configurable: true,
|
|
153
|
+
writable: true,
|
|
154
|
+
value: options
|
|
155
|
+
});
|
|
156
|
+
Object.defineProperty(this, "content", {
|
|
157
|
+
enumerable: true,
|
|
158
|
+
configurable: true,
|
|
159
|
+
writable: true,
|
|
160
|
+
value: ''
|
|
161
|
+
});
|
|
162
|
+
Object.defineProperty(this, "strOffset", {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
configurable: true,
|
|
165
|
+
writable: true,
|
|
166
|
+
value: 's+="'.length
|
|
167
|
+
});
|
|
168
|
+
Object.defineProperty(this, "varOffset", {
|
|
169
|
+
enumerable: true,
|
|
170
|
+
configurable: true,
|
|
171
|
+
writable: true,
|
|
172
|
+
value: `s+=${ESCAPE}(`.length
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
get value() {
|
|
176
|
+
return this.content;
|
|
177
|
+
}
|
|
178
|
+
get script() {
|
|
179
|
+
// eslint-disable-next-line no-new-func
|
|
180
|
+
return new Function(CONTEXT, FILTERS, ESCAPE, this.value);
|
|
181
|
+
}
|
|
182
|
+
start() {
|
|
183
|
+
if (this.options.strictMode) {
|
|
184
|
+
this.pushLine('"use strict";');
|
|
185
|
+
}
|
|
186
|
+
this.pushLine('return(async()=>{', 'let s="";');
|
|
187
|
+
}
|
|
188
|
+
end() {
|
|
189
|
+
this.pushLine('return s;', '})();');
|
|
190
|
+
}
|
|
191
|
+
pushLine(...lines) {
|
|
192
|
+
const start = this.content.length;
|
|
193
|
+
for (const line of lines) {
|
|
194
|
+
this.content += line;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
start,
|
|
198
|
+
end: this.content.length,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
pushStr(s, o) {
|
|
202
|
+
if (s) {
|
|
203
|
+
if (o?.trimStart || this.options.trimWhitespace) {
|
|
204
|
+
s = s.trimStart();
|
|
205
|
+
}
|
|
206
|
+
if (o?.trimEnd || this.options.trimWhitespace) {
|
|
207
|
+
s = s.trimEnd();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (s) {
|
|
211
|
+
this.pushLine(`s+="${s.replace(/\\/g, '\\\\')
|
|
212
|
+
.replace(/"/g, '\\"')
|
|
213
|
+
.replace(/[\n\r]/g, '\\n')}";`);
|
|
214
|
+
return {
|
|
215
|
+
start: this.content.length + this.strOffset,
|
|
216
|
+
end: this.content.length - 2,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
pushVar(v) {
|
|
221
|
+
const start = this.content.length + this.varOffset;
|
|
222
|
+
this.pushLine(`s+=${ESCAPE}(${v});`);
|
|
223
|
+
return {
|
|
224
|
+
start,
|
|
225
|
+
end: start + v.length,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Contains the source map information.
|
|
232
|
+
* @example
|
|
233
|
+
* - template:
|
|
234
|
+
* {{ foo }}
|
|
235
|
+
*
|
|
236
|
+
* - compiled:
|
|
237
|
+
* s+=e(foo);
|
|
238
|
+
*
|
|
239
|
+
* - sourceMap:
|
|
240
|
+
* Mapping {
|
|
241
|
+
* source: {
|
|
242
|
+
* start: 3,
|
|
243
|
+
* end: 6,
|
|
244
|
+
* },
|
|
245
|
+
* target: {
|
|
246
|
+
* start: 3,
|
|
247
|
+
* end: 8,
|
|
248
|
+
* }
|
|
249
|
+
* }
|
|
250
|
+
*/
|
|
251
|
+
class SourceMap {
|
|
252
|
+
constructor(options) {
|
|
253
|
+
Object.defineProperty(this, "options", {
|
|
254
|
+
enumerable: true,
|
|
255
|
+
configurable: true,
|
|
256
|
+
writable: true,
|
|
257
|
+
value: options
|
|
258
|
+
});
|
|
259
|
+
Object.defineProperty(this, "mappings", {
|
|
260
|
+
enumerable: true,
|
|
261
|
+
configurable: true,
|
|
262
|
+
writable: true,
|
|
263
|
+
value: []
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
addMapping(source, target) {
|
|
267
|
+
this.mappings.push({
|
|
268
|
+
source,
|
|
269
|
+
target,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
getRanges(offset) {
|
|
273
|
+
return this.mappings
|
|
274
|
+
.filter(({ target: { start, end } }) => start <= offset && end >= offset)
|
|
275
|
+
.map(({ source }) => source);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
class Compiler {
|
|
280
|
+
constructor(options) {
|
|
281
|
+
Object.defineProperty(this, "options", {
|
|
282
|
+
enumerable: true,
|
|
283
|
+
configurable: true,
|
|
284
|
+
writable: true,
|
|
285
|
+
value: options
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
async compile(tagToken, filepath) {
|
|
289
|
+
const ctx = new Context(this.options);
|
|
290
|
+
const out = new OutScript(this.options);
|
|
291
|
+
const sourcemap = new SourceMap(this.options);
|
|
292
|
+
out.start();
|
|
293
|
+
while (tagToken) {
|
|
294
|
+
const compilers = (this.options.compilers[tagToken.name] ?? []);
|
|
295
|
+
for (const compiler of compilers) {
|
|
296
|
+
try {
|
|
297
|
+
const r = await compiler.compile({
|
|
298
|
+
token: tagToken,
|
|
299
|
+
ctx,
|
|
300
|
+
out,
|
|
301
|
+
});
|
|
302
|
+
if (r !== false) {
|
|
303
|
+
if (r) {
|
|
304
|
+
sourcemap.addMapping(tagToken, r);
|
|
305
|
+
}
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
throw new CompileError(error.message, tagToken, filepath);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
tagToken = tagToken.next;
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
ctx.validate();
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
throw new CompileError(error.message, {
|
|
320
|
+
raw: '',
|
|
321
|
+
start: 0,
|
|
322
|
+
end: 0,
|
|
323
|
+
}, filepath);
|
|
324
|
+
}
|
|
325
|
+
out.end();
|
|
326
|
+
return { value: out.value, script: out.script, sourcemap };
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const hasOwn = Object.prototype.hasOwnProperty;
|
|
331
|
+
function translate(translations = {}, key, params = {}) {
|
|
332
|
+
return (hasOwn.call(translations, key) ? translations[key] : key).replace(/\{(.+?)\}/g, (_, name) => (hasOwn.call(params, name) ? params[name] : _));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const monthNames = [
|
|
336
|
+
'January',
|
|
337
|
+
'February',
|
|
338
|
+
'March',
|
|
339
|
+
'April',
|
|
340
|
+
'May',
|
|
341
|
+
'June',
|
|
342
|
+
'July',
|
|
343
|
+
'August',
|
|
344
|
+
'September',
|
|
345
|
+
'October',
|
|
346
|
+
'November',
|
|
347
|
+
'December',
|
|
348
|
+
];
|
|
349
|
+
const shortMonthNames = [
|
|
350
|
+
'Jan',
|
|
351
|
+
'Feb',
|
|
352
|
+
'Mar',
|
|
353
|
+
'Apr',
|
|
354
|
+
'May',
|
|
355
|
+
'Jun',
|
|
356
|
+
'Jul',
|
|
357
|
+
'Aug',
|
|
358
|
+
'Sep',
|
|
359
|
+
'Oct',
|
|
360
|
+
'Nov',
|
|
361
|
+
'Dec',
|
|
362
|
+
];
|
|
363
|
+
const dayNames = [
|
|
364
|
+
'Sunday',
|
|
365
|
+
'Monday',
|
|
366
|
+
'Tuesday',
|
|
367
|
+
'Wednesday',
|
|
368
|
+
'Thursday',
|
|
369
|
+
'Friday',
|
|
370
|
+
'Saturday',
|
|
371
|
+
];
|
|
372
|
+
const shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
373
|
+
const o = {
|
|
374
|
+
y: d => `${d.getUTCFullYear()}`,
|
|
375
|
+
M: (d, len) => `${d.getUTCMonth() + 1}`.padStart(len, '0'),
|
|
376
|
+
N: (d, len, translations) => translate(translations, (len > 1 ? monthNames : shortMonthNames)[d.getUTCMonth()]),
|
|
377
|
+
d: (d, len) => `${d.getUTCDate()}`.padStart(len, '0'),
|
|
378
|
+
h: (d, len) => `${d.getUTCHours()}`.padStart(len, '0'),
|
|
379
|
+
m: (d, len) => `${d.getUTCMinutes()}`.padStart(len, '0'),
|
|
380
|
+
s: (d, len) => `${d.getUTCSeconds()}`.padStart(len, '0'),
|
|
381
|
+
D: (d, len, translations) => translate(translations, (len > 1 ? dayNames : shortDayNames)[d.getUTCDay()]),
|
|
382
|
+
};
|
|
383
|
+
function formatDatetime(translations = {}, value = 0, format) {
|
|
384
|
+
const d = new Date(value);
|
|
385
|
+
return format.replace(/y+|M+|N+|d+|h+|m+|s+|D+/g, (str) => {
|
|
386
|
+
return o[str[0]](d, str.length, translations);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
class Safe extends String {
|
|
391
|
+
constructor(value) {
|
|
392
|
+
super(`${value}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function escape(v) {
|
|
397
|
+
return `${v}`
|
|
398
|
+
.replace(/&/g, '&')
|
|
399
|
+
.replace(/"/g, '"')
|
|
400
|
+
.replace(/'/g, ''')
|
|
401
|
+
.replace(/</g, '<')
|
|
402
|
+
.replace(/>/g, '>');
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function abs(value = 0) {
|
|
406
|
+
return Math.abs(value);
|
|
407
|
+
}
|
|
408
|
+
function add(value = 0, addend = 0) {
|
|
409
|
+
return value + addend;
|
|
410
|
+
}
|
|
411
|
+
function capitalize(value = '') {
|
|
412
|
+
return value.replace(/\b\w/g, match => match.toUpperCase());
|
|
413
|
+
}
|
|
414
|
+
function ceil(value = 0) {
|
|
415
|
+
return Math.ceil(value);
|
|
416
|
+
}
|
|
417
|
+
function compact(value = []) {
|
|
418
|
+
return value.filter(v => v != null);
|
|
419
|
+
}
|
|
420
|
+
function date(value = 0, format = 'yy-MM-dd hh:mm') {
|
|
421
|
+
return formatDatetime(this.translations, value, format);
|
|
422
|
+
}
|
|
423
|
+
function entries(value = {}) {
|
|
424
|
+
return Object.entries(value);
|
|
425
|
+
}
|
|
426
|
+
function even(value = 0) {
|
|
427
|
+
return +value % 2 === 0;
|
|
428
|
+
}
|
|
429
|
+
function fallback(value, defaultValue, anyFalsy = false) {
|
|
430
|
+
return anyFalsy ? (value || defaultValue) : (value ?? defaultValue);
|
|
431
|
+
}
|
|
432
|
+
function first(value = []) {
|
|
433
|
+
return [...value][0];
|
|
434
|
+
}
|
|
435
|
+
function get(value, ...path) {
|
|
436
|
+
return path.reduce((acc, cur) => (acc && typeof acc === 'object' && (cur in acc)) ? acc[cur] : undefined, value);
|
|
437
|
+
}
|
|
438
|
+
function groupby(value = [], key) {
|
|
439
|
+
return value.reduce((o, v) => {
|
|
440
|
+
const k = v[key];
|
|
441
|
+
return {
|
|
442
|
+
...o,
|
|
443
|
+
[k]: [...(o[k] || []), v],
|
|
444
|
+
};
|
|
445
|
+
}, {});
|
|
446
|
+
}
|
|
447
|
+
function join(value = [], separator = '') {
|
|
448
|
+
return [...value].join(separator);
|
|
449
|
+
}
|
|
450
|
+
function json(value = null, indent = 0) {
|
|
451
|
+
return new Safe(JSON.stringify(value, null, indent));
|
|
452
|
+
}
|
|
453
|
+
function keys(value = {}) {
|
|
454
|
+
return Object.keys(value);
|
|
455
|
+
}
|
|
456
|
+
function last(value = []) {
|
|
457
|
+
return [...value].reverse()[0];
|
|
458
|
+
}
|
|
459
|
+
function length(value = '') {
|
|
460
|
+
return value.length;
|
|
461
|
+
}
|
|
462
|
+
function lower(value = '') {
|
|
463
|
+
return value.toLowerCase();
|
|
464
|
+
}
|
|
465
|
+
function map(value = [], key, defaultValue) {
|
|
466
|
+
return value.map(o => o[key] ?? defaultValue);
|
|
467
|
+
}
|
|
468
|
+
function max(value = 0, ...values) {
|
|
469
|
+
return Math.max(value, ...values);
|
|
470
|
+
}
|
|
471
|
+
function min(value = 0, ...values) {
|
|
472
|
+
return Math.min(value, ...values);
|
|
473
|
+
}
|
|
474
|
+
function minus(value = 0, minuend = 0) {
|
|
475
|
+
return value - minuend;
|
|
476
|
+
}
|
|
477
|
+
function odd(value = 0) {
|
|
478
|
+
return +value % 2 === 1;
|
|
479
|
+
}
|
|
480
|
+
function omit(value = {}, ...keys) {
|
|
481
|
+
return Object.entries(value).reduce((o, [k, v]) => (keys.includes(k) ? o : { ...o, [k]: v }), {});
|
|
482
|
+
}
|
|
483
|
+
function pick(value = {}, ...keys) {
|
|
484
|
+
return keys.reduce((o, k) => ({ ...o, [k]: value[k] }), {});
|
|
485
|
+
}
|
|
486
|
+
function repeat(value = '', count = 0) {
|
|
487
|
+
return value.repeat(count);
|
|
488
|
+
}
|
|
489
|
+
function replace(value = '', search, replace) {
|
|
490
|
+
return value.replace(new RegExp(search, 'g'), replace);
|
|
491
|
+
}
|
|
492
|
+
function reverse(value = []) {
|
|
493
|
+
return Array.isArray(value) ? value.reverse() : [...value].reverse().join('');
|
|
494
|
+
}
|
|
495
|
+
function safe(value = '') {
|
|
496
|
+
return new Safe(value);
|
|
497
|
+
}
|
|
498
|
+
function slice(value = '', start = 0, end) {
|
|
499
|
+
return value.slice(start, end);
|
|
500
|
+
}
|
|
501
|
+
function sort(value = []) {
|
|
502
|
+
return Array.isArray(value) ? value.sort() : [...value].sort().join('');
|
|
503
|
+
}
|
|
504
|
+
function split(value = '', separator = '') {
|
|
505
|
+
return value.split(separator);
|
|
506
|
+
}
|
|
507
|
+
function sum(value = []) {
|
|
508
|
+
return value.reduce((a, b) => a + b, 0);
|
|
509
|
+
}
|
|
510
|
+
function t(value, params) {
|
|
511
|
+
return translate(this.translations, value, params);
|
|
512
|
+
}
|
|
513
|
+
function time(value = 0, format = 'hh:mm') {
|
|
514
|
+
return formatDatetime(this.translations, value, format);
|
|
515
|
+
}
|
|
516
|
+
function trim(value = '') {
|
|
517
|
+
return value.trim();
|
|
518
|
+
}
|
|
519
|
+
function truncate(value = '', length = 0, truncateStr = '...') {
|
|
520
|
+
return value.slice(0, length) + (value.length > length ? truncateStr : '');
|
|
521
|
+
}
|
|
522
|
+
function unique(value = '') {
|
|
523
|
+
return Array.isArray(value)
|
|
524
|
+
? Array.from(new Set(value))
|
|
525
|
+
: Array.from(new Set(value)).join('');
|
|
526
|
+
}
|
|
527
|
+
function upper(value = '') {
|
|
528
|
+
return value.toUpperCase();
|
|
529
|
+
}
|
|
530
|
+
function urldecode(value = '') {
|
|
531
|
+
return decodeURIComponent(value);
|
|
532
|
+
}
|
|
533
|
+
function urlencode(value = '') {
|
|
534
|
+
return encodeURIComponent(value);
|
|
535
|
+
}
|
|
536
|
+
function values(value = {}) {
|
|
537
|
+
return Object.values(value);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
var filters = /*#__PURE__*/Object.freeze({
|
|
541
|
+
__proto__: null,
|
|
542
|
+
abs: abs,
|
|
543
|
+
add: add,
|
|
544
|
+
capitalize: capitalize,
|
|
545
|
+
ceil: ceil,
|
|
546
|
+
compact: compact,
|
|
547
|
+
date: date,
|
|
548
|
+
entries: entries,
|
|
549
|
+
escape: escape,
|
|
550
|
+
even: even,
|
|
551
|
+
fallback: fallback,
|
|
552
|
+
first: first,
|
|
553
|
+
get: get,
|
|
554
|
+
groupby: groupby,
|
|
555
|
+
join: join,
|
|
556
|
+
json: json,
|
|
557
|
+
keys: keys,
|
|
558
|
+
last: last,
|
|
559
|
+
length: length,
|
|
560
|
+
lower: lower,
|
|
561
|
+
map: map,
|
|
562
|
+
max: max,
|
|
563
|
+
min: min,
|
|
564
|
+
minus: minus,
|
|
565
|
+
odd: odd,
|
|
566
|
+
omit: omit,
|
|
567
|
+
pick: pick,
|
|
568
|
+
repeat: repeat,
|
|
569
|
+
replace: replace,
|
|
570
|
+
reverse: reverse,
|
|
571
|
+
safe: safe,
|
|
572
|
+
slice: slice,
|
|
573
|
+
sort: sort,
|
|
574
|
+
split: split,
|
|
575
|
+
sum: sum,
|
|
576
|
+
t: t,
|
|
577
|
+
time: time,
|
|
578
|
+
trim: trim,
|
|
579
|
+
truncate: truncate,
|
|
580
|
+
unique: unique,
|
|
581
|
+
upper: upper,
|
|
582
|
+
urldecode: urldecode,
|
|
583
|
+
urlencode: urlencode,
|
|
584
|
+
values: values
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
const expTokenOperators = {
|
|
588
|
+
AND: '&&',
|
|
589
|
+
OR: '||',
|
|
590
|
+
NOT: '!',
|
|
591
|
+
EQ: '===',
|
|
592
|
+
NE: '!==',
|
|
593
|
+
GT: '>',
|
|
594
|
+
LT: '<',
|
|
595
|
+
GE: '>=',
|
|
596
|
+
LE: '<=',
|
|
597
|
+
IN: ' in ',
|
|
598
|
+
NI: ' in ',
|
|
599
|
+
OF: ' of ',
|
|
600
|
+
ADD: '+',
|
|
601
|
+
SUB: '-',
|
|
602
|
+
MUL: '*',
|
|
603
|
+
DIV: '/',
|
|
604
|
+
MOD: '%',
|
|
605
|
+
PIPE: '',
|
|
606
|
+
SET: '=',
|
|
607
|
+
LP: '(',
|
|
608
|
+
RP: ')',
|
|
609
|
+
COMMA: ',',
|
|
610
|
+
DOT: '.',
|
|
611
|
+
ID: '',
|
|
612
|
+
STR: '',
|
|
613
|
+
NUM: '',
|
|
614
|
+
IF: '',
|
|
615
|
+
ELSE: '',
|
|
616
|
+
BOOL: '',
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
class ExpCompiler {
|
|
620
|
+
constructor() {
|
|
621
|
+
Object.defineProperty(this, "context", {
|
|
622
|
+
enumerable: true,
|
|
623
|
+
configurable: true,
|
|
624
|
+
writable: true,
|
|
625
|
+
value: void 0
|
|
626
|
+
});
|
|
627
|
+
Object.defineProperty(this, "filters", {
|
|
628
|
+
enumerable: true,
|
|
629
|
+
configurable: true,
|
|
630
|
+
writable: true,
|
|
631
|
+
value: void 0
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
compile(expression, context, filters) {
|
|
635
|
+
this.context = context;
|
|
636
|
+
this.filters = filters;
|
|
637
|
+
return expression ? this.compileExp(expression) : '""';
|
|
638
|
+
}
|
|
639
|
+
compileExp(expression) {
|
|
640
|
+
switch (expression.type) {
|
|
641
|
+
case 'NOT':
|
|
642
|
+
return this.compileNot(expression);
|
|
643
|
+
case 'ID':
|
|
644
|
+
return this.compileId(expression);
|
|
645
|
+
case 'STR':
|
|
646
|
+
return this.compileStr(expression);
|
|
647
|
+
case 'NUM':
|
|
648
|
+
return this.compileNum(expression);
|
|
649
|
+
case 'BOOL':
|
|
650
|
+
return this.compileBool(expression);
|
|
651
|
+
case 'PIPE':
|
|
652
|
+
return this.compilePipe(expression);
|
|
653
|
+
case 'OF':
|
|
654
|
+
return this.compileOf(expression);
|
|
655
|
+
case 'SET':
|
|
656
|
+
return this.compileSet(expression);
|
|
657
|
+
case 'AND':
|
|
658
|
+
case 'OR':
|
|
659
|
+
case 'EQ':
|
|
660
|
+
case 'NE':
|
|
661
|
+
case 'GT':
|
|
662
|
+
case 'LT':
|
|
663
|
+
case 'GE':
|
|
664
|
+
case 'LE':
|
|
665
|
+
case 'IN':
|
|
666
|
+
case 'NI':
|
|
667
|
+
case 'ADD':
|
|
668
|
+
case 'SUB':
|
|
669
|
+
case 'MUL':
|
|
670
|
+
case 'DIV':
|
|
671
|
+
case 'MOD':
|
|
672
|
+
return this.compileBinary(expression);
|
|
673
|
+
case 'IF':
|
|
674
|
+
return this.compileTernary(expression);
|
|
675
|
+
case 'SEQ':
|
|
676
|
+
return this.compileSeq(expression);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
compileNot({ argument }) {
|
|
680
|
+
return `!${this.compileExp(argument)}`;
|
|
681
|
+
}
|
|
682
|
+
compileId({ value, path, args }) {
|
|
683
|
+
let s = `${this.context}.${value}`;
|
|
684
|
+
if (path) {
|
|
685
|
+
s += path.map(p => `.${p.value}`).join('');
|
|
686
|
+
}
|
|
687
|
+
if (args) {
|
|
688
|
+
s += `(${args.map(arg => this.compileExp(arg)).join(',')})`;
|
|
689
|
+
}
|
|
690
|
+
return s;
|
|
691
|
+
}
|
|
692
|
+
compileStr({ value }) {
|
|
693
|
+
return JSON.stringify(value);
|
|
694
|
+
}
|
|
695
|
+
compileNum({ value }) {
|
|
696
|
+
return value.toString();
|
|
697
|
+
}
|
|
698
|
+
compileBool({ value }) {
|
|
699
|
+
return value.toString();
|
|
700
|
+
}
|
|
701
|
+
compileSeq({ elements }) {
|
|
702
|
+
return elements.map(element => this.compileExp(element)).toString();
|
|
703
|
+
}
|
|
704
|
+
compilePipe({ left, right }) {
|
|
705
|
+
const { value: name, args = [] } = right;
|
|
706
|
+
return `(await ${this.filters}.${name}.call(${[this.context, this.compileExp(left), ...args.map(arg => arg.type === 'SET' ? `${this.context}.${arg.left.value}=${this.compileExp(arg.right)}` : this.compileExp(arg))].join(',')}))`;
|
|
707
|
+
}
|
|
708
|
+
compileOf({ left, right }) {
|
|
709
|
+
// destructuring
|
|
710
|
+
if (left.type === 'SEQ') {
|
|
711
|
+
return `const {${left.elements.map(({ value }) => value).join(',')}} of ${this.compileExp(right)}`;
|
|
712
|
+
}
|
|
713
|
+
return `const ${left.value} of ${this.compileExp(right)}`;
|
|
714
|
+
}
|
|
715
|
+
compileSet({ left, right }) {
|
|
716
|
+
if (left.type === 'ID') {
|
|
717
|
+
// for macros
|
|
718
|
+
if (right.type === 'SEQ') {
|
|
719
|
+
return `${this.compileExp(left)}=(${(right.elements).map(el => el.type === 'SET' ? `${el.left.value}=${this.compileExp((el.right))}` : el.value).join(',')})`;
|
|
720
|
+
}
|
|
721
|
+
return `Object.assign(${this.context},{${left.value}:${this.compileExp(right)}});`;
|
|
722
|
+
}
|
|
723
|
+
// destructuring
|
|
724
|
+
return `Object.assign(${this.context},${this.filters}.pick.call(${this.context},${this.compileExp(right)},[${left.elements.map(({ value }) => `"${value}"`).join(',')}]));`;
|
|
725
|
+
}
|
|
726
|
+
compileBinary({ type, left, right }) {
|
|
727
|
+
const ret = `(${this.compileExp(left)}${expTokenOperators[type]}${this.compileExp(right)})`;
|
|
728
|
+
return type === 'NI' ? `(!${ret})` : ret;
|
|
729
|
+
}
|
|
730
|
+
compileTernary({ test, consequent, alternative }) {
|
|
731
|
+
return `(${this.compileExp(test)}?${this.compileExp(consequent)}:${alternative ? this.compileExp(alternative) : '""'})`;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const expTokenPrecedences = {
|
|
736
|
+
COMMA: 5,
|
|
737
|
+
IF: 10,
|
|
738
|
+
ELSE: 10,
|
|
739
|
+
SET: 15,
|
|
740
|
+
LP: 20,
|
|
741
|
+
RP: 20,
|
|
742
|
+
OR: 25,
|
|
743
|
+
AND: 30,
|
|
744
|
+
EQ: 40,
|
|
745
|
+
NE: 40,
|
|
746
|
+
GT: 40,
|
|
747
|
+
LT: 40,
|
|
748
|
+
GE: 40,
|
|
749
|
+
LE: 40,
|
|
750
|
+
IN: 40,
|
|
751
|
+
NI: 40,
|
|
752
|
+
OF: 40,
|
|
753
|
+
ADD: 50,
|
|
754
|
+
SUB: 50,
|
|
755
|
+
MUL: 60,
|
|
756
|
+
DIV: 60,
|
|
757
|
+
MOD: 60,
|
|
758
|
+
NOT: 70,
|
|
759
|
+
PIPE: 80,
|
|
760
|
+
DOT: 90,
|
|
761
|
+
ID: 90,
|
|
762
|
+
STR: 90,
|
|
763
|
+
NUM: 90,
|
|
764
|
+
BOOL: 90,
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
const expTokenTypes = {
|
|
768
|
+
'and': 'AND',
|
|
769
|
+
'or': 'OR',
|
|
770
|
+
'not': 'NOT',
|
|
771
|
+
'eq': 'EQ',
|
|
772
|
+
'ne': 'NE',
|
|
773
|
+
'gt': 'GT',
|
|
774
|
+
'lt': 'LT',
|
|
775
|
+
'ge': 'GE',
|
|
776
|
+
'le': 'LE',
|
|
777
|
+
'in': 'IN',
|
|
778
|
+
'ni': 'NI',
|
|
779
|
+
'of': 'OF',
|
|
780
|
+
'+': 'ADD',
|
|
781
|
+
'-': 'SUB',
|
|
782
|
+
'*': 'MUL',
|
|
783
|
+
'/': 'DIV',
|
|
784
|
+
'%': 'MOD',
|
|
785
|
+
'if': 'IF',
|
|
786
|
+
'else': 'ELSE',
|
|
787
|
+
'true': 'BOOL',
|
|
788
|
+
'false': 'BOOL',
|
|
789
|
+
'|': 'PIPE',
|
|
790
|
+
'=': 'SET',
|
|
791
|
+
'(': 'LP',
|
|
792
|
+
')': 'RP',
|
|
793
|
+
',': 'COMMA',
|
|
794
|
+
'.': 'DOT',
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
class ParseError extends SyntaxError {
|
|
798
|
+
constructor(message, options) {
|
|
799
|
+
super(message);
|
|
800
|
+
Object.defineProperty(this, "options", {
|
|
801
|
+
enumerable: true,
|
|
802
|
+
configurable: true,
|
|
803
|
+
writable: true,
|
|
804
|
+
value: options
|
|
805
|
+
});
|
|
806
|
+
this.name = 'ParseError';
|
|
807
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
808
|
+
}
|
|
809
|
+
get details() {
|
|
810
|
+
return highlightSource(this.message, this.options.source, [this.options.range]);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
class ExpTokenizer {
|
|
815
|
+
constructor() {
|
|
816
|
+
Object.defineProperty(this, "template", {
|
|
817
|
+
enumerable: true,
|
|
818
|
+
configurable: true,
|
|
819
|
+
writable: true,
|
|
820
|
+
value: ''
|
|
821
|
+
});
|
|
822
|
+
Object.defineProperty(this, "length", {
|
|
823
|
+
enumerable: true,
|
|
824
|
+
configurable: true,
|
|
825
|
+
writable: true,
|
|
826
|
+
value: 0
|
|
827
|
+
});
|
|
828
|
+
Object.defineProperty(this, "tokens", {
|
|
829
|
+
enumerable: true,
|
|
830
|
+
configurable: true,
|
|
831
|
+
writable: true,
|
|
832
|
+
value: []
|
|
833
|
+
});
|
|
834
|
+
Object.defineProperty(this, "cursor", {
|
|
835
|
+
enumerable: true,
|
|
836
|
+
configurable: true,
|
|
837
|
+
writable: true,
|
|
838
|
+
value: 0
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
tokenize(template) {
|
|
842
|
+
this.template = template;
|
|
843
|
+
this.length = template.length;
|
|
844
|
+
this.tokens = [];
|
|
845
|
+
this.cursor = 0;
|
|
846
|
+
while (this.cursor < this.length) {
|
|
847
|
+
const char = this.template[this.cursor];
|
|
848
|
+
if (isWhitespace(char)) {
|
|
849
|
+
this.cursor++;
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
if (char === '\'' || char === '"' || char === '`') {
|
|
853
|
+
this.readString(char);
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
if (isDigit(char)) {
|
|
857
|
+
this.readNumber();
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
if (isIdentifierStartChar(char)) {
|
|
861
|
+
this.readIdentifier();
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
if (isSymbol(char)) {
|
|
865
|
+
this.readSymbol(char);
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
throw new ParseError(`unexpect "${char}"`, { source: template, range: {
|
|
869
|
+
start: this.cursor,
|
|
870
|
+
end: this.cursor + 1,
|
|
871
|
+
} });
|
|
872
|
+
}
|
|
873
|
+
return this.tokens;
|
|
874
|
+
}
|
|
875
|
+
readString(quoteChar) {
|
|
876
|
+
let value = '';
|
|
877
|
+
let escaped = false;
|
|
878
|
+
const start = this.cursor;
|
|
879
|
+
// Skip opening quote char
|
|
880
|
+
this.cursor++;
|
|
881
|
+
while (this.cursor < this.length) {
|
|
882
|
+
const char = this.template[this.cursor];
|
|
883
|
+
if (escaped) {
|
|
884
|
+
value += char;
|
|
885
|
+
escaped = false;
|
|
886
|
+
}
|
|
887
|
+
else if (char === '\\') {
|
|
888
|
+
value += char;
|
|
889
|
+
escaped = true;
|
|
890
|
+
}
|
|
891
|
+
else if (char === quoteChar) {
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
else {
|
|
895
|
+
value += char;
|
|
896
|
+
}
|
|
897
|
+
this.cursor++;
|
|
898
|
+
}
|
|
899
|
+
// skip closing quote char
|
|
900
|
+
this.cursor++;
|
|
901
|
+
this.tokens.push({
|
|
902
|
+
type: 'STR',
|
|
903
|
+
value,
|
|
904
|
+
start,
|
|
905
|
+
end: this.cursor,
|
|
906
|
+
raw: `${quoteChar}${value}${quoteChar}`,
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
readNumber() {
|
|
910
|
+
let value = '';
|
|
911
|
+
let hasDot = false;
|
|
912
|
+
const start = this.cursor;
|
|
913
|
+
while (this.cursor < this.length) {
|
|
914
|
+
const char = this.template[this.cursor];
|
|
915
|
+
if (isDigit(char)) {
|
|
916
|
+
value += char;
|
|
917
|
+
}
|
|
918
|
+
else if (char === '.' && !hasDot) {
|
|
919
|
+
value += char;
|
|
920
|
+
hasDot = true;
|
|
921
|
+
}
|
|
922
|
+
else {
|
|
923
|
+
break;
|
|
924
|
+
}
|
|
925
|
+
this.cursor++;
|
|
926
|
+
}
|
|
927
|
+
this.tokens.push({
|
|
928
|
+
type: 'NUM',
|
|
929
|
+
value: Number.parseFloat(value),
|
|
930
|
+
start,
|
|
931
|
+
end: this.cursor,
|
|
932
|
+
raw: value,
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
readIdentifier() {
|
|
936
|
+
let value = '';
|
|
937
|
+
const start = this.cursor;
|
|
938
|
+
while (this.cursor < this.length) {
|
|
939
|
+
const char = this.template[this.cursor];
|
|
940
|
+
if (isIdentifierChar(char)) {
|
|
941
|
+
value += char;
|
|
942
|
+
this.cursor++;
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
const type = value in expTokenTypes ? expTokenTypes[value] : 'ID';
|
|
948
|
+
this.tokens.push({
|
|
949
|
+
type,
|
|
950
|
+
value: type === 'BOOL' ? value === 'true' : value,
|
|
951
|
+
start,
|
|
952
|
+
end: this.cursor,
|
|
953
|
+
raw: value,
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
readSymbol(char) {
|
|
957
|
+
const start = this.cursor;
|
|
958
|
+
this.tokens.push({
|
|
959
|
+
type: expTokenTypes[char],
|
|
960
|
+
value: char,
|
|
961
|
+
start,
|
|
962
|
+
end: this.cursor,
|
|
963
|
+
raw: char,
|
|
964
|
+
});
|
|
965
|
+
this.cursor++;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
function isWhitespace(char) {
|
|
969
|
+
return /\s/.test(char);
|
|
970
|
+
}
|
|
971
|
+
function isSymbol(char) {
|
|
972
|
+
return /[+\-*/%|=(),.]/.test(char);
|
|
973
|
+
}
|
|
974
|
+
function isDigit(char) {
|
|
975
|
+
return /[-\d]/.test(char);
|
|
976
|
+
}
|
|
977
|
+
function isIdentifierStartChar(char) {
|
|
978
|
+
return /[a-z_$]/i.test(char);
|
|
979
|
+
}
|
|
980
|
+
function isIdentifierChar(char) {
|
|
981
|
+
return /[\w$]/.test(char);
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
class ExpParser {
|
|
985
|
+
constructor() {
|
|
986
|
+
Object.defineProperty(this, "template", {
|
|
987
|
+
enumerable: true,
|
|
988
|
+
configurable: true,
|
|
989
|
+
writable: true,
|
|
990
|
+
value: ''
|
|
991
|
+
});
|
|
992
|
+
Object.defineProperty(this, "tokens", {
|
|
993
|
+
enumerable: true,
|
|
994
|
+
configurable: true,
|
|
995
|
+
writable: true,
|
|
996
|
+
value: []
|
|
997
|
+
});
|
|
998
|
+
Object.defineProperty(this, "length", {
|
|
999
|
+
enumerable: true,
|
|
1000
|
+
configurable: true,
|
|
1001
|
+
writable: true,
|
|
1002
|
+
value: 0
|
|
1003
|
+
});
|
|
1004
|
+
Object.defineProperty(this, "cursor", {
|
|
1005
|
+
enumerable: true,
|
|
1006
|
+
configurable: true,
|
|
1007
|
+
writable: true,
|
|
1008
|
+
value: 0
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
parse(template) {
|
|
1012
|
+
this.template = template;
|
|
1013
|
+
this.tokens = new ExpTokenizer().tokenize(template);
|
|
1014
|
+
this.length = this.tokens.length;
|
|
1015
|
+
this.cursor = 0;
|
|
1016
|
+
return this.template ? this.parseExp() : null;
|
|
1017
|
+
}
|
|
1018
|
+
parseExp(checker) {
|
|
1019
|
+
let left = null;
|
|
1020
|
+
while (true) {
|
|
1021
|
+
const token = this.next();
|
|
1022
|
+
if (!token) {
|
|
1023
|
+
break;
|
|
1024
|
+
}
|
|
1025
|
+
const check = checker?.(token);
|
|
1026
|
+
if (check === 'BACK') {
|
|
1027
|
+
this.back();
|
|
1028
|
+
break;
|
|
1029
|
+
}
|
|
1030
|
+
if (check === 'BREAK') {
|
|
1031
|
+
break;
|
|
1032
|
+
}
|
|
1033
|
+
if (token.type === 'LP') {
|
|
1034
|
+
if (!this.peek()) {
|
|
1035
|
+
throw new ParseError(`unexpected end of expression`, {
|
|
1036
|
+
source: this.template,
|
|
1037
|
+
range: {
|
|
1038
|
+
start: token.start,
|
|
1039
|
+
end: token.end,
|
|
1040
|
+
},
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
const elements = this.parseSequence();
|
|
1044
|
+
const rp = this.consume('RP');
|
|
1045
|
+
if (!rp) {
|
|
1046
|
+
throw new ParseError(`expected "RP" after "LP"`, {
|
|
1047
|
+
source: this.template,
|
|
1048
|
+
range: {
|
|
1049
|
+
start: token.start,
|
|
1050
|
+
end: token.end,
|
|
1051
|
+
},
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
left = {
|
|
1055
|
+
...token,
|
|
1056
|
+
type: 'SEQ',
|
|
1057
|
+
elements,
|
|
1058
|
+
end: rp.end,
|
|
1059
|
+
};
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1062
|
+
if (token.type === 'NOT') {
|
|
1063
|
+
if (!this.peek()) {
|
|
1064
|
+
throw new ParseError(`unexpected end of expression`, {
|
|
1065
|
+
source: this.template,
|
|
1066
|
+
range: {
|
|
1067
|
+
start: token.start,
|
|
1068
|
+
end: token.end,
|
|
1069
|
+
},
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
left = {
|
|
1073
|
+
...token,
|
|
1074
|
+
type: 'NOT',
|
|
1075
|
+
argument: this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined),
|
|
1076
|
+
};
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
if (token.type === 'ID') {
|
|
1080
|
+
left = {
|
|
1081
|
+
...token,
|
|
1082
|
+
type: 'ID',
|
|
1083
|
+
value: token.value,
|
|
1084
|
+
};
|
|
1085
|
+
if (this.check('DOT')) {
|
|
1086
|
+
left.path = this.parsePath();
|
|
1087
|
+
}
|
|
1088
|
+
if (this.consume('LP')) {
|
|
1089
|
+
left.args = this.parseSequence();
|
|
1090
|
+
const rp = this.consume('RP');
|
|
1091
|
+
if (!rp) {
|
|
1092
|
+
throw new ParseError(`expected "RP" after "LP"`, {
|
|
1093
|
+
source: this.template,
|
|
1094
|
+
range: {
|
|
1095
|
+
start: token.start,
|
|
1096
|
+
end: token.end,
|
|
1097
|
+
},
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
if (token.type === 'STR') {
|
|
1105
|
+
left = {
|
|
1106
|
+
...token,
|
|
1107
|
+
type: 'STR',
|
|
1108
|
+
value: token.value,
|
|
1109
|
+
};
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
if (token.type === 'NUM') {
|
|
1113
|
+
left = {
|
|
1114
|
+
...token,
|
|
1115
|
+
type: 'NUM',
|
|
1116
|
+
value: token.value,
|
|
1117
|
+
};
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
if (token.type === 'BOOL') {
|
|
1121
|
+
left = {
|
|
1122
|
+
...token,
|
|
1123
|
+
type: 'BOOL',
|
|
1124
|
+
value: token.value,
|
|
1125
|
+
};
|
|
1126
|
+
continue;
|
|
1127
|
+
}
|
|
1128
|
+
if (token.type === 'IF') {
|
|
1129
|
+
const test = this.parseExp(t => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
1130
|
+
if (!test) {
|
|
1131
|
+
throw new ParseError('expected test expression', {
|
|
1132
|
+
source: this.template,
|
|
1133
|
+
range: {
|
|
1134
|
+
start: token.start,
|
|
1135
|
+
end: token.end,
|
|
1136
|
+
},
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
left = {
|
|
1140
|
+
...token,
|
|
1141
|
+
type: 'IF',
|
|
1142
|
+
test,
|
|
1143
|
+
consequent: left,
|
|
1144
|
+
};
|
|
1145
|
+
if (this.consume('ELSE')) {
|
|
1146
|
+
const alternative = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
1147
|
+
if (!alternative) {
|
|
1148
|
+
throw new ParseError('expected alternative expression', {
|
|
1149
|
+
source: this.template,
|
|
1150
|
+
range: {
|
|
1151
|
+
start: token.start,
|
|
1152
|
+
end: token.end,
|
|
1153
|
+
},
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
left.alternative = alternative;
|
|
1157
|
+
}
|
|
1158
|
+
continue;
|
|
1159
|
+
}
|
|
1160
|
+
if (token.type === 'AND'
|
|
1161
|
+
|| token.type === 'OR'
|
|
1162
|
+
|| token.type === 'EQ'
|
|
1163
|
+
|| token.type === 'NE'
|
|
1164
|
+
|| token.type === 'GT'
|
|
1165
|
+
|| token.type === 'LT'
|
|
1166
|
+
|| token.type === 'GE'
|
|
1167
|
+
|| token.type === 'LE'
|
|
1168
|
+
|| token.type === 'IN'
|
|
1169
|
+
|| token.type === 'NI'
|
|
1170
|
+
|| token.type === 'OF'
|
|
1171
|
+
|| token.type === 'ADD'
|
|
1172
|
+
|| token.type === 'SUB'
|
|
1173
|
+
|| token.type === 'MUL'
|
|
1174
|
+
|| token.type === 'DIV'
|
|
1175
|
+
|| token.type === 'MOD'
|
|
1176
|
+
|| token.type === 'SET') {
|
|
1177
|
+
if (!left) {
|
|
1178
|
+
throw new ParseError(`no left operand for "${token.type}"`, {
|
|
1179
|
+
source: this.template,
|
|
1180
|
+
range: {
|
|
1181
|
+
start: token.start,
|
|
1182
|
+
end: token.end,
|
|
1183
|
+
},
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
const right = this.parseExp(t => isHigher(token, t) ? 'BACK' : undefined);
|
|
1187
|
+
if (!right) {
|
|
1188
|
+
throw new ParseError(`no right operand for "${token.type}"`, {
|
|
1189
|
+
source: this.template,
|
|
1190
|
+
range: {
|
|
1191
|
+
start: token.start,
|
|
1192
|
+
end: token.end,
|
|
1193
|
+
},
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
left = {
|
|
1197
|
+
...token,
|
|
1198
|
+
type: token.type,
|
|
1199
|
+
left,
|
|
1200
|
+
right,
|
|
1201
|
+
};
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
if (token.type === 'PIPE') {
|
|
1205
|
+
if (!left) {
|
|
1206
|
+
throw new ParseError(`no left operand for "${token.type}"`, {
|
|
1207
|
+
source: this.template,
|
|
1208
|
+
range: {
|
|
1209
|
+
start: token.start,
|
|
1210
|
+
end: token.end,
|
|
1211
|
+
},
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
if (!this.check('ID')) {
|
|
1215
|
+
throw new ParseError(`expected "ID" after "PIPE"`, {
|
|
1216
|
+
source: this.template,
|
|
1217
|
+
range: {
|
|
1218
|
+
start: token.start,
|
|
1219
|
+
end: token.end,
|
|
1220
|
+
},
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
left = {
|
|
1224
|
+
...token,
|
|
1225
|
+
type: 'PIPE',
|
|
1226
|
+
left,
|
|
1227
|
+
right: this.parsePipe(),
|
|
1228
|
+
};
|
|
1229
|
+
continue;
|
|
1230
|
+
}
|
|
1231
|
+
this.back();
|
|
1232
|
+
break;
|
|
1233
|
+
}
|
|
1234
|
+
return left;
|
|
1235
|
+
}
|
|
1236
|
+
parsePath() {
|
|
1237
|
+
const ids = [];
|
|
1238
|
+
while (true) {
|
|
1239
|
+
const dot = this.consume('DOT');
|
|
1240
|
+
if (!dot) {
|
|
1241
|
+
break;
|
|
1242
|
+
}
|
|
1243
|
+
const token = this.next();
|
|
1244
|
+
if (!token) {
|
|
1245
|
+
throw new ParseError(`expected "ID" after "DOT"`, {
|
|
1246
|
+
source: this.template,
|
|
1247
|
+
range: {
|
|
1248
|
+
start: dot.start,
|
|
1249
|
+
end: dot.end,
|
|
1250
|
+
},
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
if (token.type !== 'ID') {
|
|
1254
|
+
throw new ParseError(`expected "ID" after "DOT"`, {
|
|
1255
|
+
source: this.template,
|
|
1256
|
+
range: {
|
|
1257
|
+
start: token.start,
|
|
1258
|
+
end: token.end,
|
|
1259
|
+
},
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
ids.push({
|
|
1263
|
+
...token,
|
|
1264
|
+
type: 'ID',
|
|
1265
|
+
value: token.value,
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
return ids;
|
|
1269
|
+
}
|
|
1270
|
+
parseSequence() {
|
|
1271
|
+
const elements = [];
|
|
1272
|
+
while (!this.check('RP')) {
|
|
1273
|
+
const element = this.parseExp(t => t.type === 'COMMA' ? 'BREAK' : undefined);
|
|
1274
|
+
if (element) {
|
|
1275
|
+
elements.push(element);
|
|
1276
|
+
}
|
|
1277
|
+
else {
|
|
1278
|
+
break;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return elements;
|
|
1282
|
+
}
|
|
1283
|
+
parsePipe() {
|
|
1284
|
+
const token = this.next();
|
|
1285
|
+
const right = {
|
|
1286
|
+
...token,
|
|
1287
|
+
type: 'ID',
|
|
1288
|
+
value: token.value,
|
|
1289
|
+
};
|
|
1290
|
+
if (this.consume('LP')) {
|
|
1291
|
+
const args = this.parseSequence();
|
|
1292
|
+
if (args.length) {
|
|
1293
|
+
right.args = args;
|
|
1294
|
+
}
|
|
1295
|
+
const rp = this.consume('RP');
|
|
1296
|
+
if (!rp) {
|
|
1297
|
+
throw new ParseError(`expected "RP" after "LP"`, {
|
|
1298
|
+
source: this.template,
|
|
1299
|
+
range: {
|
|
1300
|
+
start: token.start,
|
|
1301
|
+
end: token.end,
|
|
1302
|
+
},
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
return right;
|
|
1307
|
+
}
|
|
1308
|
+
next() {
|
|
1309
|
+
return this.tokens[this.cursor++] ?? null;
|
|
1310
|
+
}
|
|
1311
|
+
back() {
|
|
1312
|
+
return this.tokens[this.cursor--];
|
|
1313
|
+
}
|
|
1314
|
+
peek() {
|
|
1315
|
+
return this.tokens[this.cursor] ?? null;
|
|
1316
|
+
}
|
|
1317
|
+
check(type) {
|
|
1318
|
+
return this.peek()?.type === type;
|
|
1319
|
+
}
|
|
1320
|
+
consume(type) {
|
|
1321
|
+
if (this.check(type)) {
|
|
1322
|
+
return this.next();
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
function isHigher(a, b) {
|
|
1327
|
+
return expTokenPrecedences[a.type] > expTokenPrecedences[b.type];
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
const compiler = new ExpCompiler();
|
|
1331
|
+
const parser = new ExpParser();
|
|
1332
|
+
|
|
1333
|
+
const CALL = 'call';
|
|
1334
|
+
const ENDCALL = 'endcall';
|
|
1335
|
+
/**
|
|
1336
|
+
* @example {{ call my_macro(x, "a", 1) }}...{{ endcall }}
|
|
1337
|
+
*/
|
|
1338
|
+
const tag$7 = {
|
|
1339
|
+
names: [CALL, ENDCALL],
|
|
1340
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1341
|
+
if (name === CALL) {
|
|
1342
|
+
if (value?.type !== 'ID') {
|
|
1343
|
+
throw new Error(`"${CALL}" tag must specify a "macro" name`);
|
|
1344
|
+
}
|
|
1345
|
+
ctx.expect(ENDCALL);
|
|
1346
|
+
const { context } = ctx;
|
|
1347
|
+
return out.pushLine(`await ${compiler.compile(value, context, FILTERS)}(async()=>{`);
|
|
1348
|
+
}
|
|
1349
|
+
if (name === ENDCALL) {
|
|
1350
|
+
if (!ctx.consume(ENDCALL)) {
|
|
1351
|
+
throw new Error(`unexpected "${ENDCALL}"`);
|
|
1352
|
+
}
|
|
1353
|
+
return out.pushLine(`});`);
|
|
1354
|
+
}
|
|
1355
|
+
},
|
|
1356
|
+
};
|
|
1357
|
+
|
|
1358
|
+
const COMMENT = 'comment';
|
|
1359
|
+
const ENDCOMMENT = 'endcomment';
|
|
1360
|
+
/**
|
|
1361
|
+
* @example {{# This is a comment }}
|
|
1362
|
+
* @example {{ comment }} This is a comment {{ endcomment }}
|
|
1363
|
+
*/
|
|
1364
|
+
const tag$6 = {
|
|
1365
|
+
names: [COMMENT, '#', ENDCOMMENT],
|
|
1366
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1367
|
+
// inline comment
|
|
1368
|
+
if (name === '#') {
|
|
1369
|
+
if (!value.value || out.options.stripComments) {
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1372
|
+
return out.pushStr(`<!--${value.value}-->`);
|
|
1373
|
+
}
|
|
1374
|
+
// block comment
|
|
1375
|
+
if (name === COMMENT) {
|
|
1376
|
+
ctx.expect(ENDCOMMENT);
|
|
1377
|
+
return out.pushStr(`<!--`);
|
|
1378
|
+
}
|
|
1379
|
+
if (name === ENDCOMMENT) {
|
|
1380
|
+
if (!ctx.consume(ENDCOMMENT)) {
|
|
1381
|
+
throw new Error(`unexpected "${ENDCOMMENT}"`);
|
|
1382
|
+
}
|
|
1383
|
+
return out.pushStr(`-->`);
|
|
1384
|
+
}
|
|
1385
|
+
},
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
const EXPRESSION = '=';
|
|
1389
|
+
/**
|
|
1390
|
+
* @example {{= x | f }}
|
|
1391
|
+
* @example {{= 'a' if x else 'b' }}
|
|
1392
|
+
*/
|
|
1393
|
+
const tag$5 = {
|
|
1394
|
+
names: [EXPRESSION],
|
|
1395
|
+
compile({ token: { value }, ctx: { context }, out }) {
|
|
1396
|
+
return out.pushVar(compiler.compile(value, context, FILTERS));
|
|
1397
|
+
},
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
const FOR = 'for';
|
|
1401
|
+
const BREAK = 'break';
|
|
1402
|
+
const CONTINUE = 'continue';
|
|
1403
|
+
const ENDFOR = 'endfor';
|
|
1404
|
+
/**
|
|
1405
|
+
* @example {{ for item in items }}{{= item }}{{ endfor }}
|
|
1406
|
+
* @example {{ for (x, y) in items }}{{= x }},{{= y }}{{ endfor }}
|
|
1407
|
+
*/
|
|
1408
|
+
const tag$4 = {
|
|
1409
|
+
names: [FOR, BREAK, CONTINUE, ENDFOR],
|
|
1410
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1411
|
+
if (name === FOR) {
|
|
1412
|
+
if (!value) {
|
|
1413
|
+
throw new Error(`"${FOR}" tag must have expression`);
|
|
1414
|
+
}
|
|
1415
|
+
ctx.expect(ENDFOR);
|
|
1416
|
+
const { context } = ctx;
|
|
1417
|
+
if (value.left.type === 'SEQ') {
|
|
1418
|
+
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.elements.map(({ value }) => value).join(',')}`, `};`);
|
|
1419
|
+
}
|
|
1420
|
+
return out.pushLine(`{`, `for(${compiler.compile(value, context, FILTERS)}){`, `const ${ctx.in()}={`, `...${context},`, `${value.left.value},`, `};`);
|
|
1421
|
+
}
|
|
1422
|
+
if (name === BREAK) {
|
|
1423
|
+
if (!ctx.matchAny(ENDFOR)) {
|
|
1424
|
+
throw new Error(`"${BREAK}" tag must inside a "${FOR}" loop`);
|
|
1425
|
+
}
|
|
1426
|
+
return out.pushLine(`break;`);
|
|
1427
|
+
}
|
|
1428
|
+
if (name === CONTINUE) {
|
|
1429
|
+
if (!ctx.matchAny(ENDFOR)) {
|
|
1430
|
+
throw new Error(`"${CONTINUE}" tag must inside a "${FOR}" loop`);
|
|
1431
|
+
}
|
|
1432
|
+
return out.pushLine(`continue;`);
|
|
1433
|
+
}
|
|
1434
|
+
if (name === ENDFOR) {
|
|
1435
|
+
if (!ctx.consume(ENDFOR)) {
|
|
1436
|
+
throw new Error(`unexpected "${ENDFOR}"`);
|
|
1437
|
+
}
|
|
1438
|
+
ctx.out();
|
|
1439
|
+
return out.pushLine(`}}`);
|
|
1440
|
+
}
|
|
1441
|
+
},
|
|
1442
|
+
};
|
|
1443
|
+
|
|
1444
|
+
const IF = 'if';
|
|
1445
|
+
const ELIF = 'elif';
|
|
1446
|
+
const ELSE = 'else';
|
|
1447
|
+
const ENDIF = 'endif';
|
|
1448
|
+
/**
|
|
1449
|
+
* @example {{ if my_var | my_filter }}yes{{ else }}no{{ endif }}
|
|
1450
|
+
*/
|
|
1451
|
+
const tag$3 = {
|
|
1452
|
+
names: [IF, ELIF, ELSE, ENDIF],
|
|
1453
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1454
|
+
if (name === IF) {
|
|
1455
|
+
if (!value) {
|
|
1456
|
+
throw new Error(`"${IF}" tag must have expression`);
|
|
1457
|
+
}
|
|
1458
|
+
ctx.expect(ENDIF);
|
|
1459
|
+
const { context } = ctx;
|
|
1460
|
+
return out.pushLine(`if(${compiler.compile(value, context, FILTERS)}){`);
|
|
1461
|
+
}
|
|
1462
|
+
if (name === ELIF) {
|
|
1463
|
+
if (!value) {
|
|
1464
|
+
throw new Error(`"${ELIF}" tag must have expression`);
|
|
1465
|
+
}
|
|
1466
|
+
if (!ctx.match(ENDIF)) {
|
|
1467
|
+
throw new Error(`"${ELIF}" tag must follow "${IF}" tag`);
|
|
1468
|
+
}
|
|
1469
|
+
const { context } = ctx;
|
|
1470
|
+
return out.pushLine(`}else if(${compiler.compile(value, context, FILTERS)}){`);
|
|
1471
|
+
}
|
|
1472
|
+
if (name === ELSE) {
|
|
1473
|
+
if (!ctx.match(ENDIF)) {
|
|
1474
|
+
throw new Error(`"${ELSE}" tag must follow "${IF}" tag`);
|
|
1475
|
+
}
|
|
1476
|
+
return out.pushLine(`}else{`);
|
|
1477
|
+
}
|
|
1478
|
+
if (name === ENDIF) {
|
|
1479
|
+
if (!ctx.consume(ENDIF)) {
|
|
1480
|
+
throw new Error(`unexpected "${ENDIF}"`);
|
|
1481
|
+
}
|
|
1482
|
+
return out.pushLine(`}`);
|
|
1483
|
+
}
|
|
1484
|
+
},
|
|
1485
|
+
};
|
|
1486
|
+
|
|
1487
|
+
const MACRO = 'macro';
|
|
1488
|
+
const CALLER = 'caller';
|
|
1489
|
+
const ENDMACRO = 'endmacro';
|
|
1490
|
+
/**
|
|
1491
|
+
* @example {{ macro my_macro = (x, y) }}...{{ caller }}{{ endmacro }}{{ my_macro("foo", 1) }}
|
|
1492
|
+
*/
|
|
1493
|
+
const tag$2 = {
|
|
1494
|
+
names: [MACRO, CALLER, ENDMACRO],
|
|
1495
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1496
|
+
if (name === MACRO) {
|
|
1497
|
+
if (value?.type !== 'SET') {
|
|
1498
|
+
throw new Error(`"${MACRO}" tag must have "SET" expression`);
|
|
1499
|
+
}
|
|
1500
|
+
ctx.expect(ENDMACRO);
|
|
1501
|
+
const { context } = ctx;
|
|
1502
|
+
return out.pushLine(`${compiler.compile(value, context, FILTERS)}=>async(_c)=>{`, `const ${ctx.in()}={`, `...${context},`, `${(value.right.elements).map(el => el.type === 'SET' ? `${el.left.value}` : el.value).join(',')}`, `};`);
|
|
1503
|
+
}
|
|
1504
|
+
if (name === CALLER) {
|
|
1505
|
+
if (!ctx.matchAny(ENDMACRO)) {
|
|
1506
|
+
throw new Error(`"${CALLER}" tag must inside a "${MACRO}" tag`);
|
|
1507
|
+
}
|
|
1508
|
+
return out.pushLine(`await _c?.();`);
|
|
1509
|
+
}
|
|
1510
|
+
if (name === ENDMACRO) {
|
|
1511
|
+
if (!ctx.consume(ENDMACRO)) {
|
|
1512
|
+
throw new Error(`unexpected "${ENDMACRO}"`);
|
|
1513
|
+
}
|
|
1514
|
+
ctx.out();
|
|
1515
|
+
return out.pushLine(`};`);
|
|
1516
|
+
}
|
|
1517
|
+
},
|
|
1518
|
+
};
|
|
1519
|
+
|
|
1520
|
+
const RAW = 'raw';
|
|
1521
|
+
/**
|
|
1522
|
+
* @example hello world
|
|
1523
|
+
*/
|
|
1524
|
+
const tag$1 = {
|
|
1525
|
+
names: [RAW],
|
|
1526
|
+
async compile({ token: { raw, previous, next }, out }) {
|
|
1527
|
+
return out.pushStr(raw, {
|
|
1528
|
+
trimStart: previous?.stripAfter,
|
|
1529
|
+
trimEnd: next?.stripBefore,
|
|
1530
|
+
});
|
|
1531
|
+
},
|
|
1532
|
+
};
|
|
1533
|
+
|
|
1534
|
+
const SET = 'set';
|
|
1535
|
+
const ENDSET = 'endset';
|
|
1536
|
+
/**
|
|
1537
|
+
* @example {{ set left = right }}
|
|
1538
|
+
* @example {{ set variable }} this is {{= my_value }} {{ endset }}
|
|
1539
|
+
*/
|
|
1540
|
+
const tag = {
|
|
1541
|
+
names: [SET, ENDSET],
|
|
1542
|
+
async compile({ token: { name, value }, ctx, out }) {
|
|
1543
|
+
if (name === SET) {
|
|
1544
|
+
if (!value) {
|
|
1545
|
+
throw new Error(`"${SET}" tag must have expression`);
|
|
1546
|
+
}
|
|
1547
|
+
const { context } = ctx;
|
|
1548
|
+
// inline assignment
|
|
1549
|
+
if (value.right) {
|
|
1550
|
+
return out.pushLine(compiler.compile(value, CONTEXT, FILTERS));
|
|
1551
|
+
}
|
|
1552
|
+
ctx.expect(ENDSET);
|
|
1553
|
+
// block assignment
|
|
1554
|
+
return out.pushLine(`Object.assign(${context},{`, `${value.value}:await(async(s)=>{`);
|
|
1555
|
+
}
|
|
1556
|
+
if (name === ENDSET) {
|
|
1557
|
+
if (!ctx.consume(ENDSET)) {
|
|
1558
|
+
throw new Error(`unexpected "${ENDSET}"`);
|
|
1559
|
+
}
|
|
1560
|
+
return out.pushLine(`return s;})("")});`);
|
|
1561
|
+
}
|
|
1562
|
+
},
|
|
1563
|
+
};
|
|
1564
|
+
|
|
1565
|
+
const compilers = [
|
|
1566
|
+
tag,
|
|
1567
|
+
tag$7,
|
|
1568
|
+
tag$6,
|
|
1569
|
+
tag$5,
|
|
1570
|
+
tag$4,
|
|
1571
|
+
tag$3,
|
|
1572
|
+
tag$2,
|
|
1573
|
+
tag$1,
|
|
1574
|
+
].reduce((acc, compiler) => {
|
|
1575
|
+
for (const name of compiler.names) {
|
|
1576
|
+
acc[name] = [...(acc[name] || []), compiler];
|
|
1577
|
+
}
|
|
1578
|
+
return acc;
|
|
1579
|
+
}, {});
|
|
1580
|
+
|
|
1581
|
+
const config = {
|
|
1582
|
+
globals: {
|
|
1583
|
+
translations: {},
|
|
1584
|
+
},
|
|
1585
|
+
filters: { ...filters },
|
|
1586
|
+
compilers: { ...compilers },
|
|
1587
|
+
autoEscape: true,
|
|
1588
|
+
strictMode: true,
|
|
1589
|
+
stripComments: false,
|
|
1590
|
+
trimWhitespace: false,
|
|
1591
|
+
};
|
|
1592
|
+
|
|
1593
|
+
function unescapeTag(v) {
|
|
1594
|
+
return v.replace(/\\(\{|\})/g, '$1');
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
class Parser {
|
|
1598
|
+
constructor(options) {
|
|
1599
|
+
Object.defineProperty(this, "options", {
|
|
1600
|
+
enumerable: true,
|
|
1601
|
+
configurable: true,
|
|
1602
|
+
writable: true,
|
|
1603
|
+
value: options
|
|
1604
|
+
});
|
|
1605
|
+
Object.defineProperty(this, "template", {
|
|
1606
|
+
enumerable: true,
|
|
1607
|
+
configurable: true,
|
|
1608
|
+
writable: true,
|
|
1609
|
+
value: ''
|
|
1610
|
+
});
|
|
1611
|
+
Object.defineProperty(this, "index", {
|
|
1612
|
+
enumerable: true,
|
|
1613
|
+
configurable: true,
|
|
1614
|
+
writable: true,
|
|
1615
|
+
value: 0
|
|
1616
|
+
});
|
|
1617
|
+
Object.defineProperty(this, "first", {
|
|
1618
|
+
enumerable: true,
|
|
1619
|
+
configurable: true,
|
|
1620
|
+
writable: true,
|
|
1621
|
+
value: null
|
|
1622
|
+
});
|
|
1623
|
+
Object.defineProperty(this, "group", {
|
|
1624
|
+
enumerable: true,
|
|
1625
|
+
configurable: true,
|
|
1626
|
+
writable: true,
|
|
1627
|
+
value: ''
|
|
1628
|
+
});
|
|
1629
|
+
Object.defineProperty(this, "blocks", {
|
|
1630
|
+
enumerable: true,
|
|
1631
|
+
configurable: true,
|
|
1632
|
+
writable: true,
|
|
1633
|
+
value: {}
|
|
1634
|
+
});
|
|
1635
|
+
Object.defineProperty(this, "cursor", {
|
|
1636
|
+
enumerable: true,
|
|
1637
|
+
configurable: true,
|
|
1638
|
+
writable: true,
|
|
1639
|
+
value: null
|
|
1640
|
+
});
|
|
1641
|
+
}
|
|
1642
|
+
async parse(template) {
|
|
1643
|
+
this.template = template;
|
|
1644
|
+
this.index = 0;
|
|
1645
|
+
this.blocks = {};
|
|
1646
|
+
const tagRe = /\{\{(-)?([=#])?((?:\\[{}]|(?!\{\}).)+?)(-)?\}\}/gs;
|
|
1647
|
+
let match;
|
|
1648
|
+
while ((match = tagRe.exec(template))) {
|
|
1649
|
+
if (match.index > this.index) {
|
|
1650
|
+
this.strToken(this.index, match.index);
|
|
1651
|
+
}
|
|
1652
|
+
await this.tagToken(match);
|
|
1653
|
+
this.index = match.index + match[0].length;
|
|
1654
|
+
}
|
|
1655
|
+
if (this.index < template.length) {
|
|
1656
|
+
this.strToken(this.index, template.length);
|
|
1657
|
+
}
|
|
1658
|
+
this.cursor = this.first;
|
|
1659
|
+
this.rebuildBlocks();
|
|
1660
|
+
return this.cursor;
|
|
1661
|
+
}
|
|
1662
|
+
strToken(start, end) {
|
|
1663
|
+
const raw = this.template.slice(start, end);
|
|
1664
|
+
this.push({
|
|
1665
|
+
name: RAW$1,
|
|
1666
|
+
raw,
|
|
1667
|
+
previous: null,
|
|
1668
|
+
next: null,
|
|
1669
|
+
start,
|
|
1670
|
+
end,
|
|
1671
|
+
});
|
|
1672
|
+
}
|
|
1673
|
+
async tagToken(match) {
|
|
1674
|
+
const start = match.index;
|
|
1675
|
+
const end = start + match[0].length;
|
|
1676
|
+
const base = {
|
|
1677
|
+
raw: match[0],
|
|
1678
|
+
previous: null,
|
|
1679
|
+
next: null,
|
|
1680
|
+
stripBefore: match[1] === '-',
|
|
1681
|
+
stripAfter: match[4] === '-',
|
|
1682
|
+
start,
|
|
1683
|
+
end,
|
|
1684
|
+
};
|
|
1685
|
+
if (match[2] === '#') {
|
|
1686
|
+
this.push({
|
|
1687
|
+
...base,
|
|
1688
|
+
name: match[2],
|
|
1689
|
+
value: {
|
|
1690
|
+
type: 'STR',
|
|
1691
|
+
value: unescapeTag(match[3]),
|
|
1692
|
+
},
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
else if (match[2] === '=') {
|
|
1696
|
+
this.push({
|
|
1697
|
+
...base,
|
|
1698
|
+
name: match[2],
|
|
1699
|
+
value: parser.parse(unescapeTag(match[3])),
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
else {
|
|
1703
|
+
const [, name, value = ''] = match[3].trim().match(/^([a-z]+)(?: (.+))?$/) ?? [];
|
|
1704
|
+
if (name === LAYOUT) {
|
|
1705
|
+
await this.layout(value);
|
|
1706
|
+
}
|
|
1707
|
+
else if (name === INCLUDE) {
|
|
1708
|
+
await this.include(value);
|
|
1709
|
+
}
|
|
1710
|
+
else {
|
|
1711
|
+
this.push({
|
|
1712
|
+
...base,
|
|
1713
|
+
name,
|
|
1714
|
+
value: parser.parse(unescapeTag(value)),
|
|
1715
|
+
});
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
async layout(value) {
|
|
1720
|
+
const exp = parser.parse(value);
|
|
1721
|
+
if (exp?.type !== 'STR' || !exp.value) {
|
|
1722
|
+
throw new ParseError(`"layout" tag must have a file path`, {
|
|
1723
|
+
source: this.template,
|
|
1724
|
+
range: {
|
|
1725
|
+
start: this.index,
|
|
1726
|
+
end: this.index + value.length,
|
|
1727
|
+
},
|
|
1728
|
+
});
|
|
1729
|
+
}
|
|
1730
|
+
await this.child(`layouts/${exp.value}.janja`);
|
|
1731
|
+
}
|
|
1732
|
+
async include(value) {
|
|
1733
|
+
const exp = parser.parse(value.replace(/\?$/, ''));
|
|
1734
|
+
if (exp?.type !== 'STR' || !exp.value) {
|
|
1735
|
+
throw new ParseError(`"include" tag must have a file path`, {
|
|
1736
|
+
source: this.template,
|
|
1737
|
+
range: {
|
|
1738
|
+
start: this.index,
|
|
1739
|
+
end: this.index + value.length,
|
|
1740
|
+
},
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
await this.child(`partials/${exp.value}.janja`, value.endsWith('?'));
|
|
1744
|
+
}
|
|
1745
|
+
async child(path, optional) {
|
|
1746
|
+
let content;
|
|
1747
|
+
try {
|
|
1748
|
+
content = await this.options.loader(path);
|
|
1749
|
+
}
|
|
1750
|
+
catch (error) {
|
|
1751
|
+
if (optional) {
|
|
1752
|
+
return;
|
|
1753
|
+
}
|
|
1754
|
+
throw error;
|
|
1755
|
+
}
|
|
1756
|
+
if (!content) {
|
|
1757
|
+
return;
|
|
1758
|
+
}
|
|
1759
|
+
let cursor = await new Parser(this.options).parse(content);
|
|
1760
|
+
while (cursor) {
|
|
1761
|
+
this.push(cursor);
|
|
1762
|
+
cursor = cursor.next;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
push(token) {
|
|
1766
|
+
if (token.name === BLOCK) {
|
|
1767
|
+
const { type, value } = token.value;
|
|
1768
|
+
if (type !== 'ID') {
|
|
1769
|
+
throw new ParseError(`"${BLOCK}" tag must have a title`, {
|
|
1770
|
+
source: this.template,
|
|
1771
|
+
range: {
|
|
1772
|
+
start: this.index,
|
|
1773
|
+
end: this.index + value.length,
|
|
1774
|
+
},
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
this.group = value;
|
|
1778
|
+
// child blocks
|
|
1779
|
+
if (this.blocks[value]) {
|
|
1780
|
+
this.blocks[value].push([token]);
|
|
1781
|
+
return;
|
|
1782
|
+
}
|
|
1783
|
+
// parent block
|
|
1784
|
+
this.blocks[value] = [[token]];
|
|
1785
|
+
}
|
|
1786
|
+
else if (this.group) {
|
|
1787
|
+
const chunks = this.blocks[this.group];
|
|
1788
|
+
if (token.name === ENDBLOCK) {
|
|
1789
|
+
this.group = '';
|
|
1790
|
+
}
|
|
1791
|
+
chunks.at(-1).push(token);
|
|
1792
|
+
// child blocks
|
|
1793
|
+
if (chunks.length > 1) {
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
if (this.cursor) {
|
|
1798
|
+
token.previous = this.cursor;
|
|
1799
|
+
this.cursor.next = token;
|
|
1800
|
+
}
|
|
1801
|
+
else {
|
|
1802
|
+
this.first = token;
|
|
1803
|
+
}
|
|
1804
|
+
this.cursor = token;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* @note Block tags are hoisted to top level
|
|
1808
|
+
*/
|
|
1809
|
+
rebuildBlocks() {
|
|
1810
|
+
for (const chunks of Object.values(this.blocks)) {
|
|
1811
|
+
if (chunks.length < 2) {
|
|
1812
|
+
continue;
|
|
1813
|
+
}
|
|
1814
|
+
const [[{ previous }]] = chunks;
|
|
1815
|
+
const { next } = chunks[0].at(-1);
|
|
1816
|
+
// break the chain
|
|
1817
|
+
if (previous) {
|
|
1818
|
+
if (previous.next) {
|
|
1819
|
+
previous.next.previous = null;
|
|
1820
|
+
}
|
|
1821
|
+
previous.next = null;
|
|
1822
|
+
}
|
|
1823
|
+
else {
|
|
1824
|
+
this.first = null;
|
|
1825
|
+
this.cursor = null;
|
|
1826
|
+
}
|
|
1827
|
+
if (next) {
|
|
1828
|
+
if (next.previous) {
|
|
1829
|
+
next.previous.next = null;
|
|
1830
|
+
}
|
|
1831
|
+
next.previous = null;
|
|
1832
|
+
}
|
|
1833
|
+
const body = this.rebuildBlockBody(chunks.pop(), chunks);
|
|
1834
|
+
this.rebuildBlockChain(body, previous, next);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
rebuildBlockBody(child, parents) {
|
|
1838
|
+
if (!child) {
|
|
1839
|
+
return [];
|
|
1840
|
+
}
|
|
1841
|
+
const chunk = [];
|
|
1842
|
+
child.forEach((token) => {
|
|
1843
|
+
if (token.name === SUPER) {
|
|
1844
|
+
this.rebuildBlockBody(parents.pop(), parents).forEach(t => chunk.push(t));
|
|
1845
|
+
}
|
|
1846
|
+
else {
|
|
1847
|
+
chunk.push(token);
|
|
1848
|
+
}
|
|
1849
|
+
});
|
|
1850
|
+
return chunk;
|
|
1851
|
+
}
|
|
1852
|
+
rebuildBlockChain(tokens, previous, next) {
|
|
1853
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1854
|
+
tokens[i].previous = tokens[i - 1] || previous;
|
|
1855
|
+
tokens[i].next = tokens[i + 1] || next;
|
|
1856
|
+
}
|
|
1857
|
+
if (previous) {
|
|
1858
|
+
[previous.next] = tokens;
|
|
1859
|
+
}
|
|
1860
|
+
if (next) {
|
|
1861
|
+
next.previous = tokens[tokens.length - 1];
|
|
1862
|
+
}
|
|
1863
|
+
if (!this.first) {
|
|
1864
|
+
[this.first] = tokens;
|
|
1865
|
+
this.cursor = this.first;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
class RenderError extends Error {
|
|
1871
|
+
constructor(message, options) {
|
|
1872
|
+
super(message);
|
|
1873
|
+
Object.defineProperty(this, "options", {
|
|
1874
|
+
enumerable: true,
|
|
1875
|
+
configurable: true,
|
|
1876
|
+
writable: true,
|
|
1877
|
+
value: options
|
|
1878
|
+
});
|
|
1879
|
+
this.name = 'RenderError';
|
|
1880
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
1881
|
+
}
|
|
1882
|
+
get details() {
|
|
1883
|
+
const ranges = this.options.sourcemap.getRanges(+(this.options.error.stack.match(/<anonymous>:\d:(\d+)\)/)?.[1] ?? 0));
|
|
1884
|
+
return highlightSource(this.message, this.options.source, ranges);
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
|
|
1888
|
+
class Engine {
|
|
1889
|
+
constructor({ globals, filters, compilers, ...options } = {}) {
|
|
1890
|
+
Object.defineProperty(this, "options", {
|
|
1891
|
+
enumerable: true,
|
|
1892
|
+
configurable: true,
|
|
1893
|
+
writable: true,
|
|
1894
|
+
value: void 0
|
|
1895
|
+
});
|
|
1896
|
+
this.options = { ...config, ...options };
|
|
1897
|
+
Object.assign(this.options.globals, globals);
|
|
1898
|
+
Object.assign(this.options.filters, filters);
|
|
1899
|
+
Object.assign(this.options.compilers, compilers);
|
|
1900
|
+
}
|
|
1901
|
+
async render(template, globals) {
|
|
1902
|
+
const { script, sourcemap } = await this._compile(template);
|
|
1903
|
+
return this._render(template, globals, script, sourcemap);
|
|
1904
|
+
}
|
|
1905
|
+
async renderFile(filepath, globals) {
|
|
1906
|
+
if (this.options.cache?.has(filepath)) {
|
|
1907
|
+
const { template, script, sourcemap } = this.options.cache.get(filepath);
|
|
1908
|
+
return this._render(template, globals, script, sourcemap);
|
|
1909
|
+
}
|
|
1910
|
+
const template = await this.options.loader(filepath);
|
|
1911
|
+
const { script, sourcemap } = await this._compile(template);
|
|
1912
|
+
this.options.cache?.set(filepath, { template, script, sourcemap });
|
|
1913
|
+
return this._render(template, globals, script, sourcemap);
|
|
1914
|
+
}
|
|
1915
|
+
async _compile(template, filepath) {
|
|
1916
|
+
return new Compiler(this.options).compile(await new Parser(this.options).parse(template), filepath);
|
|
1917
|
+
}
|
|
1918
|
+
async _render(template, globals, script, sourcemap) {
|
|
1919
|
+
try {
|
|
1920
|
+
return await script({ ...this.options.globals, ...globals }, { ...this.options.filters, ...filters }, (v) => {
|
|
1921
|
+
if (v instanceof Safe) {
|
|
1922
|
+
return `${v}`;
|
|
1923
|
+
}
|
|
1924
|
+
return this.options.autoEscape ? escape(v) : v;
|
|
1925
|
+
});
|
|
1926
|
+
}
|
|
1927
|
+
catch (error) {
|
|
1928
|
+
throw new RenderError(error.message, {
|
|
1929
|
+
source: template,
|
|
1930
|
+
error,
|
|
1931
|
+
sourcemap,
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
async function render(template, data, options) {
|
|
1937
|
+
return new Engine(options).render(template, data);
|
|
1938
|
+
}
|
|
1939
|
+
async function renderFile(filepath, data, options) {
|
|
1940
|
+
return new Engine(options).renderFile(filepath, data);
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
exports.Engine = Engine;
|
|
1944
|
+
exports.ExpCompiler = ExpCompiler;
|
|
1945
|
+
exports.ExpParser = ExpParser;
|
|
1946
|
+
exports.compiler = compiler;
|
|
1947
|
+
exports.parser = parser;
|
|
1948
|
+
exports.render = render;
|
|
1949
|
+
exports.renderFile = renderFile;
|
|
1950
|
+
|
|
1951
|
+
})(this.janja = this.janja || {});
|