aktion-runtime 0.5.3 → 0.5.5
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 +126 -31
- package/dist/aktion.iife.js +106 -460
- package/dist/aktion.iife.js.map +1 -1
- package/dist/aktion.js +5688 -5055
- package/dist/aktion.js.map +1 -1
- package/dist/aktion.umd.cjs +106 -460
- package/dist/aktion.umd.cjs.map +1 -1
- package/dist/devtools.js +1367 -0
- package/dist/devtools.js.map +1 -0
- package/dist/language.js +15311 -0
- package/dist/language.js.map +1 -0
- package/dist/plugin.cjs +2650 -0
- package/dist/plugin.cjs.map +1 -0
- package/dist/plugin.js +2650 -0
- package/dist/plugin.js.map +1 -0
- package/dist/system_prompt.txt +164 -447
- package/dist/system_prompt_chat.txt +33 -84
- package/dist/testing.js +29496 -0
- package/dist/testing.js.map +1 -0
- package/dist/types/aktion-modules.d.ts +18 -0
- package/dist/types/compiler/index.d.ts +12 -0
- package/dist/types/compiler/linker.d.ts +27 -0
- package/dist/types/compiler/project.d.ts +45 -0
- package/dist/types/compiler/runtime.d.ts +59 -0
- package/dist/types/devtools/hook.d.ts +88 -0
- package/dist/types/devtools/index.d.ts +35 -0
- package/dist/types/devtools/panel.d.ts +109 -0
- package/dist/types/devtools/protocol.d.ts +130 -0
- package/dist/types/devtools/styles.d.ts +7 -0
- package/dist/types/element.d.ts +76 -8
- package/dist/types/index.d.ts +1 -0
- package/dist/types/language-api.d.ts +15 -0
- package/dist/types/parser/types.d.ts +38 -1
- package/dist/types/plugin/index.d.ts +17 -0
- package/dist/types/renderer/renderer.d.ts +28 -0
- package/dist/types/runtime/builtins.d.ts +5 -11
- package/dist/types/runtime/effects.d.ts +15 -0
- package/dist/types/runtime/http.d.ts +0 -1
- package/dist/types/testing/index.d.ts +201 -0
- package/dist/types/theme/index.d.ts +3 -3
- package/dist/types/theme/styles.d.ts +1 -1
- package/dist/types/tooling/formatter.d.ts +7 -1
- package/dist/types/tooling/index.d.ts +0 -3
- package/package.json +35 -2
- package/dist/types/library/components/theme.d.ts +0 -2
- package/dist/types/tooling/codemod.d.ts +0 -36
package/dist/plugin.cjs
ADDED
|
@@ -0,0 +1,2650 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const node_fs = require("node:fs");
|
|
4
|
+
const node_path = require("node:path");
|
|
5
|
+
const KEYWORDS_AKTION = /* @__PURE__ */ new Set([
|
|
6
|
+
"function",
|
|
7
|
+
// Module syntax for multi-file `.aktion` programs (resolved by the in-browser
|
|
8
|
+
// linker / `linkProject`; a no-op for the streaming single-file runtime).
|
|
9
|
+
// `from`/`as` are NOT keywords — they stay usable as ordinary identifiers and
|
|
10
|
+
// are matched contextually by the import parser.
|
|
11
|
+
"import",
|
|
12
|
+
"export",
|
|
13
|
+
// NOTE: `$effect` (the side-effect builtin) is `$`-prefixed, so it lexes as
|
|
14
|
+
// a StateIdentifier and is recognised in the parser — it is NOT a keyword.
|
|
15
|
+
"if",
|
|
16
|
+
"else",
|
|
17
|
+
"switch",
|
|
18
|
+
"case",
|
|
19
|
+
"break",
|
|
20
|
+
"continue",
|
|
21
|
+
"for",
|
|
22
|
+
"while",
|
|
23
|
+
"do",
|
|
24
|
+
"of",
|
|
25
|
+
"in",
|
|
26
|
+
"let",
|
|
27
|
+
"var",
|
|
28
|
+
"const",
|
|
29
|
+
"await",
|
|
30
|
+
"async",
|
|
31
|
+
"return",
|
|
32
|
+
"default",
|
|
33
|
+
"try",
|
|
34
|
+
"catch",
|
|
35
|
+
"finally",
|
|
36
|
+
"throw",
|
|
37
|
+
"new",
|
|
38
|
+
"typeof",
|
|
39
|
+
"instanceof",
|
|
40
|
+
"delete",
|
|
41
|
+
"void"
|
|
42
|
+
]);
|
|
43
|
+
const SINGLE_CHAR_PUNCT = /* @__PURE__ */ new Set(["(", ")", "[", "]", "{", "}", ",", ":", "?", "."]);
|
|
44
|
+
const KEYWORDS = {
|
|
45
|
+
true: "Boolean",
|
|
46
|
+
false: "Boolean",
|
|
47
|
+
null: "Null"
|
|
48
|
+
};
|
|
49
|
+
function tokenize(source) {
|
|
50
|
+
const tokens = [];
|
|
51
|
+
let i = 0;
|
|
52
|
+
let line = 1;
|
|
53
|
+
let column = 1;
|
|
54
|
+
const peek = (offset = 0) => source[i + offset];
|
|
55
|
+
const push = (type, value, startLine, startCol) => {
|
|
56
|
+
tokens.push({ type, value, line: startLine, column: startCol });
|
|
57
|
+
};
|
|
58
|
+
const advance = () => {
|
|
59
|
+
const ch = source[i];
|
|
60
|
+
i += 1;
|
|
61
|
+
if (ch === "\n") {
|
|
62
|
+
line += 1;
|
|
63
|
+
column = 1;
|
|
64
|
+
} else {
|
|
65
|
+
column += 1;
|
|
66
|
+
}
|
|
67
|
+
return ch;
|
|
68
|
+
};
|
|
69
|
+
while (i < source.length) {
|
|
70
|
+
const ch = peek();
|
|
71
|
+
if (ch === void 0) break;
|
|
72
|
+
if (ch === "\n") {
|
|
73
|
+
const startLine = line;
|
|
74
|
+
const startCol = column;
|
|
75
|
+
advance();
|
|
76
|
+
push("Newline", "\n", startLine, startCol);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (ch === " " || ch === " " || ch === "\r") {
|
|
80
|
+
advance();
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (ch === "/" && peek(1) === "/") {
|
|
84
|
+
while (i < source.length && peek() !== "\n") advance();
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (ch === "/" && peek(1) === "*") {
|
|
88
|
+
advance();
|
|
89
|
+
advance();
|
|
90
|
+
while (i < source.length && !(peek() === "*" && peek(1) === "/")) {
|
|
91
|
+
advance();
|
|
92
|
+
}
|
|
93
|
+
if (i < source.length) {
|
|
94
|
+
advance();
|
|
95
|
+
advance();
|
|
96
|
+
}
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (ch === ";") {
|
|
100
|
+
const startLine = line;
|
|
101
|
+
const startCol = column;
|
|
102
|
+
advance();
|
|
103
|
+
push("Semicolon", ";", startLine, startCol);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (ch === '"' || ch === "'") {
|
|
107
|
+
const quote = ch;
|
|
108
|
+
const startLine = line;
|
|
109
|
+
const startCol = column;
|
|
110
|
+
advance();
|
|
111
|
+
let value = "";
|
|
112
|
+
while (i < source.length && peek() !== quote) {
|
|
113
|
+
if (peek() === "\\" && peek(1) !== void 0) {
|
|
114
|
+
advance();
|
|
115
|
+
const escaped = advance();
|
|
116
|
+
switch (escaped) {
|
|
117
|
+
case "n":
|
|
118
|
+
value += "\n";
|
|
119
|
+
break;
|
|
120
|
+
case "t":
|
|
121
|
+
value += " ";
|
|
122
|
+
break;
|
|
123
|
+
case "r":
|
|
124
|
+
value += "\r";
|
|
125
|
+
break;
|
|
126
|
+
case "\\":
|
|
127
|
+
value += "\\";
|
|
128
|
+
break;
|
|
129
|
+
case '"':
|
|
130
|
+
value += '"';
|
|
131
|
+
break;
|
|
132
|
+
case "'":
|
|
133
|
+
value += "'";
|
|
134
|
+
break;
|
|
135
|
+
case "`":
|
|
136
|
+
value += "`";
|
|
137
|
+
break;
|
|
138
|
+
default:
|
|
139
|
+
value += escaped ?? "";
|
|
140
|
+
}
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (peek() === "\n") {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
value += advance();
|
|
147
|
+
}
|
|
148
|
+
if (peek() === quote) advance();
|
|
149
|
+
push("String", value, startLine, startCol);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (ch === "`") {
|
|
153
|
+
const startLine = line;
|
|
154
|
+
const startCol = column;
|
|
155
|
+
advance();
|
|
156
|
+
const parts = [];
|
|
157
|
+
let chunk = "";
|
|
158
|
+
let sawExpr = false;
|
|
159
|
+
while (i < source.length && peek() !== "`") {
|
|
160
|
+
if (peek() === "\\" && peek(1) !== void 0) {
|
|
161
|
+
advance();
|
|
162
|
+
const escaped = advance();
|
|
163
|
+
switch (escaped) {
|
|
164
|
+
case "n":
|
|
165
|
+
chunk += "\n";
|
|
166
|
+
break;
|
|
167
|
+
case "t":
|
|
168
|
+
chunk += " ";
|
|
169
|
+
break;
|
|
170
|
+
case "r":
|
|
171
|
+
chunk += "\r";
|
|
172
|
+
break;
|
|
173
|
+
case "\\":
|
|
174
|
+
chunk += "\\";
|
|
175
|
+
break;
|
|
176
|
+
case '"':
|
|
177
|
+
chunk += '"';
|
|
178
|
+
break;
|
|
179
|
+
case "'":
|
|
180
|
+
chunk += "'";
|
|
181
|
+
break;
|
|
182
|
+
case "`":
|
|
183
|
+
chunk += "`";
|
|
184
|
+
break;
|
|
185
|
+
case "$":
|
|
186
|
+
chunk += "$";
|
|
187
|
+
break;
|
|
188
|
+
default:
|
|
189
|
+
chunk += escaped ?? "";
|
|
190
|
+
}
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (peek() === "$" && peek(1) === "{") {
|
|
194
|
+
parts.push({ kind: "str", text: chunk });
|
|
195
|
+
chunk = "";
|
|
196
|
+
const exprLine = line;
|
|
197
|
+
const exprCol = column;
|
|
198
|
+
advance();
|
|
199
|
+
advance();
|
|
200
|
+
let depth = 1;
|
|
201
|
+
let source2 = "";
|
|
202
|
+
while (i < source.length && depth > 0) {
|
|
203
|
+
const next = peek();
|
|
204
|
+
if (next === void 0) break;
|
|
205
|
+
if (next === "`") {
|
|
206
|
+
source2 += advance();
|
|
207
|
+
while (i < source.length && peek() !== "`") {
|
|
208
|
+
if (peek() === "\\" && peek(1) !== void 0) {
|
|
209
|
+
source2 += advance();
|
|
210
|
+
source2 += advance();
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
source2 += advance();
|
|
214
|
+
}
|
|
215
|
+
if (peek() === "`") source2 += advance();
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (next === '"' || next === "'") {
|
|
219
|
+
const q = next;
|
|
220
|
+
source2 += advance();
|
|
221
|
+
while (i < source.length && peek() !== q) {
|
|
222
|
+
if (peek() === "\\" && peek(1) !== void 0) {
|
|
223
|
+
source2 += advance();
|
|
224
|
+
source2 += advance();
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (peek() === "\n") break;
|
|
228
|
+
source2 += advance();
|
|
229
|
+
}
|
|
230
|
+
if (peek() === q) source2 += advance();
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (next === "{") {
|
|
234
|
+
depth += 1;
|
|
235
|
+
source2 += advance();
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (next === "}") {
|
|
239
|
+
depth -= 1;
|
|
240
|
+
if (depth === 0) {
|
|
241
|
+
advance();
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
source2 += advance();
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
source2 += advance();
|
|
248
|
+
}
|
|
249
|
+
parts.push({ kind: "expr", source: source2, line: exprLine, column: exprCol });
|
|
250
|
+
sawExpr = true;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
chunk += advance();
|
|
254
|
+
}
|
|
255
|
+
if (peek() === "`") advance();
|
|
256
|
+
parts.push({ kind: "str", text: chunk });
|
|
257
|
+
if (!sawExpr) {
|
|
258
|
+
push("String", chunk, startLine, startCol);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
tokens.push({
|
|
262
|
+
type: "TemplateString",
|
|
263
|
+
value: "",
|
|
264
|
+
line: startLine,
|
|
265
|
+
column: startCol,
|
|
266
|
+
parts
|
|
267
|
+
});
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const lastToken = tokens[tokens.length - 1];
|
|
271
|
+
const allowSignedNumber = !lastToken || lastToken.type === "Newline" || lastToken.type === "Semicolon" || lastToken.type === "Operator" || lastToken.type === "Punctuation" && (lastToken.value === "(" || lastToken.value === "[" || lastToken.value === "," || lastToken.value === ":" || lastToken.value === "?" || lastToken.value === "{");
|
|
272
|
+
if (isDigit(ch) || ch === "-" && isDigit(peek(1) ?? "") && allowSignedNumber || ch === "." && isDigit(peek(1) ?? "") && allowSignedNumber) {
|
|
273
|
+
const startLine = line;
|
|
274
|
+
const startCol = column;
|
|
275
|
+
let raw = "";
|
|
276
|
+
if (ch === "-") raw += advance();
|
|
277
|
+
const radixMark = peek(1);
|
|
278
|
+
if (peek() === "0" && (radixMark === "x" || radixMark === "X" || radixMark === "b" || radixMark === "B" || radixMark === "o" || radixMark === "O")) {
|
|
279
|
+
raw += advance();
|
|
280
|
+
raw += advance();
|
|
281
|
+
while (i < source.length) {
|
|
282
|
+
const next = peek() ?? "";
|
|
283
|
+
if (isHexDigit(next)) {
|
|
284
|
+
raw += advance();
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (next === "_" && isHexDigit(peek(1) ?? "")) {
|
|
288
|
+
raw += advance();
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
push("Number", raw, startLine, startCol);
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
let sawDot = false;
|
|
297
|
+
let sawExp = false;
|
|
298
|
+
while (i < source.length) {
|
|
299
|
+
const next = peek() ?? "";
|
|
300
|
+
if (isDigit(next)) {
|
|
301
|
+
raw += advance();
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (next === "_" && isDigit(peek(1) ?? "")) {
|
|
305
|
+
raw += advance();
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (next === "." && !sawDot && !sawExp && isDigit(peek(1) ?? "")) {
|
|
309
|
+
sawDot = true;
|
|
310
|
+
raw += advance();
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if ((next === "e" || next === "E") && !sawExp) {
|
|
314
|
+
const afterE = peek(1) ?? "";
|
|
315
|
+
const afterSign = afterE === "+" || afterE === "-" ? peek(2) ?? "" : afterE;
|
|
316
|
+
if (isDigit(afterSign)) {
|
|
317
|
+
sawExp = true;
|
|
318
|
+
raw += advance();
|
|
319
|
+
if (peek() === "+" || peek() === "-") raw += advance();
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
push("Number", raw, startLine, startCol);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (ch === "$") {
|
|
329
|
+
const startLine = line;
|
|
330
|
+
const startCol = column;
|
|
331
|
+
advance();
|
|
332
|
+
let name = "";
|
|
333
|
+
while (i < source.length && isIdentifierChar(peek() ?? "")) {
|
|
334
|
+
name += advance();
|
|
335
|
+
}
|
|
336
|
+
push("StateIdentifier", name, startLine, startCol);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
if (isIdentifierStart(ch)) {
|
|
340
|
+
const startLine = line;
|
|
341
|
+
const startCol = column;
|
|
342
|
+
let name = "";
|
|
343
|
+
while (i < source.length && isIdentifierChar(peek() ?? "")) {
|
|
344
|
+
name += advance();
|
|
345
|
+
}
|
|
346
|
+
const keyword = KEYWORDS[name];
|
|
347
|
+
if (keyword === "Boolean") {
|
|
348
|
+
push("Boolean", name, startLine, startCol);
|
|
349
|
+
} else if (keyword === "Null") {
|
|
350
|
+
push("Null", name, startLine, startCol);
|
|
351
|
+
} else if (KEYWORDS_AKTION.has(name)) {
|
|
352
|
+
push("Keyword", name, startLine, startCol);
|
|
353
|
+
} else {
|
|
354
|
+
push("Identifier", name, startLine, startCol);
|
|
355
|
+
}
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (ch === "." && peek(1) === "." && peek(2) === ".") {
|
|
359
|
+
const startLine = line;
|
|
360
|
+
const startCol = column;
|
|
361
|
+
advance();
|
|
362
|
+
advance();
|
|
363
|
+
advance();
|
|
364
|
+
push("Operator", "...", startLine, startCol);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
const two = ch + (peek(1) ?? "");
|
|
368
|
+
const three = two + (peek(2) ?? "");
|
|
369
|
+
const four = three + (peek(3) ?? "");
|
|
370
|
+
if (four === ">>>=") {
|
|
371
|
+
const startLine = line;
|
|
372
|
+
const startCol = column;
|
|
373
|
+
advance();
|
|
374
|
+
advance();
|
|
375
|
+
advance();
|
|
376
|
+
advance();
|
|
377
|
+
push("Operator", four, startLine, startCol);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (three === "===" || three === "!==" || three === "**=" || three === "??=" || three === "&&=" || three === "||=" || three === ">>>" || three === ">>=" || three === "<<=") {
|
|
381
|
+
const startLine = line;
|
|
382
|
+
const startCol = column;
|
|
383
|
+
advance();
|
|
384
|
+
advance();
|
|
385
|
+
advance();
|
|
386
|
+
push("Operator", three, startLine, startCol);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (two === "==" || two === "!=" || two === ">=" || two === "<=" || two === "&&" || two === "||" || two === "??" || two === "?." || two === "->" || two === "=>" || two === "**" || two === "%=" || two === "+=" || two === "-=" || two === "*=" || two === "/=" || two === "++" || two === "--" || two === "<<" || two === ">>" || two === "&=" || two === "|=" || two === "^=") {
|
|
390
|
+
const startLine = line;
|
|
391
|
+
const startCol = column;
|
|
392
|
+
advance();
|
|
393
|
+
advance();
|
|
394
|
+
push("Operator", two, startLine, startCol);
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if ("+-*/%!=<>&|^~".includes(ch)) {
|
|
398
|
+
const startLine = line;
|
|
399
|
+
const startCol = column;
|
|
400
|
+
advance();
|
|
401
|
+
push("Operator", ch, startLine, startCol);
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (SINGLE_CHAR_PUNCT.has(ch)) {
|
|
405
|
+
const startLine = line;
|
|
406
|
+
const startCol = column;
|
|
407
|
+
advance();
|
|
408
|
+
push("Punctuation", ch, startLine, startCol);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
advance();
|
|
412
|
+
}
|
|
413
|
+
tokens.push({ type: "EOF", value: "", line, column });
|
|
414
|
+
return tokens;
|
|
415
|
+
}
|
|
416
|
+
function isDigit(ch) {
|
|
417
|
+
return ch >= "0" && ch <= "9";
|
|
418
|
+
}
|
|
419
|
+
function isHexDigit(ch) {
|
|
420
|
+
return ch >= "0" && ch <= "9" || ch >= "a" && ch <= "f" || ch >= "A" && ch <= "F";
|
|
421
|
+
}
|
|
422
|
+
function isIdentifierStart(ch) {
|
|
423
|
+
return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch === "_";
|
|
424
|
+
}
|
|
425
|
+
function isIdentifierChar(ch) {
|
|
426
|
+
return isIdentifierStart(ch) || isDigit(ch);
|
|
427
|
+
}
|
|
428
|
+
function parse(source) {
|
|
429
|
+
const tokens = tokenize(source);
|
|
430
|
+
const ctx = new ParserContext(tokens);
|
|
431
|
+
const statements = [];
|
|
432
|
+
const errors = [];
|
|
433
|
+
while (!ctx.isEnd()) {
|
|
434
|
+
if (ctx.match("Newline") || ctx.match("Semicolon")) continue;
|
|
435
|
+
try {
|
|
436
|
+
const stmt = parseStatement(ctx, true);
|
|
437
|
+
if (stmt) statements.push(stmt);
|
|
438
|
+
} catch (err) {
|
|
439
|
+
const error = err;
|
|
440
|
+
errors.push(error);
|
|
441
|
+
ctx.recoverToNextLine();
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { statements, errors };
|
|
445
|
+
}
|
|
446
|
+
function parseStatement(ctx, _topLevel) {
|
|
447
|
+
const head = ctx.peek();
|
|
448
|
+
if (head.type === "StateIdentifier" && head.value === "effect" && ctx.peek(1).type === "Punctuation" && ctx.peek(1).value === "(") {
|
|
449
|
+
return parseEffectStatement(ctx);
|
|
450
|
+
}
|
|
451
|
+
if (head.type === "Keyword") {
|
|
452
|
+
switch (head.value) {
|
|
453
|
+
case "function":
|
|
454
|
+
return parseFunctionDecl(ctx);
|
|
455
|
+
case "import":
|
|
456
|
+
return parseImportStatement(ctx);
|
|
457
|
+
case "export":
|
|
458
|
+
return parseExportStatement(ctx);
|
|
459
|
+
case "await":
|
|
460
|
+
return parseAwait(ctx);
|
|
461
|
+
case "async": {
|
|
462
|
+
if (ctx.peek(1).type === "Keyword" && ctx.peek(1).value === "function") {
|
|
463
|
+
ctx.consume();
|
|
464
|
+
return parseFunctionDecl(ctx);
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case "return":
|
|
469
|
+
return parseReturn(ctx);
|
|
470
|
+
case "let":
|
|
471
|
+
case "const":
|
|
472
|
+
case "var":
|
|
473
|
+
return parseVarDecl(ctx);
|
|
474
|
+
case "if":
|
|
475
|
+
return parseIfStatement(ctx);
|
|
476
|
+
case "switch":
|
|
477
|
+
return parseSwitchStatement(ctx);
|
|
478
|
+
case "for":
|
|
479
|
+
return parseForStatement(ctx);
|
|
480
|
+
case "while":
|
|
481
|
+
return parseWhileStatement(ctx);
|
|
482
|
+
case "do":
|
|
483
|
+
return parseDoWhileStatement(ctx);
|
|
484
|
+
case "break":
|
|
485
|
+
return parseBreakStatement(ctx);
|
|
486
|
+
case "continue":
|
|
487
|
+
return parseContinueStatement(ctx);
|
|
488
|
+
case "throw":
|
|
489
|
+
return parseThrowStatement(ctx);
|
|
490
|
+
case "try":
|
|
491
|
+
return parseTryStatement(ctx);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
const saved = ctx.snapshot();
|
|
495
|
+
if (couldStartAssignment(ctx)) {
|
|
496
|
+
try {
|
|
497
|
+
return parseAssignment(ctx);
|
|
498
|
+
} catch (err) {
|
|
499
|
+
if (err && typeof err === "object" && err.__definitive) {
|
|
500
|
+
throw err;
|
|
501
|
+
}
|
|
502
|
+
ctx.restore(saved);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return parseExpressionStatement(ctx);
|
|
506
|
+
}
|
|
507
|
+
function couldStartAssignment(ctx) {
|
|
508
|
+
const head = ctx.peek();
|
|
509
|
+
if (head.type !== "Identifier" && head.type !== "StateIdentifier") {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
const next = ctx.peek(1);
|
|
513
|
+
return next.type === "Operator" && next.value === "=";
|
|
514
|
+
}
|
|
515
|
+
function parseExpressionStatement(ctx) {
|
|
516
|
+
const start = ctx.peek();
|
|
517
|
+
let expression = parseExpression(ctx);
|
|
518
|
+
const next = ctx.peek();
|
|
519
|
+
if (next.type === "Operator" && isAssignmentOperator(next.value)) {
|
|
520
|
+
if (isAssignableTarget(expression)) {
|
|
521
|
+
ctx.consume();
|
|
522
|
+
const value = parseExpression(ctx);
|
|
523
|
+
expression = {
|
|
524
|
+
kind: "BuiltinCall",
|
|
525
|
+
name: "__rui_assign__",
|
|
526
|
+
arguments: [
|
|
527
|
+
expression,
|
|
528
|
+
value,
|
|
529
|
+
{ kind: "Literal", value: next.value }
|
|
530
|
+
],
|
|
531
|
+
loc: { line: next.line, column: next.column }
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
} else if (next.type === "Operator" && (next.value === "++" || next.value === "--")) {
|
|
535
|
+
if (isAssignableTarget(expression)) {
|
|
536
|
+
ctx.consume();
|
|
537
|
+
expression = {
|
|
538
|
+
kind: "BuiltinCall",
|
|
539
|
+
name: "__rui_postfix__",
|
|
540
|
+
arguments: [expression, { kind: "Literal", value: next.value }],
|
|
541
|
+
loc: { line: next.line, column: next.column }
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
skipTerminator(ctx);
|
|
546
|
+
return {
|
|
547
|
+
kind: "ExpressionStatement",
|
|
548
|
+
expression,
|
|
549
|
+
loc: { line: start.line, column: start.column }
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function isAssignmentOperator(value) {
|
|
553
|
+
return value === "=" || value === "+=" || value === "-=" || value === "*=" || value === "/=" || value === "%=" || value === "**=" || value === "??=" || value === "&&=" || value === "||=" || value === "&=" || value === "|=" || value === "^=" || value === "<<=" || value === ">>=" || value === ">>>=";
|
|
554
|
+
}
|
|
555
|
+
function isAssignableTarget(expr) {
|
|
556
|
+
if (expr.kind === "Member") return true;
|
|
557
|
+
if (expr.kind === "StateRef") return true;
|
|
558
|
+
if (expr.kind === "Identifier") return true;
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
function parseFunctionDecl(ctx) {
|
|
562
|
+
const start = ctx.expect("Keyword", "function");
|
|
563
|
+
const isHook = ctx.peek().type === "StateIdentifier";
|
|
564
|
+
const nameTok = isHook ? ctx.consume() : ctx.expect("Identifier");
|
|
565
|
+
const params = parseFunctionParams(ctx);
|
|
566
|
+
const body = parseBlock(ctx);
|
|
567
|
+
skipTerminator(ctx);
|
|
568
|
+
if (isHook) {
|
|
569
|
+
return {
|
|
570
|
+
kind: "HookDeclaration",
|
|
571
|
+
name: nameTok.value,
|
|
572
|
+
params,
|
|
573
|
+
body,
|
|
574
|
+
loc: { line: start.line, column: start.column }
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
const isPascalCase = nameTok.value.length > 0 && nameTok.value[0] >= "A" && nameTok.value[0] <= "Z";
|
|
578
|
+
if (isPascalCase) {
|
|
579
|
+
return {
|
|
580
|
+
kind: "ComponentDeclaration",
|
|
581
|
+
name: nameTok.value,
|
|
582
|
+
params,
|
|
583
|
+
slots: [],
|
|
584
|
+
body,
|
|
585
|
+
loc: { line: start.line, column: start.column }
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
return {
|
|
589
|
+
kind: "ActionDeclaration",
|
|
590
|
+
name: nameTok.value,
|
|
591
|
+
params,
|
|
592
|
+
optimistic: false,
|
|
593
|
+
body,
|
|
594
|
+
loc: { line: start.line, column: start.column }
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
function parseFunctionParams(ctx) {
|
|
598
|
+
ctx.expect("Punctuation", "(");
|
|
599
|
+
const params = [];
|
|
600
|
+
skipWhitespace(ctx);
|
|
601
|
+
if (!(ctx.peek().type === "Punctuation" && ctx.peek().value === ")")) {
|
|
602
|
+
while (true) {
|
|
603
|
+
skipWhitespace(ctx);
|
|
604
|
+
let isRest = false;
|
|
605
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "...") {
|
|
606
|
+
ctx.consume();
|
|
607
|
+
isRest = true;
|
|
608
|
+
}
|
|
609
|
+
const tok = ctx.peek();
|
|
610
|
+
if (!isRest && tok.type === "Punctuation" && (tok.value === "{" || tok.value === "[")) {
|
|
611
|
+
const pattern = parseDestructuringPattern(ctx);
|
|
612
|
+
let defaultValue;
|
|
613
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
614
|
+
ctx.consume();
|
|
615
|
+
defaultValue = parseExpression(ctx);
|
|
616
|
+
}
|
|
617
|
+
const param = { name: "", pattern };
|
|
618
|
+
if (defaultValue) param.defaultValue = defaultValue;
|
|
619
|
+
params.push(param);
|
|
620
|
+
} else if (tok.type === "Identifier" || tok.type === "Keyword") {
|
|
621
|
+
const nameTok = ctx.consume();
|
|
622
|
+
let defaultValue;
|
|
623
|
+
if (!isRest && ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
624
|
+
ctx.consume();
|
|
625
|
+
defaultValue = parseExpression(ctx);
|
|
626
|
+
}
|
|
627
|
+
const param = { name: nameTok.value };
|
|
628
|
+
if (defaultValue) param.defaultValue = defaultValue;
|
|
629
|
+
if (isRest) param.rest = true;
|
|
630
|
+
params.push(param);
|
|
631
|
+
} else {
|
|
632
|
+
throw {
|
|
633
|
+
message: `Expected parameter name, got ${tok.type} "${tok.value}"`,
|
|
634
|
+
line: tok.line,
|
|
635
|
+
column: tok.column
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
skipWhitespace(ctx);
|
|
639
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
640
|
+
if (isRest) {
|
|
641
|
+
throw {
|
|
642
|
+
message: "Rest parameter `...name` must be the final parameter.",
|
|
643
|
+
line: ctx.peek().line,
|
|
644
|
+
column: ctx.peek().column
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
ctx.consume();
|
|
648
|
+
skipWhitespace(ctx);
|
|
649
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ")") break;
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
break;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
ctx.expect("Punctuation", ")");
|
|
656
|
+
return params;
|
|
657
|
+
}
|
|
658
|
+
function parseEffectStatement(ctx) {
|
|
659
|
+
const start = ctx.consume();
|
|
660
|
+
ctx.expect("Punctuation", "(");
|
|
661
|
+
skipWhitespace(ctx);
|
|
662
|
+
const callbackExpr = parseExpression(ctx);
|
|
663
|
+
let body;
|
|
664
|
+
if (callbackExpr.kind === "Lambda") {
|
|
665
|
+
body = callbackExpr.body.kind === "Block" ? callbackExpr.body : { kind: "Block", body: [{ kind: "ExpressionStatement", expression: callbackExpr.body }] };
|
|
666
|
+
} else {
|
|
667
|
+
body = { kind: "Block", body: [] };
|
|
668
|
+
}
|
|
669
|
+
const triggers = [];
|
|
670
|
+
let rateLimit;
|
|
671
|
+
skipWhitespace(ctx);
|
|
672
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
673
|
+
ctx.consume();
|
|
674
|
+
skipWhitespace(ctx);
|
|
675
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "[") {
|
|
676
|
+
ctx.consume();
|
|
677
|
+
skipWhitespace(ctx);
|
|
678
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "]")) {
|
|
679
|
+
parseEffectDep(ctx, triggers, (rl) => {
|
|
680
|
+
rateLimit = rl;
|
|
681
|
+
});
|
|
682
|
+
skipWhitespace(ctx);
|
|
683
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
684
|
+
ctx.consume();
|
|
685
|
+
skipWhitespace(ctx);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
ctx.expect("Punctuation", "]");
|
|
689
|
+
} else {
|
|
690
|
+
parseExpression(ctx);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
skipWhitespace(ctx);
|
|
694
|
+
ctx.expect("Punctuation", ")");
|
|
695
|
+
skipTerminator(ctx);
|
|
696
|
+
const decl = {
|
|
697
|
+
kind: "EffectDeclaration",
|
|
698
|
+
name: `__effect_L${start.line}_C${start.column}`,
|
|
699
|
+
triggers,
|
|
700
|
+
body,
|
|
701
|
+
loc: { line: start.line, column: start.column }
|
|
702
|
+
};
|
|
703
|
+
if (rateLimit) decl.rateLimit = rateLimit;
|
|
704
|
+
return decl;
|
|
705
|
+
}
|
|
706
|
+
function parseEffectDep(ctx, triggers, setRateLimit) {
|
|
707
|
+
const head = ctx.peek();
|
|
708
|
+
if (head.type === "StateIdentifier") {
|
|
709
|
+
ctx.consume();
|
|
710
|
+
let name = head.value;
|
|
711
|
+
while (ctx.peek().type === "Punctuation" && ctx.peek().value === ".") {
|
|
712
|
+
const prop = ctx.peek(1);
|
|
713
|
+
if (prop.type !== "Identifier" && prop.type !== "Keyword") break;
|
|
714
|
+
ctx.consume();
|
|
715
|
+
ctx.consume();
|
|
716
|
+
name += "." + prop.value;
|
|
717
|
+
}
|
|
718
|
+
triggers.push({ kind: "state", name });
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
if (head.type === "String") {
|
|
722
|
+
ctx.consume();
|
|
723
|
+
const val = head.value;
|
|
724
|
+
if (val === "mount" || val === "unmount") {
|
|
725
|
+
triggers.push({ kind: "lifecycle", name: val });
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const everyMatch = val.match(/^every\((\d+)\)$/);
|
|
729
|
+
if (everyMatch) {
|
|
730
|
+
triggers.push({ kind: "every", intervalMs: Number(everyMatch[1]) });
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
const debounceMatch = val.match(/^debounce\((\d+)\)$/);
|
|
734
|
+
if (debounceMatch) {
|
|
735
|
+
setRateLimit({ kind: "debounce", ms: Number(debounceMatch[1]) });
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const throttleMatch = val.match(/^throttle\((\d+)\)$/);
|
|
739
|
+
if (throttleMatch) {
|
|
740
|
+
setRateLimit({ kind: "throttle", ms: Number(throttleMatch[1]) });
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
throw {
|
|
744
|
+
message: `Unknown effect dependency string "${val}". Expected "mount", "unmount", "every(N)", "debounce(N)", or "throttle(N)".`,
|
|
745
|
+
line: head.line,
|
|
746
|
+
column: head.column
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
throw {
|
|
750
|
+
message: `Unexpected ${head.type} "${head.value}" inside effect dependency array. Expected $state or a string token ("mount", "unmount", "every(N)", etc.).`,
|
|
751
|
+
line: head.line,
|
|
752
|
+
column: head.column
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function parseVarDecl(ctx) {
|
|
756
|
+
const start = ctx.consume();
|
|
757
|
+
const head = ctx.peek();
|
|
758
|
+
if (head.type === "Punctuation" && (head.value === "[" || head.value === "{")) {
|
|
759
|
+
return parseDestructureDecl(ctx, start);
|
|
760
|
+
}
|
|
761
|
+
let identifier = "";
|
|
762
|
+
let isState = false;
|
|
763
|
+
if (head.type === "StateIdentifier") {
|
|
764
|
+
identifier = ctx.consume().value;
|
|
765
|
+
isState = true;
|
|
766
|
+
} else if (head.type === "Identifier") {
|
|
767
|
+
identifier = ctx.consume().value;
|
|
768
|
+
} else {
|
|
769
|
+
throw {
|
|
770
|
+
message: `Expected identifier after "${start.value}", got ${head.type} "${head.value}"`,
|
|
771
|
+
line: head.line,
|
|
772
|
+
column: head.column
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
ctx.expect("Operator", "=");
|
|
776
|
+
const expression = parseExpression(ctx);
|
|
777
|
+
skipTerminator(ctx);
|
|
778
|
+
return {
|
|
779
|
+
kind: "Assignment",
|
|
780
|
+
identifier,
|
|
781
|
+
isState,
|
|
782
|
+
expression,
|
|
783
|
+
loc: { line: start.line, column: start.column }
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function parseDestructureDecl(ctx, start) {
|
|
787
|
+
const pattern = parseDestructuringPattern(ctx);
|
|
788
|
+
ctx.expect("Operator", "=");
|
|
789
|
+
const expression = parseExpression(ctx);
|
|
790
|
+
skipTerminator(ctx);
|
|
791
|
+
return {
|
|
792
|
+
kind: "DestructureStatement",
|
|
793
|
+
patternKind: pattern.kind,
|
|
794
|
+
bindings: pattern.bindings,
|
|
795
|
+
expression,
|
|
796
|
+
loc: { line: start.line, column: start.column }
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
function parseDestructuringPattern(ctx) {
|
|
800
|
+
const head = ctx.consume();
|
|
801
|
+
const patternKind = head.value === "[" ? "array" : "object";
|
|
802
|
+
const bindings = [];
|
|
803
|
+
if (patternKind === "array") {
|
|
804
|
+
skipWhitespace(ctx);
|
|
805
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "]")) {
|
|
806
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
807
|
+
bindings.push({ name: "" });
|
|
808
|
+
ctx.consume();
|
|
809
|
+
skipWhitespace(ctx);
|
|
810
|
+
continue;
|
|
811
|
+
}
|
|
812
|
+
let isRest = false;
|
|
813
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "...") {
|
|
814
|
+
ctx.consume();
|
|
815
|
+
isRest = true;
|
|
816
|
+
}
|
|
817
|
+
const nameTok = ctx.expect("Identifier");
|
|
818
|
+
let defaultValue;
|
|
819
|
+
if (!isRest && ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
820
|
+
ctx.consume();
|
|
821
|
+
defaultValue = parseExpression(ctx);
|
|
822
|
+
}
|
|
823
|
+
const binding = { name: nameTok.value };
|
|
824
|
+
if (isRest) binding.rest = true;
|
|
825
|
+
if (defaultValue) binding.defaultValue = defaultValue;
|
|
826
|
+
bindings.push(binding);
|
|
827
|
+
skipWhitespace(ctx);
|
|
828
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
829
|
+
ctx.consume();
|
|
830
|
+
skipWhitespace(ctx);
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
ctx.expect("Punctuation", "]");
|
|
836
|
+
} else {
|
|
837
|
+
skipWhitespace(ctx);
|
|
838
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
839
|
+
let isRest = false;
|
|
840
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "...") {
|
|
841
|
+
ctx.consume();
|
|
842
|
+
isRest = true;
|
|
843
|
+
}
|
|
844
|
+
const keyTok = ctx.expect("Identifier");
|
|
845
|
+
let alias = keyTok.value;
|
|
846
|
+
let sourceKey;
|
|
847
|
+
if (!isRest && ctx.peek().type === "Punctuation" && ctx.peek().value === ":") {
|
|
848
|
+
ctx.consume();
|
|
849
|
+
const aliasTok = ctx.expect("Identifier");
|
|
850
|
+
sourceKey = keyTok.value;
|
|
851
|
+
alias = aliasTok.value;
|
|
852
|
+
}
|
|
853
|
+
let defaultValue;
|
|
854
|
+
if (!isRest && ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
855
|
+
ctx.consume();
|
|
856
|
+
defaultValue = parseExpression(ctx);
|
|
857
|
+
}
|
|
858
|
+
const binding = { name: alias };
|
|
859
|
+
if (sourceKey) binding.sourceKey = sourceKey;
|
|
860
|
+
if (isRest) binding.rest = true;
|
|
861
|
+
if (defaultValue) binding.defaultValue = defaultValue;
|
|
862
|
+
bindings.push(binding);
|
|
863
|
+
skipWhitespace(ctx);
|
|
864
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
865
|
+
ctx.consume();
|
|
866
|
+
skipWhitespace(ctx);
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
ctx.expect("Punctuation", "}");
|
|
872
|
+
}
|
|
873
|
+
return { kind: patternKind, bindings };
|
|
874
|
+
}
|
|
875
|
+
function parseBlock(ctx) {
|
|
876
|
+
const start = ctx.expect("Punctuation", "{");
|
|
877
|
+
const body = [];
|
|
878
|
+
skipWhitespace(ctx);
|
|
879
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
880
|
+
const stmt = parseStatement(ctx);
|
|
881
|
+
if (stmt) body.push(stmt);
|
|
882
|
+
skipWhitespace(ctx);
|
|
883
|
+
}
|
|
884
|
+
ctx.expect("Punctuation", "}");
|
|
885
|
+
return {
|
|
886
|
+
kind: "Block",
|
|
887
|
+
body,
|
|
888
|
+
loc: { line: start.line, column: start.column }
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
function parseBlockOrSingleStatement(ctx) {
|
|
892
|
+
skipWhitespace(ctx);
|
|
893
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "{") {
|
|
894
|
+
return parseBlock(ctx);
|
|
895
|
+
}
|
|
896
|
+
const head = ctx.peek();
|
|
897
|
+
const stmt = parseStatement(ctx);
|
|
898
|
+
return {
|
|
899
|
+
kind: "Block",
|
|
900
|
+
body: stmt ? [stmt] : [],
|
|
901
|
+
loc: { line: head.line, column: head.column }
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
function parseAwait(ctx) {
|
|
905
|
+
const start = ctx.expect("Keyword", "await");
|
|
906
|
+
const argument = parseExpression(ctx);
|
|
907
|
+
skipTerminator(ctx);
|
|
908
|
+
return {
|
|
909
|
+
kind: "Await",
|
|
910
|
+
argument,
|
|
911
|
+
loc: { line: start.line, column: start.column }
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
function parseReturn(ctx) {
|
|
915
|
+
const start = ctx.expect("Keyword", "return");
|
|
916
|
+
let argument;
|
|
917
|
+
const next = ctx.peek();
|
|
918
|
+
if (next.type !== "Newline" && next.type !== "Semicolon" && !(next.type === "Punctuation" && next.value === "}")) {
|
|
919
|
+
argument = parseExpression(ctx);
|
|
920
|
+
}
|
|
921
|
+
skipTerminator(ctx);
|
|
922
|
+
return {
|
|
923
|
+
kind: "Return",
|
|
924
|
+
argument,
|
|
925
|
+
loc: { line: start.line, column: start.column }
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
class ParserContext {
|
|
929
|
+
constructor(tokens) {
|
|
930
|
+
this.tokens = tokens;
|
|
931
|
+
}
|
|
932
|
+
index = 0;
|
|
933
|
+
isEnd() {
|
|
934
|
+
return this.peek().type === "EOF";
|
|
935
|
+
}
|
|
936
|
+
peek(offset = 0) {
|
|
937
|
+
return this.tokens[this.index + offset] ?? { type: "EOF", value: "", line: 0, column: 0 };
|
|
938
|
+
}
|
|
939
|
+
consume() {
|
|
940
|
+
const tok = this.tokens[this.index] ?? { type: "EOF", value: "", line: 0, column: 0 };
|
|
941
|
+
this.index += 1;
|
|
942
|
+
return tok;
|
|
943
|
+
}
|
|
944
|
+
match(type, value) {
|
|
945
|
+
const tok = this.peek();
|
|
946
|
+
if (tok.type !== type) return false;
|
|
947
|
+
if (value !== void 0 && tok.value !== value) return false;
|
|
948
|
+
this.consume();
|
|
949
|
+
return true;
|
|
950
|
+
}
|
|
951
|
+
expect(type, value) {
|
|
952
|
+
const tok = this.peek();
|
|
953
|
+
if (tok.type !== type || value !== void 0 && tok.value !== value) {
|
|
954
|
+
throw {
|
|
955
|
+
message: `Expected ${type}${value !== void 0 ? ` "${value}"` : ""} but got ${tok.type} "${tok.value}"`,
|
|
956
|
+
line: tok.line,
|
|
957
|
+
column: tok.column
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
return this.consume();
|
|
961
|
+
}
|
|
962
|
+
recoverToNextLine() {
|
|
963
|
+
while (!this.isEnd() && this.peek().type !== "Newline" && this.peek().type !== "Semicolon") this.consume();
|
|
964
|
+
if (this.peek().type === "Newline" || this.peek().type === "Semicolon") this.consume();
|
|
965
|
+
}
|
|
966
|
+
snapshot() {
|
|
967
|
+
return this.index;
|
|
968
|
+
}
|
|
969
|
+
restore(index) {
|
|
970
|
+
this.index = index;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
function parseAssignment(ctx) {
|
|
974
|
+
const head = ctx.peek();
|
|
975
|
+
let identifier = "";
|
|
976
|
+
let isState = false;
|
|
977
|
+
if (head.type === "Identifier") {
|
|
978
|
+
identifier = ctx.consume().value;
|
|
979
|
+
} else if (head.type === "StateIdentifier") {
|
|
980
|
+
identifier = ctx.consume().value;
|
|
981
|
+
isState = true;
|
|
982
|
+
} else {
|
|
983
|
+
throw {
|
|
984
|
+
message: `Expected identifier at start of statement, got ${head.type} "${head.value}"`,
|
|
985
|
+
line: head.line,
|
|
986
|
+
column: head.column
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
const eq = ctx.expect("Operator", "=");
|
|
990
|
+
const expression = parseExpression(ctx);
|
|
991
|
+
skipTerminator(ctx);
|
|
992
|
+
return {
|
|
993
|
+
kind: "Assignment",
|
|
994
|
+
identifier,
|
|
995
|
+
isState,
|
|
996
|
+
expression,
|
|
997
|
+
loc: { line: eq.line, column: eq.column }
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
function parseImportStatement(ctx) {
|
|
1001
|
+
const start = ctx.expect("Keyword", "import");
|
|
1002
|
+
ctx.expect("Punctuation", "{");
|
|
1003
|
+
const specifiers = [];
|
|
1004
|
+
while (!ctx.isEnd() && !(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
1005
|
+
const importedTok = ctx.peek();
|
|
1006
|
+
let imported;
|
|
1007
|
+
let isState = false;
|
|
1008
|
+
if (importedTok.type === "StateIdentifier") {
|
|
1009
|
+
imported = ctx.consume().value;
|
|
1010
|
+
isState = true;
|
|
1011
|
+
} else if (importedTok.type === "Identifier") {
|
|
1012
|
+
imported = ctx.consume().value;
|
|
1013
|
+
} else {
|
|
1014
|
+
throw {
|
|
1015
|
+
message: `Expected an import name, got ${importedTok.type} "${importedTok.value}"`,
|
|
1016
|
+
line: importedTok.line,
|
|
1017
|
+
column: importedTok.column
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
let local = imported;
|
|
1021
|
+
if (ctx.peek().type === "Identifier" && ctx.peek().value === "as") {
|
|
1022
|
+
ctx.consume();
|
|
1023
|
+
const aliasTok = ctx.peek();
|
|
1024
|
+
let aliasIsState = false;
|
|
1025
|
+
if (aliasTok.type === "StateIdentifier") {
|
|
1026
|
+
local = ctx.consume().value;
|
|
1027
|
+
aliasIsState = true;
|
|
1028
|
+
} else if (aliasTok.type === "Identifier") {
|
|
1029
|
+
local = ctx.consume().value;
|
|
1030
|
+
} else {
|
|
1031
|
+
throw {
|
|
1032
|
+
message: `Expected an alias after \`as\`, got ${aliasTok.type} "${aliasTok.value}"`,
|
|
1033
|
+
line: aliasTok.line,
|
|
1034
|
+
column: aliasTok.column
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
if (aliasIsState !== isState) {
|
|
1038
|
+
throw {
|
|
1039
|
+
message: "A `$state` import must keep its `$` across `as` (e.g. `{ $x as $y }`); a non-state import must not gain one.",
|
|
1040
|
+
line: aliasTok.line,
|
|
1041
|
+
column: aliasTok.column
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
specifiers.push(isState ? { imported, local, isState: true } : { imported, local });
|
|
1046
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
1047
|
+
ctx.consume();
|
|
1048
|
+
continue;
|
|
1049
|
+
}
|
|
1050
|
+
break;
|
|
1051
|
+
}
|
|
1052
|
+
ctx.expect("Punctuation", "}");
|
|
1053
|
+
const fromTok = ctx.peek();
|
|
1054
|
+
if (!(fromTok.type === "Identifier" && fromTok.value === "from")) {
|
|
1055
|
+
throw {
|
|
1056
|
+
message: `Expected \`from\` after import specifiers, got ${fromTok.type} "${fromTok.value}"`,
|
|
1057
|
+
line: fromTok.line,
|
|
1058
|
+
column: fromTok.column
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
ctx.consume();
|
|
1062
|
+
const sourceTok = ctx.expect("String");
|
|
1063
|
+
skipTerminator(ctx);
|
|
1064
|
+
return {
|
|
1065
|
+
kind: "Import",
|
|
1066
|
+
specifiers,
|
|
1067
|
+
source: sourceTok.value,
|
|
1068
|
+
loc: { line: start.line, column: start.column }
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
function parseExportStatement(ctx) {
|
|
1072
|
+
const start = ctx.expect("Keyword", "export");
|
|
1073
|
+
const next = ctx.peek();
|
|
1074
|
+
if (next.type === "Punctuation" && next.value === "{") {
|
|
1075
|
+
throw {
|
|
1076
|
+
message: "`export { … }` lists are not supported yet — use inline `export <declaration>` (e.g. `export function Foo() {…}`, `export $count = 0`).",
|
|
1077
|
+
line: next.line,
|
|
1078
|
+
column: next.column
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
let stmt;
|
|
1082
|
+
if (next.type === "Keyword" && next.value === "function") {
|
|
1083
|
+
stmt = parseFunctionDecl(ctx);
|
|
1084
|
+
} else if (next.type === "Keyword" && next.value === "async" && ctx.peek(1).type === "Keyword" && ctx.peek(1).value === "function") {
|
|
1085
|
+
ctx.consume();
|
|
1086
|
+
stmt = parseFunctionDecl(ctx);
|
|
1087
|
+
} else if (next.type === "Keyword" && (next.value === "let" || next.value === "const" || next.value === "var")) {
|
|
1088
|
+
stmt = parseVarDecl(ctx);
|
|
1089
|
+
if (stmt && stmt.kind === "DestructureStatement") {
|
|
1090
|
+
throw {
|
|
1091
|
+
message: "`export` of a destructuring declaration is not supported — export named bindings individually.",
|
|
1092
|
+
line: next.line,
|
|
1093
|
+
column: next.column
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
} else if (couldStartAssignment(ctx)) {
|
|
1097
|
+
stmt = parseAssignment(ctx);
|
|
1098
|
+
} else {
|
|
1099
|
+
throw {
|
|
1100
|
+
message: "`export` must be followed by a declaration or assignment (`export function …`, `export let x = …`, `export $state = …`).",
|
|
1101
|
+
line: next.line,
|
|
1102
|
+
column: next.column
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
if (stmt && (stmt.kind === "Assignment" || stmt.kind === "ComponentDeclaration" || stmt.kind === "ActionDeclaration" || stmt.kind === "HookDeclaration")) {
|
|
1106
|
+
stmt.exported = true;
|
|
1107
|
+
return stmt;
|
|
1108
|
+
}
|
|
1109
|
+
throw {
|
|
1110
|
+
message: "`export` must be followed by a declaration or assignment.",
|
|
1111
|
+
line: start.line,
|
|
1112
|
+
column: start.column
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
function parseExpression(ctx) {
|
|
1116
|
+
return parseTernary(ctx);
|
|
1117
|
+
}
|
|
1118
|
+
function peekNonNewline(ctx) {
|
|
1119
|
+
let i = 0;
|
|
1120
|
+
while (true) {
|
|
1121
|
+
const t = ctx.peek(i);
|
|
1122
|
+
if (t.type === "Newline") {
|
|
1123
|
+
i += 1;
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
return { token: t, skipped: i };
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
function consumeNewlinesIfNext(ctx, predicate) {
|
|
1130
|
+
const { token, skipped } = peekNonNewline(ctx);
|
|
1131
|
+
if (!predicate(token)) return false;
|
|
1132
|
+
for (let i = 0; i < skipped; i += 1) ctx.consume();
|
|
1133
|
+
return true;
|
|
1134
|
+
}
|
|
1135
|
+
function parseTernary(ctx) {
|
|
1136
|
+
const test = parseLogicalOr(ctx);
|
|
1137
|
+
if (consumeNewlinesIfNext(ctx, (t) => t.type === "Punctuation" && t.value === "?")) {
|
|
1138
|
+
ctx.consume();
|
|
1139
|
+
skipWhitespace(ctx);
|
|
1140
|
+
const consequent = parseExpression(ctx);
|
|
1141
|
+
skipWhitespace(ctx);
|
|
1142
|
+
ctx.expect("Punctuation", ":");
|
|
1143
|
+
skipWhitespace(ctx);
|
|
1144
|
+
const alternate = parseExpression(ctx);
|
|
1145
|
+
return { kind: "Ternary", test, consequent, alternate };
|
|
1146
|
+
}
|
|
1147
|
+
return test;
|
|
1148
|
+
}
|
|
1149
|
+
function parseLogicalOr(ctx) {
|
|
1150
|
+
let left = parseLogicalAnd(ctx);
|
|
1151
|
+
while (consumeNewlinesIfNext(
|
|
1152
|
+
ctx,
|
|
1153
|
+
(t) => t.type === "Operator" && (t.value === "||" || t.value === "??")
|
|
1154
|
+
)) {
|
|
1155
|
+
const op = ctx.consume().value;
|
|
1156
|
+
skipWhitespace(ctx);
|
|
1157
|
+
const right = parseLogicalAnd(ctx);
|
|
1158
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1159
|
+
}
|
|
1160
|
+
return left;
|
|
1161
|
+
}
|
|
1162
|
+
function parseLogicalAnd(ctx) {
|
|
1163
|
+
let left = parseBitwiseOr(ctx);
|
|
1164
|
+
while (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && t.value === "&&")) {
|
|
1165
|
+
ctx.consume();
|
|
1166
|
+
skipWhitespace(ctx);
|
|
1167
|
+
const right = parseBitwiseOr(ctx);
|
|
1168
|
+
left = { kind: "Binary", operator: "&&", left, right };
|
|
1169
|
+
}
|
|
1170
|
+
return left;
|
|
1171
|
+
}
|
|
1172
|
+
function parseBitwiseOr(ctx) {
|
|
1173
|
+
let left = parseBitwiseXor(ctx);
|
|
1174
|
+
while (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && t.value === "|")) {
|
|
1175
|
+
ctx.consume();
|
|
1176
|
+
skipWhitespace(ctx);
|
|
1177
|
+
const right = parseBitwiseXor(ctx);
|
|
1178
|
+
left = { kind: "Binary", operator: "|", left, right };
|
|
1179
|
+
}
|
|
1180
|
+
return left;
|
|
1181
|
+
}
|
|
1182
|
+
function parseBitwiseXor(ctx) {
|
|
1183
|
+
let left = parseBitwiseAnd(ctx);
|
|
1184
|
+
while (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && t.value === "^")) {
|
|
1185
|
+
ctx.consume();
|
|
1186
|
+
skipWhitespace(ctx);
|
|
1187
|
+
const right = parseBitwiseAnd(ctx);
|
|
1188
|
+
left = { kind: "Binary", operator: "^", left, right };
|
|
1189
|
+
}
|
|
1190
|
+
return left;
|
|
1191
|
+
}
|
|
1192
|
+
function parseBitwiseAnd(ctx) {
|
|
1193
|
+
let left = parseEquality(ctx);
|
|
1194
|
+
while (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && t.value === "&")) {
|
|
1195
|
+
ctx.consume();
|
|
1196
|
+
skipWhitespace(ctx);
|
|
1197
|
+
const right = parseEquality(ctx);
|
|
1198
|
+
left = { kind: "Binary", operator: "&", left, right };
|
|
1199
|
+
}
|
|
1200
|
+
return left;
|
|
1201
|
+
}
|
|
1202
|
+
function parseEquality(ctx) {
|
|
1203
|
+
let left = parseComparison(ctx);
|
|
1204
|
+
while (consumeNewlinesIfNext(
|
|
1205
|
+
ctx,
|
|
1206
|
+
(t) => t.type === "Operator" && (t.value === "==" || t.value === "!=" || t.value === "===" || t.value === "!==")
|
|
1207
|
+
)) {
|
|
1208
|
+
const op = ctx.consume().value;
|
|
1209
|
+
skipWhitespace(ctx);
|
|
1210
|
+
const right = parseComparison(ctx);
|
|
1211
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1212
|
+
}
|
|
1213
|
+
return left;
|
|
1214
|
+
}
|
|
1215
|
+
function parseComparison(ctx) {
|
|
1216
|
+
let left = parseShift(ctx);
|
|
1217
|
+
while (true) {
|
|
1218
|
+
if (consumeNewlinesIfNext(
|
|
1219
|
+
ctx,
|
|
1220
|
+
(t) => t.type === "Operator" && (t.value === ">" || t.value === "<" || t.value === ">=" || t.value === "<=")
|
|
1221
|
+
)) {
|
|
1222
|
+
const op = ctx.consume().value;
|
|
1223
|
+
skipWhitespace(ctx);
|
|
1224
|
+
const right = parseShift(ctx);
|
|
1225
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1226
|
+
continue;
|
|
1227
|
+
}
|
|
1228
|
+
if (consumeNewlinesIfNext(ctx, (t) => t.type === "Keyword" && (t.value === "instanceof" || t.value === "in"))) {
|
|
1229
|
+
const op = ctx.consume().value;
|
|
1230
|
+
skipWhitespace(ctx);
|
|
1231
|
+
const right = parseShift(ctx);
|
|
1232
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1233
|
+
continue;
|
|
1234
|
+
}
|
|
1235
|
+
break;
|
|
1236
|
+
}
|
|
1237
|
+
return left;
|
|
1238
|
+
}
|
|
1239
|
+
function parseShift(ctx) {
|
|
1240
|
+
let left = parseAdditive(ctx);
|
|
1241
|
+
while (consumeNewlinesIfNext(
|
|
1242
|
+
ctx,
|
|
1243
|
+
(t) => t.type === "Operator" && (t.value === "<<" || t.value === ">>" || t.value === ">>>")
|
|
1244
|
+
)) {
|
|
1245
|
+
const op = ctx.consume().value;
|
|
1246
|
+
skipWhitespace(ctx);
|
|
1247
|
+
const right = parseAdditive(ctx);
|
|
1248
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1249
|
+
}
|
|
1250
|
+
return left;
|
|
1251
|
+
}
|
|
1252
|
+
function parseAdditive(ctx) {
|
|
1253
|
+
let left = parseMultiplicative(ctx);
|
|
1254
|
+
while (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && (t.value === "+" || t.value === "-"))) {
|
|
1255
|
+
const op = ctx.consume().value;
|
|
1256
|
+
skipWhitespace(ctx);
|
|
1257
|
+
const right = parseMultiplicative(ctx);
|
|
1258
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1259
|
+
}
|
|
1260
|
+
return left;
|
|
1261
|
+
}
|
|
1262
|
+
function parseMultiplicative(ctx) {
|
|
1263
|
+
let left = parseExponent(ctx);
|
|
1264
|
+
while (consumeNewlinesIfNext(
|
|
1265
|
+
ctx,
|
|
1266
|
+
(t) => t.type === "Operator" && (t.value === "*" || t.value === "/" || t.value === "%")
|
|
1267
|
+
)) {
|
|
1268
|
+
const op = ctx.consume().value;
|
|
1269
|
+
skipWhitespace(ctx);
|
|
1270
|
+
const right = parseExponent(ctx);
|
|
1271
|
+
left = { kind: "Binary", operator: op, left, right };
|
|
1272
|
+
}
|
|
1273
|
+
return left;
|
|
1274
|
+
}
|
|
1275
|
+
function parseExponent(ctx) {
|
|
1276
|
+
const left = parseUnary(ctx);
|
|
1277
|
+
if (consumeNewlinesIfNext(ctx, (t) => t.type === "Operator" && t.value === "**")) {
|
|
1278
|
+
ctx.consume();
|
|
1279
|
+
skipWhitespace(ctx);
|
|
1280
|
+
const right = parseExponent(ctx);
|
|
1281
|
+
return { kind: "Binary", operator: "**", left, right };
|
|
1282
|
+
}
|
|
1283
|
+
return left;
|
|
1284
|
+
}
|
|
1285
|
+
function parseUnary(ctx) {
|
|
1286
|
+
const tok = ctx.peek();
|
|
1287
|
+
if (tok.type === "Operator" && (tok.value === "!" || tok.value === "-" || tok.value === "+" || tok.value === "~")) {
|
|
1288
|
+
ctx.consume();
|
|
1289
|
+
const argument = parseUnary(ctx);
|
|
1290
|
+
return { kind: "Unary", operator: tok.value, argument };
|
|
1291
|
+
}
|
|
1292
|
+
if (tok.type === "Keyword" && tok.value === "await") {
|
|
1293
|
+
ctx.consume();
|
|
1294
|
+
const argument = parseUnary(ctx);
|
|
1295
|
+
return {
|
|
1296
|
+
kind: "BuiltinCall",
|
|
1297
|
+
name: "__rui_await__",
|
|
1298
|
+
arguments: [argument],
|
|
1299
|
+
loc: { line: tok.line, column: tok.column }
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
if (tok.type === "Operator" && (tok.value === "++" || tok.value === "--")) {
|
|
1303
|
+
ctx.consume();
|
|
1304
|
+
const argument = parseUnary(ctx);
|
|
1305
|
+
return {
|
|
1306
|
+
kind: "BuiltinCall",
|
|
1307
|
+
name: "__rui_prefix__",
|
|
1308
|
+
arguments: [
|
|
1309
|
+
argument,
|
|
1310
|
+
{ kind: "Literal", value: tok.value }
|
|
1311
|
+
],
|
|
1312
|
+
loc: { line: tok.line, column: tok.column }
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
if (tok.type === "Keyword" && (tok.value === "typeof" || tok.value === "void" || tok.value === "delete")) {
|
|
1316
|
+
ctx.consume();
|
|
1317
|
+
const argument = parseUnary(ctx);
|
|
1318
|
+
return {
|
|
1319
|
+
kind: "Unary",
|
|
1320
|
+
operator: tok.value,
|
|
1321
|
+
argument
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
if (tok.type === "Keyword" && tok.value === "new") {
|
|
1325
|
+
ctx.consume();
|
|
1326
|
+
let callee = parsePrimary(ctx);
|
|
1327
|
+
let args = [];
|
|
1328
|
+
if (callee.kind === "Call") {
|
|
1329
|
+
args = callee.arguments;
|
|
1330
|
+
callee = { kind: "Identifier", name: callee.callee, loc: callee.loc };
|
|
1331
|
+
} else {
|
|
1332
|
+
while (true) {
|
|
1333
|
+
const t = ctx.peek();
|
|
1334
|
+
if (t.type === "Punctuation" && t.value === ".") {
|
|
1335
|
+
ctx.consume();
|
|
1336
|
+
const propTok = ctx.consume();
|
|
1337
|
+
if (propTok.type !== "Identifier" && propTok.type !== "Keyword") {
|
|
1338
|
+
throw {
|
|
1339
|
+
message: `Expected Identifier but got ${propTok.type} "${propTok.value}"`,
|
|
1340
|
+
line: propTok.line,
|
|
1341
|
+
column: propTok.column
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
callee = { kind: "Member", object: callee, property: propTok.value };
|
|
1345
|
+
continue;
|
|
1346
|
+
}
|
|
1347
|
+
if (t.type === "Punctuation" && t.value === "[") {
|
|
1348
|
+
ctx.consume();
|
|
1349
|
+
const computed = parseExpression(ctx);
|
|
1350
|
+
ctx.expect("Punctuation", "]");
|
|
1351
|
+
callee = { kind: "Member", object: callee, computed };
|
|
1352
|
+
continue;
|
|
1353
|
+
}
|
|
1354
|
+
break;
|
|
1355
|
+
}
|
|
1356
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "(") {
|
|
1357
|
+
ctx.consume();
|
|
1358
|
+
args = parseCallArgs(ctx);
|
|
1359
|
+
ctx.expect("Punctuation", ")");
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
const newNode = {
|
|
1363
|
+
kind: "New",
|
|
1364
|
+
callee,
|
|
1365
|
+
arguments: args,
|
|
1366
|
+
loc: { line: tok.line, column: tok.column }
|
|
1367
|
+
};
|
|
1368
|
+
return parsePostfixFrom(ctx, newNode);
|
|
1369
|
+
}
|
|
1370
|
+
return parsePostfixWithIncDec(ctx);
|
|
1371
|
+
}
|
|
1372
|
+
function parsePostfixWithIncDec(ctx) {
|
|
1373
|
+
const expr = parsePostfix(ctx);
|
|
1374
|
+
const tok = ctx.peek();
|
|
1375
|
+
if (tok.type === "Operator" && (tok.value === "++" || tok.value === "--")) {
|
|
1376
|
+
ctx.consume();
|
|
1377
|
+
return {
|
|
1378
|
+
kind: "BuiltinCall",
|
|
1379
|
+
name: "__rui_postfix__",
|
|
1380
|
+
arguments: [
|
|
1381
|
+
expr,
|
|
1382
|
+
{ kind: "Literal", value: tok.value }
|
|
1383
|
+
],
|
|
1384
|
+
loc: { line: tok.line, column: tok.column }
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
return expr;
|
|
1388
|
+
}
|
|
1389
|
+
function parsePostfix(ctx) {
|
|
1390
|
+
return parsePostfixFrom(ctx, parsePrimary(ctx));
|
|
1391
|
+
}
|
|
1392
|
+
function parsePostfixFrom(ctx, base) {
|
|
1393
|
+
let expr = base;
|
|
1394
|
+
while (true) {
|
|
1395
|
+
consumeNewlinesIfNext(
|
|
1396
|
+
ctx,
|
|
1397
|
+
(t) => t.type === "Punctuation" && t.value === "." || t.type === "Operator" && t.value === "?."
|
|
1398
|
+
);
|
|
1399
|
+
const tok = ctx.peek();
|
|
1400
|
+
if (tok.type === "Punctuation" && tok.value === ".") {
|
|
1401
|
+
ctx.consume();
|
|
1402
|
+
const propTok = ctx.consume();
|
|
1403
|
+
if (propTok.type !== "Identifier" && propTok.type !== "Keyword") {
|
|
1404
|
+
throw {
|
|
1405
|
+
message: `Expected Identifier but got ${propTok.type} "${propTok.value}"`,
|
|
1406
|
+
line: propTok.line,
|
|
1407
|
+
column: propTok.column
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
const after = ctx.peek();
|
|
1411
|
+
if (after.type === "Punctuation" && after.value === "(") {
|
|
1412
|
+
ctx.consume();
|
|
1413
|
+
const args = parseCallArgs(ctx);
|
|
1414
|
+
ctx.expect("Punctuation", ")");
|
|
1415
|
+
expr = {
|
|
1416
|
+
kind: "MethodCall",
|
|
1417
|
+
object: expr,
|
|
1418
|
+
method: propTok.value,
|
|
1419
|
+
arguments: args,
|
|
1420
|
+
loc: { line: propTok.line, column: propTok.column }
|
|
1421
|
+
};
|
|
1422
|
+
continue;
|
|
1423
|
+
}
|
|
1424
|
+
expr = { kind: "Member", object: expr, property: propTok.value };
|
|
1425
|
+
continue;
|
|
1426
|
+
}
|
|
1427
|
+
if (tok.type === "Operator" && tok.value === "?.") {
|
|
1428
|
+
ctx.consume();
|
|
1429
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "[") {
|
|
1430
|
+
ctx.consume();
|
|
1431
|
+
const computed = parseExpression(ctx);
|
|
1432
|
+
ctx.expect("Punctuation", "]");
|
|
1433
|
+
expr = { kind: "Member", object: expr, computed, optional: true };
|
|
1434
|
+
} else if (ctx.peek().type === "Punctuation" && ctx.peek().value === "(") {
|
|
1435
|
+
ctx.consume();
|
|
1436
|
+
const args = parseCallArgs(ctx);
|
|
1437
|
+
ctx.expect("Punctuation", ")");
|
|
1438
|
+
expr = {
|
|
1439
|
+
kind: "Invoke",
|
|
1440
|
+
callee: expr,
|
|
1441
|
+
arguments: args,
|
|
1442
|
+
optional: true,
|
|
1443
|
+
loc: { line: tok.line, column: tok.column }
|
|
1444
|
+
};
|
|
1445
|
+
} else {
|
|
1446
|
+
const propTok = ctx.consume();
|
|
1447
|
+
if (propTok.type !== "Identifier" && propTok.type !== "Keyword") {
|
|
1448
|
+
throw {
|
|
1449
|
+
message: `Expected Identifier but got ${propTok.type} "${propTok.value}"`,
|
|
1450
|
+
line: propTok.line,
|
|
1451
|
+
column: propTok.column
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
const after = ctx.peek();
|
|
1455
|
+
if (after.type === "Punctuation" && after.value === "(") {
|
|
1456
|
+
ctx.consume();
|
|
1457
|
+
const args = parseCallArgs(ctx);
|
|
1458
|
+
ctx.expect("Punctuation", ")");
|
|
1459
|
+
expr = {
|
|
1460
|
+
kind: "MethodCall",
|
|
1461
|
+
object: expr,
|
|
1462
|
+
method: propTok.value,
|
|
1463
|
+
arguments: args,
|
|
1464
|
+
optional: true,
|
|
1465
|
+
loc: { line: propTok.line, column: propTok.column }
|
|
1466
|
+
};
|
|
1467
|
+
continue;
|
|
1468
|
+
}
|
|
1469
|
+
expr = { kind: "Member", object: expr, property: propTok.value, optional: true };
|
|
1470
|
+
}
|
|
1471
|
+
continue;
|
|
1472
|
+
}
|
|
1473
|
+
if (tok.type === "Punctuation" && tok.value === "[") {
|
|
1474
|
+
ctx.consume();
|
|
1475
|
+
const computed = parseExpression(ctx);
|
|
1476
|
+
ctx.expect("Punctuation", "]");
|
|
1477
|
+
expr = { kind: "Member", object: expr, computed };
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
if (tok.type === "Punctuation" && tok.value === "(") {
|
|
1481
|
+
ctx.consume();
|
|
1482
|
+
const args = parseCallArgs(ctx);
|
|
1483
|
+
ctx.expect("Punctuation", ")");
|
|
1484
|
+
expr = {
|
|
1485
|
+
kind: "Invoke",
|
|
1486
|
+
callee: expr,
|
|
1487
|
+
arguments: args,
|
|
1488
|
+
loc: { line: tok.line, column: tok.column }
|
|
1489
|
+
};
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
break;
|
|
1493
|
+
}
|
|
1494
|
+
return expr;
|
|
1495
|
+
}
|
|
1496
|
+
function parsePrimary(ctx) {
|
|
1497
|
+
const tok = ctx.peek();
|
|
1498
|
+
if (tok.type === "Keyword" && (tok.value === "if" || tok.value === "for" || tok.value === "switch" || tok.value === "while" || tok.value === "try")) {
|
|
1499
|
+
const hint = tok.value === "if" ? "Use the ternary operator (`cond ? a : b`) when you need a value." : tok.value === "for" ? "Use `arr.map(x => …)` (or `.filter`, `.reduce`, …) to collect bodies into an array." : tok.value === "switch" ? "Use chained ternaries, an object lookup, or wrap the switch inside a `function`." : `Use the ${tok.value} statement inside a function / effect body.`;
|
|
1500
|
+
const err = {
|
|
1501
|
+
message: `\`${tok.value}\` is a statement, not an expression. ${hint}`,
|
|
1502
|
+
line: tok.line,
|
|
1503
|
+
column: tok.column
|
|
1504
|
+
};
|
|
1505
|
+
err.__definitive = true;
|
|
1506
|
+
throw err;
|
|
1507
|
+
}
|
|
1508
|
+
if (tok.type === "Keyword") {
|
|
1509
|
+
if (tok.value === "function") {
|
|
1510
|
+
const lookahead = ctx.peek(1);
|
|
1511
|
+
const lookahead2 = ctx.peek(2);
|
|
1512
|
+
const looksLikeFunctionExpr = lookahead.type === "Punctuation" && lookahead.value === "(" || lookahead.type === "Identifier" && lookahead2.type === "Punctuation" && lookahead2.value === "(";
|
|
1513
|
+
if (looksLikeFunctionExpr) {
|
|
1514
|
+
const start = tok;
|
|
1515
|
+
ctx.consume();
|
|
1516
|
+
if (ctx.peek().type === "Identifier") ctx.consume();
|
|
1517
|
+
const params = parseFunctionParams(ctx);
|
|
1518
|
+
const body = parseBlock(ctx);
|
|
1519
|
+
return {
|
|
1520
|
+
kind: "Lambda",
|
|
1521
|
+
params,
|
|
1522
|
+
body,
|
|
1523
|
+
loc: { line: start.line, column: start.column }
|
|
1524
|
+
};
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
if (tok.value === "function" || tok.value === "let" || tok.value === "const" || tok.value === "var" || tok.value === "of" || tok.value === "in" || tok.value === "case" || tok.value === "break" || tok.value === "continue" || tok.value === "default") {
|
|
1528
|
+
ctx.consume();
|
|
1529
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "(") {
|
|
1530
|
+
ctx.consume();
|
|
1531
|
+
const args = parseCallArgs(ctx);
|
|
1532
|
+
ctx.expect("Punctuation", ")");
|
|
1533
|
+
return {
|
|
1534
|
+
kind: "Call",
|
|
1535
|
+
callee: tok.value,
|
|
1536
|
+
arguments: args,
|
|
1537
|
+
loc: { line: tok.line, column: tok.column }
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
return { kind: "Identifier", name: tok.value, loc: { line: tok.line, column: tok.column } };
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
if (tok.type === "Number") {
|
|
1544
|
+
ctx.consume();
|
|
1545
|
+
return { kind: "Literal", value: numericLiteralValue(tok.value) };
|
|
1546
|
+
}
|
|
1547
|
+
if (tok.type === "String") {
|
|
1548
|
+
ctx.consume();
|
|
1549
|
+
return { kind: "Literal", value: tok.value };
|
|
1550
|
+
}
|
|
1551
|
+
if (tok.type === "TemplateString") {
|
|
1552
|
+
ctx.consume();
|
|
1553
|
+
const parts = tok.parts ?? [];
|
|
1554
|
+
const quasis = [];
|
|
1555
|
+
const expressions = [];
|
|
1556
|
+
let pendingChunk = "";
|
|
1557
|
+
let hasPendingChunk = false;
|
|
1558
|
+
const flushChunk = () => {
|
|
1559
|
+
quasis.push(pendingChunk);
|
|
1560
|
+
pendingChunk = "";
|
|
1561
|
+
hasPendingChunk = false;
|
|
1562
|
+
};
|
|
1563
|
+
for (const part of parts) {
|
|
1564
|
+
if (part.kind === "str") {
|
|
1565
|
+
pendingChunk += part.text;
|
|
1566
|
+
hasPendingChunk = true;
|
|
1567
|
+
continue;
|
|
1568
|
+
}
|
|
1569
|
+
if (!hasPendingChunk) {
|
|
1570
|
+
quasis.push("");
|
|
1571
|
+
} else {
|
|
1572
|
+
flushChunk();
|
|
1573
|
+
}
|
|
1574
|
+
const sub = parse(`__rui_tmpl__ = ${part.source}`);
|
|
1575
|
+
const firstStmt = sub.statements[0];
|
|
1576
|
+
if (firstStmt && firstStmt.kind === "Assignment") {
|
|
1577
|
+
expressions.push(firstStmt.expression);
|
|
1578
|
+
} else {
|
|
1579
|
+
expressions.push({ kind: "Literal", value: "" });
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
if (hasPendingChunk || quasis.length === 0) {
|
|
1583
|
+
quasis.push(pendingChunk);
|
|
1584
|
+
}
|
|
1585
|
+
while (quasis.length <= expressions.length) quasis.push("");
|
|
1586
|
+
return {
|
|
1587
|
+
kind: "Template",
|
|
1588
|
+
quasis,
|
|
1589
|
+
expressions,
|
|
1590
|
+
loc: { line: tok.line, column: tok.column }
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
if (tok.type === "Boolean") {
|
|
1594
|
+
ctx.consume();
|
|
1595
|
+
return { kind: "Literal", value: tok.value === "true" };
|
|
1596
|
+
}
|
|
1597
|
+
if (tok.type === "Null") {
|
|
1598
|
+
ctx.consume();
|
|
1599
|
+
return { kind: "Literal", value: null };
|
|
1600
|
+
}
|
|
1601
|
+
if (tok.type === "StateIdentifier") {
|
|
1602
|
+
if (tok.value === "effect" && ctx.peek(1).type === "Punctuation" && ctx.peek(1).value === "(") {
|
|
1603
|
+
ctx.consume();
|
|
1604
|
+
return parseEffectCallAsExpr(ctx, tok);
|
|
1605
|
+
}
|
|
1606
|
+
ctx.consume();
|
|
1607
|
+
return { kind: "StateRef", name: tok.value };
|
|
1608
|
+
}
|
|
1609
|
+
if (tok.type === "Identifier") {
|
|
1610
|
+
ctx.consume();
|
|
1611
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "=>") {
|
|
1612
|
+
ctx.consume();
|
|
1613
|
+
const body = parseLambdaBody(ctx);
|
|
1614
|
+
return {
|
|
1615
|
+
kind: "Lambda",
|
|
1616
|
+
params: [{ name: tok.value }],
|
|
1617
|
+
body,
|
|
1618
|
+
loc: { line: tok.line, column: tok.column }
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "(") {
|
|
1622
|
+
ctx.consume();
|
|
1623
|
+
const args = parseCallArgs(ctx);
|
|
1624
|
+
ctx.expect("Punctuation", ")");
|
|
1625
|
+
return {
|
|
1626
|
+
kind: "Call",
|
|
1627
|
+
callee: tok.value,
|
|
1628
|
+
arguments: args,
|
|
1629
|
+
loc: { line: tok.line, column: tok.column }
|
|
1630
|
+
};
|
|
1631
|
+
}
|
|
1632
|
+
return {
|
|
1633
|
+
kind: "Identifier",
|
|
1634
|
+
name: tok.value,
|
|
1635
|
+
loc: { line: tok.line, column: tok.column }
|
|
1636
|
+
};
|
|
1637
|
+
}
|
|
1638
|
+
if (tok.type === "Punctuation" && tok.value === "[") {
|
|
1639
|
+
ctx.consume();
|
|
1640
|
+
const elements = parseCallArgs(ctx);
|
|
1641
|
+
ctx.expect("Punctuation", "]");
|
|
1642
|
+
return { kind: "Array", elements };
|
|
1643
|
+
}
|
|
1644
|
+
if (tok.type === "Punctuation" && tok.value === "{") {
|
|
1645
|
+
ctx.consume();
|
|
1646
|
+
const properties = parseObjectProps(ctx);
|
|
1647
|
+
ctx.expect("Punctuation", "}");
|
|
1648
|
+
return { kind: "Object", properties };
|
|
1649
|
+
}
|
|
1650
|
+
if (tok.type === "Punctuation" && tok.value === "(") {
|
|
1651
|
+
const saved = ctx.snapshot();
|
|
1652
|
+
const lambda = tryParseLambdaFromParenList(ctx);
|
|
1653
|
+
if (lambda) return lambda;
|
|
1654
|
+
ctx.restore(saved);
|
|
1655
|
+
ctx.consume();
|
|
1656
|
+
const expr = parseExpression(ctx);
|
|
1657
|
+
ctx.expect("Punctuation", ")");
|
|
1658
|
+
return expr;
|
|
1659
|
+
}
|
|
1660
|
+
throw {
|
|
1661
|
+
message: `Unexpected token ${tok.type} "${tok.value}"`,
|
|
1662
|
+
line: tok.line,
|
|
1663
|
+
column: tok.column
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
function parseEffectCallAsExpr(ctx, nameTok) {
|
|
1667
|
+
ctx.consume();
|
|
1668
|
+
const args = parseCallArgs(ctx);
|
|
1669
|
+
ctx.expect("Punctuation", ")");
|
|
1670
|
+
return {
|
|
1671
|
+
kind: "Call",
|
|
1672
|
+
callee: nameTok.value,
|
|
1673
|
+
arguments: args,
|
|
1674
|
+
loc: { line: nameTok.line, column: nameTok.column }
|
|
1675
|
+
};
|
|
1676
|
+
}
|
|
1677
|
+
function parseCallArgs(ctx) {
|
|
1678
|
+
const args = [];
|
|
1679
|
+
skipWhitespace(ctx);
|
|
1680
|
+
if (ctx.peek().type === "Punctuation" && (ctx.peek().value === ")" || ctx.peek().value === "]")) {
|
|
1681
|
+
return args;
|
|
1682
|
+
}
|
|
1683
|
+
args.push(parseArgItem(ctx));
|
|
1684
|
+
skipWhitespace(ctx);
|
|
1685
|
+
while (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
1686
|
+
ctx.consume();
|
|
1687
|
+
skipWhitespace(ctx);
|
|
1688
|
+
if (ctx.peek().type === "Punctuation" && (ctx.peek().value === ")" || ctx.peek().value === "]")) {
|
|
1689
|
+
break;
|
|
1690
|
+
}
|
|
1691
|
+
args.push(parseArgItem(ctx));
|
|
1692
|
+
skipWhitespace(ctx);
|
|
1693
|
+
}
|
|
1694
|
+
skipWhitespace(ctx);
|
|
1695
|
+
return args;
|
|
1696
|
+
}
|
|
1697
|
+
function parseArgItem(ctx) {
|
|
1698
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "...") {
|
|
1699
|
+
const tok = ctx.consume();
|
|
1700
|
+
const argument = parseExpression(ctx);
|
|
1701
|
+
return { kind: "Spread", argument, loc: { line: tok.line, column: tok.column } };
|
|
1702
|
+
}
|
|
1703
|
+
return parseExpression(ctx);
|
|
1704
|
+
}
|
|
1705
|
+
function parseIfStatement(ctx) {
|
|
1706
|
+
const start = ctx.expect("Keyword", "if");
|
|
1707
|
+
ctx.expect("Punctuation", "(");
|
|
1708
|
+
const test = parseExpression(ctx);
|
|
1709
|
+
ctx.expect("Punctuation", ")");
|
|
1710
|
+
const consequent = parseBlockOrSingleStatement(ctx);
|
|
1711
|
+
let alternate;
|
|
1712
|
+
skipWhitespace(ctx);
|
|
1713
|
+
if (ctx.peek().type === "Keyword" && ctx.peek().value === "else") {
|
|
1714
|
+
ctx.consume();
|
|
1715
|
+
skipWhitespace(ctx);
|
|
1716
|
+
if (ctx.peek().type === "Keyword" && ctx.peek().value === "if") {
|
|
1717
|
+
alternate = parseIfStatement(ctx);
|
|
1718
|
+
} else {
|
|
1719
|
+
alternate = parseBlockOrSingleStatement(ctx);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
skipTerminator(ctx);
|
|
1723
|
+
return {
|
|
1724
|
+
kind: "IfStatement",
|
|
1725
|
+
test,
|
|
1726
|
+
consequent,
|
|
1727
|
+
alternate,
|
|
1728
|
+
loc: { line: start.line, column: start.column }
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
function parseSwitchStatement(ctx) {
|
|
1732
|
+
const start = ctx.expect("Keyword", "switch");
|
|
1733
|
+
ctx.expect("Punctuation", "(");
|
|
1734
|
+
const discriminant = parseExpression(ctx);
|
|
1735
|
+
ctx.expect("Punctuation", ")");
|
|
1736
|
+
ctx.expect("Punctuation", "{");
|
|
1737
|
+
const cases = [];
|
|
1738
|
+
skipWhitespace(ctx);
|
|
1739
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
1740
|
+
let test = null;
|
|
1741
|
+
if (ctx.peek().type === "Keyword" && ctx.peek().value === "case") {
|
|
1742
|
+
ctx.consume();
|
|
1743
|
+
test = parseExpression(ctx);
|
|
1744
|
+
} else if (ctx.peek().type === "Keyword" && ctx.peek().value === "default") {
|
|
1745
|
+
ctx.consume();
|
|
1746
|
+
test = null;
|
|
1747
|
+
} else {
|
|
1748
|
+
throw {
|
|
1749
|
+
message: `Expected "case" or "default" in switch body, got ${ctx.peek().type} "${ctx.peek().value}"`,
|
|
1750
|
+
line: ctx.peek().line,
|
|
1751
|
+
column: ctx.peek().column
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
ctx.expect("Punctuation", ":");
|
|
1755
|
+
skipWhitespace(ctx);
|
|
1756
|
+
const body = [];
|
|
1757
|
+
while (!ctx.isEnd() && !(ctx.peek().type === "Keyword" && (ctx.peek().value === "case" || ctx.peek().value === "default")) && !(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
1758
|
+
const stmt = parseStatement(ctx);
|
|
1759
|
+
if (stmt) body.push(stmt);
|
|
1760
|
+
skipWhitespace(ctx);
|
|
1761
|
+
}
|
|
1762
|
+
cases.push({ test, body });
|
|
1763
|
+
skipWhitespace(ctx);
|
|
1764
|
+
}
|
|
1765
|
+
ctx.expect("Punctuation", "}");
|
|
1766
|
+
skipTerminator(ctx);
|
|
1767
|
+
return {
|
|
1768
|
+
kind: "SwitchStatement",
|
|
1769
|
+
discriminant,
|
|
1770
|
+
cases,
|
|
1771
|
+
loc: { line: start.line, column: start.column }
|
|
1772
|
+
};
|
|
1773
|
+
}
|
|
1774
|
+
function parseForStatement(ctx) {
|
|
1775
|
+
const start = ctx.expect("Keyword", "for");
|
|
1776
|
+
ctx.expect("Punctuation", "(");
|
|
1777
|
+
skipWhitespace(ctx);
|
|
1778
|
+
const headSnapshot = ctx.snapshot();
|
|
1779
|
+
let kind = "for-of";
|
|
1780
|
+
{
|
|
1781
|
+
let depth = 1;
|
|
1782
|
+
let i = 0;
|
|
1783
|
+
while (true) {
|
|
1784
|
+
const tok = ctx.peek(i);
|
|
1785
|
+
if (tok.type === "EOF") break;
|
|
1786
|
+
if (tok.type === "Punctuation" && tok.value === "(") depth += 1;
|
|
1787
|
+
else if (tok.type === "Punctuation" && tok.value === ")") {
|
|
1788
|
+
depth -= 1;
|
|
1789
|
+
if (depth === 0) break;
|
|
1790
|
+
} else if (depth === 1 && tok.type === "Semicolon") {
|
|
1791
|
+
kind = "classic";
|
|
1792
|
+
break;
|
|
1793
|
+
} else if (depth === 1 && tok.type === "Keyword" && tok.value === "of") {
|
|
1794
|
+
kind = "for-of";
|
|
1795
|
+
break;
|
|
1796
|
+
} else if (depth === 1 && tok.type === "Keyword" && tok.value === "in") {
|
|
1797
|
+
kind = "for-in";
|
|
1798
|
+
break;
|
|
1799
|
+
}
|
|
1800
|
+
i += 1;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
ctx.restore(headSnapshot);
|
|
1804
|
+
if (kind === "classic") return parseForClassic(ctx, start);
|
|
1805
|
+
if (ctx.peek().type === "Keyword" && (ctx.peek().value === "let" || ctx.peek().value === "const" || ctx.peek().value === "var")) {
|
|
1806
|
+
ctx.consume();
|
|
1807
|
+
}
|
|
1808
|
+
skipWhitespace(ctx);
|
|
1809
|
+
let item;
|
|
1810
|
+
let index;
|
|
1811
|
+
let destructure;
|
|
1812
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "[") {
|
|
1813
|
+
ctx.consume();
|
|
1814
|
+
item = ctx.expect("Identifier").value;
|
|
1815
|
+
ctx.expect("Punctuation", ",");
|
|
1816
|
+
index = ctx.expect("Identifier").value;
|
|
1817
|
+
ctx.expect("Punctuation", "]");
|
|
1818
|
+
} else if (ctx.peek().type === "Punctuation" && ctx.peek().value === "{") {
|
|
1819
|
+
ctx.consume();
|
|
1820
|
+
const fields = [];
|
|
1821
|
+
while (!(ctx.peek().type === "Punctuation" && ctx.peek().value === "}")) {
|
|
1822
|
+
fields.push(ctx.expect("Identifier").value);
|
|
1823
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") ctx.consume();
|
|
1824
|
+
}
|
|
1825
|
+
ctx.expect("Punctuation", "}");
|
|
1826
|
+
item = "__row";
|
|
1827
|
+
destructure = fields;
|
|
1828
|
+
} else {
|
|
1829
|
+
item = ctx.expect("Identifier").value;
|
|
1830
|
+
}
|
|
1831
|
+
if (kind === "for-in") {
|
|
1832
|
+
ctx.expect("Keyword", "in");
|
|
1833
|
+
} else {
|
|
1834
|
+
ctx.expect("Keyword", "of");
|
|
1835
|
+
}
|
|
1836
|
+
const iterable = parseExpression(ctx);
|
|
1837
|
+
ctx.expect("Punctuation", ")");
|
|
1838
|
+
const body = parseBlockOrSingleStatement(ctx);
|
|
1839
|
+
skipTerminator(ctx);
|
|
1840
|
+
if (kind === "for-in") {
|
|
1841
|
+
return {
|
|
1842
|
+
kind: "ForInStatement",
|
|
1843
|
+
item,
|
|
1844
|
+
iterable,
|
|
1845
|
+
body,
|
|
1846
|
+
loc: { line: start.line, column: start.column }
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
return {
|
|
1850
|
+
kind: "ForOfStatement",
|
|
1851
|
+
item,
|
|
1852
|
+
index,
|
|
1853
|
+
destructure,
|
|
1854
|
+
iterable,
|
|
1855
|
+
body,
|
|
1856
|
+
loc: { line: start.line, column: start.column }
|
|
1857
|
+
};
|
|
1858
|
+
}
|
|
1859
|
+
function parseForClassic(ctx, start) {
|
|
1860
|
+
let init;
|
|
1861
|
+
if (!(ctx.peek().type === "Semicolon")) {
|
|
1862
|
+
if (ctx.peek().type === "Keyword" && (ctx.peek().value === "let" || ctx.peek().value === "const" || ctx.peek().value === "var")) {
|
|
1863
|
+
const decl = parseVarDecl(ctx);
|
|
1864
|
+
if (decl.kind === "Assignment") init = decl;
|
|
1865
|
+
skipWhitespace(ctx);
|
|
1866
|
+
} else {
|
|
1867
|
+
const exprStart = ctx.peek();
|
|
1868
|
+
const expression = parseExpression(ctx);
|
|
1869
|
+
init = {
|
|
1870
|
+
kind: "ExpressionStatement",
|
|
1871
|
+
expression,
|
|
1872
|
+
loc: { line: exprStart.line, column: exprStart.column }
|
|
1873
|
+
};
|
|
1874
|
+
ctx.expect("Semicolon");
|
|
1875
|
+
skipWhitespace(ctx);
|
|
1876
|
+
}
|
|
1877
|
+
} else {
|
|
1878
|
+
ctx.expect("Semicolon");
|
|
1879
|
+
skipWhitespace(ctx);
|
|
1880
|
+
}
|
|
1881
|
+
let test;
|
|
1882
|
+
if (!(ctx.peek().type === "Semicolon")) {
|
|
1883
|
+
test = parseExpression(ctx);
|
|
1884
|
+
}
|
|
1885
|
+
ctx.expect("Semicolon");
|
|
1886
|
+
skipWhitespace(ctx);
|
|
1887
|
+
let update;
|
|
1888
|
+
if (!(ctx.peek().type === "Punctuation" && ctx.peek().value === ")")) {
|
|
1889
|
+
update = parseAssignmentLikeExpression(ctx);
|
|
1890
|
+
}
|
|
1891
|
+
ctx.expect("Punctuation", ")");
|
|
1892
|
+
const body = parseBlockOrSingleStatement(ctx);
|
|
1893
|
+
skipTerminator(ctx);
|
|
1894
|
+
return {
|
|
1895
|
+
kind: "ForClassicStatement",
|
|
1896
|
+
init,
|
|
1897
|
+
test,
|
|
1898
|
+
update,
|
|
1899
|
+
body,
|
|
1900
|
+
loc: { line: start.line, column: start.column }
|
|
1901
|
+
};
|
|
1902
|
+
}
|
|
1903
|
+
function parseWhileStatement(ctx) {
|
|
1904
|
+
const start = ctx.expect("Keyword", "while");
|
|
1905
|
+
ctx.expect("Punctuation", "(");
|
|
1906
|
+
const test = parseExpression(ctx);
|
|
1907
|
+
ctx.expect("Punctuation", ")");
|
|
1908
|
+
const body = parseBlockOrSingleStatement(ctx);
|
|
1909
|
+
skipTerminator(ctx);
|
|
1910
|
+
return {
|
|
1911
|
+
kind: "WhileStatement",
|
|
1912
|
+
test,
|
|
1913
|
+
body,
|
|
1914
|
+
loc: { line: start.line, column: start.column }
|
|
1915
|
+
};
|
|
1916
|
+
}
|
|
1917
|
+
function parseDoWhileStatement(ctx) {
|
|
1918
|
+
const start = ctx.expect("Keyword", "do");
|
|
1919
|
+
const body = parseBlockOrSingleStatement(ctx);
|
|
1920
|
+
skipWhitespace(ctx);
|
|
1921
|
+
ctx.expect("Keyword", "while");
|
|
1922
|
+
ctx.expect("Punctuation", "(");
|
|
1923
|
+
const test = parseExpression(ctx);
|
|
1924
|
+
ctx.expect("Punctuation", ")");
|
|
1925
|
+
skipTerminator(ctx);
|
|
1926
|
+
return {
|
|
1927
|
+
kind: "DoWhileStatement",
|
|
1928
|
+
test,
|
|
1929
|
+
body,
|
|
1930
|
+
loc: { line: start.line, column: start.column }
|
|
1931
|
+
};
|
|
1932
|
+
}
|
|
1933
|
+
function parseBreakStatement(ctx) {
|
|
1934
|
+
const start = ctx.expect("Keyword", "break");
|
|
1935
|
+
skipTerminator(ctx);
|
|
1936
|
+
return { kind: "BreakStatement", loc: { line: start.line, column: start.column } };
|
|
1937
|
+
}
|
|
1938
|
+
function parseContinueStatement(ctx) {
|
|
1939
|
+
const start = ctx.expect("Keyword", "continue");
|
|
1940
|
+
skipTerminator(ctx);
|
|
1941
|
+
return { kind: "ContinueStatement", loc: { line: start.line, column: start.column } };
|
|
1942
|
+
}
|
|
1943
|
+
function parseThrowStatement(ctx) {
|
|
1944
|
+
const start = ctx.expect("Keyword", "throw");
|
|
1945
|
+
const argument = parseExpression(ctx);
|
|
1946
|
+
skipTerminator(ctx);
|
|
1947
|
+
return {
|
|
1948
|
+
kind: "ThrowStatement",
|
|
1949
|
+
argument,
|
|
1950
|
+
loc: { line: start.line, column: start.column }
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
function parseTryStatement(ctx) {
|
|
1954
|
+
const start = ctx.expect("Keyword", "try");
|
|
1955
|
+
const block = parseBlock(ctx);
|
|
1956
|
+
let catchParam;
|
|
1957
|
+
let catchBlock;
|
|
1958
|
+
let finallyBlock;
|
|
1959
|
+
skipWhitespace(ctx);
|
|
1960
|
+
if (ctx.peek().type === "Keyword" && ctx.peek().value === "catch") {
|
|
1961
|
+
ctx.consume();
|
|
1962
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "(") {
|
|
1963
|
+
ctx.consume();
|
|
1964
|
+
if (ctx.peek().type === "Identifier") {
|
|
1965
|
+
catchParam = ctx.consume().value;
|
|
1966
|
+
}
|
|
1967
|
+
ctx.expect("Punctuation", ")");
|
|
1968
|
+
}
|
|
1969
|
+
catchBlock = parseBlock(ctx);
|
|
1970
|
+
skipWhitespace(ctx);
|
|
1971
|
+
}
|
|
1972
|
+
if (ctx.peek().type === "Keyword" && ctx.peek().value === "finally") {
|
|
1973
|
+
ctx.consume();
|
|
1974
|
+
finallyBlock = parseBlock(ctx);
|
|
1975
|
+
}
|
|
1976
|
+
skipTerminator(ctx);
|
|
1977
|
+
return {
|
|
1978
|
+
kind: "TryStatement",
|
|
1979
|
+
block,
|
|
1980
|
+
catchParam,
|
|
1981
|
+
catchBlock,
|
|
1982
|
+
finallyBlock,
|
|
1983
|
+
loc: { line: start.line, column: start.column }
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
function tryParseLambdaFromParenList(ctx) {
|
|
1987
|
+
const start = ctx.peek();
|
|
1988
|
+
if (start.type !== "Punctuation" || start.value !== "(") return null;
|
|
1989
|
+
ctx.consume();
|
|
1990
|
+
const params = [];
|
|
1991
|
+
skipWhitespace(ctx);
|
|
1992
|
+
if (!(ctx.peek().type === "Punctuation" && ctx.peek().value === ")")) {
|
|
1993
|
+
while (true) {
|
|
1994
|
+
skipWhitespace(ctx);
|
|
1995
|
+
let isRest = false;
|
|
1996
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "...") {
|
|
1997
|
+
ctx.consume();
|
|
1998
|
+
isRest = true;
|
|
1999
|
+
}
|
|
2000
|
+
const tok = ctx.peek();
|
|
2001
|
+
if (!isRest && tok.type === "Punctuation" && (tok.value === "{" || tok.value === "[")) {
|
|
2002
|
+
let pattern;
|
|
2003
|
+
try {
|
|
2004
|
+
pattern = parseDestructuringPattern(ctx);
|
|
2005
|
+
} catch {
|
|
2006
|
+
return null;
|
|
2007
|
+
}
|
|
2008
|
+
const param2 = { name: "", pattern };
|
|
2009
|
+
if (ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
2010
|
+
ctx.consume();
|
|
2011
|
+
try {
|
|
2012
|
+
param2.defaultValue = parseExpression(ctx);
|
|
2013
|
+
} catch {
|
|
2014
|
+
return null;
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
params.push(param2);
|
|
2018
|
+
skipWhitespace(ctx);
|
|
2019
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
2020
|
+
ctx.consume();
|
|
2021
|
+
continue;
|
|
2022
|
+
}
|
|
2023
|
+
break;
|
|
2024
|
+
}
|
|
2025
|
+
if (tok.type !== "Identifier") return null;
|
|
2026
|
+
ctx.consume();
|
|
2027
|
+
const param = { name: tok.value };
|
|
2028
|
+
if (isRest) param.rest = true;
|
|
2029
|
+
if (!isRest && ctx.peek().type === "Operator" && ctx.peek().value === "=") {
|
|
2030
|
+
ctx.consume();
|
|
2031
|
+
try {
|
|
2032
|
+
param.defaultValue = parseExpression(ctx);
|
|
2033
|
+
} catch {
|
|
2034
|
+
return null;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
params.push(param);
|
|
2038
|
+
skipWhitespace(ctx);
|
|
2039
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
2040
|
+
if (isRest) return null;
|
|
2041
|
+
ctx.consume();
|
|
2042
|
+
continue;
|
|
2043
|
+
}
|
|
2044
|
+
break;
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
skipWhitespace(ctx);
|
|
2048
|
+
if (!(ctx.peek().type === "Punctuation" && ctx.peek().value === ")")) {
|
|
2049
|
+
return null;
|
|
2050
|
+
}
|
|
2051
|
+
ctx.consume();
|
|
2052
|
+
if (!(ctx.peek().type === "Operator" && ctx.peek().value === "=>")) {
|
|
2053
|
+
return null;
|
|
2054
|
+
}
|
|
2055
|
+
ctx.consume();
|
|
2056
|
+
const body = parseLambdaBody(ctx);
|
|
2057
|
+
return {
|
|
2058
|
+
kind: "Lambda",
|
|
2059
|
+
params,
|
|
2060
|
+
body,
|
|
2061
|
+
loc: { line: start.line, column: start.column }
|
|
2062
|
+
};
|
|
2063
|
+
}
|
|
2064
|
+
function parseLambdaBody(ctx) {
|
|
2065
|
+
skipWhitespace(ctx);
|
|
2066
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "{") {
|
|
2067
|
+
return parseBlock(ctx);
|
|
2068
|
+
}
|
|
2069
|
+
return parseAssignmentLikeExpression(ctx);
|
|
2070
|
+
}
|
|
2071
|
+
function parseAssignmentLikeExpression(ctx) {
|
|
2072
|
+
const expression = parseExpression(ctx);
|
|
2073
|
+
const next = ctx.peek();
|
|
2074
|
+
if (next.type === "Operator") {
|
|
2075
|
+
if (isAssignmentOperator(next.value)) {
|
|
2076
|
+
ctx.consume();
|
|
2077
|
+
const value = parseExpression(ctx);
|
|
2078
|
+
return {
|
|
2079
|
+
kind: "BuiltinCall",
|
|
2080
|
+
name: "__rui_assign__",
|
|
2081
|
+
arguments: [
|
|
2082
|
+
expression,
|
|
2083
|
+
value,
|
|
2084
|
+
{ kind: "Literal", value: next.value }
|
|
2085
|
+
],
|
|
2086
|
+
loc: { line: next.line, column: next.column }
|
|
2087
|
+
};
|
|
2088
|
+
}
|
|
2089
|
+
if (next.value === "++" || next.value === "--") {
|
|
2090
|
+
ctx.consume();
|
|
2091
|
+
return {
|
|
2092
|
+
kind: "BuiltinCall",
|
|
2093
|
+
name: "__rui_postfix__",
|
|
2094
|
+
arguments: [
|
|
2095
|
+
expression,
|
|
2096
|
+
{ kind: "Literal", value: next.value }
|
|
2097
|
+
],
|
|
2098
|
+
loc: { line: next.line, column: next.column }
|
|
2099
|
+
};
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
return expression;
|
|
2103
|
+
}
|
|
2104
|
+
function parseObjectProps(ctx) {
|
|
2105
|
+
const props = [];
|
|
2106
|
+
skipWhitespace(ctx);
|
|
2107
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "}") return props;
|
|
2108
|
+
while (true) {
|
|
2109
|
+
skipWhitespace(ctx);
|
|
2110
|
+
const keyTok = ctx.peek();
|
|
2111
|
+
if (keyTok.type === "Operator" && keyTok.value === "...") {
|
|
2112
|
+
ctx.consume();
|
|
2113
|
+
const value2 = parseExpression(ctx);
|
|
2114
|
+
props.push({ key: "", value: value2, spread: true });
|
|
2115
|
+
skipWhitespace(ctx);
|
|
2116
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
2117
|
+
ctx.consume();
|
|
2118
|
+
skipWhitespace(ctx);
|
|
2119
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "}") break;
|
|
2120
|
+
continue;
|
|
2121
|
+
}
|
|
2122
|
+
break;
|
|
2123
|
+
}
|
|
2124
|
+
let key;
|
|
2125
|
+
let computedKey;
|
|
2126
|
+
if (keyTok.type === "Punctuation" && keyTok.value === "[") {
|
|
2127
|
+
ctx.consume();
|
|
2128
|
+
skipWhitespace(ctx);
|
|
2129
|
+
computedKey = parseExpression(ctx);
|
|
2130
|
+
skipWhitespace(ctx);
|
|
2131
|
+
ctx.expect("Punctuation", "]");
|
|
2132
|
+
key = "";
|
|
2133
|
+
} else if (keyTok.type === "Number") {
|
|
2134
|
+
key = ctx.consume().value;
|
|
2135
|
+
} else if (keyTok.type === "Identifier" || keyTok.type === "String" || keyTok.type === "Keyword") {
|
|
2136
|
+
key = ctx.consume().value;
|
|
2137
|
+
} else {
|
|
2138
|
+
throw {
|
|
2139
|
+
message: `Expected object key, got ${keyTok.type} "${keyTok.value}"`,
|
|
2140
|
+
line: keyTok.line,
|
|
2141
|
+
column: keyTok.column
|
|
2142
|
+
};
|
|
2143
|
+
}
|
|
2144
|
+
const after = ctx.peek();
|
|
2145
|
+
let value;
|
|
2146
|
+
if (!computedKey && keyTok.type === "Identifier" && after.type === "Punctuation" && (after.value === "," || after.value === "}")) {
|
|
2147
|
+
value = { kind: "Identifier", name: key, loc: { line: keyTok.line, column: keyTok.column } };
|
|
2148
|
+
} else {
|
|
2149
|
+
ctx.expect("Punctuation", ":");
|
|
2150
|
+
value = parseExpression(ctx);
|
|
2151
|
+
}
|
|
2152
|
+
const prop = { key, value };
|
|
2153
|
+
if (computedKey) prop.computedKey = computedKey;
|
|
2154
|
+
props.push(prop);
|
|
2155
|
+
skipWhitespace(ctx);
|
|
2156
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === ",") {
|
|
2157
|
+
ctx.consume();
|
|
2158
|
+
skipWhitespace(ctx);
|
|
2159
|
+
if (ctx.peek().type === "Punctuation" && ctx.peek().value === "}") break;
|
|
2160
|
+
continue;
|
|
2161
|
+
}
|
|
2162
|
+
break;
|
|
2163
|
+
}
|
|
2164
|
+
skipWhitespace(ctx);
|
|
2165
|
+
return props;
|
|
2166
|
+
}
|
|
2167
|
+
function skipWhitespace(ctx) {
|
|
2168
|
+
while (ctx.match("Newline") || ctx.match("Semicolon")) {
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
function skipTerminator(ctx) {
|
|
2172
|
+
if (!ctx.isEnd()) {
|
|
2173
|
+
ctx.match("Newline") || ctx.match("Semicolon");
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
function numericLiteralValue(raw) {
|
|
2177
|
+
let s = raw.replace(/_/g, "");
|
|
2178
|
+
let sign = 1;
|
|
2179
|
+
if (s.startsWith("-")) {
|
|
2180
|
+
sign = -1;
|
|
2181
|
+
s = s.slice(1);
|
|
2182
|
+
} else if (s.startsWith("+")) {
|
|
2183
|
+
s = s.slice(1);
|
|
2184
|
+
}
|
|
2185
|
+
return sign * Number(s);
|
|
2186
|
+
}
|
|
2187
|
+
const COMPILED_PROGRAM_VERSION = 1;
|
|
2188
|
+
function linkProgram(entrySource, entryPath, resolver) {
|
|
2189
|
+
const modules = /* @__PURE__ */ new Map();
|
|
2190
|
+
const order = [];
|
|
2191
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
2192
|
+
const diagnostics = [];
|
|
2193
|
+
let nextId = 0;
|
|
2194
|
+
const fail = (path, line, column, message) => {
|
|
2195
|
+
diagnostics.push({
|
|
2196
|
+
line,
|
|
2197
|
+
column,
|
|
2198
|
+
message: path === entryPath ? message : `${path}: ${message}`,
|
|
2199
|
+
severity: "error"
|
|
2200
|
+
});
|
|
2201
|
+
};
|
|
2202
|
+
function load(path, sourceOverride) {
|
|
2203
|
+
const existing = modules.get(path);
|
|
2204
|
+
if (existing) return existing;
|
|
2205
|
+
if (visiting.has(path)) return void 0;
|
|
2206
|
+
visiting.add(path);
|
|
2207
|
+
let src;
|
|
2208
|
+
if (sourceOverride !== null) {
|
|
2209
|
+
src = sourceOverride;
|
|
2210
|
+
} else {
|
|
2211
|
+
try {
|
|
2212
|
+
src = resolver.load(path);
|
|
2213
|
+
} catch {
|
|
2214
|
+
visiting.delete(path);
|
|
2215
|
+
fail(entryPath, 0, 0, `Failed to load imported module "${path}".`);
|
|
2216
|
+
return void 0;
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
const program = parse(src);
|
|
2220
|
+
const rec = {
|
|
2221
|
+
id: nextId++,
|
|
2222
|
+
path,
|
|
2223
|
+
program,
|
|
2224
|
+
edges: [],
|
|
2225
|
+
declaredPlain: /* @__PURE__ */ new Set(),
|
|
2226
|
+
declaredState: /* @__PURE__ */ new Set(),
|
|
2227
|
+
exportedPlain: /* @__PURE__ */ new Set(),
|
|
2228
|
+
exportedState: /* @__PURE__ */ new Set(),
|
|
2229
|
+
renamePlain: /* @__PURE__ */ new Map(),
|
|
2230
|
+
renameState: /* @__PURE__ */ new Map()
|
|
2231
|
+
};
|
|
2232
|
+
modules.set(path, rec);
|
|
2233
|
+
buildSymbolTable(rec);
|
|
2234
|
+
for (const stmt of program.statements) {
|
|
2235
|
+
if (stmt.kind !== "Import") continue;
|
|
2236
|
+
const resolved = resolver.resolve(stmt.source, path);
|
|
2237
|
+
rec.edges.push({ stmt, resolvedPath: resolved });
|
|
2238
|
+
if (resolved === null) {
|
|
2239
|
+
fail(path, stmt.loc?.line ?? 0, stmt.loc?.column ?? 0, `Cannot resolve import "${stmt.source}".`);
|
|
2240
|
+
continue;
|
|
2241
|
+
}
|
|
2242
|
+
load(resolved, null);
|
|
2243
|
+
}
|
|
2244
|
+
visiting.delete(path);
|
|
2245
|
+
order.push(path);
|
|
2246
|
+
return rec;
|
|
2247
|
+
}
|
|
2248
|
+
load(entryPath, entrySource);
|
|
2249
|
+
for (const rec of modules.values()) {
|
|
2250
|
+
if (rec.path === entryPath) continue;
|
|
2251
|
+
for (const name of rec.declaredPlain) rec.renamePlain.set(name, `__a${rec.id}_${name}`);
|
|
2252
|
+
for (const name of rec.declaredState) rec.renameState.set(name, `__a${rec.id}_${name}`);
|
|
2253
|
+
}
|
|
2254
|
+
for (const rec of modules.values()) {
|
|
2255
|
+
for (const { stmt, resolvedPath } of rec.edges) {
|
|
2256
|
+
if (resolvedPath === null) continue;
|
|
2257
|
+
const src = modules.get(resolvedPath);
|
|
2258
|
+
if (!src) continue;
|
|
2259
|
+
for (const spec of stmt.specifiers) {
|
|
2260
|
+
const line = stmt.loc?.line ?? 0;
|
|
2261
|
+
const column = stmt.loc?.column ?? 0;
|
|
2262
|
+
if (spec.isState) {
|
|
2263
|
+
if (!src.exportedState.has(spec.imported)) {
|
|
2264
|
+
fail(rec.path, line, column, `"${stmt.source}" does not export \`$${spec.imported}\`.`);
|
|
2265
|
+
continue;
|
|
2266
|
+
}
|
|
2267
|
+
rec.renameState.set(spec.local, src.renameState.get(spec.imported) ?? `__a${src.id}_${spec.imported}`);
|
|
2268
|
+
} else {
|
|
2269
|
+
if (!src.exportedPlain.has(spec.imported)) {
|
|
2270
|
+
fail(rec.path, line, column, `"${stmt.source}" does not export \`${spec.imported}\`.`);
|
|
2271
|
+
continue;
|
|
2272
|
+
}
|
|
2273
|
+
rec.renamePlain.set(spec.local, src.renamePlain.get(spec.imported) ?? `__a${src.id}_${spec.imported}`);
|
|
2274
|
+
}
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
const merged = [];
|
|
2279
|
+
for (const path of order) {
|
|
2280
|
+
const rec = modules.get(path);
|
|
2281
|
+
const renamer = makeRenamer(rec);
|
|
2282
|
+
for (const stmt of rec.program.statements) {
|
|
2283
|
+
if (stmt.kind === "Import") continue;
|
|
2284
|
+
renamer.renameTopLevel(stmt);
|
|
2285
|
+
stripExported(stmt);
|
|
2286
|
+
if (stmt.kind === "EffectDeclaration" && rec.path !== entryPath) {
|
|
2287
|
+
stmt.name = `__effect_a${rec.id}_${stmt.name.replace(/^__effect_/, "")}`;
|
|
2288
|
+
}
|
|
2289
|
+
merged.push(stmt);
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
const entryRec = modules.get(entryPath);
|
|
2293
|
+
return {
|
|
2294
|
+
program: { statements: merged, errors: entryRec ? entryRec.program.errors : [] },
|
|
2295
|
+
diagnostics,
|
|
2296
|
+
dependencies: order.filter((p) => p !== entryPath)
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
function buildSymbolTable(rec) {
|
|
2300
|
+
for (const stmt of rec.program.statements) {
|
|
2301
|
+
switch (stmt.kind) {
|
|
2302
|
+
case "Assignment":
|
|
2303
|
+
if (stmt.isState) {
|
|
2304
|
+
rec.declaredState.add(stmt.identifier);
|
|
2305
|
+
if (stmt.exported) rec.exportedState.add(stmt.identifier);
|
|
2306
|
+
} else {
|
|
2307
|
+
rec.declaredPlain.add(stmt.identifier);
|
|
2308
|
+
if (stmt.exported) rec.exportedPlain.add(stmt.identifier);
|
|
2309
|
+
}
|
|
2310
|
+
break;
|
|
2311
|
+
case "ComponentDeclaration":
|
|
2312
|
+
case "ActionDeclaration":
|
|
2313
|
+
rec.declaredPlain.add(stmt.name);
|
|
2314
|
+
if (stmt.exported) rec.exportedPlain.add(stmt.name);
|
|
2315
|
+
break;
|
|
2316
|
+
case "HookDeclaration":
|
|
2317
|
+
rec.declaredState.add(stmt.name);
|
|
2318
|
+
if (stmt.exported) rec.exportedState.add(stmt.name);
|
|
2319
|
+
break;
|
|
2320
|
+
case "DestructureStatement":
|
|
2321
|
+
for (const b of stmt.bindings) if (b.name) rec.declaredPlain.add(b.name);
|
|
2322
|
+
break;
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
function stripExported(stmt) {
|
|
2327
|
+
if (stmt.kind === "Assignment" || stmt.kind === "ComponentDeclaration" || stmt.kind === "ActionDeclaration" || stmt.kind === "HookDeclaration") {
|
|
2328
|
+
delete stmt.exported;
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
function makeRenamer(rec) {
|
|
2332
|
+
const shadow = [];
|
|
2333
|
+
const shadowed = (name) => {
|
|
2334
|
+
for (const set of shadow) if (set.has(name)) return true;
|
|
2335
|
+
return false;
|
|
2336
|
+
};
|
|
2337
|
+
const rPlain = (name) => shadowed(name) ? name : rec.renamePlain.get(name) ?? name;
|
|
2338
|
+
const rState = (name) => rec.renameState.get(name) ?? name;
|
|
2339
|
+
const push = (names) => {
|
|
2340
|
+
shadow.push(new Set(names));
|
|
2341
|
+
};
|
|
2342
|
+
const pop = () => {
|
|
2343
|
+
shadow.pop();
|
|
2344
|
+
};
|
|
2345
|
+
const patternNames = (p) => p.bindings.map((b) => b.name).filter((n) => !!n);
|
|
2346
|
+
const paramNames = (params) => {
|
|
2347
|
+
const out = [];
|
|
2348
|
+
for (const p of params) {
|
|
2349
|
+
if (p.name) out.push(p.name);
|
|
2350
|
+
if (p.pattern) out.push(...patternNames(p.pattern));
|
|
2351
|
+
}
|
|
2352
|
+
return out;
|
|
2353
|
+
};
|
|
2354
|
+
const renameParamDefaults = (params) => {
|
|
2355
|
+
for (const p of params) if (p.defaultValue) renameExpr(p.defaultValue);
|
|
2356
|
+
};
|
|
2357
|
+
function renameExpr(expr) {
|
|
2358
|
+
switch (expr.kind) {
|
|
2359
|
+
case "Literal":
|
|
2360
|
+
return;
|
|
2361
|
+
case "Identifier":
|
|
2362
|
+
expr.name = rPlain(expr.name);
|
|
2363
|
+
return;
|
|
2364
|
+
case "StateRef":
|
|
2365
|
+
expr.name = rState(expr.name);
|
|
2366
|
+
return;
|
|
2367
|
+
case "Array":
|
|
2368
|
+
for (const el of expr.elements) renameExpr(el);
|
|
2369
|
+
return;
|
|
2370
|
+
case "Object":
|
|
2371
|
+
for (const prop of expr.properties) {
|
|
2372
|
+
if (prop.computedKey) renameExpr(prop.computedKey);
|
|
2373
|
+
renameExpr(prop.value);
|
|
2374
|
+
}
|
|
2375
|
+
return;
|
|
2376
|
+
case "Member":
|
|
2377
|
+
renameExpr(expr.object);
|
|
2378
|
+
if (expr.computed) renameExpr(expr.computed);
|
|
2379
|
+
return;
|
|
2380
|
+
case "Unary":
|
|
2381
|
+
renameExpr(expr.argument);
|
|
2382
|
+
return;
|
|
2383
|
+
case "Binary":
|
|
2384
|
+
renameExpr(expr.left);
|
|
2385
|
+
renameExpr(expr.right);
|
|
2386
|
+
return;
|
|
2387
|
+
case "Ternary":
|
|
2388
|
+
renameExpr(expr.test);
|
|
2389
|
+
renameExpr(expr.consequent);
|
|
2390
|
+
renameExpr(expr.alternate);
|
|
2391
|
+
return;
|
|
2392
|
+
case "Call":
|
|
2393
|
+
expr.callee = rPlain(expr.callee);
|
|
2394
|
+
for (const a of expr.arguments) renameExpr(a);
|
|
2395
|
+
return;
|
|
2396
|
+
case "MethodCall":
|
|
2397
|
+
renameExpr(expr.object);
|
|
2398
|
+
for (const a of expr.arguments) renameExpr(a);
|
|
2399
|
+
return;
|
|
2400
|
+
case "Invoke":
|
|
2401
|
+
renameExpr(expr.callee);
|
|
2402
|
+
for (const a of expr.arguments) renameExpr(a);
|
|
2403
|
+
return;
|
|
2404
|
+
case "BuiltinCall":
|
|
2405
|
+
for (const a of expr.arguments) renameExpr(a);
|
|
2406
|
+
return;
|
|
2407
|
+
case "New":
|
|
2408
|
+
renameExpr(expr.callee);
|
|
2409
|
+
for (const a of expr.arguments) renameExpr(a);
|
|
2410
|
+
return;
|
|
2411
|
+
case "Template":
|
|
2412
|
+
for (const e of expr.expressions) renameExpr(e);
|
|
2413
|
+
return;
|
|
2414
|
+
case "Spread":
|
|
2415
|
+
renameExpr(expr.argument);
|
|
2416
|
+
return;
|
|
2417
|
+
case "Lambda":
|
|
2418
|
+
push(paramNames(expr.params));
|
|
2419
|
+
renameParamDefaults(expr.params);
|
|
2420
|
+
renameExpr(expr.body);
|
|
2421
|
+
pop();
|
|
2422
|
+
return;
|
|
2423
|
+
case "Block":
|
|
2424
|
+
renameBlock(expr);
|
|
2425
|
+
return;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
function renameBlock(block) {
|
|
2429
|
+
push([]);
|
|
2430
|
+
const scope = shadow[shadow.length - 1];
|
|
2431
|
+
for (const stmt of block.body) {
|
|
2432
|
+
renameStatement(stmt, false);
|
|
2433
|
+
addBlockLocals(stmt, scope);
|
|
2434
|
+
}
|
|
2435
|
+
pop();
|
|
2436
|
+
}
|
|
2437
|
+
function addBlockLocals(stmt, scope) {
|
|
2438
|
+
if (stmt.kind === "DestructureStatement") {
|
|
2439
|
+
for (const b of stmt.bindings) if (b.name) scope.add(b.name);
|
|
2440
|
+
} else if (stmt.kind === "ComponentDeclaration" || stmt.kind === "ActionDeclaration") {
|
|
2441
|
+
scope.add(stmt.name);
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
function renameTopLevel(stmt) {
|
|
2445
|
+
if (stmt.kind === "DestructureStatement") {
|
|
2446
|
+
renameExpr(stmt.expression);
|
|
2447
|
+
for (const b of stmt.bindings) {
|
|
2448
|
+
if (b.defaultValue) renameExpr(b.defaultValue);
|
|
2449
|
+
b.name = rPlain(b.name);
|
|
2450
|
+
}
|
|
2451
|
+
return;
|
|
2452
|
+
}
|
|
2453
|
+
renameStatement(stmt, true);
|
|
2454
|
+
}
|
|
2455
|
+
function renameStatement(stmt, topLevel) {
|
|
2456
|
+
switch (stmt.kind) {
|
|
2457
|
+
case "Import":
|
|
2458
|
+
return;
|
|
2459
|
+
case "Assignment":
|
|
2460
|
+
stmt.identifier = stmt.isState ? rState(stmt.identifier) : rPlain(stmt.identifier);
|
|
2461
|
+
renameExpr(stmt.expression);
|
|
2462
|
+
return;
|
|
2463
|
+
case "ComponentDeclaration":
|
|
2464
|
+
case "ActionDeclaration":
|
|
2465
|
+
stmt.name = rPlain(stmt.name);
|
|
2466
|
+
push(paramNames(stmt.params));
|
|
2467
|
+
renameParamDefaults(stmt.params);
|
|
2468
|
+
renameBlock(stmt.body);
|
|
2469
|
+
pop();
|
|
2470
|
+
return;
|
|
2471
|
+
case "HookDeclaration":
|
|
2472
|
+
stmt.name = rState(stmt.name);
|
|
2473
|
+
push(paramNames(stmt.params));
|
|
2474
|
+
renameParamDefaults(stmt.params);
|
|
2475
|
+
renameBlock(stmt.body);
|
|
2476
|
+
pop();
|
|
2477
|
+
return;
|
|
2478
|
+
case "EffectDeclaration":
|
|
2479
|
+
for (const t of stmt.triggers) if (t.kind === "state") t.name = rState(t.name);
|
|
2480
|
+
renameBlock(stmt.body);
|
|
2481
|
+
return;
|
|
2482
|
+
case "Await":
|
|
2483
|
+
renameExpr(stmt.argument);
|
|
2484
|
+
return;
|
|
2485
|
+
case "Return":
|
|
2486
|
+
if (stmt.argument) renameExpr(stmt.argument);
|
|
2487
|
+
return;
|
|
2488
|
+
case "ExpressionStatement":
|
|
2489
|
+
renameExpr(stmt.expression);
|
|
2490
|
+
return;
|
|
2491
|
+
case "IfStatement":
|
|
2492
|
+
renameExpr(stmt.test);
|
|
2493
|
+
renameBlock(stmt.consequent);
|
|
2494
|
+
if (stmt.alternate) {
|
|
2495
|
+
if (stmt.alternate.kind === "IfStatement") renameStatement(stmt.alternate, false);
|
|
2496
|
+
else renameBlock(stmt.alternate);
|
|
2497
|
+
}
|
|
2498
|
+
return;
|
|
2499
|
+
case "SwitchStatement":
|
|
2500
|
+
renameExpr(stmt.discriminant);
|
|
2501
|
+
for (const c of stmt.cases) {
|
|
2502
|
+
if (c.test) renameExpr(c.test);
|
|
2503
|
+
for (const s of c.body) renameStatement(s, false);
|
|
2504
|
+
}
|
|
2505
|
+
return;
|
|
2506
|
+
case "ForOfStatement": {
|
|
2507
|
+
renameExpr(stmt.iterable);
|
|
2508
|
+
const names = [stmt.item];
|
|
2509
|
+
if (stmt.index) names.push(stmt.index);
|
|
2510
|
+
if (stmt.destructure) names.push(...stmt.destructure);
|
|
2511
|
+
push(names);
|
|
2512
|
+
renameBlock(stmt.body);
|
|
2513
|
+
pop();
|
|
2514
|
+
return;
|
|
2515
|
+
}
|
|
2516
|
+
case "ForInStatement":
|
|
2517
|
+
renameExpr(stmt.iterable);
|
|
2518
|
+
push([stmt.item]);
|
|
2519
|
+
renameBlock(stmt.body);
|
|
2520
|
+
pop();
|
|
2521
|
+
return;
|
|
2522
|
+
case "ForClassicStatement": {
|
|
2523
|
+
const initNames = [];
|
|
2524
|
+
if (stmt.init && stmt.init.kind === "Assignment" && stmt.init.identifier) {
|
|
2525
|
+
initNames.push(stmt.init.identifier);
|
|
2526
|
+
}
|
|
2527
|
+
push(initNames);
|
|
2528
|
+
if (stmt.init) renameStatement(stmt.init, false);
|
|
2529
|
+
if (stmt.test) renameExpr(stmt.test);
|
|
2530
|
+
if (stmt.update) renameExpr(stmt.update);
|
|
2531
|
+
renameBlock(stmt.body);
|
|
2532
|
+
pop();
|
|
2533
|
+
return;
|
|
2534
|
+
}
|
|
2535
|
+
case "WhileStatement":
|
|
2536
|
+
case "DoWhileStatement":
|
|
2537
|
+
renameExpr(stmt.test);
|
|
2538
|
+
renameBlock(stmt.body);
|
|
2539
|
+
return;
|
|
2540
|
+
case "DestructureStatement":
|
|
2541
|
+
renameExpr(stmt.expression);
|
|
2542
|
+
for (const b of stmt.bindings) if (b.defaultValue) renameExpr(b.defaultValue);
|
|
2543
|
+
if (topLevel) for (const b of stmt.bindings) b.name = rPlain(b.name);
|
|
2544
|
+
return;
|
|
2545
|
+
case "ThrowStatement":
|
|
2546
|
+
renameExpr(stmt.argument);
|
|
2547
|
+
return;
|
|
2548
|
+
case "TryStatement":
|
|
2549
|
+
renameBlock(stmt.block);
|
|
2550
|
+
if (stmt.catchBlock) {
|
|
2551
|
+
push(stmt.catchParam ? [stmt.catchParam] : []);
|
|
2552
|
+
renameBlock(stmt.catchBlock);
|
|
2553
|
+
pop();
|
|
2554
|
+
}
|
|
2555
|
+
if (stmt.finallyBlock) renameBlock(stmt.finallyBlock);
|
|
2556
|
+
return;
|
|
2557
|
+
case "BreakStatement":
|
|
2558
|
+
case "ContinueStatement":
|
|
2559
|
+
return;
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
return { renameTopLevel };
|
|
2563
|
+
}
|
|
2564
|
+
function aktionPlugin(options = {}) {
|
|
2565
|
+
const runtimeModuleId = options.runtimeModuleId ?? "aktion-runtime";
|
|
2566
|
+
let isServe = false;
|
|
2567
|
+
return {
|
|
2568
|
+
name: "aktion",
|
|
2569
|
+
enforce: "pre",
|
|
2570
|
+
configResolved(config) {
|
|
2571
|
+
isServe = config.command === "serve";
|
|
2572
|
+
},
|
|
2573
|
+
transform(code, id) {
|
|
2574
|
+
if (!isAktionId(id)) return null;
|
|
2575
|
+
const cleanId = stripQuery(id);
|
|
2576
|
+
const { program, diagnostics, dependencies } = linkProgram(code, cleanId, nodeResolver());
|
|
2577
|
+
for (const dep of dependencies) this.addWatchFile(dep);
|
|
2578
|
+
const warnings = [...diagnostics];
|
|
2579
|
+
if (!hasEntryBinding(program)) {
|
|
2580
|
+
warnings.push({
|
|
2581
|
+
line: 1,
|
|
2582
|
+
column: 1,
|
|
2583
|
+
severity: "warning",
|
|
2584
|
+
message: "No top-level `aktion = …` entry binding found — this program renders nothing."
|
|
2585
|
+
});
|
|
2586
|
+
}
|
|
2587
|
+
const fatal = warnings.find((d) => d.severity === "error" || options.strict && d.severity === "warning");
|
|
2588
|
+
if (fatal) {
|
|
2589
|
+
return this.error({ message: fatal.message, id: cleanId, loc: { file: cleanId, line: fatal.line, column: fatal.column } });
|
|
2590
|
+
}
|
|
2591
|
+
for (const w of warnings) {
|
|
2592
|
+
if (w.severity === "warning") this.warn(w.message);
|
|
2593
|
+
}
|
|
2594
|
+
const moduleCode = emitModule(program, code, cleanId, runtimeModuleId) + (isServe ? HMR_FOOTER : "");
|
|
2595
|
+
return { code: moduleCode, map: { mappings: "" } };
|
|
2596
|
+
}
|
|
2597
|
+
};
|
|
2598
|
+
}
|
|
2599
|
+
function isAktionId(id) {
|
|
2600
|
+
return stripQuery(id).endsWith(".aktion");
|
|
2601
|
+
}
|
|
2602
|
+
function stripQuery(id) {
|
|
2603
|
+
const q = id.indexOf("?");
|
|
2604
|
+
const base = q === -1 ? id : id.slice(0, q);
|
|
2605
|
+
const h = base.indexOf("#");
|
|
2606
|
+
return h === -1 ? base : base.slice(0, h);
|
|
2607
|
+
}
|
|
2608
|
+
function nodeResolver() {
|
|
2609
|
+
return {
|
|
2610
|
+
resolve(spec, importerPath) {
|
|
2611
|
+
if (!spec.startsWith(".") && !spec.startsWith("/")) return null;
|
|
2612
|
+
try {
|
|
2613
|
+
return node_path.resolve(node_path.dirname(importerPath), spec);
|
|
2614
|
+
} catch {
|
|
2615
|
+
return null;
|
|
2616
|
+
}
|
|
2617
|
+
},
|
|
2618
|
+
load(path) {
|
|
2619
|
+
return node_fs.readFileSync(path, "utf8");
|
|
2620
|
+
}
|
|
2621
|
+
};
|
|
2622
|
+
}
|
|
2623
|
+
function hasEntryBinding(program) {
|
|
2624
|
+
return program.statements.some((s) => s.kind === "Assignment" && s.identifier === "aktion");
|
|
2625
|
+
}
|
|
2626
|
+
function emitModule(program, source, path, runtimeModuleId) {
|
|
2627
|
+
const programLiteral = JSON.stringify(JSON.stringify(program));
|
|
2628
|
+
return `// Generated by the Aktion Vite plugin — do not edit by hand.
|
|
2629
|
+
import { defineCompiledProgram } from ${JSON.stringify(runtimeModuleId)};
|
|
2630
|
+
const program = /*#__PURE__*/ JSON.parse(${programLiteral});
|
|
2631
|
+
const source = ${JSON.stringify(source)};
|
|
2632
|
+
export default /*#__PURE__*/ defineCompiledProgram({ __aktionCompiled: ${COMPILED_PROGRAM_VERSION}, program, source, path: ${JSON.stringify(path)} });
|
|
2633
|
+
`;
|
|
2634
|
+
}
|
|
2635
|
+
const HMR_FOOTER = `
|
|
2636
|
+
if (import.meta.hot) {
|
|
2637
|
+
import.meta.hot.accept((mod) => {
|
|
2638
|
+
const next = mod && mod.default;
|
|
2639
|
+
if (!next || typeof document === "undefined") return;
|
|
2640
|
+
for (const el of document.querySelectorAll("aktion-app")) {
|
|
2641
|
+
if (el.sourceId !== next.path) continue;
|
|
2642
|
+
el.mountCompiled(next, el.serializeState());
|
|
2643
|
+
}
|
|
2644
|
+
});
|
|
2645
|
+
}
|
|
2646
|
+
`;
|
|
2647
|
+
exports.aktionPlugin = aktionPlugin;
|
|
2648
|
+
exports.default = aktionPlugin;
|
|
2649
|
+
exports.isAktionId = isAktionId;
|
|
2650
|
+
//# sourceMappingURL=plugin.cjs.map
|