janja 2.4.0 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +143 -69
- package/dist/index.d.ts +81 -3
- package/dist/index.esm.js +131 -70
- package/dist/index.js +143 -69
- package/package.json +7 -7
package/dist/index.esm.js
CHANGED
|
@@ -882,7 +882,6 @@ class CommentNode extends Traversal {
|
|
|
882
882
|
}
|
|
883
883
|
|
|
884
884
|
async function* parseComment({ val, loc, strip }) {
|
|
885
|
-
console.log(val);
|
|
886
885
|
yield 'NEXT';
|
|
887
886
|
yield new CommentNode(val, loc, strip);
|
|
888
887
|
}
|
|
@@ -1343,17 +1342,22 @@ const plugin$6 = {
|
|
|
1343
1342
|
parsers: parsers$6,
|
|
1344
1343
|
};
|
|
1345
1344
|
|
|
1346
|
-
async function compileMacro({ val
|
|
1345
|
+
async function compileMacro({ val, loc, body }, compiler) {
|
|
1347
1346
|
const { context } = compiler;
|
|
1348
|
-
const { elements } = right;
|
|
1349
1347
|
const expCompiler = new ExpCompiler();
|
|
1350
|
-
|
|
1351
|
-
.
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
.
|
|
1356
|
-
|
|
1348
|
+
if (val.type === 'ID') {
|
|
1349
|
+
compiler.pushRaw(loc, `${expCompiler.compile(val)}=()=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, '};');
|
|
1350
|
+
}
|
|
1351
|
+
else {
|
|
1352
|
+
const { left, right: { elements }, } = val;
|
|
1353
|
+
compiler.pushRaw(loc, `${expCompiler.compile(left)}=(${elements
|
|
1354
|
+
.map((el) => el.type === 'ASSIGN'
|
|
1355
|
+
? `${el.left.value}=${expCompiler.compile(el.right)}`
|
|
1356
|
+
: el.value)
|
|
1357
|
+
.join(',')})=>async(_c)=>{`, `const ${compiler.in()}={`, `...${context},`, elements
|
|
1358
|
+
.map((el) => (el.type === 'ASSIGN' ? el.left.value : el.value))
|
|
1359
|
+
.join(','), '};');
|
|
1360
|
+
}
|
|
1357
1361
|
await compiler.compileNodes(body);
|
|
1358
1362
|
compiler.out();
|
|
1359
1363
|
compiler.pushRaw(null, '};');
|
|
@@ -1447,7 +1451,36 @@ async function* parseMacro(token, parser) {
|
|
|
1447
1451
|
else {
|
|
1448
1452
|
throw parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
1449
1453
|
}
|
|
1450
|
-
|
|
1454
|
+
const val = parser.parseExp(token.expression);
|
|
1455
|
+
if (val.type === 'ASSIGN') {
|
|
1456
|
+
if (val.left.type !== 'ID') {
|
|
1457
|
+
parser.options.debug?.(new CompileError('Invalid macro name', parser.template, token.loc));
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
if (val.right.type !== 'SEQ') {
|
|
1461
|
+
parser.options.debug?.(new CompileError('Invalid macro parameters', parser.template, token.loc));
|
|
1462
|
+
return;
|
|
1463
|
+
}
|
|
1464
|
+
else {
|
|
1465
|
+
for (const element of val.right.elements) {
|
|
1466
|
+
if (element.type === 'ASSIGN') {
|
|
1467
|
+
if (element.left.type !== 'ID') {
|
|
1468
|
+
parser.options.debug?.(new CompileError('Invalid parameter name', parser.template, token.loc));
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
else if (element.type !== 'ID') {
|
|
1473
|
+
parser.options.debug?.(new CompileError('Invalid macro parameter', parser.template, token.loc));
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
else if (val.type !== 'ID') {
|
|
1480
|
+
parser.options.debug?.(new CompileError('Invalid macro definition', parser.template, token.loc));
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
yield new MacroNode(val, body, token.loc, token.strip);
|
|
1451
1484
|
}
|
|
1452
1485
|
async function* parseCaller(token, parser) {
|
|
1453
1486
|
if (token.expression) {
|
|
@@ -1815,6 +1848,20 @@ const mergeOptions = (defaultOptions, options) => {
|
|
|
1815
1848
|
return mergedOptions;
|
|
1816
1849
|
};
|
|
1817
1850
|
|
|
1851
|
+
class ExpError extends SyntaxError {
|
|
1852
|
+
constructor(message, loc) {
|
|
1853
|
+
super(message);
|
|
1854
|
+
Object.defineProperty(this, "loc", {
|
|
1855
|
+
enumerable: true,
|
|
1856
|
+
configurable: true,
|
|
1857
|
+
writable: true,
|
|
1858
|
+
value: loc
|
|
1859
|
+
});
|
|
1860
|
+
this.name = 'ExpError';
|
|
1861
|
+
ExpError.captureStackTrace?.(this, this.constructor);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1818
1865
|
function updatePosition(str, pos) {
|
|
1819
1866
|
const newlines = (str.match(/\n/g) || []).length;
|
|
1820
1867
|
if (newlines > 0) {
|
|
@@ -1863,14 +1910,22 @@ const expTokenTypes = {
|
|
|
1863
1910
|
',': 'COMMA',
|
|
1864
1911
|
'.': 'DOT',
|
|
1865
1912
|
};
|
|
1913
|
+
function normalizeLitValue(value) {
|
|
1914
|
+
switch (value) {
|
|
1915
|
+
case 'true':
|
|
1916
|
+
return true;
|
|
1917
|
+
case 'false':
|
|
1918
|
+
return false;
|
|
1919
|
+
case 'null':
|
|
1920
|
+
return null;
|
|
1921
|
+
case 'undefined':
|
|
1922
|
+
return undefined;
|
|
1923
|
+
default:
|
|
1924
|
+
return value;
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1866
1927
|
class ExpTokenizer {
|
|
1867
|
-
constructor(
|
|
1868
|
-
Object.defineProperty(this, "template", {
|
|
1869
|
-
enumerable: true,
|
|
1870
|
-
configurable: true,
|
|
1871
|
-
writable: true,
|
|
1872
|
-
value: template
|
|
1873
|
-
});
|
|
1928
|
+
constructor() {
|
|
1874
1929
|
Object.defineProperty(this, "val", {
|
|
1875
1930
|
enumerable: true,
|
|
1876
1931
|
configurable: true,
|
|
@@ -1925,19 +1980,19 @@ class ExpTokenizer {
|
|
|
1925
1980
|
this.readString(char);
|
|
1926
1981
|
continue;
|
|
1927
1982
|
}
|
|
1928
|
-
if (
|
|
1929
|
-
this.readNumber();
|
|
1983
|
+
if (isDigitStart(char)) {
|
|
1984
|
+
this.readNumber(char);
|
|
1930
1985
|
continue;
|
|
1931
1986
|
}
|
|
1932
|
-
if (
|
|
1933
|
-
this.readIdentifier();
|
|
1987
|
+
if (isIdentifierStart(char)) {
|
|
1988
|
+
this.readIdentifier(char);
|
|
1934
1989
|
continue;
|
|
1935
1990
|
}
|
|
1936
1991
|
if (isSymbol(char)) {
|
|
1937
1992
|
this.readSymbol(char);
|
|
1938
1993
|
continue;
|
|
1939
1994
|
}
|
|
1940
|
-
throw new
|
|
1995
|
+
throw new ExpError(`Unexpect "${char}"`, {
|
|
1941
1996
|
start: { line: this.line, column: this.column },
|
|
1942
1997
|
end: { line: this.line, column: this.column + 1 },
|
|
1943
1998
|
});
|
|
@@ -1980,10 +2035,11 @@ class ExpTokenizer {
|
|
|
1980
2035
|
raw: `${quoteChar}${value}${quoteChar}`,
|
|
1981
2036
|
});
|
|
1982
2037
|
}
|
|
1983
|
-
readNumber() {
|
|
1984
|
-
let value =
|
|
2038
|
+
readNumber(startChar) {
|
|
2039
|
+
let value = startChar;
|
|
1985
2040
|
let hasDot = false;
|
|
1986
2041
|
const start = { line: this.line, column: this.column };
|
|
2042
|
+
this.index++;
|
|
1987
2043
|
while (this.index < this.val.length) {
|
|
1988
2044
|
const char = this.val[this.index];
|
|
1989
2045
|
if (isDigit(char)) {
|
|
@@ -2008,12 +2064,13 @@ class ExpTokenizer {
|
|
|
2008
2064
|
raw: value,
|
|
2009
2065
|
});
|
|
2010
2066
|
}
|
|
2011
|
-
readIdentifier() {
|
|
2012
|
-
let value =
|
|
2067
|
+
readIdentifier(startChar) {
|
|
2068
|
+
let value = startChar;
|
|
2013
2069
|
const start = { line: this.line, column: this.column };
|
|
2070
|
+
this.index++;
|
|
2014
2071
|
while (this.index < this.val.length) {
|
|
2015
2072
|
const char = this.val[this.index];
|
|
2016
|
-
if (
|
|
2073
|
+
if (isIdentifier(char)) {
|
|
2017
2074
|
value += char;
|
|
2018
2075
|
this.index++;
|
|
2019
2076
|
continue;
|
|
@@ -2025,17 +2082,7 @@ class ExpTokenizer {
|
|
|
2025
2082
|
: 'ID';
|
|
2026
2083
|
this.tokens.push({
|
|
2027
2084
|
type,
|
|
2028
|
-
value: type === 'LIT'
|
|
2029
|
-
? value === 'true'
|
|
2030
|
-
? true
|
|
2031
|
-
: value === 'false'
|
|
2032
|
-
? false
|
|
2033
|
-
: value === 'null'
|
|
2034
|
-
? null
|
|
2035
|
-
: value === 'undefined'
|
|
2036
|
-
? undefined
|
|
2037
|
-
: value
|
|
2038
|
-
: value,
|
|
2085
|
+
value: type === 'LIT' ? normalizeLitValue(value) : value,
|
|
2039
2086
|
loc: {
|
|
2040
2087
|
start,
|
|
2041
2088
|
end: updatePosition(value, this),
|
|
@@ -2063,13 +2110,16 @@ function isWhitespace(char) {
|
|
|
2063
2110
|
function isSymbol(char) {
|
|
2064
2111
|
return /[+\-*/%|=(),.]/.test(char);
|
|
2065
2112
|
}
|
|
2066
|
-
function
|
|
2113
|
+
function isDigitStart(char) {
|
|
2067
2114
|
return /[-\d]/.test(char);
|
|
2068
2115
|
}
|
|
2069
|
-
function
|
|
2116
|
+
function isDigit(char) {
|
|
2117
|
+
return /\d/.test(char);
|
|
2118
|
+
}
|
|
2119
|
+
function isIdentifierStart(char) {
|
|
2070
2120
|
return /[a-z_$]/i.test(char);
|
|
2071
2121
|
}
|
|
2072
|
-
function
|
|
2122
|
+
function isIdentifier(char) {
|
|
2073
2123
|
return /[\w$]/.test(char);
|
|
2074
2124
|
}
|
|
2075
2125
|
|
|
@@ -2104,13 +2154,7 @@ const expTokenPrecedences = {
|
|
|
2104
2154
|
LIT: 90,
|
|
2105
2155
|
};
|
|
2106
2156
|
class ExpParser {
|
|
2107
|
-
constructor(
|
|
2108
|
-
Object.defineProperty(this, "template", {
|
|
2109
|
-
enumerable: true,
|
|
2110
|
-
configurable: true,
|
|
2111
|
-
writable: true,
|
|
2112
|
-
value: template
|
|
2113
|
-
});
|
|
2157
|
+
constructor() {
|
|
2114
2158
|
Object.defineProperty(this, "val", {
|
|
2115
2159
|
enumerable: true,
|
|
2116
2160
|
configurable: true,
|
|
@@ -2147,7 +2191,7 @@ class ExpParser {
|
|
|
2147
2191
|
this.index = 0;
|
|
2148
2192
|
this.line = loc.start.line;
|
|
2149
2193
|
this.column = loc.start.column;
|
|
2150
|
-
this.tokens = new ExpTokenizer(
|
|
2194
|
+
this.tokens = new ExpTokenizer().tokenize(val, loc);
|
|
2151
2195
|
return this.val ? this.parseExp() : null;
|
|
2152
2196
|
}
|
|
2153
2197
|
parseExp(checker) {
|
|
@@ -2167,12 +2211,12 @@ class ExpParser {
|
|
|
2167
2211
|
}
|
|
2168
2212
|
if (token.type === 'LP') {
|
|
2169
2213
|
if (!this.peek()) {
|
|
2170
|
-
throw new
|
|
2214
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2171
2215
|
}
|
|
2172
2216
|
const elements = this.parseSequence();
|
|
2173
2217
|
const rp = this.consume('RP');
|
|
2174
2218
|
if (!rp) {
|
|
2175
|
-
throw new
|
|
2219
|
+
throw new ExpError('Expected "RP" after "LP"', token.loc);
|
|
2176
2220
|
}
|
|
2177
2221
|
left = {
|
|
2178
2222
|
...token,
|
|
@@ -2187,7 +2231,7 @@ class ExpParser {
|
|
|
2187
2231
|
}
|
|
2188
2232
|
if (token.type === 'NOT') {
|
|
2189
2233
|
if (!this.peek()) {
|
|
2190
|
-
throw new
|
|
2234
|
+
throw new ExpError('Unexpected end of expression', token.loc);
|
|
2191
2235
|
}
|
|
2192
2236
|
left = {
|
|
2193
2237
|
...token,
|
|
@@ -2209,7 +2253,7 @@ class ExpParser {
|
|
|
2209
2253
|
if (lp) {
|
|
2210
2254
|
left.args = this.parseSequence();
|
|
2211
2255
|
if (!this.consume('RP')) {
|
|
2212
|
-
throw new
|
|
2256
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2213
2257
|
}
|
|
2214
2258
|
continue;
|
|
2215
2259
|
}
|
|
@@ -2225,7 +2269,7 @@ class ExpParser {
|
|
|
2225
2269
|
if (token.type === 'IF') {
|
|
2226
2270
|
const test = this.parseExp((t) => t.type === 'ELSE' ? 'BACK' : isHigher(token, t) ? 'BACK' : undefined);
|
|
2227
2271
|
if (!test) {
|
|
2228
|
-
throw new
|
|
2272
|
+
throw new ExpError('Expected test expression', token.loc);
|
|
2229
2273
|
}
|
|
2230
2274
|
left = {
|
|
2231
2275
|
...token,
|
|
@@ -2236,7 +2280,7 @@ class ExpParser {
|
|
|
2236
2280
|
if (this.consume('ELSE')) {
|
|
2237
2281
|
const alternative = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2238
2282
|
if (!alternative) {
|
|
2239
|
-
throw new
|
|
2283
|
+
throw new ExpError('Expected alternative expression', token.loc);
|
|
2240
2284
|
}
|
|
2241
2285
|
left.alternative = alternative;
|
|
2242
2286
|
}
|
|
@@ -2261,11 +2305,21 @@ class ExpParser {
|
|
|
2261
2305
|
token.type === 'MOD' ||
|
|
2262
2306
|
token.type === 'ASSIGN') {
|
|
2263
2307
|
if (!left) {
|
|
2264
|
-
throw new
|
|
2308
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2309
|
+
}
|
|
2310
|
+
if (token.type === 'ASSIGN') {
|
|
2311
|
+
if (left.type === 'SEQ') {
|
|
2312
|
+
if (left.elements.some((el) => el.type !== 'ID')) {
|
|
2313
|
+
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
else if (left.type !== 'ID') {
|
|
2317
|
+
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
2318
|
+
}
|
|
2265
2319
|
}
|
|
2266
2320
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
2267
2321
|
if (!right) {
|
|
2268
|
-
throw new
|
|
2322
|
+
throw new ExpError(`No right operand for "${token.type}"`, token.loc);
|
|
2269
2323
|
}
|
|
2270
2324
|
left = {
|
|
2271
2325
|
...token,
|
|
@@ -2277,10 +2331,10 @@ class ExpParser {
|
|
|
2277
2331
|
}
|
|
2278
2332
|
if (token.type === 'PIPE') {
|
|
2279
2333
|
if (!left) {
|
|
2280
|
-
throw new
|
|
2334
|
+
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
2281
2335
|
}
|
|
2282
2336
|
if (!this.match('ID')) {
|
|
2283
|
-
throw new
|
|
2337
|
+
throw new ExpError('Expected "ID" after "PIPE"', token.loc);
|
|
2284
2338
|
}
|
|
2285
2339
|
left = {
|
|
2286
2340
|
...token,
|
|
@@ -2304,10 +2358,10 @@ class ExpParser {
|
|
|
2304
2358
|
}
|
|
2305
2359
|
const token = this.next();
|
|
2306
2360
|
if (!token) {
|
|
2307
|
-
throw new
|
|
2361
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2308
2362
|
}
|
|
2309
2363
|
if (token.type !== 'ID') {
|
|
2310
|
-
throw new
|
|
2364
|
+
throw new ExpError('Expected "ID" after "DOT"', dot.loc);
|
|
2311
2365
|
}
|
|
2312
2366
|
ids.push({
|
|
2313
2367
|
...token,
|
|
@@ -2344,7 +2398,7 @@ class ExpParser {
|
|
|
2344
2398
|
right.args = args;
|
|
2345
2399
|
}
|
|
2346
2400
|
if (!this.consume('RP')) {
|
|
2347
|
-
throw new
|
|
2401
|
+
throw new ExpError('Expected "RP" after "LP"', lp.loc);
|
|
2348
2402
|
}
|
|
2349
2403
|
}
|
|
2350
2404
|
return right;
|
|
@@ -2576,7 +2630,7 @@ class Parser extends Tokenizer {
|
|
|
2576
2630
|
this.tokenize(template);
|
|
2577
2631
|
const start = this.tokens[0]?.loc.start ?? { line: 1, column: 1 };
|
|
2578
2632
|
const end = this.tokens.at(-1)?.loc.end ?? { line: 1, column: 1 };
|
|
2579
|
-
this.expParser = new ExpParser(
|
|
2633
|
+
this.expParser = new ExpParser();
|
|
2580
2634
|
this.cursor = 0;
|
|
2581
2635
|
return new RootNode(await this.parseUntil(), { start, end });
|
|
2582
2636
|
}
|
|
@@ -2625,7 +2679,14 @@ class Parser extends Tokenizer {
|
|
|
2625
2679
|
return nodes;
|
|
2626
2680
|
}
|
|
2627
2681
|
parseExp({ val, loc }) {
|
|
2628
|
-
|
|
2682
|
+
try {
|
|
2683
|
+
return this.expParser.parse(val, loc);
|
|
2684
|
+
}
|
|
2685
|
+
catch (error) {
|
|
2686
|
+
this.options.debug(error.loc
|
|
2687
|
+
? new CompileError(error.message, this.template, error.loc)
|
|
2688
|
+
: error);
|
|
2689
|
+
}
|
|
2629
2690
|
}
|
|
2630
2691
|
peek() {
|
|
2631
2692
|
return this.tokens.at(this.cursor) ?? null;
|
|
@@ -2637,7 +2698,7 @@ class Parser extends Tokenizer {
|
|
|
2637
2698
|
return names.includes(token?.name);
|
|
2638
2699
|
}
|
|
2639
2700
|
emitExpErr({ name, loc }, required = true) {
|
|
2640
|
-
this.options.debug
|
|
2701
|
+
this.options.debug(new CompileError(required
|
|
2641
2702
|
? `"${name}" requires expression`
|
|
2642
2703
|
: `"${name}" should not have expression`, this.template, loc));
|
|
2643
2704
|
}
|
|
@@ -2736,7 +2797,7 @@ class Compiler extends Context {
|
|
|
2736
2797
|
template = await this.options.loader(path);
|
|
2737
2798
|
}
|
|
2738
2799
|
catch {
|
|
2739
|
-
this.options.debug
|
|
2800
|
+
this.options.debug(new CompileError(`Failed to load template from "${path}"`, this.template, node.loc));
|
|
2740
2801
|
return;
|
|
2741
2802
|
}
|
|
2742
2803
|
if (template) {
|
|
@@ -2797,7 +2858,7 @@ class Compiler extends Context {
|
|
|
2797
2858
|
this.partials.set(path, code);
|
|
2798
2859
|
}
|
|
2799
2860
|
catch {
|
|
2800
|
-
this.options.debug
|
|
2861
|
+
this.options.debug(new CompileError('Failed to compile template', this.template, node.loc));
|
|
2801
2862
|
}
|
|
2802
2863
|
}
|
|
2803
2864
|
}
|
|
@@ -2817,7 +2878,7 @@ class RenderError extends CompileError {
|
|
|
2817
2878
|
writable: true,
|
|
2818
2879
|
value: getLoc
|
|
2819
2880
|
});
|
|
2820
|
-
this.name = '
|
|
2881
|
+
this.name = 'RenderError';
|
|
2821
2882
|
RenderError.captureStackTrace?.(this, this.constructor);
|
|
2822
2883
|
}
|
|
2823
2884
|
get details() {
|
|
@@ -2879,4 +2940,4 @@ async function renderFile(filepath, data, options) {
|
|
|
2879
2940
|
return new Renderer(options).renderFile(filepath, data);
|
|
2880
2941
|
}
|
|
2881
2942
|
|
|
2882
|
-
export { Renderer, render, renderFile };
|
|
2943
|
+
export { CompileError, Compiler, ExpCompiler, ExpError, ExpParser, ExpTokenizer, Parser, RenderError, Renderer, RootNode, Safe, Tokenizer, Traversal, expTokenTypes, render, renderFile };
|