@yannosay/sinth 0.18.1 → 0.19.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 +3 -3
- package/package.json +1 -1
- package/src/core/compiler.ts +11 -1
- package/src/core/parser.ts +82 -54
- package/src/core/types.ts +1 -1
package/README.md
CHANGED
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -1282,6 +1282,16 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1282
1282
|
m.key === "domdiffing" &&
|
|
1283
1283
|
(m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true"))
|
|
1284
1284
|
);
|
|
1285
|
+
const topLevelLogicIfs: string[] = [];
|
|
1286
|
+
file.scripts = file.scripts.filter(s => {
|
|
1287
|
+
const trimmed = s.raw.trim();
|
|
1288
|
+
if (/^if\s*\(/.test(trimmed) || /^\{/.test(trimmed)) {
|
|
1289
|
+
topLevelLogicIfs.push(trimmed);
|
|
1290
|
+
return false;
|
|
1291
|
+
}
|
|
1292
|
+
return true;
|
|
1293
|
+
});
|
|
1294
|
+
|
|
1285
1295
|
const ctx: CompileCtx = {
|
|
1286
1296
|
allDefs, functionDefs, customEls, cssLinks, jsLinks,
|
|
1287
1297
|
scopeHash: hash,
|
|
@@ -1289,7 +1299,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
1289
1299
|
extraCSS: [],
|
|
1290
1300
|
mixedBlocks: [],
|
|
1291
1301
|
mixedCounter: 0,
|
|
1292
|
-
logicBlocks: [],
|
|
1302
|
+
logicBlocks: [...topLevelLogicIfs],
|
|
1293
1303
|
ifIdCounter: 0,
|
|
1294
1304
|
exprRegistry: [],
|
|
1295
1305
|
exprMap: new Map(),
|
package/src/core/parser.ts
CHANGED
|
@@ -481,6 +481,9 @@ private parseArrayLiteral(): Literal {
|
|
|
481
481
|
private parseFunctionDef(): FunctionDef {
|
|
482
482
|
const loc = this.consume(TT.KW_FUNCTION).loc;
|
|
483
483
|
const name = this.consume(TT.IDENT).value;
|
|
484
|
+
if (name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase()) {
|
|
485
|
+
throw new SinthError(`Function '${name}' must start with lowercase. Use 'component' for uppercase names.`, loc);
|
|
486
|
+
}
|
|
484
487
|
const params = this.parseParamDecls();
|
|
485
488
|
let returnType: VarType | undefined;
|
|
486
489
|
if (this.check(TT.OP_ARROW)) {
|
|
@@ -603,45 +606,53 @@ private parseArrayLiteral(): Literal {
|
|
|
603
606
|
const body = this.parseUnifiedBody();
|
|
604
607
|
this.consume(TT.RBRACE);
|
|
605
608
|
|
|
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
|
-
}
|
|
609
|
+
const elseBody = this.parseElseChain();
|
|
641
610
|
|
|
642
611
|
return { kind: "if", condition, body, elseBody, persist, loc };
|
|
643
612
|
}
|
|
644
613
|
|
|
614
|
+
private parseElseChain(): Child[] | undefined {
|
|
615
|
+
if (!this.check(TT.KW_ELSE)) return undefined;
|
|
616
|
+
|
|
617
|
+
const elseTok = this.consume(TT.KW_ELSE);
|
|
618
|
+
|
|
619
|
+
if (this.check(TT.LBRACE)) {
|
|
620
|
+
this.consume(TT.LBRACE);
|
|
621
|
+
const body = this.parseUnifiedBody();
|
|
622
|
+
this.consume(TT.RBRACE);
|
|
623
|
+
return body;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
if (this.check(TT.KW_IF) || elseTok.value === "elif") {
|
|
627
|
+
const ifBlock = this.parseIfContinue(elseTok);
|
|
628
|
+
return [ifBlock];
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
throw new SinthError("Expected '{', 'if', or 'elif' after 'else'", this.peek().loc);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
private parseIfContinue(elseTok: Token): IfBlock {
|
|
635
|
+
let condition: Expression;
|
|
636
|
+
|
|
637
|
+
if (elseTok.value === "elif") {
|
|
638
|
+
condition = this.parseExpression()!;
|
|
639
|
+
if (!condition) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
|
|
640
|
+
} else {
|
|
641
|
+
this.consume(TT.KW_IF);
|
|
642
|
+
condition = this.parseExpression()!;
|
|
643
|
+
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after condition", this.peek().loc);
|
|
647
|
+
this.consume(TT.LBRACE);
|
|
648
|
+
const body = this.parseUnifiedBody();
|
|
649
|
+
this.consume(TT.RBRACE);
|
|
650
|
+
|
|
651
|
+
const elseBody = this.parseElseChain();
|
|
652
|
+
|
|
653
|
+
return { kind: "if", condition, body, elseBody, loc: elseTok.loc };
|
|
654
|
+
}
|
|
655
|
+
|
|
645
656
|
private parseRawIfStatement(): string {
|
|
646
657
|
this.consume(TT.KW_IF);
|
|
647
658
|
const cond = this.parseExpression();
|
|
@@ -825,10 +836,19 @@ private parseArrayLiteral(): Literal {
|
|
|
825
836
|
if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
|
|
826
837
|
children.push(this.parseCompUse());
|
|
827
838
|
} else {
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
839
|
+
this.consume(TT.IDENT);
|
|
840
|
+
this.consume(TT.LPAREN);
|
|
841
|
+
const args: Expression[] = [];
|
|
842
|
+
if (!this.check(TT.RPAREN)) {
|
|
843
|
+
args.push(this.parseExpression()!);
|
|
844
|
+
while (this.check(TT.COMMA)) {
|
|
845
|
+
this.consume(TT.COMMA);
|
|
846
|
+
args.push(this.parseExpression()!);
|
|
847
|
+
}
|
|
831
848
|
}
|
|
849
|
+
this.consume(TT.RPAREN);
|
|
850
|
+
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
851
|
+
children.push({ kind: "expr", expression: callExpr, loc });
|
|
832
852
|
}
|
|
833
853
|
continue;
|
|
834
854
|
}
|
|
@@ -1249,24 +1269,32 @@ private parseArrayLiteral(): Literal {
|
|
|
1249
1269
|
children.push(this.parseCompUse());
|
|
1250
1270
|
continue;
|
|
1251
1271
|
} else {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1272
|
+
this.consume(TT.IDENT);
|
|
1273
|
+
this.consume(TT.LPAREN);
|
|
1274
|
+
const args: Expression[] = [];
|
|
1275
|
+
if (!this.check(TT.RPAREN)) {
|
|
1276
|
+
args.push(this.parseExpression()!);
|
|
1277
|
+
while (this.check(TT.COMMA)) {
|
|
1278
|
+
this.consume(TT.COMMA);
|
|
1279
|
+
args.push(this.parseExpression()!);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
this.consume(TT.RPAREN);
|
|
1283
|
+
let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
1284
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1285
|
+
this.consume(TT.OP_PLUS);
|
|
1286
|
+
let rhs: Expression | null = null;
|
|
1287
|
+
if (this.check(TT.STRING)) {
|
|
1288
|
+
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1289
|
+
} else if (this.check(TT.IDENT)) {
|
|
1290
|
+
rhs = this.parseExpression();
|
|
1291
|
+
} else {
|
|
1292
|
+
rhs = this.parseExpression(true);
|
|
1267
1293
|
}
|
|
1268
|
-
|
|
1294
|
+
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1295
|
+
finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
|
|
1269
1296
|
}
|
|
1297
|
+
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
1270
1298
|
continue;
|
|
1271
1299
|
}
|
|
1272
1300
|
} 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;
|