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