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