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