midnight-shield 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/LICENSE +21 -0
- package/README.md +384 -2
- package/action.yml +56 -0
- package/dist/src/analyzer.d.ts +97 -0
- package/dist/src/analyzer.js +895 -0
- package/dist/src/ast.d.ts +211 -0
- package/dist/src/ast.js +2 -0
- package/dist/src/browser.d.ts +3 -0
- package/dist/src/browser.js +5 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +336 -0
- package/dist/src/config.d.ts +26 -0
- package/dist/src/config.js +87 -0
- package/dist/src/fixes.d.ts +10 -0
- package/dist/src/fixes.js +45 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +10 -0
- package/dist/src/lexer.d.ts +18 -0
- package/dist/src/lexer.js +135 -0
- package/dist/src/linter.d.ts +67 -0
- package/dist/src/linter.js +137 -0
- package/dist/src/node.d.ts +13 -0
- package/dist/src/node.js +98 -0
- package/dist/src/parser.d.ts +2 -0
- package/dist/src/parser.js +791 -0
- package/dist/src/plugin-loader.d.ts +3 -0
- package/dist/src/plugin-loader.js +31 -0
- package/dist/src/plugins.d.ts +34 -0
- package/dist/src/plugins.js +59 -0
- package/dist/src/reporters.d.ts +22 -0
- package/dist/src/reporters.js +286 -0
- package/dist/src/rules.d.ts +23 -0
- package/dist/src/rules.js +104 -0
- package/dist/src/sdk.d.ts +6 -0
- package/dist/src/sdk.js +130 -0
- package/dist/src/walk.d.ts +28 -0
- package/dist/src/walk.js +110 -0
- package/docs/rules/MS000.md +7 -0
- package/docs/rules/MS001.md +46 -0
- package/docs/rules/MS002.md +42 -0
- package/docs/rules/MS003.md +41 -0
- package/docs/rules/MS004.md +41 -0
- package/docs/rules/MS005.md +32 -0
- package/docs/rules/MS006.md +17 -0
- package/docs/rules/MS007.md +44 -0
- package/docs/rules/MS008.md +59 -0
- package/docs/rules/MS009.md +45 -0
- package/docs/rules/MS010.md +46 -0
- package/docs/rules/MS101.md +32 -0
- package/docs/rules/MS102.md +31 -0
- package/package.json +67 -4
- package/skills/midnight-shield/SKILL.md +67 -0
|
@@ -0,0 +1,791 @@
|
|
|
1
|
+
// Recursive-descent parser for Compact.
|
|
2
|
+
// It is tolerant: a statement or declaration it cannot parse is skipped and
|
|
3
|
+
// reported as a diagnostic, so one unsupported construct never hides findings
|
|
4
|
+
// in the rest of the file.
|
|
5
|
+
import { lex } from "./lexer.js";
|
|
6
|
+
class ParseError extends Error {
|
|
7
|
+
loc;
|
|
8
|
+
constructor(message, loc) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.loc = loc;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const MODIFIERS = new Set(["export", "sealed", "pure"]);
|
|
14
|
+
export function parse(source) {
|
|
15
|
+
const { tokens } = lex(source);
|
|
16
|
+
return new Parser(tokens).parseProgram();
|
|
17
|
+
}
|
|
18
|
+
class Parser {
|
|
19
|
+
tokens;
|
|
20
|
+
pos = 0;
|
|
21
|
+
diagnostics = [];
|
|
22
|
+
constructor(tokens) {
|
|
23
|
+
this.tokens = tokens;
|
|
24
|
+
}
|
|
25
|
+
// ---------- token helpers ----------
|
|
26
|
+
peek(offset = 0) {
|
|
27
|
+
return this.tokens[Math.min(this.pos + offset, this.tokens.length - 1)];
|
|
28
|
+
}
|
|
29
|
+
next() {
|
|
30
|
+
const t = this.peek();
|
|
31
|
+
if (t.kind !== "eof")
|
|
32
|
+
this.pos++;
|
|
33
|
+
return t;
|
|
34
|
+
}
|
|
35
|
+
is(value, offset = 0) {
|
|
36
|
+
const t = this.peek(offset);
|
|
37
|
+
return (t.kind === "punct" || t.kind === "ident") && t.value === value;
|
|
38
|
+
}
|
|
39
|
+
eat(value) {
|
|
40
|
+
if (this.is(value)) {
|
|
41
|
+
this.pos++;
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
expect(value) {
|
|
47
|
+
if (!this.is(value)) {
|
|
48
|
+
const t = this.peek();
|
|
49
|
+
throw new ParseError(`expected '${value}' but found '${t.value || "end of file"}'`, this.loc(t));
|
|
50
|
+
}
|
|
51
|
+
return this.next();
|
|
52
|
+
}
|
|
53
|
+
ident() {
|
|
54
|
+
const t = this.peek();
|
|
55
|
+
if (t.kind !== "ident")
|
|
56
|
+
throw new ParseError(`expected identifier but found '${t.value}'`, this.loc(t));
|
|
57
|
+
this.pos++;
|
|
58
|
+
return t.value;
|
|
59
|
+
}
|
|
60
|
+
loc(t = this.peek()) {
|
|
61
|
+
return { line: t.line, col: t.col };
|
|
62
|
+
}
|
|
63
|
+
atEnd() {
|
|
64
|
+
return this.peek().kind === "eof";
|
|
65
|
+
}
|
|
66
|
+
/** Skip a balanced bracket group starting at the current opening token. */
|
|
67
|
+
skipBalanced() {
|
|
68
|
+
const open = this.next().value;
|
|
69
|
+
const close = open === "{" ? "}" : open === "(" ? ")" : "]";
|
|
70
|
+
let depth = 1;
|
|
71
|
+
while (!this.atEnd() && depth > 0) {
|
|
72
|
+
const t = this.next();
|
|
73
|
+
if (t.value === open)
|
|
74
|
+
depth++;
|
|
75
|
+
else if (t.value === close)
|
|
76
|
+
depth--;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Error recovery: skip to the end of the current statement or block. */
|
|
80
|
+
recover() {
|
|
81
|
+
let depth = 0;
|
|
82
|
+
while (!this.atEnd()) {
|
|
83
|
+
const t = this.peek();
|
|
84
|
+
if (t.value === "{" || t.value === "(" || t.value === "[")
|
|
85
|
+
depth++;
|
|
86
|
+
if (t.value === "}" || t.value === ")" || t.value === "]") {
|
|
87
|
+
if (depth === 0)
|
|
88
|
+
return;
|
|
89
|
+
depth--;
|
|
90
|
+
if (depth === 0 && t.value === "}") {
|
|
91
|
+
this.pos++;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (t.value === ";" && depth === 0) {
|
|
96
|
+
this.pos++;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
this.pos++;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
report(e) {
|
|
103
|
+
if (e instanceof ParseError) {
|
|
104
|
+
this.diagnostics.push({ message: e.message, loc: e.loc });
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
throw e;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// ---------- program ----------
|
|
111
|
+
parseProgram() {
|
|
112
|
+
const decls = this.parseDecls(false);
|
|
113
|
+
return { decls, diagnostics: this.diagnostics };
|
|
114
|
+
}
|
|
115
|
+
parseDecls(inModule) {
|
|
116
|
+
const decls = [];
|
|
117
|
+
while (!this.atEnd()) {
|
|
118
|
+
if (inModule && this.is("}"))
|
|
119
|
+
break;
|
|
120
|
+
const start = this.pos;
|
|
121
|
+
try {
|
|
122
|
+
const d = this.parseDecl();
|
|
123
|
+
if (d)
|
|
124
|
+
decls.push(d);
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
this.report(e);
|
|
128
|
+
if (this.pos === start)
|
|
129
|
+
this.pos++;
|
|
130
|
+
this.recover();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return decls;
|
|
134
|
+
}
|
|
135
|
+
parseDecl() {
|
|
136
|
+
const startTok = this.peek();
|
|
137
|
+
if (this.is("pragma")) {
|
|
138
|
+
const parts = [];
|
|
139
|
+
this.next();
|
|
140
|
+
while (!this.atEnd() && !this.is(";"))
|
|
141
|
+
parts.push(this.next().value);
|
|
142
|
+
this.eat(";");
|
|
143
|
+
return { kind: "pragma", text: parts.join(" "), loc: this.loc(startTok) };
|
|
144
|
+
}
|
|
145
|
+
if (this.is("include")) {
|
|
146
|
+
this.next();
|
|
147
|
+
const t = this.next();
|
|
148
|
+
this.eat(";");
|
|
149
|
+
return t.kind === "string" ? { kind: "include", path: t.value.slice(1, -1), loc: this.loc(startTok) } : undefined;
|
|
150
|
+
}
|
|
151
|
+
if (this.is("import")) {
|
|
152
|
+
// import Name; import "path"<args> prefix P_; import { a, b } from "path" prefix P_;
|
|
153
|
+
this.next();
|
|
154
|
+
if (this.is("{")) {
|
|
155
|
+
this.skipBalanced();
|
|
156
|
+
this.expect("from");
|
|
157
|
+
}
|
|
158
|
+
const t = this.next();
|
|
159
|
+
const isPath = t.kind === "string";
|
|
160
|
+
const target = isPath ? t.value.slice(1, -1) : t.value;
|
|
161
|
+
this.skipTypeParams();
|
|
162
|
+
let prefix = "";
|
|
163
|
+
if (this.eat("prefix"))
|
|
164
|
+
prefix = this.ident();
|
|
165
|
+
while (!this.atEnd() && !this.is(";"))
|
|
166
|
+
this.next();
|
|
167
|
+
this.eat(";");
|
|
168
|
+
return { kind: "import", target, isPath, prefix, loc: this.loc(startTok) };
|
|
169
|
+
}
|
|
170
|
+
if (this.is("export") && this.is("{", 1)) {
|
|
171
|
+
this.next();
|
|
172
|
+
this.skipBalanced();
|
|
173
|
+
this.eat(";");
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
if (this.is(";")) {
|
|
177
|
+
this.next();
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
let exported = false;
|
|
181
|
+
let sealed = false;
|
|
182
|
+
let pure = false;
|
|
183
|
+
while (this.peek().kind === "ident" && MODIFIERS.has(this.peek().value)) {
|
|
184
|
+
const m = this.next().value;
|
|
185
|
+
if (m === "export")
|
|
186
|
+
exported = true;
|
|
187
|
+
if (m === "sealed")
|
|
188
|
+
sealed = true;
|
|
189
|
+
if (m === "pure")
|
|
190
|
+
pure = true;
|
|
191
|
+
}
|
|
192
|
+
const kw = this.peek();
|
|
193
|
+
const loc = this.loc(kw);
|
|
194
|
+
switch (kw.value) {
|
|
195
|
+
case "ledger": {
|
|
196
|
+
this.next();
|
|
197
|
+
const name = this.ident();
|
|
198
|
+
this.expect(":");
|
|
199
|
+
const type = this.parseType();
|
|
200
|
+
this.eat(";");
|
|
201
|
+
return { kind: "ledger", name, type, exported, sealed, loc };
|
|
202
|
+
}
|
|
203
|
+
case "witness": {
|
|
204
|
+
this.next();
|
|
205
|
+
const name = this.ident();
|
|
206
|
+
this.skipTypeParams();
|
|
207
|
+
const params = this.parseParams();
|
|
208
|
+
let returnType;
|
|
209
|
+
if (this.eat(":"))
|
|
210
|
+
returnType = this.parseType();
|
|
211
|
+
this.eat(";");
|
|
212
|
+
return { kind: "witness", name, params, returnType, loc };
|
|
213
|
+
}
|
|
214
|
+
case "circuit": {
|
|
215
|
+
this.next();
|
|
216
|
+
const name = this.ident();
|
|
217
|
+
this.skipTypeParams();
|
|
218
|
+
const params = this.parseParams();
|
|
219
|
+
let returnType;
|
|
220
|
+
if (this.eat(":"))
|
|
221
|
+
returnType = this.parseType();
|
|
222
|
+
let body;
|
|
223
|
+
if (this.is("{"))
|
|
224
|
+
body = this.parseBlock();
|
|
225
|
+
else
|
|
226
|
+
this.eat(";");
|
|
227
|
+
return { kind: "circuit", name, params, returnType, body, exported, pure, loc };
|
|
228
|
+
}
|
|
229
|
+
case "constructor": {
|
|
230
|
+
this.next();
|
|
231
|
+
const params = this.parseParams();
|
|
232
|
+
const body = this.parseBlock();
|
|
233
|
+
return { kind: "constructor", params, body, loc };
|
|
234
|
+
}
|
|
235
|
+
case "struct": {
|
|
236
|
+
this.next();
|
|
237
|
+
const name = this.ident();
|
|
238
|
+
this.skipTypeParams();
|
|
239
|
+
this.expect("{");
|
|
240
|
+
const fields = [];
|
|
241
|
+
while (!this.atEnd() && !this.is("}")) {
|
|
242
|
+
const floc = this.loc();
|
|
243
|
+
const fname = this.ident();
|
|
244
|
+
this.expect(":");
|
|
245
|
+
const type = this.parseType();
|
|
246
|
+
fields.push({ name: fname, type, loc: floc });
|
|
247
|
+
if (!this.eat(","))
|
|
248
|
+
this.eat(";");
|
|
249
|
+
}
|
|
250
|
+
this.expect("}");
|
|
251
|
+
this.eat(";");
|
|
252
|
+
return { kind: "struct", name, fields, loc };
|
|
253
|
+
}
|
|
254
|
+
case "enum": {
|
|
255
|
+
this.next();
|
|
256
|
+
const name = this.ident();
|
|
257
|
+
this.expect("{");
|
|
258
|
+
const members = [];
|
|
259
|
+
while (!this.atEnd() && !this.is("}")) {
|
|
260
|
+
members.push(this.ident());
|
|
261
|
+
this.eat(",");
|
|
262
|
+
}
|
|
263
|
+
this.expect("}");
|
|
264
|
+
this.eat(";");
|
|
265
|
+
return { kind: "enum", name, members, loc };
|
|
266
|
+
}
|
|
267
|
+
case "module": {
|
|
268
|
+
this.next();
|
|
269
|
+
const name = this.ident();
|
|
270
|
+
this.skipTypeParams();
|
|
271
|
+
this.expect("{");
|
|
272
|
+
const decls = this.parseDecls(true);
|
|
273
|
+
this.expect("}");
|
|
274
|
+
return { kind: "module", name, decls, loc };
|
|
275
|
+
}
|
|
276
|
+
case "new":
|
|
277
|
+
case "type": {
|
|
278
|
+
// `new type Name = T;` or `type Name = T;`
|
|
279
|
+
if (kw.value === "new")
|
|
280
|
+
this.next();
|
|
281
|
+
this.expect("type");
|
|
282
|
+
const name = this.ident();
|
|
283
|
+
this.skipTypeParams();
|
|
284
|
+
this.expect("=");
|
|
285
|
+
const type = this.parseType();
|
|
286
|
+
this.eat(";");
|
|
287
|
+
return { kind: "typeAlias", name, type, loc };
|
|
288
|
+
}
|
|
289
|
+
case "contract": {
|
|
290
|
+
// Cross-contract interface declaration: nothing to analyze.
|
|
291
|
+
this.next();
|
|
292
|
+
this.ident();
|
|
293
|
+
if (this.is("{"))
|
|
294
|
+
this.skipBalanced();
|
|
295
|
+
this.eat(";");
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
default:
|
|
299
|
+
throw new ParseError(`unexpected '${kw.value || "end of file"}' at top level`, loc);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
skipTypeParams() {
|
|
303
|
+
if (!this.is("<"))
|
|
304
|
+
return;
|
|
305
|
+
let depth = 0;
|
|
306
|
+
do {
|
|
307
|
+
const t = this.next();
|
|
308
|
+
if (t.value === "<")
|
|
309
|
+
depth++;
|
|
310
|
+
if (t.value === ">")
|
|
311
|
+
depth--;
|
|
312
|
+
} while (!this.atEnd() && depth > 0);
|
|
313
|
+
}
|
|
314
|
+
parseParams() {
|
|
315
|
+
this.expect("(");
|
|
316
|
+
const params = [];
|
|
317
|
+
while (!this.atEnd() && !this.is(")")) {
|
|
318
|
+
const loc = this.loc();
|
|
319
|
+
if (this.is("[") || this.is("{")) {
|
|
320
|
+
// destructured parameter: keep going without a name
|
|
321
|
+
this.skipBalanced();
|
|
322
|
+
let type;
|
|
323
|
+
if (this.eat(":"))
|
|
324
|
+
type = this.parseType();
|
|
325
|
+
params.push({ name: "_", type, loc });
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
const name = this.ident();
|
|
329
|
+
let type;
|
|
330
|
+
if (this.eat(":"))
|
|
331
|
+
type = this.parseType();
|
|
332
|
+
params.push({ name, type, loc });
|
|
333
|
+
}
|
|
334
|
+
if (!this.eat(","))
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
this.expect(")");
|
|
338
|
+
return params;
|
|
339
|
+
}
|
|
340
|
+
// ---------- types ----------
|
|
341
|
+
parseType() {
|
|
342
|
+
const start = this.pos;
|
|
343
|
+
if (this.is("[")) {
|
|
344
|
+
// tuple type: [] or [A, B]
|
|
345
|
+
this.next();
|
|
346
|
+
const args = [];
|
|
347
|
+
while (!this.atEnd() && !this.is("]")) {
|
|
348
|
+
args.push({ kind: "type", type: this.parseType() });
|
|
349
|
+
if (!this.eat(","))
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
this.expect("]");
|
|
353
|
+
return { name: "[]", args, text: this.textFrom(start) };
|
|
354
|
+
}
|
|
355
|
+
let name = this.ident();
|
|
356
|
+
while (this.is(".") && this.peek(1).kind === "ident") {
|
|
357
|
+
this.next();
|
|
358
|
+
name += "." + this.ident();
|
|
359
|
+
}
|
|
360
|
+
const args = [];
|
|
361
|
+
if (this.is("<")) {
|
|
362
|
+
this.next();
|
|
363
|
+
while (!this.atEnd() && !this.is(">")) {
|
|
364
|
+
args.push(this.parseTypeArg());
|
|
365
|
+
if (!this.eat(","))
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
this.expect(">");
|
|
369
|
+
}
|
|
370
|
+
return { name, args, text: this.textFrom(start) };
|
|
371
|
+
}
|
|
372
|
+
parseTypeArg() {
|
|
373
|
+
const t = this.peek();
|
|
374
|
+
if (t.kind === "number") {
|
|
375
|
+
this.next();
|
|
376
|
+
const from = parseNumber(t.value);
|
|
377
|
+
if (this.eat("..")) {
|
|
378
|
+
const to = this.next();
|
|
379
|
+
return { kind: "range", from, to: parseNumber(to.value) };
|
|
380
|
+
}
|
|
381
|
+
return { kind: "number", value: from };
|
|
382
|
+
}
|
|
383
|
+
if (t.kind === "string") {
|
|
384
|
+
this.next();
|
|
385
|
+
return { kind: "string", value: t.value.slice(1, -1) };
|
|
386
|
+
}
|
|
387
|
+
if (this.is("#")) {
|
|
388
|
+
this.next();
|
|
389
|
+
return { kind: "param", name: this.ident() };
|
|
390
|
+
}
|
|
391
|
+
return { kind: "type", type: this.parseType() };
|
|
392
|
+
}
|
|
393
|
+
textFrom(start) {
|
|
394
|
+
let out = "";
|
|
395
|
+
for (let i = start; i < this.pos; i++) {
|
|
396
|
+
const v = this.tokens[i].value;
|
|
397
|
+
out += v === "," ? ", " : v;
|
|
398
|
+
}
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
/** Try to parse `<T, U>` generic arguments; backtrack if this is a comparison. */
|
|
402
|
+
tryTypeArgs() {
|
|
403
|
+
if (!this.is("<"))
|
|
404
|
+
return undefined;
|
|
405
|
+
const save = this.pos;
|
|
406
|
+
const saveDiag = this.diagnostics.length;
|
|
407
|
+
try {
|
|
408
|
+
this.next();
|
|
409
|
+
const args = [];
|
|
410
|
+
while (!this.is(">")) {
|
|
411
|
+
const t = this.peek();
|
|
412
|
+
if (t.kind === "number") {
|
|
413
|
+
this.next();
|
|
414
|
+
if (this.eat(".."))
|
|
415
|
+
this.next();
|
|
416
|
+
args.push({ name: t.value, args: [], text: t.value });
|
|
417
|
+
}
|
|
418
|
+
else if (t.kind === "string") {
|
|
419
|
+
this.next();
|
|
420
|
+
args.push({ name: t.value, args: [], text: t.value });
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
args.push(this.parseType());
|
|
424
|
+
}
|
|
425
|
+
if (!this.eat(","))
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
this.expect(">");
|
|
429
|
+
const n = this.peek();
|
|
430
|
+
// After real type arguments comes a call, a struct literal, or an operator /
|
|
431
|
+
// closing token, never the start of another operand.
|
|
432
|
+
const ok = !(n.kind === "ident" || n.kind === "number" || n.kind === "string" || ["[", "!", "-"].includes(n.value));
|
|
433
|
+
if (!ok)
|
|
434
|
+
throw new ParseError("not type arguments", this.loc());
|
|
435
|
+
return args;
|
|
436
|
+
}
|
|
437
|
+
catch {
|
|
438
|
+
this.pos = save;
|
|
439
|
+
this.diagnostics.length = saveDiag;
|
|
440
|
+
return undefined;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
// ---------- statements ----------
|
|
444
|
+
parseBlock() {
|
|
445
|
+
this.expect("{");
|
|
446
|
+
const body = [];
|
|
447
|
+
while (!this.atEnd() && !this.is("}")) {
|
|
448
|
+
const start = this.pos;
|
|
449
|
+
try {
|
|
450
|
+
body.push(this.parseStmt());
|
|
451
|
+
}
|
|
452
|
+
catch (e) {
|
|
453
|
+
this.report(e);
|
|
454
|
+
if (this.pos === start)
|
|
455
|
+
this.pos++;
|
|
456
|
+
this.recover();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
this.expect("}");
|
|
460
|
+
return body;
|
|
461
|
+
}
|
|
462
|
+
parseBody() {
|
|
463
|
+
return this.is("{") ? this.parseBlock() : [this.parseStmt()];
|
|
464
|
+
}
|
|
465
|
+
parseStmt() {
|
|
466
|
+
const loc = this.loc();
|
|
467
|
+
if (this.is("{"))
|
|
468
|
+
return { kind: "block", body: this.parseBlock(), loc };
|
|
469
|
+
if (this.is("const")) {
|
|
470
|
+
this.next();
|
|
471
|
+
const names = [];
|
|
472
|
+
if (this.is("[") || this.is("{")) {
|
|
473
|
+
const close = this.is("[") ? "]" : "}";
|
|
474
|
+
this.next();
|
|
475
|
+
while (!this.atEnd() && !this.is(close)) {
|
|
476
|
+
if (this.peek().kind === "ident")
|
|
477
|
+
names.push(this.next().value);
|
|
478
|
+
else
|
|
479
|
+
this.next();
|
|
480
|
+
}
|
|
481
|
+
this.expect(close);
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
names.push(this.ident());
|
|
485
|
+
}
|
|
486
|
+
let type;
|
|
487
|
+
if (this.eat(":"))
|
|
488
|
+
type = this.parseType();
|
|
489
|
+
this.expect("=");
|
|
490
|
+
const init = this.parseExpr();
|
|
491
|
+
this.eat(";");
|
|
492
|
+
return { kind: "const", names, type, init, loc };
|
|
493
|
+
}
|
|
494
|
+
if (this.is("if")) {
|
|
495
|
+
this.next();
|
|
496
|
+
this.expect("(");
|
|
497
|
+
const test = this.parseExpr();
|
|
498
|
+
this.expect(")");
|
|
499
|
+
const then = this.parseBody();
|
|
500
|
+
let elseBody;
|
|
501
|
+
if (this.eat("else"))
|
|
502
|
+
elseBody = this.parseBody();
|
|
503
|
+
return { kind: "if", test, then, else: elseBody, loc };
|
|
504
|
+
}
|
|
505
|
+
if (this.is("for")) {
|
|
506
|
+
this.next();
|
|
507
|
+
this.expect("(");
|
|
508
|
+
this.eat("const");
|
|
509
|
+
const name = this.ident();
|
|
510
|
+
this.expect("of");
|
|
511
|
+
const iter = this.parseExpr();
|
|
512
|
+
this.expect(")");
|
|
513
|
+
const body = this.parseBody();
|
|
514
|
+
return { kind: "for", name, iter, body, loc };
|
|
515
|
+
}
|
|
516
|
+
if (this.is("return")) {
|
|
517
|
+
this.next();
|
|
518
|
+
let value;
|
|
519
|
+
if (!this.is(";") && !this.is("}"))
|
|
520
|
+
value = this.parseExpr();
|
|
521
|
+
this.eat(";");
|
|
522
|
+
return { kind: "return", value, loc };
|
|
523
|
+
}
|
|
524
|
+
const expr = this.parseExpr();
|
|
525
|
+
for (const op of ["=", "+=", "-=", "*="]) {
|
|
526
|
+
if (this.is(op)) {
|
|
527
|
+
this.next();
|
|
528
|
+
const value = this.parseExpr();
|
|
529
|
+
this.eat(";");
|
|
530
|
+
return { kind: "assign", target: expr, op, value, loc };
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
this.eat(";");
|
|
534
|
+
return { kind: "expr", expr, loc };
|
|
535
|
+
}
|
|
536
|
+
// ---------- expressions ----------
|
|
537
|
+
parseExpr() {
|
|
538
|
+
return this.parseCond();
|
|
539
|
+
}
|
|
540
|
+
parseCond() {
|
|
541
|
+
const test = this.parseBinary(0);
|
|
542
|
+
if (this.is("?")) {
|
|
543
|
+
const loc = this.loc();
|
|
544
|
+
this.next();
|
|
545
|
+
const then = this.parseExpr();
|
|
546
|
+
this.expect(":");
|
|
547
|
+
const elseExpr = this.parseExpr();
|
|
548
|
+
return { kind: "cond", test, then, else: elseExpr, loc };
|
|
549
|
+
}
|
|
550
|
+
return test;
|
|
551
|
+
}
|
|
552
|
+
static PRECEDENCE = [
|
|
553
|
+
["??"],
|
|
554
|
+
["||"],
|
|
555
|
+
["&&"],
|
|
556
|
+
["==", "!="],
|
|
557
|
+
["<", "<=", ">", ">="],
|
|
558
|
+
[".."],
|
|
559
|
+
["+", "-"],
|
|
560
|
+
["*", "/", "%"],
|
|
561
|
+
];
|
|
562
|
+
parseBinary(level) {
|
|
563
|
+
if (level >= Parser.PRECEDENCE.length)
|
|
564
|
+
return this.parseUnary();
|
|
565
|
+
let left = this.parseBinary(level + 1);
|
|
566
|
+
for (;;) {
|
|
567
|
+
const op = this.binaryOp(Parser.PRECEDENCE[level]);
|
|
568
|
+
if (!op)
|
|
569
|
+
return left;
|
|
570
|
+
const loc = this.loc();
|
|
571
|
+
this.pos += op.length;
|
|
572
|
+
const right = this.parseBinary(level + 1);
|
|
573
|
+
left = { kind: "binary", op: op.op, left, right, loc };
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/** Match a binary operator, joining `>` `=` into `>=` (the lexer never emits `>=`). */
|
|
577
|
+
binaryOp(ops) {
|
|
578
|
+
const t = this.peek();
|
|
579
|
+
if (t.kind !== "punct")
|
|
580
|
+
return undefined;
|
|
581
|
+
if (t.value === ">" && this.is("=", 1) && !this.is("=", 2) && this.peek(1).offset === t.offset + 1) {
|
|
582
|
+
return ops.includes(">=") ? { op: ">=", length: 2 } : undefined;
|
|
583
|
+
}
|
|
584
|
+
return ops.includes(t.value) ? { op: t.value, length: 1 } : undefined;
|
|
585
|
+
}
|
|
586
|
+
parseUnary() {
|
|
587
|
+
if (this.is("!") || this.is("-")) {
|
|
588
|
+
const loc = this.loc();
|
|
589
|
+
const op = this.next().value;
|
|
590
|
+
return { kind: "unary", op, arg: this.parseUnary(), loc };
|
|
591
|
+
}
|
|
592
|
+
if (this.is("...")) {
|
|
593
|
+
const loc = this.loc();
|
|
594
|
+
this.next();
|
|
595
|
+
return { kind: "spread", arg: this.parseUnary(), loc };
|
|
596
|
+
}
|
|
597
|
+
let expr = this.parsePostfix();
|
|
598
|
+
while (this.is("as")) {
|
|
599
|
+
const loc = exprLoc(expr);
|
|
600
|
+
this.next();
|
|
601
|
+
expr = { kind: "cast", expr, type: this.parseType(), loc };
|
|
602
|
+
}
|
|
603
|
+
return expr;
|
|
604
|
+
}
|
|
605
|
+
parsePostfix() {
|
|
606
|
+
let expr = this.parsePrimary();
|
|
607
|
+
for (;;) {
|
|
608
|
+
// postfix expressions are located at the start of the whole expression
|
|
609
|
+
const loc = exprLoc(expr);
|
|
610
|
+
if (this.is(".") && this.peek(1).kind === "ident") {
|
|
611
|
+
this.next();
|
|
612
|
+
const property = this.ident();
|
|
613
|
+
expr = { kind: "member", object: expr, property, loc };
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (this.is("[")) {
|
|
617
|
+
this.next();
|
|
618
|
+
const index = this.parseExpr();
|
|
619
|
+
this.expect("]");
|
|
620
|
+
expr = { kind: "index", object: expr, index, loc };
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (this.is("<") && (expr.kind === "ident" || expr.kind === "member")) {
|
|
624
|
+
const typeArgs = this.tryTypeArgs();
|
|
625
|
+
if (typeArgs) {
|
|
626
|
+
if (this.is("(")) {
|
|
627
|
+
expr = { kind: "call", callee: expr, typeArgs, args: this.parseArgs(), loc: exprLoc(expr) };
|
|
628
|
+
}
|
|
629
|
+
else if (this.is("{") && expr.kind === "ident") {
|
|
630
|
+
const type = { name: expr.name, args: typeArgs.map((t) => ({ kind: "type", type: t })), text: expr.name };
|
|
631
|
+
expr = this.parseStructLiteral(type, exprLoc(expr));
|
|
632
|
+
}
|
|
633
|
+
else if (expr.kind === "ident") {
|
|
634
|
+
expr = { ...expr, typeArgs };
|
|
635
|
+
}
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
return expr;
|
|
639
|
+
}
|
|
640
|
+
if (this.is("(")) {
|
|
641
|
+
expr = { kind: "call", callee: expr, args: this.parseArgs(), loc: exprLoc(expr) };
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
return expr;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
parseArgs() {
|
|
648
|
+
this.expect("(");
|
|
649
|
+
const args = [];
|
|
650
|
+
while (!this.atEnd() && !this.is(")")) {
|
|
651
|
+
args.push(this.parseExpr());
|
|
652
|
+
if (!this.eat(","))
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
this.expect(")");
|
|
656
|
+
return args;
|
|
657
|
+
}
|
|
658
|
+
parseStructLiteral(type, loc) {
|
|
659
|
+
this.expect("{");
|
|
660
|
+
const fields = [];
|
|
661
|
+
while (!this.atEnd() && !this.is("}")) {
|
|
662
|
+
if (this.is("...")) {
|
|
663
|
+
this.next();
|
|
664
|
+
fields.push({ name: "...", value: this.parseExpr() });
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
const name = this.ident();
|
|
668
|
+
if (this.eat(":"))
|
|
669
|
+
fields.push({ name, value: this.parseExpr() });
|
|
670
|
+
else
|
|
671
|
+
fields.push({ name, value: { kind: "ident", name, loc } });
|
|
672
|
+
}
|
|
673
|
+
if (!this.eat(","))
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
this.expect("}");
|
|
677
|
+
return { kind: "struct", type, fields, loc };
|
|
678
|
+
}
|
|
679
|
+
looksLikeStructLiteral() {
|
|
680
|
+
// Called with the current token at `{`.
|
|
681
|
+
if (!this.is("{"))
|
|
682
|
+
return false;
|
|
683
|
+
const a = this.peek(1);
|
|
684
|
+
const b = this.peek(2);
|
|
685
|
+
if (a.value === "}")
|
|
686
|
+
return true;
|
|
687
|
+
if (a.value === "...")
|
|
688
|
+
return true;
|
|
689
|
+
return a.kind === "ident" && (b.value === ":" || b.value === "," || b.value === "}");
|
|
690
|
+
}
|
|
691
|
+
isLambdaAhead() {
|
|
692
|
+
// Current token is `(`; find its match and look at what follows.
|
|
693
|
+
let depth = 0;
|
|
694
|
+
for (let i = this.pos; i < this.tokens.length; i++) {
|
|
695
|
+
const v = this.tokens[i].value;
|
|
696
|
+
if (v === "(")
|
|
697
|
+
depth++;
|
|
698
|
+
else if (v === ")") {
|
|
699
|
+
depth--;
|
|
700
|
+
if (depth === 0) {
|
|
701
|
+
const after = this.tokens[i + 1]?.value;
|
|
702
|
+
if (after === "=>")
|
|
703
|
+
return true;
|
|
704
|
+
if (after !== ":")
|
|
705
|
+
return false;
|
|
706
|
+
// `(a: T): R => ...` vs. the `(x) :` of a conditional expression:
|
|
707
|
+
// only a lambda has `=>` after the return type.
|
|
708
|
+
let d = 0;
|
|
709
|
+
for (let j = i + 2; j < this.tokens.length; j++) {
|
|
710
|
+
const w = this.tokens[j].value;
|
|
711
|
+
if (w === "<" || w === "[" || w === "(")
|
|
712
|
+
d++;
|
|
713
|
+
else if (w === ">" || w === "]" || w === ")") {
|
|
714
|
+
if (d === 0)
|
|
715
|
+
return false;
|
|
716
|
+
d--;
|
|
717
|
+
}
|
|
718
|
+
else if (d === 0 && w === "=>")
|
|
719
|
+
return true;
|
|
720
|
+
else if (d === 0 && (w === "," || w === ";" || w === "?" || w === "{"))
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
parsePrimary() {
|
|
730
|
+
const t = this.peek();
|
|
731
|
+
const loc = this.loc(t);
|
|
732
|
+
if (t.kind === "number") {
|
|
733
|
+
this.next();
|
|
734
|
+
return { kind: "number", value: t.value, loc };
|
|
735
|
+
}
|
|
736
|
+
if (t.kind === "string") {
|
|
737
|
+
this.next();
|
|
738
|
+
return { kind: "string", value: t.value.slice(1, -1), loc };
|
|
739
|
+
}
|
|
740
|
+
if (this.is("(")) {
|
|
741
|
+
if (this.isLambdaAhead())
|
|
742
|
+
return this.parseLambda();
|
|
743
|
+
this.next();
|
|
744
|
+
const inner = this.parseExpr();
|
|
745
|
+
this.expect(")");
|
|
746
|
+
return inner;
|
|
747
|
+
}
|
|
748
|
+
if (this.is("[")) {
|
|
749
|
+
this.next();
|
|
750
|
+
const elements = [];
|
|
751
|
+
while (!this.atEnd() && !this.is("]")) {
|
|
752
|
+
elements.push(this.parseExpr());
|
|
753
|
+
if (!this.eat(","))
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
this.expect("]");
|
|
757
|
+
return { kind: "vector", elements, loc };
|
|
758
|
+
}
|
|
759
|
+
if (t.kind === "ident") {
|
|
760
|
+
this.next();
|
|
761
|
+
// single-parameter lambda without parentheses: x => ...
|
|
762
|
+
if (this.is("=>")) {
|
|
763
|
+
this.next();
|
|
764
|
+
return { kind: "lambda", params: [{ name: t.value, loc }], body: this.lambdaBody(), loc };
|
|
765
|
+
}
|
|
766
|
+
if (this.is("{") && /^[A-Z]/.test(t.value) && this.looksLikeStructLiteral()) {
|
|
767
|
+
return this.parseStructLiteral({ name: t.value, args: [], text: t.value }, loc);
|
|
768
|
+
}
|
|
769
|
+
return { kind: "ident", name: t.value, loc };
|
|
770
|
+
}
|
|
771
|
+
throw new ParseError(`unexpected '${t.value || "end of file"}' in expression`, loc);
|
|
772
|
+
}
|
|
773
|
+
parseLambda() {
|
|
774
|
+
const loc = this.loc();
|
|
775
|
+
const params = this.parseParams();
|
|
776
|
+
if (this.eat(":"))
|
|
777
|
+
this.parseType();
|
|
778
|
+
this.expect("=>");
|
|
779
|
+
return { kind: "lambda", params, body: this.lambdaBody(), loc };
|
|
780
|
+
}
|
|
781
|
+
lambdaBody() {
|
|
782
|
+
return this.is("{") ? this.parseBlock() : this.parseExpr();
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function exprLoc(e) {
|
|
786
|
+
return e.loc;
|
|
787
|
+
}
|
|
788
|
+
function parseNumber(raw) {
|
|
789
|
+
const n = Number(raw.replace(/_/g, ""));
|
|
790
|
+
return Number.isFinite(n) ? n : 0;
|
|
791
|
+
}
|