@yannosay/sinth 0.8.1 → 0.10.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.
@@ -0,0 +1,1078 @@
1
+ import { TT, Token, Loc, Literal, Expression, Attr, Child, CompUse, IfBlock, ForLoop, RemoveStmt, ParamDecl, StyleBlock, ScriptBlock, CompDef, CustomElDecl, VarDeclaration, ImportNode, MetaEntry, FunctionDef, SinthFile, SinthError, SinthWarning, AssignOp, VarType, BinaryOp } from "./types.ts";
2
+ import { Lexer } from "./lexer.ts";
3
+ import { compileExprToJS, compileIfToJS } from "./expr.ts";
4
+ import { tagNameToPascal } from "../utils.ts";
5
+
6
+
7
+
8
+ export class Parser {
9
+ private pos = 0;
10
+ private loopVar: string | null = null;
11
+ private _varDecls: VarDeclaration[] = [];
12
+
13
+ constructor(private tokens: Token[], private file: string) {}
14
+
15
+
16
+ parse(): SinthFile {
17
+ let isPage = false;
18
+ const imports: ImportNode[] = [];
19
+ const meta: MetaEntry[] = [];
20
+ const defs: CompDef[] = [];
21
+ const functions: FunctionDef[] = [];
22
+ const uses: CompUse[] = [];
23
+ const styles: StyleBlock[] = [];
24
+ const scripts: ScriptBlock[] = [];
25
+ const customEls: CustomElDecl[] = [];
26
+ const varDecls: VarDeclaration[] = [];
27
+ this._varDecls = varDecls;
28
+
29
+ if (this.check(TT.KW_PAGE)) { this.consume(TT.KW_PAGE); isPage = true; }
30
+
31
+ while (this.check(TT.KW_IMPORT)) imports.push(this.parseImport());
32
+
33
+ while (this.isMetaStart()) {
34
+ const entry = this.parseMeta();
35
+ meta.push(entry);
36
+ if (entry.key === "title") isPage = true;
37
+ }
38
+
39
+
40
+ while (this.check(TT.IDENT) && this.peek().value === "var") {
41
+ const vd = this.parseVarDeclaration();
42
+ if (vd) { varDecls.push(vd); }
43
+ }
44
+
45
+
46
+ while (!this.check(TT.EOF)) {
47
+ if (this.check(TT.KW_COMPONENT)) { defs.push(this.parseCompDef()); }
48
+ else if (this.check(TT.KW_FUNCTION)) { functions.push(this.parseFunctionDef()); } // ← new
49
+ else if (this.check(TT.KW_STYLE)) { styles.push(this.parseStyleBlock()); }
50
+ else if (this.check(TT.KW_SCRIPT)) { scripts.push(this.parseScriptBlock()); }
51
+ else if (this.check(TT.KW_CUSTOM_EL) || this.check(TT.KW_CUSTOM)) { customEls.push(this.parseCustomEl()); }
52
+ else if (this.check(TT.IDENT) && this.peek().value === "var") {
53
+ const vd = this.parseVarDeclaration();
54
+ if (vd) varDecls.push(vd);
55
+ }
56
+ else if (this.check(TT.KW_IF)) {
57
+ const ifNode = this.parseIfBlock();
58
+ uses.push(...this.flattenIfToUses(ifNode, scripts));
59
+ }
60
+ else if (this.check(TT.KW_FOR)) {
61
+ const forNode = this.parseForLoop();
62
+ uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [forNode], loc: forNode.loc });
63
+ }
64
+ else if (this.check(TT.IDENT)) {
65
+ const savedPos = this.pos;
66
+ const identName = this.peek().value;
67
+ const nextType = this.tokens[this.pos + 1]?.type;
68
+
69
+ // detect accidental top-level expressions (guess == target" instead of "if guess == target")
70
+ const isComparison = [TT.OP_EQEQ, TT.OP_NEQ, TT.OP_LT, TT.OP_GT, TT.OP_LTEQ, TT.OP_GTEQ].includes(nextType);
71
+ const isLogicAndOr = nextType === TT.IDENT && (this.tokens[this.pos + 1]?.value === "and" || this.tokens[this.pos + 1]?.value === "or");
72
+
73
+ if (isComparison || isLogicAndOr) {
74
+ throw new SinthError(
75
+ `Unexpected expression '${identName} ...' at top level. Did you mean to wrap this in an 'if' block?`,
76
+ this.peek().loc,
77
+ );
78
+ }
79
+
80
+ // top-level assignment: ident = expr
81
+ if (nextType === TT.EQUALS ||
82
+ (nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) ||
83
+ (nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS)) {
84
+ this.consume(TT.IDENT); // consume name
85
+ let op: AssignOp = "=";
86
+ if (this.check(TT.OP_PLUS)) { this.consume(TT.OP_PLUS); op = "+="; }
87
+ else if (this.check(TT.OP_MINUS)) { this.consume(TT.OP_MINUS); op = "-="; }
88
+ this.consume(TT.EQUALS);
89
+ const expr = this.parseExpression();
90
+ if (expr) {
91
+ const js = `${identName} ${op} ${compileExprToJS(expr)};
92
+ sinthRender();`;
93
+ scripts.push({ raw: js, attrs: {}, loc: this.peek().loc });
94
+ }
95
+ continue;
96
+ }
97
+
98
+ // component usage
99
+ this.pos = savedPos;
100
+ uses.push(this.parseCompUse());
101
+ }
102
+ else throw new SinthError(
103
+ `Unexpected token '${this.peek().value}' (${TT[this.peek().type]}) at top level`,
104
+ this.peek().loc,
105
+ );
106
+ }
107
+
108
+ return { filePath: this.file, isPage, imports, meta, defs, functions, uses, styles, scripts, customEls, varDecls };
109
+ }
110
+
111
+ private flattenIfToUses(ifNode: IfBlock, scripts: ScriptBlock[]): CompUse[] {
112
+ const bodyHasComp = ifNode.body.some(c => c.kind === "use");
113
+ const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
114
+ if (!bodyHasComp && !elseHasComp) {
115
+ scripts.push({ raw: compileIfToJS(ifNode), attrs: {}, loc: ifNode.loc });
116
+ return [];
117
+ }
118
+ const syntheticUse: CompUse = {
119
+ kind: "use",
120
+ name: "__IF_ROOT__",
121
+ attrs: [],
122
+ children: [ifNode],
123
+ loc: ifNode.loc,
124
+ };
125
+ return [syntheticUse];
126
+ }
127
+
128
+ // imports
129
+
130
+ private parseImport(): ImportNode {
131
+ const loc = this.consume(TT.KW_IMPORT).loc;
132
+ if (this.check(TT.KW_CSS)) {
133
+ this.consume(TT.KW_CSS);
134
+ return { kind: "css", path: this.consume(TT.STRING).value, loc };
135
+ }
136
+ if (this.check(TT.STRING)) {
137
+ return { kind: "sinth", path: this.consume(TT.STRING).value, loc };
138
+ }
139
+ if (this.check(TT.IDENT)) {
140
+ const name = this.consume(TT.IDENT).value;
141
+ let alias: string | undefined;
142
+ if (this.check(TT.KW_AS)) { this.consume(TT.KW_AS); alias = this.consume(TT.IDENT).value; }
143
+ return { kind: "js", name, alias, loc };
144
+ }
145
+ throw new SinthError(`Expected import path, library name, or 'css'`, loc);
146
+ }
147
+
148
+ // metadata
149
+
150
+ private isMetaStart(): boolean {
151
+ if (!this.check(TT.IDENT)) return false;
152
+ return this.tokens[this.pos + 1]?.type === TT.EQUALS;
153
+ }
154
+
155
+ private parseMeta(): MetaEntry {
156
+ const loc = this.peek().loc;
157
+ const key = this.consume(TT.IDENT).value;
158
+ this.consume(TT.EQUALS);
159
+ return { key, value: this.parseLiteral(), loc };
160
+ }
161
+
162
+ // var declarations
163
+
164
+ private parseVarDeclaration(): VarDeclaration | null {
165
+ const loc = this.peek().loc;
166
+ this.consume(TT.IDENT); // consume 'var'
167
+
168
+ const typeTok = this.consume(TT.IDENT);
169
+ let typeStr = typeTok.value;
170
+ if (this.check(TT.LBRACKET) && this.tokens[this.pos + 1]?.type === TT.RBRACKET) {
171
+ this.consume(TT.LBRACKET); this.consume(TT.RBRACKET);
172
+ typeStr = "str[]";
173
+ }
174
+ if (!["int", "str", "bool", "str[]", "obj"].includes(typeStr)) {
175
+ throw new SinthError(`Unknown type '${typeStr}'. Expected: int, str, bool, str[], obj`, typeTok.loc);
176
+ }
177
+
178
+ const nameTok = this.consume(TT.IDENT);
179
+ let val: Literal | null = null;
180
+
181
+ if (this.check(TT.EQUALS)) {
182
+ this.consume(TT.EQUALS);
183
+ if (this.check(TT.LBRACKET)) {
184
+ val = this.parseArrayLiteral();
185
+ } else if (this.check(TT.IDENT)) {
186
+ const refName = this.consume(TT.IDENT).value;
187
+ val = { kind: "str", value: `__VAR__${refName}` };
188
+ } else {
189
+ val = this.parseLiteral();
190
+ }
191
+ }
192
+
193
+ return { kind: "var", name: nameTok.value, varType: typeStr as VarType, value: val, loc };
194
+ }
195
+
196
+ private parseArrayLiteral(): Literal {
197
+ this.consume(TT.LBRACKET);
198
+ const items: any[] = [];
199
+ while (!this.check(TT.RBRACKET) && !this.check(TT.EOF)) {
200
+ if (this.check(TT.LBRACE)) {
201
+ items.push(this.parseObjectLiteral());
202
+ } else if (this.check(TT.STRING)) {
203
+ items.push(this.consume(TT.STRING).value);
204
+ } else if (this.check(TT.NUMBER)) {
205
+ items.push(parseFloat(this.consume(TT.NUMBER).value));
206
+ } else if (this.check(TT.BOOL_TRUE)) {
207
+ this.consume(TT.BOOL_TRUE);
208
+ items.push(true);
209
+ } else if (this.check(TT.BOOL_FALSE)) {
210
+ this.consume(TT.BOOL_FALSE);
211
+ items.push(false);
212
+ } else if (this.check(TT.NULL_LIT)) {
213
+ this.consume(TT.NULL_LIT);
214
+ items.push(null);
215
+ }
216
+ if (this.check(TT.COMMA)) this.consume(TT.COMMA); else break;
217
+ }
218
+ this.consume(TT.RBRACKET);
219
+ return { kind: "str", value: `__ARR__${JSON.stringify(items)}` };
220
+ }
221
+
222
+ private parseObjectLiteral(): Record<string, any> {
223
+ this.consume(TT.LBRACE);
224
+ const obj: Record<string, any> = {};
225
+ while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
226
+ const key = this.consume(TT.IDENT).value;
227
+ this.consume(TT.COLON);
228
+ if (this.check(TT.STRING)) {
229
+ obj[key] = this.consume(TT.STRING).value;
230
+ } else if (this.check(TT.NUMBER)) {
231
+ obj[key] = parseFloat(this.consume(TT.NUMBER).value);
232
+ } else if (this.check(TT.BOOL_TRUE)) {
233
+ this.consume(TT.BOOL_TRUE);
234
+ obj[key] = true;
235
+ } else if (this.check(TT.BOOL_FALSE)) {
236
+ this.consume(TT.BOOL_FALSE);
237
+ obj[key] = false;
238
+ } else if (this.check(TT.NULL_LIT)) {
239
+ this.consume(TT.NULL_LIT);
240
+ obj[key] = null;
241
+ } else {
242
+ throw new SinthError(`Expected literal value for key '${key}'`, this.peek().loc);
243
+ }
244
+ if (this.check(TT.COMMA)) this.consume(TT.COMMA);
245
+ }
246
+ this.consume(TT.RBRACE);
247
+ return obj;
248
+ }
249
+
250
+ // expression parsing
251
+
252
+ parseExpression(): Expression | null {
253
+ const tok = this.peek();
254
+
255
+ // Unary 'not'
256
+ if (tok.type === TT.IDENT && tok.value === "not") {
257
+ this.consume(TT.IDENT);
258
+ const operand = this.parseExpression();
259
+ if (!operand) throw new SinthError("Expected expression after 'not'", tok.loc);
260
+ const unaryExpr: Expression = { kind: "unary", op: "not", operand };
261
+ return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(unaryExpr)));
262
+ }
263
+
264
+ if (this.check(TT.LPAREN)) {
265
+ this.consume(TT.LPAREN);
266
+ const inner = this.parseExpression();
267
+ if (!inner) throw new SinthError("Expected expression after '('", this.peek().loc);
268
+ if (!this.check(TT.RPAREN)) throw new SinthError("Expected closing ')'", this.peek().loc);
269
+ this.consume(TT.RPAREN);
270
+ return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(inner)));
271
+ }
272
+
273
+ // literal
274
+ if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) ||
275
+ this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
276
+ const lit = this.parseLiteral();
277
+ const base: Expression = { kind: "literal", value: lit };
278
+ return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
279
+ }
280
+
281
+ // var or assignment
282
+ if (tok.type === TT.IDENT) {
283
+ const name = this.consume(TT.IDENT).value;
284
+ let varExpr: Expression;
285
+
286
+ // dot notation -> user.age
287
+ if (this.check(TT.DOT)) {
288
+ this.consume(TT.DOT);
289
+ const propName = this.consume(TT.IDENT).value;
290
+ const fullName = `${name}.${propName}`;
291
+ varExpr = { kind: "variable", name: fullName };
292
+ } else {
293
+ varExpr = { kind: "variable", name };
294
+ }
295
+
296
+ // apply bracket postfixes (e.g. data[key], data["x"])
297
+ const indexedExpr = this.parsePostfix(varExpr);
298
+
299
+ // if indexing was applied, we don't allow assignment, just binary RHS (after possible call)
300
+ if (indexedExpr !== varExpr) {
301
+ return this.parseBinaryRHS(this.parseCallExpr(indexedExpr));
302
+ }
303
+
304
+ // no indexing – check for assignment operators
305
+ const nextType = this.tokens[this.pos]?.type;
306
+ if (nextType === TT.OP_PLUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
307
+ this.consume(TT.OP_PLUS); this.consume(TT.EQUALS);
308
+ const rhs = this.parseExpression();
309
+ if (!rhs) throw new SinthError("Expected expression after +=", this.peek().loc);
310
+ return { kind: "assign", target: varExpr.name!, op: "+=", right: rhs };
311
+ }
312
+ if (nextType === TT.OP_MINUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
313
+ this.consume(TT.OP_MINUS); this.consume(TT.EQUALS);
314
+ const rhs = this.parseExpression();
315
+ if (!rhs) throw new SinthError("Expected expression after -=", this.peek().loc);
316
+ return { kind: "assign", target: varExpr.name!, op: "-=", right: rhs };
317
+ }
318
+ if (nextType === TT.EQUALS) {
319
+ this.consume(TT.EQUALS);
320
+ const rhs = this.parseExpression();
321
+ if (!rhs) throw new SinthError("Expected expression after =", this.peek().loc);
322
+ return { kind: "assign", target: varExpr.name!, op: "=", right: rhs };
323
+ }
324
+
325
+ return this.parseBinaryRHS(this.parseCallExpr(varExpr));
326
+ }
327
+
328
+ return null;
329
+ }
330
+
331
+ private parseBinaryRHS(left: Expression): Expression {
332
+ const t = this.peek().type;
333
+ let op: BinaryOp | null = null;
334
+ if (t === TT.OP_EQEQ) op = "==";
335
+ else if (t === TT.OP_NEQ) op = "!=";
336
+ else if (t === TT.OP_LT) op = "<";
337
+ else if (t === TT.OP_GT) op = ">";
338
+ else if (t === TT.OP_LTEQ) op = "<=";
339
+ else if (t === TT.OP_GTEQ) op = ">=";
340
+ else if (t === TT.OP_PLUS) op = "+";
341
+ else if (t === TT.OP_MINUS) op = "-";
342
+ else if (t === TT.OP_STAR) op = "*";
343
+ else if (t === TT.OP_SLASH) op = "/";
344
+ else if (t === TT.IDENT && this.peek().value === "and") op = "and";
345
+ else if (t === TT.IDENT && this.peek().value === "or") op = "or";
346
+
347
+ if (!op) return left;
348
+ this.pos++;
349
+
350
+ const right = this.parseExpression();
351
+ if (!right) throw new SinthError(`Expected expression after '${op}'`, this.peek().loc);
352
+ return { kind: "binary", left, op, right };
353
+ }
354
+
355
+ private parsePostfix(base: Expression): Expression {
356
+ let expr = base;
357
+ while (this.check(TT.LBRACKET)) {
358
+ this.consume(TT.LBRACKET);
359
+ const key = this.parseExpression();
360
+ if (!key) throw new SinthError("Expected expression inside [...]", this.peek().loc);
361
+ this.consume(TT.RBRACKET);
362
+ expr = { kind: "index", object: expr, key };
363
+ }
364
+ return expr;
365
+ }
366
+
367
+ private parseCallExpr(expr: Expression): Expression {
368
+ while (this.check(TT.LPAREN)) {
369
+ this.consume(TT.LPAREN);
370
+ const args: Expression[] = [];
371
+ if (!this.check(TT.RPAREN)) {
372
+ args.push(this.parseExpression()!);
373
+ while (this.check(TT.COMMA)) {
374
+ this.consume(TT.COMMA);
375
+ args.push(this.parseExpression()!);
376
+ }
377
+ }
378
+ if (!this.check(TT.RPAREN)) throw new SinthError("Expected ')' after arguments", this.peek().loc);
379
+ this.consume(TT.RPAREN);
380
+ expr = { kind: "call", callee: expr, args };
381
+ }
382
+ return expr;
383
+ }
384
+
385
+
386
+ // component definitions
387
+
388
+ private parseFunctionDef(): FunctionDef {
389
+ const loc = this.consume(TT.KW_FUNCTION).loc;
390
+ const name = this.consume(TT.IDENT).value;
391
+ const params = this.parseParamDecls();
392
+ let returnType: VarType | undefined;
393
+ if (this.check(TT.OP_ARROW)) {
394
+ this.consume(TT.OP_ARROW);
395
+ const typeTok = this.consume(TT.IDENT);
396
+ if (!["int","str","bool","str[]","obj"].includes(typeTok.value)) {
397
+ throw new SinthError(`Unknown return type '${typeTok.value}'. Expected: int, str, bool, str[], obj`, typeTok.loc);
398
+ }
399
+ returnType = typeTok.value as VarType;
400
+ }
401
+ if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after function signature", this.peek().loc);
402
+ this.consume(TT.LBRACE);
403
+ const body = this.parseChildList();
404
+ this.consume(TT.RBRACE);
405
+ return { name, params, returnType, body, loc };
406
+ }
407
+
408
+ private parseCompDef(): CompDef {
409
+ const loc = this.consume(TT.KW_COMPONENT).loc;
410
+ const name = this.consume(TT.IDENT).value;
411
+ const params = this.parseParamDecls();
412
+ const rawTok = this.consume(TT.RAW_BLOCK);
413
+ const { body, styles, scripts } = this.parseCompBody(rawTok.value, rawTok.loc);
414
+ return { name, params, body, styles, scripts, loc };
415
+ }
416
+
417
+ private parseCustomEl(): CustomElDecl {
418
+ const loc = this.peek().loc;
419
+ if (this.check(TT.KW_CUSTOM_EL)) this.consume(TT.KW_CUSTOM_EL);
420
+ else if (this.check(TT.KW_CUSTOM)) this.consume(TT.KW_CUSTOM);
421
+ const tagName = this.consume(TT.IDENT).value;
422
+ const sinthName = tagNameToPascal(tagName);
423
+ const params = this.parseParamDecls();
424
+ return { sinthName, tagName, params, loc };
425
+ }
426
+
427
+ private parseParamDecls(): ParamDecl[] {
428
+ const params: ParamDecl[] = [];
429
+ if (!this.check(TT.LPAREN)) return params;
430
+ this.consume(TT.LPAREN);
431
+ while (!this.check(TT.RPAREN)) {
432
+ const ploc = this.peek().loc;
433
+ let pname: string;
434
+ if (this.check(TT.IDENT) && ["int","str","bool","str[]","obj"].includes(this.peek().value) &&
435
+ this.tokens[this.pos + 1]?.type === TT.IDENT) {
436
+ this.consume(TT.IDENT); // consume type
437
+ pname = this.consume(TT.IDENT).value;
438
+ } else {
439
+ const nameTok = this.check(TT.IDENT) ? this.consume(TT.IDENT) : this.consume(this.peek().type);
440
+ pname = nameTok.value;
441
+ }
442
+ let defaultVal: Literal | undefined;
443
+ if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); defaultVal = this.parseLiteral(); }
444
+ params.push({ name: pname, defaultVal, loc: ploc });
445
+ if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
446
+ }
447
+ this.consume(TT.RPAREN);
448
+ return params;
449
+ }
450
+
451
+ private parseCompBody(
452
+ raw: string,
453
+ loc: Loc,
454
+ ): { body: Child[]; styles: StyleBlock[]; scripts: ScriptBlock[] } {
455
+ const tokens = new Lexer(raw, loc.file).tokenize();
456
+ const sub = new Parser(tokens, loc.file);
457
+ sub._varDecls = this._varDecls;
458
+ return sub.parseBodyContents();
459
+ }
460
+
461
+ private parseBodyContents(): { body: Child[]; styles: StyleBlock[]; scripts: ScriptBlock[] } {
462
+ const body: Child[] = [], styles: StyleBlock[] = [], scripts: ScriptBlock[] = [];
463
+ while (!this.check(TT.EOF)) {
464
+ if (this.check(TT.KW_STYLE)) { styles.push(this.parseStyleBlock()); }
465
+ else if (this.check(TT.KW_SCRIPT)) { scripts.push(this.parseScriptBlock()); }
466
+ else if (this.check(TT.STRING)) {
467
+ const loc = this.peek().loc;
468
+ body.push({ kind: "text", value: this.consume(TT.STRING).value, loc });
469
+ }
470
+ else if (this.check(TT.IDENT) && this.peek().value === "var") {
471
+ const vd = this.parseVarDeclaration();
472
+ if (vd) this._varDecls.push(vd);
473
+ continue;
474
+ }
475
+ else if (this.check(TT.KW_IF)) { body.push(this.parseIfBlock()); }
476
+ else if (this.check(TT.KW_FOR)) { body.push(this.parseForLoop()); }
477
+ else if (this.check(TT.IDENT)) { body.push(this.parseCompUse()); }
478
+ else throw new SinthError(
479
+ `Unexpected token '${this.peek().value}' in component body`,
480
+ this.peek().loc,
481
+ );
482
+ }
483
+ return { body, styles, scripts };
484
+ }
485
+
486
+ // if / else blocks
487
+
488
+ private parseIfBlock(): IfBlock {
489
+ const loc = this.consume(TT.KW_IF).loc;
490
+ const condition = this.parseExpression();
491
+ if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
492
+
493
+ if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after if condition", this.peek().loc);
494
+ this.consume(TT.LBRACE);
495
+ const body = this.parseUnifiedBody();
496
+ this.consume(TT.RBRACE);
497
+
498
+ let elseBody: Child[] | undefined;
499
+ if (this.check(TT.KW_ELSE)) {
500
+ this.consume(TT.KW_ELSE);
501
+ if (this.check(TT.KW_IF)) {
502
+ elseBody = [this.parseIfBlock()];
503
+ } else {
504
+ if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
505
+ this.consume(TT.LBRACE);
506
+ elseBody = this.parseUnifiedBody();
507
+ this.consume(TT.RBRACE);
508
+ }
509
+ }
510
+
511
+ return { kind: "if", condition, body, elseBody, loc };
512
+ }
513
+
514
+ private parseUnifiedBody(): Child[] {
515
+ const children: Child[] = [];
516
+
517
+ while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
518
+ if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
519
+ if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
520
+ if (this.check(TT.LPAREN)) {
521
+ const loc = this.peek().loc;
522
+ const expr = this.parseExpression();
523
+ if (expr) {
524
+ children.push({ kind: "expr", expression: expr, loc });
525
+ }
526
+ continue;
527
+ }
528
+ if (this.check(TT.STRING)) {
529
+ const loc = this.peek().loc;
530
+ children.push({ kind: "text", value: this.consume(TT.STRING).value, loc });
531
+ continue;
532
+ }
533
+
534
+ if (this.check(TT.KW_REMOVE)) {
535
+ const loc = this.consume(TT.KW_REMOVE).loc;
536
+ if (this.check(TT.STRING)) {
537
+ const target = this.consume(TT.STRING).value;
538
+ children.push({ kind: "remove", target, loc });
539
+ } else {
540
+ throw new SinthError(
541
+ `Expected a string after 'remove', got '${this.peek().value}'. Use remove "id".`,
542
+ this.peek().loc,
543
+ );
544
+ }
545
+ continue;
546
+ }
547
+ if (this.check(TT.IDENT) && this.peek().value === "var") {
548
+ const vd = this.parseVarDeclaration();
549
+ if (vd) this._varDecls.push(vd);
550
+ continue;
551
+ }
552
+
553
+ if (this.check(TT.IDENT)) {
554
+ const loc = this.peek().loc;
555
+ const name = this.peek().value;
556
+ const nextType = this.tokens[this.pos + 1]?.type;
557
+ const nextNextType = this.tokens[this.pos + 2]?.type;
558
+
559
+ if (nextType === TT.DOT && (nextNextType === TT.EQUALS ||
560
+ (nextNextType === TT.OP_PLUS && this.tokens[this.pos + 3]?.type === TT.EQUALS) ||
561
+ (nextNextType === TT.OP_MINUS && this.tokens[this.pos + 3]?.type === TT.EQUALS))) {
562
+ let fullName = this.consume(TT.IDENT).value;
563
+ this.consume(TT.DOT);
564
+ fullName = fullName + "." + this.consume(TT.IDENT).value;
565
+
566
+ let op: AssignOp = "=";
567
+ const afterDotType = this.tokens[this.pos]?.type;
568
+ if (afterDotType === TT.OP_PLUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
569
+ this.consume(TT.OP_PLUS); op = "+=";
570
+ } else if (afterDotType === TT.OP_MINUS && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
571
+ this.consume(TT.OP_MINUS); op = "-=";
572
+ }
573
+ this.consume(TT.EQUALS);
574
+ const rhs = this.parseExpression();
575
+ if (!rhs) throw new SinthError("Expected expression after assignment", this.peek().loc);
576
+ const assignExpr: Expression = { kind: "assign", target: fullName, op, right: rhs };
577
+ children.push({ kind: "assign_stmt", expression: assignExpr, loc });
578
+ continue;
579
+ }
580
+
581
+ // assignment: ident = expr | ident += expr | ident -= expr
582
+ const isSimpleAssign = nextType === TT.EQUALS;
583
+ const isPlusAssign = nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
584
+ const isMinusAssign = nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS;
585
+
586
+ if (isSimpleAssign || isPlusAssign || isMinusAssign) {
587
+ this.consume(TT.IDENT); // consume name
588
+ let op: AssignOp = "=";
589
+ if (isPlusAssign) { this.consume(TT.OP_PLUS); op = "+="; }
590
+ else if (isMinusAssign) { this.consume(TT.OP_MINUS); op = "-="; }
591
+ this.consume(TT.EQUALS);
592
+ const rhs = this.parseExpression();
593
+ if (!rhs) throw new SinthError("Expected expression after assignment", this.peek().loc);
594
+ const assignExpr: Expression = { kind: "assign", target: name, op, right: rhs };
595
+ children.push({ kind: "assign_stmt", expression: assignExpr, loc });
596
+ continue;
597
+ }
598
+
599
+ // Component usage: ident( or ident{ or ident RAW_BLOCK
600
+ if (nextType === TT.LPAREN || nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
601
+ children.push(this.parseCompUse());
602
+ continue;
603
+ }
604
+
605
+ // var expression
606
+ // check dot notation variable reference (user.age without assignment)
607
+ if (nextType === TT.DOT) {
608
+ let fullName = this.consume(TT.IDENT).value;
609
+ this.consume(TT.DOT);
610
+ fullName = fullName + "." + this.consume(TT.IDENT).value;
611
+ children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
612
+ continue;
613
+ }
614
+
615
+ // bracket notation: ident[...]
616
+ if (nextType === TT.LBRACKET) {
617
+ this.consume(TT.IDENT);
618
+ const varExpr: Expression = { kind: "variable", name };
619
+ const indexedExpr = this.parsePostfix(varExpr);
620
+ children.push({ kind: "expr", expression: indexedExpr, loc });
621
+ continue;
622
+ }
623
+
624
+
625
+ // simple variable reference
626
+ this.consume(TT.IDENT);
627
+ children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
628
+ continue;
629
+ }
630
+
631
+ throw new SinthError(
632
+ `Unexpected token '${this.peek().value}' in if/else body`,
633
+ this.peek().loc,
634
+ );
635
+
636
+ // reject implicit concatenation only after expression children
637
+ if (children.length > 0) {
638
+ const lastChild = children[children.length - 1];
639
+ if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
640
+ const nextTT = this.tokens[this.pos]?.type;
641
+ if (
642
+ nextTT === TT.STRING || nextTT === TT.NUMBER ||
643
+ nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
644
+ (nextTT === TT.IDENT &&
645
+ this.tokens[this.pos]?.value !== "var" &&
646
+ this.tokens[this.pos]?.value !== "if" &&
647
+ this.tokens[this.pos]?.value !== "for" &&
648
+ this.tokens[this.pos]?.value !== "else" &&
649
+ this.tokens[this.pos]?.value !== "and" &&
650
+ this.tokens[this.pos]?.value !== "or")
651
+ ) {
652
+ throw new SinthError(
653
+ `Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
654
+ this.peek().loc,
655
+ );
656
+ }
657
+ }
658
+ }
659
+ }
660
+
661
+ return children;
662
+ }
663
+
664
+ // for loops
665
+
666
+ private parseForLoop(): ForLoop {
667
+ const loc = this.consume(TT.KW_FOR).loc;
668
+ const firstVar = this.consume(TT.IDENT).value;
669
+ let keyVar: string | undefined;
670
+ let itemVar: string;
671
+ let indexVar: string | undefined;
672
+ if (this.check(TT.COMMA)) {
673
+ this.consume(TT.COMMA);
674
+ const secondVar = this.consume(TT.IDENT).value;
675
+ if (this.check(TT.COMMA)) {
676
+ // three variables: key, value, index
677
+ this.consume(TT.COMMA);
678
+ keyVar = firstVar;
679
+ itemVar = secondVar;
680
+ indexVar = this.consume(TT.IDENT).value;
681
+ } else {
682
+ // two variables – tentatively value, index; fix after seeing source type
683
+ itemVar = firstVar;
684
+ indexVar = secondVar;
685
+ }
686
+ } else {
687
+ // one variable: item only
688
+ itemVar = firstVar;
689
+ }
690
+ if (!this.check(TT.KW_IN)) throw new SinthError("Expected 'in' after loop variable", this.peek().loc);
691
+ this.consume(TT.KW_IN);
692
+ const arrayVar = this.consume(TT.IDENT).value;
693
+
694
+ // If two variables and the source is an object, swap to key, value
695
+ if (keyVar === undefined && indexVar !== undefined) {
696
+ const srcDecl = this._varDecls.find(d => d.name === arrayVar);
697
+ if (srcDecl && srcDecl.varType === "obj") {
698
+ keyVar = itemVar; // first → key
699
+ itemVar = indexVar; // second → value
700
+ indexVar = undefined;
701
+ }
702
+ }
703
+
704
+ if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after for..in expression", this.peek().loc);
705
+ this.consume(TT.LBRACE);
706
+ this.loopVar = itemVar;
707
+ const body = this.parseChildList();
708
+ this.loopVar = null;
709
+ this.consume(TT.RBRACE);
710
+
711
+ return { kind: "for", keyVar, itemVar, indexVar, arrayVar, body, loc };
712
+ }
713
+
714
+ // component usages
715
+
716
+ private parseCompUse(): CompUse {
717
+ const loc = this.peek().loc;
718
+ const name = this.consume(TT.IDENT).value;
719
+ const attrs: Attr[] = [];
720
+
721
+ if (this.check(TT.LPAREN)) {
722
+ this.consume(TT.LPAREN);
723
+ while (!this.check(TT.RPAREN)) {
724
+ const aloc = this.peek().loc;
725
+ const aname = this.consume(TT.IDENT).value;
726
+ let value: Literal | null = null;
727
+ if (this.check(TT.COLON)) {
728
+ this.consume(TT.COLON);
729
+ if (this.isEventAttr(aname)) {
730
+ if (this.check(TT.STRING)) {
731
+ value = this.parseLiteral();
732
+ } else {
733
+ const savedPos = this.pos;
734
+ const expr = this.parseExpression();
735
+ if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN) || this.check(TT.OP_SEMI))) {
736
+ const exprs: Expression[] = [expr];
737
+ while (this.check(TT.OP_SEMI)) {
738
+ this.consume(TT.OP_SEMI);
739
+ const next = this.parseExpression();
740
+ if (next) exprs.push(next);
741
+ else break;
742
+ }
743
+ if (exprs.length === 1) {
744
+ value = { kind: "str", value: "__EXPR__" + JSON.stringify(exprs[0]) };
745
+ } else {
746
+ value = { kind: "str", value: "__MULTI_EXPR__" + JSON.stringify(exprs) };
747
+ }
748
+ } else {
749
+ this.pos = savedPos;
750
+ value = this.parseLiteral();
751
+ }
752
+ }
753
+ } else {
754
+ if (this.check(TT.STRING) || this.check(TT.NUMBER) ||
755
+ this.check(TT.BOOL_TRUE) || this.check(TT.BOOL_FALSE) ||
756
+ this.check(TT.NULL_LIT) || this.check(TT.LBRACKET) || this.check(TT.LBRACE)) {
757
+ value = this.parseLiteral();
758
+ } else if (this.check(TT.IDENT) || this.check(TT.LPAREN)) {
759
+ const savedPos = this.pos;
760
+ const expr = this.parseExpression();
761
+ if (expr && (this.check(TT.COMMA) || this.check(TT.RPAREN))) {
762
+ value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
763
+ } else {
764
+ this.pos = savedPos;
765
+ if (this.check(TT.IDENT)) {
766
+ value = { kind: "str", value: this.consume(TT.IDENT).value };
767
+ } else {
768
+ value = this.parseLiteral();
769
+ }
770
+ }
771
+ } else {
772
+ value = this.parseLiteral();
773
+ }
774
+ }
775
+ }
776
+ attrs.push({ name: aname, value, loc: aloc });
777
+ if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
778
+ }
779
+ this.consume(TT.RPAREN);
780
+ }
781
+
782
+ const children: Child[] = [];
783
+ if (this.check(TT.RAW_BLOCK)) {
784
+ const rawTok = this.consume(TT.RAW_BLOCK);
785
+ const tokens = new Lexer(rawTok.value, rawTok.loc.file).tokenize();
786
+ const sub = new Parser(tokens, rawTok.loc.file);
787
+ sub._varDecls = this._varDecls;
788
+ children.push(...sub.parseChildList());
789
+ } else if (this.check(TT.LBRACE)) {
790
+ this.consume(TT.LBRACE);
791
+ children.push(...this.parseChildList());
792
+ this.consume(TT.RBRACE);
793
+ }
794
+
795
+ return { kind: "use", name, attrs, children, loc };
796
+ }
797
+
798
+ private isEventAttr(name: string): boolean {
799
+ return name.startsWith("on") && name.length > 2 && name[2] === name[2].toUpperCase();
800
+ }
801
+
802
+ private parseChildList(): Child[] {
803
+ const children: Child[] = [];
804
+ while (!this.check(TT.EOF) && !this.check(TT.RBRACE)) {
805
+ if (this.check(TT.LPAREN)) {
806
+ const loc = this.peek().loc;
807
+ let expr = this.parseExpression();
808
+ if (expr) {
809
+ while (this.check(TT.OP_PLUS)) {
810
+ this.consume(TT.OP_PLUS);
811
+ let rhs: Expression | null = null;
812
+ if (this.check(TT.STRING)) {
813
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
814
+ } else if (this.check(TT.IDENT)) {
815
+ const rhsName = this.consume(TT.IDENT).value;
816
+ if (this.check(TT.DOT)) {
817
+ this.consume(TT.DOT);
818
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
819
+ } else if (this.check(TT.LBRACKET)) {
820
+ const rhsVar: Expression = { kind: "variable", name: rhsName };
821
+ rhs = this.parsePostfix(rhsVar);
822
+ } else {
823
+ rhs = { kind: "variable", name: rhsName };
824
+ }
825
+ } else {
826
+ rhs = this.parseExpression();
827
+ }
828
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
829
+ expr = { kind: "binary", left: expr, op: "+", right: rhs };
830
+ }
831
+ children.push({ kind: "expr", expression: expr, loc });
832
+ }
833
+ continue;
834
+ }
835
+ if (this.check(TT.STRING)) {
836
+ const loc = this.peek().loc;
837
+ let leftExpr: Expression = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
838
+ while (this.check(TT.OP_PLUS)) {
839
+ this.consume(TT.OP_PLUS);
840
+ let rhs: Expression | null = null;
841
+ if (this.check(TT.STRING)) {
842
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
843
+ } else if (this.check(TT.IDENT)) {
844
+ const rhsName = this.consume(TT.IDENT).value;
845
+ if (this.check(TT.DOT)) {
846
+ this.consume(TT.DOT);
847
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
848
+ } else if (this.check(TT.LBRACKET)) {
849
+ const rhsVar: Expression = { kind: "variable", name: rhsName };
850
+ rhs = this.parsePostfix(rhsVar);
851
+ } else {
852
+ rhs = { kind: "variable", name: rhsName };
853
+ }
854
+ } else {
855
+ rhs = this.parseExpression();
856
+ }
857
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
858
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
859
+ }
860
+ children.push({ kind: "expr", expression: leftExpr, loc });
861
+ }
862
+ else if (this.check(TT.IDENT) && this.peek().value === "var") {
863
+ const vd = this.parseVarDeclaration();
864
+ if (vd) this._varDecls.push(vd);
865
+ continue;
866
+ }
867
+ else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); }
868
+ else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); }
869
+ else if (this.check(TT.IDENT)) {
870
+ const loc = this.peek().loc;
871
+ const rawName = this.peek().value;
872
+ const nextType = this.tokens[this.pos + 1]?.type;
873
+
874
+ if (nextType === TT.DOT) {
875
+ let fullName = this.consume(TT.IDENT).value;
876
+ this.consume(TT.DOT);
877
+ fullName = fullName + "." + this.consume(TT.IDENT).value;
878
+ const afterDotType = this.tokens[this.pos]?.type;
879
+
880
+ if (afterDotType === TT.OP_PLUS) {
881
+ let leftExpr: Expression = { kind: "variable", name: fullName };
882
+ while (this.check(TT.OP_PLUS)) {
883
+ this.consume(TT.OP_PLUS);
884
+ let rhs: Expression | null = null;
885
+ if (this.check(TT.STRING)) {
886
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
887
+ } else if (this.check(TT.IDENT)) {
888
+ const rhsName = this.consume(TT.IDENT).value;
889
+ if (this.check(TT.DOT)) {
890
+ this.consume(TT.DOT);
891
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
892
+ } else if (this.check(TT.LBRACKET)) {
893
+ const rhsVar: Expression = { kind: "variable", name: rhsName };
894
+ rhs = this.parsePostfix(rhsVar);
895
+ } else {
896
+ rhs = { kind: "variable", name: rhsName };
897
+ }
898
+ } else {
899
+ rhs = this.parseExpression();
900
+ }
901
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
902
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
903
+ }
904
+ children.push({ kind: "expr", expression: leftExpr, loc });
905
+ } else if (this.loopVar && fullName.split('.')[0] === this.loopVar) {
906
+ children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
907
+ } else if (afterDotType === TT.LPAREN || afterDotType === TT.LBRACE || afterDotType === TT.RAW_BLOCK) {
908
+ throw new SinthError(`Unexpected dot notation before component usage`, loc);
909
+ } else {
910
+ children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
911
+ }
912
+ } else if (nextType === TT.OP_PLUS) {
913
+ const name = this.consume(TT.IDENT).value;
914
+ let leftExpr: Expression = { kind: "variable", name };
915
+ while (this.check(TT.OP_PLUS)) {
916
+ this.consume(TT.OP_PLUS);
917
+ let rhs: Expression | null = null;
918
+ if (this.check(TT.STRING)) {
919
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
920
+ } else if (this.check(TT.IDENT)) {
921
+ const rhsName = this.consume(TT.IDENT).value;
922
+ if (this.check(TT.DOT)) {
923
+ this.consume(TT.DOT);
924
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
925
+ } else if (this.check(TT.LBRACKET)) {
926
+ const rhsVar: Expression = { kind: "variable", name: rhsName };
927
+ rhs = this.parsePostfix(rhsVar);
928
+ } else {
929
+ rhs = { kind: "variable", name: rhsName };
930
+ }
931
+ } else {
932
+ rhs = this.parseExpression();
933
+ }
934
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
935
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
936
+ }
937
+ children.push({ kind: "expr", expression: leftExpr, loc });
938
+ } else if (this.loopVar && rawName === this.loopVar) {
939
+ this.consume(TT.IDENT);
940
+ children.push({ kind: "expr", expression: { kind: "variable", name: rawName }, loc });
941
+ } else if (nextType === TT.LBRACKET) {
942
+ const name = this.consume(TT.IDENT).value;
943
+ const varExpr: Expression = { kind: "variable", name };
944
+ let indexedExpr = this.parsePostfix(varExpr);
945
+ // support concatenation: data[prop] + "..."
946
+ if (this.check(TT.OP_PLUS)) {
947
+ let leftExpr: Expression = indexedExpr;
948
+ while (this.check(TT.OP_PLUS)) {
949
+ this.consume(TT.OP_PLUS);
950
+ let rhs: Expression | null = null;
951
+ if (this.check(TT.STRING)) {
952
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
953
+ } else if (this.check(TT.IDENT)) {
954
+ const rhsName = this.consume(TT.IDENT).value;
955
+ if (this.check(TT.DOT)) {
956
+ this.consume(TT.DOT);
957
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
958
+ } else if (this.check(TT.LBRACKET)) {
959
+ const rhsVar: Expression = { kind: "variable", name: rhsName };
960
+ rhs = this.parsePostfix(rhsVar);
961
+ } else {
962
+ rhs = { kind: "variable", name: rhsName };
963
+ }
964
+ } else {
965
+ rhs = this.parseExpression();
966
+ }
967
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
968
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
969
+ }
970
+ children.push({ kind: "expr", expression: leftExpr, loc });
971
+ } else {
972
+ children.push({ kind: "expr", expression: indexedExpr, loc });
973
+ }
974
+ } else if (nextType === TT.LPAREN || nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
975
+ children.push(this.parseCompUse());
976
+ } else {
977
+ const name = this.consume(TT.IDENT).value;
978
+ children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
979
+ }
980
+ }
981
+ else throw new SinthError(`Unexpected token '${this.peek().value}' in children`, this.peek().loc);
982
+ // reject implicit concatenation only after expression children
983
+ if (children.length > 0) {
984
+ const lastChild = children[children.length - 1];
985
+ if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
986
+ const nextTT = this.tokens[this.pos]?.type;
987
+ if (
988
+ nextTT === TT.STRING || nextTT === TT.NUMBER ||
989
+ nextTT === TT.BOOL_TRUE || nextTT === TT.BOOL_FALSE || nextTT === TT.NULL_LIT ||
990
+ (nextTT === TT.IDENT &&
991
+ this.tokens[this.pos]?.value !== "var" &&
992
+ this.tokens[this.pos]?.value !== "if" &&
993
+ this.tokens[this.pos]?.value !== "for" &&
994
+ this.tokens[this.pos]?.value !== "else" &&
995
+ this.tokens[this.pos]?.value !== "and" &&
996
+ this.tokens[this.pos]?.value !== "or")
997
+ ) {
998
+ throw new SinthError(
999
+ `Unexpected ${TT[nextTT]}. Use '+' to concatenate values.`,
1000
+ this.peek().loc,
1001
+ );
1002
+ }
1003
+ }
1004
+ }
1005
+ }
1006
+ return children;
1007
+ }
1008
+
1009
+ // style & script blocks
1010
+
1011
+ private parseStyleBlock(): StyleBlock {
1012
+ const loc = this.consume(TT.KW_STYLE).loc;
1013
+ let global = false, lang: "css" | "scss" = "css", target: string | undefined;
1014
+ const extraAttrs: Record<string, string> = {};
1015
+
1016
+ while (!this.check(TT.RAW_BLOCK) && !this.check(TT.EOF)) {
1017
+ if (this.check(TT.KW_GLOBAL)) { global = true; this.consume(TT.KW_GLOBAL); }
1018
+ else if (this.check(TT.KW_LANG)) {
1019
+ this.consume(TT.KW_LANG); this.consume(TT.EQUALS);
1020
+ const lv = this.consume(TT.STRING).value;
1021
+ if (lv !== "css" && lv !== "scss") throw new SinthError(`Unsupported style lang '${lv}'`, loc);
1022
+ lang = lv as "css" | "scss";
1023
+ }
1024
+ else if (this.check(TT.IDENT)) {
1025
+ if (this.tokens[this.pos + 1]?.type !== TT.EQUALS) {
1026
+ target = this.consume(TT.IDENT).value;
1027
+ } else {
1028
+ const k = this.consume(TT.IDENT).value;
1029
+ this.consume(TT.EQUALS);
1030
+ extraAttrs[k] = this.consume(TT.STRING).value;
1031
+ }
1032
+ }
1033
+ else throw new SinthError(`Unexpected token '${this.peek().value}' in style modifiers`, this.peek().loc);
1034
+ }
1035
+
1036
+ return { global, lang, raw: this.consume(TT.RAW_BLOCK).value, target, extraAttrs, loc };
1037
+ }
1038
+
1039
+ private parseScriptBlock(): ScriptBlock {
1040
+ const loc = this.consume(TT.KW_SCRIPT).loc;
1041
+ const attrs: Record<string, string> = {};
1042
+ while (!this.check(TT.RAW_BLOCK) && !this.check(TT.EOF)) {
1043
+ const key = this.consume(TT.IDENT).value;
1044
+ if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); attrs[key] = this.consume(TT.STRING).value; }
1045
+ else { attrs[key] = "true"; }
1046
+ }
1047
+ return { raw: this.consume(TT.RAW_BLOCK).value, attrs, loc };
1048
+ }
1049
+
1050
+ private parseLiteral(): Literal {
1051
+ const tok = this.peek();
1052
+ if (this.check(TT.LBRACKET)) { return this.parseArrayLiteral(); }
1053
+ if (this.check(TT.LBRACE)) {
1054
+ const obj = this.parseObjectLiteral();
1055
+ return { kind: "str", value: JSON.stringify(obj) };
1056
+ }
1057
+ if (this.check(TT.STRING)) { this.consume(TT.STRING); return { kind: "str", value: tok.value }; }
1058
+ if (this.check(TT.NUMBER)) { this.consume(TT.NUMBER); return { kind: "num", value: parseFloat(tok.value) }; }
1059
+ if (this.check(TT.BOOL_TRUE)) { this.consume(TT.BOOL_TRUE); return { kind: "bool", value: true }; }
1060
+ if (this.check(TT.BOOL_FALSE)){ this.consume(TT.BOOL_FALSE);return { kind: "bool", value: false }; }
1061
+ if (this.check(TT.NULL_LIT)) { this.consume(TT.NULL_LIT); return { kind: "null" }; }
1062
+ throw new SinthError(`Expected literal value, got '${tok.value}' (${TT[tok.type]})`, tok.loc);
1063
+ }
1064
+
1065
+ private peek(): Token { return this.tokens[this.pos]; }
1066
+ private check(t: TT): boolean { return this.tokens[this.pos]?.type === t; }
1067
+ private consume(t: TT): Token {
1068
+ const tok = this.tokens[this.pos];
1069
+ if (tok.type !== t) {
1070
+ throw new SinthError(
1071
+ `Expected ${TT[t]}, got '${tok.value}' (${TT[tok.type]})`,
1072
+ tok.loc,
1073
+ );
1074
+ }
1075
+ this.pos++;
1076
+ return tok;
1077
+ }
1078
+ }