@yannosay/sinth 1.11.4 → 1.12.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,1672 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Parser = void 0;
4
+ const types_1 = require("./types");
5
+ const lexer_1 = require("./lexer");
6
+ const expr_1 = require("./expr");
7
+ class Parser {
8
+ constructor(tokens, file) {
9
+ this.tokens = tokens;
10
+ this.file = file;
11
+ this.pos = 0;
12
+ this.loopVar = null;
13
+ this._varDecls = [];
14
+ this._visited = new Set();
15
+ }
16
+ parse() {
17
+ let isPage = false;
18
+ const imports = [];
19
+ const meta = [];
20
+ const defs = [];
21
+ const functions = [];
22
+ const uses = [];
23
+ const styles = [];
24
+ const scripts = [];
25
+ const customEls = [];
26
+ const varDecls = [];
27
+ this._varDecls = varDecls;
28
+ if (this.check(types_1.TT.KW_PAGE)) {
29
+ this.consume(types_1.TT.KW_PAGE);
30
+ isPage = true;
31
+ }
32
+ while (this.check(types_1.TT.KW_IMPORT))
33
+ imports.push(this.parseImport());
34
+ while (this.isMetaStart()) {
35
+ const entry = this.parseMeta();
36
+ meta.push(entry);
37
+ if (entry.key === "title")
38
+ isPage = true;
39
+ }
40
+ while (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
41
+ const vd = this.parseVarDeclaration();
42
+ if (vd) {
43
+ varDecls.push(vd);
44
+ }
45
+ }
46
+ while (!this.check(types_1.TT.EOF)) {
47
+ if (this.check(types_1.TT.KW_COMPONENT)) {
48
+ defs.push(this.parseCompDef());
49
+ }
50
+ else if (this.check(types_1.TT.KW_FUNCTION)) {
51
+ functions.push(this.parseFunctionDef());
52
+ } // ← new
53
+ else if (this.check(types_1.TT.KW_STYLE)) {
54
+ styles.push(this.parseStyleBlock());
55
+ }
56
+ else if (this.check(types_1.TT.KW_SCRIPT)) {
57
+ scripts.push(this.parseScriptBlock());
58
+ }
59
+ else if (this.check(types_1.TT.KW_CUSTOM_EL) || this.check(types_1.TT.KW_CUSTOM)) {
60
+ customEls.push(this.parseCustomEl());
61
+ }
62
+ else if (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
63
+ const vd = this.parseVarDeclaration();
64
+ if (vd)
65
+ varDecls.push(vd);
66
+ }
67
+ else if (this.check(types_1.TT.KW_IF)) {
68
+ const ifNode = this.parseIfBlock();
69
+ uses.push(...this.flattenIfToUses(ifNode, scripts));
70
+ }
71
+ else if (this.check(types_1.TT.KW_FOR)) {
72
+ const forNode = this.parseForLoop();
73
+ uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [forNode], loc: forNode.loc });
74
+ }
75
+ else if (this.check(types_1.TT.LPAREN)) {
76
+ const loc = this.peek().loc;
77
+ this.consume(types_1.TT.LPAREN);
78
+ const expr = this.parseExpression();
79
+ if (!expr)
80
+ throw new types_1.SinthError("Expected expression after '('", loc);
81
+ if (!this.check(types_1.TT.RPAREN))
82
+ throw new types_1.SinthError("Expected ')'", loc);
83
+ this.consume(types_1.TT.RPAREN);
84
+ uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [{ kind: "expr", expression: expr, loc }], loc });
85
+ }
86
+ else if (this.check(types_1.TT.IDENT)) {
87
+ const savedPos = this.pos;
88
+ const identName = this.peek().value;
89
+ const nextType = this.tokens[this.pos + 1]?.type;
90
+ // detect accidental top-level expressions (guess == target" instead of "if guess == target")
91
+ const isComparison = [types_1.TT.OP_EQEQ, types_1.TT.OP_NEQ, types_1.TT.OP_LT, types_1.TT.OP_GT, types_1.TT.OP_LTEQ, types_1.TT.OP_GTEQ].includes(nextType);
92
+ const isLogicAndOr = nextType === types_1.TT.IDENT && (this.tokens[this.pos + 1]?.value === "and" || this.tokens[this.pos + 1]?.value === "or");
93
+ if (isComparison || isLogicAndOr) {
94
+ throw new types_1.SinthError(`Unexpected expression '${identName} ...' at top level. Did you mean to wrap this in an 'if' block?`, this.peek().loc);
95
+ }
96
+ // top-level assignment: ident = expr
97
+ if (nextType === types_1.TT.EQUALS ||
98
+ (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS) ||
99
+ (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS)) {
100
+ this.consume(types_1.TT.IDENT); // consume name
101
+ let op = "=";
102
+ if (this.check(types_1.TT.OP_PLUS)) {
103
+ this.consume(types_1.TT.OP_PLUS);
104
+ op = "+=";
105
+ }
106
+ else if (this.check(types_1.TT.OP_MINUS)) {
107
+ this.consume(types_1.TT.OP_MINUS);
108
+ op = "-=";
109
+ }
110
+ this.consume(types_1.TT.EQUALS);
111
+ const expr = this.parseExpression();
112
+ if (expr) {
113
+ const js = `${identName} ${op} ${(0, expr_1.compileExprToJS)(expr)};
114
+ sinthRender();`;
115
+ scripts.push({ raw: js, attrs: {}, loc: this.peek().loc });
116
+ }
117
+ continue;
118
+ }
119
+ if (nextType === types_1.TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
120
+ this.consume(types_1.TT.IDENT);
121
+ this.consume(types_1.TT.LPAREN);
122
+ const args = [];
123
+ if (!this.check(types_1.TT.RPAREN)) {
124
+ args.push(this.parseExpression());
125
+ while (this.check(types_1.TT.COMMA)) {
126
+ this.consume(types_1.TT.COMMA);
127
+ args.push(this.parseExpression());
128
+ }
129
+ }
130
+ this.consume(types_1.TT.RPAREN);
131
+ const expr = { kind: "call", callee: { kind: "variable", name: identName }, args };
132
+ uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [{ kind: "expr", expression: expr, loc: this.peek().loc }], loc: this.peek().loc });
133
+ }
134
+ else {
135
+ this.pos = savedPos;
136
+ uses.push(this.parseCompUse());
137
+ }
138
+ }
139
+ else
140
+ throw new types_1.SinthError(`Unexpected token '${this.peek().value}' (${types_1.TT[this.peek().type]}) at top level`, this.peek().loc);
141
+ }
142
+ this._visited.clear();
143
+ return { filePath: this.file, isPage, imports, meta, defs, functions, uses, styles, scripts, customEls, varDecls };
144
+ }
145
+ flattenIfToUses(ifNode, scripts) {
146
+ const bodyHasComp = ifNode.body.some(c => c.kind === "use");
147
+ const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
148
+ if (!bodyHasComp && !elseHasComp) {
149
+ scripts.push({ raw: (0, expr_1.compileIfToJS)(ifNode), attrs: {}, loc: ifNode.loc });
150
+ return [];
151
+ }
152
+ const syntheticUse = {
153
+ kind: "use",
154
+ name: "__IF_ROOT__",
155
+ attrs: [],
156
+ children: [ifNode],
157
+ loc: ifNode.loc,
158
+ };
159
+ return [syntheticUse];
160
+ }
161
+ // imports
162
+ parseImport() {
163
+ const loc = this.consume(types_1.TT.KW_IMPORT).loc;
164
+ if (this.check(types_1.TT.KW_CSS)) {
165
+ this.consume(types_1.TT.KW_CSS);
166
+ return { kind: "css", path: this.consume(types_1.TT.STRING).value, loc };
167
+ }
168
+ if (this.check(types_1.TT.STRING)) {
169
+ return { kind: "sinth", path: this.consume(types_1.TT.STRING).value, loc };
170
+ }
171
+ if (this.check(types_1.TT.IDENT)) {
172
+ const name = this.consume(types_1.TT.IDENT).value;
173
+ let alias;
174
+ if (this.check(types_1.TT.KW_AS)) {
175
+ this.consume(types_1.TT.KW_AS);
176
+ alias = this.consume(types_1.TT.IDENT).value;
177
+ }
178
+ return { kind: "js", name, alias, loc };
179
+ }
180
+ throw new types_1.SinthError(`Expected import path, library name, or 'css'`, loc);
181
+ }
182
+ // metadata
183
+ isMetaStart() {
184
+ if (!this.check(types_1.TT.IDENT))
185
+ return false;
186
+ return this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS;
187
+ }
188
+ parseMeta() {
189
+ const loc = this.peek().loc;
190
+ const key = this.consume(types_1.TT.IDENT).value;
191
+ this.consume(types_1.TT.EQUALS);
192
+ return { key, value: this.parseLiteral(), loc };
193
+ }
194
+ // var declarations
195
+ parseVarDeclaration() {
196
+ const loc = this.peek().loc;
197
+ this.consume(types_1.TT.IDENT);
198
+ const typeTok = this.consume(types_1.TT.IDENT);
199
+ let typeStr = typeTok.value;
200
+ if (this.check(types_1.TT.LBRACKET) && this.tokens[this.pos + 1]?.type === types_1.TT.RBRACKET) {
201
+ this.consume(types_1.TT.LBRACKET);
202
+ this.consume(types_1.TT.RBRACKET);
203
+ typeStr = "str[]";
204
+ }
205
+ if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeStr)) {
206
+ throw new types_1.SinthError(`Unknown type '${typeStr}'. Expected: int, str, bool, str[], obj, component`, typeTok.loc);
207
+ }
208
+ const nameTok = this.consume(types_1.TT.IDENT);
209
+ let val = null;
210
+ if (this.check(types_1.TT.EQUALS)) {
211
+ this.consume(types_1.TT.EQUALS);
212
+ if (this.check(types_1.TT.LBRACKET)) {
213
+ val = this.parseArrayLiteral();
214
+ }
215
+ else if (this.check(types_1.TT.IDENT) || this.check(types_1.TT.LPAREN)) {
216
+ const savedPos = this.pos;
217
+ const expr = this.parseExpression();
218
+ if (expr && (expr.kind !== "literal" || (expr.kind === "literal" && expr.value?.kind === "str" && expr.value.value.startsWith("__VAR__")))) {
219
+ val = { kind: "str", value: `__EXPR__${JSON.stringify(expr)}` };
220
+ }
221
+ else {
222
+ this.pos = savedPos;
223
+ val = this.parseLiteral();
224
+ }
225
+ }
226
+ else if (this.check(types_1.TT.NUMBER)) {
227
+ val = this.parseLiteral();
228
+ }
229
+ else {
230
+ val = this.parseLiteral();
231
+ }
232
+ }
233
+ return { kind: "var", name: nameTok.value, varType: typeStr, value: val, loc };
234
+ }
235
+ parseArrayLiteral() {
236
+ this.consume(types_1.TT.LBRACKET);
237
+ const items = [];
238
+ while (!this.check(types_1.TT.RBRACKET) && !this.check(types_1.TT.EOF)) {
239
+ if (this.check(types_1.TT.LBRACE)) {
240
+ items.push(this.parseObjectLiteral());
241
+ }
242
+ else if (this.check(types_1.TT.STRING)) {
243
+ items.push(this.consume(types_1.TT.STRING).value);
244
+ }
245
+ else if (this.check(types_1.TT.NUMBER)) {
246
+ items.push(parseFloat(this.consume(types_1.TT.NUMBER).value));
247
+ }
248
+ else if (this.check(types_1.TT.BOOL_TRUE)) {
249
+ this.consume(types_1.TT.BOOL_TRUE);
250
+ items.push(true);
251
+ }
252
+ else if (this.check(types_1.TT.BOOL_FALSE)) {
253
+ this.consume(types_1.TT.BOOL_FALSE);
254
+ items.push(false);
255
+ }
256
+ else if (this.check(types_1.TT.NULL_LIT)) {
257
+ this.consume(types_1.TT.NULL_LIT);
258
+ items.push(null);
259
+ }
260
+ if (this.check(types_1.TT.COMMA))
261
+ this.consume(types_1.TT.COMMA);
262
+ else
263
+ break;
264
+ }
265
+ this.consume(types_1.TT.RBRACKET);
266
+ return { kind: "str", value: `__ARR__${JSON.stringify(items)}` };
267
+ }
268
+ parseObjectLiteral() {
269
+ this.consume(types_1.TT.LBRACE);
270
+ const obj = {};
271
+ while (!this.check(types_1.TT.RBRACE) && !this.check(types_1.TT.EOF)) {
272
+ const key = this.consume(types_1.TT.IDENT).value;
273
+ this.consume(types_1.TT.COLON);
274
+ if (this.check(types_1.TT.STRING)) {
275
+ obj[key] = this.consume(types_1.TT.STRING).value;
276
+ }
277
+ else if (this.check(types_1.TT.NUMBER)) {
278
+ obj[key] = parseFloat(this.consume(types_1.TT.NUMBER).value);
279
+ }
280
+ else if (this.check(types_1.TT.BOOL_TRUE)) {
281
+ this.consume(types_1.TT.BOOL_TRUE);
282
+ obj[key] = true;
283
+ }
284
+ else if (this.check(types_1.TT.BOOL_FALSE)) {
285
+ this.consume(types_1.TT.BOOL_FALSE);
286
+ obj[key] = false;
287
+ }
288
+ else if (this.check(types_1.TT.NULL_LIT)) {
289
+ this.consume(types_1.TT.NULL_LIT);
290
+ obj[key] = null;
291
+ }
292
+ else if (this.check(types_1.TT.IDENT)) {
293
+ obj[key] = `__VAR__${this.consume(types_1.TT.IDENT).value}`;
294
+ }
295
+ else {
296
+ throw new types_1.SinthError(`Expected literal value for key '${key}'`, this.peek().loc);
297
+ }
298
+ if (this.check(types_1.TT.COMMA))
299
+ this.consume(types_1.TT.COMMA);
300
+ }
301
+ this.consume(types_1.TT.RBRACE);
302
+ return obj;
303
+ }
304
+ parseExpression(skipBinary = false) {
305
+ if (this._visited.has(this.pos))
306
+ throw new types_1.SinthError("Circular expression reference", this.peek().loc);
307
+ this._visited.add(this.pos);
308
+ const tok = this.peek();
309
+ // unary 'not'
310
+ if ((tok.type === types_1.TT.IDENT && tok.value === "not") || tok.type === types_1.TT.OP_NOT) {
311
+ if (tok.type === types_1.TT.IDENT)
312
+ this.consume(types_1.TT.IDENT);
313
+ else
314
+ this.consume(types_1.TT.OP_NOT);
315
+ const operand = this.parseExpression();
316
+ if (!operand)
317
+ throw new types_1.SinthError("Expected expression after 'not'", tok.loc);
318
+ const unaryExpr = { kind: "unary", op: "not", operand };
319
+ return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(unaryExpr)));
320
+ }
321
+ if (this.check(types_1.TT.LPAREN)) {
322
+ this.consume(types_1.TT.LPAREN);
323
+ const inner = this.parseExpression();
324
+ if (!inner)
325
+ throw new types_1.SinthError("Expected expression after '('", this.peek().loc);
326
+ if (!this.check(types_1.TT.RPAREN))
327
+ throw new types_1.SinthError("Expected closing ')'", this.peek().loc);
328
+ this.consume(types_1.TT.RPAREN);
329
+ const result = this.parseCallExpr(this.parsePostfix(inner));
330
+ return skipBinary ? result : this.parseBinaryRHS(result);
331
+ }
332
+ if (this.check(types_1.TT.STRING) || this.check(types_1.TT.NUMBER) || this.check(types_1.TT.BOOL_TRUE) ||
333
+ this.check(types_1.TT.BOOL_FALSE) || this.check(types_1.TT.NULL_LIT) || this.check(types_1.TT.LBRACE) || this.check(types_1.TT.LBRACKET)) {
334
+ const lit = this.parseLiteral();
335
+ const base = { kind: "literal", value: lit };
336
+ return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
337
+ }
338
+ // remove as function
339
+ if (tok.type === types_1.TT.KW_REMOVE) {
340
+ const name = this.consume(types_1.TT.KW_REMOVE).value;
341
+ return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
342
+ }
343
+ // var or assignment
344
+ if (tok.type === types_1.TT.IDENT) {
345
+ let memo = false;
346
+ let rawName = this.consume(types_1.TT.IDENT).value;
347
+ if (rawName.startsWith("$")) {
348
+ memo = true;
349
+ rawName = rawName.substring(1);
350
+ }
351
+ const name = rawName;
352
+ let varExpr;
353
+ // dot notation -> e.g. user.age
354
+ if (this.check(types_1.TT.DOT)) {
355
+ this.consume(types_1.TT.DOT);
356
+ const propName = this.consume(types_1.TT.IDENT).value;
357
+ const fullName = `${name}.${propName}`;
358
+ varExpr = { kind: "variable", name: fullName };
359
+ }
360
+ else {
361
+ varExpr = { kind: "variable", name };
362
+ }
363
+ // apply bracket postfixes (e.g. data[key], data["x"])
364
+ const indexedExpr = this.parsePostfix(varExpr);
365
+ // if indexing was applied, we don't allow assignment, just binary RHS (after possible call)
366
+ if (indexedExpr !== varExpr) {
367
+ return this.parseBinaryRHS(this.parseCallExpr(indexedExpr));
368
+ }
369
+ // no indexing – check for assignment operators
370
+ const nextType = this.tokens[this.pos]?.type;
371
+ if (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
372
+ this.consume(types_1.TT.OP_PLUS);
373
+ this.consume(types_1.TT.EQUALS);
374
+ const rhs = this.parseExpression();
375
+ if (!rhs)
376
+ throw new types_1.SinthError("Expected expression after +=", this.peek().loc);
377
+ return { kind: "assign", target: varExpr.name, op: "+=", right: rhs };
378
+ }
379
+ if (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
380
+ this.consume(types_1.TT.OP_MINUS);
381
+ this.consume(types_1.TT.EQUALS);
382
+ const rhs = this.parseExpression();
383
+ if (!rhs)
384
+ throw new types_1.SinthError("Expected expression after -=", this.peek().loc);
385
+ return { kind: "assign", target: varExpr.name, op: "-=", right: rhs };
386
+ }
387
+ if (nextType === types_1.TT.EQUALS) {
388
+ this.consume(types_1.TT.EQUALS);
389
+ const rhs = this.parseExpression();
390
+ if (!rhs)
391
+ throw new types_1.SinthError("Expected expression after =", this.peek().loc);
392
+ return { kind: "assign", target: varExpr.name, op: "=", right: rhs };
393
+ }
394
+ return this.parseBinaryRHS(this.parseCallExpr(varExpr));
395
+ }
396
+ return null;
397
+ }
398
+ parsePrimary() {
399
+ const tok = this.peek();
400
+ if (tok.type === types_1.TT.NUMBER || tok.type === types_1.TT.STRING || tok.type === types_1.TT.BOOL_TRUE || tok.type === types_1.TT.BOOL_FALSE || tok.type === types_1.TT.NULL_LIT) {
401
+ const lit = this.parseLiteral();
402
+ return { kind: "literal", value: lit };
403
+ }
404
+ if (tok.type === types_1.TT.IDENT) {
405
+ let memo = false;
406
+ let rawName = this.consume(types_1.TT.IDENT).value;
407
+ if (rawName.startsWith("$")) {
408
+ memo = true;
409
+ rawName = rawName.substring(1);
410
+ }
411
+ const name = rawName;
412
+ let expr;
413
+ if (this.check(types_1.TT.DOT)) {
414
+ this.consume(types_1.TT.DOT);
415
+ expr = { kind: "variable", name: name + "." + this.consume(types_1.TT.IDENT).value };
416
+ }
417
+ else {
418
+ expr = { kind: "variable", name };
419
+ }
420
+ if (memo)
421
+ expr.memo = true;
422
+ return expr;
423
+ }
424
+ if (tok.type === types_1.TT.LPAREN) {
425
+ this.consume(types_1.TT.LPAREN);
426
+ const inner = this.parseExpression();
427
+ this.consume(types_1.TT.RPAREN);
428
+ return inner;
429
+ }
430
+ if (tok.type === types_1.TT.LBRACE) {
431
+ const obj = this.parseObjectLiteral();
432
+ return { kind: "literal", value: { kind: "str", value: JSON.stringify(obj) } };
433
+ }
434
+ throw new types_1.SinthError(`Expected expression`, this.peek().loc);
435
+ }
436
+ parseComparison(left) {
437
+ const savedPos = this.pos;
438
+ const t = this.peek().type;
439
+ let op = null;
440
+ if (t === types_1.TT.OP_EQEQ)
441
+ op = "==";
442
+ else if (t === types_1.TT.OP_NEQ)
443
+ op = "!=";
444
+ else if (t === types_1.TT.OP_LT)
445
+ op = "<";
446
+ else if (t === types_1.TT.OP_GT)
447
+ op = ">";
448
+ else if (t === types_1.TT.OP_LTEQ)
449
+ op = "<=";
450
+ else if (t === types_1.TT.OP_GTEQ)
451
+ op = ">=";
452
+ if (!op)
453
+ return left;
454
+ this.pos++;
455
+ const rightPrimary = this.parsePrimary();
456
+ const rightWithPostfix = this.parsePostfix(rightPrimary);
457
+ const rightWithCall = this.parseCallExpr(rightWithPostfix);
458
+ const right = this.parseArithmetic(rightWithCall);
459
+ if (!right) {
460
+ this.pos = savedPos;
461
+ return left;
462
+ }
463
+ if (this.pos === savedPos)
464
+ return { kind: "binary", left, op, right };
465
+ return { kind: "binary", left, op, right };
466
+ }
467
+ parseLogical(left) {
468
+ const savedPos = this.pos;
469
+ const t = this.peek().type;
470
+ let op = null;
471
+ if (t === types_1.TT.IDENT && this.peek().value === "and")
472
+ op = "and";
473
+ else if (t === types_1.TT.IDENT && this.peek().value === "or")
474
+ op = "or";
475
+ if (!op)
476
+ return left;
477
+ this.pos++;
478
+ const right = this.parseExpression();
479
+ if (!right) {
480
+ this.pos = savedPos;
481
+ return left;
482
+ }
483
+ return this.parseLogical({ kind: "binary", left, op, right });
484
+ }
485
+ parseArithmetic(left) {
486
+ while (true) {
487
+ const t = this.peek().type;
488
+ let op = null;
489
+ if (t === types_1.TT.OP_PLUS)
490
+ op = "+";
491
+ else if (t === types_1.TT.OP_MINUS)
492
+ op = "-";
493
+ else if (t === types_1.TT.OP_STAR)
494
+ op = "*";
495
+ else if (t === types_1.TT.OP_SLASH)
496
+ op = "/";
497
+ else if (t === types_1.TT.OP_MOD)
498
+ op = "%";
499
+ if (!op)
500
+ break;
501
+ this.pos++;
502
+ const rightPrimary = this.parsePrimary();
503
+ const rightWithPostfix = this.parsePostfix(rightPrimary);
504
+ const rightWithCall = this.parseCallExpr(rightWithPostfix);
505
+ const right = rightWithCall;
506
+ if (!right)
507
+ throw new types_1.SinthError(`Expected expression after '${op}'`, this.peek().loc);
508
+ left = { kind: "binary", left, op, right };
509
+ }
510
+ return left;
511
+ }
512
+ parseBinaryRHS(left) {
513
+ left = this.parseArithmetic(left);
514
+ left = this.parseComparison(left);
515
+ left = this.parseLogical(left);
516
+ return left;
517
+ }
518
+ parsePostfix(base) {
519
+ let expr = base;
520
+ while (this.check(types_1.TT.LBRACKET)) {
521
+ this.consume(types_1.TT.LBRACKET);
522
+ const key = this.parseExpression();
523
+ if (!key)
524
+ throw new types_1.SinthError("Expected expression inside [...]", this.peek().loc);
525
+ this.consume(types_1.TT.RBRACKET);
526
+ expr = { kind: "index", object: expr, key };
527
+ }
528
+ return expr;
529
+ }
530
+ parseCallExpr(expr) {
531
+ while (this.check(types_1.TT.LPAREN)) {
532
+ this.consume(types_1.TT.LPAREN);
533
+ const args = [];
534
+ if (!this.check(types_1.TT.RPAREN)) {
535
+ args.push(this.parseExpression());
536
+ while (this.check(types_1.TT.COMMA)) {
537
+ this.consume(types_1.TT.COMMA);
538
+ args.push(this.parseExpression());
539
+ }
540
+ }
541
+ if (!this.check(types_1.TT.RPAREN))
542
+ throw new types_1.SinthError("Expected ')' after arguments", this.peek().loc);
543
+ this.consume(types_1.TT.RPAREN);
544
+ expr = { kind: "call", callee: expr, args };
545
+ }
546
+ return expr;
547
+ }
548
+ // component definitions
549
+ parseFunctionDef() {
550
+ const loc = this.consume(types_1.TT.KW_FUNCTION).loc;
551
+ const name = this.consume(types_1.TT.IDENT).value;
552
+ if (name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase()) {
553
+ throw new types_1.SinthError(`Function '${name}' must start with lowercase. Use 'component' for uppercase names.`, loc);
554
+ }
555
+ const params = this.parseParamDecls();
556
+ let returnType;
557
+ if (this.check(types_1.TT.OP_ARROW)) {
558
+ this.consume(types_1.TT.OP_ARROW);
559
+ const typeTok = this.consume(types_1.TT.IDENT);
560
+ if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeTok.value)) {
561
+ throw new types_1.SinthError(`Unknown return type '${typeTok.value}'. Expected: int, str, bool, str[], obj, ui`, typeTok.loc);
562
+ }
563
+ returnType = typeTok.value;
564
+ }
565
+ if (!this.check(types_1.TT.LBRACE))
566
+ throw new types_1.SinthError("Expected '{' after function signature", this.peek().loc);
567
+ this.consume(types_1.TT.LBRACE);
568
+ const body = this.parseUnifiedBody();
569
+ this.consume(types_1.TT.RBRACE);
570
+ return { name, params, returnType, body, loc };
571
+ }
572
+ parseCompDef() {
573
+ const loc = this.consume(types_1.TT.KW_COMPONENT).loc;
574
+ const name = this.consume(types_1.TT.IDENT).value;
575
+ const params = this.parseParamDecls();
576
+ const rawTok = this.consume(types_1.TT.RAW_BLOCK);
577
+ const { body, styles, scripts } = this.parseCompBody(rawTok.value, rawTok.loc);
578
+ return { name, params, body, styles, scripts, loc };
579
+ }
580
+ parseCustomEl() {
581
+ const loc = this.peek().loc;
582
+ if (this.check(types_1.TT.KW_CUSTOM_EL))
583
+ this.consume(types_1.TT.KW_CUSTOM_EL);
584
+ else if (this.check(types_1.TT.KW_CUSTOM))
585
+ this.consume(types_1.TT.KW_CUSTOM);
586
+ const sinthName = this.consume(types_1.TT.IDENT).value;
587
+ let exportTag;
588
+ if (this.check(types_1.TT.LPAREN)) {
589
+ this.consume(types_1.TT.LPAREN);
590
+ while (!this.check(types_1.TT.RPAREN)) {
591
+ const attrName = this.consume(types_1.TT.IDENT).value;
592
+ if (attrName === "export") {
593
+ this.consume(types_1.TT.COLON);
594
+ if (!this.check(types_1.TT.OP_LT))
595
+ throw new types_1.SinthError("Expected '<tag/>' after 'export:'", this.peek().loc);
596
+ this.consume(types_1.TT.OP_LT);
597
+ exportTag = this.consume(types_1.TT.IDENT).value;
598
+ if (!this.check(types_1.TT.OP_SLASH))
599
+ throw new types_1.SinthError("Expected '/>' in tag literal", this.peek().loc);
600
+ this.consume(types_1.TT.OP_SLASH);
601
+ this.consume(types_1.TT.OP_GT);
602
+ }
603
+ if (!this.check(types_1.TT.RPAREN))
604
+ this.consume(types_1.TT.COMMA);
605
+ }
606
+ this.consume(types_1.TT.RPAREN);
607
+ }
608
+ if (this.check(types_1.TT.RAW_BLOCK)) {
609
+ const rawTok = this.consume(types_1.TT.RAW_BLOCK);
610
+ const tokens = new lexer_1.Lexer(rawTok.value, rawTok.loc.file).tokenize();
611
+ const sub = new Parser(tokens, rawTok.loc.file);
612
+ sub._varDecls = this._varDecls;
613
+ const parsed = sub.parseBodyContents();
614
+ return { sinthName, tagName: sinthName.toLowerCase(), exportTag, params: [], body: parsed.body, styles: parsed.styles, scripts: parsed.scripts, varDecls: this._varDecls, loc };
615
+ }
616
+ if (!this.check(types_1.TT.LBRACE))
617
+ throw new types_1.SinthError("Expected '{' after custom element declaration", this.peek().loc);
618
+ this.consume(types_1.TT.LBRACE);
619
+ const body = [];
620
+ const styles = [];
621
+ const scripts = [];
622
+ const varDecls = [];
623
+ while (!this.check(types_1.TT.RBRACE) && !this.check(types_1.TT.EOF)) {
624
+ if (this.check(types_1.TT.KW_STYLE)) {
625
+ styles.push(this.parseStyleBlock());
626
+ }
627
+ else if (this.check(types_1.TT.KW_SCRIPT)) {
628
+ scripts.push(this.parseScriptBlock());
629
+ }
630
+ else if (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
631
+ const vd = this.parseVarDeclaration();
632
+ if (vd)
633
+ varDecls.push(vd);
634
+ }
635
+ else if (this.check(types_1.TT.KW_IF)) {
636
+ body.push(this.parseIfBlock());
637
+ }
638
+ else if (this.check(types_1.TT.KW_FOR)) {
639
+ body.push(this.parseForLoop());
640
+ }
641
+ else if (this.check(types_1.TT.IDENT)) {
642
+ body.push(this.parseCompUse());
643
+ }
644
+ else if (this.check(types_1.TT.STRING)) {
645
+ body.push({ kind: "text", value: this.consume(types_1.TT.STRING).value, loc: this.peek().loc });
646
+ }
647
+ else
648
+ throw new types_1.SinthError(`Unexpected token in custom element body`, this.peek().loc);
649
+ }
650
+ this.consume(types_1.TT.RBRACE);
651
+ return { sinthName, tagName: sinthName.toLowerCase(), exportTag, params: [], body, styles, scripts, varDecls, loc };
652
+ }
653
+ parseReturnStmt() {
654
+ const loc = this.consume(types_1.TT.KW_RETURN).loc;
655
+ let expression;
656
+ if (!this.check(types_1.TT.RBRACE) && !this.check(types_1.TT.EOF) && !this.check(types_1.TT.KW_IF) && !this.check(types_1.TT.KW_FOR) && !this.check(types_1.TT.KW_RETURN)) {
657
+ expression = this.parseExpression() ?? undefined;
658
+ }
659
+ return { kind: "return", expression, loc };
660
+ }
661
+ parseParamDecls() {
662
+ const params = [];
663
+ if (!this.check(types_1.TT.LPAREN))
664
+ return params;
665
+ this.consume(types_1.TT.LPAREN);
666
+ while (!this.check(types_1.TT.RPAREN)) {
667
+ const ploc = this.peek().loc;
668
+ let pname;
669
+ let paramType;
670
+ if (this.check(types_1.TT.IDENT) && ["int", "str", "bool", "str[]", "obj"].includes(this.peek().value) &&
671
+ this.tokens[this.pos + 1]?.type === types_1.TT.IDENT) {
672
+ paramType = this.consume(types_1.TT.IDENT).value;
673
+ pname = this.consume(types_1.TT.IDENT).value;
674
+ }
675
+ else {
676
+ const nameTok = this.check(types_1.TT.IDENT) ? this.consume(types_1.TT.IDENT) : this.consume(this.peek().type);
677
+ pname = nameTok.value;
678
+ }
679
+ let defaultVal;
680
+ if (this.check(types_1.TT.EQUALS)) {
681
+ this.consume(types_1.TT.EQUALS);
682
+ defaultVal = this.parseLiteral();
683
+ }
684
+ params.push({ name: pname, paramType, defaultVal, loc: ploc });
685
+ if (!this.check(types_1.TT.RPAREN))
686
+ this.consume(types_1.TT.COMMA);
687
+ }
688
+ this.consume(types_1.TT.RPAREN);
689
+ return params;
690
+ }
691
+ parseCompBody(raw, loc) {
692
+ const tokens = new lexer_1.Lexer(raw, loc.file).tokenize();
693
+ const sub = new Parser(tokens, loc.file);
694
+ sub._varDecls = this._varDecls;
695
+ return sub.parseBodyContents();
696
+ }
697
+ parseBodyContents() {
698
+ const body = [], styles = [], scripts = [];
699
+ while (!this.check(types_1.TT.EOF)) {
700
+ if (this.check(types_1.TT.KW_STYLE)) {
701
+ styles.push(this.parseStyleBlock());
702
+ }
703
+ else if (this.check(types_1.TT.KW_SCRIPT)) {
704
+ scripts.push(this.parseScriptBlock());
705
+ }
706
+ else if (this.check(types_1.TT.STRING)) {
707
+ const loc = this.peek().loc;
708
+ body.push({ kind: "text", value: this.consume(types_1.TT.STRING).value, loc });
709
+ }
710
+ else if (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
711
+ const vd = this.parseVarDeclaration();
712
+ if (vd) {
713
+ this._varDecls.push(vd);
714
+ body.push(vd);
715
+ }
716
+ continue;
717
+ }
718
+ else if (this.check(types_1.TT.KW_RETURN)) {
719
+ body.push(this.parseReturnStmt());
720
+ }
721
+ else if (this.check(types_1.TT.KW_IF)) {
722
+ body.push(this.parseIfBlock());
723
+ }
724
+ else if (this.check(types_1.TT.KW_FOR)) {
725
+ body.push(this.parseForLoop());
726
+ }
727
+ else if (this.check(types_1.TT.IDENT)) {
728
+ body.push(this.parseCompUse());
729
+ }
730
+ else
731
+ throw new types_1.SinthError(`Unexpected token '${this.peek().value}' in component body`, this.peek().loc);
732
+ }
733
+ return { body, styles, scripts };
734
+ }
735
+ // if / else blocks
736
+ parseIfBlock() {
737
+ const loc = this.consume(types_1.TT.KW_IF).loc;
738
+ const condition = this.parseExpression();
739
+ if (!condition)
740
+ throw new types_1.SinthError("Expected condition after 'if'", this.peek().loc);
741
+ let persist = false;
742
+ if (this.check(types_1.TT.IDENT) && this.peek().value === "persist") {
743
+ this.consume(types_1.TT.IDENT);
744
+ persist = true;
745
+ }
746
+ this.consume(types_1.TT.LBRACE);
747
+ const body = this.parseUnifiedBody();
748
+ this.consume(types_1.TT.RBRACE);
749
+ const elseBody = this.parseElseChain();
750
+ return { kind: "if", condition, body, elseBody, persist, loc };
751
+ }
752
+ parseElseChain() {
753
+ if (!this.check(types_1.TT.KW_ELSE))
754
+ return undefined;
755
+ const elseTok = this.consume(types_1.TT.KW_ELSE);
756
+ if (this.check(types_1.TT.LBRACE)) {
757
+ this.consume(types_1.TT.LBRACE);
758
+ const body = this.parseUnifiedBody();
759
+ this.consume(types_1.TT.RBRACE);
760
+ return body;
761
+ }
762
+ if (this.check(types_1.TT.KW_IF) || elseTok.value === "elif") {
763
+ const ifBlock = this.parseIfContinue(elseTok);
764
+ return [ifBlock];
765
+ }
766
+ throw new types_1.SinthError("Expected '{', 'if', or 'elif' after 'else'", this.peek().loc);
767
+ }
768
+ parseIfContinue(elseTok) {
769
+ let condition;
770
+ if (elseTok.value === "elif") {
771
+ condition = this.parseExpression();
772
+ if (!condition)
773
+ throw new types_1.SinthError("Expected condition after 'elif'", this.peek().loc);
774
+ }
775
+ else {
776
+ this.consume(types_1.TT.KW_IF);
777
+ condition = this.parseExpression();
778
+ if (!condition)
779
+ throw new types_1.SinthError("Expected condition after 'if'", this.peek().loc);
780
+ }
781
+ if (!this.check(types_1.TT.LBRACE))
782
+ throw new types_1.SinthError("Expected '{' after condition", this.peek().loc);
783
+ this.consume(types_1.TT.LBRACE);
784
+ const body = this.parseUnifiedBody();
785
+ this.consume(types_1.TT.RBRACE);
786
+ const elseBody = this.parseElseChain();
787
+ return { kind: "if", condition, body, elseBody, loc: elseTok.loc };
788
+ }
789
+ parseRawIfStatement() {
790
+ this.consume(types_1.TT.KW_IF);
791
+ const cond = this.parseExpression();
792
+ if (!cond)
793
+ throw new types_1.SinthError("Expected condition after 'if'", this.peek().loc);
794
+ const condJS = (0, expr_1.compileExprToJS)(cond);
795
+ this.consume(types_1.TT.LBRACE);
796
+ let depth = 1;
797
+ const bodyTokens = [];
798
+ while (depth > 0 && !this.check(types_1.TT.EOF)) {
799
+ const t = this.peek();
800
+ if (t.type === types_1.TT.LBRACE)
801
+ depth++;
802
+ else if (t.type === types_1.TT.RBRACE) {
803
+ depth--;
804
+ if (depth === 0)
805
+ break;
806
+ }
807
+ bodyTokens.push(this.consume(t.type));
808
+ }
809
+ this.consume(types_1.TT.RBRACE);
810
+ const rawBody = bodyTokens.map(t => {
811
+ if (t.type === types_1.TT.STRING)
812
+ return '"' + t.value + '"';
813
+ return t.value;
814
+ }).join(' ');
815
+ let result = `if (${condJS}) { ${rawBody} }`;
816
+ if (this.check(types_1.TT.KW_ELSE)) {
817
+ this.consume(types_1.TT.KW_ELSE);
818
+ if (this.check(types_1.TT.KW_IF)) {
819
+ const elseIfJS = this.parseRawIfStatement();
820
+ result += ` else ${elseIfJS}`;
821
+ }
822
+ else {
823
+ this.consume(types_1.TT.LBRACE);
824
+ depth = 1;
825
+ const elseTokens = [];
826
+ while (depth > 0 && !this.check(types_1.TT.EOF)) {
827
+ const t = this.peek();
828
+ if (t.type === types_1.TT.LBRACE)
829
+ depth++;
830
+ else if (t.type === types_1.TT.RBRACE) {
831
+ depth--;
832
+ if (depth === 0)
833
+ break;
834
+ }
835
+ elseTokens.push(this.consume(t.type));
836
+ }
837
+ this.consume(types_1.TT.RBRACE);
838
+ const elseBody = elseTokens.map(t => {
839
+ if (t.type === types_1.TT.STRING)
840
+ return '"' + t.value + '"';
841
+ return t.value;
842
+ }).join(' ');
843
+ result += ` else { ${elseBody} }`;
844
+ }
845
+ }
846
+ return result;
847
+ }
848
+ parseUnifiedBody() {
849
+ const children = [];
850
+ while (!this.check(types_1.TT.RBRACE) && !this.check(types_1.TT.EOF)) {
851
+ if (this.check(types_1.TT.KW_RETURN)) {
852
+ children.push(this.parseReturnStmt());
853
+ continue;
854
+ }
855
+ if (this.check(types_1.TT.KW_IF)) {
856
+ children.push(this.parseIfBlock());
857
+ continue;
858
+ }
859
+ if (this.check(types_1.TT.KW_FOR)) {
860
+ children.push(this.parseForLoop());
861
+ continue;
862
+ }
863
+ if (this.check(types_1.TT.LPAREN)) {
864
+ const loc = this.peek().loc;
865
+ const expr = this.parseExpression();
866
+ if (expr) {
867
+ children.push({ kind: "expr", expression: expr, loc });
868
+ }
869
+ continue;
870
+ }
871
+ if (this.check(types_1.TT.IDENT)) {
872
+ const savedPos = this.pos;
873
+ const name = this.peek().value;
874
+ const nextType = this.tokens[this.pos + 1]?.type;
875
+ if (nextType === types_1.TT.EQUALS || (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS) || (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS)) {
876
+ this.consume(types_1.TT.IDENT);
877
+ let op = "=";
878
+ if (this.check(types_1.TT.OP_PLUS)) {
879
+ this.consume(types_1.TT.OP_PLUS);
880
+ op = "+=";
881
+ }
882
+ else if (this.check(types_1.TT.OP_MINUS)) {
883
+ this.consume(types_1.TT.OP_MINUS);
884
+ op = "-=";
885
+ }
886
+ this.consume(types_1.TT.EQUALS);
887
+ const rhs = this.parseExpression();
888
+ if (!rhs)
889
+ throw new types_1.SinthError("Expected expression after assignment", this.peek().loc);
890
+ children.push({ kind: "assign_stmt", expression: { kind: "assign", target: name, op, right: rhs }, loc: this.peek().loc });
891
+ continue;
892
+ }
893
+ this.pos = savedPos;
894
+ }
895
+ if (this.check(types_1.TT.STRING)) {
896
+ const loc = this.peek().loc;
897
+ let leftExpr = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
898
+ while (this.check(types_1.TT.OP_PLUS)) {
899
+ this.consume(types_1.TT.OP_PLUS);
900
+ let rhs = null;
901
+ if (this.check(types_1.TT.STRING)) {
902
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
903
+ }
904
+ else if (this.check(types_1.TT.IDENT)) {
905
+ const rhsName = this.consume(types_1.TT.IDENT).value;
906
+ rhs = { kind: "variable", name: rhsName };
907
+ }
908
+ else {
909
+ rhs = this.parseExpression(true);
910
+ }
911
+ if (!rhs)
912
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
913
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
914
+ }
915
+ children.push({ kind: "expr", expression: leftExpr, loc });
916
+ continue;
917
+ }
918
+ if (this.check(types_1.TT.KW_REMOVE)) {
919
+ const loc = this.consume(types_1.TT.KW_REMOVE).loc;
920
+ if (this.check(types_1.TT.STRING)) {
921
+ const target = this.consume(types_1.TT.STRING).value;
922
+ children.push({ kind: "remove", target, loc });
923
+ }
924
+ else {
925
+ throw new types_1.SinthError(`Expected a string after 'remove', got '${this.peek().value}'. Use remove "id".`, this.peek().loc);
926
+ }
927
+ continue;
928
+ }
929
+ if (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
930
+ const vd = this.parseVarDeclaration();
931
+ if (vd) {
932
+ this._varDecls.push(vd);
933
+ children.push(vd);
934
+ }
935
+ continue;
936
+ }
937
+ if (this.check(types_1.TT.IDENT)) {
938
+ const loc = this.peek().loc;
939
+ const name = this.peek().value;
940
+ const nextType = this.tokens[this.pos + 1]?.type;
941
+ const nextNextType = this.tokens[this.pos + 2]?.type;
942
+ if (nextType === types_1.TT.DOT && (nextNextType === types_1.TT.EQUALS ||
943
+ (nextNextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 3]?.type === types_1.TT.EQUALS) ||
944
+ (nextNextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 3]?.type === types_1.TT.EQUALS))) {
945
+ let fullName = this.consume(types_1.TT.IDENT).value;
946
+ this.consume(types_1.TT.DOT);
947
+ fullName = fullName + "." + this.consume(types_1.TT.IDENT).value;
948
+ let op = "=";
949
+ const afterDotType = this.tokens[this.pos]?.type;
950
+ if (afterDotType === types_1.TT.OP_PLUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
951
+ this.consume(types_1.TT.OP_PLUS);
952
+ op = "+=";
953
+ }
954
+ else if (afterDotType === types_1.TT.OP_MINUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
955
+ this.consume(types_1.TT.OP_MINUS);
956
+ op = "-=";
957
+ }
958
+ this.consume(types_1.TT.EQUALS);
959
+ const rhs = this.parseExpression();
960
+ if (!rhs)
961
+ throw new types_1.SinthError("Expected expression after assignment", this.peek().loc);
962
+ const assignExpr = { kind: "assign", target: fullName, op, right: rhs };
963
+ children.push({ kind: "assign_stmt", expression: assignExpr, loc });
964
+ continue;
965
+ }
966
+ // assignment: ident = expr | ident += expr | ident -= expr
967
+ const isSimpleAssign = nextType === types_1.TT.EQUALS;
968
+ const isPlusAssign = nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS;
969
+ const isMinusAssign = nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS;
970
+ if (isSimpleAssign || isPlusAssign || isMinusAssign) {
971
+ this.consume(types_1.TT.IDENT); // consume name
972
+ let op = "=";
973
+ if (isPlusAssign) {
974
+ this.consume(types_1.TT.OP_PLUS);
975
+ op = "+=";
976
+ }
977
+ else if (isMinusAssign) {
978
+ this.consume(types_1.TT.OP_MINUS);
979
+ op = "-=";
980
+ }
981
+ this.consume(types_1.TT.EQUALS);
982
+ const rhs = this.parseExpression();
983
+ if (!rhs)
984
+ throw new types_1.SinthError("Expected expression after assignment", this.peek().loc);
985
+ const assignExpr = { kind: "assign", target: name, op, right: rhs };
986
+ children.push({ kind: "assign_stmt", expression: assignExpr, loc });
987
+ continue;
988
+ }
989
+ if (nextType === types_1.TT.LPAREN) {
990
+ const identName = this.peek().value;
991
+ const isMemo = identName.startsWith("$");
992
+ const realName = isMemo ? identName.substring(1) : identName;
993
+ if (realName[0] === realName[0].toUpperCase() && realName[0] !== realName[0].toLowerCase()) {
994
+ children.push(this.parseCompUse());
995
+ }
996
+ else {
997
+ this.consume(types_1.TT.IDENT);
998
+ this.consume(types_1.TT.LPAREN);
999
+ const args = [];
1000
+ if (!this.check(types_1.TT.RPAREN)) {
1001
+ args.push(this.parseExpression());
1002
+ while (this.check(types_1.TT.COMMA)) {
1003
+ this.consume(types_1.TT.COMMA);
1004
+ args.push(this.parseExpression());
1005
+ }
1006
+ }
1007
+ this.consume(types_1.TT.RPAREN);
1008
+ const callExpr = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
1009
+ children.push({ kind: "expr", expression: callExpr, loc });
1010
+ }
1011
+ continue;
1012
+ }
1013
+ if (nextType === types_1.TT.LBRACE || nextType === types_1.TT.RAW_BLOCK) {
1014
+ children.push(this.parseCompUse());
1015
+ continue;
1016
+ }
1017
+ // var expression
1018
+ // check dot notation variable reference (user.age without assignment)
1019
+ if (nextType === types_1.TT.DOT) {
1020
+ let fullName = this.consume(types_1.TT.IDENT).value;
1021
+ this.consume(types_1.TT.DOT);
1022
+ fullName = fullName + "." + this.consume(types_1.TT.IDENT).value;
1023
+ if (this.check(types_1.TT.LPAREN)) {
1024
+ this.consume(types_1.TT.LPAREN);
1025
+ const args = [];
1026
+ if (!this.check(types_1.TT.RPAREN)) {
1027
+ args.push(this.parseExpression());
1028
+ while (this.check(types_1.TT.COMMA)) {
1029
+ this.consume(types_1.TT.COMMA);
1030
+ args.push(this.parseExpression());
1031
+ }
1032
+ }
1033
+ this.consume(types_1.TT.RPAREN);
1034
+ children.push({ kind: "expr", expression: { kind: "call", callee: { kind: "variable", name: fullName }, args }, loc });
1035
+ }
1036
+ else {
1037
+ children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
1038
+ }
1039
+ continue;
1040
+ }
1041
+ // bracket notation: ident[...]
1042
+ if (nextType === types_1.TT.LBRACKET) {
1043
+ this.consume(types_1.TT.IDENT);
1044
+ const varExpr = { kind: "variable", name };
1045
+ const indexedExpr = this.parsePostfix(varExpr);
1046
+ children.push({ kind: "expr", expression: indexedExpr, loc });
1047
+ continue;
1048
+ }
1049
+ // simple variable reference
1050
+ this.consume(types_1.TT.IDENT);
1051
+ children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
1052
+ continue;
1053
+ }
1054
+ // reject implicit concatenation only after expression children
1055
+ if (children.length > 0) {
1056
+ const lastChild = children[children.length - 1];
1057
+ if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
1058
+ const nextTT = this.tokens[this.pos]?.type;
1059
+ if (nextTT === types_1.TT.STRING || nextTT === types_1.TT.NUMBER ||
1060
+ nextTT === types_1.TT.BOOL_TRUE || nextTT === types_1.TT.BOOL_FALSE || nextTT === types_1.TT.NULL_LIT ||
1061
+ (nextTT === types_1.TT.IDENT &&
1062
+ this.tokens[this.pos]?.value !== "var" &&
1063
+ this.tokens[this.pos]?.value !== "if" &&
1064
+ this.tokens[this.pos]?.value !== "for" &&
1065
+ this.tokens[this.pos]?.value !== "else" &&
1066
+ this.tokens[this.pos]?.value !== "and" &&
1067
+ this.tokens[this.pos]?.value !== "or" &&
1068
+ this.tokens[this.pos]?.value[0] !== this.tokens[this.pos]?.value[0]?.toUpperCase())) {
1069
+ throw new types_1.SinthError(`Unexpected ${types_1.TT[nextTT]}. Use '+' to concatenate values.`, this.peek().loc);
1070
+ }
1071
+ }
1072
+ }
1073
+ }
1074
+ return children;
1075
+ }
1076
+ parseForLoop() {
1077
+ const loc = this.consume(types_1.TT.KW_FOR).loc;
1078
+ const firstVar = this.consume(types_1.TT.IDENT).value;
1079
+ let keyVar;
1080
+ let itemVar;
1081
+ let indexVar;
1082
+ if (this.check(types_1.TT.COMMA)) {
1083
+ this.consume(types_1.TT.COMMA);
1084
+ const secondVar = this.consume(types_1.TT.IDENT).value;
1085
+ if (this.check(types_1.TT.COMMA)) {
1086
+ this.consume(types_1.TT.COMMA);
1087
+ keyVar = firstVar;
1088
+ itemVar = secondVar;
1089
+ indexVar = this.consume(types_1.TT.IDENT).value;
1090
+ }
1091
+ else {
1092
+ itemVar = firstVar;
1093
+ indexVar = secondVar;
1094
+ }
1095
+ }
1096
+ else {
1097
+ itemVar = firstVar;
1098
+ }
1099
+ if (!this.check(types_1.TT.KW_IN))
1100
+ throw new types_1.SinthError("Expected 'in' after loop variable", this.peek().loc);
1101
+ this.consume(types_1.TT.KW_IN);
1102
+ let arrayVar = this.consume(types_1.TT.IDENT).value;
1103
+ while (this.check(types_1.TT.DOT)) {
1104
+ this.consume(types_1.TT.DOT);
1105
+ const propName = this.consume(types_1.TT.IDENT).value;
1106
+ if (this.check(types_1.TT.LPAREN)) {
1107
+ this.consume(types_1.TT.LPAREN);
1108
+ const args = [];
1109
+ if (!this.check(types_1.TT.RPAREN)) {
1110
+ if (this.check(types_1.TT.STRING))
1111
+ args.push(this.consume(types_1.TT.STRING).value);
1112
+ else if (this.check(types_1.TT.NUMBER))
1113
+ args.push(this.consume(types_1.TT.NUMBER).value);
1114
+ while (this.check(types_1.TT.COMMA)) {
1115
+ this.consume(types_1.TT.COMMA);
1116
+ if (this.check(types_1.TT.STRING))
1117
+ args.push(this.consume(types_1.TT.STRING).value);
1118
+ else if (this.check(types_1.TT.NUMBER))
1119
+ args.push(this.consume(types_1.TT.NUMBER).value);
1120
+ }
1121
+ }
1122
+ this.consume(types_1.TT.RPAREN);
1123
+ arrayVar = arrayVar + "." + propName + "(" + args.map(a => JSON.stringify(a)).join(",") + ")";
1124
+ }
1125
+ else {
1126
+ arrayVar = arrayVar + "." + propName;
1127
+ }
1128
+ }
1129
+ if (keyVar === undefined && indexVar !== undefined) {
1130
+ const srcDecl = this._varDecls.find(d => d.name === arrayVar);
1131
+ if (srcDecl && srcDecl.varType === "obj") {
1132
+ const val = srcDecl.value;
1133
+ const isArray = val && val.kind === "str" && val.value.startsWith("__ARR__");
1134
+ if (!isArray) {
1135
+ keyVar = itemVar;
1136
+ itemVar = indexVar;
1137
+ indexVar = undefined;
1138
+ }
1139
+ }
1140
+ }
1141
+ if (!this.check(types_1.TT.LBRACE))
1142
+ throw new types_1.SinthError("Expected '{' after for..in expression", this.peek().loc);
1143
+ this.consume(types_1.TT.LBRACE);
1144
+ this.loopVar = itemVar;
1145
+ const body = this.parseChildList();
1146
+ this.loopVar = null;
1147
+ this.consume(types_1.TT.RBRACE);
1148
+ return { kind: "for", keyVar, itemVar, indexVar, arrayVar, body, loc };
1149
+ }
1150
+ parseCompUse() {
1151
+ const loc = this.peek().loc;
1152
+ const name = this.consume(types_1.TT.IDENT).value;
1153
+ const attrs = [];
1154
+ if (this.check(types_1.TT.LPAREN)) {
1155
+ this.consume(types_1.TT.LPAREN);
1156
+ while (!this.check(types_1.TT.RPAREN)) {
1157
+ const aloc = this.peek().loc;
1158
+ const aname = this.consume(types_1.TT.IDENT).value;
1159
+ let value = null;
1160
+ if (this.check(types_1.TT.COLON)) {
1161
+ this.consume(types_1.TT.COLON);
1162
+ if (this.isEventAttr(aname)) {
1163
+ if (this.check(types_1.TT.STRING)) {
1164
+ value = this.parseLiteral();
1165
+ }
1166
+ else {
1167
+ const savedPos = this.pos;
1168
+ const expr = this.parseExpression();
1169
+ if (expr && (this.check(types_1.TT.COMMA) || this.check(types_1.TT.RPAREN) || this.check(types_1.TT.OP_SEMI) || this.check(types_1.TT.KW_IF))) {
1170
+ const actions = [{ kind: "expr", expr }];
1171
+ while (this.check(types_1.TT.OP_SEMI)) {
1172
+ this.consume(types_1.TT.OP_SEMI);
1173
+ if (this.check(types_1.TT.KW_IF)) {
1174
+ const jsIf = this.parseRawIfStatement();
1175
+ actions.push({ kind: "if", js: jsIf });
1176
+ }
1177
+ else {
1178
+ const next = this.parseExpression();
1179
+ if (next)
1180
+ actions.push({ kind: "expr", expr: next });
1181
+ else
1182
+ break;
1183
+ }
1184
+ }
1185
+ const hasIf = actions.some(a => a.kind === "if");
1186
+ if (actions.length === 1 && !hasIf) {
1187
+ const single = actions[0].expr;
1188
+ value = { kind: "str", value: "__EXPR__" + JSON.stringify(single) };
1189
+ }
1190
+ else {
1191
+ const partsJS = [];
1192
+ for (const a of actions) {
1193
+ if (a.kind === "if") {
1194
+ partsJS.push(a.js);
1195
+ }
1196
+ else {
1197
+ partsJS.push((0, expr_1.compileExprToJS)(a.expr) + ";");
1198
+ }
1199
+ }
1200
+ value = { kind: "str", value: "__ACTION_JS__" + partsJS.join(" ") };
1201
+ }
1202
+ }
1203
+ else {
1204
+ this.pos = savedPos;
1205
+ value = this.parseLiteral();
1206
+ }
1207
+ }
1208
+ }
1209
+ else {
1210
+ if (this.check(types_1.TT.STRING) || this.check(types_1.TT.NUMBER) ||
1211
+ this.check(types_1.TT.BOOL_TRUE) || this.check(types_1.TT.BOOL_FALSE) ||
1212
+ this.check(types_1.TT.NULL_LIT) || this.check(types_1.TT.LBRACKET) || this.check(types_1.TT.LBRACE)) {
1213
+ value = this.parseLiteral();
1214
+ }
1215
+ else if (this.check(types_1.TT.IDENT) || this.check(types_1.TT.LPAREN) || this.check(types_1.TT.NUMBER)) {
1216
+ const savedPos = this.pos;
1217
+ const expr = this.parseExpression();
1218
+ if (expr && (this.check(types_1.TT.COMMA) || this.check(types_1.TT.RPAREN))) {
1219
+ value = { kind: "str", value: "__EXPR__" + JSON.stringify(expr) };
1220
+ }
1221
+ else {
1222
+ this.pos = savedPos;
1223
+ if (this.check(types_1.TT.IDENT)) {
1224
+ value = { kind: "str", value: this.consume(types_1.TT.IDENT).value };
1225
+ }
1226
+ else {
1227
+ value = this.parseLiteral();
1228
+ }
1229
+ }
1230
+ }
1231
+ else {
1232
+ value = this.parseLiteral();
1233
+ }
1234
+ }
1235
+ }
1236
+ attrs.push({ name: aname, value, loc: aloc });
1237
+ if (!this.check(types_1.TT.RPAREN))
1238
+ this.consume(types_1.TT.COMMA);
1239
+ }
1240
+ this.consume(types_1.TT.RPAREN);
1241
+ }
1242
+ const children = [];
1243
+ if (this.check(types_1.TT.RAW_BLOCK)) {
1244
+ const rawTok = this.consume(types_1.TT.RAW_BLOCK);
1245
+ const tokens = new lexer_1.Lexer(rawTok.value, rawTok.loc.file).tokenize();
1246
+ const sub = new Parser(tokens, rawTok.loc.file);
1247
+ sub._varDecls = this._varDecls;
1248
+ children.push(...sub.parseChildList());
1249
+ }
1250
+ else if (this.check(types_1.TT.LBRACE)) {
1251
+ this.consume(types_1.TT.LBRACE);
1252
+ children.push(...this.parseChildList());
1253
+ this.consume(types_1.TT.RBRACE);
1254
+ }
1255
+ return { kind: "use", name, attrs, children, loc };
1256
+ }
1257
+ isEventAttr(name) {
1258
+ return name.startsWith("on") && name.length > 2 && name[2] === name[2].toUpperCase();
1259
+ }
1260
+ parseChildList() {
1261
+ const children = [];
1262
+ let _iter = 0;
1263
+ while (!this.check(types_1.TT.EOF) && !this.check(types_1.TT.RBRACE)) {
1264
+ if (++_iter > 1000)
1265
+ throw new types_1.SinthError("Infinite loop in children", this.peek().loc);
1266
+ if (this.check(types_1.TT.LPAREN)) {
1267
+ const loc = this.peek().loc;
1268
+ this.consume(types_1.TT.LPAREN);
1269
+ let expr = this.parseExpression(true);
1270
+ if (!expr)
1271
+ throw new types_1.SinthError("Expected expression after '('", this.peek().loc);
1272
+ if (!this.check(types_1.TT.RPAREN))
1273
+ throw new types_1.SinthError("Expected ')'", this.peek().loc);
1274
+ this.consume(types_1.TT.RPAREN);
1275
+ while (this.check(types_1.TT.OP_PLUS)) {
1276
+ this.consume(types_1.TT.OP_PLUS);
1277
+ let rhs = null;
1278
+ if (this.check(types_1.TT.STRING)) {
1279
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1280
+ }
1281
+ else if (this.check(types_1.TT.IDENT)) {
1282
+ const rhsName = this.consume(types_1.TT.IDENT).value;
1283
+ rhs = { kind: "variable", name: rhsName };
1284
+ }
1285
+ else {
1286
+ rhs = this.parseExpression(true);
1287
+ }
1288
+ if (!rhs)
1289
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1290
+ expr = { kind: "binary", left: expr, op: "+", right: rhs };
1291
+ }
1292
+ children.push({ kind: "expr", expression: expr, loc });
1293
+ continue;
1294
+ }
1295
+ if (this.check(types_1.TT.STRING)) {
1296
+ const loc = this.peek().loc;
1297
+ let leftExpr = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1298
+ while (this.check(types_1.TT.OP_PLUS)) {
1299
+ this.consume(types_1.TT.OP_PLUS);
1300
+ let rhs = null;
1301
+ if (this.check(types_1.TT.STRING)) {
1302
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1303
+ }
1304
+ else if (this.check(types_1.TT.IDENT)) {
1305
+ const rhsName = this.consume(types_1.TT.IDENT).value;
1306
+ if (this.check(types_1.TT.LPAREN)) {
1307
+ this.consume(types_1.TT.LPAREN);
1308
+ const args = [];
1309
+ if (!this.check(types_1.TT.RPAREN)) {
1310
+ args.push(this.parseExpression());
1311
+ while (this.check(types_1.TT.COMMA)) {
1312
+ this.consume(types_1.TT.COMMA);
1313
+ args.push(this.parseExpression());
1314
+ }
1315
+ }
1316
+ this.consume(types_1.TT.RPAREN);
1317
+ rhs = { kind: "call", callee: { kind: "variable", name: rhsName }, args };
1318
+ }
1319
+ else if (this.check(types_1.TT.DOT)) {
1320
+ this.consume(types_1.TT.DOT);
1321
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(types_1.TT.IDENT).value };
1322
+ }
1323
+ else {
1324
+ rhs = { kind: "variable", name: rhsName };
1325
+ }
1326
+ }
1327
+ else {
1328
+ rhs = this.parseExpression(true);
1329
+ }
1330
+ if (!rhs)
1331
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1332
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
1333
+ }
1334
+ children.push({ kind: "expr", expression: leftExpr, loc });
1335
+ }
1336
+ else if (this.check(types_1.TT.IDENT) && this.peek().value === "var") {
1337
+ const vd = this.parseVarDeclaration();
1338
+ if (vd) {
1339
+ this._varDecls.push(vd);
1340
+ children.push(vd);
1341
+ }
1342
+ continue;
1343
+ }
1344
+ else if (this.check(types_1.TT.KW_RETURN)) {
1345
+ children.push(this.parseReturnStmt());
1346
+ continue;
1347
+ }
1348
+ else if (this.check(types_1.TT.KW_IF)) {
1349
+ children.push(this.parseIfBlock());
1350
+ continue;
1351
+ }
1352
+ else if (this.check(types_1.TT.KW_FOR)) {
1353
+ children.push(this.parseForLoop());
1354
+ continue;
1355
+ }
1356
+ else if (this.check(types_1.TT.IDENT)) {
1357
+ const loc = this.peek().loc;
1358
+ const rawName = this.peek().value;
1359
+ const nextType = this.tokens[this.pos + 1]?.type;
1360
+ if (nextType === types_1.TT.EQUALS || (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS) || (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 2]?.type === types_1.TT.EQUALS)) {
1361
+ this.consume(types_1.TT.IDENT);
1362
+ let op = "=";
1363
+ if (this.check(types_1.TT.OP_PLUS)) {
1364
+ this.consume(types_1.TT.OP_PLUS);
1365
+ op = "+=";
1366
+ }
1367
+ else if (this.check(types_1.TT.OP_MINUS)) {
1368
+ this.consume(types_1.TT.OP_MINUS);
1369
+ op = "-=";
1370
+ }
1371
+ this.consume(types_1.TT.EQUALS);
1372
+ const rhs = this.parseExpression();
1373
+ if (!rhs)
1374
+ throw new types_1.SinthError("Expected expression after assignment", this.peek().loc);
1375
+ children.push({ kind: "assign_stmt", expression: { kind: "assign", target: rawName, op, right: rhs }, loc });
1376
+ continue;
1377
+ }
1378
+ if (nextType === types_1.TT.DOT) {
1379
+ let rawFirst = this.consume(types_1.TT.IDENT).value;
1380
+ const isMemo = rawFirst.startsWith("$");
1381
+ const firstPart = isMemo ? rawFirst.slice(1) : rawFirst;
1382
+ this.consume(types_1.TT.DOT);
1383
+ const fullName = firstPart + "." + this.consume(types_1.TT.IDENT).value;
1384
+ const afterDotType = this.tokens[this.pos]?.type;
1385
+ if (afterDotType === types_1.TT.OP_PLUS) {
1386
+ let leftExpr = { kind: "variable", name: fullName };
1387
+ while (this.check(types_1.TT.OP_PLUS)) {
1388
+ this.consume(types_1.TT.OP_PLUS);
1389
+ let rhs = null;
1390
+ if (this.check(types_1.TT.STRING)) {
1391
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1392
+ }
1393
+ else if (this.check(types_1.TT.IDENT)) {
1394
+ const rhsName = this.consume(types_1.TT.IDENT).value;
1395
+ if (this.check(types_1.TT.DOT)) {
1396
+ this.consume(types_1.TT.DOT);
1397
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(types_1.TT.IDENT).value };
1398
+ }
1399
+ else {
1400
+ rhs = { kind: "variable", name: rhsName };
1401
+ }
1402
+ }
1403
+ else {
1404
+ rhs = this.parseExpression(true);
1405
+ }
1406
+ if (!rhs)
1407
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1408
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
1409
+ }
1410
+ children.push({ kind: "expr", expression: leftExpr, loc });
1411
+ }
1412
+ else if (this.loopVar && fullName.split('.')[0] === this.loopVar) {
1413
+ children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
1414
+ }
1415
+ else if (afterDotType === types_1.TT.LPAREN) {
1416
+ this.consume(types_1.TT.LPAREN);
1417
+ const args = [];
1418
+ if (!this.check(types_1.TT.RPAREN)) {
1419
+ args.push(this.parseExpression());
1420
+ while (this.check(types_1.TT.COMMA)) {
1421
+ this.consume(types_1.TT.COMMA);
1422
+ args.push(this.parseExpression());
1423
+ }
1424
+ }
1425
+ this.consume(types_1.TT.RPAREN);
1426
+ const callExpr = { kind: "call", callee: { kind: "variable", name: fullName }, args, memo: isMemo || undefined };
1427
+ children.push({ kind: "expr", expression: callExpr, loc });
1428
+ }
1429
+ else if (afterDotType === types_1.TT.LBRACE || afterDotType === types_1.TT.RAW_BLOCK) {
1430
+ throw new types_1.SinthError(`Unexpected dot notation before component usage`, loc);
1431
+ }
1432
+ else {
1433
+ const varExpr = { kind: "variable", name: fullName };
1434
+ if (isMemo)
1435
+ varExpr.memo = true;
1436
+ children.push({ kind: "expr", expression: varExpr, loc });
1437
+ }
1438
+ continue;
1439
+ }
1440
+ else if (nextType === types_1.TT.OP_PLUS) {
1441
+ const name = this.consume(types_1.TT.IDENT).value;
1442
+ let leftExpr = { kind: "variable", name };
1443
+ while (this.check(types_1.TT.OP_PLUS)) {
1444
+ this.consume(types_1.TT.OP_PLUS);
1445
+ let rhs = null;
1446
+ if (this.check(types_1.TT.STRING)) {
1447
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1448
+ }
1449
+ else if (this.check(types_1.TT.IDENT)) {
1450
+ const rhsName = this.consume(types_1.TT.IDENT).value;
1451
+ if (this.check(types_1.TT.DOT)) {
1452
+ this.consume(types_1.TT.DOT);
1453
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(types_1.TT.IDENT).value };
1454
+ }
1455
+ else {
1456
+ rhs = { kind: "variable", name: rhsName };
1457
+ }
1458
+ }
1459
+ else {
1460
+ rhs = this.parseExpression(true);
1461
+ }
1462
+ if (!rhs)
1463
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1464
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
1465
+ }
1466
+ children.push({ kind: "expr", expression: leftExpr, loc });
1467
+ }
1468
+ else if (this.loopVar && rawName === this.loopVar) {
1469
+ this.consume(types_1.TT.IDENT);
1470
+ children.push({ kind: "expr", expression: { kind: "variable", name: rawName }, loc });
1471
+ }
1472
+ else if (nextType === types_1.TT.LBRACKET) {
1473
+ const name = this.consume(types_1.TT.IDENT).value;
1474
+ const varExpr = { kind: "variable", name };
1475
+ let indexedExpr = this.parsePostfix(varExpr);
1476
+ if (this.check(types_1.TT.OP_PLUS)) {
1477
+ let leftExpr = indexedExpr;
1478
+ while (this.check(types_1.TT.OP_PLUS)) {
1479
+ this.consume(types_1.TT.OP_PLUS);
1480
+ let rhs = null;
1481
+ if (this.check(types_1.TT.STRING)) {
1482
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1483
+ }
1484
+ else if (this.check(types_1.TT.IDENT)) {
1485
+ const rhsName = this.consume(types_1.TT.IDENT).value;
1486
+ if (this.check(types_1.TT.DOT)) {
1487
+ this.consume(types_1.TT.DOT);
1488
+ rhs = { kind: "variable", name: rhsName + "." + this.consume(types_1.TT.IDENT).value };
1489
+ }
1490
+ else {
1491
+ rhs = { kind: "variable", name: rhsName };
1492
+ }
1493
+ }
1494
+ else {
1495
+ rhs = this.parseExpression(true);
1496
+ }
1497
+ if (!rhs)
1498
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1499
+ leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
1500
+ }
1501
+ children.push({ kind: "expr", expression: leftExpr, loc });
1502
+ }
1503
+ else {
1504
+ children.push({ kind: "expr", expression: indexedExpr, loc });
1505
+ }
1506
+ }
1507
+ else if (nextType === types_1.TT.LPAREN) {
1508
+ const identName = this.peek().value;
1509
+ const isMemo = identName.startsWith("$");
1510
+ const realName = isMemo ? identName.substring(1) : identName;
1511
+ if (realName[0] === realName[0].toUpperCase() && realName[0] !== realName[0].toLowerCase()) {
1512
+ children.push(this.parseCompUse());
1513
+ continue;
1514
+ }
1515
+ else {
1516
+ this.consume(types_1.TT.IDENT);
1517
+ this.consume(types_1.TT.LPAREN);
1518
+ const args = [];
1519
+ if (!this.check(types_1.TT.RPAREN)) {
1520
+ args.push(this.parseExpression());
1521
+ while (this.check(types_1.TT.COMMA)) {
1522
+ this.consume(types_1.TT.COMMA);
1523
+ args.push(this.parseExpression());
1524
+ }
1525
+ }
1526
+ this.consume(types_1.TT.RPAREN);
1527
+ let finalExpr = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
1528
+ while (this.check(types_1.TT.OP_PLUS)) {
1529
+ this.consume(types_1.TT.OP_PLUS);
1530
+ let rhs = null;
1531
+ if (this.check(types_1.TT.STRING)) {
1532
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
1533
+ }
1534
+ else if (this.check(types_1.TT.IDENT)) {
1535
+ rhs = this.parseExpression();
1536
+ }
1537
+ else {
1538
+ rhs = this.parseExpression(true);
1539
+ }
1540
+ if (!rhs)
1541
+ throw new types_1.SinthError("Expected expression after +", this.peek().loc);
1542
+ finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
1543
+ }
1544
+ children.push({ kind: "expr", expression: finalExpr, loc });
1545
+ continue;
1546
+ }
1547
+ }
1548
+ else if (nextType === types_1.TT.LBRACE || nextType === types_1.TT.RAW_BLOCK) {
1549
+ children.push(this.parseCompUse());
1550
+ continue;
1551
+ }
1552
+ else {
1553
+ const name = this.consume(types_1.TT.IDENT).value;
1554
+ children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
1555
+ }
1556
+ }
1557
+ // reject implicit concatenation only after expression children
1558
+ if (children.length > 0) {
1559
+ const lastChild = children[children.length - 1];
1560
+ if (lastChild.kind === "text" || lastChild.kind === "expr" || lastChild.kind === "assign_stmt") {
1561
+ const nextTT = this.tokens[this.pos]?.type;
1562
+ if (nextTT === types_1.TT.STRING || nextTT === types_1.TT.NUMBER ||
1563
+ nextTT === types_1.TT.BOOL_TRUE || nextTT === types_1.TT.BOOL_FALSE || nextTT === types_1.TT.NULL_LIT ||
1564
+ (nextTT === types_1.TT.IDENT &&
1565
+ this.tokens[this.pos]?.value !== "var" &&
1566
+ this.tokens[this.pos]?.value !== "if" &&
1567
+ this.tokens[this.pos]?.value !== "for" &&
1568
+ this.tokens[this.pos]?.value !== "else" &&
1569
+ this.tokens[this.pos]?.value !== "and" &&
1570
+ this.tokens[this.pos]?.value !== "or" &&
1571
+ this.tokens[this.pos]?.value[0] !== this.tokens[this.pos]?.value[0]?.toUpperCase())) {
1572
+ throw new types_1.SinthError(`Unexpected ${types_1.TT[nextTT]}. Use '+' to concatenate values.`, this.peek().loc);
1573
+ }
1574
+ }
1575
+ }
1576
+ if (!this.check(types_1.TT.RBRACE) && !this.check(types_1.TT.EOF)) {
1577
+ throw new types_1.SinthError(`Unexpected token '${this.peek().value}' in children`, this.peek().loc);
1578
+ }
1579
+ }
1580
+ return children;
1581
+ }
1582
+ // style & script blocks
1583
+ parseStyleBlock() {
1584
+ const loc = this.consume(types_1.TT.KW_STYLE).loc;
1585
+ let global = false, lang = "css", target;
1586
+ const extraAttrs = {};
1587
+ while (!this.check(types_1.TT.RAW_BLOCK) && !this.check(types_1.TT.EOF)) {
1588
+ if (this.check(types_1.TT.KW_GLOBAL)) {
1589
+ global = true;
1590
+ this.consume(types_1.TT.KW_GLOBAL);
1591
+ }
1592
+ else if (this.check(types_1.TT.KW_LANG)) {
1593
+ this.consume(types_1.TT.KW_LANG);
1594
+ this.consume(types_1.TT.EQUALS);
1595
+ const lv = this.consume(types_1.TT.STRING).value;
1596
+ if (lv !== "css" && lv !== "scss")
1597
+ throw new types_1.SinthError(`Unsupported style lang '${lv}'`, loc);
1598
+ lang = lv;
1599
+ }
1600
+ else if (this.check(types_1.TT.IDENT)) {
1601
+ if (this.tokens[this.pos + 1]?.type !== types_1.TT.EQUALS) {
1602
+ target = this.consume(types_1.TT.IDENT).value;
1603
+ }
1604
+ else {
1605
+ const k = this.consume(types_1.TT.IDENT).value;
1606
+ this.consume(types_1.TT.EQUALS);
1607
+ extraAttrs[k] = this.consume(types_1.TT.STRING).value;
1608
+ }
1609
+ }
1610
+ else
1611
+ throw new types_1.SinthError(`Unexpected token '${this.peek().value}' in style modifiers`, this.peek().loc);
1612
+ }
1613
+ return { global, lang, raw: this.consume(types_1.TT.RAW_BLOCK).value, target, extraAttrs, loc };
1614
+ }
1615
+ parseScriptBlock() {
1616
+ const loc = this.consume(types_1.TT.KW_SCRIPT).loc;
1617
+ const attrs = {};
1618
+ while (!this.check(types_1.TT.RAW_BLOCK) && !this.check(types_1.TT.EOF)) {
1619
+ const key = this.consume(types_1.TT.IDENT).value;
1620
+ if (this.check(types_1.TT.EQUALS)) {
1621
+ this.consume(types_1.TT.EQUALS);
1622
+ attrs[key] = this.consume(types_1.TT.STRING).value;
1623
+ }
1624
+ else {
1625
+ attrs[key] = "true";
1626
+ }
1627
+ }
1628
+ return { raw: this.consume(types_1.TT.RAW_BLOCK).value, attrs, loc };
1629
+ }
1630
+ parseLiteral() {
1631
+ const tok = this.peek();
1632
+ if (this.check(types_1.TT.LBRACKET)) {
1633
+ return this.parseArrayLiteral();
1634
+ }
1635
+ if (this.check(types_1.TT.LBRACE)) {
1636
+ const obj = this.parseObjectLiteral();
1637
+ return { kind: "str", value: JSON.stringify(obj) };
1638
+ }
1639
+ if (this.check(types_1.TT.STRING)) {
1640
+ this.consume(types_1.TT.STRING);
1641
+ return { kind: "str", value: tok.value };
1642
+ }
1643
+ if (this.check(types_1.TT.NUMBER)) {
1644
+ this.consume(types_1.TT.NUMBER);
1645
+ return { kind: "num", value: parseFloat(tok.value) };
1646
+ }
1647
+ if (this.check(types_1.TT.BOOL_TRUE)) {
1648
+ this.consume(types_1.TT.BOOL_TRUE);
1649
+ return { kind: "bool", value: true };
1650
+ }
1651
+ if (this.check(types_1.TT.BOOL_FALSE)) {
1652
+ this.consume(types_1.TT.BOOL_FALSE);
1653
+ return { kind: "bool", value: false };
1654
+ }
1655
+ if (this.check(types_1.TT.NULL_LIT)) {
1656
+ this.consume(types_1.TT.NULL_LIT);
1657
+ return { kind: "null" };
1658
+ }
1659
+ throw new types_1.SinthError(`Expected literal value, got '${tok.value}' (${types_1.TT[tok.type]})`, tok.loc);
1660
+ }
1661
+ peek() { return this.tokens[this.pos]; }
1662
+ check(t) { return this.tokens[this.pos]?.type === t; }
1663
+ consume(t) {
1664
+ const tok = this.tokens[this.pos];
1665
+ if (tok.type !== t) {
1666
+ throw new types_1.SinthError(`Expected ${types_1.TT[t]}, got '${tok.value}' (${types_1.TT[tok.type]})`, tok.loc);
1667
+ }
1668
+ this.pos++;
1669
+ return tok;
1670
+ }
1671
+ }
1672
+ exports.Parser = Parser;