@yannosay/sinth 0.14.3 → 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 +3 -1
- package/src/core/lexer.ts +2 -0
- package/src/core/parser.ts +113 -15
- 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
|
@@ -533,6 +533,7 @@ depth: number,
|
|
|
533
533
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
534
534
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
535
535
|
let replaceAttr = "";
|
|
536
|
+
let persistAttr = ifBlock.persist ? ` data-sinth-if-persist="true"` : "";
|
|
536
537
|
const firstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
537
538
|
if (firstComp) {
|
|
538
539
|
const idAttr = firstComp.attrs.find(a => a.name === "id");
|
|
@@ -574,7 +575,7 @@ depth: number,
|
|
|
574
575
|
}
|
|
575
576
|
}
|
|
576
577
|
return (
|
|
577
|
-
`<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>` +
|
|
578
579
|
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}"${elseDelayAttr}${elseDelayHideAttr}>${elseHTML}</template>` : "")
|
|
579
580
|
);
|
|
580
581
|
}
|
|
@@ -1100,6 +1101,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1100
1101
|
}
|
|
1101
1102
|
if (child.kind === "use") {
|
|
1102
1103
|
(child as CompUse).attrs.forEach(a => {
|
|
1104
|
+
if (a.name === "id") return;
|
|
1103
1105
|
if (a.value?.kind === "str") {
|
|
1104
1106
|
const v = a.value.value;
|
|
1105
1107
|
if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
|
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
|
@@ -335,7 +335,36 @@ private parseArrayLiteral(): Literal {
|
|
|
335
335
|
return null;
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
-
|
|
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;
|
|
339
368
|
const t = this.peek().type;
|
|
340
369
|
let op: BinaryOp | null = null;
|
|
341
370
|
if (t === TT.OP_EQEQ) op = "==";
|
|
@@ -344,19 +373,61 @@ private parseArrayLiteral(): Literal {
|
|
|
344
373
|
else if (t === TT.OP_GT) op = ">";
|
|
345
374
|
else if (t === TT.OP_LTEQ) op = "<=";
|
|
346
375
|
else if (t === TT.OP_GTEQ) op = ">=";
|
|
347
|
-
else if (t === TT.OP_PLUS) op = "+";
|
|
348
|
-
else if (t === TT.OP_MINUS) op = "-";
|
|
349
|
-
else if (t === TT.OP_STAR) op = "*";
|
|
350
|
-
else if (t === TT.OP_SLASH) op = "/";
|
|
351
|
-
else if (t === TT.IDENT && this.peek().value === "and") op = "and";
|
|
352
|
-
else if (t === TT.IDENT && this.peek().value === "or") op = "or";
|
|
353
|
-
|
|
354
376
|
if (!op) return left;
|
|
355
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
|
+
}
|
|
356
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++;
|
|
357
397
|
const right = this.parseExpression();
|
|
358
|
-
if (!right)
|
|
359
|
-
|
|
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;
|
|
360
431
|
}
|
|
361
432
|
|
|
362
433
|
private parsePostfix(base: Expression): Expression {
|
|
@@ -507,16 +578,43 @@ private parseArrayLiteral(): Literal {
|
|
|
507
578
|
const condition = this.parseExpression();
|
|
508
579
|
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
509
580
|
|
|
510
|
-
|
|
581
|
+
let persist = false;
|
|
582
|
+
if (this.check(TT.IDENT) && this.peek().value === "persist") {
|
|
583
|
+
this.consume(TT.IDENT);
|
|
584
|
+
persist = true;
|
|
585
|
+
}
|
|
511
586
|
this.consume(TT.LBRACE);
|
|
512
587
|
const body = this.parseUnifiedBody();
|
|
513
588
|
this.consume(TT.RBRACE);
|
|
514
589
|
|
|
515
590
|
let elseBody: Child[] | undefined;
|
|
516
591
|
if (this.check(TT.KW_ELSE)) {
|
|
517
|
-
this.consume(TT.KW_ELSE);
|
|
518
|
-
if (this.check(TT.KW_IF)) {
|
|
519
|
-
|
|
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
|
+
}
|
|
520
618
|
} else {
|
|
521
619
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
522
620
|
this.consume(TT.LBRACE);
|
|
@@ -525,7 +623,7 @@ private parseArrayLiteral(): Literal {
|
|
|
525
623
|
}
|
|
526
624
|
}
|
|
527
625
|
|
|
528
|
-
return { kind: "if", condition, body, elseBody, loc };
|
|
626
|
+
return { kind: "if", condition, body, elseBody, persist, loc };
|
|
529
627
|
}
|
|
530
628
|
|
|
531
629
|
private parseUnifiedBody(): Child[] {
|
|
@@ -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 {
|