@aufbau/syntax 0.0.1
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 +47 -0
- package/LICENSE +202 -0
- package/README.md +118 -0
- package/dist/diagnostics.d.ts +26 -0
- package/dist/diagnostics.js +23 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/elab.d.ts +47 -0
- package/dist/elab.js +234 -0
- package/dist/elab.js.map +1 -0
- package/dist/engine.d.ts +20 -0
- package/dist/engine.js +40 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/parse.d.ts +113 -0
- package/dist/parse.js +896 -0
- package/dist/parse.js.map +1 -0
- package/dist/print.d.ts +28 -0
- package/dist/print.js +183 -0
- package/dist/print.js.map +1 -0
- package/dist/reader/annotations.d.ts +92 -0
- package/dist/reader/annotations.js +198 -0
- package/dist/reader/annotations.js.map +1 -0
- package/dist/reader/lex.d.ts +37 -0
- package/dist/reader/lex.js +123 -0
- package/dist/reader/lex.js.map +1 -0
- package/dist/reader/spec.d.ts +88 -0
- package/dist/reader/spec.js +423 -0
- package/dist/reader/spec.js.map +1 -0
- package/dist/reader/statements.d.ts +115 -0
- package/dist/reader/statements.js +577 -0
- package/dist/reader/statements.js.map +1 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +96 -0
- package/dist/scan.js.map +1 -0
- package/dist/term.d.ts +48 -0
- package/dist/term.js +28 -0
- package/dist/term.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MM0 statement parser: file tokens in, statements out.
|
|
3
|
+
*
|
|
4
|
+
* The grammar is the upstream one (mm0.md), in the subset Aufbau reads —
|
|
5
|
+
* summarized in the manual's appendix-grammars.md. Math strings are carried
|
|
6
|
+
* through opaque: scanning their contents is the surface layer's job, and an
|
|
7
|
+
* axiom's body never needs it at all.
|
|
8
|
+
*
|
|
9
|
+
* On a malformed statement the parser records one diagnostic and skips to
|
|
10
|
+
* the next `;`, so a spec author sees every broken statement at once.
|
|
11
|
+
*/
|
|
12
|
+
import { diagnostic } from "../diagnostics.js";
|
|
13
|
+
import { lexFile } from "./lex.js";
|
|
14
|
+
const SORT_MODIFIERS = new Set(["pure", "strict", "provable", "free"]);
|
|
15
|
+
const TEMPLATES = {
|
|
16
|
+
lex_unexpected_character: "unexpected character",
|
|
17
|
+
lex_unterminated_math: "unterminated math string",
|
|
18
|
+
expected_token: "expected {expected} but found {found}",
|
|
19
|
+
unknown_statement: "unknown statement keyword {keyword}",
|
|
20
|
+
bad_precedence: "expected a precedence (a number or max)",
|
|
21
|
+
empty_delimiter: "a delimiter math string must list characters",
|
|
22
|
+
multibyte_delimiter: "delimiter {token} is not a single byte; the engine cannot split on it",
|
|
23
|
+
notation_leading_variable: "the first literal of a notation must be a constant",
|
|
24
|
+
};
|
|
25
|
+
class Cursor {
|
|
26
|
+
tokens;
|
|
27
|
+
sourceLength;
|
|
28
|
+
diagnostics = [];
|
|
29
|
+
position = 0;
|
|
30
|
+
constructor(tokens, sourceLength) {
|
|
31
|
+
this.tokens = tokens;
|
|
32
|
+
this.sourceLength = sourceLength;
|
|
33
|
+
}
|
|
34
|
+
peek() {
|
|
35
|
+
return this.tokens[this.position] ?? null;
|
|
36
|
+
}
|
|
37
|
+
next() {
|
|
38
|
+
const token = this.tokens[this.position] ?? null;
|
|
39
|
+
if (token !== null) {
|
|
40
|
+
this.position += 1;
|
|
41
|
+
}
|
|
42
|
+
return token;
|
|
43
|
+
}
|
|
44
|
+
atEnd() {
|
|
45
|
+
return this.position >= this.tokens.length;
|
|
46
|
+
}
|
|
47
|
+
hereSpan() {
|
|
48
|
+
const token = this.peek();
|
|
49
|
+
return token
|
|
50
|
+
? token.span
|
|
51
|
+
: { start: this.sourceLength, end: this.sourceLength };
|
|
52
|
+
}
|
|
53
|
+
report(id, params, span) {
|
|
54
|
+
const template = TEMPLATES[id] ?? id;
|
|
55
|
+
this.diagnostics.push(diagnostic(id, template, params, span));
|
|
56
|
+
}
|
|
57
|
+
/** Consume a specific symbol, or report and return false. */
|
|
58
|
+
expectSymbol(text) {
|
|
59
|
+
const token = this.peek();
|
|
60
|
+
if (token !== null && token.kind === "symbol" && token.text === text) {
|
|
61
|
+
this.next();
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
this.report("expected_token", { expected: `"${text}"`, found: token ? token.text : "end of file" }, this.hereSpan());
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
expectIdentifier() {
|
|
68
|
+
const token = this.peek();
|
|
69
|
+
if (token !== null && token.kind === "identifier") {
|
|
70
|
+
this.next();
|
|
71
|
+
return token.text;
|
|
72
|
+
}
|
|
73
|
+
this.report("expected_token", {
|
|
74
|
+
expected: "an identifier",
|
|
75
|
+
found: token ? token.text : "end of file",
|
|
76
|
+
}, this.hereSpan());
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
expectMath() {
|
|
80
|
+
const token = this.peek();
|
|
81
|
+
if (token !== null && token.kind === "math") {
|
|
82
|
+
this.next();
|
|
83
|
+
return { span: token.span, text: token.text };
|
|
84
|
+
}
|
|
85
|
+
this.report("expected_token", {
|
|
86
|
+
expected: "a math string",
|
|
87
|
+
found: token ? token.text : "end of file",
|
|
88
|
+
}, this.hereSpan());
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
/** Skip past the next `;`, for error recovery. */
|
|
92
|
+
synchronize() {
|
|
93
|
+
while (!this.atEnd()) {
|
|
94
|
+
const token = this.next();
|
|
95
|
+
if (token !== null && token.kind === "symbol" && token.text === ";") {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** `identifier (identifier)*` — a sort applied to its dependencies. */
|
|
102
|
+
function parseTypeRef(cursor) {
|
|
103
|
+
const start = cursor.hereSpan();
|
|
104
|
+
const sort = cursor.expectIdentifier();
|
|
105
|
+
if (sort === null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const dependencies = [];
|
|
109
|
+
let end = start;
|
|
110
|
+
for (;;) {
|
|
111
|
+
const token = cursor.peek();
|
|
112
|
+
if (token === null || token.kind !== "identifier") {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
cursor.next();
|
|
116
|
+
dependencies.push(token.text);
|
|
117
|
+
end = token.span;
|
|
118
|
+
}
|
|
119
|
+
return { sort, dependencies, span: { start: start.start, end: end.end } };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* One binder group — `{x y: type}` or `(a b: type-or-formula)` — flattened
|
|
123
|
+
* to one Binder per name. `allowFormula` admits hypothesis binders (assert
|
|
124
|
+
* statements); `allowDummy` admits `.`-prefixed names (defs).
|
|
125
|
+
*/
|
|
126
|
+
function parseBinderGroup(cursor, options) {
|
|
127
|
+
const open = cursor.peek();
|
|
128
|
+
if (open === null || open.kind !== "symbol") {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const binds = open.text === "{";
|
|
132
|
+
if (!binds && open.text !== "(") {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
cursor.next();
|
|
136
|
+
const names = [];
|
|
137
|
+
for (;;) {
|
|
138
|
+
const token = cursor.peek();
|
|
139
|
+
if (token === null) {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
if (token.kind === "symbol" && token.text === ".") {
|
|
143
|
+
if (!options.allowDummy) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
cursor.next();
|
|
147
|
+
const name = cursor.expectIdentifier();
|
|
148
|
+
if (name === null) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
names.push({ name, dummy: true, span: token.span });
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (token.kind === "identifier") {
|
|
155
|
+
cursor.next();
|
|
156
|
+
names.push({ name: token.text, dummy: false, span: token.span });
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
// An anonymous binder position, `_`.
|
|
160
|
+
if (token.kind === "symbol" && token.text === "_") {
|
|
161
|
+
cursor.next();
|
|
162
|
+
names.push({ name: "_", dummy: false, span: token.span });
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
if (!cursor.expectSymbol(":")) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
let type;
|
|
171
|
+
const typeToken = cursor.peek();
|
|
172
|
+
if (options.allowFormula &&
|
|
173
|
+
typeToken !== null &&
|
|
174
|
+
typeToken.kind === "math") {
|
|
175
|
+
cursor.next();
|
|
176
|
+
type = { span: typeToken.span, text: typeToken.text };
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
type = parseTypeRef(cursor);
|
|
180
|
+
}
|
|
181
|
+
if (type === null) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
if (!cursor.expectSymbol(binds ? "}" : ")")) {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
return names.map((entry) => ({
|
|
188
|
+
binds,
|
|
189
|
+
dummy: entry.dummy,
|
|
190
|
+
name: entry.name,
|
|
191
|
+
span: entry.span,
|
|
192
|
+
type: type,
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
function parseBinders(cursor, options) {
|
|
196
|
+
const binders = [];
|
|
197
|
+
for (;;) {
|
|
198
|
+
const token = cursor.peek();
|
|
199
|
+
if (token === null ||
|
|
200
|
+
token.kind !== "symbol" ||
|
|
201
|
+
(token.text !== "(" && token.text !== "{")) {
|
|
202
|
+
return binders;
|
|
203
|
+
}
|
|
204
|
+
const group = parseBinderGroup(cursor, options);
|
|
205
|
+
if (group === null) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
binders.push(...group);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function parsePrecedence(cursor) {
|
|
212
|
+
const token = cursor.peek();
|
|
213
|
+
if (token !== null && token.kind === "number") {
|
|
214
|
+
cursor.next();
|
|
215
|
+
return Number.parseInt(token.text, 10);
|
|
216
|
+
}
|
|
217
|
+
if (token !== null && token.kind === "identifier" && token.text === "max") {
|
|
218
|
+
cursor.next();
|
|
219
|
+
return "max";
|
|
220
|
+
}
|
|
221
|
+
cursor.report("bad_precedence", {}, cursor.hereSpan());
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
/** Split a delimiter math string into its whitespace-separated tokens. */
|
|
225
|
+
function delimiterChars(cursor, math) {
|
|
226
|
+
const parts = math.text.split(/\s+/).filter((part) => part.length > 0);
|
|
227
|
+
if (parts.length === 0) {
|
|
228
|
+
cursor.report("empty_delimiter", {}, math.span);
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
for (const part of parts) {
|
|
232
|
+
const points = [...part];
|
|
233
|
+
const single = points.length === 1 && (points[0]?.codePointAt(0) ?? 0x80) <= 0x7f;
|
|
234
|
+
if (!single) {
|
|
235
|
+
cursor.report("multibyte_delimiter", { token: part }, math.span);
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return parts;
|
|
240
|
+
}
|
|
241
|
+
function parseStatement(cursor, keyword, pending) {
|
|
242
|
+
const base = (endSpan) => ({
|
|
243
|
+
annotations: pending.annotations,
|
|
244
|
+
span: { start: pending.start.start, end: endSpan.end },
|
|
245
|
+
});
|
|
246
|
+
const finish = (statement) => cursor.expectSymbol(";") ? statement : null;
|
|
247
|
+
if (SORT_MODIFIERS.has(keyword.text) || keyword.text === "sort") {
|
|
248
|
+
const modifiers = [];
|
|
249
|
+
let cursorToken = keyword;
|
|
250
|
+
while (cursorToken !== null && SORT_MODIFIERS.has(cursorToken.text)) {
|
|
251
|
+
modifiers.push(cursorToken.text);
|
|
252
|
+
cursorToken = cursor.next();
|
|
253
|
+
}
|
|
254
|
+
if (cursorToken === null || cursorToken.text !== "sort") {
|
|
255
|
+
cursor.report("expected_token", {
|
|
256
|
+
expected: '"sort"',
|
|
257
|
+
found: cursorToken ? cursorToken.text : "end of file",
|
|
258
|
+
}, cursor.hereSpan());
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
const name = cursor.expectIdentifier();
|
|
262
|
+
if (name === null) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
return finish({
|
|
266
|
+
...base(cursor.hereSpan()),
|
|
267
|
+
kind: "sort",
|
|
268
|
+
modifiers,
|
|
269
|
+
name,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
switch (keyword.text) {
|
|
273
|
+
case "term": {
|
|
274
|
+
const name = cursor.expectIdentifier();
|
|
275
|
+
if (name === null)
|
|
276
|
+
return null;
|
|
277
|
+
const binders = parseBinders(cursor, {
|
|
278
|
+
allowFormula: false,
|
|
279
|
+
allowDummy: false,
|
|
280
|
+
});
|
|
281
|
+
if (binders === null)
|
|
282
|
+
return null;
|
|
283
|
+
if (!cursor.expectSymbol(":"))
|
|
284
|
+
return null;
|
|
285
|
+
const returnChain = [];
|
|
286
|
+
for (;;) {
|
|
287
|
+
const type = parseTypeRef(cursor);
|
|
288
|
+
if (type === null)
|
|
289
|
+
return null;
|
|
290
|
+
returnChain.push(type);
|
|
291
|
+
const arrow = cursor.peek();
|
|
292
|
+
if (arrow !== null && arrow.kind === "symbol" && arrow.text === ">") {
|
|
293
|
+
cursor.next();
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
return finish({
|
|
299
|
+
...base(cursor.hereSpan()),
|
|
300
|
+
kind: "term",
|
|
301
|
+
name,
|
|
302
|
+
binders,
|
|
303
|
+
returnChain,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
case "def": {
|
|
307
|
+
const name = cursor.expectIdentifier();
|
|
308
|
+
if (name === null)
|
|
309
|
+
return null;
|
|
310
|
+
const binders = parseBinders(cursor, {
|
|
311
|
+
allowFormula: false,
|
|
312
|
+
allowDummy: true,
|
|
313
|
+
});
|
|
314
|
+
if (binders === null)
|
|
315
|
+
return null;
|
|
316
|
+
if (!cursor.expectSymbol(":"))
|
|
317
|
+
return null;
|
|
318
|
+
const returnType = parseTypeRef(cursor);
|
|
319
|
+
if (returnType === null)
|
|
320
|
+
return null;
|
|
321
|
+
let definiens = null;
|
|
322
|
+
const eq = cursor.peek();
|
|
323
|
+
if (eq !== null && eq.kind === "symbol" && eq.text === "=") {
|
|
324
|
+
cursor.next();
|
|
325
|
+
definiens = cursor.expectMath();
|
|
326
|
+
if (definiens === null)
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
return finish({
|
|
330
|
+
...base(cursor.hereSpan()),
|
|
331
|
+
kind: "def",
|
|
332
|
+
name,
|
|
333
|
+
binders,
|
|
334
|
+
returnType,
|
|
335
|
+
definiens,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
case "axiom":
|
|
339
|
+
case "theorem": {
|
|
340
|
+
const name = cursor.expectIdentifier();
|
|
341
|
+
if (name === null)
|
|
342
|
+
return null;
|
|
343
|
+
const binders = parseBinders(cursor, {
|
|
344
|
+
allowFormula: true,
|
|
345
|
+
allowDummy: false,
|
|
346
|
+
});
|
|
347
|
+
if (binders === null)
|
|
348
|
+
return null;
|
|
349
|
+
if (!cursor.expectSymbol(":"))
|
|
350
|
+
return null;
|
|
351
|
+
const chain = [];
|
|
352
|
+
for (;;) {
|
|
353
|
+
const token = cursor.peek();
|
|
354
|
+
if (token !== null && token.kind === "math") {
|
|
355
|
+
cursor.next();
|
|
356
|
+
chain.push({ span: token.span, text: token.text });
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
const type = parseTypeRef(cursor);
|
|
360
|
+
if (type === null)
|
|
361
|
+
return null;
|
|
362
|
+
chain.push(type);
|
|
363
|
+
}
|
|
364
|
+
const arrow = cursor.peek();
|
|
365
|
+
if (arrow !== null && arrow.kind === "symbol" && arrow.text === ">") {
|
|
366
|
+
cursor.next();
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
const conclusion = chain[chain.length - 1];
|
|
372
|
+
if (conclusion === undefined || !("text" in conclusion)) {
|
|
373
|
+
cursor.report("expected_token", { expected: "a concluding math string", found: ";" }, cursor.hereSpan());
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
return finish({
|
|
377
|
+
...base(cursor.hereSpan()),
|
|
378
|
+
kind: keyword.text,
|
|
379
|
+
name,
|
|
380
|
+
binders,
|
|
381
|
+
hypotheses: chain.slice(0, -1),
|
|
382
|
+
conclusion,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
case "delimiter": {
|
|
386
|
+
const first = cursor.expectMath();
|
|
387
|
+
if (first === null)
|
|
388
|
+
return null;
|
|
389
|
+
const firstChars = delimiterChars(cursor, first);
|
|
390
|
+
if (firstChars === null)
|
|
391
|
+
return null;
|
|
392
|
+
const maybeSecond = cursor.peek();
|
|
393
|
+
if (maybeSecond !== null && maybeSecond.kind === "math") {
|
|
394
|
+
cursor.next();
|
|
395
|
+
const secondChars = delimiterChars(cursor, {
|
|
396
|
+
span: maybeSecond.span,
|
|
397
|
+
text: maybeSecond.text,
|
|
398
|
+
});
|
|
399
|
+
if (secondChars === null)
|
|
400
|
+
return null;
|
|
401
|
+
return finish({
|
|
402
|
+
...base(cursor.hereSpan()),
|
|
403
|
+
kind: "delimiter",
|
|
404
|
+
left: firstChars,
|
|
405
|
+
right: secondChars,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return finish({
|
|
409
|
+
...base(cursor.hereSpan()),
|
|
410
|
+
kind: "delimiter",
|
|
411
|
+
left: firstChars,
|
|
412
|
+
right: firstChars,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
case "prefix":
|
|
416
|
+
case "infixl":
|
|
417
|
+
case "infixr": {
|
|
418
|
+
const term = cursor.expectIdentifier();
|
|
419
|
+
if (term === null)
|
|
420
|
+
return null;
|
|
421
|
+
if (!cursor.expectSymbol(":"))
|
|
422
|
+
return null;
|
|
423
|
+
const token = cursor.expectMath();
|
|
424
|
+
if (token === null)
|
|
425
|
+
return null;
|
|
426
|
+
const precKeyword = cursor.expectIdentifier();
|
|
427
|
+
if (precKeyword === null)
|
|
428
|
+
return null;
|
|
429
|
+
if (precKeyword !== "prec") {
|
|
430
|
+
cursor.report("expected_token", { expected: '"prec"', found: precKeyword }, cursor.hereSpan());
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
const prec = parsePrecedence(cursor);
|
|
434
|
+
if (prec === null)
|
|
435
|
+
return null;
|
|
436
|
+
return finish({
|
|
437
|
+
...base(cursor.hereSpan()),
|
|
438
|
+
kind: "simple-notation",
|
|
439
|
+
fixity: keyword.text,
|
|
440
|
+
term,
|
|
441
|
+
token: token.text.trim(),
|
|
442
|
+
prec,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
case "coercion": {
|
|
446
|
+
const name = cursor.expectIdentifier();
|
|
447
|
+
if (name === null)
|
|
448
|
+
return null;
|
|
449
|
+
if (!cursor.expectSymbol(":"))
|
|
450
|
+
return null;
|
|
451
|
+
const from = cursor.expectIdentifier();
|
|
452
|
+
if (from === null)
|
|
453
|
+
return null;
|
|
454
|
+
if (!cursor.expectSymbol(">"))
|
|
455
|
+
return null;
|
|
456
|
+
const to = cursor.expectIdentifier();
|
|
457
|
+
if (to === null)
|
|
458
|
+
return null;
|
|
459
|
+
return finish({
|
|
460
|
+
...base(cursor.hereSpan()),
|
|
461
|
+
kind: "coercion",
|
|
462
|
+
name,
|
|
463
|
+
from,
|
|
464
|
+
to,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
case "notation": {
|
|
468
|
+
const term = cursor.expectIdentifier();
|
|
469
|
+
if (term === null)
|
|
470
|
+
return null;
|
|
471
|
+
const binders = parseBinders(cursor, {
|
|
472
|
+
allowFormula: false,
|
|
473
|
+
allowDummy: false,
|
|
474
|
+
});
|
|
475
|
+
if (binders === null)
|
|
476
|
+
return null;
|
|
477
|
+
if (!cursor.expectSymbol(":"))
|
|
478
|
+
return null;
|
|
479
|
+
const returnType = parseTypeRef(cursor);
|
|
480
|
+
if (returnType === null)
|
|
481
|
+
return null;
|
|
482
|
+
if (!cursor.expectSymbol("="))
|
|
483
|
+
return null;
|
|
484
|
+
const literals = [];
|
|
485
|
+
for (;;) {
|
|
486
|
+
const token = cursor.peek();
|
|
487
|
+
if (token === null) {
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
if (token.kind === "symbol" && token.text === "(") {
|
|
491
|
+
cursor.next();
|
|
492
|
+
const constant = cursor.expectMath();
|
|
493
|
+
if (constant === null)
|
|
494
|
+
return null;
|
|
495
|
+
if (!cursor.expectSymbol(":"))
|
|
496
|
+
return null;
|
|
497
|
+
const prec = parsePrecedence(cursor);
|
|
498
|
+
if (prec === null)
|
|
499
|
+
return null;
|
|
500
|
+
if (!cursor.expectSymbol(")"))
|
|
501
|
+
return null;
|
|
502
|
+
literals.push({
|
|
503
|
+
kind: "constant",
|
|
504
|
+
prec,
|
|
505
|
+
token: constant.text.trim(),
|
|
506
|
+
span: constant.span,
|
|
507
|
+
});
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (token.kind === "identifier") {
|
|
511
|
+
cursor.next();
|
|
512
|
+
literals.push({
|
|
513
|
+
kind: "variable",
|
|
514
|
+
name: token.text,
|
|
515
|
+
span: token.span,
|
|
516
|
+
});
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
const first = literals[0];
|
|
522
|
+
if (first === undefined || first.kind !== "constant") {
|
|
523
|
+
cursor.report("notation_leading_variable", {}, first?.span ?? cursor.hereSpan());
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
return finish({
|
|
527
|
+
...base(cursor.hereSpan()),
|
|
528
|
+
kind: "notation",
|
|
529
|
+
term,
|
|
530
|
+
binders,
|
|
531
|
+
returnType,
|
|
532
|
+
literals,
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
default:
|
|
536
|
+
cursor.report("unknown_statement", { keyword: keyword.text }, keyword.span);
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
export function parseStatements(source) {
|
|
541
|
+
const lexed = lexFile(source);
|
|
542
|
+
const cursor = new Cursor(lexed.tokens, source.length);
|
|
543
|
+
for (const error of lexed.errors) {
|
|
544
|
+
cursor.report(`lex_${error.id}`, {}, { start: error.position, end: error.position + 1 });
|
|
545
|
+
}
|
|
546
|
+
const statements = [];
|
|
547
|
+
let annotations = [];
|
|
548
|
+
while (!cursor.atEnd()) {
|
|
549
|
+
const token = cursor.next();
|
|
550
|
+
if (token === null) {
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
if (token.kind === "annotation") {
|
|
554
|
+
annotations.push({ span: token.span, text: token.text });
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
if (token.kind !== "identifier") {
|
|
558
|
+
cursor.report("expected_token", { expected: "a statement keyword", found: token.text }, token.span);
|
|
559
|
+
cursor.synchronize();
|
|
560
|
+
annotations = [];
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const statement = parseStatement(cursor, token, {
|
|
564
|
+
annotations,
|
|
565
|
+
start: annotations[0]?.span ?? token.span,
|
|
566
|
+
});
|
|
567
|
+
if (statement === null) {
|
|
568
|
+
cursor.synchronize();
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
statements.push(statement);
|
|
572
|
+
}
|
|
573
|
+
annotations = [];
|
|
574
|
+
}
|
|
575
|
+
return { statements, diagnostics: cursor.diagnostics };
|
|
576
|
+
}
|
|
577
|
+
//# sourceMappingURL=statements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"statements.js","sour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S,KAAK,IAAI,EAAE,CAAC;YACvB,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC7B,CAAC;QAED,WAAW,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC;AACzD,CAAC","sourcesContent":["/**\n * The MM0 statement parser: file tokens in, statements out.\n *\n * The grammar is the upstream one (mm0.md), in the subset Aufbau reads —\n * summarized in the manual's appendix-grammars.md. Math strings are carried\n * through opaque: scanning their contents is the surface layer's job, and an\n * axiom's body never needs it at all.\n *\n * On a malformed statement the parser records one diagnostic and skips to\n * the next `;`, so a spec author sees every broken statement at once.\n */\n\nimport { type Diagnostic, diagnostic, type Span } from \"../diagnostics.js\";\nimport { type FileToken, lexFile } from \"./lex.js\";\n\n/** A math string, with the span of the whole `$ … $` lexeme. */\nexport interface MathString {\n readonly span: Span;\n readonly text: string;\n}\n\n/** An annotation line's payload, e.g. `@acui seq_assoc _ snil _`. */\nexport interface Annotation {\n readonly span: Span;\n readonly text: string;\n}\n\n/** A sort name applied to the bound variables the value may depend on. */\nexport interface TypeRef {\n readonly dependencies: readonly string[];\n readonly sort: string;\n readonly span: Span;\n}\n\nexport interface Binder {\n /** `{x: s}` binders bind; `(a: s)` binders are regular. */\n readonly binds: boolean;\n /** True for a `.`-prefixed dummy on a `def`. */\n readonly dummy: boolean;\n readonly name: string;\n readonly span: Span;\n /** A hypothesis binder `(h: $ … $)` carries a formula instead. */\n readonly type: TypeRef | MathString;\n}\n\nexport type Precedence = number | \"max\";\n\n/** One slot of a general notation: a constant literal or a variable. */\nexport type NotationLiteral =\n | {\n readonly kind: \"constant\";\n readonly prec: Precedence;\n readonly token: string;\n readonly span: Span;\n }\n | { readonly kind: \"variable\"; readonly name: string; readonly span: Span };\n\ninterface StatementBase {\n readonly annotations: readonly Annotation[];\n readonly span: Span;\n}\n\nexport interface SortStatement extends StatementBase {\n readonly kind: \"sort\";\n readonly modifiers: readonly string[];\n readonly name: string;\n}\n\nexport interface TermStatement extends StatementBase {\n readonly kind: \"term\";\n readonly name: string;\n readonly binders: readonly Binder[];\n /** The `:`-side arrow chain; the last entry is the return type. */\n readonly returnChain: readonly TypeRef[];\n}\n\nexport interface DefStatement extends StatementBase {\n readonly kind: \"def\";\n readonly name: string;\n readonly binders: readonly Binder[];\n readonly returnType: TypeRef;\n /** Absent on a bodyless def (definiens supplied by the proof file). */\n readonly definiens: MathString | null;\n}\n\nexport interface AssertStatement extends StatementBase {\n readonly kind: \"axiom\" | \"theorem\";\n readonly name: string;\n readonly binders: readonly Binder[];\n /** The `>`-chain: hypotheses (types or formulas), then the conclusion. */\n readonly hypotheses: readonly (TypeRef | MathString)[];\n readonly conclusion: MathString;\n}\n\nexport interface DelimiterStatement extends StatementBase {\n readonly kind: \"delimiter\";\n /** The one-list form fills both sides with the same characters. */\n readonly left: readonly string[];\n readonly right: readonly string[];\n}\n\nexport interface SimpleNotationStatement extends StatementBase {\n readonly kind: \"simple-notation\";\n readonly fixity: \"infixl\" | \"infixr\" | \"prefix\";\n readonly term: string;\n readonly token: string;\n readonly prec: Precedence;\n}\n\nexport interface CoercionStatement extends StatementBase {\n readonly kind: \"coercion\";\n readonly name: string;\n readonly from: string;\n readonly to: string;\n}\n\nexport interface GenNotationStatement extends StatementBase {\n readonly kind: \"notation\";\n readonly term: string;\n readonly binders: readonly Binder[];\n readonly returnType: TypeRef;\n readonly literals: readonly NotationLiteral[];\n}\n\nexport type Statement =\n | AssertStatement\n | CoercionStatement\n | DefStatement\n | DelimiterStatement\n | GenNotationStatement\n | SimpleNotationStatement\n | SortStatement\n | TermStatement;\n\nexport interface StatementParse {\n readonly statements: readonly Statement[];\n readonly diagnostics: readonly Diagnostic[];\n}\n\nconst SORT_MODIFIERS = new Set([\"pure\", \"strict\", \"provable\", \"free\"]);\n\nconst TEMPLATES: Record<string, string> = {\n lex_unexpected_character: \"unexpected character\",\n lex_unterminated_math: \"unterminated math string\",\n expected_token: \"expected {expected} but found {found}\",\n unknown_statement: \"unknown statement keyword {keyword}\",\n bad_precedence: \"expected a precedence (a number or max)\",\n empty_delimiter: \"a delimiter math string must list characters\",\n multibyte_delimiter:\n \"delimiter {token} is not a single byte; the engine cannot split on it\",\n notation_leading_variable:\n \"the first literal of a notation must be a constant\",\n};\n\nclass Cursor {\n readonly diagnostics: Diagnostic[] = [];\n private position = 0;\n\n constructor(\n private readonly tokens: readonly FileToken[],\n private readonly sourceLength: number,\n ) {}\n\n peek(): FileToken | null {\n return this.tokens[this.position] ?? null;\n }\n\n next(): FileToken | null {\n const token = this.tokens[this.position] ?? null;\n if (token !== null) {\n this.position += 1;\n }\n return token;\n }\n\n atEnd(): boolean {\n return this.position >= this.tokens.length;\n }\n\n hereSpan(): Span {\n const token = this.peek();\n return token\n ? token.span\n : { start: this.sourceLength, end: this.sourceLength };\n }\n\n report(id: string, params: Record<string, string>, span: Span): void {\n const template = TEMPLATES[id] ?? id;\n this.diagnostics.push(diagnostic(id, template, params, span));\n }\n\n /** Consume a specific symbol, or report and return false. */\n expectSymbol(text: string): boolean {\n const token = this.peek();\n\n if (token !== null && token.kind === \"symbol\" && token.text === text) {\n this.next();\n return true;\n }\n\n this.report(\n \"expected_token\",\n { expected: `\"${text}\"`, found: token ? token.text : \"end of file\" },\n this.hereSpan(),\n );\n return false;\n }\n\n expectIdentifier(): string | null {\n const token = this.peek();\n\n if (token !== null && token.kind === \"identifier\") {\n this.next();\n return token.text;\n }\n\n this.report(\n \"expected_token\",\n {\n expected: \"an identifier\",\n found: token ? token.text : \"end of file\",\n },\n this.hereSpan(),\n );\n return null;\n }\n\n expectMath(): MathString | null {\n const token = this.peek();\n\n if (token !== null && token.kind === \"math\") {\n this.next();\n return { span: token.span, text: token.text };\n }\n\n this.report(\n \"expected_token\",\n {\n expected: \"a math string\",\n found: token ? token.text : \"end of file\",\n },\n this.hereSpan(),\n );\n return null;\n }\n\n /** Skip past the next `;`, for error recovery. */\n synchronize(): void {\n while (!this.atEnd()) {\n const token = this.next();\n if (token !== null && token.kind === \"symbol\" && token.text === \";\") {\n return;\n }\n }\n }\n}\n\n/** `identifier (identifier)*` — a sort applied to its dependencies. */\nfunction parseTypeRef(cursor: Cursor): TypeRef | null {\n const start = cursor.hereSpan();\n const sort = cursor.expectIdentifier();\n\n if (sort === null) {\n return null;\n }\n\n const dependencies: string[] = [];\n let end = start;\n\n for (;;) {\n const token = cursor.peek();\n if (token === null || token.kind !== \"identifier\") {\n break;\n }\n cursor.next();\n dependencies.push(token.text);\n end = token.span;\n }\n\n return { sort, dependencies, span: { start: start.start, end: end.end } };\n}\n\n/**\n * One binder group — `{x y: type}` or `(a b: type-or-formula)` — flattened\n * to one Binder per name. `allowFormula` admits hypothesis binders (assert\n * statements); `allowDummy` admits `.`-prefixed names (defs).\n */\nfunction parseBinderGroup(\n cursor: Cursor,\n options: { allowFormula: boolean; allowDummy: boolean },\n): Binder[] | null {\n const open = cursor.peek();\n\n if (open === null || open.kind !== \"symbol\") {\n return null;\n }\n\n const binds = open.text === \"{\";\n\n if (!binds && open.text !== \"(\") {\n return null;\n }\n\n cursor.next();\n const names: { name: string; dummy: boolean; span: Span }[] = [];\n\n for (;;) {\n const token = cursor.peek();\n\n if (token === null) {\n break;\n }\n\n if (token.kind === \"symbol\" && token.text === \".\") {\n if (!options.allowDummy) {\n break;\n }\n cursor.next();\n const name = cursor.expectIdentifier();\n if (name === null) {\n return null;\n }\n names.push({ name, dummy: true, span: token.span });\n continue;\n }\n\n if (token.kind === \"identifier\") {\n cursor.next();\n names.push({ name: token.text, dummy: false, span: token.span });\n continue;\n }\n\n // An anonymous binder position, `_`.\n if (token.kind === \"symbol\" && token.text === \"_\") {\n cursor.next();\n names.push({ name: \"_\", dummy: false, span: token.span });\n continue;\n }\n\n break;\n }\n\n if (!cursor.expectSymbol(\":\")) {\n return null;\n }\n\n let type: TypeRef | MathString | null;\n const typeToken = cursor.peek();\n\n if (\n options.allowFormula &&\n typeToken !== null &&\n typeToken.kind === \"math\"\n ) {\n cursor.next();\n type = { span: typeToken.span, text: typeToken.text };\n } else {\n type = parseTypeRef(cursor);\n }\n\n if (type === null) {\n return null;\n }\n\n if (!cursor.expectSymbol(binds ? \"}\" : \")\")) {\n return null;\n }\n\n return names.map((entry) => ({\n binds,\n dummy: entry.dummy,\n name: entry.name,\n span: entry.span,\n type: type as TypeRef | MathString,\n }));\n}\n\nfunction parseBinders(\n cursor: Cursor,\n options: { allowFormula: boolean; allowDummy: boolean },\n): Binder[] | null {\n const binders: Binder[] = [];\n\n for (;;) {\n const token = cursor.peek();\n\n if (\n token === null ||\n token.kind !== \"symbol\" ||\n (token.text !== \"(\" && token.text !== \"{\")\n ) {\n return binders;\n }\n\n const group = parseBinderGroup(cursor, options);\n\n if (group === null) {\n return null;\n }\n\n binders.push(...group);\n }\n}\n\nfunction parsePrecedence(cursor: Cursor): Precedence | null {\n const token = cursor.peek();\n\n if (token !== null && token.kind === \"number\") {\n cursor.next();\n return Number.parseInt(token.text, 10);\n }\n\n if (token !== null && token.kind === \"identifier\" && token.text === \"max\") {\n cursor.next();\n return \"max\";\n }\n\n cursor.report(\"bad_precedence\", {}, cursor.hereSpan());\n return null;\n}\n\n/** Split a delimiter math string into its whitespace-separated tokens. */\nfunction delimiterChars(cursor: Cursor, math: MathString): string[] | null {\n const parts = math.text.split(/\\s+/).filter((part) => part.length > 0);\n\n if (parts.length === 0) {\n cursor.report(\"empty_delimiter\", {}, math.span);\n return null;\n }\n\n for (const part of parts) {\n const points = [...part];\n const single =\n points.length === 1 && (points[0]?.codePointAt(0) ?? 0x80) <= 0x7f;\n\n if (!single) {\n cursor.report(\"multibyte_delimiter\", { token: part }, math.span);\n return null;\n }\n }\n\n return parts;\n}\n\ninterface Pending {\n readonly annotations: Annotation[];\n readonly start: Span;\n}\n\nfunction parseStatement(\n cursor: Cursor,\n keyword: FileToken,\n pending: Pending,\n): Statement | null {\n const base = (endSpan: Span): StatementBase => ({\n annotations: pending.annotations,\n span: { start: pending.start.start, end: endSpan.end },\n });\n\n const finish = <S extends Statement>(statement: S): S | null =>\n cursor.expectSymbol(\";\") ? statement : null;\n\n if (SORT_MODIFIERS.has(keyword.text) || keyword.text === \"sort\") {\n const modifiers: string[] = [];\n let cursorToken: FileToken | null = keyword;\n\n while (cursorToken !== null && SORT_MODIFIERS.has(cursorToken.text)) {\n modifiers.push(cursorToken.text);\n cursorToken = cursor.next();\n }\n\n if (cursorToken === null || cursorToken.text !== \"sort\") {\n cursor.report(\n \"expected_token\",\n {\n expected: '\"sort\"',\n found: cursorToken ? cursorToken.text : \"end of file\",\n },\n cursor.hereSpan(),\n );\n return null;\n }\n\n const name = cursor.expectIdentifier();\n\n if (name === null) {\n return null;\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"sort\",\n modifiers,\n name,\n });\n }\n\n switch (keyword.text) {\n case \"term\": {\n const name = cursor.expectIdentifier();\n if (name === null) return null;\n\n const binders = parseBinders(cursor, {\n allowFormula: false,\n allowDummy: false,\n });\n if (binders === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const returnChain: TypeRef[] = [];\n\n for (;;) {\n const type = parseTypeRef(cursor);\n if (type === null) return null;\n returnChain.push(type);\n\n const arrow = cursor.peek();\n if (arrow !== null && arrow.kind === \"symbol\" && arrow.text === \">\") {\n cursor.next();\n continue;\n }\n break;\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"term\",\n name,\n binders,\n returnChain,\n });\n }\n\n case \"def\": {\n const name = cursor.expectIdentifier();\n if (name === null) return null;\n\n const binders = parseBinders(cursor, {\n allowFormula: false,\n allowDummy: true,\n });\n if (binders === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const returnType = parseTypeRef(cursor);\n if (returnType === null) return null;\n\n let definiens: MathString | null = null;\n const eq = cursor.peek();\n\n if (eq !== null && eq.kind === \"symbol\" && eq.text === \"=\") {\n cursor.next();\n definiens = cursor.expectMath();\n if (definiens === null) return null;\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"def\",\n name,\n binders,\n returnType,\n definiens,\n });\n }\n\n case \"axiom\":\n case \"theorem\": {\n const name = cursor.expectIdentifier();\n if (name === null) return null;\n\n const binders = parseBinders(cursor, {\n allowFormula: true,\n allowDummy: false,\n });\n if (binders === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const chain: (TypeRef | MathString)[] = [];\n\n for (;;) {\n const token = cursor.peek();\n\n if (token !== null && token.kind === \"math\") {\n cursor.next();\n chain.push({ span: token.span, text: token.text });\n } else {\n const type = parseTypeRef(cursor);\n if (type === null) return null;\n chain.push(type);\n }\n\n const arrow = cursor.peek();\n if (arrow !== null && arrow.kind === \"symbol\" && arrow.text === \">\") {\n cursor.next();\n continue;\n }\n break;\n }\n\n const conclusion = chain[chain.length - 1];\n\n if (conclusion === undefined || !(\"text\" in conclusion)) {\n cursor.report(\n \"expected_token\",\n { expected: \"a concluding math string\", found: \";\" },\n cursor.hereSpan(),\n );\n return null;\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: keyword.text,\n name,\n binders,\n hypotheses: chain.slice(0, -1),\n conclusion,\n });\n }\n\n case \"delimiter\": {\n const first = cursor.expectMath();\n if (first === null) return null;\n\n const firstChars = delimiterChars(cursor, first);\n if (firstChars === null) return null;\n\n const maybeSecond = cursor.peek();\n\n if (maybeSecond !== null && maybeSecond.kind === \"math\") {\n cursor.next();\n const secondChars = delimiterChars(cursor, {\n span: maybeSecond.span,\n text: maybeSecond.text,\n });\n if (secondChars === null) return null;\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"delimiter\",\n left: firstChars,\n right: secondChars,\n });\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"delimiter\",\n left: firstChars,\n right: firstChars,\n });\n }\n\n case \"prefix\":\n case \"infixl\":\n case \"infixr\": {\n const term = cursor.expectIdentifier();\n if (term === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const token = cursor.expectMath();\n if (token === null) return null;\n\n const precKeyword = cursor.expectIdentifier();\n if (precKeyword === null) return null;\n\n if (precKeyword !== \"prec\") {\n cursor.report(\n \"expected_token\",\n { expected: '\"prec\"', found: precKeyword },\n cursor.hereSpan(),\n );\n return null;\n }\n\n const prec = parsePrecedence(cursor);\n if (prec === null) return null;\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"simple-notation\",\n fixity: keyword.text,\n term,\n token: token.text.trim(),\n prec,\n });\n }\n\n case \"coercion\": {\n const name = cursor.expectIdentifier();\n if (name === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const from = cursor.expectIdentifier();\n if (from === null) return null;\n if (!cursor.expectSymbol(\">\")) return null;\n\n const to = cursor.expectIdentifier();\n if (to === null) return null;\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"coercion\",\n name,\n from,\n to,\n });\n }\n\n case \"notation\": {\n const term = cursor.expectIdentifier();\n if (term === null) return null;\n\n const binders = parseBinders(cursor, {\n allowFormula: false,\n allowDummy: false,\n });\n if (binders === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n\n const returnType = parseTypeRef(cursor);\n if (returnType === null) return null;\n if (!cursor.expectSymbol(\"=\")) return null;\n\n const literals: NotationLiteral[] = [];\n\n for (;;) {\n const token = cursor.peek();\n\n if (token === null) {\n break;\n }\n\n if (token.kind === \"symbol\" && token.text === \"(\") {\n cursor.next();\n const constant = cursor.expectMath();\n if (constant === null) return null;\n if (!cursor.expectSymbol(\":\")) return null;\n const prec = parsePrecedence(cursor);\n if (prec === null) return null;\n if (!cursor.expectSymbol(\")\")) return null;\n\n literals.push({\n kind: \"constant\",\n prec,\n token: constant.text.trim(),\n span: constant.span,\n });\n continue;\n }\n\n if (token.kind === \"identifier\") {\n cursor.next();\n literals.push({\n kind: \"variable\",\n name: token.text,\n span: token.span,\n });\n continue;\n }\n\n break;\n }\n\n const first = literals[0];\n\n if (first === undefined || first.kind !== \"constant\") {\n cursor.report(\n \"notation_leading_variable\",\n {},\n first?.span ?? cursor.hereSpan(),\n );\n return null;\n }\n\n return finish({\n ...base(cursor.hereSpan()),\n kind: \"notation\",\n term,\n binders,\n returnType,\n literals,\n });\n }\n\n default:\n cursor.report(\n \"unknown_statement\",\n { keyword: keyword.text },\n keyword.span,\n );\n return null;\n }\n}\n\nexport function parseStatements(source: string): StatementParse {\n const lexed = lexFile(source);\n const cursor = new Cursor(lexed.tokens, source.length);\n\n for (const error of lexed.errors) {\n cursor.report(\n `lex_${error.id}`,\n {},\n { start: error.position, end: error.position + 1 },\n );\n }\n\n const statements: Statement[] = [];\n let annotations: Annotation[] = [];\n\n while (!cursor.atEnd()) {\n const token = cursor.next();\n\n if (token === null) {\n break;\n }\n\n if (token.kind === \"annotation\") {\n annotations.push({ span: token.span, text: token.text });\n continue;\n }\n\n if (token.kind !== \"identifier\") {\n cursor.report(\n \"expected_token\",\n { expected: \"a statement keyword\", found: token.text },\n token.span,\n );\n cursor.synchronize();\n annotations = [];\n continue;\n }\n\n const statement = parseStatement(cursor, token, {\n annotations,\n start: annotations[0]?.span ?? token.span,\n });\n\n if (statement === null) {\n cursor.synchronize();\n } else {\n statements.push(statement);\n }\n\n annotations = [];\n }\n\n return { statements, diagnostics: cursor.diagnostics };\n}\n"]}
|