@yannosay/sinth 0.5.3 → 0.6.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 +113 -32
- package/package.json +43 -43
- package/src/sinth.ts +276 -263
package/src/sinth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Sinth Compiler
|
|
2
|
+
// Sinth Compiler 0.5.4 — Unified if/else, Sinth Style language, tree-shaken runtime
|
|
3
3
|
// Single-file TypeScript compiler for the Sinth declarative web language.
|
|
4
4
|
// Run with: npx ts-node sinth.ts build | sinth build | sinth dev
|
|
5
5
|
|
|
@@ -7,13 +7,10 @@ import * as fs from "fs";
|
|
|
7
7
|
import * as path from "path";
|
|
8
8
|
import * as http from "http";
|
|
9
9
|
|
|
10
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
11
|
-
// §1 TYPES & AST DEFINITIONS
|
|
12
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
13
10
|
|
|
14
11
|
interface Loc { file: string; line: number; col: number }
|
|
15
12
|
|
|
16
|
-
//
|
|
13
|
+
// plain `enum TT` so TT[t] string-indexing works at runtime.
|
|
17
14
|
enum TT {
|
|
18
15
|
LBRACE, RBRACE, LPAREN, RPAREN, COLON, COMMA, EQUALS, DOT,
|
|
19
16
|
LBRACKET, RBRACKET,
|
|
@@ -32,21 +29,21 @@ enum TT {
|
|
|
32
29
|
|
|
33
30
|
interface Token { type: TT; value: string; loc: Loc }
|
|
34
31
|
|
|
35
|
-
//
|
|
32
|
+
// literal types
|
|
36
33
|
type LitStr = { kind: "str"; value: string };
|
|
37
34
|
type LitNum = { kind: "num"; value: number };
|
|
38
35
|
type LitBool = { kind: "bool"; value: boolean };
|
|
39
36
|
type LitNull = { kind: "null" };
|
|
40
37
|
type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
41
38
|
|
|
42
|
-
//
|
|
39
|
+
// expression AST
|
|
43
40
|
type UnaryOp = "not" | "-";
|
|
44
41
|
type BinaryOp = "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
45
42
|
type AssignOp = "=" | "+=" | "-=";
|
|
46
43
|
type PostfixOp = "++" | "--";
|
|
47
44
|
|
|
48
45
|
interface Expression {
|
|
49
|
-
kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix";
|
|
46
|
+
kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index";
|
|
50
47
|
value?: Literal;
|
|
51
48
|
name?: string;
|
|
52
49
|
op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;
|
|
@@ -54,9 +51,10 @@ interface Expression {
|
|
|
54
51
|
left?: Expression;
|
|
55
52
|
right?: Expression;
|
|
56
53
|
target?: string;
|
|
54
|
+
object?: Expression;
|
|
55
|
+
key?: Expression;
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
// ── AST node types ───────────────────────────────────────────────────────────
|
|
60
58
|
interface Attr { name: string; value: Literal | null; loc: Loc }
|
|
61
59
|
interface TextNode { kind: "text"; value: string; loc: Loc }
|
|
62
60
|
interface CompUse { kind: "use"; name: string; attrs: Attr[]; children: Child[]; loc: Loc }
|
|
@@ -65,17 +63,17 @@ interface AssignStmt { kind: "assign_stmt"; expression: Expression;
|
|
|
65
63
|
interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
|
|
66
64
|
interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
67
65
|
|
|
68
|
-
//
|
|
66
|
+
// assignStmt is a first-class child so if bodies can be mixed
|
|
69
67
|
type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop;
|
|
70
68
|
|
|
71
|
-
//
|
|
69
|
+
// component / file structure
|
|
72
70
|
interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
|
|
73
71
|
|
|
74
72
|
interface StyleBlock {
|
|
75
73
|
global: boolean;
|
|
76
74
|
lang: "css" | "scss";
|
|
77
75
|
raw: string;
|
|
78
|
-
target?: string;
|
|
76
|
+
target?: string;
|
|
79
77
|
extraAttrs: Record<string, string>;
|
|
80
78
|
loc: Loc;
|
|
81
79
|
}
|
|
@@ -108,7 +106,6 @@ interface SinthFile {
|
|
|
108
106
|
varDecls: VarDeclaration[];
|
|
109
107
|
}
|
|
110
108
|
|
|
111
|
-
// ── Mixed if/else block tracking ─────────────────────────────────────────────
|
|
112
109
|
interface MixedBlockEntry {
|
|
113
110
|
id: string;
|
|
114
111
|
conditionJS: string;
|
|
@@ -132,10 +129,6 @@ interface CompileCtx {
|
|
|
132
129
|
logicBlocks: string[];
|
|
133
130
|
}
|
|
134
131
|
|
|
135
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
136
|
-
// §2 UTILITIES
|
|
137
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
138
|
-
|
|
139
132
|
class SinthError extends Error {
|
|
140
133
|
constructor(
|
|
141
134
|
public readonly rawMsg: string,
|
|
@@ -183,7 +176,6 @@ function fnv1a(s: string): string {
|
|
|
183
176
|
return h.toString(16).padStart(8, "0").slice(0, 8);
|
|
184
177
|
}
|
|
185
178
|
|
|
186
|
-
/** camelCase → kebab-case with vendor prefix support */
|
|
187
179
|
function camelToKebab(s: string): string {
|
|
188
180
|
const vp = s.match(/^(Webkit|Moz|Ms)(.+)$/);
|
|
189
181
|
if (vp) return `-${vp[1].toLowerCase()}-${camelToKebab(vp[2])}`;
|
|
@@ -212,10 +204,7 @@ function litToString(lit: Literal): string {
|
|
|
212
204
|
}
|
|
213
205
|
}
|
|
214
206
|
|
|
215
|
-
//
|
|
216
|
-
// §3 LEXER
|
|
217
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
218
|
-
|
|
207
|
+
// lexer
|
|
219
208
|
const KEYWORDS: Record<string, TT> = {
|
|
220
209
|
"import": TT.KW_IMPORT,
|
|
221
210
|
"as": TT.KW_AS,
|
|
@@ -263,7 +252,7 @@ export class Lexer {
|
|
|
263
252
|
if (ch === "[") { tokens.push(this.single(TT.LBRACKET, loc)); continue; }
|
|
264
253
|
if (ch === "]") { tokens.push(this.single(TT.RBRACKET, loc)); continue; }
|
|
265
254
|
|
|
266
|
-
//
|
|
255
|
+
// two-character operators ->
|
|
267
256
|
if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
|
|
268
257
|
if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
|
|
269
258
|
if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
|
|
@@ -321,7 +310,7 @@ export class Lexer {
|
|
|
321
310
|
const ch = this.src[this.pos];
|
|
322
311
|
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") { this.adv(); continue; }
|
|
323
312
|
|
|
324
|
-
//
|
|
313
|
+
// sinth comments: -- line or --[ nestable block ]--
|
|
325
314
|
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
326
315
|
if (this.src[this.pos + 2] === "[") {
|
|
327
316
|
this.adv(); this.adv(); this.adv();
|
|
@@ -478,9 +467,7 @@ export class Lexer {
|
|
|
478
467
|
private isIdentCont(c: string) { return /[a-zA-Z0-9_\-\u00C0-\uFFFF]/.test(c); }
|
|
479
468
|
}
|
|
480
469
|
|
|
481
|
-
//
|
|
482
|
-
// §4 PARSER
|
|
483
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
470
|
+
// parser
|
|
484
471
|
|
|
485
472
|
export class Parser {
|
|
486
473
|
private pos = 0;
|
|
@@ -490,8 +477,8 @@ export class Parser {
|
|
|
490
477
|
|
|
491
478
|
constructor(private tokens: Token[], private file: string) {}
|
|
492
479
|
|
|
493
|
-
// ── Top-level file parse ────────────────────────────────────────────────
|
|
494
480
|
|
|
481
|
+
// top-level file parse
|
|
495
482
|
parse(): SinthFile {
|
|
496
483
|
let isPage = false;
|
|
497
484
|
const imports: ImportNode[] = [];
|
|
@@ -514,13 +501,13 @@ export class Parser {
|
|
|
514
501
|
if (entry.key === "title") isPage = true;
|
|
515
502
|
}
|
|
516
503
|
|
|
517
|
-
|
|
504
|
+
|
|
518
505
|
while (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
519
506
|
const vd = this.parseVarDeclaration();
|
|
520
507
|
if (vd) { varDecls.push(vd); }
|
|
521
508
|
}
|
|
522
509
|
|
|
523
|
-
|
|
510
|
+
|
|
524
511
|
while (!this.check(TT.EOF)) {
|
|
525
512
|
if (this.check(TT.KW_COMPONENT)) { defs.push(this.parseCompDef()); }
|
|
526
513
|
else if (this.check(TT.KW_STYLE)) { styles.push(this.parseStyleBlock()); }
|
|
@@ -543,8 +530,7 @@ const savedPos = this.pos;
|
|
|
543
530
|
const identName = this.peek().value;
|
|
544
531
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
545
532
|
|
|
546
|
-
//
|
|
547
|
-
// This prevents the parser from treating "guess" as a component and failing later on "=="
|
|
533
|
+
// detect accidental top-level expressions (guess == target" instead of "if guess == target")
|
|
548
534
|
const isComparison = [TT.OP_EQEQ, TT.OP_NEQ, TT.OP_LT, TT.OP_GT, TT.OP_LTEQ, TT.OP_GTEQ].includes(nextType);
|
|
549
535
|
const isLogicAndOr = nextType === TT.IDENT && (this.tokens[this.pos + 1]?.value === "and" || this.tokens[this.pos + 1]?.value === "or");
|
|
550
536
|
|
|
@@ -555,7 +541,7 @@ if (isComparison || isLogicAndOr) {
|
|
|
555
541
|
);
|
|
556
542
|
}
|
|
557
543
|
|
|
558
|
-
//
|
|
544
|
+
// top-level assignment: ident = expr
|
|
559
545
|
if (nextType === TT.EQUALS ||
|
|
560
546
|
(nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) ||
|
|
561
547
|
(nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS)) {
|
|
@@ -573,7 +559,7 @@ sinthRender();`;
|
|
|
573
559
|
continue;
|
|
574
560
|
}
|
|
575
561
|
|
|
576
|
-
//
|
|
562
|
+
// component usage
|
|
577
563
|
this.pos = savedPos;
|
|
578
564
|
uses.push(this.parseCompUse());
|
|
579
565
|
}
|
|
@@ -586,19 +572,13 @@ uses.push(this.parseCompUse());
|
|
|
586
572
|
return { filePath: this.file, isPage, imports, meta, defs, uses, styles, scripts, customEls, varDecls };
|
|
587
573
|
}
|
|
588
574
|
|
|
589
|
-
/** Flatten a top-level IfBlock into uses/scripts arrays so rendering works. */
|
|
590
575
|
private flattenIfToUses(ifNode: IfBlock, scripts: ScriptBlock[]): CompUse[] {
|
|
591
|
-
// Pure logic (only AssignStmt children): emit as script block
|
|
592
576
|
const bodyHasComp = ifNode.body.some(c => c.kind === "use");
|
|
593
577
|
const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
|
|
594
578
|
if (!bodyHasComp && !elseHasComp) {
|
|
595
579
|
scripts.push({ raw: compileIfToJS(ifNode), attrs: {}, loc: ifNode.loc });
|
|
596
580
|
return [];
|
|
597
581
|
}
|
|
598
|
-
// Otherwise keep it embedded — it will be rendered as a child
|
|
599
|
-
// We wrap it in a synthetic CompUse placeholder so it flows through renderCompUse
|
|
600
|
-
// Actually: embed the IfBlock in a fake wrapper use
|
|
601
|
-
// We'll handle it by emitting a special "sinth-if-root" use
|
|
602
582
|
const syntheticUse: CompUse = {
|
|
603
583
|
kind: "use",
|
|
604
584
|
name: "__IF_ROOT__",
|
|
@@ -609,7 +589,7 @@ uses.push(this.parseCompUse());
|
|
|
609
589
|
return [syntheticUse];
|
|
610
590
|
}
|
|
611
591
|
|
|
612
|
-
//
|
|
592
|
+
// imports
|
|
613
593
|
|
|
614
594
|
private parseImport(): ImportNode {
|
|
615
595
|
const loc = this.consume(TT.KW_IMPORT).loc;
|
|
@@ -629,7 +609,7 @@ uses.push(this.parseCompUse());
|
|
|
629
609
|
throw new SinthError(`Expected import path, library name, or 'css'`, loc);
|
|
630
610
|
}
|
|
631
611
|
|
|
632
|
-
//
|
|
612
|
+
// metadata
|
|
633
613
|
|
|
634
614
|
private isMetaStart(): boolean {
|
|
635
615
|
if (!this.check(TT.IDENT)) return false;
|
|
@@ -643,7 +623,7 @@ uses.push(this.parseCompUse());
|
|
|
643
623
|
return { key, value: this.parseLiteral(), loc };
|
|
644
624
|
}
|
|
645
625
|
|
|
646
|
-
//
|
|
626
|
+
// var declarations
|
|
647
627
|
|
|
648
628
|
private parseVarDeclaration(): VarDeclaration | null {
|
|
649
629
|
const loc = this.peek().loc;
|
|
@@ -731,17 +711,8 @@ private parseArrayLiteral(): Literal {
|
|
|
731
711
|
return obj;
|
|
732
712
|
}
|
|
733
713
|
|
|
734
|
-
//
|
|
714
|
+
// expression parsing
|
|
735
715
|
|
|
736
|
-
/**
|
|
737
|
-
* Parses an expression. Handles:
|
|
738
|
-
* - Literals (string, number, bool, null)
|
|
739
|
-
* - Variable references
|
|
740
|
-
* - Parenthesised sub-expressions
|
|
741
|
-
* - Unary "not" / "-"
|
|
742
|
-
* - Binary comparisons (==, !=, <, >, <=, >=, +, -, *, /)
|
|
743
|
-
* - Assignments (=, +=, -=)
|
|
744
|
-
*/
|
|
745
716
|
parseExpression(): Expression | null {
|
|
746
717
|
const tok = this.peek();
|
|
747
718
|
|
|
@@ -750,93 +721,77 @@ private parseArrayLiteral(): Literal {
|
|
|
750
721
|
this.consume(TT.IDENT);
|
|
751
722
|
const operand = this.parseExpression();
|
|
752
723
|
if (!operand) throw new SinthError("Expected expression after 'not'", tok.loc);
|
|
753
|
-
|
|
724
|
+
const unaryExpr: Expression = { kind: "unary", op: "not", operand };
|
|
725
|
+
return this.parseBinaryRHS(this.parsePostfix(unaryExpr));
|
|
754
726
|
}
|
|
755
727
|
|
|
756
|
-
// Parenthesised expression
|
|
757
728
|
if (this.check(TT.LPAREN)) {
|
|
758
729
|
this.consume(TT.LPAREN);
|
|
759
730
|
const inner = this.parseExpression();
|
|
760
731
|
if (!inner) throw new SinthError("Expected expression after '('", this.peek().loc);
|
|
761
732
|
if (!this.check(TT.RPAREN)) throw new SinthError("Expected closing ')'", this.peek().loc);
|
|
762
733
|
this.consume(TT.RPAREN);
|
|
763
|
-
|
|
764
|
-
return this.parseBinaryRHS(inner);
|
|
734
|
+
return this.parseBinaryRHS(this.parsePostfix(inner));
|
|
765
735
|
}
|
|
766
736
|
|
|
767
|
-
//
|
|
737
|
+
// literal
|
|
768
738
|
if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) ||
|
|
769
739
|
this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
|
|
770
740
|
const lit = this.parseLiteral();
|
|
771
741
|
const base: Expression = { kind: "literal", value: lit };
|
|
772
|
-
return this.parseBinaryRHS(base);
|
|
742
|
+
return this.parseBinaryRHS(this.parsePostfix(base));
|
|
773
743
|
}
|
|
774
744
|
|
|
775
|
-
//
|
|
745
|
+
// var or assignment
|
|
776
746
|
if (tok.type === TT.IDENT) {
|
|
777
747
|
const name = this.consume(TT.IDENT).value;
|
|
778
|
-
|
|
779
|
-
|
|
748
|
+
let varExpr: Expression;
|
|
749
|
+
|
|
750
|
+
// dot notation -> user.age
|
|
780
751
|
if (this.check(TT.DOT)) {
|
|
781
752
|
this.consume(TT.DOT);
|
|
782
753
|
const propName = this.consume(TT.IDENT).value;
|
|
783
754
|
const fullName = `${name}.${propName}`;
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
const rhs = this.parseExpression();
|
|
796
|
-
if (!rhs) throw new SinthError("Expected expression after -=", this.peek().loc);
|
|
797
|
-
return { kind: "assign", target: fullName, op: "-=", right: rhs };
|
|
798
|
-
}
|
|
799
|
-
if (nextType === TT.EQUALS) {
|
|
800
|
-
this.consume(TT.EQUALS);
|
|
801
|
-
const rhs = this.parseExpression();
|
|
802
|
-
if (!rhs) throw new SinthError("Expected expression after =", this.peek().loc);
|
|
803
|
-
return { kind: "assign", target: fullName, op: "=", right: rhs };
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
const varExpr: Expression = { kind: "variable", name: fullName };
|
|
807
|
-
return this.parseBinaryRHS(varExpr);
|
|
755
|
+
varExpr = { kind: "variable", name: fullName };
|
|
756
|
+
} else {
|
|
757
|
+
varExpr = { kind: "variable", name };
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// apply bracket postfixes (e.g. data[key], data["x"])
|
|
761
|
+
const indexedExpr = this.parsePostfix(varExpr);
|
|
762
|
+
|
|
763
|
+
// if indexing was applied, we don't allow assignment, just binary RHS
|
|
764
|
+
if (indexedExpr !== varExpr) {
|
|
765
|
+
return this.parseBinaryRHS(indexedExpr);
|
|
808
766
|
}
|
|
809
767
|
|
|
810
|
-
//
|
|
768
|
+
// no indexing – check for assignment operators
|
|
811
769
|
const nextType = this.tokens[this.pos]?.type;
|
|
812
770
|
if (nextType === TT.OP_PLUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
813
771
|
this.consume(TT.OP_PLUS); this.consume(TT.EQUALS);
|
|
814
772
|
const rhs = this.parseExpression();
|
|
815
773
|
if (!rhs) throw new SinthError("Expected expression after +=", this.peek().loc);
|
|
816
|
-
return { kind: "assign", target: name
|
|
774
|
+
return { kind: "assign", target: varExpr.name!, op: "+=", right: rhs };
|
|
817
775
|
}
|
|
818
776
|
if (nextType === TT.OP_MINUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
819
777
|
this.consume(TT.OP_MINUS); this.consume(TT.EQUALS);
|
|
820
778
|
const rhs = this.parseExpression();
|
|
821
779
|
if (!rhs) throw new SinthError("Expected expression after -=", this.peek().loc);
|
|
822
|
-
return { kind: "assign", target: name
|
|
780
|
+
return { kind: "assign", target: varExpr.name!, op: "-=", right: rhs };
|
|
823
781
|
}
|
|
824
|
-
// Simple assignment
|
|
825
782
|
if (nextType === TT.EQUALS) {
|
|
826
783
|
this.consume(TT.EQUALS);
|
|
827
784
|
const rhs = this.parseExpression();
|
|
828
785
|
if (!rhs) throw new SinthError("Expected expression after =", this.peek().loc);
|
|
829
|
-
return { kind: "assign", target: name
|
|
786
|
+
return { kind: "assign", target: varExpr.name!, op: "=", right: rhs };
|
|
830
787
|
}
|
|
831
788
|
|
|
832
|
-
const varExpr: Expression = { kind: "variable", name };
|
|
833
789
|
return this.parseBinaryRHS(varExpr);
|
|
834
790
|
}
|
|
835
791
|
|
|
836
792
|
return null;
|
|
837
793
|
}
|
|
838
794
|
|
|
839
|
-
/** After parsing a left-hand operand, check for a binary operator and parse RHS. */
|
|
840
795
|
private parseBinaryRHS(left: Expression): Expression {
|
|
841
796
|
const t = this.peek().type;
|
|
842
797
|
let op: BinaryOp | null = null;
|
|
@@ -861,7 +816,20 @@ private parseArrayLiteral(): Literal {
|
|
|
861
816
|
return { kind: "binary", left, op, right };
|
|
862
817
|
}
|
|
863
818
|
|
|
864
|
-
|
|
819
|
+
private parsePostfix(base: Expression): Expression {
|
|
820
|
+
let expr = base;
|
|
821
|
+
while (this.check(TT.LBRACKET)) {
|
|
822
|
+
this.consume(TT.LBRACKET);
|
|
823
|
+
const key = this.parseExpression();
|
|
824
|
+
if (!key) throw new SinthError("Expected expression inside [...]", this.peek().loc);
|
|
825
|
+
this.consume(TT.RBRACKET);
|
|
826
|
+
expr = { kind: "index", object: expr, key };
|
|
827
|
+
}
|
|
828
|
+
return expr;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
// component definitions
|
|
865
833
|
|
|
866
834
|
private parseCompDef(): CompDef {
|
|
867
835
|
const loc = this.consume(TT.KW_COMPONENT).loc;
|
|
@@ -934,7 +902,7 @@ private parseArrayLiteral(): Literal {
|
|
|
934
902
|
return { body, styles, scripts };
|
|
935
903
|
}
|
|
936
904
|
|
|
937
|
-
//
|
|
905
|
+
// if / else blocks
|
|
938
906
|
|
|
939
907
|
private parseIfBlock(): IfBlock {
|
|
940
908
|
const loc = this.consume(TT.KW_IF).loc;
|
|
@@ -950,7 +918,6 @@ private parseArrayLiteral(): Literal {
|
|
|
950
918
|
if (this.check(TT.KW_ELSE)) {
|
|
951
919
|
this.consume(TT.KW_ELSE);
|
|
952
920
|
if (this.check(TT.KW_IF)) {
|
|
953
|
-
// else if → treat as nested IfBlock
|
|
954
921
|
elseBody = [this.parseIfBlock()];
|
|
955
922
|
} else {
|
|
956
923
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
@@ -963,10 +930,6 @@ private parseArrayLiteral(): Literal {
|
|
|
963
930
|
return { kind: "if", condition, body, elseBody, loc };
|
|
964
931
|
}
|
|
965
932
|
|
|
966
|
-
/**
|
|
967
|
-
* v1.0.0: Parses a body that may contain ANY mix of assignment statements and
|
|
968
|
-
* component usages. This replaces the old split between "inline logic" and "DOM if".
|
|
969
|
-
*/
|
|
970
933
|
private parseUnifiedBody(): Child[] {
|
|
971
934
|
const children: Child[] = [];
|
|
972
935
|
|
|
@@ -979,7 +942,6 @@ private parseArrayLiteral(): Literal {
|
|
|
979
942
|
continue;
|
|
980
943
|
}
|
|
981
944
|
|
|
982
|
-
// Variable declarations inside unified bodies
|
|
983
945
|
if (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
984
946
|
const vd = this.parseVarDeclaration();
|
|
985
947
|
if (vd) this._varDecls.push(vd);
|
|
@@ -992,7 +954,6 @@ private parseArrayLiteral(): Literal {
|
|
|
992
954
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
993
955
|
const nextNextType = this.tokens[this.pos + 2]?.type;
|
|
994
956
|
|
|
995
|
-
// Handle dot notation for assignments: user.age = value
|
|
996
957
|
if (nextType === TT.DOT && (nextNextType === TT.EQUALS ||
|
|
997
958
|
(nextNextType === TT.OP_PLUS && this.tokens[this.pos + 3]?.type === TT.EQUALS) ||
|
|
998
959
|
(nextNextType === TT.OP_MINUS && this.tokens[this.pos + 3]?.type === TT.EQUALS))) {
|
|
@@ -1015,7 +976,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1015
976
|
continue;
|
|
1016
977
|
}
|
|
1017
978
|
|
|
1018
|
-
//
|
|
979
|
+
// assignment: ident = expr | ident += expr | ident -= expr
|
|
1019
980
|
const isSimpleAssign = nextType === TT.EQUALS;
|
|
1020
981
|
const isPlusAssign = nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
|
|
1021
982
|
const isMinusAssign = nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
|
|
@@ -1039,8 +1000,8 @@ private parseArrayLiteral(): Literal {
|
|
|
1039
1000
|
continue;
|
|
1040
1001
|
}
|
|
1041
1002
|
|
|
1042
|
-
//
|
|
1043
|
-
//
|
|
1003
|
+
// var expression
|
|
1004
|
+
// check dot notation variable reference (user.age without assignment)
|
|
1044
1005
|
if (nextType === TT.DOT) {
|
|
1045
1006
|
let fullName = this.consume(TT.IDENT).value;
|
|
1046
1007
|
this.consume(TT.DOT);
|
|
@@ -1049,7 +1010,17 @@ private parseArrayLiteral(): Literal {
|
|
|
1049
1010
|
continue;
|
|
1050
1011
|
}
|
|
1051
1012
|
|
|
1052
|
-
//
|
|
1013
|
+
// bracket notation: ident[...]
|
|
1014
|
+
if (nextType === TT.LBRACKET) {
|
|
1015
|
+
this.consume(TT.IDENT);
|
|
1016
|
+
const varExpr: Expression = { kind: "variable", name };
|
|
1017
|
+
const indexedExpr = this.parsePostfix(varExpr);
|
|
1018
|
+
children.push({ kind: "expr", expression: indexedExpr, loc });
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
// simple variable reference
|
|
1053
1024
|
this.consume(TT.IDENT);
|
|
1054
1025
|
children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
|
|
1055
1026
|
continue;
|
|
@@ -1064,7 +1035,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1064
1035
|
return children;
|
|
1065
1036
|
}
|
|
1066
1037
|
|
|
1067
|
-
//
|
|
1038
|
+
// for loops
|
|
1068
1039
|
|
|
1069
1040
|
private parseForLoop(): ForLoop {
|
|
1070
1041
|
const loc = this.consume(TT.KW_FOR).loc;
|
|
@@ -1101,7 +1072,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1101
1072
|
return { kind: "for", keyVar, itemVar, indexVar, arrayVar, body, loc };
|
|
1102
1073
|
}
|
|
1103
1074
|
|
|
1104
|
-
//
|
|
1075
|
+
// component usages
|
|
1105
1076
|
|
|
1106
1077
|
private parseCompUse(): CompUse {
|
|
1107
1078
|
const loc = this.peek().loc;
|
|
@@ -1135,13 +1106,11 @@ private parseArrayLiteral(): Literal {
|
|
|
1135
1106
|
this.check(TT.NULL_LIT) || this.check(TT.LBRACKET) || this.check(TT.LBRACE)) {
|
|
1136
1107
|
value = this.parseLiteral();
|
|
1137
1108
|
} else if (this.check(TT.IDENT) || this.check(TT.LPAREN)) {
|
|
1138
|
-
// Try to parse as expression (handles user.age, user.age * 20, etc.)
|
|
1139
1109
|
const savedPos = this.pos;
|
|
1140
1110
|
const expr = this.parseExpression();
|
|
1141
1111
|
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN))) {
|
|
1142
1112
|
value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
|
|
1143
1113
|
} else {
|
|
1144
|
-
// Not a full expression, treat as simple variable reference
|
|
1145
1114
|
this.pos = savedPos;
|
|
1146
1115
|
if (this.check(TT.IDENT)) {
|
|
1147
1116
|
value = { kind: "str", value: this.consume(TT.IDENT).value };
|
|
@@ -1196,6 +1165,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1196
1165
|
if (this.check(TT.DOT)) {
|
|
1197
1166
|
this.consume(TT.DOT);
|
|
1198
1167
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1168
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1169
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1170
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1199
1171
|
} else {
|
|
1200
1172
|
rhs = { kind: "variable", name: rhsName };
|
|
1201
1173
|
}
|
|
@@ -1219,7 +1191,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1219
1191
|
const rawName = this.peek().value;
|
|
1220
1192
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
1221
1193
|
|
|
1222
|
-
// Handle dot notation: user.age
|
|
1223
1194
|
if (nextType === TT.DOT) {
|
|
1224
1195
|
let fullName = this.consume(TT.IDENT).value;
|
|
1225
1196
|
this.consume(TT.DOT);
|
|
@@ -1227,7 +1198,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1227
1198
|
const afterDotType = this.tokens[this.pos]?.type;
|
|
1228
1199
|
|
|
1229
1200
|
if (afterDotType === TT.OP_PLUS) {
|
|
1230
|
-
// user.name + "string" + user.age
|
|
1231
1201
|
let leftExpr: Expression = { kind: "variable", name: fullName };
|
|
1232
1202
|
while (this.check(TT.OP_PLUS)) {
|
|
1233
1203
|
this.consume(TT.OP_PLUS);
|
|
@@ -1239,6 +1209,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1239
1209
|
if (this.check(TT.DOT)) {
|
|
1240
1210
|
this.consume(TT.DOT);
|
|
1241
1211
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1212
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1213
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1214
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1242
1215
|
} else {
|
|
1243
1216
|
rhs = { kind: "variable", name: rhsName };
|
|
1244
1217
|
}
|
|
@@ -1269,6 +1242,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1269
1242
|
if (this.check(TT.DOT)) {
|
|
1270
1243
|
this.consume(TT.DOT);
|
|
1271
1244
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1245
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1246
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1247
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1272
1248
|
} else {
|
|
1273
1249
|
rhs = { kind: "variable", name: rhsName };
|
|
1274
1250
|
}
|
|
@@ -1282,6 +1258,39 @@ private parseArrayLiteral(): Literal {
|
|
|
1282
1258
|
} else if (this.loopVar && rawName === this.loopVar) {
|
|
1283
1259
|
this.consume(TT.IDENT);
|
|
1284
1260
|
children.push({ kind: "expr", expression: { kind: "variable", name: rawName }, loc });
|
|
1261
|
+
} else if (nextType === TT.LBRACKET) {
|
|
1262
|
+
const name = this.consume(TT.IDENT).value;
|
|
1263
|
+
const varExpr: Expression = { kind: "variable", name };
|
|
1264
|
+
let indexedExpr = this.parsePostfix(varExpr);
|
|
1265
|
+
// support concatenation: data[prop] + "..."
|
|
1266
|
+
if (this.check(TT.OP_PLUS)) {
|
|
1267
|
+
let leftExpr: Expression = indexedExpr;
|
|
1268
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1269
|
+
this.consume(TT.OP_PLUS);
|
|
1270
|
+
let rhs: Expression | null = null;
|
|
1271
|
+
if (this.check(TT.STRING)) {
|
|
1272
|
+
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1273
|
+
} else if (this.check(TT.IDENT)) {
|
|
1274
|
+
const rhsName = this.consume(TT.IDENT).value;
|
|
1275
|
+
if (this.check(TT.DOT)) {
|
|
1276
|
+
this.consume(TT.DOT);
|
|
1277
|
+
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1278
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1279
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1280
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1281
|
+
} else {
|
|
1282
|
+
rhs = { kind: "variable", name: rhsName };
|
|
1283
|
+
}
|
|
1284
|
+
} else {
|
|
1285
|
+
rhs = this.parseExpression();
|
|
1286
|
+
}
|
|
1287
|
+
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1288
|
+
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1289
|
+
}
|
|
1290
|
+
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
1291
|
+
} else {
|
|
1292
|
+
children.push({ kind: "expr", expression: indexedExpr, loc });
|
|
1293
|
+
}
|
|
1285
1294
|
} else if (nextType === TT.LPAREN || nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|
|
1286
1295
|
children.push(this.parseCompUse());
|
|
1287
1296
|
} else {
|
|
@@ -1294,7 +1303,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1294
1303
|
return children;
|
|
1295
1304
|
}
|
|
1296
1305
|
|
|
1297
|
-
//
|
|
1306
|
+
// style & script blocks
|
|
1298
1307
|
|
|
1299
1308
|
private parseStyleBlock(): StyleBlock {
|
|
1300
1309
|
const loc = this.consume(TT.KW_STYLE).loc;
|
|
@@ -1310,7 +1319,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1310
1319
|
lang = lv as "css" | "scss";
|
|
1311
1320
|
}
|
|
1312
1321
|
else if (this.check(TT.IDENT)) {
|
|
1313
|
-
// v1.0.0: If IDENT is NOT followed by EQUALS, it's a component target name.
|
|
1314
1322
|
if (this.tokens[this.pos + 1]?.type !== TT.EQUALS) {
|
|
1315
1323
|
target = this.consume(TT.IDENT).value;
|
|
1316
1324
|
} else {
|
|
@@ -1336,8 +1344,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1336
1344
|
return { raw: this.consume(TT.RAW_BLOCK).value, attrs, loc };
|
|
1337
1345
|
}
|
|
1338
1346
|
|
|
1339
|
-
// ── Literals ─────────────────────────────────────────────────────────────
|
|
1340
|
-
|
|
1341
1347
|
private parseLiteral(): Literal {
|
|
1342
1348
|
const tok = this.peek();
|
|
1343
1349
|
if (this.check(TT.LBRACKET)) { return this.parseArrayLiteral(); }
|
|
@@ -1353,8 +1359,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1353
1359
|
throw new SinthError(`Expected literal value, got '${tok.value}' (${TT[tok.type]})`, tok.loc);
|
|
1354
1360
|
}
|
|
1355
1361
|
|
|
1356
|
-
// ── Token helpers ─────────────────────────────────────────────────────────
|
|
1357
|
-
|
|
1358
1362
|
private peek(): Token { return this.tokens[this.pos]; }
|
|
1359
1363
|
private check(t: TT): boolean { return this.tokens[this.pos]?.type === t; }
|
|
1360
1364
|
private consume(t: TT): Token {
|
|
@@ -1370,9 +1374,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1370
1374
|
}
|
|
1371
1375
|
}
|
|
1372
1376
|
|
|
1373
|
-
//
|
|
1374
|
-
// §5 IMPORT RESOLVER
|
|
1375
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1377
|
+
// import resolver
|
|
1376
1378
|
|
|
1377
1379
|
interface ResolverConfig { projectRoot: string; libraryPaths: string[] }
|
|
1378
1380
|
|
|
@@ -1516,14 +1518,12 @@ function resolveSinthPath(p: string, fromFile: string, cfg: ResolverConfig): str
|
|
|
1516
1518
|
);
|
|
1517
1519
|
}
|
|
1518
1520
|
|
|
1519
|
-
//
|
|
1520
|
-
// §6 BUILT-IN COMPONENT MAP
|
|
1521
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1521
|
+
// built-in component map
|
|
1522
1522
|
|
|
1523
1523
|
interface BuiltinInfo { tag: string; defaultClass?: string; voidEl?: boolean }
|
|
1524
1524
|
|
|
1525
1525
|
const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
1526
|
-
//
|
|
1526
|
+
// structural
|
|
1527
1527
|
Main: { tag: "main" },
|
|
1528
1528
|
Header: { tag: "header" },
|
|
1529
1529
|
Footer: { tag: "footer" },
|
|
@@ -1541,7 +1541,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1541
1541
|
Row: { tag: "div", defaultClass: "row" },
|
|
1542
1542
|
Column: { tag: "div", defaultClass: "col" },
|
|
1543
1543
|
CardGrid: { tag: "div", defaultClass: "card-grid" },
|
|
1544
|
-
//
|
|
1544
|
+
// typography
|
|
1545
1545
|
Heading: { tag: "h1" },
|
|
1546
1546
|
SubHeading: { tag: "p", defaultClass: "subheading" },
|
|
1547
1547
|
Paragraph: { tag: "p" },
|
|
@@ -1572,7 +1572,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1572
1572
|
Ruby: { tag: "ruby" },
|
|
1573
1573
|
Rt: { tag: "rt" },
|
|
1574
1574
|
Rp: { tag: "rp" },
|
|
1575
|
-
//
|
|
1575
|
+
// interactive
|
|
1576
1576
|
Button: { tag: "button" },
|
|
1577
1577
|
Link: { tag: "a" },
|
|
1578
1578
|
NavLink: { tag: "a" },
|
|
@@ -1591,7 +1591,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1591
1591
|
Meter: { tag: "meter" },
|
|
1592
1592
|
Output: { tag: "output" },
|
|
1593
1593
|
Map: { tag: "map" },
|
|
1594
|
-
//
|
|
1594
|
+
// media
|
|
1595
1595
|
Picture: { tag: "picture" },
|
|
1596
1596
|
Video: { tag: "video" },
|
|
1597
1597
|
Audio: { tag: "audio" },
|
|
@@ -1601,10 +1601,11 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1601
1601
|
Svg: { tag: "svg" },
|
|
1602
1602
|
IFrame: { tag: "iframe" },
|
|
1603
1603
|
Object: { tag: "object" },
|
|
1604
|
-
//
|
|
1604
|
+
// void elements
|
|
1605
1605
|
Img: { tag: "img", voidEl: true },
|
|
1606
1606
|
Logo: { tag: "img", voidEl: true },
|
|
1607
1607
|
Input: { tag: "input", voidEl: true },
|
|
1608
|
+
Checkbox: { tag: "input", voidEl: true },
|
|
1608
1609
|
Hr: { tag: "hr", voidEl: true },
|
|
1609
1610
|
Br: { tag: "br", voidEl: true },
|
|
1610
1611
|
Wbr: { tag: "wbr", voidEl: true },
|
|
@@ -1612,10 +1613,10 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1612
1613
|
Embed: { tag: "embed", voidEl: true },
|
|
1613
1614
|
Col: { tag: "col", voidEl: true },
|
|
1614
1615
|
Area: { tag: "area", voidEl: true },
|
|
1615
|
-
//
|
|
1616
|
+
// lists
|
|
1616
1617
|
Ul: { tag: "ul" }, Ol: { tag: "ol" }, Li: { tag: "li" },
|
|
1617
1618
|
Dl: { tag: "dl" }, Dt: { tag: "dt" }, Dd: { tag: "dd" },
|
|
1618
|
-
//
|
|
1619
|
+
// tables
|
|
1619
1620
|
Table: { tag: "table" },
|
|
1620
1621
|
Caption: { tag: "caption" },
|
|
1621
1622
|
Thead: { tag: "thead" },
|
|
@@ -1625,7 +1626,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1625
1626
|
Th: { tag: "th" },
|
|
1626
1627
|
Td: { tag: "td" },
|
|
1627
1628
|
Colgroup: { tag: "colgroup" },
|
|
1628
|
-
//
|
|
1629
|
+
// utility
|
|
1629
1630
|
Template: { tag: "template" },
|
|
1630
1631
|
Slot: { tag: "slot" },
|
|
1631
1632
|
NoScript: { tag: "noscript" },
|
|
@@ -1642,13 +1643,11 @@ function eventAttrName(name: string): string | null {
|
|
|
1642
1643
|
return EVENT_RE.test(name) ? name.toLowerCase() : null;
|
|
1643
1644
|
}
|
|
1644
1645
|
|
|
1645
|
-
//
|
|
1646
|
-
// §7 SINTH STYLE LANGUAGE — PREPROCESSOR
|
|
1647
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1646
|
+
// preprocessor
|
|
1648
1647
|
|
|
1649
1648
|
/**
|
|
1650
|
-
*
|
|
1651
|
-
*
|
|
1649
|
+
* for who's reading this: This maps Sinth Style pseudo-class shorthand keywords to real CSS pseudo-classes.
|
|
1650
|
+
* these are used inside component style blocks, for example: onHover { color: "blue" }
|
|
1652
1651
|
*/
|
|
1653
1652
|
const SINTH_PSEUDO_CLASS: Record<string, string> = {
|
|
1654
1653
|
onHover: ":hover",
|
|
@@ -1674,9 +1673,6 @@ const SINTH_PSEUDO_CLASS: Record<string, string> = {
|
|
|
1674
1673
|
onLink: ":link",
|
|
1675
1674
|
};
|
|
1676
1675
|
|
|
1677
|
-
/**
|
|
1678
|
-
* Maps Sinth Style pseudo-element shorthand keywords to real CSS pseudo-elements.
|
|
1679
|
-
*/
|
|
1680
1676
|
const SINTH_PSEUDO_ELEMENT: Record<string, string> = {
|
|
1681
1677
|
before: "::before",
|
|
1682
1678
|
after: "::after",
|
|
@@ -1688,7 +1684,6 @@ const SINTH_PSEUDO_ELEMENT: Record<string, string> = {
|
|
|
1688
1684
|
backdrop: "::backdrop",
|
|
1689
1685
|
};
|
|
1690
1686
|
|
|
1691
|
-
/** Resolves a Sinth component name to its CSS selector string. */
|
|
1692
1687
|
function sinthCompToSelector(name: string): string {
|
|
1693
1688
|
const info = BUILTIN_MAP[name];
|
|
1694
1689
|
if (!info) return name.toLowerCase();
|
|
@@ -1700,14 +1695,11 @@ function sinthCompToSelector(name: string): string {
|
|
|
1700
1695
|
}
|
|
1701
1696
|
|
|
1702
1697
|
/**
|
|
1703
|
-
*
|
|
1704
|
-
*
|
|
1705
|
-
*
|
|
1706
|
-
*
|
|
1707
|
-
* -
|
|
1708
|
-
* - Pseudo-class shorthands: `onHover { ... }` → `&:hover { ... }`
|
|
1709
|
-
* - Pseudo-element shorthands: `before { ... }` → `&::before { ... }`
|
|
1710
|
-
* - Media queries: `media(maxWidth: "600px") { ... }` → `@media (max-width: 600px) { ... }`
|
|
1698
|
+
* supported conversions:
|
|
1699
|
+
* - component names as selectors: `Paragraph { ... }` → `p { ... }`
|
|
1700
|
+
* - pseudo-class shorthands: `onHover { ... }` → `&:hover { ... }`
|
|
1701
|
+
* - pseudo-element shorthands: `before { ... }` → `&::before { ... }`
|
|
1702
|
+
* - media queries: `media(maxWidth: "600px") { ... }` → `@media (max-width: 600px) { ... }`
|
|
1711
1703
|
* - CSS custom properties: `var --primary: "#3b82f6"` → `--primary: #3b82f6;`
|
|
1712
1704
|
*/
|
|
1713
1705
|
function preprocessSinthStyle(raw: string): string {
|
|
@@ -1718,17 +1710,14 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1718
1710
|
const trimmed = line.trim();
|
|
1719
1711
|
const indent = line.match(/^(\s*)/)?.[1] ?? "";
|
|
1720
1712
|
|
|
1721
|
-
// Empty lines pass through
|
|
1722
1713
|
if (!trimmed) { result.push(line); continue; }
|
|
1723
1714
|
|
|
1724
|
-
// Strip Sinth comments from style blocks
|
|
1725
1715
|
if (trimmed.startsWith("--") && !trimmed.startsWith("--[")) { continue; }
|
|
1726
1716
|
// Standard comments pass through
|
|
1727
1717
|
if (trimmed.startsWith("//") || trimmed.startsWith("/*") || trimmed.startsWith("*")) {
|
|
1728
1718
|
result.push(line); continue;
|
|
1729
1719
|
}
|
|
1730
1720
|
|
|
1731
|
-
// media(maxWidth: "600px") { → @media (max-width: 600px) {
|
|
1732
1721
|
const mediaMatch = trimmed.match(/^media\s*\(([^)]+)\)\s*(\{?\s*)$/);
|
|
1733
1722
|
if (mediaMatch) {
|
|
1734
1723
|
const params = parseSinthMediaParams(mediaMatch[1]);
|
|
@@ -1736,7 +1725,6 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1736
1725
|
continue;
|
|
1737
1726
|
}
|
|
1738
1727
|
|
|
1739
|
-
// Block opener: check if it's a keyword that maps to a pseudo-class/element
|
|
1740
1728
|
const blockMatch = trimmed.match(/^([a-zA-Z][a-zA-Z0-9]*)(\s*\{\s*)$/);
|
|
1741
1729
|
if (blockMatch) {
|
|
1742
1730
|
const keyword = blockMatch[1];
|
|
@@ -1753,14 +1741,12 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1753
1741
|
continue;
|
|
1754
1742
|
}
|
|
1755
1743
|
|
|
1756
|
-
// Capital-letter IDENT = component name
|
|
1757
1744
|
if (/^[A-Z]/.test(keyword) && BUILTIN_MAP[keyword]) {
|
|
1758
1745
|
result.push(`${indent}${sinthCompToSelector(keyword)} {`);
|
|
1759
1746
|
continue;
|
|
1760
1747
|
}
|
|
1761
1748
|
}
|
|
1762
1749
|
|
|
1763
|
-
// CSS custom property shorthand: `var --primary: "value"` → `--primary: value;`
|
|
1764
1750
|
const varPropMatch = trimmed.match(/^var\s+(--[a-zA-Z][a-zA-Z0-9-]*)\s*:\s*["']?([^"';]+)["']?\s*;?\s*$/);
|
|
1765
1751
|
if (varPropMatch) {
|
|
1766
1752
|
result.push(`${indent}${varPropMatch[1]}: ${varPropMatch[2].trim()};`);
|
|
@@ -1773,7 +1759,6 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1773
1759
|
return result.join("\n");
|
|
1774
1760
|
}
|
|
1775
1761
|
|
|
1776
|
-
/** Converts Sinth media-query params to CSS @media condition string. */
|
|
1777
1762
|
function parseSinthMediaParams(params: string): string {
|
|
1778
1763
|
const parts = params.split(",").map(p => {
|
|
1779
1764
|
const m = p.trim().match(/^([a-zA-Z]+)\s*:\s*["']?([^"',]+)["']?\s*$/);
|
|
@@ -1783,15 +1768,9 @@ function parseSinthMediaParams(params: string): string {
|
|
|
1783
1768
|
return parts.join(") and (");
|
|
1784
1769
|
}
|
|
1785
1770
|
|
|
1786
|
-
//
|
|
1787
|
-
|
|
1788
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1771
|
+
// style processor
|
|
1772
|
+
|
|
1789
1773
|
|
|
1790
|
-
/**
|
|
1791
|
-
* Converts camelCase CSS property names to kebab-case, strips surrounding
|
|
1792
|
-
* quotes from values, and ensures each declaration ends with a semicolon.
|
|
1793
|
-
* Selectors, @rules, comments, and braces are left untouched.
|
|
1794
|
-
*/
|
|
1795
1774
|
function convertCSSProps(css: string): string {
|
|
1796
1775
|
return css.split("\n").map(line => {
|
|
1797
1776
|
const trimmed = line.trim();
|
|
@@ -1806,7 +1785,6 @@ function convertCSSProps(css: string): string {
|
|
|
1806
1785
|
|
|
1807
1786
|
const [, indent, rawProp, , rawVal] = m;
|
|
1808
1787
|
|
|
1809
|
-
// Only convert if there's a camelCase hint or a quoted value
|
|
1810
1788
|
const hasCamelCase = /[a-z][A-Z]/.test(rawProp) || /^[A-Z]/.test(rawProp);
|
|
1811
1789
|
const hasQuotedVal = /^["']/.test(rawVal.trim());
|
|
1812
1790
|
if (!hasCamelCase && !hasQuotedVal) return line;
|
|
@@ -1822,15 +1800,6 @@ function convertCSSProps(css: string): string {
|
|
|
1822
1800
|
}).join("\n");
|
|
1823
1801
|
}
|
|
1824
1802
|
|
|
1825
|
-
/**
|
|
1826
|
-
* Processes a StyleBlock:
|
|
1827
|
-
* 1. Interpolate {param} placeholders
|
|
1828
|
-
* 2. Sinth Style preprocessing (pseudo-class shorthands, media, component selectors)
|
|
1829
|
-
* 3. camelCase → kebab-case conversion
|
|
1830
|
-
* 4. SCSS compilation (if lang="scss")
|
|
1831
|
-
* 5. Scoping (unless global)
|
|
1832
|
-
* 6. If block has a `target` component, wrap in the appropriate CSS selector
|
|
1833
|
-
*/
|
|
1834
1803
|
function processStyleBlock(
|
|
1835
1804
|
block: StyleBlock,
|
|
1836
1805
|
hash: string,
|
|
@@ -1838,13 +1807,13 @@ function processStyleBlock(
|
|
|
1838
1807
|
): string {
|
|
1839
1808
|
let raw = block.raw;
|
|
1840
1809
|
|
|
1841
|
-
// 1.
|
|
1810
|
+
// 1. interpolate {param} placeholders
|
|
1842
1811
|
if (params.size > 0) raw = interpolateAttr(raw, params);
|
|
1843
1812
|
|
|
1844
|
-
// 2.
|
|
1813
|
+
// 2. sinth style preprocessing
|
|
1845
1814
|
raw = preprocessSinthStyle(raw);
|
|
1846
1815
|
|
|
1847
|
-
// 3.
|
|
1816
|
+
// 3. warn if & in plain CSS
|
|
1848
1817
|
if (block.lang === "css" && raw.includes("&")) {
|
|
1849
1818
|
SinthWarning.emit(
|
|
1850
1819
|
`'&' (CSS nesting) found in style block. Nested selectors require a modern browser or lang="scss".`,
|
|
@@ -1870,13 +1839,13 @@ function processStyleBlock(
|
|
|
1870
1839
|
}
|
|
1871
1840
|
}
|
|
1872
1841
|
|
|
1873
|
-
// 6.
|
|
1842
|
+
// 6. if target component given, wrap content in its CSS selector
|
|
1874
1843
|
if (block.target) {
|
|
1875
1844
|
const selector = sinthCompToSelector(block.target);
|
|
1876
1845
|
css = `${selector} {\n${css}\n}`;
|
|
1877
1846
|
}
|
|
1878
1847
|
|
|
1879
|
-
// 7.
|
|
1848
|
+
// 7. scope (except it's global)
|
|
1880
1849
|
if (!block.global) css = scopeCSS(css, hash);
|
|
1881
1850
|
|
|
1882
1851
|
return css;
|
|
@@ -1970,9 +1939,8 @@ function extractCSSBlock(css: string, openBrace: number): { content: string; end
|
|
|
1970
1939
|
return { content: css.substring(openBrace + 1, i), end: i + 1 };
|
|
1971
1940
|
}
|
|
1972
1941
|
|
|
1973
|
-
|
|
1974
|
-
//
|
|
1975
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1942
|
+
|
|
1943
|
+
// expression -> js compiler
|
|
1976
1944
|
|
|
1977
1945
|
function compileExprToJS(expr: Expression): string {
|
|
1978
1946
|
switch (expr.kind) {
|
|
@@ -1998,6 +1966,8 @@ function compileExprToJS(expr: Expression): string {
|
|
|
1998
1966
|
const v = expr.right ? compileExprToJS(expr.right) : "null";
|
|
1999
1967
|
return `${expr.target} ${expr.op} ${v}`;
|
|
2000
1968
|
}
|
|
1969
|
+
case "index":
|
|
1970
|
+
return `${compileExprToJS(expr.object!)}[${compileExprToJS(expr.key!)}]`;
|
|
2001
1971
|
case "postfix":
|
|
2002
1972
|
return `${expr.target}${expr.op}`;
|
|
2003
1973
|
default:
|
|
@@ -2005,7 +1975,6 @@ function compileExprToJS(expr: Expression): string {
|
|
|
2005
1975
|
}
|
|
2006
1976
|
}
|
|
2007
1977
|
|
|
2008
|
-
/** Compiles a pure-logic IfBlock (containing only AssignStmt children) to JS. */
|
|
2009
1978
|
function compileIfToJS(ifBlock: IfBlock): string {
|
|
2010
1979
|
const cond = compileExprToJS(ifBlock.condition);
|
|
2011
1980
|
const ifJS = bodyToJS(ifBlock.body);
|
|
@@ -2026,9 +1995,7 @@ function bodyToJS(children: Child[]): string {
|
|
|
2026
1995
|
.join("");
|
|
2027
1996
|
}
|
|
2028
1997
|
|
|
2029
|
-
//
|
|
2030
|
-
// §10 HTML GENERATOR
|
|
2031
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1998
|
+
// the HTML generator
|
|
2032
1999
|
|
|
2033
2000
|
/**
|
|
2034
2001
|
* CSS property names that are allowed as inline style shorthand attributes on
|
|
@@ -2084,6 +2051,8 @@ if (name === "model" && value?.kind === "str") {
|
|
|
2084
2051
|
return `oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)" value="${escAttr(vName)}"`;
|
|
2085
2052
|
}
|
|
2086
2053
|
|
|
2054
|
+
|
|
2055
|
+
|
|
2087
2056
|
if (name === "delay") {
|
|
2088
2057
|
if (value.kind === "num") {
|
|
2089
2058
|
return `data-sinth-delay="${value.value}" style="display:none"`;
|
|
@@ -2100,11 +2069,14 @@ if (name === "delay") {
|
|
|
2100
2069
|
return `data-sinth-delay-expr="${escAttr(v)}" style="display:none"`;
|
|
2101
2070
|
}
|
|
2102
2071
|
|
|
2072
|
+
if (name === "checked") {
|
|
2073
|
+
return (value as any)?.kind === "bool" && (value as any).value ? "checked" : "";
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2103
2076
|
if (value.kind === "num") return `${name}="${value.value}"`;
|
|
2104
2077
|
|
|
2105
2078
|
let raw = value.value;
|
|
2106
2079
|
|
|
2107
|
-
// Inline expression reference
|
|
2108
2080
|
if (raw.startsWith("__EXPR__")) {
|
|
2109
2081
|
const exprJson = raw.substring("__EXPR__".length);
|
|
2110
2082
|
try {
|
|
@@ -2180,8 +2152,6 @@ function renderChild(
|
|
|
2180
2152
|
}
|
|
2181
2153
|
|
|
2182
2154
|
case "assign_stmt": {
|
|
2183
|
-
// An AssignStmt appearing in a child list (outside an if-block) is emitted as
|
|
2184
|
-
// a logic block that runs in sinthRender().
|
|
2185
2155
|
ctx.logicBlocks.push(compileExprToJS(child.expression) + ";");
|
|
2186
2156
|
return "";
|
|
2187
2157
|
}
|
|
@@ -2203,15 +2173,6 @@ function renderChild(
|
|
|
2203
2173
|
}
|
|
2204
2174
|
}
|
|
2205
2175
|
|
|
2206
|
-
/**
|
|
2207
|
-
* v1.0.0: Unified if/else block renderer.
|
|
2208
|
-
*
|
|
2209
|
-
* Classifies each branch as containing assignments, components, or both, then
|
|
2210
|
-
* chooses the appropriate rendering strategy:
|
|
2211
|
-
* • Pure-logic → JS fragment in sinthRender(); no HTML output.
|
|
2212
|
-
* • Pure-DOM → <template data-sinth-if> for runtime DOM diffing.
|
|
2213
|
-
* • Mixed → <span id="__smN__"> placeholder; sinthRender() sets innerHTML.
|
|
2214
|
-
*/
|
|
2215
2176
|
function renderIfBlock(
|
|
2216
2177
|
ifBlock: IfBlock,
|
|
2217
2178
|
ctx: CompileCtx,
|
|
@@ -2225,13 +2186,13 @@ depth: number,
|
|
|
2225
2186
|
const hasAssign = allChildren.some(c => c.kind === "assign_stmt");
|
|
2226
2187
|
const hasComp = allChildren.some(c => c.kind === "use");
|
|
2227
2188
|
|
|
2228
|
-
//
|
|
2189
|
+
// pure logic
|
|
2229
2190
|
if (hasAssign && !hasComp) {
|
|
2230
2191
|
ctx.logicBlocks.push(compileIfToJS(ifBlock));
|
|
2231
2192
|
return "";
|
|
2232
2193
|
}
|
|
2233
2194
|
|
|
2234
|
-
//
|
|
2195
|
+
// pure DOM
|
|
2235
2196
|
if (!hasAssign && hasComp) {
|
|
2236
2197
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2237
2198
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
@@ -2241,8 +2202,7 @@ depth: number,
|
|
|
2241
2202
|
);
|
|
2242
2203
|
}
|
|
2243
2204
|
|
|
2244
|
-
//
|
|
2245
|
-
// Both assignments and component children appear in at least one branch.
|
|
2205
|
+
// mixed
|
|
2246
2206
|
const id = `__sm${ctx.mixedCounter++}__`;
|
|
2247
2207
|
|
|
2248
2208
|
const ifAssignJS = ifBlock.body
|
|
@@ -2265,7 +2225,6 @@ depth: number,
|
|
|
2265
2225
|
.map(c => renderChild(c, ctx, params, depth + 1))
|
|
2266
2226
|
.join("");
|
|
2267
2227
|
|
|
2268
|
-
// Detect if both branches have a component with the same id — use it as replace anchor
|
|
2269
2228
|
let replaceId: string | undefined;
|
|
2270
2229
|
const ifFirstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
2271
2230
|
const elseFirstComp = (ifBlock.elseBody ?? []).find(c => c.kind === "use") as CompUse | undefined;
|
|
@@ -2300,7 +2259,7 @@ function renderCompUse(
|
|
|
2300
2259
|
params: Map<string, string>,
|
|
2301
2260
|
depth: number,
|
|
2302
2261
|
): string {
|
|
2303
|
-
//
|
|
2262
|
+
// transparent wrapper inserted by flattenIfToUses()
|
|
2304
2263
|
if (use.name === "__IF_ROOT__") {
|
|
2305
2264
|
return use.children.map(c => renderChild(c, ctx, params, depth)).join("");
|
|
2306
2265
|
}
|
|
@@ -2312,11 +2271,11 @@ function renderCompUse(
|
|
|
2312
2271
|
return interpolateAttr(litToString(ca.value), params);
|
|
2313
2272
|
}
|
|
2314
2273
|
|
|
2315
|
-
//
|
|
2274
|
+
// user-defined component
|
|
2316
2275
|
const userDef = ctx.allDefs.get(use.name);
|
|
2317
2276
|
if (userDef) return expandUserComp(use, userDef, ctx, params, depth + 1);
|
|
2318
2277
|
|
|
2319
|
-
//
|
|
2278
|
+
// custom element
|
|
2320
2279
|
const customEl = ctx.customEls.get(use.name);
|
|
2321
2280
|
if (customEl) {
|
|
2322
2281
|
const attrParts = [`data-s="${ctx.scopeHash}"`];
|
|
@@ -2328,7 +2287,7 @@ function renderCompUse(
|
|
|
2328
2287
|
return `<${customEl.tagName} ${attrParts.join(" ")}>${inner}</${customEl.tagName}>`;
|
|
2329
2288
|
}
|
|
2330
2289
|
|
|
2331
|
-
//
|
|
2290
|
+
// built-in component
|
|
2332
2291
|
const { tag, defaultClass, voidEl } = resolveBuiltinTag(use.name, use.attrs);
|
|
2333
2292
|
const isVoid = voidEl || VOID_TAGS.has(tag);
|
|
2334
2293
|
|
|
@@ -2339,7 +2298,6 @@ function renderCompUse(
|
|
|
2339
2298
|
for (const attr of use.attrs) {
|
|
2340
2299
|
if (attr.name === "level" && use.name === "Heading") continue;
|
|
2341
2300
|
|
|
2342
|
-
// v1.0.0: Inline CSS shorthand Paragraph(color: "red") { ... }
|
|
2343
2301
|
if (INLINE_STYLE_PROPS.has(attr.name) && attr.value) {
|
|
2344
2302
|
const val = attr.value.kind === "str"
|
|
2345
2303
|
? interpolateAttr(attr.value.value, params)
|
|
@@ -2360,6 +2318,38 @@ function renderCompUse(
|
|
|
2360
2318
|
if (classes) attrParts.push(`class="${escAttr(classes)}"`);
|
|
2361
2319
|
if (inlineStyleParts.length > 0) attrParts.push(`style="${escAttr(inlineStyleParts.join("; "))}"`);
|
|
2362
2320
|
if (tag === "button" && !use.attrs.some(a => a.name === "type")) attrParts.push(`type="button"`);
|
|
2321
|
+
if (use.name === "Input") {
|
|
2322
|
+
const bindAttr = use.attrs.find(a => a.name === "bind");
|
|
2323
|
+
const modelAttr = use.attrs.find(a => a.name === "model");
|
|
2324
|
+
const bindOrModel = bindAttr || modelAttr;
|
|
2325
|
+
if (bindOrModel?.value?.kind === "str") {
|
|
2326
|
+
let vName = bindOrModel.value.value;
|
|
2327
|
+
if (vName.startsWith("__EXPR__")) {
|
|
2328
|
+
try {
|
|
2329
|
+
const expr: Expression = JSON.parse(vName.substring(8));
|
|
2330
|
+
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
2331
|
+
} catch {}
|
|
2332
|
+
}
|
|
2333
|
+
if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="text"`);
|
|
2334
|
+
attrParts.push(`oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)"`);
|
|
2335
|
+
attrParts.push(`data-sinth-value="${escAttr(vName)}"`);
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
if (use.name === "Checkbox") {
|
|
2339
|
+
if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="checkbox"`);
|
|
2340
|
+
const bindAttr = use.attrs.find(a => a.name === "bind");
|
|
2341
|
+
if (bindAttr?.value?.kind === "str") {
|
|
2342
|
+
let vName = bindAttr.value.value;
|
|
2343
|
+
if (vName.startsWith("__EXPR__")) {
|
|
2344
|
+
try {
|
|
2345
|
+
const expr: Expression = JSON.parse(vName.substring(8));
|
|
2346
|
+
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
2347
|
+
} catch {}
|
|
2348
|
+
}
|
|
2349
|
+
attrParts.push(`onchange="(function(e){ ${vName} = e.target.checked; sinthRender(); })(event)"`);
|
|
2350
|
+
attrParts.push(`data-sinth-checked="${escAttr(vName)}"`);
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2363
2353
|
|
|
2364
2354
|
const attrStr = attrParts.length ? " " + attrParts.join(" ") : "";
|
|
2365
2355
|
|
|
@@ -2417,9 +2407,7 @@ function expandUserComp(
|
|
|
2417
2407
|
return def.body.map(c => renderChild(c, ctx, local, depth)).join("");
|
|
2418
2408
|
}
|
|
2419
2409
|
|
|
2420
|
-
//
|
|
2421
|
-
// §11 SCRIPT COLLECTOR
|
|
2422
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2410
|
+
// script collector
|
|
2423
2411
|
|
|
2424
2412
|
function collectScripts(
|
|
2425
2413
|
file: SinthFile,
|
|
@@ -2458,9 +2446,8 @@ function extractFunctionNames(js: string): string[] {
|
|
|
2458
2446
|
return names;
|
|
2459
2447
|
}
|
|
2460
2448
|
|
|
2461
|
-
|
|
2462
|
-
//
|
|
2463
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2449
|
+
|
|
2450
|
+
// head gen
|
|
2464
2451
|
|
|
2465
2452
|
interface HeadData {
|
|
2466
2453
|
title?: string; fav?: string;
|
|
@@ -2543,14 +2530,9 @@ function renderHead(
|
|
|
2543
2530
|
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
2544
2531
|
}
|
|
2545
2532
|
|
|
2546
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2547
|
-
// §13 SINTH RUNTIME — TREE-SHAKEN BUILDER
|
|
2548
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2549
2533
|
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
* features that are actually used by this page. This keeps output lean.
|
|
2553
|
-
*/
|
|
2534
|
+
// sinth runtime
|
|
2535
|
+
|
|
2554
2536
|
function buildRuntime(opts: {
|
|
2555
2537
|
varDecls: VarDeclaration[];
|
|
2556
2538
|
bodyHTML: string;
|
|
@@ -2560,8 +2542,7 @@ function buildRuntime(opts: {
|
|
|
2560
2542
|
}): string {
|
|
2561
2543
|
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars } = opts;
|
|
2562
2544
|
|
|
2563
|
-
|
|
2564
|
-
// Feature flags
|
|
2545
|
+
|
|
2565
2546
|
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
2566
2547
|
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
2567
2548
|
const needsFor = bodyHTML.includes("data-sinth-for");
|
|
@@ -2570,7 +2551,7 @@ const needsMixed = mixedBlocks.length > 0;
|
|
|
2570
2551
|
const needsLogic = logicBlocks.length > 0;
|
|
2571
2552
|
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
2572
2553
|
|
|
2573
|
-
//
|
|
2554
|
+
// var decls with type defaults
|
|
2574
2555
|
const varLines = varDecls.map(v => {
|
|
2575
2556
|
if (!v.value) {
|
|
2576
2557
|
if (!assignedVars.has(v.name)) {
|
|
@@ -2603,21 +2584,23 @@ return `var ${v.name} = ${val};`;
|
|
|
2603
2584
|
|
|
2604
2585
|
let renderBody = "";
|
|
2605
2586
|
|
|
2606
|
-
// Scroll position preservation
|
|
2607
2587
|
renderBody += ` var _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
2608
2588
|
|
|
2609
|
-
// Pure-logic if blocks
|
|
2610
2589
|
if (needsLogic) {
|
|
2611
2590
|
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
2612
2591
|
}
|
|
2613
2592
|
|
|
2614
|
-
// data-sinth-for templates (MUST run before if-templates)
|
|
2615
|
-
// data-sinth-for templates (MUST run before if-templates)
|
|
2616
2593
|
if (needsFor) {
|
|
2617
2594
|
renderBody += `
|
|
2618
2595
|
document.querySelectorAll('template[data-sinth-for]').forEach(function(t) {
|
|
2619
2596
|
var source;
|
|
2620
2597
|
try { source = eval(t.dataset.sinthFor); } catch(e) { source = []; }
|
|
2598
|
+
var newHash = '';
|
|
2599
|
+
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
2600
|
+
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
2601
|
+
return;
|
|
2602
|
+
}
|
|
2603
|
+
t.dataset.sinthForHash = newHash;
|
|
2621
2604
|
var isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
2622
2605
|
var entries;
|
|
2623
2606
|
if (isObj) {
|
|
@@ -2683,12 +2666,16 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2683
2666
|
}
|
|
2684
2667
|
});
|
|
2685
2668
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2669
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2670
|
+
el.dataset.sinthDelayDone = '1';
|
|
2686
2671
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2687
2672
|
el.style.display = 'none';
|
|
2688
2673
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2689
2674
|
else el.style.display = '';
|
|
2690
2675
|
});
|
|
2691
2676
|
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2677
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2678
|
+
el.dataset.sinthDelayDone = '1';
|
|
2692
2679
|
try {
|
|
2693
2680
|
eval('var ' + _item + ' = _user;');
|
|
2694
2681
|
if (_key) eval('var ' + _key + ' = _k;');
|
|
@@ -2704,7 +2691,6 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2704
2691
|
});
|
|
2705
2692
|
`;
|
|
2706
2693
|
}
|
|
2707
|
-
// data-sinth-if / data-sinth-else templates (run AFTER for-loops)
|
|
2708
2694
|
if (needsIf) {
|
|
2709
2695
|
renderBody += `
|
|
2710
2696
|
document.querySelectorAll('template[data-sinth-if]').forEach(function(t) {
|
|
@@ -2726,6 +2712,8 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2726
2712
|
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2727
2713
|
});
|
|
2728
2714
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2715
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2716
|
+
el.dataset.sinthDelayDone = '1';
|
|
2729
2717
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2730
2718
|
el.style.display = 'none';
|
|
2731
2719
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
@@ -2789,7 +2777,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2789
2777
|
}
|
|
2790
2778
|
|
|
2791
2779
|
|
|
2792
|
-
//
|
|
2780
|
+
// mixed if/else blocks
|
|
2793
2781
|
if (needsMixed) {
|
|
2794
2782
|
for (const mb of mixedBlocks) {
|
|
2795
2783
|
const ifHTMLJS = JSON.stringify(mb.ifHTML);
|
|
@@ -2809,6 +2797,26 @@ ${mb.elseJS ? mb.elseJS : ""} __el.innerHTML = ${elseHTMLJS};
|
|
|
2809
2797
|
__el.querySelectorAll('.sinth-expr').forEach(function(e) {
|
|
2810
2798
|
try { e.textContent = eval(e.dataset.expr); } catch(ex) {}
|
|
2811
2799
|
});
|
|
2800
|
+
__el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
2801
|
+
var innerCond;
|
|
2802
|
+
try { innerCond = eval(innerT.dataset.sinthIf); } catch(e) { innerCond = false; }
|
|
2803
|
+
if (innerCond) {
|
|
2804
|
+
var innerFrag = document.createRange().createContextualFragment(innerT.innerHTML);
|
|
2805
|
+
innerFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2806
|
+
try { e2.textContent = eval(e2.dataset.expr); } catch(ex) {}
|
|
2807
|
+
});
|
|
2808
|
+
innerT.parentNode.insertBefore(innerFrag, innerT);
|
|
2809
|
+
} else {
|
|
2810
|
+
var innerElse = innerT.nextElementSibling;
|
|
2811
|
+
if (innerElse && innerElse.hasAttribute('data-sinth-else')) {
|
|
2812
|
+
var innerElseFrag = document.createRange().createContextualFragment(innerElse.innerHTML);
|
|
2813
|
+
innerElseFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2814
|
+
try { e2.textContent = eval(e2.dataset.expr); } catch(ex) {}
|
|
2815
|
+
});
|
|
2816
|
+
innerT.parentNode.insertBefore(innerElseFrag, innerT);
|
|
2817
|
+
}
|
|
2818
|
+
}
|
|
2819
|
+
});
|
|
2812
2820
|
${hasDelay ? `
|
|
2813
2821
|
// Handle delays for this mixed block
|
|
2814
2822
|
__el.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
@@ -2832,7 +2840,17 @@ else el.style.display = '';
|
|
|
2832
2840
|
}
|
|
2833
2841
|
}
|
|
2834
2842
|
|
|
2835
|
-
//
|
|
2843
|
+
// set input vals from bound vars
|
|
2844
|
+
renderBody += `
|
|
2845
|
+
document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
2846
|
+
try { el.value = eval(el.dataset.sinthValue) || ''; } catch(e) {}
|
|
2847
|
+
});
|
|
2848
|
+
document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
2849
|
+
try { el.checked = !!eval(el.dataset.sinthChecked); } catch(e) {}
|
|
2850
|
+
});
|
|
2851
|
+
`;
|
|
2852
|
+
|
|
2853
|
+
// expression spans
|
|
2836
2854
|
if (needsExpr) {
|
|
2837
2855
|
renderBody += `
|
|
2838
2856
|
document.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
@@ -2841,10 +2859,10 @@ else el.style.display = '';
|
|
|
2841
2859
|
`;
|
|
2842
2860
|
}
|
|
2843
2861
|
|
|
2844
|
-
|
|
2862
|
+
|
|
2845
2863
|
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|
|
2846
2864
|
|
|
2847
|
-
|
|
2865
|
+
|
|
2848
2866
|
const delayBlock = needsDelay ? `
|
|
2849
2867
|
setTimeout(function() {
|
|
2850
2868
|
document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
@@ -2862,15 +2880,23 @@ setTimeout(function() {
|
|
|
2862
2880
|
}, 0);
|
|
2863
2881
|
` : "";
|
|
2864
2882
|
|
|
2865
|
-
return `// Sinth
|
|
2883
|
+
return `// Sinth compiled runtime
|
|
2866
2884
|
${varLines}
|
|
2885
|
+
${needsFor ? `function hashString(str) {
|
|
2886
|
+
var hash = 0, i, chr;
|
|
2887
|
+
for (i = 0; i < str.length; i++) {
|
|
2888
|
+
chr = str.charCodeAt(i);
|
|
2889
|
+
hash = ((hash << 5) - hash) + chr;
|
|
2890
|
+
hash |= 0;
|
|
2891
|
+
}
|
|
2892
|
+
return String(hash);
|
|
2893
|
+
}
|
|
2894
|
+
` : ""}
|
|
2867
2895
|
${needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : ""}
|
|
2868
2896
|
${delayBlock}`;
|
|
2869
2897
|
}
|
|
2870
2898
|
|
|
2871
|
-
//
|
|
2872
|
-
// §14 MAIN COMPILATION PIPELINE
|
|
2873
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2899
|
+
// main compilation pipeline
|
|
2874
2900
|
|
|
2875
2901
|
interface CompileOptions {
|
|
2876
2902
|
projectRoot: string;
|
|
@@ -2888,7 +2914,6 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2888
2914
|
const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
|
|
2889
2915
|
const hash = fnv1a(absPath);
|
|
2890
2916
|
|
|
2891
|
-
// All var declarations are already collected via shared _varDecls reference
|
|
2892
2917
|
const allVarDecls: VarDeclaration[] = file.varDecls;
|
|
2893
2918
|
|
|
2894
2919
|
const ctx: CompileCtx = {
|
|
@@ -2909,29 +2934,24 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2909
2934
|
}
|
|
2910
2935
|
|
|
2911
2936
|
const headData = buildHeadData(file.meta);
|
|
2912
|
-
|
|
2913
|
-
// Render body HTML (populates ctx.extraCSS, ctx.mixedBlocks, ctx.logicBlocks)
|
|
2914
2937
|
const bodyHTML = file.uses.map(u => renderCompUse(u, ctx, new Map(), 0)).join("\n");
|
|
2915
|
-
|
|
2916
2938
|
const pageCSS = file.styles.map(s => processStyleBlock(s, hash, new Map())).join("\n");
|
|
2917
2939
|
const scopedCSS = [pageCSS, ...ctx.extraCSS].filter(c => c.trim()).join("\n");
|
|
2918
|
-
|
|
2919
2940
|
const { componentScripts, pageScripts } = collectScripts(file, allDefs);
|
|
2920
2941
|
|
|
2921
|
-
//
|
|
2942
|
+
// collect all assigned variables for default-value warnings
|
|
2922
2943
|
const assignedVars = new Set<string>();
|
|
2923
2944
|
for (const s of file.scripts) {
|
|
2924
2945
|
for (const m of s.raw.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?:[+\-]?=)/g)) assignedVars.add(m[1]);
|
|
2925
2946
|
}
|
|
2926
2947
|
for (const v of file.varDecls) { if (v.value) assignedVars.add(v.name); }
|
|
2927
2948
|
|
|
2928
|
-
//
|
|
2949
|
+
// companion JS file (needs same name as .sinth, adds support for JS libraries)
|
|
2929
2950
|
const companionJS = (() => {
|
|
2930
2951
|
const base = absPath.replace(/\.sinth$/, ".js");
|
|
2931
2952
|
return fs.existsSync(base) ? path.basename(base) : undefined;
|
|
2932
2953
|
})();
|
|
2933
2954
|
|
|
2934
|
-
// Make CSS / JS links relative to the HTML output directory
|
|
2935
2955
|
const relativeCssLinks = cssLinks.map(css => {
|
|
2936
2956
|
const rel = path.relative(path.dirname(absPath), css).replace(/\\/g, "/");
|
|
2937
2957
|
return rel.startsWith(".") ? rel : "./" + rel;
|
|
@@ -2946,7 +2966,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2946
2966
|
|
|
2947
2967
|
const head = renderHead(headData, relativeCssLinks, relativeJsLinks, scopedCSS, companionJS);
|
|
2948
2968
|
|
|
2949
|
-
//
|
|
2969
|
+
// build tree-shaken runtime
|
|
2950
2970
|
const runtimeJS = buildRuntime({
|
|
2951
2971
|
varDecls: allVarDecls,
|
|
2952
2972
|
bodyHTML,
|
|
@@ -2955,13 +2975,11 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2955
2975
|
assignedVars,
|
|
2956
2976
|
});
|
|
2957
2977
|
|
|
2958
|
-
// Component-level script IIFEs
|
|
2959
2978
|
const scriptTags: string[] = [];
|
|
2960
2979
|
if (componentScripts.length > 0) {
|
|
2961
2980
|
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
2962
2981
|
}
|
|
2963
2982
|
|
|
2964
|
-
// Page-level script blocks
|
|
2965
2983
|
for (const s of pageScripts) {
|
|
2966
2984
|
const extra = Object.entries(s.attrs)
|
|
2967
2985
|
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
@@ -2989,9 +3007,7 @@ function minifyHTML(html: string): string {
|
|
|
2989
3007
|
return html.replace(/>\s+</g, "><").replace(/\n\s*\n/g, "\n").trim();
|
|
2990
3008
|
}
|
|
2991
3009
|
|
|
2992
|
-
//
|
|
2993
|
-
// §15 FILE DISCOVERY & ASSET COPY
|
|
2994
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3010
|
+
// file discovery & asset copy
|
|
2995
3011
|
|
|
2996
3012
|
function findSinthPages(dir: string, outDir?: string): string[] {
|
|
2997
3013
|
const results: string[] = [];
|
|
@@ -3016,9 +3032,8 @@ function copyDir(src: string, dest: string): void {
|
|
|
3016
3032
|
}
|
|
3017
3033
|
}
|
|
3018
3034
|
|
|
3019
|
-
|
|
3020
|
-
//
|
|
3021
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3035
|
+
|
|
3036
|
+
// dev server
|
|
3022
3037
|
|
|
3023
3038
|
const LIVE_RELOAD_SCRIPT = `<script>
|
|
3024
3039
|
(function(){
|
|
@@ -3133,9 +3148,8 @@ async function startDevServer(opts: CompileOptions & { port: number; files?: str
|
|
|
3133
3148
|
});
|
|
3134
3149
|
}
|
|
3135
3150
|
|
|
3136
|
-
|
|
3137
|
-
//
|
|
3138
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3151
|
+
|
|
3152
|
+
// cli entry point
|
|
3139
3153
|
|
|
3140
3154
|
function loadConfig(root: string): Record<string, unknown> {
|
|
3141
3155
|
const cfgPath = path.join(root, "sinth.config.json");
|
|
@@ -3195,14 +3209,13 @@ async function main(): Promise<void> {
|
|
|
3195
3209
|
}
|
|
3196
3210
|
}
|
|
3197
3211
|
|
|
3198
|
-
//
|
|
3212
|
+
// copies to output
|
|
3199
3213
|
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
3200
3214
|
if (fs.existsSync(assetsIn)) {
|
|
3201
3215
|
copyDir(assetsIn, assetsOut);
|
|
3202
3216
|
process.stdout.write(` \x1b[32m✓\x1b[0m assets/ → ${path.relative(cwd, assetsOut)}/\n`);
|
|
3203
3217
|
}
|
|
3204
3218
|
|
|
3205
|
-
// Copy libraries/ (without .sinth / .html files)
|
|
3206
3219
|
const libIn = path.join(cwd, "libraries"), libOut = path.join(outDir, "libraries");
|
|
3207
3220
|
if (fs.existsSync(libIn)) {
|
|
3208
3221
|
copyDir(libIn, libOut);
|
|
@@ -3298,9 +3311,7 @@ async function main(): Promise<void> {
|
|
|
3298
3311
|
}
|
|
3299
3312
|
}
|
|
3300
3313
|
|
|
3301
|
-
//
|
|
3302
|
-
// §18 PROJECT SCAFFOLDING
|
|
3303
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3314
|
+
// project scaffholding
|
|
3304
3315
|
|
|
3305
3316
|
function scaffoldProject(root: string): void {
|
|
3306
3317
|
for (const d of ["pages", "components", "styles", "libraries", "assets"]) {
|
|
@@ -3452,7 +3463,9 @@ component Card(title, color = "blue") {
|
|
|
3452
3463
|
`);
|
|
3453
3464
|
}
|
|
3454
3465
|
|
|
3455
|
-
|
|
3466
|
+
|
|
3467
|
+
|
|
3468
|
+
|
|
3456
3469
|
main().catch(e => {
|
|
3457
3470
|
process.stderr.write(`\x1b[31m${(e as Error).message}\x1b[0m\n`);
|
|
3458
3471
|
process.exit(1);
|