@yannosay/sinth 0.18.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -6
- package/package.json +1 -1
- package/src/core/compiler.ts +17 -1
- package/src/core/expr.ts +4 -0
- package/src/core/parser.ts +96 -57
- package/src/core/types.ts +1 -1
package/README.md
CHANGED
|
@@ -169,11 +169,31 @@ Chain statements with ;. No wrapper functions. No script blocks.
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
```ts
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
page
|
|
173
|
+
|
|
174
|
+
title = "Remove Test"
|
|
175
|
+
|
|
176
|
+
var bool show = true
|
|
177
|
+
var bool deleted = false
|
|
178
|
+
|
|
179
|
+
Main {
|
|
180
|
+
Button(onClick: show = not show) {
|
|
181
|
+
if show { "Hide Box" } else { "Show Box" }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
Button(onClick: remove("my-box"); deleted = true) {
|
|
185
|
+
"Delete Box Forever"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if show and not deleted {
|
|
189
|
+
Div(id: "my-box") {
|
|
190
|
+
Paragraph { "I'm a box!" }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
174
193
|
}
|
|
194
|
+
|
|
175
195
|
```
|
|
176
|
-
Delete elements.
|
|
196
|
+
Delete elements. Easy.
|
|
177
197
|
|
|
178
198
|
Compiles to pure HTML.
|
|
179
199
|
|
|
@@ -236,9 +256,9 @@ function Counter(int value) -> ui {
|
|
|
236
256
|
}
|
|
237
257
|
|
|
238
258
|
Main {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
259
|
+
Counter(apples)
|
|
260
|
+
Counter(bananas)
|
|
261
|
+
Counter(oranges)
|
|
242
262
|
}
|
|
243
263
|
```
|
|
244
264
|
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -385,6 +385,12 @@ export function renderChild(
|
|
|
385
385
|
}
|
|
386
386
|
if (child.expression.kind === "call" && child.expression.callee?.kind === "variable") {
|
|
387
387
|
const fnName = child.expression.callee.name;
|
|
388
|
+
if (fnName === "remove" && child.expression.args && child.expression.args.length === 1) {
|
|
389
|
+
const arg = child.expression.args[0];
|
|
390
|
+
if (arg.kind === "literal" && arg.value?.kind === "str") {
|
|
391
|
+
return `<span data-sinth-remove="${esc(arg.value.value)}"></span>`;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
388
394
|
const fnDef = fnName ? ctx.functionDefs.find(f => f.name === fnName) : undefined;
|
|
389
395
|
if (fnDef && fnDef.returnType === "ui") {
|
|
390
396
|
const callArgs = child.expression.args ?? [];
|
|
@@ -1282,6 +1288,16 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1282
1288
|
m.key === "domdiffing" &&
|
|
1283
1289
|
(m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true"))
|
|
1284
1290
|
);
|
|
1291
|
+
const topLevelLogicIfs: string[] = [];
|
|
1292
|
+
file.scripts = file.scripts.filter(s => {
|
|
1293
|
+
const trimmed = s.raw.trim();
|
|
1294
|
+
if (/^if\s*\(/.test(trimmed) || /^\{/.test(trimmed)) {
|
|
1295
|
+
topLevelLogicIfs.push(trimmed);
|
|
1296
|
+
return false;
|
|
1297
|
+
}
|
|
1298
|
+
return true;
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1285
1301
|
const ctx: CompileCtx = {
|
|
1286
1302
|
allDefs, functionDefs, customEls, cssLinks, jsLinks,
|
|
1287
1303
|
scopeHash: hash,
|
|
@@ -1289,7 +1305,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1289
1305
|
extraCSS: [],
|
|
1290
1306
|
mixedBlocks: [],
|
|
1291
1307
|
mixedCounter: 0,
|
|
1292
|
-
logicBlocks: [],
|
|
1308
|
+
logicBlocks: [...topLevelLogicIfs],
|
|
1293
1309
|
ifIdCounter: 0,
|
|
1294
1310
|
exprRegistry: [],
|
|
1295
1311
|
exprMap: new Map(),
|
package/src/core/expr.ts
CHANGED
|
@@ -38,6 +38,10 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
38
38
|
return `${expr.target}${expr.op}`;
|
|
39
39
|
case "call": {
|
|
40
40
|
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
41
|
+
if (callee === "remove") {
|
|
42
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
43
|
+
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
44
|
+
}
|
|
41
45
|
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
42
46
|
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
43
47
|
return callee;
|
package/src/core/parser.ts
CHANGED
|
@@ -274,7 +274,8 @@ private parseArrayLiteral(): Literal {
|
|
|
274
274
|
parseExpression(skipBinary: boolean = false): Expression | null {
|
|
275
275
|
const tok = this.peek();
|
|
276
276
|
|
|
277
|
-
|
|
277
|
+
|
|
278
|
+
// unary 'not'
|
|
278
279
|
if ((tok.type === TT.IDENT && tok.value === "not") || tok.type === TT.OP_NOT) {
|
|
279
280
|
if (tok.type === TT.IDENT) this.consume(TT.IDENT); else this.consume(TT.OP_NOT);
|
|
280
281
|
const operand = this.parseExpression();
|
|
@@ -300,7 +301,17 @@ private parseArrayLiteral(): Literal {
|
|
|
300
301
|
return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
|
|
301
302
|
}
|
|
302
303
|
|
|
304
|
+
// remove as function
|
|
305
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
306
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
307
|
+
return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
|
|
308
|
+
}
|
|
309
|
+
|
|
303
310
|
// var or assignment
|
|
311
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
312
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
313
|
+
return { kind: "variable", name };
|
|
314
|
+
}
|
|
304
315
|
if (tok.type === TT.IDENT) {
|
|
305
316
|
const name = this.consume(TT.IDENT).value;
|
|
306
317
|
let varExpr: Expression;
|
|
@@ -481,6 +492,9 @@ private parseArrayLiteral(): Literal {
|
|
|
481
492
|
private parseFunctionDef(): FunctionDef {
|
|
482
493
|
const loc = this.consume(TT.KW_FUNCTION).loc;
|
|
483
494
|
const name = this.consume(TT.IDENT).value;
|
|
495
|
+
if (name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase()) {
|
|
496
|
+
throw new SinthError(`Function '${name}' must start with lowercase. Use 'component' for uppercase names.`, loc);
|
|
497
|
+
}
|
|
484
498
|
const params = this.parseParamDecls();
|
|
485
499
|
let returnType: VarType | undefined;
|
|
486
500
|
if (this.check(TT.OP_ARROW)) {
|
|
@@ -603,45 +617,53 @@ private parseArrayLiteral(): Literal {
|
|
|
603
617
|
const body = this.parseUnifiedBody();
|
|
604
618
|
this.consume(TT.RBRACE);
|
|
605
619
|
|
|
606
|
-
|
|
607
|
-
if (this.check(TT.KW_ELSE)) {
|
|
608
|
-
const elseTok = this.consume(TT.KW_ELSE);
|
|
609
|
-
if (this.check(TT.KW_IF) || elseTok.value === "elif") {
|
|
610
|
-
if (elseTok.value === "elif") {
|
|
611
|
-
const cond = this.parseExpression();
|
|
612
|
-
if (!cond) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
|
|
613
|
-
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after elif condition", this.peek().loc);
|
|
614
|
-
this.consume(TT.LBRACE);
|
|
615
|
-
const elifBody = this.parseUnifiedBody();
|
|
616
|
-
this.consume(TT.RBRACE);
|
|
617
|
-
const elifBlock: IfBlock = { kind: "if", condition: cond, body: elifBody, loc: elseTok.loc };
|
|
618
|
-
if (this.check(TT.KW_ELSE)) {
|
|
619
|
-
const nextElse = this.consume(TT.KW_ELSE);
|
|
620
|
-
if (this.check(TT.KW_IF) || nextElse.value === "elif") {
|
|
621
|
-
this.pos--;
|
|
622
|
-
elifBlock.elseBody = [this.parseIfBlock()];
|
|
623
|
-
} else {
|
|
624
|
-
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
625
|
-
this.consume(TT.LBRACE);
|
|
626
|
-
elifBlock.elseBody = this.parseUnifiedBody();
|
|
627
|
-
this.consume(TT.RBRACE);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
elseBody = [elifBlock];
|
|
631
|
-
} else {
|
|
632
|
-
elseBody = [this.parseIfBlock()];
|
|
633
|
-
}
|
|
634
|
-
} else {
|
|
635
|
-
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
636
|
-
this.consume(TT.LBRACE);
|
|
637
|
-
elseBody = this.parseUnifiedBody();
|
|
638
|
-
this.consume(TT.RBRACE);
|
|
639
|
-
}
|
|
640
|
-
}
|
|
620
|
+
const elseBody = this.parseElseChain();
|
|
641
621
|
|
|
642
622
|
return { kind: "if", condition, body, elseBody, persist, loc };
|
|
643
623
|
}
|
|
644
624
|
|
|
625
|
+
private parseElseChain(): Child[] | undefined {
|
|
626
|
+
if (!this.check(TT.KW_ELSE)) return undefined;
|
|
627
|
+
|
|
628
|
+
const elseTok = this.consume(TT.KW_ELSE);
|
|
629
|
+
|
|
630
|
+
if (this.check(TT.LBRACE)) {
|
|
631
|
+
this.consume(TT.LBRACE);
|
|
632
|
+
const body = this.parseUnifiedBody();
|
|
633
|
+
this.consume(TT.RBRACE);
|
|
634
|
+
return body;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (this.check(TT.KW_IF) || elseTok.value === "elif") {
|
|
638
|
+
const ifBlock = this.parseIfContinue(elseTok);
|
|
639
|
+
return [ifBlock];
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
throw new SinthError("Expected '{', 'if', or 'elif' after 'else'", this.peek().loc);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
private parseIfContinue(elseTok: Token): IfBlock {
|
|
646
|
+
let condition: Expression;
|
|
647
|
+
|
|
648
|
+
if (elseTok.value === "elif") {
|
|
649
|
+
condition = this.parseExpression()!;
|
|
650
|
+
if (!condition) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
|
|
651
|
+
} else {
|
|
652
|
+
this.consume(TT.KW_IF);
|
|
653
|
+
condition = this.parseExpression()!;
|
|
654
|
+
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after condition", this.peek().loc);
|
|
658
|
+
this.consume(TT.LBRACE);
|
|
659
|
+
const body = this.parseUnifiedBody();
|
|
660
|
+
this.consume(TT.RBRACE);
|
|
661
|
+
|
|
662
|
+
const elseBody = this.parseElseChain();
|
|
663
|
+
|
|
664
|
+
return { kind: "if", condition, body, elseBody, loc: elseTok.loc };
|
|
665
|
+
}
|
|
666
|
+
|
|
645
667
|
private parseRawIfStatement(): string {
|
|
646
668
|
this.consume(TT.KW_IF);
|
|
647
669
|
const cond = this.parseExpression();
|
|
@@ -825,10 +847,19 @@ private parseArrayLiteral(): Literal {
|
|
|
825
847
|
if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
|
|
826
848
|
children.push(this.parseCompUse());
|
|
827
849
|
} else {
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
850
|
+
this.consume(TT.IDENT);
|
|
851
|
+
this.consume(TT.LPAREN);
|
|
852
|
+
const args: Expression[] = [];
|
|
853
|
+
if (!this.check(TT.RPAREN)) {
|
|
854
|
+
args.push(this.parseExpression()!);
|
|
855
|
+
while (this.check(TT.COMMA)) {
|
|
856
|
+
this.consume(TT.COMMA);
|
|
857
|
+
args.push(this.parseExpression()!);
|
|
858
|
+
}
|
|
831
859
|
}
|
|
860
|
+
this.consume(TT.RPAREN);
|
|
861
|
+
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
862
|
+
children.push({ kind: "expr", expression: callExpr, loc });
|
|
832
863
|
}
|
|
833
864
|
continue;
|
|
834
865
|
}
|
|
@@ -1125,8 +1156,8 @@ private parseArrayLiteral(): Literal {
|
|
|
1125
1156
|
continue;
|
|
1126
1157
|
}
|
|
1127
1158
|
else if (this.check(TT.KW_RETURN)) { children.push(this.parseReturnStmt()); continue; }
|
|
1128
|
-
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); }
|
|
1129
|
-
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); }
|
|
1159
|
+
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
|
|
1160
|
+
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
|
|
1130
1161
|
else if (this.check(TT.IDENT)) {
|
|
1131
1162
|
const loc = this.peek().loc;
|
|
1132
1163
|
const rawName = this.peek().value;
|
|
@@ -1249,24 +1280,32 @@ private parseArrayLiteral(): Literal {
|
|
|
1249
1280
|
children.push(this.parseCompUse());
|
|
1250
1281
|
continue;
|
|
1251
1282
|
} else {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
} else if (this.check(TT.IDENT)) {
|
|
1261
|
-
rhs = this.parseExpression();
|
|
1262
|
-
} else {
|
|
1263
|
-
rhs = this.parseExpression(true);
|
|
1264
|
-
}
|
|
1265
|
-
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1266
|
-
finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
|
|
1283
|
+
this.consume(TT.IDENT);
|
|
1284
|
+
this.consume(TT.LPAREN);
|
|
1285
|
+
const args: Expression[] = [];
|
|
1286
|
+
if (!this.check(TT.RPAREN)) {
|
|
1287
|
+
args.push(this.parseExpression()!);
|
|
1288
|
+
while (this.check(TT.COMMA)) {
|
|
1289
|
+
this.consume(TT.COMMA);
|
|
1290
|
+
args.push(this.parseExpression()!);
|
|
1267
1291
|
}
|
|
1268
|
-
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
1269
1292
|
}
|
|
1293
|
+
this.consume(TT.RPAREN);
|
|
1294
|
+
let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
1295
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1296
|
+
this.consume(TT.OP_PLUS);
|
|
1297
|
+
let rhs: Expression | null = null;
|
|
1298
|
+
if (this.check(TT.STRING)) {
|
|
1299
|
+
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1300
|
+
} else if (this.check(TT.IDENT)) {
|
|
1301
|
+
rhs = this.parseExpression();
|
|
1302
|
+
} else {
|
|
1303
|
+
rhs = this.parseExpression(true);
|
|
1304
|
+
}
|
|
1305
|
+
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1306
|
+
finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
|
|
1307
|
+
}
|
|
1308
|
+
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
1270
1309
|
continue;
|
|
1271
1310
|
}
|
|
1272
1311
|
} else if (nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|
package/src/core/types.ts
CHANGED
|
@@ -70,7 +70,7 @@ export interface ComponentExpr { kind: "component_expr"; children: Child[]; loc:
|
|
|
70
70
|
|
|
71
71
|
export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt | ReturnStmt | ComponentExpr;
|
|
72
72
|
|
|
73
|
-
export interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
|
|
73
|
+
export interface ParamDecl { name: string; paramType?: VarType; defaultVal?: Literal; loc: Loc }
|
|
74
74
|
|
|
75
75
|
export interface StyleBlock {
|
|
76
76
|
global: boolean;
|