@yannosay/sinth 0.5.4 → 0.6.1
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 +334 -271
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;
|
|
@@ -130,12 +127,9 @@ interface CompileCtx {
|
|
|
130
127
|
mixedBlocks: MixedBlockEntry[];
|
|
131
128
|
mixedCounter: number;
|
|
132
129
|
logicBlocks: string[];
|
|
130
|
+
ifIdCounter: number;
|
|
133
131
|
}
|
|
134
132
|
|
|
135
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
136
|
-
// §2 UTILITIES
|
|
137
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
138
|
-
|
|
139
133
|
class SinthError extends Error {
|
|
140
134
|
constructor(
|
|
141
135
|
public readonly rawMsg: string,
|
|
@@ -183,7 +177,6 @@ function fnv1a(s: string): string {
|
|
|
183
177
|
return h.toString(16).padStart(8, "0").slice(0, 8);
|
|
184
178
|
}
|
|
185
179
|
|
|
186
|
-
/** camelCase → kebab-case with vendor prefix support */
|
|
187
180
|
function camelToKebab(s: string): string {
|
|
188
181
|
const vp = s.match(/^(Webkit|Moz|Ms)(.+)$/);
|
|
189
182
|
if (vp) return `-${vp[1].toLowerCase()}-${camelToKebab(vp[2])}`;
|
|
@@ -212,10 +205,7 @@ function litToString(lit: Literal): string {
|
|
|
212
205
|
}
|
|
213
206
|
}
|
|
214
207
|
|
|
215
|
-
//
|
|
216
|
-
// §3 LEXER
|
|
217
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
218
|
-
|
|
208
|
+
// lexer
|
|
219
209
|
const KEYWORDS: Record<string, TT> = {
|
|
220
210
|
"import": TT.KW_IMPORT,
|
|
221
211
|
"as": TT.KW_AS,
|
|
@@ -263,7 +253,7 @@ export class Lexer {
|
|
|
263
253
|
if (ch === "[") { tokens.push(this.single(TT.LBRACKET, loc)); continue; }
|
|
264
254
|
if (ch === "]") { tokens.push(this.single(TT.RBRACKET, loc)); continue; }
|
|
265
255
|
|
|
266
|
-
//
|
|
256
|
+
// two-character operators ->
|
|
267
257
|
if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
|
|
268
258
|
if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
|
|
269
259
|
if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
|
|
@@ -321,7 +311,7 @@ export class Lexer {
|
|
|
321
311
|
const ch = this.src[this.pos];
|
|
322
312
|
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") { this.adv(); continue; }
|
|
323
313
|
|
|
324
|
-
//
|
|
314
|
+
// sinth comments: -- line or --[ nestable block ]--
|
|
325
315
|
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
326
316
|
if (this.src[this.pos + 2] === "[") {
|
|
327
317
|
this.adv(); this.adv(); this.adv();
|
|
@@ -478,9 +468,7 @@ export class Lexer {
|
|
|
478
468
|
private isIdentCont(c: string) { return /[a-zA-Z0-9_\-\u00C0-\uFFFF]/.test(c); }
|
|
479
469
|
}
|
|
480
470
|
|
|
481
|
-
//
|
|
482
|
-
// §4 PARSER
|
|
483
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
471
|
+
// parser
|
|
484
472
|
|
|
485
473
|
export class Parser {
|
|
486
474
|
private pos = 0;
|
|
@@ -490,8 +478,8 @@ export class Parser {
|
|
|
490
478
|
|
|
491
479
|
constructor(private tokens: Token[], private file: string) {}
|
|
492
480
|
|
|
493
|
-
// ── Top-level file parse ────────────────────────────────────────────────
|
|
494
481
|
|
|
482
|
+
// top-level file parse
|
|
495
483
|
parse(): SinthFile {
|
|
496
484
|
let isPage = false;
|
|
497
485
|
const imports: ImportNode[] = [];
|
|
@@ -514,13 +502,13 @@ export class Parser {
|
|
|
514
502
|
if (entry.key === "title") isPage = true;
|
|
515
503
|
}
|
|
516
504
|
|
|
517
|
-
|
|
505
|
+
|
|
518
506
|
while (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
519
507
|
const vd = this.parseVarDeclaration();
|
|
520
508
|
if (vd) { varDecls.push(vd); }
|
|
521
509
|
}
|
|
522
510
|
|
|
523
|
-
|
|
511
|
+
|
|
524
512
|
while (!this.check(TT.EOF)) {
|
|
525
513
|
if (this.check(TT.KW_COMPONENT)) { defs.push(this.parseCompDef()); }
|
|
526
514
|
else if (this.check(TT.KW_STYLE)) { styles.push(this.parseStyleBlock()); }
|
|
@@ -543,8 +531,7 @@ const savedPos = this.pos;
|
|
|
543
531
|
const identName = this.peek().value;
|
|
544
532
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
545
533
|
|
|
546
|
-
//
|
|
547
|
-
// This prevents the parser from treating "guess" as a component and failing later on "=="
|
|
534
|
+
// detect accidental top-level expressions (guess == target" instead of "if guess == target")
|
|
548
535
|
const isComparison = [TT.OP_EQEQ, TT.OP_NEQ, TT.OP_LT, TT.OP_GT, TT.OP_LTEQ, TT.OP_GTEQ].includes(nextType);
|
|
549
536
|
const isLogicAndOr = nextType === TT.IDENT && (this.tokens[this.pos + 1]?.value === "and" || this.tokens[this.pos + 1]?.value === "or");
|
|
550
537
|
|
|
@@ -555,7 +542,7 @@ if (isComparison || isLogicAndOr) {
|
|
|
555
542
|
);
|
|
556
543
|
}
|
|
557
544
|
|
|
558
|
-
//
|
|
545
|
+
// top-level assignment: ident = expr
|
|
559
546
|
if (nextType === TT.EQUALS ||
|
|
560
547
|
(nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) ||
|
|
561
548
|
(nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS)) {
|
|
@@ -573,7 +560,7 @@ sinthRender();`;
|
|
|
573
560
|
continue;
|
|
574
561
|
}
|
|
575
562
|
|
|
576
|
-
//
|
|
563
|
+
// component usage
|
|
577
564
|
this.pos = savedPos;
|
|
578
565
|
uses.push(this.parseCompUse());
|
|
579
566
|
}
|
|
@@ -586,19 +573,13 @@ uses.push(this.parseCompUse());
|
|
|
586
573
|
return { filePath: this.file, isPage, imports, meta, defs, uses, styles, scripts, customEls, varDecls };
|
|
587
574
|
}
|
|
588
575
|
|
|
589
|
-
/** Flatten a top-level IfBlock into uses/scripts arrays so rendering works. */
|
|
590
576
|
private flattenIfToUses(ifNode: IfBlock, scripts: ScriptBlock[]): CompUse[] {
|
|
591
|
-
// Pure logic (only AssignStmt children): emit as script block
|
|
592
577
|
const bodyHasComp = ifNode.body.some(c => c.kind === "use");
|
|
593
578
|
const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
|
|
594
579
|
if (!bodyHasComp && !elseHasComp) {
|
|
595
580
|
scripts.push({ raw: compileIfToJS(ifNode), attrs: {}, loc: ifNode.loc });
|
|
596
581
|
return [];
|
|
597
582
|
}
|
|
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
583
|
const syntheticUse: CompUse = {
|
|
603
584
|
kind: "use",
|
|
604
585
|
name: "__IF_ROOT__",
|
|
@@ -609,7 +590,7 @@ uses.push(this.parseCompUse());
|
|
|
609
590
|
return [syntheticUse];
|
|
610
591
|
}
|
|
611
592
|
|
|
612
|
-
//
|
|
593
|
+
// imports
|
|
613
594
|
|
|
614
595
|
private parseImport(): ImportNode {
|
|
615
596
|
const loc = this.consume(TT.KW_IMPORT).loc;
|
|
@@ -629,7 +610,7 @@ uses.push(this.parseCompUse());
|
|
|
629
610
|
throw new SinthError(`Expected import path, library name, or 'css'`, loc);
|
|
630
611
|
}
|
|
631
612
|
|
|
632
|
-
//
|
|
613
|
+
// metadata
|
|
633
614
|
|
|
634
615
|
private isMetaStart(): boolean {
|
|
635
616
|
if (!this.check(TT.IDENT)) return false;
|
|
@@ -643,7 +624,7 @@ uses.push(this.parseCompUse());
|
|
|
643
624
|
return { key, value: this.parseLiteral(), loc };
|
|
644
625
|
}
|
|
645
626
|
|
|
646
|
-
//
|
|
627
|
+
// var declarations
|
|
647
628
|
|
|
648
629
|
private parseVarDeclaration(): VarDeclaration | null {
|
|
649
630
|
const loc = this.peek().loc;
|
|
@@ -731,17 +712,8 @@ private parseArrayLiteral(): Literal {
|
|
|
731
712
|
return obj;
|
|
732
713
|
}
|
|
733
714
|
|
|
734
|
-
//
|
|
715
|
+
// expression parsing
|
|
735
716
|
|
|
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
717
|
parseExpression(): Expression | null {
|
|
746
718
|
const tok = this.peek();
|
|
747
719
|
|
|
@@ -750,93 +722,77 @@ private parseArrayLiteral(): Literal {
|
|
|
750
722
|
this.consume(TT.IDENT);
|
|
751
723
|
const operand = this.parseExpression();
|
|
752
724
|
if (!operand) throw new SinthError("Expected expression after 'not'", tok.loc);
|
|
753
|
-
|
|
725
|
+
const unaryExpr: Expression = { kind: "unary", op: "not", operand };
|
|
726
|
+
return this.parseBinaryRHS(this.parsePostfix(unaryExpr));
|
|
754
727
|
}
|
|
755
728
|
|
|
756
|
-
// Parenthesised expression
|
|
757
729
|
if (this.check(TT.LPAREN)) {
|
|
758
730
|
this.consume(TT.LPAREN);
|
|
759
731
|
const inner = this.parseExpression();
|
|
760
732
|
if (!inner) throw new SinthError("Expected expression after '('", this.peek().loc);
|
|
761
733
|
if (!this.check(TT.RPAREN)) throw new SinthError("Expected closing ')'", this.peek().loc);
|
|
762
734
|
this.consume(TT.RPAREN);
|
|
763
|
-
|
|
764
|
-
return this.parseBinaryRHS(inner);
|
|
735
|
+
return this.parseBinaryRHS(this.parsePostfix(inner));
|
|
765
736
|
}
|
|
766
737
|
|
|
767
|
-
//
|
|
738
|
+
// literal
|
|
768
739
|
if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) ||
|
|
769
740
|
this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
|
|
770
741
|
const lit = this.parseLiteral();
|
|
771
742
|
const base: Expression = { kind: "literal", value: lit };
|
|
772
|
-
return this.parseBinaryRHS(base);
|
|
743
|
+
return this.parseBinaryRHS(this.parsePostfix(base));
|
|
773
744
|
}
|
|
774
745
|
|
|
775
|
-
//
|
|
746
|
+
// var or assignment
|
|
776
747
|
if (tok.type === TT.IDENT) {
|
|
777
748
|
const name = this.consume(TT.IDENT).value;
|
|
778
|
-
|
|
779
|
-
|
|
749
|
+
let varExpr: Expression;
|
|
750
|
+
|
|
751
|
+
// dot notation -> user.age
|
|
780
752
|
if (this.check(TT.DOT)) {
|
|
781
753
|
this.consume(TT.DOT);
|
|
782
754
|
const propName = this.consume(TT.IDENT).value;
|
|
783
755
|
const fullName = `${name}.${propName}`;
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
if (nextType === TT.OP_PLUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
788
|
-
this.consume(TT.OP_PLUS); this.consume(TT.EQUALS);
|
|
789
|
-
const rhs = this.parseExpression();
|
|
790
|
-
if (!rhs) throw new SinthError("Expected expression after +=", this.peek().loc);
|
|
791
|
-
return { kind: "assign", target: fullName, op: "+=", right: rhs };
|
|
792
|
-
}
|
|
793
|
-
if (nextType === TT.OP_MINUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
794
|
-
this.consume(TT.OP_MINUS); this.consume(TT.EQUALS);
|
|
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);
|
|
756
|
+
varExpr = { kind: "variable", name: fullName };
|
|
757
|
+
} else {
|
|
758
|
+
varExpr = { kind: "variable", name };
|
|
808
759
|
}
|
|
809
760
|
|
|
810
|
-
//
|
|
761
|
+
// apply bracket postfixes (e.g. data[key], data["x"])
|
|
762
|
+
const indexedExpr = this.parsePostfix(varExpr);
|
|
763
|
+
|
|
764
|
+
// if indexing was applied, we don't allow assignment, just binary RHS
|
|
765
|
+
if (indexedExpr !== varExpr) {
|
|
766
|
+
return this.parseBinaryRHS(indexedExpr);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// no indexing – check for assignment operators
|
|
811
770
|
const nextType = this.tokens[this.pos]?.type;
|
|
812
771
|
if (nextType === TT.OP_PLUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
813
772
|
this.consume(TT.OP_PLUS); this.consume(TT.EQUALS);
|
|
814
773
|
const rhs = this.parseExpression();
|
|
815
774
|
if (!rhs) throw new SinthError("Expected expression after +=", this.peek().loc);
|
|
816
|
-
return { kind: "assign", target: name
|
|
775
|
+
return { kind: "assign", target: varExpr.name!, op: "+=", right: rhs };
|
|
817
776
|
}
|
|
818
777
|
if (nextType === TT.OP_MINUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
819
778
|
this.consume(TT.OP_MINUS); this.consume(TT.EQUALS);
|
|
820
779
|
const rhs = this.parseExpression();
|
|
821
780
|
if (!rhs) throw new SinthError("Expected expression after -=", this.peek().loc);
|
|
822
|
-
return { kind: "assign", target: name
|
|
781
|
+
return { kind: "assign", target: varExpr.name!, op: "-=", right: rhs };
|
|
823
782
|
}
|
|
824
|
-
// Simple assignment
|
|
825
783
|
if (nextType === TT.EQUALS) {
|
|
826
784
|
this.consume(TT.EQUALS);
|
|
827
785
|
const rhs = this.parseExpression();
|
|
828
786
|
if (!rhs) throw new SinthError("Expected expression after =", this.peek().loc);
|
|
829
|
-
return { kind: "assign", target: name
|
|
787
|
+
return { kind: "assign", target: varExpr.name!, op: "=", right: rhs };
|
|
830
788
|
}
|
|
831
789
|
|
|
832
|
-
const varExpr: Expression = { kind: "variable", name };
|
|
833
790
|
return this.parseBinaryRHS(varExpr);
|
|
834
791
|
}
|
|
835
792
|
|
|
836
793
|
return null;
|
|
837
794
|
}
|
|
838
795
|
|
|
839
|
-
/** After parsing a left-hand operand, check for a binary operator and parse RHS. */
|
|
840
796
|
private parseBinaryRHS(left: Expression): Expression {
|
|
841
797
|
const t = this.peek().type;
|
|
842
798
|
let op: BinaryOp | null = null;
|
|
@@ -861,7 +817,20 @@ private parseArrayLiteral(): Literal {
|
|
|
861
817
|
return { kind: "binary", left, op, right };
|
|
862
818
|
}
|
|
863
819
|
|
|
864
|
-
|
|
820
|
+
private parsePostfix(base: Expression): Expression {
|
|
821
|
+
let expr = base;
|
|
822
|
+
while (this.check(TT.LBRACKET)) {
|
|
823
|
+
this.consume(TT.LBRACKET);
|
|
824
|
+
const key = this.parseExpression();
|
|
825
|
+
if (!key) throw new SinthError("Expected expression inside [...]", this.peek().loc);
|
|
826
|
+
this.consume(TT.RBRACKET);
|
|
827
|
+
expr = { kind: "index", object: expr, key };
|
|
828
|
+
}
|
|
829
|
+
return expr;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
// component definitions
|
|
865
834
|
|
|
866
835
|
private parseCompDef(): CompDef {
|
|
867
836
|
const loc = this.consume(TT.KW_COMPONENT).loc;
|
|
@@ -934,7 +903,7 @@ private parseArrayLiteral(): Literal {
|
|
|
934
903
|
return { body, styles, scripts };
|
|
935
904
|
}
|
|
936
905
|
|
|
937
|
-
//
|
|
906
|
+
// if / else blocks
|
|
938
907
|
|
|
939
908
|
private parseIfBlock(): IfBlock {
|
|
940
909
|
const loc = this.consume(TT.KW_IF).loc;
|
|
@@ -950,7 +919,6 @@ private parseArrayLiteral(): Literal {
|
|
|
950
919
|
if (this.check(TT.KW_ELSE)) {
|
|
951
920
|
this.consume(TT.KW_ELSE);
|
|
952
921
|
if (this.check(TT.KW_IF)) {
|
|
953
|
-
// else if → treat as nested IfBlock
|
|
954
922
|
elseBody = [this.parseIfBlock()];
|
|
955
923
|
} else {
|
|
956
924
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
|
|
@@ -963,10 +931,6 @@ private parseArrayLiteral(): Literal {
|
|
|
963
931
|
return { kind: "if", condition, body, elseBody, loc };
|
|
964
932
|
}
|
|
965
933
|
|
|
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
934
|
private parseUnifiedBody(): Child[] {
|
|
971
935
|
const children: Child[] = [];
|
|
972
936
|
|
|
@@ -979,7 +943,6 @@ private parseArrayLiteral(): Literal {
|
|
|
979
943
|
continue;
|
|
980
944
|
}
|
|
981
945
|
|
|
982
|
-
// Variable declarations inside unified bodies
|
|
983
946
|
if (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
984
947
|
const vd = this.parseVarDeclaration();
|
|
985
948
|
if (vd) this._varDecls.push(vd);
|
|
@@ -992,7 +955,6 @@ private parseArrayLiteral(): Literal {
|
|
|
992
955
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
993
956
|
const nextNextType = this.tokens[this.pos + 2]?.type;
|
|
994
957
|
|
|
995
|
-
// Handle dot notation for assignments: user.age = value
|
|
996
958
|
if (nextType === TT.DOT && (nextNextType === TT.EQUALS ||
|
|
997
959
|
(nextNextType === TT.OP_PLUS && this.tokens[this.pos + 3]?.type === TT.EQUALS) ||
|
|
998
960
|
(nextNextType === TT.OP_MINUS && this.tokens[this.pos + 3]?.type === TT.EQUALS))) {
|
|
@@ -1015,7 +977,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1015
977
|
continue;
|
|
1016
978
|
}
|
|
1017
979
|
|
|
1018
|
-
//
|
|
980
|
+
// assignment: ident = expr | ident += expr | ident -= expr
|
|
1019
981
|
const isSimpleAssign = nextType === TT.EQUALS;
|
|
1020
982
|
const isPlusAssign = nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
|
|
1021
983
|
const isMinusAssign = nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
|
|
@@ -1039,8 +1001,8 @@ private parseArrayLiteral(): Literal {
|
|
|
1039
1001
|
continue;
|
|
1040
1002
|
}
|
|
1041
1003
|
|
|
1042
|
-
//
|
|
1043
|
-
//
|
|
1004
|
+
// var expression
|
|
1005
|
+
// check dot notation variable reference (user.age without assignment)
|
|
1044
1006
|
if (nextType === TT.DOT) {
|
|
1045
1007
|
let fullName = this.consume(TT.IDENT).value;
|
|
1046
1008
|
this.consume(TT.DOT);
|
|
@@ -1049,7 +1011,17 @@ private parseArrayLiteral(): Literal {
|
|
|
1049
1011
|
continue;
|
|
1050
1012
|
}
|
|
1051
1013
|
|
|
1052
|
-
//
|
|
1014
|
+
// bracket notation: ident[...]
|
|
1015
|
+
if (nextType === TT.LBRACKET) {
|
|
1016
|
+
this.consume(TT.IDENT);
|
|
1017
|
+
const varExpr: Expression = { kind: "variable", name };
|
|
1018
|
+
const indexedExpr = this.parsePostfix(varExpr);
|
|
1019
|
+
children.push({ kind: "expr", expression: indexedExpr, loc });
|
|
1020
|
+
continue;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
// simple variable reference
|
|
1053
1025
|
this.consume(TT.IDENT);
|
|
1054
1026
|
children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
|
|
1055
1027
|
continue;
|
|
@@ -1059,12 +1031,36 @@ private parseArrayLiteral(): Literal {
|
|
|
1059
1031
|
`Unexpected token '${this.peek().value}' in if/else body`,
|
|
1060
1032
|
this.peek().loc,
|
|
1061
1033
|
);
|
|
1034
|
+
|
|
1035
|
+
// reject implicit concatenation only after expression children
|
|
1036
|
+
if (children.length > 0) {
|
|
1037
|
+
const lastChild = children[children.length - 1];
|
|
1038
|
+
if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
|
|
1039
|
+
const nextTT = this.tokens[this.pos]?.type;
|
|
1040
|
+
if (
|
|
1041
|
+
nextTT === TT.STRING || nextTT === TT.NUMBER ||
|
|
1042
|
+
nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
|
|
1043
|
+
(nextTT === TT.IDENT &&
|
|
1044
|
+
this.tokens[this.pos]?.value !== "var" &&
|
|
1045
|
+
this.tokens[this.pos]?.value !== "if" &&
|
|
1046
|
+
this.tokens[this.pos]?.value !== "for" &&
|
|
1047
|
+
this.tokens[this.pos]?.value !== "else" &&
|
|
1048
|
+
this.tokens[this.pos]?.value !== "and" &&
|
|
1049
|
+
this.tokens[this.pos]?.value !== "or")
|
|
1050
|
+
) {
|
|
1051
|
+
throw new SinthError(
|
|
1052
|
+
`Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
|
|
1053
|
+
this.peek().loc,
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1062
1058
|
}
|
|
1063
1059
|
|
|
1064
1060
|
return children;
|
|
1065
1061
|
}
|
|
1066
1062
|
|
|
1067
|
-
//
|
|
1063
|
+
// for loops
|
|
1068
1064
|
|
|
1069
1065
|
private parseForLoop(): ForLoop {
|
|
1070
1066
|
const loc = this.consume(TT.KW_FOR).loc;
|
|
@@ -1101,7 +1097,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1101
1097
|
return { kind: "for", keyVar, itemVar, indexVar, arrayVar, body, loc };
|
|
1102
1098
|
}
|
|
1103
1099
|
|
|
1104
|
-
//
|
|
1100
|
+
// component usages
|
|
1105
1101
|
|
|
1106
1102
|
private parseCompUse(): CompUse {
|
|
1107
1103
|
const loc = this.peek().loc;
|
|
@@ -1135,13 +1131,11 @@ private parseArrayLiteral(): Literal {
|
|
|
1135
1131
|
this.check(TT.NULL_LIT) || this.check(TT.LBRACKET) || this.check(TT.LBRACE)) {
|
|
1136
1132
|
value = this.parseLiteral();
|
|
1137
1133
|
} else if (this.check(TT.IDENT) || this.check(TT.LPAREN)) {
|
|
1138
|
-
// Try to parse as expression (handles user.age, user.age * 20, etc.)
|
|
1139
1134
|
const savedPos = this.pos;
|
|
1140
1135
|
const expr = this.parseExpression();
|
|
1141
1136
|
if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN))) {
|
|
1142
1137
|
value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
|
|
1143
1138
|
} else {
|
|
1144
|
-
// Not a full expression, treat as simple variable reference
|
|
1145
1139
|
this.pos = savedPos;
|
|
1146
1140
|
if (this.check(TT.IDENT)) {
|
|
1147
1141
|
value = { kind: "str", value: this.consume(TT.IDENT).value };
|
|
@@ -1196,6 +1190,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1196
1190
|
if (this.check(TT.DOT)) {
|
|
1197
1191
|
this.consume(TT.DOT);
|
|
1198
1192
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1193
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1194
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1195
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1199
1196
|
} else {
|
|
1200
1197
|
rhs = { kind: "variable", name: rhsName };
|
|
1201
1198
|
}
|
|
@@ -1219,7 +1216,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1219
1216
|
const rawName = this.peek().value;
|
|
1220
1217
|
const nextType = this.tokens[this.pos + 1]?.type;
|
|
1221
1218
|
|
|
1222
|
-
// Handle dot notation: user.age
|
|
1223
1219
|
if (nextType === TT.DOT) {
|
|
1224
1220
|
let fullName = this.consume(TT.IDENT).value;
|
|
1225
1221
|
this.consume(TT.DOT);
|
|
@@ -1227,7 +1223,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1227
1223
|
const afterDotType = this.tokens[this.pos]?.type;
|
|
1228
1224
|
|
|
1229
1225
|
if (afterDotType === TT.OP_PLUS) {
|
|
1230
|
-
// user.name + "string" + user.age
|
|
1231
1226
|
let leftExpr: Expression = { kind: "variable", name: fullName };
|
|
1232
1227
|
while (this.check(TT.OP_PLUS)) {
|
|
1233
1228
|
this.consume(TT.OP_PLUS);
|
|
@@ -1239,6 +1234,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1239
1234
|
if (this.check(TT.DOT)) {
|
|
1240
1235
|
this.consume(TT.DOT);
|
|
1241
1236
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1237
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1238
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1239
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1242
1240
|
} else {
|
|
1243
1241
|
rhs = { kind: "variable", name: rhsName };
|
|
1244
1242
|
}
|
|
@@ -1269,6 +1267,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1269
1267
|
if (this.check(TT.DOT)) {
|
|
1270
1268
|
this.consume(TT.DOT);
|
|
1271
1269
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1270
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1271
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1272
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1272
1273
|
} else {
|
|
1273
1274
|
rhs = { kind: "variable", name: rhsName };
|
|
1274
1275
|
}
|
|
@@ -1282,6 +1283,39 @@ private parseArrayLiteral(): Literal {
|
|
|
1282
1283
|
} else if (this.loopVar && rawName === this.loopVar) {
|
|
1283
1284
|
this.consume(TT.IDENT);
|
|
1284
1285
|
children.push({ kind: "expr", expression: { kind: "variable", name: rawName }, loc });
|
|
1286
|
+
} else if (nextType === TT.LBRACKET) {
|
|
1287
|
+
const name = this.consume(TT.IDENT).value;
|
|
1288
|
+
const varExpr: Expression = { kind: "variable", name };
|
|
1289
|
+
let indexedExpr = this.parsePostfix(varExpr);
|
|
1290
|
+
// support concatenation: data[prop] + "..."
|
|
1291
|
+
if (this.check(TT.OP_PLUS)) {
|
|
1292
|
+
let leftExpr: Expression = indexedExpr;
|
|
1293
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1294
|
+
this.consume(TT.OP_PLUS);
|
|
1295
|
+
let rhs: Expression | null = null;
|
|
1296
|
+
if (this.check(TT.STRING)) {
|
|
1297
|
+
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1298
|
+
} else if (this.check(TT.IDENT)) {
|
|
1299
|
+
const rhsName = this.consume(TT.IDENT).value;
|
|
1300
|
+
if (this.check(TT.DOT)) {
|
|
1301
|
+
this.consume(TT.DOT);
|
|
1302
|
+
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1303
|
+
} else if (this.check(TT.LBRACKET)) {
|
|
1304
|
+
const rhsVar: Expression = { kind: "variable", name: rhsName };
|
|
1305
|
+
rhs = this.parsePostfix(rhsVar);
|
|
1306
|
+
} else {
|
|
1307
|
+
rhs = { kind: "variable", name: rhsName };
|
|
1308
|
+
}
|
|
1309
|
+
} else {
|
|
1310
|
+
rhs = this.parseExpression();
|
|
1311
|
+
}
|
|
1312
|
+
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1313
|
+
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1314
|
+
}
|
|
1315
|
+
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
1316
|
+
} else {
|
|
1317
|
+
children.push({ kind: "expr", expression: indexedExpr, loc });
|
|
1318
|
+
}
|
|
1285
1319
|
} else if (nextType === TT.LPAREN || nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|
|
1286
1320
|
children.push(this.parseCompUse());
|
|
1287
1321
|
} else {
|
|
@@ -1290,11 +1324,34 @@ private parseArrayLiteral(): Literal {
|
|
|
1290
1324
|
}
|
|
1291
1325
|
}
|
|
1292
1326
|
else throw new SinthError(`Unexpected token '${this.peek().value}' in children`, this.peek().loc);
|
|
1327
|
+
// reject implicit concatenation only after expression children
|
|
1328
|
+
if (children.length > 0) {
|
|
1329
|
+
const lastChild = children[children.length - 1];
|
|
1330
|
+
if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
|
|
1331
|
+
const nextTT = this.tokens[this.pos]?.type;
|
|
1332
|
+
if (
|
|
1333
|
+
nextTT === TT.STRING || nextTT === TT.NUMBER ||
|
|
1334
|
+
nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
|
|
1335
|
+
(nextTT === TT.IDENT &&
|
|
1336
|
+
this.tokens[this.pos]?.value !== "var" &&
|
|
1337
|
+
this.tokens[this.pos]?.value !== "if" &&
|
|
1338
|
+
this.tokens[this.pos]?.value !== "for" &&
|
|
1339
|
+
this.tokens[this.pos]?.value !== "else" &&
|
|
1340
|
+
this.tokens[this.pos]?.value !== "and" &&
|
|
1341
|
+
this.tokens[this.pos]?.value !== "or")
|
|
1342
|
+
) {
|
|
1343
|
+
throw new SinthError(
|
|
1344
|
+
`Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
|
|
1345
|
+
this.peek().loc,
|
|
1346
|
+
);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1293
1350
|
}
|
|
1294
1351
|
return children;
|
|
1295
1352
|
}
|
|
1296
1353
|
|
|
1297
|
-
//
|
|
1354
|
+
// style & script blocks
|
|
1298
1355
|
|
|
1299
1356
|
private parseStyleBlock(): StyleBlock {
|
|
1300
1357
|
const loc = this.consume(TT.KW_STYLE).loc;
|
|
@@ -1310,7 +1367,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1310
1367
|
lang = lv as "css" | "scss";
|
|
1311
1368
|
}
|
|
1312
1369
|
else if (this.check(TT.IDENT)) {
|
|
1313
|
-
// v1.0.0: If IDENT is NOT followed by EQUALS, it's a component target name.
|
|
1314
1370
|
if (this.tokens[this.pos + 1]?.type !== TT.EQUALS) {
|
|
1315
1371
|
target = this.consume(TT.IDENT).value;
|
|
1316
1372
|
} else {
|
|
@@ -1336,8 +1392,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1336
1392
|
return { raw: this.consume(TT.RAW_BLOCK).value, attrs, loc };
|
|
1337
1393
|
}
|
|
1338
1394
|
|
|
1339
|
-
// ── Literals ─────────────────────────────────────────────────────────────
|
|
1340
|
-
|
|
1341
1395
|
private parseLiteral(): Literal {
|
|
1342
1396
|
const tok = this.peek();
|
|
1343
1397
|
if (this.check(TT.LBRACKET)) { return this.parseArrayLiteral(); }
|
|
@@ -1353,8 +1407,6 @@ private parseArrayLiteral(): Literal {
|
|
|
1353
1407
|
throw new SinthError(`Expected literal value, got '${tok.value}' (${TT[tok.type]})`, tok.loc);
|
|
1354
1408
|
}
|
|
1355
1409
|
|
|
1356
|
-
// ── Token helpers ─────────────────────────────────────────────────────────
|
|
1357
|
-
|
|
1358
1410
|
private peek(): Token { return this.tokens[this.pos]; }
|
|
1359
1411
|
private check(t: TT): boolean { return this.tokens[this.pos]?.type === t; }
|
|
1360
1412
|
private consume(t: TT): Token {
|
|
@@ -1370,9 +1422,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1370
1422
|
}
|
|
1371
1423
|
}
|
|
1372
1424
|
|
|
1373
|
-
//
|
|
1374
|
-
// §5 IMPORT RESOLVER
|
|
1375
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1425
|
+
// import resolver
|
|
1376
1426
|
|
|
1377
1427
|
interface ResolverConfig { projectRoot: string; libraryPaths: string[] }
|
|
1378
1428
|
|
|
@@ -1516,14 +1566,12 @@ function resolveSinthPath(p: string, fromFile: string, cfg: ResolverConfig): str
|
|
|
1516
1566
|
);
|
|
1517
1567
|
}
|
|
1518
1568
|
|
|
1519
|
-
//
|
|
1520
|
-
// §6 BUILT-IN COMPONENT MAP
|
|
1521
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1569
|
+
// built-in component map
|
|
1522
1570
|
|
|
1523
1571
|
interface BuiltinInfo { tag: string; defaultClass?: string; voidEl?: boolean }
|
|
1524
1572
|
|
|
1525
1573
|
const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
1526
|
-
//
|
|
1574
|
+
// structural
|
|
1527
1575
|
Main: { tag: "main" },
|
|
1528
1576
|
Header: { tag: "header" },
|
|
1529
1577
|
Footer: { tag: "footer" },
|
|
@@ -1541,7 +1589,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1541
1589
|
Row: { tag: "div", defaultClass: "row" },
|
|
1542
1590
|
Column: { tag: "div", defaultClass: "col" },
|
|
1543
1591
|
CardGrid: { tag: "div", defaultClass: "card-grid" },
|
|
1544
|
-
//
|
|
1592
|
+
// typography
|
|
1545
1593
|
Heading: { tag: "h1" },
|
|
1546
1594
|
SubHeading: { tag: "p", defaultClass: "subheading" },
|
|
1547
1595
|
Paragraph: { tag: "p" },
|
|
@@ -1572,7 +1620,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1572
1620
|
Ruby: { tag: "ruby" },
|
|
1573
1621
|
Rt: { tag: "rt" },
|
|
1574
1622
|
Rp: { tag: "rp" },
|
|
1575
|
-
//
|
|
1623
|
+
// interactive
|
|
1576
1624
|
Button: { tag: "button" },
|
|
1577
1625
|
Link: { tag: "a" },
|
|
1578
1626
|
NavLink: { tag: "a" },
|
|
@@ -1591,7 +1639,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1591
1639
|
Meter: { tag: "meter" },
|
|
1592
1640
|
Output: { tag: "output" },
|
|
1593
1641
|
Map: { tag: "map" },
|
|
1594
|
-
//
|
|
1642
|
+
// media
|
|
1595
1643
|
Picture: { tag: "picture" },
|
|
1596
1644
|
Video: { tag: "video" },
|
|
1597
1645
|
Audio: { tag: "audio" },
|
|
@@ -1601,10 +1649,11 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1601
1649
|
Svg: { tag: "svg" },
|
|
1602
1650
|
IFrame: { tag: "iframe" },
|
|
1603
1651
|
Object: { tag: "object" },
|
|
1604
|
-
//
|
|
1652
|
+
// void elements
|
|
1605
1653
|
Img: { tag: "img", voidEl: true },
|
|
1606
1654
|
Logo: { tag: "img", voidEl: true },
|
|
1607
1655
|
Input: { tag: "input", voidEl: true },
|
|
1656
|
+
Checkbox: { tag: "input", voidEl: true },
|
|
1608
1657
|
Hr: { tag: "hr", voidEl: true },
|
|
1609
1658
|
Br: { tag: "br", voidEl: true },
|
|
1610
1659
|
Wbr: { tag: "wbr", voidEl: true },
|
|
@@ -1612,10 +1661,10 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1612
1661
|
Embed: { tag: "embed", voidEl: true },
|
|
1613
1662
|
Col: { tag: "col", voidEl: true },
|
|
1614
1663
|
Area: { tag: "area", voidEl: true },
|
|
1615
|
-
//
|
|
1664
|
+
// lists
|
|
1616
1665
|
Ul: { tag: "ul" }, Ol: { tag: "ol" }, Li: { tag: "li" },
|
|
1617
1666
|
Dl: { tag: "dl" }, Dt: { tag: "dt" }, Dd: { tag: "dd" },
|
|
1618
|
-
//
|
|
1667
|
+
// tables
|
|
1619
1668
|
Table: { tag: "table" },
|
|
1620
1669
|
Caption: { tag: "caption" },
|
|
1621
1670
|
Thead: { tag: "thead" },
|
|
@@ -1625,7 +1674,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
1625
1674
|
Th: { tag: "th" },
|
|
1626
1675
|
Td: { tag: "td" },
|
|
1627
1676
|
Colgroup: { tag: "colgroup" },
|
|
1628
|
-
//
|
|
1677
|
+
// utility
|
|
1629
1678
|
Template: { tag: "template" },
|
|
1630
1679
|
Slot: { tag: "slot" },
|
|
1631
1680
|
NoScript: { tag: "noscript" },
|
|
@@ -1642,13 +1691,11 @@ function eventAttrName(name: string): string | null {
|
|
|
1642
1691
|
return EVENT_RE.test(name) ? name.toLowerCase() : null;
|
|
1643
1692
|
}
|
|
1644
1693
|
|
|
1645
|
-
//
|
|
1646
|
-
// §7 SINTH STYLE LANGUAGE — PREPROCESSOR
|
|
1647
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1694
|
+
// preprocessor
|
|
1648
1695
|
|
|
1649
1696
|
/**
|
|
1650
|
-
*
|
|
1651
|
-
*
|
|
1697
|
+
* for who's reading this: This maps Sinth Style pseudo-class shorthand keywords to real CSS pseudo-classes.
|
|
1698
|
+
* these are used inside component style blocks, for example: onHover { color: "blue" }
|
|
1652
1699
|
*/
|
|
1653
1700
|
const SINTH_PSEUDO_CLASS: Record<string, string> = {
|
|
1654
1701
|
onHover: ":hover",
|
|
@@ -1674,9 +1721,6 @@ const SINTH_PSEUDO_CLASS: Record<string, string> = {
|
|
|
1674
1721
|
onLink: ":link",
|
|
1675
1722
|
};
|
|
1676
1723
|
|
|
1677
|
-
/**
|
|
1678
|
-
* Maps Sinth Style pseudo-element shorthand keywords to real CSS pseudo-elements.
|
|
1679
|
-
*/
|
|
1680
1724
|
const SINTH_PSEUDO_ELEMENT: Record<string, string> = {
|
|
1681
1725
|
before: "::before",
|
|
1682
1726
|
after: "::after",
|
|
@@ -1688,7 +1732,6 @@ const SINTH_PSEUDO_ELEMENT: Record<string, string> = {
|
|
|
1688
1732
|
backdrop: "::backdrop",
|
|
1689
1733
|
};
|
|
1690
1734
|
|
|
1691
|
-
/** Resolves a Sinth component name to its CSS selector string. */
|
|
1692
1735
|
function sinthCompToSelector(name: string): string {
|
|
1693
1736
|
const info = BUILTIN_MAP[name];
|
|
1694
1737
|
if (!info) return name.toLowerCase();
|
|
@@ -1700,14 +1743,11 @@ function sinthCompToSelector(name: string): string {
|
|
|
1700
1743
|
}
|
|
1701
1744
|
|
|
1702
1745
|
/**
|
|
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) { ... }`
|
|
1746
|
+
* supported conversions:
|
|
1747
|
+
* - component names as selectors: `Paragraph { ... }` → `p { ... }`
|
|
1748
|
+
* - pseudo-class shorthands: `onHover { ... }` → `&:hover { ... }`
|
|
1749
|
+
* - pseudo-element shorthands: `before { ... }` → `&::before { ... }`
|
|
1750
|
+
* - media queries: `media(maxWidth: "600px") { ... }` → `@media (max-width: 600px) { ... }`
|
|
1711
1751
|
* - CSS custom properties: `var --primary: "#3b82f6"` → `--primary: #3b82f6;`
|
|
1712
1752
|
*/
|
|
1713
1753
|
function preprocessSinthStyle(raw: string): string {
|
|
@@ -1718,17 +1758,14 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1718
1758
|
const trimmed = line.trim();
|
|
1719
1759
|
const indent = line.match(/^(\s*)/)?.[1] ?? "";
|
|
1720
1760
|
|
|
1721
|
-
// Empty lines pass through
|
|
1722
1761
|
if (!trimmed) { result.push(line); continue; }
|
|
1723
1762
|
|
|
1724
|
-
// Strip Sinth comments from style blocks
|
|
1725
1763
|
if (trimmed.startsWith("--") && !trimmed.startsWith("--[")) { continue; }
|
|
1726
1764
|
// Standard comments pass through
|
|
1727
1765
|
if (trimmed.startsWith("//") || trimmed.startsWith("/*") || trimmed.startsWith("*")) {
|
|
1728
1766
|
result.push(line); continue;
|
|
1729
1767
|
}
|
|
1730
1768
|
|
|
1731
|
-
// media(maxWidth: "600px") { → @media (max-width: 600px) {
|
|
1732
1769
|
const mediaMatch = trimmed.match(/^media\s*\(([^)]+)\)\s*(\{?\s*)$/);
|
|
1733
1770
|
if (mediaMatch) {
|
|
1734
1771
|
const params = parseSinthMediaParams(mediaMatch[1]);
|
|
@@ -1736,7 +1773,6 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1736
1773
|
continue;
|
|
1737
1774
|
}
|
|
1738
1775
|
|
|
1739
|
-
// Block opener: check if it's a keyword that maps to a pseudo-class/element
|
|
1740
1776
|
const blockMatch = trimmed.match(/^([a-zA-Z][a-zA-Z0-9]*)(\s*\{\s*)$/);
|
|
1741
1777
|
if (blockMatch) {
|
|
1742
1778
|
const keyword = blockMatch[1];
|
|
@@ -1753,14 +1789,12 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1753
1789
|
continue;
|
|
1754
1790
|
}
|
|
1755
1791
|
|
|
1756
|
-
// Capital-letter IDENT = component name
|
|
1757
1792
|
if (/^[A-Z]/.test(keyword) && BUILTIN_MAP[keyword]) {
|
|
1758
1793
|
result.push(`${indent}${sinthCompToSelector(keyword)} {`);
|
|
1759
1794
|
continue;
|
|
1760
1795
|
}
|
|
1761
1796
|
}
|
|
1762
1797
|
|
|
1763
|
-
// CSS custom property shorthand: `var --primary: "value"` → `--primary: value;`
|
|
1764
1798
|
const varPropMatch = trimmed.match(/^var\s+(--[a-zA-Z][a-zA-Z0-9-]*)\s*:\s*["']?([^"';]+)["']?\s*;?\s*$/);
|
|
1765
1799
|
if (varPropMatch) {
|
|
1766
1800
|
result.push(`${indent}${varPropMatch[1]}: ${varPropMatch[2].trim()};`);
|
|
@@ -1773,7 +1807,6 @@ function preprocessSinthStyle(raw: string): string {
|
|
|
1773
1807
|
return result.join("\n");
|
|
1774
1808
|
}
|
|
1775
1809
|
|
|
1776
|
-
/** Converts Sinth media-query params to CSS @media condition string. */
|
|
1777
1810
|
function parseSinthMediaParams(params: string): string {
|
|
1778
1811
|
const parts = params.split(",").map(p => {
|
|
1779
1812
|
const m = p.trim().match(/^([a-zA-Z]+)\s*:\s*["']?([^"',]+)["']?\s*$/);
|
|
@@ -1783,15 +1816,9 @@ function parseSinthMediaParams(params: string): string {
|
|
|
1783
1816
|
return parts.join(") and (");
|
|
1784
1817
|
}
|
|
1785
1818
|
|
|
1786
|
-
//
|
|
1787
|
-
|
|
1788
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1819
|
+
// style processor
|
|
1820
|
+
|
|
1789
1821
|
|
|
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
1822
|
function convertCSSProps(css: string): string {
|
|
1796
1823
|
return css.split("\n").map(line => {
|
|
1797
1824
|
const trimmed = line.trim();
|
|
@@ -1806,7 +1833,6 @@ function convertCSSProps(css: string): string {
|
|
|
1806
1833
|
|
|
1807
1834
|
const [, indent, rawProp, , rawVal] = m;
|
|
1808
1835
|
|
|
1809
|
-
// Only convert if there's a camelCase hint or a quoted value
|
|
1810
1836
|
const hasCamelCase = /[a-z][A-Z]/.test(rawProp) || /^[A-Z]/.test(rawProp);
|
|
1811
1837
|
const hasQuotedVal = /^["']/.test(rawVal.trim());
|
|
1812
1838
|
if (!hasCamelCase && !hasQuotedVal) return line;
|
|
@@ -1822,15 +1848,6 @@ function convertCSSProps(css: string): string {
|
|
|
1822
1848
|
}).join("\n");
|
|
1823
1849
|
}
|
|
1824
1850
|
|
|
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
1851
|
function processStyleBlock(
|
|
1835
1852
|
block: StyleBlock,
|
|
1836
1853
|
hash: string,
|
|
@@ -1838,13 +1855,13 @@ function processStyleBlock(
|
|
|
1838
1855
|
): string {
|
|
1839
1856
|
let raw = block.raw;
|
|
1840
1857
|
|
|
1841
|
-
// 1.
|
|
1858
|
+
// 1. interpolate {param} placeholders
|
|
1842
1859
|
if (params.size > 0) raw = interpolateAttr(raw, params);
|
|
1843
1860
|
|
|
1844
|
-
// 2.
|
|
1861
|
+
// 2. sinth style preprocessing
|
|
1845
1862
|
raw = preprocessSinthStyle(raw);
|
|
1846
1863
|
|
|
1847
|
-
// 3.
|
|
1864
|
+
// 3. warn if & in plain CSS
|
|
1848
1865
|
if (block.lang === "css" && raw.includes("&")) {
|
|
1849
1866
|
SinthWarning.emit(
|
|
1850
1867
|
`'&' (CSS nesting) found in style block. Nested selectors require a modern browser or lang="scss".`,
|
|
@@ -1870,13 +1887,13 @@ function processStyleBlock(
|
|
|
1870
1887
|
}
|
|
1871
1888
|
}
|
|
1872
1889
|
|
|
1873
|
-
// 6.
|
|
1890
|
+
// 6. if target component given, wrap content in its CSS selector
|
|
1874
1891
|
if (block.target) {
|
|
1875
1892
|
const selector = sinthCompToSelector(block.target);
|
|
1876
1893
|
css = `${selector} {\n${css}\n}`;
|
|
1877
1894
|
}
|
|
1878
1895
|
|
|
1879
|
-
// 7.
|
|
1896
|
+
// 7. scope (except it's global)
|
|
1880
1897
|
if (!block.global) css = scopeCSS(css, hash);
|
|
1881
1898
|
|
|
1882
1899
|
return css;
|
|
@@ -1970,9 +1987,8 @@ function extractCSSBlock(css: string, openBrace: number): { content: string; end
|
|
|
1970
1987
|
return { content: css.substring(openBrace + 1, i), end: i + 1 };
|
|
1971
1988
|
}
|
|
1972
1989
|
|
|
1973
|
-
|
|
1974
|
-
//
|
|
1975
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
1990
|
+
|
|
1991
|
+
// expression -> js compiler
|
|
1976
1992
|
|
|
1977
1993
|
function compileExprToJS(expr: Expression): string {
|
|
1978
1994
|
switch (expr.kind) {
|
|
@@ -1998,6 +2014,8 @@ function compileExprToJS(expr: Expression): string {
|
|
|
1998
2014
|
const v = expr.right ? compileExprToJS(expr.right) : "null";
|
|
1999
2015
|
return `${expr.target} ${expr.op} ${v}`;
|
|
2000
2016
|
}
|
|
2017
|
+
case "index":
|
|
2018
|
+
return `${compileExprToJS(expr.object!)}[${compileExprToJS(expr.key!)}]`;
|
|
2001
2019
|
case "postfix":
|
|
2002
2020
|
return `${expr.target}${expr.op}`;
|
|
2003
2021
|
default:
|
|
@@ -2005,7 +2023,6 @@ function compileExprToJS(expr: Expression): string {
|
|
|
2005
2023
|
}
|
|
2006
2024
|
}
|
|
2007
2025
|
|
|
2008
|
-
/** Compiles a pure-logic IfBlock (containing only AssignStmt children) to JS. */
|
|
2009
2026
|
function compileIfToJS(ifBlock: IfBlock): string {
|
|
2010
2027
|
const cond = compileExprToJS(ifBlock.condition);
|
|
2011
2028
|
const ifJS = bodyToJS(ifBlock.body);
|
|
@@ -2026,9 +2043,7 @@ function bodyToJS(children: Child[]): string {
|
|
|
2026
2043
|
.join("");
|
|
2027
2044
|
}
|
|
2028
2045
|
|
|
2029
|
-
//
|
|
2030
|
-
// §10 HTML GENERATOR
|
|
2031
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2046
|
+
// the HTML generator
|
|
2032
2047
|
|
|
2033
2048
|
/**
|
|
2034
2049
|
* CSS property names that are allowed as inline style shorthand attributes on
|
|
@@ -2084,6 +2099,8 @@ if (name === "model" && value?.kind === "str") {
|
|
|
2084
2099
|
return `oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)" value="${escAttr(vName)}"`;
|
|
2085
2100
|
}
|
|
2086
2101
|
|
|
2102
|
+
|
|
2103
|
+
|
|
2087
2104
|
if (name === "delay") {
|
|
2088
2105
|
if (value.kind === "num") {
|
|
2089
2106
|
return `data-sinth-delay="${value.value}" style="display:none"`;
|
|
@@ -2100,11 +2117,14 @@ if (name === "delay") {
|
|
|
2100
2117
|
return `data-sinth-delay-expr="${escAttr(v)}" style="display:none"`;
|
|
2101
2118
|
}
|
|
2102
2119
|
|
|
2120
|
+
if (name === "checked") {
|
|
2121
|
+
return (value as any)?.kind === "bool" && (value as any).value ? "checked" : "";
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2103
2124
|
if (value.kind === "num") return `${name}="${value.value}"`;
|
|
2104
2125
|
|
|
2105
2126
|
let raw = value.value;
|
|
2106
2127
|
|
|
2107
|
-
// Inline expression reference
|
|
2108
2128
|
if (raw.startsWith("__EXPR__")) {
|
|
2109
2129
|
const exprJson = raw.substring("__EXPR__".length);
|
|
2110
2130
|
try {
|
|
@@ -2180,8 +2200,6 @@ function renderChild(
|
|
|
2180
2200
|
}
|
|
2181
2201
|
|
|
2182
2202
|
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
2203
|
ctx.logicBlocks.push(compileExprToJS(child.expression) + ";");
|
|
2186
2204
|
return "";
|
|
2187
2205
|
}
|
|
@@ -2203,15 +2221,6 @@ function renderChild(
|
|
|
2203
2221
|
}
|
|
2204
2222
|
}
|
|
2205
2223
|
|
|
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
2224
|
function renderIfBlock(
|
|
2216
2225
|
ifBlock: IfBlock,
|
|
2217
2226
|
ctx: CompileCtx,
|
|
@@ -2225,24 +2234,24 @@ depth: number,
|
|
|
2225
2234
|
const hasAssign = allChildren.some(c => c.kind === "assign_stmt");
|
|
2226
2235
|
const hasComp = allChildren.some(c => c.kind === "use");
|
|
2227
2236
|
|
|
2228
|
-
//
|
|
2237
|
+
// pure logic
|
|
2229
2238
|
if (hasAssign && !hasComp) {
|
|
2230
2239
|
ctx.logicBlocks.push(compileIfToJS(ifBlock));
|
|
2231
2240
|
return "";
|
|
2232
2241
|
}
|
|
2233
2242
|
|
|
2234
|
-
//
|
|
2243
|
+
// pure DOM
|
|
2235
2244
|
if (!hasAssign && hasComp) {
|
|
2245
|
+
const tplId = ctx.ifIdCounter++;
|
|
2236
2246
|
const bodyHTML = ifBlock.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2237
2247
|
const elseHTML = (ifBlock.elseBody ?? []).map(c => renderChild(c, ctx, params, depth + 1)).join("");
|
|
2238
2248
|
return (
|
|
2239
|
-
`<template data-sinth-if="${escAttr(condJS)}">${bodyHTML}</template>` +
|
|
2240
|
-
(elseHTML ? `<template data-sinth-else>${elseHTML}</template>` : "")
|
|
2249
|
+
`<template data-sinth-if="${escAttr(condJS)}" data-sinth-if-id="${tplId}">${bodyHTML}</template>` +
|
|
2250
|
+
(elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
|
|
2241
2251
|
);
|
|
2242
2252
|
}
|
|
2243
2253
|
|
|
2244
|
-
//
|
|
2245
|
-
// Both assignments and component children appear in at least one branch.
|
|
2254
|
+
// mixed
|
|
2246
2255
|
const id = `__sm${ctx.mixedCounter++}__`;
|
|
2247
2256
|
|
|
2248
2257
|
const ifAssignJS = ifBlock.body
|
|
@@ -2265,7 +2274,6 @@ depth: number,
|
|
|
2265
2274
|
.map(c => renderChild(c, ctx, params, depth + 1))
|
|
2266
2275
|
.join("");
|
|
2267
2276
|
|
|
2268
|
-
// Detect if both branches have a component with the same id — use it as replace anchor
|
|
2269
2277
|
let replaceId: string | undefined;
|
|
2270
2278
|
const ifFirstComp = ifBlock.body.find(c => c.kind === "use") as CompUse | undefined;
|
|
2271
2279
|
const elseFirstComp = (ifBlock.elseBody ?? []).find(c => c.kind === "use") as CompUse | undefined;
|
|
@@ -2300,7 +2308,7 @@ function renderCompUse(
|
|
|
2300
2308
|
params: Map<string, string>,
|
|
2301
2309
|
depth: number,
|
|
2302
2310
|
): string {
|
|
2303
|
-
//
|
|
2311
|
+
// transparent wrapper inserted by flattenIfToUses()
|
|
2304
2312
|
if (use.name === "__IF_ROOT__") {
|
|
2305
2313
|
return use.children.map(c => renderChild(c, ctx, params, depth)).join("");
|
|
2306
2314
|
}
|
|
@@ -2312,11 +2320,11 @@ function renderCompUse(
|
|
|
2312
2320
|
return interpolateAttr(litToString(ca.value), params);
|
|
2313
2321
|
}
|
|
2314
2322
|
|
|
2315
|
-
//
|
|
2323
|
+
// user-defined component
|
|
2316
2324
|
const userDef = ctx.allDefs.get(use.name);
|
|
2317
2325
|
if (userDef) return expandUserComp(use, userDef, ctx, params, depth + 1);
|
|
2318
2326
|
|
|
2319
|
-
//
|
|
2327
|
+
// custom element
|
|
2320
2328
|
const customEl = ctx.customEls.get(use.name);
|
|
2321
2329
|
if (customEl) {
|
|
2322
2330
|
const attrParts = [`data-s="${ctx.scopeHash}"`];
|
|
@@ -2328,7 +2336,7 @@ function renderCompUse(
|
|
|
2328
2336
|
return `<${customEl.tagName} ${attrParts.join(" ")}>${inner}</${customEl.tagName}>`;
|
|
2329
2337
|
}
|
|
2330
2338
|
|
|
2331
|
-
//
|
|
2339
|
+
// built-in component
|
|
2332
2340
|
const { tag, defaultClass, voidEl } = resolveBuiltinTag(use.name, use.attrs);
|
|
2333
2341
|
const isVoid = voidEl || VOID_TAGS.has(tag);
|
|
2334
2342
|
|
|
@@ -2339,7 +2347,6 @@ function renderCompUse(
|
|
|
2339
2347
|
for (const attr of use.attrs) {
|
|
2340
2348
|
if (attr.name === "level" && use.name === "Heading") continue;
|
|
2341
2349
|
|
|
2342
|
-
// v1.0.0: Inline CSS shorthand Paragraph(color: "red") { ... }
|
|
2343
2350
|
if (INLINE_STYLE_PROPS.has(attr.name) && attr.value) {
|
|
2344
2351
|
const val = attr.value.kind === "str"
|
|
2345
2352
|
? interpolateAttr(attr.value.value, params)
|
|
@@ -2360,6 +2367,38 @@ function renderCompUse(
|
|
|
2360
2367
|
if (classes) attrParts.push(`class="${escAttr(classes)}"`);
|
|
2361
2368
|
if (inlineStyleParts.length > 0) attrParts.push(`style="${escAttr(inlineStyleParts.join("; "))}"`);
|
|
2362
2369
|
if (tag === "button" && !use.attrs.some(a => a.name === "type")) attrParts.push(`type="button"`);
|
|
2370
|
+
if (use.name === "Input") {
|
|
2371
|
+
const bindAttr = use.attrs.find(a => a.name === "bind");
|
|
2372
|
+
const modelAttr = use.attrs.find(a => a.name === "model");
|
|
2373
|
+
const bindOrModel = bindAttr || modelAttr;
|
|
2374
|
+
if (bindOrModel?.value?.kind === "str") {
|
|
2375
|
+
let vName = bindOrModel.value.value;
|
|
2376
|
+
if (vName.startsWith("__EXPR__")) {
|
|
2377
|
+
try {
|
|
2378
|
+
const expr: Expression = JSON.parse(vName.substring(8));
|
|
2379
|
+
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
2380
|
+
} catch {}
|
|
2381
|
+
}
|
|
2382
|
+
if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="text"`);
|
|
2383
|
+
attrParts.push(`oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)"`);
|
|
2384
|
+
attrParts.push(`data-sinth-value="${escAttr(vName)}"`);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
if (use.name === "Checkbox") {
|
|
2388
|
+
if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="checkbox"`);
|
|
2389
|
+
const bindAttr = use.attrs.find(a => a.name === "bind");
|
|
2390
|
+
if (bindAttr?.value?.kind === "str") {
|
|
2391
|
+
let vName = bindAttr.value.value;
|
|
2392
|
+
if (vName.startsWith("__EXPR__")) {
|
|
2393
|
+
try {
|
|
2394
|
+
const expr: Expression = JSON.parse(vName.substring(8));
|
|
2395
|
+
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
2396
|
+
} catch {}
|
|
2397
|
+
}
|
|
2398
|
+
attrParts.push(`onchange="(function(e){ ${vName} = e.target.checked; sinthRender(); })(event)"`);
|
|
2399
|
+
attrParts.push(`data-sinth-checked="${escAttr(vName)}"`);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2363
2402
|
|
|
2364
2403
|
const attrStr = attrParts.length ? " " + attrParts.join(" ") : "";
|
|
2365
2404
|
|
|
@@ -2417,9 +2456,7 @@ function expandUserComp(
|
|
|
2417
2456
|
return def.body.map(c => renderChild(c, ctx, local, depth)).join("");
|
|
2418
2457
|
}
|
|
2419
2458
|
|
|
2420
|
-
//
|
|
2421
|
-
// §11 SCRIPT COLLECTOR
|
|
2422
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2459
|
+
// script collector
|
|
2423
2460
|
|
|
2424
2461
|
function collectScripts(
|
|
2425
2462
|
file: SinthFile,
|
|
@@ -2458,9 +2495,8 @@ function extractFunctionNames(js: string): string[] {
|
|
|
2458
2495
|
return names;
|
|
2459
2496
|
}
|
|
2460
2497
|
|
|
2461
|
-
|
|
2462
|
-
//
|
|
2463
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2498
|
+
|
|
2499
|
+
// head gen
|
|
2464
2500
|
|
|
2465
2501
|
interface HeadData {
|
|
2466
2502
|
title?: string; fav?: string;
|
|
@@ -2543,14 +2579,9 @@ function renderHead(
|
|
|
2543
2579
|
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
2544
2580
|
}
|
|
2545
2581
|
|
|
2546
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2547
|
-
// §13 SINTH RUNTIME — TREE-SHAKEN BUILDER
|
|
2548
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2549
2582
|
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
* features that are actually used by this page. This keeps output lean.
|
|
2553
|
-
*/
|
|
2583
|
+
// sinth runtime
|
|
2584
|
+
|
|
2554
2585
|
function buildRuntime(opts: {
|
|
2555
2586
|
varDecls: VarDeclaration[];
|
|
2556
2587
|
bodyHTML: string;
|
|
@@ -2560,8 +2591,7 @@ function buildRuntime(opts: {
|
|
|
2560
2591
|
}): string {
|
|
2561
2592
|
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars } = opts;
|
|
2562
2593
|
|
|
2563
|
-
|
|
2564
|
-
// Feature flags
|
|
2594
|
+
|
|
2565
2595
|
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
2566
2596
|
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
2567
2597
|
const needsFor = bodyHTML.includes("data-sinth-for");
|
|
@@ -2570,7 +2600,7 @@ const needsMixed = mixedBlocks.length > 0;
|
|
|
2570
2600
|
const needsLogic = logicBlocks.length > 0;
|
|
2571
2601
|
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
2572
2602
|
|
|
2573
|
-
//
|
|
2603
|
+
// var decls with type defaults
|
|
2574
2604
|
const varLines = varDecls.map(v => {
|
|
2575
2605
|
if (!v.value) {
|
|
2576
2606
|
if (!assignedVars.has(v.name)) {
|
|
@@ -2603,21 +2633,23 @@ return `var ${v.name} = ${val};`;
|
|
|
2603
2633
|
|
|
2604
2634
|
let renderBody = "";
|
|
2605
2635
|
|
|
2606
|
-
// Scroll position preservation
|
|
2607
2636
|
renderBody += ` var _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
2608
2637
|
|
|
2609
|
-
// Pure-logic if blocks
|
|
2610
2638
|
if (needsLogic) {
|
|
2611
2639
|
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
2612
2640
|
}
|
|
2613
2641
|
|
|
2614
|
-
// data-sinth-for templates (MUST run before if-templates)
|
|
2615
|
-
// data-sinth-for templates (MUST run before if-templates)
|
|
2616
2642
|
if (needsFor) {
|
|
2617
2643
|
renderBody += `
|
|
2618
2644
|
document.querySelectorAll('template[data-sinth-for]').forEach(function(t) {
|
|
2619
2645
|
var source;
|
|
2620
2646
|
try { source = eval(t.dataset.sinthFor); } catch(e) { source = []; }
|
|
2647
|
+
var newHash = '';
|
|
2648
|
+
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
2649
|
+
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
t.dataset.sinthForHash = newHash;
|
|
2621
2653
|
var isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
2622
2654
|
var entries;
|
|
2623
2655
|
if (isObj) {
|
|
@@ -2683,12 +2715,16 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2683
2715
|
}
|
|
2684
2716
|
});
|
|
2685
2717
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2718
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2719
|
+
el.dataset.sinthDelayDone = '1';
|
|
2686
2720
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2687
2721
|
el.style.display = 'none';
|
|
2688
2722
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
2689
2723
|
else el.style.display = '';
|
|
2690
2724
|
});
|
|
2691
2725
|
frag.querySelectorAll('[data-sinth-delay-expr]').forEach(function(el) {
|
|
2726
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2727
|
+
el.dataset.sinthDelayDone = '1';
|
|
2692
2728
|
try {
|
|
2693
2729
|
eval('var ' + _item + ' = _user;');
|
|
2694
2730
|
if (_key) eval('var ' + _key + ' = _k;');
|
|
@@ -2704,11 +2740,11 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2704
2740
|
});
|
|
2705
2741
|
`;
|
|
2706
2742
|
}
|
|
2707
|
-
// data-sinth-if / data-sinth-else templates (run AFTER for-loops)
|
|
2708
2743
|
if (needsIf) {
|
|
2709
2744
|
renderBody += `
|
|
2710
2745
|
document.querySelectorAll('template[data-sinth-if]').forEach(function(t) {
|
|
2711
|
-
var
|
|
2746
|
+
var ifId = t.dataset.sinthIfId;
|
|
2747
|
+
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
2712
2748
|
var cond;
|
|
2713
2749
|
try { cond = eval(t.dataset.sinthIf); } catch(e) { cond = false; }
|
|
2714
2750
|
if (cond) {
|
|
@@ -2718,7 +2754,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2718
2754
|
} else {
|
|
2719
2755
|
anchor = document.createElement('span');
|
|
2720
2756
|
anchor.style.display = 'none';
|
|
2721
|
-
anchor.dataset.sinthIfAnchor =
|
|
2757
|
+
anchor.dataset.sinthIfAnchor = ifId;
|
|
2722
2758
|
t.parentNode.insertBefore(anchor, t);
|
|
2723
2759
|
}
|
|
2724
2760
|
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
@@ -2726,6 +2762,8 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2726
2762
|
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2727
2763
|
});
|
|
2728
2764
|
frag.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
2765
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
2766
|
+
el.dataset.sinthDelayDone = '1';
|
|
2729
2767
|
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
2730
2768
|
el.style.display = 'none';
|
|
2731
2769
|
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
@@ -2747,14 +2785,14 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2747
2785
|
}
|
|
2748
2786
|
var elseT = t.nextElementSibling;
|
|
2749
2787
|
if (elseT && elseT.dataset && elseT.hasAttribute('data-sinth-else')) {
|
|
2750
|
-
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' +
|
|
2788
|
+
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
2751
2789
|
if (ea) {
|
|
2752
2790
|
var cur3 = ea.nextSibling;
|
|
2753
2791
|
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
2754
2792
|
} else {
|
|
2755
2793
|
ea = document.createElement('span');
|
|
2756
2794
|
ea.style.display = 'none';
|
|
2757
|
-
ea.dataset.sinthIfAnchor = '__else__' +
|
|
2795
|
+
ea.dataset.sinthIfAnchor = '__else__' + ifId;
|
|
2758
2796
|
t.parentNode.insertBefore(ea, t);
|
|
2759
2797
|
}
|
|
2760
2798
|
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
@@ -2776,7 +2814,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2776
2814
|
});
|
|
2777
2815
|
t.parentNode.insertBefore(ef, t);
|
|
2778
2816
|
} else {
|
|
2779
|
-
var ea2 = t.parentNode ? t.parentNode.querySelector('[data-sinth-if-anchor="__else__' +
|
|
2817
|
+
var ea2 = t.parentNode ? t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]') : null;
|
|
2780
2818
|
if (ea2) {
|
|
2781
2819
|
var ec = ea2.nextSibling;
|
|
2782
2820
|
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
@@ -2788,8 +2826,7 @@ try { eval('var ' + _item + ' = _user;'); if (_key) eval('var ' + _key + ' = _k;
|
|
|
2788
2826
|
`;
|
|
2789
2827
|
}
|
|
2790
2828
|
|
|
2791
|
-
|
|
2792
|
-
// Mixed if/else blocks
|
|
2829
|
+
// mixed if/else blocks
|
|
2793
2830
|
if (needsMixed) {
|
|
2794
2831
|
for (const mb of mixedBlocks) {
|
|
2795
2832
|
const ifHTMLJS = JSON.stringify(mb.ifHTML);
|
|
@@ -2809,6 +2846,26 @@ ${mb.elseJS ? mb.elseJS : ""} __el.innerHTML = ${elseHTMLJS};
|
|
|
2809
2846
|
__el.querySelectorAll('.sinth-expr').forEach(function(e) {
|
|
2810
2847
|
try { e.textContent = eval(e.dataset.expr); } catch(ex) {}
|
|
2811
2848
|
});
|
|
2849
|
+
__el.querySelectorAll('template[data-sinth-if]').forEach(function(innerT) {
|
|
2850
|
+
var innerCond;
|
|
2851
|
+
try { innerCond = eval(innerT.dataset.sinthIf); } catch(e) { innerCond = false; }
|
|
2852
|
+
if (innerCond) {
|
|
2853
|
+
var innerFrag = document.createRange().createContextualFragment(innerT.innerHTML);
|
|
2854
|
+
innerFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2855
|
+
try { e2.textContent = eval(e2.dataset.expr); } catch(ex) {}
|
|
2856
|
+
});
|
|
2857
|
+
innerT.parentNode.insertBefore(innerFrag, innerT);
|
|
2858
|
+
} else {
|
|
2859
|
+
var innerElse = innerT.nextElementSibling;
|
|
2860
|
+
if (innerElse && innerElse.hasAttribute('data-sinth-else')) {
|
|
2861
|
+
var innerElseFrag = document.createRange().createContextualFragment(innerElse.innerHTML);
|
|
2862
|
+
innerElseFrag.querySelectorAll('.sinth-expr').forEach(function(e2) {
|
|
2863
|
+
try { e2.textContent = eval(e2.dataset.expr); } catch(ex) {}
|
|
2864
|
+
});
|
|
2865
|
+
innerT.parentNode.insertBefore(innerElseFrag, innerT);
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
});
|
|
2812
2869
|
${hasDelay ? `
|
|
2813
2870
|
// Handle delays for this mixed block
|
|
2814
2871
|
__el.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
@@ -2832,7 +2889,17 @@ else el.style.display = '';
|
|
|
2832
2889
|
}
|
|
2833
2890
|
}
|
|
2834
2891
|
|
|
2835
|
-
//
|
|
2892
|
+
// set input vals from bound vars
|
|
2893
|
+
renderBody += `
|
|
2894
|
+
document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
2895
|
+
try { el.value = eval(el.dataset.sinthValue) || ''; } catch(e) {}
|
|
2896
|
+
});
|
|
2897
|
+
document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
2898
|
+
try { el.checked = !!eval(el.dataset.sinthChecked); } catch(e) {}
|
|
2899
|
+
});
|
|
2900
|
+
`;
|
|
2901
|
+
|
|
2902
|
+
// expression spans
|
|
2836
2903
|
if (needsExpr) {
|
|
2837
2904
|
renderBody += `
|
|
2838
2905
|
document.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
@@ -2841,10 +2908,10 @@ else el.style.display = '';
|
|
|
2841
2908
|
`;
|
|
2842
2909
|
}
|
|
2843
2910
|
|
|
2844
|
-
|
|
2911
|
+
|
|
2845
2912
|
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|
|
2846
2913
|
|
|
2847
|
-
|
|
2914
|
+
|
|
2848
2915
|
const delayBlock = needsDelay ? `
|
|
2849
2916
|
setTimeout(function() {
|
|
2850
2917
|
document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
|
|
@@ -2862,15 +2929,23 @@ setTimeout(function() {
|
|
|
2862
2929
|
}, 0);
|
|
2863
2930
|
` : "";
|
|
2864
2931
|
|
|
2865
|
-
return `// Sinth
|
|
2932
|
+
return `// Sinth compiled runtime
|
|
2866
2933
|
${varLines}
|
|
2934
|
+
${needsFor ? `function hashString(str) {
|
|
2935
|
+
var hash = 0, i, chr;
|
|
2936
|
+
for (i = 0; i < str.length; i++) {
|
|
2937
|
+
chr = str.charCodeAt(i);
|
|
2938
|
+
hash = ((hash << 5) - hash) + chr;
|
|
2939
|
+
hash |= 0;
|
|
2940
|
+
}
|
|
2941
|
+
return String(hash);
|
|
2942
|
+
}
|
|
2943
|
+
` : ""}
|
|
2867
2944
|
${needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : ""}
|
|
2868
2945
|
${delayBlock}`;
|
|
2869
2946
|
}
|
|
2870
2947
|
|
|
2871
|
-
//
|
|
2872
|
-
// §14 MAIN COMPILATION PIPELINE
|
|
2873
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
2948
|
+
// main compilation pipeline
|
|
2874
2949
|
|
|
2875
2950
|
interface CompileOptions {
|
|
2876
2951
|
projectRoot: string;
|
|
@@ -2888,7 +2963,6 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2888
2963
|
const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
|
|
2889
2964
|
const hash = fnv1a(absPath);
|
|
2890
2965
|
|
|
2891
|
-
// All var declarations are already collected via shared _varDecls reference
|
|
2892
2966
|
const allVarDecls: VarDeclaration[] = file.varDecls;
|
|
2893
2967
|
|
|
2894
2968
|
const ctx: CompileCtx = {
|
|
@@ -2899,6 +2973,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2899
2973
|
mixedBlocks: [],
|
|
2900
2974
|
mixedCounter: 0,
|
|
2901
2975
|
logicBlocks: [],
|
|
2976
|
+
ifIdCounter: 0,
|
|
2902
2977
|
};
|
|
2903
2978
|
|
|
2904
2979
|
if (!file.isPage) {
|
|
@@ -2909,29 +2984,24 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2909
2984
|
}
|
|
2910
2985
|
|
|
2911
2986
|
const headData = buildHeadData(file.meta);
|
|
2912
|
-
|
|
2913
|
-
// Render body HTML (populates ctx.extraCSS, ctx.mixedBlocks, ctx.logicBlocks)
|
|
2914
2987
|
const bodyHTML = file.uses.map(u => renderCompUse(u, ctx, new Map(), 0)).join("\n");
|
|
2915
|
-
|
|
2916
2988
|
const pageCSS = file.styles.map(s => processStyleBlock(s, hash, new Map())).join("\n");
|
|
2917
2989
|
const scopedCSS = [pageCSS, ...ctx.extraCSS].filter(c => c.trim()).join("\n");
|
|
2918
|
-
|
|
2919
2990
|
const { componentScripts, pageScripts } = collectScripts(file, allDefs);
|
|
2920
2991
|
|
|
2921
|
-
//
|
|
2992
|
+
// collect all assigned variables for default-value warnings
|
|
2922
2993
|
const assignedVars = new Set<string>();
|
|
2923
2994
|
for (const s of file.scripts) {
|
|
2924
2995
|
for (const m of s.raw.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?:[+\-]?=)/g)) assignedVars.add(m[1]);
|
|
2925
2996
|
}
|
|
2926
2997
|
for (const v of file.varDecls) { if (v.value) assignedVars.add(v.name); }
|
|
2927
2998
|
|
|
2928
|
-
//
|
|
2999
|
+
// companion JS file (needs same name as .sinth, adds support for JS libraries)
|
|
2929
3000
|
const companionJS = (() => {
|
|
2930
3001
|
const base = absPath.replace(/\.sinth$/, ".js");
|
|
2931
3002
|
return fs.existsSync(base) ? path.basename(base) : undefined;
|
|
2932
3003
|
})();
|
|
2933
3004
|
|
|
2934
|
-
// Make CSS / JS links relative to the HTML output directory
|
|
2935
3005
|
const relativeCssLinks = cssLinks.map(css => {
|
|
2936
3006
|
const rel = path.relative(path.dirname(absPath), css).replace(/\\/g, "/");
|
|
2937
3007
|
return rel.startsWith(".") ? rel : "./" + rel;
|
|
@@ -2946,7 +3016,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2946
3016
|
|
|
2947
3017
|
const head = renderHead(headData, relativeCssLinks, relativeJsLinks, scopedCSS, companionJS);
|
|
2948
3018
|
|
|
2949
|
-
//
|
|
3019
|
+
// build tree-shaken runtime
|
|
2950
3020
|
const runtimeJS = buildRuntime({
|
|
2951
3021
|
varDecls: allVarDecls,
|
|
2952
3022
|
bodyHTML,
|
|
@@ -2955,13 +3025,11 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2955
3025
|
assignedVars,
|
|
2956
3026
|
});
|
|
2957
3027
|
|
|
2958
|
-
// Component-level script IIFEs
|
|
2959
3028
|
const scriptTags: string[] = [];
|
|
2960
3029
|
if (componentScripts.length > 0) {
|
|
2961
3030
|
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
2962
3031
|
}
|
|
2963
3032
|
|
|
2964
|
-
// Page-level script blocks
|
|
2965
3033
|
for (const s of pageScripts) {
|
|
2966
3034
|
const extra = Object.entries(s.attrs)
|
|
2967
3035
|
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
@@ -2989,9 +3057,7 @@ function minifyHTML(html: string): string {
|
|
|
2989
3057
|
return html.replace(/>\s+</g, "><").replace(/\n\s*\n/g, "\n").trim();
|
|
2990
3058
|
}
|
|
2991
3059
|
|
|
2992
|
-
//
|
|
2993
|
-
// §15 FILE DISCOVERY & ASSET COPY
|
|
2994
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3060
|
+
// file discovery & asset copy
|
|
2995
3061
|
|
|
2996
3062
|
function findSinthPages(dir: string, outDir?: string): string[] {
|
|
2997
3063
|
const results: string[] = [];
|
|
@@ -3016,9 +3082,8 @@ function copyDir(src: string, dest: string): void {
|
|
|
3016
3082
|
}
|
|
3017
3083
|
}
|
|
3018
3084
|
|
|
3019
|
-
|
|
3020
|
-
//
|
|
3021
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3085
|
+
|
|
3086
|
+
// dev server
|
|
3022
3087
|
|
|
3023
3088
|
const LIVE_RELOAD_SCRIPT = `<script>
|
|
3024
3089
|
(function(){
|
|
@@ -3133,9 +3198,8 @@ async function startDevServer(opts: CompileOptions & { port: number; files?: str
|
|
|
3133
3198
|
});
|
|
3134
3199
|
}
|
|
3135
3200
|
|
|
3136
|
-
|
|
3137
|
-
//
|
|
3138
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3201
|
+
|
|
3202
|
+
// cli entry point
|
|
3139
3203
|
|
|
3140
3204
|
function loadConfig(root: string): Record<string, unknown> {
|
|
3141
3205
|
const cfgPath = path.join(root, "sinth.config.json");
|
|
@@ -3195,14 +3259,13 @@ async function main(): Promise<void> {
|
|
|
3195
3259
|
}
|
|
3196
3260
|
}
|
|
3197
3261
|
|
|
3198
|
-
//
|
|
3262
|
+
// copies to output
|
|
3199
3263
|
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
3200
3264
|
if (fs.existsSync(assetsIn)) {
|
|
3201
3265
|
copyDir(assetsIn, assetsOut);
|
|
3202
3266
|
process.stdout.write(` \x1b[32m✓\x1b[0m assets/ → ${path.relative(cwd, assetsOut)}/\n`);
|
|
3203
3267
|
}
|
|
3204
3268
|
|
|
3205
|
-
// Copy libraries/ (without .sinth / .html files)
|
|
3206
3269
|
const libIn = path.join(cwd, "libraries"), libOut = path.join(outDir, "libraries");
|
|
3207
3270
|
if (fs.existsSync(libIn)) {
|
|
3208
3271
|
copyDir(libIn, libOut);
|
|
@@ -3298,9 +3361,7 @@ async function main(): Promise<void> {
|
|
|
3298
3361
|
}
|
|
3299
3362
|
}
|
|
3300
3363
|
|
|
3301
|
-
//
|
|
3302
|
-
// §18 PROJECT SCAFFOLDING
|
|
3303
|
-
// ═══════════════════════════════════════════════════════════════════════════
|
|
3364
|
+
// project scaffholding
|
|
3304
3365
|
|
|
3305
3366
|
function scaffoldProject(root: string): void {
|
|
3306
3367
|
for (const d of ["pages", "components", "styles", "libraries", "assets"]) {
|
|
@@ -3452,7 +3513,9 @@ component Card(title, color = "blue") {
|
|
|
3452
3513
|
`);
|
|
3453
3514
|
}
|
|
3454
3515
|
|
|
3455
|
-
|
|
3516
|
+
|
|
3517
|
+
|
|
3518
|
+
|
|
3456
3519
|
main().catch(e => {
|
|
3457
3520
|
process.stderr.write(`\x1b[31m${(e as Error).message}\x1b[0m\n`);
|
|
3458
3521
|
process.exit(1);
|