Repository navigation
Expand file tree
/
Copy pathparser.js
More file actions
661 lines (644 loc) · 34.1 KB
/
Copy pathparser.js
File metadata and controls
661 lines (644 loc) · 34.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZyoraLabs. Runtime Library Exception applies — see LICENSE.
import { Scanner, readExpressionStatement, RESERVED_WORDS } from "./scanner.js";
import { parseExpression, ExpressionParser, patternBindings } from "./expressions.js";
import { readType, readTypeParameters, typeReadCount } from "./types.js";
export { NiralScriptError } from "./errors.js";
export { analyzeNiralScript } from "./analysis.js";
const IDENTIFIER = /[A-Za-z_$][\w$]*/y;
export function parseNiralScript(code, filename = "<anonymous>") {
const parser = new StatementParser(code, filename);
const reads = typeReadCount();
const body = parser.body(true, 0);
const typed = typeReadCount() > reads;
return { type: "Program", body, source: code, filename, typed, start: 0, end: code.length };
}
export class StatementParser {
constructor(code, filename) {
this.scanner = new Scanner(code, filename);
this.loopDepth = 0;
this.labels = [];
}
trivia() {
const scanner = this.scanner;
while (!scanner.eof()) {
scanner.skipWs();
if (scanner.eat("//")) {
while (!scanner.eof() && !/[\r\n]/.test(scanner.peek())) scanner.pos++;
} else if (scanner.eat("/*")) {
scanner.readUntil("*/");
scanner.expect("*/", "NRS070", "Unclosed NiralScript comment");
} else break;
}
}
name(names, declarationStart) {
this.trivia();
const scanner = this.scanner;
const start = scanner.pos;
const name = scanner.read(IDENTIFIER);
if (!name || RESERVED_WORDS.has(name) || name.startsWith("__") || name.startsWith("$")) {
scanner.error("NRS070", "Expected a non-reserved NiralScript identifier", { start: declarationStart });
}
if (names.has(name)) scanner.error("NRS070", `Duplicate declaration '${name}'`, { start: declarationStart });
names.add(name);
return { type: "Identifier", name, start, end: scanner.pos };
}
body(topLevel, depth, names = new Set()) {
const body = [];
this.trivia();
while (!this.scanner.eof() && (topLevel || this.scanner.peek() !== "}")) {
body.push(this.statement(topLevel, depth, names));
this.trivia();
}
return body;
}
pattern(names) {
this.trivia();
const scanner = this.scanner;
if (!"[{".includes(scanner.peek())) return this.name(names, scanner.pos);
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const pattern = parser.binding(names, false);
scanner.pos = pattern.end;
return pattern;
}
annotation() {
const parser = new ExpressionParser(this.scanner.src, this.scanner.filename, this.scanner.pos, this.scanner.src.length);
const annotation = parser.annotation();
if (annotation) this.scanner.pos = annotation.end;
return annotation;
}
block(depth, names = new Set()) {
this.trivia();
const scanner = this.scanner;
const start = scanner.pos;
scanner.expect("{", "NRS072", "Expected a braced statement block");
const body = this.body(false, depth, names);
scanner.expect("}", "NRS072", "Expected '}' after statement block");
return { type: "BlockStatement", body, start, end: scanner.pos };
}
// Shared tail of `function f(...)` declarations and `function (...)` expressions.
functionRest(start, depth, id, async, type) {
const scanner = this.scanner;
this.trivia();
const genericParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const typeParameters = readTypeParameters(genericParser);
if (typeParameters) { scanner.pos = typeParameters.end; this.trivia(); }
scanner.expect("(", "NRS072", "Expected function parameters");
const parameterParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
let params;
try { params = parameterParser.parameters().items; }
catch (error) {
if (error.message === "Expected unique non-reserved binding names") error.code = "NRS070";
throw error;
}
scanner.pos = parameterParser.scanner.pos;
const returnType = this.annotation();
const parameterNames = new Set();
function collectNames(pattern) {
if (!pattern) return;
if (pattern.type === "Identifier") parameterNames.add(pattern.name);
else if (pattern.type === "AssignmentPattern") collectNames(pattern.left);
else if (pattern.type === "RestElement") collectNames(pattern.argument);
else for (const item of pattern.elements ?? pattern.properties) collectNames(item?.value ?? item);
}
params.forEach(collectNames);
const outerLoopDepth = this.loopDepth;
const outerLabels = this.labels;
this.loopDepth = 0;
this.labels = [];
const body = this.block(depth + 1, parameterNames);
this.loopDepth = outerLoopDepth;
this.labels = outerLabels;
return { type, ...(async ? { async: true } : {}), ...(returnType ? { returnType } : {}), ...(typeParameters ? { typeParameters } : {}), id, params, body, start, end: body.end };
}
// Named expressions keep their own name in scope so they can recurse.
functionExpression(start, async) {
const scanner = this.scanner;
this.trivia();
const nameStart = scanner.pos;
const id = scanner.peek() === "(" || scanner.peek() === "<" ? null : this.name(new Set(), nameStart);
return this.functionRest(start, 0, id, async, "FunctionExpression");
}
classDeclaration(start, depth, names, optional = false) {
const scanner = this.scanner;
let id = null;
if (!optional) {
id = this.name(names, start);
} else {
const savedPos = scanner.pos;
this.trivia();
const IDENTIFIER = /[A-Za-z_$][\w$]*/y;
IDENTIFIER.lastIndex = scanner.pos;
const word = IDENTIFIER.exec(scanner.src)?.[0];
scanner.pos = savedPos;
if (word && word !== "extends" && !RESERVED_WORDS.has(word)) id = this.name(names, start);
}
let parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const typeParameters = readTypeParameters(parser);
let superClass = null;
let superTypeArguments = null;
if (parser.peek().value === "extends") {
parser.take();
superClass = parser.primary();
if (parser.peek().kind === "<") {
const opening = parser.take();
const values = [];
do { values.push(readType(parser).value); } while (parser.eat(","));
const close = parser.expectClose();
superTypeArguments = { type: "TypeAnnotation", value: { kind: "arguments", values }, start: opening.start, end: close.end };
}
}
parser.expect("{");
const members = [];
const keys = new Map();
while (parser.peek().kind !== "}") {
if (parser.eat(";")) continue;
const memberStart = parser.peek().start;
const decorators = parser.decorators(!optional);
let key = parser.take();
let isStatic = false;
let async = false;
let kind = "method";
if (key.value === "static" && !["(", ":", "=", ";", "}"].includes(parser.peek().kind)) {
isStatic = true;
if (parser.peek().kind === "{") {
if (decorators.length) parser.error("Static initialization blocks cannot be decorated", memberStart);
scanner.pos = parser.peek().start;
const outerLoopDepth = this.loopDepth;
const outerLabels = this.labels;
this.loopDepth = 0;
this.labels = [];
const body = this.block(depth + 1);
this.loopDepth = outerLoopDepth;
this.labels = outerLabels;
members.push({ type: "StaticBlock", static: true, body, start: memberStart, end: body.end });
parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
continue;
}
key = parser.take();
}
if (key.value === "async" && ["identifier", "#", "["].includes(parser.peek().kind)) { async = true; key = parser.take(); }
if (["get", "set"].includes(key.value) && ["identifier", "#", "["].includes(parser.peek().kind)) { kind = key.value; key = parser.take(); }
if (async && kind !== "method") parser.error("Accessors cannot be async", key.start);
if (key.kind === "#") { const privateName = parser.expect("identifier"); key = { ...privateName, value: "#" + privateName.value, start: key.start }; }
if (key.kind === "[") {
key = parser.take();
if (!["string", "number"].includes(key.kind)) parser.error("Computed class names require a string or number literal", key.start);
parser.expect("]");
}
if (!["identifier", "string", "number"].includes(key.kind)) parser.error("Expected a class member name", key.start);
const name = String(key.value);
const memberKey = { type: "Identifier", name, start: key.start, end: key.end };
const identity = `${isStatic}:${name}`;
const previous = keys.get(identity);
if (decorators.length && name.startsWith("#")) parser.error("Private class members cannot be decorated", memberStart);
if (name === "#constructor") parser.error("Private constructors are not allowed", key.start);
if (name.startsWith("#") && keys.has(`${!isStatic}:${name}`)) parser.error("Private names cannot be shared by static and instance members", key.start);
if (previous && !(previous === "get" && kind === "set" || previous === "set" && kind === "get")) parser.error(`Duplicate class member '${name}'`, key.start);
if (isStatic && name === "prototype") parser.error("Static prototype members are not allowed", key.start);
const methodTypes = readTypeParameters(parser);
if (parser.eat("(")) {
if (name === "constructor" && !isStatic) {
if (async || kind !== "method" || methodTypes) parser.error("A constructor cannot be async, generic or an accessor", key.start);
if (decorators.length) parser.error("Class constructors cannot be decorated", memberStart);
kind = "constructor";
}
const params = parser.parameters().items;
if (kind === "get" && params.length || kind === "set" && (params.length !== 1 || params[0].type === "RestElement")) parser.error("Invalid accessor parameters", key.start);
const returnType = parser.annotation();
if (kind === "constructor" && returnType) parser.error("Constructors cannot declare a return type", returnType.start);
scanner.pos = parser.peek().start;
const outerLoopDepth = this.loopDepth;
const outerLabels = this.labels;
this.loopDepth = 0;
this.labels = [];
const body = this.block(depth + 1, new Set(params.flatMap(patternBindings).map(binding => binding.id.name)));
this.loopDepth = outerLoopDepth;
this.labels = outerLabels;
members.push({ type: "MethodDefinition", key: memberKey, static: isStatic, kind, async, params, returnType, typeParameters: methodTypes, ...(decorators.length ? { decorators } : {}), body, start: memberStart, end: body.end });
parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
} else {
if (async || kind !== "method" || methodTypes || name === "constructor") parser.error("Invalid class field", key.start);
if (decorators.length && !isStatic) parser.error("Only static fields can be decorated", memberStart);
const typeAnnotation = parser.annotation();
const value = parser.eat("=") ? parser.expression() : null;
const end = value?.end ?? typeAnnotation?.end ?? key.end;
if (!parser.eat(";") && parser.peek().kind !== "}" && !/[\r\n]/.test(scanner.src.slice(end, parser.peek().start))) parser.error("Expected a semicolon or newline after field", end);
members.push({ type: "PropertyDefinition", key: memberKey, static: isStatic, typeAnnotation, ...(decorators.length ? { decorators } : {}), value, start: memberStart, end });
kind = "field";
}
keys.set(identity, previous ? "accessors" : kind);
}
const end = parser.expect("}").end;
scanner.pos = end;
return { type: "ClassDeclaration", id, typeParameters, superClass, superTypeArguments, members, start, end };
}
statement(topLevel, depth, names) {
const scanner = this.scanner;
const start = scanner.pos;
if (scanner.peek() === "@") {
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const decorators = parser.decorators(true);
scanner.pos = decorators[decorators.length - 1].end;
this.trivia();
const declaration = this.statement(topLevel, depth, names);
if (declaration.type !== "ClassDeclaration") scanner.error("NRS071", "Decorators require a class declaration", { start });
return { ...declaration, decorators, start };
}
let kind = scanner.read(IDENTIFIER);
if (kind && !RESERVED_WORDS.has(kind)) {
const afterLabel = scanner.pos;
this.trivia();
if (scanner.peek() === ":" && scanner.src[scanner.pos + 1] !== ":") {
scanner.pos++;
this.trivia();
const labelStart = scanner.pos;
const target = scanner.read(IDENTIFIER);
scanner.pos = labelStart;
if (!["for", "while", "do"].includes(target)) scanner.error("NRS072", "Labels may only prefix a loop", { start });
if (this.labels.includes(kind)) scanner.error("NRS072", `Duplicate label '${kind}'`, { start });
this.labels.push(kind);
const body = this.statement(false, depth, names);
this.labels.pop();
return { type: "LabeledStatement", label: { type: "Identifier", name: kind, start, end: afterLabel }, body, start, end: body.end };
}
scanner.pos = afterLabel;
}
let async = false;
if (kind === "async") {
const afterAsync = scanner.pos;
this.trivia();
if (scanner.read(IDENTIFIER) === "function") {
if (/[\r\n]/.test(scanner.src.slice(afterAsync, scanner.pos))) scanner.error("NRS072", "Async function declarations require the same line", { start });
kind = "function";
async = true;
} else scanner.pos = afterAsync;
}
if (kind === "import") {
if (!topLevel) scanner.error("NRS078", "Imports are only valid at top level", { start });
this.trivia();
const importStart = scanner.pos;
const typeOnly = scanner.read(IDENTIFIER) === "type";
if (!typeOnly) scanner.pos = importStart;
else { names = new Set(); this.trivia(); }
scanner.expect("{", "NRS078", "Expected named imports in braces");
const specifiers = [];
this.trivia();
while (scanner.peek() !== "}") {
const imported = this.name(new Set(), scanner.pos);
let local = imported;
this.trivia();
const afterName = scanner.pos;
if (scanner.read(IDENTIFIER) === "as") local = this.name(names, scanner.pos);
else {
scanner.pos = afterName;
if (names.has(local.name)) scanner.error("NRS070", `Duplicate declaration '${local.name}'`, { start: local.start });
names.add(local.name);
}
specifiers.push({ type: "ImportSpecifier", imported, local, start: imported.start, end: local.end });
this.trivia();
if (!scanner.eat(",")) break;
this.trivia();
}
scanner.expect("}", "NRS078", "Expected '}' after named imports");
this.trivia();
if (scanner.read(IDENTIFIER) !== "from") scanner.error("NRS078", "Expected 'from' after named imports", { start });
this.trivia();
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const token = parser.take();
if (token.kind !== "string") scanner.error("NRS078", "Expected a quoted module specifier", { start });
scanner.pos = token.end;
this.trivia();
const terminated = scanner.eat(";");
if (!terminated && !scanner.eof() && !/[\r\n]/.test(scanner.src.slice(token.end, scanner.pos))) scanner.error("NRS078", "Expected a semicolon or newline after import", { start: token.end });
return { type: "ImportDeclaration", ...(typeOnly ? { typeOnly } : {}), specifiers, source: { type: "Literal", value: token.value, start: token.start, end: token.end }, start, end: terminated ? scanner.pos : token.end };
}
if (kind === "export") {
if (!topLevel) scanner.error("NRS078", "Exports are only valid at top level", { start });
this.trivia();
if (scanner.peek() === "{") {
scanner.expect("{", "NRS078", "Expected named re-exports in braces");
const specifiers = [];
this.trivia();
while (scanner.peek() !== "}") {
const importedStart = scanner.pos;
const importedName = scanner.read(IDENTIFIER);
if (!importedName || RESERVED_WORDS.has(importedName)) scanner.error("NRS070", "Expected a non-reserved NiralScript identifier", { start: importedStart });
const imported = { type: "Identifier", name: importedName, start: importedStart, end: scanner.pos };
let local = imported;
this.trivia();
const afterName = scanner.pos;
if (scanner.read(IDENTIFIER) === "as") local = this.name(names, scanner.pos);
else {
scanner.pos = afterName;
if (names.has(imported.name)) scanner.error("NRS070", `Duplicate declaration '${imported.name}'`, { start: imported.start });
names.add(imported.name);
}
specifiers.push({ type: "ExportSpecifier", imported, local, start: imported.start, end: local.end });
this.trivia();
if (!scanner.eat(",")) break;
this.trivia();
}
scanner.expect("}", "NRS078", "Expected '}' after named re-exports");
this.trivia();
if (scanner.read(IDENTIFIER) !== "from") scanner.error("NRS078", "Re-exports must name a module with 'from'", { start });
this.trivia();
const moduleParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const token = moduleParser.take();
if (token.kind !== "string") scanner.error("NRS078", "Expected a quoted module specifier", { start });
scanner.pos = token.end;
this.trivia();
const terminated = scanner.eat(";");
if (!terminated && !scanner.eof() && !/[\r\n]/.test(scanner.src.slice(token.end, scanner.pos))) scanner.error("NRS078", "Expected a semicolon or newline after re-export", { start: token.end });
return { type: "ExportNamedDeclaration", specifiers, source: { type: "Literal", value: token.value, start: token.start, end: token.end }, exported: true, start, end: terminated ? scanner.pos : token.end };
}
const declarationStart = scanner.pos;
const declarationKind = scanner.read(IDENTIFIER);
if (!["const", "function", "async", "type", "interface", "class"].includes(declarationKind)) scanner.error("NRS078", "Only constants, functions, classes and types can be exported", { start });
scanner.pos = declarationStart;
const declaration = this.statement(topLevel, depth, names);
if (!["VariableDeclaration", "FunctionDeclaration", "TypeAliasDeclaration", "InterfaceDeclaration", "ClassDeclaration"].includes(declaration.type)) scanner.error("NRS078", "Only constants, functions, classes and types can be exported", { start });
return { ...declaration, exported: true, start };
}
if (kind === "class") return this.classDeclaration(start, depth, names);
if (kind === "type") {
if (!topLevel) scanner.error("NRS080", "Type aliases are declared at module top level", { start });
const id = this.name(new Set(), start);
this.trivia();
const genericParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const typeParameters = readTypeParameters(genericParser);
if (typeParameters) { scanner.pos = typeParameters.end; this.trivia(); }
scanner.expect("=", "NRS080", "Expected '=' after type name");
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const annotationStart = scanner.pos;
const { value, end } = readType(parser);
scanner.pos = end;
scanner.eat(";");
return { type: "TypeAliasDeclaration", id, ...(typeParameters ? { typeParameters } : {}), value: { type: "TypeAnnotation", value, start: annotationStart, end }, start, end: scanner.pos };
}
if (kind === "interface") {
if (!topLevel) scanner.error("NRS080", "Interfaces are declared at module top level", { start });
const id = this.name(new Set(), start);
this.trivia();
const genericParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const typeParameters = readTypeParameters(genericParser);
if (typeParameters) { scanner.pos = typeParameters.end; this.trivia(); }
const extendsTypes = [];
const afterId = scanner.pos;
if (scanner.read(IDENTIFIER) === "extends") {
do {
this.trivia();
const extParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const { value: extValue, end: extEnd } = readType(extParser);
extendsTypes.push({ type: "TypeAnnotation", value: extValue, start: scanner.pos, end: extEnd });
scanner.pos = extEnd;
this.trivia();
} while (scanner.eat(","));
} else scanner.pos = afterId;
this.trivia();
scanner.expect("{", "NRS080", "Expected '{' for interface body");
const bodyParser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const properties = {};
while (bodyParser.peek().kind !== "}") {
const key = bodyParser.expect("identifier");
if (Object.hasOwn(properties, key.value)) bodyParser.error(`Duplicate interface property '${key.value}'`, key.start);
const optional = !!bodyParser.eat("?");
bodyParser.expect(":");
const { value: propValue } = readType(bodyParser);
Object.defineProperty(properties, key.value, { value: optional ? { kind: "union", members: [propValue, { kind: "undefined" }] } : propValue, enumerable: true });
if (!bodyParser.eat(",") && !bodyParser.eat(";")) break;
}
const closeEnd = bodyParser.expect("}").end;
scanner.pos = closeEnd;
const bodyAnnotation = { type: "TypeAnnotation", value: { kind: "object", properties }, start: start, end: closeEnd };
return { type: "InterfaceDeclaration", id, ...(typeParameters ? { typeParameters } : {}), ...(extendsTypes.length ? { extends: extendsTypes } : {}), value: bodyAnnotation, start, end: scanner.pos };
}
if (kind === "input") {
if (!topLevel) scanner.error("NRS072", "Inputs are only valid at component top level", { start });
const id = this.name(names, start);
const typeAnnotation = this.annotation();
if (typeAnnotation) id.typeAnnotation = typeAnnotation;
this.trivia();
const terminated = scanner.eat(";");
const bindingEnd = typeAnnotation?.end ?? id.end;
if (!terminated && !scanner.eof() && !/[\r\n]/.test(scanner.src.slice(id.end, scanner.pos))) scanner.error("NRS072", "Expected a semicolon or newline after input", { start: bindingEnd });
return { type: "InputDeclaration", id, start, end: terminated ? scanner.pos : bindingEnd };
}
if (["state", "derive", "const", "let"].includes(kind)) {
if (!topLevel && (kind === "state" || kind === "derive")) {
scanner.error("NRS072", "Reactive declarations must be at component top level", { start });
}
const id = kind === "const" || kind === "let" ? this.pattern(names) : this.name(names, start);
const typeAnnotation = this.annotation();
if (typeAnnotation) id.typeAnnotation = typeAnnotation;
this.trivia();
scanner.expect("=", "NRS070", `Expected '=' after binding`);
if (scanner.peek() === "=" || scanner.peek() === ">") scanner.error("NRS070", `Expected an initializer after '=' for '${id.name}'`);
const init = this.initializer(id.name, start);
return {
type: kind === "state" ? "StateDeclaration" : kind === "derive" ? "DerivedDeclaration" : "VariableDeclaration",
...(kind === "let" || kind === "const" ? { kind } : {}),
id, init, start, end: scanner.pos,
};
}
if (kind === "function") {
const id = this.name(names, start);
return this.functionRest(start, depth, id, async, "FunctionDeclaration");
}
if (kind === "for") {
this.trivia();
scanner.expect("(", "NRS072", "Expected '(' after for");
this.trivia();
const headerStart = scanner.pos;
const declarationKind = scanner.read(IDENTIFIER);
let init = null;
let parser;
if (["const", "let"].includes(declarationKind)) {
const id = this.pattern(new Set());
const typeAnnotation = this.annotation();
if (typeAnnotation) id.typeAnnotation = typeAnnotation;
this.trivia();
parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
if (parser.peek().value === "of" || parser.peek().value === "in") {
const iteration = parser.take().value;
if (iteration === "in" && id.type !== "Identifier") scanner.error("NRS072", "For-in keys require an identifier binding", { start: id.start });
if (iteration === "in" && typeAnnotation) scanner.error("NRS072", "For-in keys are always strings", { start: typeAnnotation.start });
const right = parser.expression();
parser.expect(")");
scanner.pos = parser.scanner.pos;
this.loopDepth++;
const body = this.block(depth);
this.loopDepth--;
return { type: iteration === "in" ? "ForInStatement" : "ForOfStatement", kind: declarationKind, id, right, body, start, end: body.end };
}
parser.expect("=");
const value = parser.expression();
init = { type: "VariableDeclaration", kind: declarationKind, id, init: value, start: headerStart, end: value.end };
} else {
parser = new ExpressionParser(scanner.src, scanner.filename, headerStart, scanner.src.length);
if (parser.peek().kind !== ";") init = this.simpleStatement(parser);
}
parser.expect(";");
const test = parser.peek().kind === ";" ? null : parser.expression();
parser.expect(";");
const update = parser.peek().kind === ")" ? null : this.simpleStatement(parser);
parser.expect(")");
scanner.pos = parser.scanner.pos;
this.loopDepth++;
const body = this.block(depth);
this.loopDepth--;
return { type: "ForStatement", init, test, update, body, start, end: body.end };
}
if (kind === "while") {
this.trivia();
scanner.expect("(", "NRS072", "Expected '(' after while");
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const test = parser.expression();
parser.expect(")");
scanner.pos = parser.scanner.pos;
this.loopDepth++;
const body = this.block(depth);
this.loopDepth--;
return { type: "WhileStatement", test, body, start, end: body.end };
}
if (kind === "do") {
this.trivia();
this.loopDepth++;
const body = this.block(depth);
this.loopDepth--;
this.trivia();
if (scanner.read(IDENTIFIER) !== "while") scanner.error("NRS072", "Expected 'while' after do block", { start });
this.trivia();
scanner.expect("(", "NRS072", "Expected '(' after while");
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const test = parser.expression();
parser.expect(")");
scanner.pos = parser.scanner.pos;
this.trivia();
scanner.eat(";");
return { type: "DoWhileStatement", test, body, start, end: scanner.pos };
}
if (kind === "break" || kind === "continue") {
if (!this.loopDepth) scanner.error("NRS072", `${kind} is only valid inside a loop`, { start });
const keywordEnd = scanner.pos;
this.trivia();
let label = null;
const sameLine = !/[\r\n]/.test(scanner.src.slice(keywordEnd, scanner.pos));
const labelStart = scanner.pos;
const candidate = sameLine ? scanner.read(IDENTIFIER) : "";
if (candidate && !RESERVED_WORDS.has(candidate)) {
if (!this.labels.includes(candidate)) scanner.error("NRS072", `Undefined label '${candidate}'`, { start: labelStart });
label = { type: "Identifier", name: candidate, start: labelStart, end: scanner.pos };
this.trivia();
} else scanner.pos = labelStart;
if (!scanner.eat(";") && scanner.peek() !== "}" && !scanner.eof() && !/[\r\n]/.test(scanner.src.slice(label?.end ?? keywordEnd, scanner.pos))) scanner.error("NRS072", "Expected a semicolon or newline after " + kind, { start });
return { type: kind === "break" ? "BreakStatement" : "ContinueStatement", ...(label ? { label } : {}), start, end: scanner.pos };
}
if (kind === "if") {
this.trivia();
scanner.expect("(", "NRS072", "Expected '(' after if");
const parser = new ExpressionParser(scanner.src, scanner.filename, scanner.pos, scanner.src.length);
const test = parser.expression();
parser.expect(")");
scanner.pos = parser.scanner.pos;
const consequent = this.block(depth);
this.trivia();
let alternate = null;
const afterConsequent = scanner.pos;
if (scanner.read(IDENTIFIER) === "else") {
this.trivia();
const branchStart = scanner.pos;
const branch = scanner.read(IDENTIFIER);
scanner.pos = branchStart;
alternate = branch === "if" ? this.statement(false, depth, new Set()) : this.block(depth);
} else scanner.pos = afterConsequent;
return { type: "IfStatement", test, consequent, alternate, start, end: alternate?.end ?? consequent.end };
}
if (kind === "try") {
const block = this.block(depth);
let handler = null;
let finalizer = null;
this.trivia();
let next = scanner.pos;
if (scanner.read(IDENTIFIER) === "catch") {
this.trivia();
const parameters = new Set();
let param = null;
if (scanner.eat("(")) {
param = this.pattern(parameters);
this.trivia();
scanner.expect(")", "NRS072", "Expected ')' after catch binding");
}
const body = this.block(depth, parameters);
handler = { type: "CatchClause", param, body, start: next, end: body.end };
} else scanner.pos = next;
this.trivia();
next = scanner.pos;
if (scanner.read(IDENTIFIER) === "finally") finalizer = this.block(depth);
else scanner.pos = next;
if (!handler && !finalizer) scanner.error("NRS072", "Try requires catch or finally", { start });
return { type: "TryStatement", block, handler, finalizer, start, end: finalizer?.end ?? handler.end };
}
if (kind === "throw") {
const argumentStart = scanner.pos;
const raw = readExpressionStatement(scanner, true);
if (this.empty(raw)) scanner.error("NRS072", "Throw requires an expression on the same line", { start });
const argument = parseExpression(scanner.src, scanner.filename, argumentStart, scanner.pos);
scanner.eat(";");
return { type: "ThrowStatement", argument, start, end: scanner.pos };
}
if (kind === "return") {
if (!depth) scanner.error("NRS072", "Return is only valid inside a function", { start });
const argumentStart = scanner.pos;
const raw = readExpressionStatement(scanner, true);
const argument = this.empty(raw) ? null : parseExpression(scanner.src, scanner.filename, argumentStart, scanner.pos);
scanner.eat(";");
return { type: "ReturnStatement", argument, start, end: scanner.pos };
}
scanner.pos = start;
if (scanner.peek() === "{") return this.block(depth);
readExpressionStatement(scanner, true);
const parser = new ExpressionParser(scanner.src, scanner.filename, start, scanner.pos);
const statement = this.simpleStatement(parser);
parser.expect("eof");
scanner.eat(";");
return { ...statement, start, end: scanner.pos };
}
simpleStatement(parser) {
const scanner = this.scanner;
const start = parser.peek().start;
const destructured = parser.destructuringAssignment();
if (destructured) return destructured;
const prefix = ["++", "--"].includes(parser.peek().kind) ? parser.take() : null;
const expression = parser.expression();
const operator = prefix ?? parser.peek();
let statement;
if (["=", "+=", "-=", "*=", "/=", "%=", "**=", "<<=", ">>=", ">>>=", "&=", "|=", "^=", "++", "--"].includes(operator.kind)) {
if (!prefix) parser.take();
if (!["Identifier", "MemberExpression"].includes(expression.type)) scanner.error("NRS072", "Invalid assignment target", { start });
for (let target = expression; target && ["MemberExpression", "CallExpression"].includes(target.type); target = target.object ?? target.callee) if (target.optional) scanner.error("NRS072", "Optional chains cannot be assignment targets", { start });
const update = ["++", "--"].includes(operator.kind);
const right = update ? { type: "Literal", value: 1, start: operator.start, end: operator.end } : parser.expression();
statement = { type: "AssignmentStatement", left: expression, operator: update ? operator.kind === "++" ? "+=" : "-=" : operator.kind, ...(update ? { update: true } : {}), right };
} else {
if (!["CallExpression", "NewExpression", "AwaitExpression"].includes(expression.type)) scanner.error("NRS072", "Only calls, construction, await and assignments are expression statements", { start });
statement = { type: "ExpressionStatement", expression };
}
return { ...statement, start, end: Math.max(expression.end, statement.right?.end ?? expression.end) };
}
empty(raw) {
return /^(?:\s|\/\*[\s\S]*?\*\/|\/\/[^\n]*)*$/.test(raw);
}
initializer(name, start) {
const scanner = this.scanner;
const expressionStart = scanner.pos;
const expression = readExpressionStatement(scanner, true);
if (this.empty(expression)) scanner.error("NRS070", `Expected an initializer for '${name}'`, { start });
const expressionEnd = scanner.pos;
scanner.eat(";");
return {
...parseExpression(scanner.src, scanner.filename, expressionStart, expressionEnd),
raw: expression, start: expressionStart, end: expressionEnd,
};
}
}