@yannosay/sinth 0.14.2 → 0.14.4
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/package.json +1 -1
- package/src/core/compiler.ts +89 -3
- package/src/core/expr.ts +3 -0
- package/src/core/lexer.ts +2 -0
- package/src/core/parser.ts +146 -35
- package/src/core/runtime/helpers.ts +14 -0
- package/src/core/types.ts +4 -3
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -46,6 +46,71 @@ export const INLINE_STYLE_PROPS = new Set([
|
|
|
46
46
|
"content","quotes",
|
|
47
47
|
]);
|
|
48
48
|
|
|
49
|
+
function inlineFunctionBody(fnDef: FunctionDef, args: Expression[], ctx: CompileCtx): string | null {
|
|
50
|
+
const paramMap = new Map<string, string>();
|
|
51
|
+
for (let i = 0; i < fnDef.params.length && i < args.length; i++) {
|
|
52
|
+
paramMap.set(fnDef.params[i].name, compileExprToJS(args[i]));
|
|
53
|
+
}
|
|
54
|
+
const subExpr = (expr: Expression): Expression => {
|
|
55
|
+
if (expr.kind === "variable" && expr.name && paramMap.has(expr.name)) {
|
|
56
|
+
return { kind: "variable", name: paramMap.get(expr.name)! };
|
|
57
|
+
}
|
|
58
|
+
if (expr.kind === "binary") {
|
|
59
|
+
return { ...expr, left: expr.left ? subExpr(expr.left) : undefined, right: expr.right ? subExpr(expr.right) : undefined };
|
|
60
|
+
}
|
|
61
|
+
if (expr.kind === "unary") {
|
|
62
|
+
return { ...expr, operand: expr.operand ? subExpr(expr.operand) : undefined };
|
|
63
|
+
}
|
|
64
|
+
if (expr.kind === "assign") {
|
|
65
|
+
if (expr.target && paramMap.has(expr.target)) {
|
|
66
|
+
return { ...expr, target: paramMap.get(expr.target)!, right: expr.right ? subExpr(expr.right) : undefined };
|
|
67
|
+
}
|
|
68
|
+
return { ...expr, right: expr.right ? subExpr(expr.right) : undefined };
|
|
69
|
+
}
|
|
70
|
+
if (expr.kind === "postfix" && expr.target && paramMap.has(expr.target)) {
|
|
71
|
+
return { ...expr, target: paramMap.get(expr.target)!, op: expr.op };
|
|
72
|
+
}
|
|
73
|
+
if (expr.kind === "index") {
|
|
74
|
+
return { ...expr, object: expr.object ? subExpr(expr.object) : undefined, key: expr.key ? subExpr(expr.key) : undefined };
|
|
75
|
+
}
|
|
76
|
+
if (expr.kind === "call") {
|
|
77
|
+
return { ...expr, callee: expr.callee ? subExpr(expr.callee) : undefined, args: expr.args?.map(a => subExpr(a)) };
|
|
78
|
+
}
|
|
79
|
+
return expr;
|
|
80
|
+
};
|
|
81
|
+
const subChild = (child: Child): Child => {
|
|
82
|
+
switch (child.kind) {
|
|
83
|
+
case "assign_stmt":
|
|
84
|
+
return { ...child, expression: subExpr(child.expression) as Expression };
|
|
85
|
+
case "if": {
|
|
86
|
+
const ib = child as IfBlock;
|
|
87
|
+
return {
|
|
88
|
+
...ib,
|
|
89
|
+
condition: subExpr(ib.condition),
|
|
90
|
+
body: ib.body.map(subChild),
|
|
91
|
+
elseBody: ib.elseBody?.map(subChild),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
case "return":
|
|
95
|
+
return { ...child, expression: child.expression ? subExpr(child.expression) : undefined };
|
|
96
|
+
default:
|
|
97
|
+
return child;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const statements: string[] = [];
|
|
101
|
+
for (const c of fnDef.body) {
|
|
102
|
+
const substituted = subChild(c);
|
|
103
|
+
if (substituted.kind === "assign_stmt") {
|
|
104
|
+
statements.push(compileExprToJS(substituted.expression) + ";");
|
|
105
|
+
} else if (substituted.kind === "if") {
|
|
106
|
+
statements.push(compileIfToJS(substituted as IfBlock));
|
|
107
|
+
} else if (substituted.kind === "return") {
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (statements.length === 0) return null;
|
|
111
|
+
return statements.join(" ");
|
|
112
|
+
}
|
|
113
|
+
|
|
49
114
|
export function resolveBuiltinTag(
|
|
50
115
|
name: string,
|
|
51
116
|
attrs: Attr[],
|
|
@@ -177,6 +242,12 @@ if (name === "delay") {
|
|
|
177
242
|
|
|
178
243
|
let raw = value.value;
|
|
179
244
|
|
|
245
|
+
if (raw.startsWith("__ACTION_JS__")) {
|
|
246
|
+
const js = raw.substring("__ACTION_JS__".length);
|
|
247
|
+
const ev = eventAttrName(name);
|
|
248
|
+
if (ev) return `${ev}="(function(){ ${js.replace(/"/g, """)}; sinthRender(); })()"`;
|
|
249
|
+
return `${name}="${escAttr(js)}"`;
|
|
250
|
+
}
|
|
180
251
|
if (raw.startsWith("__MULTI_EXPR__")) {
|
|
181
252
|
const exprJson = raw.substring("__MULTI_EXPR__".length);
|
|
182
253
|
try {
|
|
@@ -191,8 +262,18 @@ if (name === "delay") {
|
|
|
191
262
|
const exprJson = raw.substring("__EXPR__".length);
|
|
192
263
|
try {
|
|
193
264
|
const expr: Expression = JSON.parse(exprJson);
|
|
194
|
-
const jsExpr = compileExprToJS(expr, ctx?.loopVars);
|
|
195
265
|
const ev = eventAttrName(name);
|
|
266
|
+
if (ev && expr.kind === "call" && expr.callee?.kind === "variable") {
|
|
267
|
+
const fnName = expr.callee.name;
|
|
268
|
+
const fnDef = fnName ? ctx.functionDefs.find(f => f.name === fnName) : undefined;
|
|
269
|
+
if (fnDef) {
|
|
270
|
+
const inlinedJS = inlineFunctionBody(fnDef, expr.args ?? [], ctx);
|
|
271
|
+
if (inlinedJS) {
|
|
272
|
+
return `${ev}="(function(){ ${inlinedJS.replace(/"/g, """)}; sinthRender(); })()"`;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const jsExpr = compileExprToJS(expr, ctx?.loopVars);
|
|
196
277
|
if (ev) return `${ev}="(function(){ ${jsExpr.replace(/"/g, """)}; sinthRender(); })()"`;
|
|
197
278
|
return `${name}="${escAttr(jsExpr)}"`;
|
|
198
279
|
} catch { }
|
|
@@ -342,6 +423,9 @@ export function renderChild(
|
|
|
342
423
|
return { ...a, value: { kind: "str" as const, value: "__EXPR__" + JSON.stringify(substituted) } };
|
|
343
424
|
} catch {}
|
|
344
425
|
}
|
|
426
|
+
if (raw.startsWith("__ACTION_JS__")) {
|
|
427
|
+
return { ...a, value: { kind: "str" as const, value: raw } };
|
|
428
|
+
}
|
|
345
429
|
if (raw.startsWith("__MULTI_EXPR__")) {
|
|
346
430
|
try {
|
|
347
431
|
const exprs: Expression[] = JSON.parse(raw.substring("__MULTI_EXPR__".length));
|
|
@@ -449,6 +533,7 @@ depth: number,
|
|
|
449
533
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
450
534
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
451
535
|
let replaceAttr = "";
|
|
536
|
+
let persistAttr = ifBlock.persist ? ` data-sinth-if-persist="true"` : "";
|
|
452
537
|
const firstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
453
538
|
if (firstComp) {
|
|
454
539
|
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
@@ -490,7 +575,7 @@ depth: number,
|
|
|
490
575
|
}
|
|
491
576
|
}
|
|
492
577
|
return (
|
|
493
|
-
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}${delayAttr}${delayHideAttr}>${bodyHTML}</template>` +
|
|
578
|
+
`<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}${delayAttr}${delayHideAttr}${persistAttr}>${bodyHTML}</template>` +
|
|
494
579
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}"${elseDelayAttr}${elseDelayHideAttr}>${elseHTML}</template>` : "")
|
|
495
580
|
);
|
|
496
581
|
}
|
|
@@ -1016,8 +1101,9 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1016
1101
|
}
|
|
1017
1102
|
if (child.kind === "use") {
|
|
1018
1103
|
(child as CompUse).attrs.forEach(a => {
|
|
1104
|
+
if (a.name === "id") return;
|
|
1019
1105
|
if (a.value?.kind === "str") {
|
|
1020
|
-
const v = a.value.value;
|
|
1106
|
+
const v = a.value.value;
|
|
1021
1107
|
if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
|
|
1022
1108
|
try {
|
|
1023
1109
|
const exprs: Expression[] = v.startsWith("__MULTI_EXPR__")
|
package/src/core/expr.ts
CHANGED
|
@@ -39,6 +39,9 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
39
39
|
case "call": {
|
|
40
40
|
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
41
41
|
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
42
|
+
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
43
|
+
return callee;
|
|
44
|
+
}
|
|
42
45
|
return `${callee}(${args})`;
|
|
43
46
|
}
|
|
44
47
|
default:
|
package/src/core/lexer.ts
CHANGED
|
@@ -18,6 +18,7 @@ const KEYWORDS: Record<string, TT> = {
|
|
|
18
18
|
"custom": TT.KW_CUSTOM,
|
|
19
19
|
"if": TT.KW_IF,
|
|
20
20
|
"else": TT.KW_ELSE,
|
|
21
|
+
"elif": TT.KW_ELSE,
|
|
21
22
|
"for": TT.KW_FOR,
|
|
22
23
|
"in": TT.KW_IN,
|
|
23
24
|
"remove": TT.KW_REMOVE,
|
|
@@ -62,6 +63,7 @@ export class Lexer {
|
|
|
62
63
|
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
63
64
|
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
64
65
|
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
66
|
+
if (ch === "%") { tokens.push(this.single(TT.OP_MOD, loc)); continue; }
|
|
65
67
|
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
66
68
|
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
67
69
|
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
package/src/core/parser.ts
CHANGED
|
@@ -256,9 +256,7 @@ private parseArrayLiteral(): Literal {
|
|
|
256
256
|
return obj;
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
parseExpression(): Expression | null {
|
|
259
|
+
parseExpression(skipBinary: boolean = false): Expression | null {
|
|
262
260
|
const tok = this.peek();
|
|
263
261
|
|
|
264
262
|
// Unary 'not'
|
|
@@ -276,10 +274,10 @@ private parseArrayLiteral(): Literal {
|
|
|
276
274
|
if (!inner) throw new SinthError("Expected expression after '('", this.peek().loc);
|
|
277
275
|
if (!this.check(TT.RPAREN)) throw new SinthError("Expected closing ')'", this.peek().loc);
|
|
278
276
|
this.consume(TT.RPAREN);
|
|
279
|
-
|
|
277
|
+
const result = this.parseCallExpr(this.parsePostfix(inner));
|
|
278
|
+
return skipBinary ? result : this.parseBinaryRHS(result);
|
|
280
279
|
}
|
|
281
280
|
|
|
282
|
-
// literal
|
|
283
281
|
if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) ||
|
|
284
282
|
this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
|
|
285
283
|
const lit = this.parseLiteral();
|
|
@@ -337,7 +335,36 @@ private parseArrayLiteral(): Literal {
|
|
|
337
335
|
return null;
|
|
338
336
|
}
|
|
339
337
|
|
|
340
|
-
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
private parsePrimary(): Expression {
|
|
341
|
+
const tok = this.peek();
|
|
342
|
+
if (tok.type === TT.NUMBER || tok.type === TT.STRING || tok.type === TT.BOOL_TRUE || tok.type === TT.BOOL_FALSE || tok.type === TT.NULL_LIT) {
|
|
343
|
+
const lit = this.parseLiteral();
|
|
344
|
+
return { kind: "literal", value: lit };
|
|
345
|
+
}
|
|
346
|
+
if (tok.type === TT.IDENT) {
|
|
347
|
+
const name = this.consume(TT.IDENT).value;
|
|
348
|
+
let expr: Expression;
|
|
349
|
+
if (this.check(TT.DOT)) {
|
|
350
|
+
this.consume(TT.DOT);
|
|
351
|
+
expr = { kind: "variable", name: name + "." + this.consume(TT.IDENT).value };
|
|
352
|
+
} else {
|
|
353
|
+
expr = { kind: "variable", name };
|
|
354
|
+
}
|
|
355
|
+
return expr;
|
|
356
|
+
}
|
|
357
|
+
if (tok.type === TT.LPAREN) {
|
|
358
|
+
this.consume(TT.LPAREN);
|
|
359
|
+
const inner = this.parseExpression();
|
|
360
|
+
this.consume(TT.RPAREN);
|
|
361
|
+
return inner!;
|
|
362
|
+
}
|
|
363
|
+
throw new SinthError(`Expected expression`, this.peek().loc);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private parseComparison(left: Expression): Expression {
|
|
367
|
+
const savedPos = this.pos;
|
|
341
368
|
const t = this.peek().type;
|
|
342
369
|
let op: BinaryOp | null = null;
|
|
343
370
|
if (t === TT.OP_EQEQ) op = "==";
|
|
@@ -346,19 +373,61 @@ private parseArrayLiteral(): Literal {
|
|
|
346
373
|
else if (t === TT.OP_GT) op = ">";
|
|
347
374
|
else if (t === TT.OP_LTEQ) op = "<=";
|
|
348
375
|
else if (t === TT.OP_GTEQ) op = ">=";
|
|
349
|
-
else if (t === TT.OP_PLUS) op = "+";
|
|
350
|
-
else if (t === TT.OP_MINUS) op = "-";
|
|
351
|
-
else if (t === TT.OP_STAR) op = "*";
|
|
352
|
-
else if (t === TT.OP_SLASH) op = "/";
|
|
353
|
-
else if (t === TT.IDENT && this.peek().value === "and") op = "and";
|
|
354
|
-
else if (t === TT.IDENT && this.peek().value === "or") op = "or";
|
|
355
|
-
|
|
356
376
|
if (!op) return left;
|
|
357
377
|
this.pos++;
|
|
378
|
+
const rightPrimary = this.parsePrimary();
|
|
379
|
+
const rightWithPostfix = this.parsePostfix(rightPrimary);
|
|
380
|
+
const rightWithCall = this.parseCallExpr(rightWithPostfix);
|
|
381
|
+
const right = this.parseArithmetic(rightWithCall);
|
|
382
|
+
if (!right) {
|
|
383
|
+
this.pos = savedPos;
|
|
384
|
+
return left;
|
|
385
|
+
}
|
|
386
|
+
return this.parseComparison({ kind: "binary", left, op, right });
|
|
387
|
+
}
|
|
358
388
|
|
|
389
|
+
private parseLogical(left: Expression): Expression {
|
|
390
|
+
const savedPos = this.pos;
|
|
391
|
+
const t = this.peek().type;
|
|
392
|
+
let op: BinaryOp | null = null;
|
|
393
|
+
if (t === TT.IDENT && this.peek().value === "and") op = "and";
|
|
394
|
+
else if (t === TT.IDENT && this.peek().value === "or") op = "or";
|
|
395
|
+
if (!op) return left;
|
|
396
|
+
this.pos++;
|
|
359
397
|
const right = this.parseExpression();
|
|
360
|
-
if (!right)
|
|
361
|
-
|
|
398
|
+
if (!right) {
|
|
399
|
+
this.pos = savedPos;
|
|
400
|
+
return left;
|
|
401
|
+
}
|
|
402
|
+
return this.parseLogical({ kind: "binary", left, op, right });
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private parseArithmetic(left: Expression): Expression {
|
|
406
|
+
while (true) {
|
|
407
|
+
const t = this.peek().type;
|
|
408
|
+
let op: BinaryOp | null = null;
|
|
409
|
+
if (t === TT.OP_PLUS) op = "+";
|
|
410
|
+
else if (t === TT.OP_MINUS) op = "-";
|
|
411
|
+
else if (t === TT.OP_STAR) op = "*";
|
|
412
|
+
else if (t === TT.OP_SLASH) op = "/";
|
|
413
|
+
else if (t === TT.OP_MOD) op = "%";
|
|
414
|
+
if (!op) break;
|
|
415
|
+
this.pos++;
|
|
416
|
+
const rightPrimary = this.parsePrimary();
|
|
417
|
+
const rightWithPostfix = this.parsePostfix(rightPrimary);
|
|
418
|
+
const rightWithCall = this.parseCallExpr(rightWithPostfix);
|
|
419
|
+
const right = this.parseArithmetic(rightWithCall);
|
|
420
|
+
if (!right) throw new SinthError(`Expected expression after '${op}'`, this.peek().loc);
|
|
421
|
+
left = { kind: "binary", left, op, right };
|
|
422
|
+
}
|
|
423
|
+
return left;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private parseBinaryRHS(left: Expression): Expression {
|
|
427
|
+
left = this.parseArithmetic(left);
|
|
428
|
+
left = this.parseComparison(left);
|
|
429
|
+
left = this.parseLogical(left);
|
|
430
|
+
return left;
|
|
362
431
|
}
|
|
363
432
|
|
|
364
433
|
private parsePostfix(base: Expression): Expression {
|
|
@@ -509,16 +578,43 @@ private parseArrayLiteral(): Literal {
|
|
|
509
578
|
const condition = this.parseExpression();
|
|
510
579
|
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
511
580
|
|
|
512
|
-
|
|
581
|
+
let persist = false;
|
|
582
|
+
if (this.check(TT.IDENT) && this.peek().value === "persist") {
|
|
583
|
+
this.consume(TT.IDENT);
|
|
584
|
+
persist = true;
|
|
585
|
+
}
|
|
513
586
|
this.consume(TT.LBRACE);
|
|
514
587
|
const body = this.parseUnifiedBody();
|
|
515
588
|
this.consume(TT.RBRACE);
|
|
516
589
|
|
|
517
590
|
let elseBody: Child[] | undefined;
|
|
518
591
|
if (this.check(TT.KW_ELSE)) {
|
|
519
|
-
this.consume(TT.KW_ELSE);
|
|
520
|
-
if (this.check(TT.KW_IF)) {
|
|
521
|
-
|
|
592
|
+
const elseTok = this.consume(TT.KW_ELSE);
|
|
593
|
+
if (this.check(TT.KW_IF) || elseTok.value === "elif") {
|
|
594
|
+
if (elseTok.value === "elif") {
|
|
595
|
+
const cond = this.parseExpression();
|
|
596
|
+
if (!cond) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
|
|
597
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after elif condition", this.peek().loc);
|
|
598
|
+
this.consume(TT.LBRACE);
|
|
599
|
+
const elifBody = this.parseUnifiedBody();
|
|
600
|
+
this.consume(TT.RBRACE);
|
|
601
|
+
const elifBlock: IfBlock = { kind: "if", condition: cond, body: elifBody, loc: elseTok.loc };
|
|
602
|
+
if (this.check(TT.KW_ELSE)) {
|
|
603
|
+
const nextElse = this.consume(TT.KW_ELSE);
|
|
604
|
+
if (this.check(TT.KW_IF) || nextElse.value === "elif") {
|
|
605
|
+
this.pos--;
|
|
606
|
+
elifBlock.elseBody = [this.parseIfBlock()];
|
|
607
|
+
} else {
|
|
608
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
609
|
+
this.consume(TT.LBRACE);
|
|
610
|
+
elifBlock.elseBody = this.parseUnifiedBody();
|
|
611
|
+
this.consume(TT.RBRACE);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
elseBody = [elifBlock];
|
|
615
|
+
} else {
|
|
616
|
+
elseBody = [this.parseIfBlock()];
|
|
617
|
+
}
|
|
522
618
|
} else {
|
|
523
619
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
524
620
|
this.consume(TT.LBRACE);
|
|
@@ -527,7 +623,7 @@ private parseArrayLiteral(): Literal {
|
|
|
527
623
|
}
|
|
528
624
|
}
|
|
529
625
|
|
|
530
|
-
return { kind: "if", condition, body, elseBody, loc };
|
|
626
|
+
return { kind: "if", condition, body, elseBody, persist, loc };
|
|
531
627
|
}
|
|
532
628
|
|
|
533
629
|
private parseUnifiedBody(): Child[] {
|
|
@@ -582,7 +678,7 @@ private parseArrayLiteral(): Literal {
|
|
|
582
678
|
rhs = { kind: "variable", name: rhsName };
|
|
583
679
|
}
|
|
584
680
|
} else {
|
|
585
|
-
rhs = this.parseExpression();
|
|
681
|
+
rhs = this.parseExpression(true);
|
|
586
682
|
}
|
|
587
683
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
588
684
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
@@ -791,18 +887,33 @@ private parseArrayLiteral(): Literal {
|
|
|
791
887
|
} else {
|
|
792
888
|
const savedPos = this.pos;
|
|
793
889
|
const expr = this.parseExpression();
|
|
794
|
-
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN) || this.check(TT.OP_SEMI))) {
|
|
795
|
-
const
|
|
890
|
+
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN) || this.check(TT.OP_SEMI) || this.check(TT.KW_IF))) {
|
|
891
|
+
const actions: (Expression | { kind: "if"; js: string })[] = [{ kind: "expr", expr } as any];
|
|
796
892
|
while (this.check(TT.OP_SEMI)) {
|
|
797
893
|
this.consume(TT.OP_SEMI);
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
894
|
+
if (this.check(TT.KW_IF)) {
|
|
895
|
+
const ifBlock = this.parseIfBlock();
|
|
896
|
+
actions.push({ kind: "if", js: compileIfToJS(ifBlock) });
|
|
897
|
+
} else {
|
|
898
|
+
const next = this.parseExpression();
|
|
899
|
+
if (next) actions.push({ kind: "expr", expr: next } as any);
|
|
900
|
+
else break;
|
|
901
|
+
}
|
|
801
902
|
}
|
|
802
|
-
|
|
803
|
-
|
|
903
|
+
const hasIf = actions.some(a => a.kind === "if");
|
|
904
|
+
if (actions.length === 1 && !hasIf) {
|
|
905
|
+
const single = (actions[0] as any).expr as Expression;
|
|
906
|
+
value = { kind: "str", value: "__EXPR__" + JSON.stringify(single) };
|
|
804
907
|
} else {
|
|
805
|
-
|
|
908
|
+
const partsJS: string[] = [];
|
|
909
|
+
for (const a of actions) {
|
|
910
|
+
if (a.kind === "if") {
|
|
911
|
+
partsJS.push((a as any).js as string);
|
|
912
|
+
} else {
|
|
913
|
+
partsJS.push(compileExprToJS((a as any).expr as Expression) + ";");
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
value = { kind: "str", value: "__ACTION_JS__" + partsJS.join(" ") };
|
|
806
917
|
}
|
|
807
918
|
} else {
|
|
808
919
|
this.pos = savedPos;
|
|
@@ -864,7 +975,7 @@ private parseArrayLiteral(): Literal {
|
|
|
864
975
|
if (this.check(TT.LPAREN)) {
|
|
865
976
|
const loc = this.peek().loc;
|
|
866
977
|
this.consume(TT.LPAREN);
|
|
867
|
-
let expr = this.parseExpression();
|
|
978
|
+
let expr = this.parseExpression(true);
|
|
868
979
|
if (!expr) throw new SinthError("Expected expression after '('", this.peek().loc);
|
|
869
980
|
if (!this.check(TT.RPAREN)) throw new SinthError("Expected ')'", this.peek().loc);
|
|
870
981
|
this.consume(TT.RPAREN);
|
|
@@ -885,7 +996,7 @@ private parseArrayLiteral(): Literal {
|
|
|
885
996
|
rhs = { kind: "variable", name: rhsName };
|
|
886
997
|
}
|
|
887
998
|
} else {
|
|
888
|
-
rhs = this.parseExpression();
|
|
999
|
+
rhs = this.parseExpression(true);
|
|
889
1000
|
}
|
|
890
1001
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
891
1002
|
expr = { kind: "binary", left: expr, op: "+", right: rhs };
|
|
@@ -913,7 +1024,7 @@ private parseArrayLiteral(): Literal {
|
|
|
913
1024
|
rhs = { kind: "variable", name: rhsName };
|
|
914
1025
|
}
|
|
915
1026
|
} else {
|
|
916
|
-
rhs = this.parseExpression();
|
|
1027
|
+
rhs = this.parseExpression(true);
|
|
917
1028
|
}
|
|
918
1029
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
919
1030
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
@@ -970,7 +1081,7 @@ private parseArrayLiteral(): Literal {
|
|
|
970
1081
|
rhs = { kind: "variable", name: rhsName };
|
|
971
1082
|
}
|
|
972
1083
|
} else {
|
|
973
|
-
rhs = this.parseExpression();
|
|
1084
|
+
rhs = this.parseExpression(true);
|
|
974
1085
|
}
|
|
975
1086
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
976
1087
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
@@ -1003,7 +1114,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1003
1114
|
rhs = { kind: "variable", name: rhsName };
|
|
1004
1115
|
}
|
|
1005
1116
|
} else {
|
|
1006
|
-
rhs = this.parseExpression();
|
|
1117
|
+
rhs = this.parseExpression(true);
|
|
1007
1118
|
}
|
|
1008
1119
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1009
1120
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
@@ -1035,7 +1146,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1035
1146
|
rhs = { kind: "variable", name: rhsName };
|
|
1036
1147
|
}
|
|
1037
1148
|
} else {
|
|
1038
|
-
rhs = this.parseExpression();
|
|
1149
|
+
rhs = this.parseExpression(true);
|
|
1039
1150
|
}
|
|
1040
1151
|
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1041
1152
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
@@ -111,6 +111,13 @@ function sinthIfBlock(t) {
|
|
|
111
111
|
var condFn = __X[t.dataset.sinthIfExpr];
|
|
112
112
|
var cond = condFn ? condFn() : false;
|
|
113
113
|
if (cond) {
|
|
114
|
+
var elseA = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
115
|
+
if (elseA) {
|
|
116
|
+
var c = elseA.nextSibling;
|
|
117
|
+
while (c && c !== t) { var n = c.nextSibling; c.remove(); c = n; }
|
|
118
|
+
elseA.remove();
|
|
119
|
+
}
|
|
120
|
+
if (t.dataset.sinthIfPersist === "true" && anchor) return;
|
|
114
121
|
var _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
115
122
|
if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
|
|
116
123
|
var dms = parseInt(t.dataset.sinthIfDelay) || 0;
|
|
@@ -122,6 +129,7 @@ function sinthIfBlock(t) {
|
|
|
122
129
|
}
|
|
123
130
|
} else {
|
|
124
131
|
var runElse = function() {
|
|
132
|
+
if (t.dataset.sinthIfPersist === "true") return;
|
|
125
133
|
if (anchor) {
|
|
126
134
|
if (anchor._sinthReplaced) {
|
|
127
135
|
var insFirst = anchor._sinthInsertedFirst;
|
|
@@ -171,7 +179,9 @@ function sinthIfBlock(t) {
|
|
|
171
179
|
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
172
180
|
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
|
|
173
181
|
}
|
|
182
|
+
var innerTemplates = ef.querySelectorAll('template[data-sinth-if-expr]');
|
|
174
183
|
t.parentNode.insertBefore(ef, t);
|
|
184
|
+
for (var i = 0; i < innerTemplates.length; i++) sinthIfBlock(innerTemplates[i]);
|
|
175
185
|
ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
176
186
|
delete el.dataset.sinthDelayDone;
|
|
177
187
|
sinthDelay(el);
|
|
@@ -183,6 +193,10 @@ function sinthIfBlock(t) {
|
|
|
183
193
|
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
184
194
|
ea2.remove();
|
|
185
195
|
}
|
|
196
|
+
if (anchor) {
|
|
197
|
+
var ac = anchor.nextSibling;
|
|
198
|
+
while (ac && ac !== t) { var an = ac.nextSibling; ac.remove(); ac = an; }
|
|
199
|
+
}
|
|
186
200
|
}
|
|
187
201
|
};
|
|
188
202
|
var elseT = t.nextElementSibling;
|
package/src/core/types.ts
CHANGED
|
@@ -20,7 +20,7 @@ export enum TT {
|
|
|
20
20
|
KW_CUSTOM_EL, KW_CUSTOM,
|
|
21
21
|
KW_IF, KW_ELSE, KW_FOR, KW_IN, KW_REMOVE,
|
|
22
22
|
KW_FUNCTION, KW_RETURN,
|
|
23
|
-
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_SEMI,
|
|
23
|
+
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_MOD, OP_SEMI,
|
|
24
24
|
OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
|
|
25
25
|
OP_ARROW,
|
|
26
26
|
OP_NOT,
|
|
@@ -36,7 +36,7 @@ export type LitNull = { kind: "null" };
|
|
|
36
36
|
export type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
37
37
|
|
|
38
38
|
export type UnaryOp = "not" | "-";
|
|
39
|
-
export type BinaryOp = "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
39
|
+
export type BinaryOp = "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
40
40
|
export type AssignOp = "=" | "+=" | "-=";
|
|
41
41
|
export type PostfixOp = "++" | "--";
|
|
42
42
|
|
|
@@ -61,7 +61,7 @@ export interface TextNode { kind: "text"; value: string;
|
|
|
61
61
|
export interface CompUse { kind: "use"; name: string; attrs: Attr[]; children: Child[]; loc: Loc }
|
|
62
62
|
export interface ExprNode { kind: "expr"; expression: Expression; loc: Loc }
|
|
63
63
|
export interface AssignStmt { kind: "assign_stmt"; expression: Expression; loc: Loc }
|
|
64
|
-
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
|
|
64
|
+
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; persist?: boolean; loc: Loc }
|
|
65
65
|
export interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
66
66
|
export interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
|
|
67
67
|
export interface ReturnStmt { kind: "return"; expression?: Expression; loc: Loc }
|
|
@@ -126,6 +126,7 @@ export interface MixedBlockEntry {
|
|
|
126
126
|
elseJS: string;
|
|
127
127
|
elseHTML: string;
|
|
128
128
|
replaceId?: string;
|
|
129
|
+
persist?: boolean;
|
|
129
130
|
}
|
|
130
131
|
|
|
131
132
|
export interface CompileCtx {
|