@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
package/dist/parse.js
ADDED
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The surface parser: a faithful port of MM0's operator-precedence math
|
|
3
|
+
* parser (mm0.md, "the dynamic parser"), running over the character-level
|
|
4
|
+
* scanner instead of MM0's whitespace lexer, with exactly three declared
|
|
5
|
+
* extensions beyond the upstream grammar:
|
|
6
|
+
*
|
|
7
|
+
* 1. **Grouping pairs** — `expression(max) → open expression(0) close`
|
|
8
|
+
* for every declared bracket pair, close matching open;
|
|
9
|
+
* 2. **assoc-none levels** — a precedence in `@syntax assoc-none` refuses
|
|
10
|
+
* to chain regardless of its declared associativity (`P → Q → R` is an
|
|
11
|
+
* error, per forallx);
|
|
12
|
+
* 3. **Lints** — the closed set of post-parse checks
|
|
13
|
+
* (`parenthesize-binary-only`, `closed-sentences`).
|
|
14
|
+
*
|
|
15
|
+
* Two survivable deviations, both surface-side supersets: a lexicon name
|
|
16
|
+
* (nullary, elided-bare, or applied to parenthesized or juxtaposed
|
|
17
|
+
* arguments) counts as `expression(max)`, where MM0 puts constructor
|
|
18
|
+
* application at 1024; and a token that is both a notation and a lexicon
|
|
19
|
+
* name (Calgary's `A`) is disambiguated by backtracking, notation reading
|
|
20
|
+
* first.
|
|
21
|
+
*
|
|
22
|
+
* mm0.md's slot-precedence rules are used exactly: a prefix notation's
|
|
23
|
+
* intermediate arguments parse at max and its last at the notation's
|
|
24
|
+
* precedence; a general notation's slot parses at max before a variable, at
|
|
25
|
+
* `p+1` before a constant of precedence `p`, and at the head precedence
|
|
26
|
+
* when last. Coercions are inserted along the (unique) path in the coercion
|
|
27
|
+
* graph, per the spec.
|
|
28
|
+
*/
|
|
29
|
+
import { diagnostic } from "./diagnostics.js";
|
|
30
|
+
import { elaborate, remapSpan } from "./elab.js";
|
|
31
|
+
import { Scanner } from "./scan.js";
|
|
32
|
+
import { walkTerm } from "./term.js";
|
|
33
|
+
const TEMPLATES = {
|
|
34
|
+
unrecognized_character: "Unexpected character “{character}”.",
|
|
35
|
+
expected_formula: "Expected a formula.",
|
|
36
|
+
expected_formula_found: "Expected a formula but found “{token}”.",
|
|
37
|
+
unexpected_token: "Unexpected “{token}”.",
|
|
38
|
+
expected_variable: "Expected a variable after the quantifier.",
|
|
39
|
+
expected_bracket: "Expected “{bracket}”.",
|
|
40
|
+
chain_refused: "“{operator}” cannot be chained; add parentheses to group it.",
|
|
41
|
+
group_binary_only: "Parentheses may only enclose a sentence joined by a two-place connective.",
|
|
42
|
+
free_variable: "“{name}” is a free variable; every formula must be a sentence.",
|
|
43
|
+
sort_mismatch: "This has sort {actual} where {expected} is needed.",
|
|
44
|
+
term_not_sentence: "This is a {actual}, not a complete sentence.",
|
|
45
|
+
needs_parentheses: "“{token}” binds too loosely here; parenthesize it.",
|
|
46
|
+
spec_notation_incomplete: "The notation for {term} does not cover all of its arguments.",
|
|
47
|
+
};
|
|
48
|
+
function precNum(prec) {
|
|
49
|
+
return prec === "max" ? Number.POSITIVE_INFINITY : prec;
|
|
50
|
+
}
|
|
51
|
+
function binderSort(binder) {
|
|
52
|
+
if (binder === undefined || !("sort" in binder.type)) {
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
return binder.type.sort;
|
|
56
|
+
}
|
|
57
|
+
/** Everything derived from a Spec that parsing needs, built once. */
|
|
58
|
+
export class SurfaceLanguage {
|
|
59
|
+
spec;
|
|
60
|
+
scanner;
|
|
61
|
+
heads = new Map();
|
|
62
|
+
infixes = new Map();
|
|
63
|
+
closeOf = new Map();
|
|
64
|
+
closers = new Set();
|
|
65
|
+
/** Per sort, its `@syntax juxtaposed` combiner — adjacency's meaning. */
|
|
66
|
+
juxtaposedOf = new Map();
|
|
67
|
+
/** Per sort, its `@syntax elided` term — the unwritten argument. */
|
|
68
|
+
elidedOf = new Map();
|
|
69
|
+
/** Sorts some quantifier binds — whose variables *can* be captured. */
|
|
70
|
+
bindableSorts = new Set();
|
|
71
|
+
provableSort;
|
|
72
|
+
/** Per term, its last-declared notation — the canonical spelling. */
|
|
73
|
+
canonical = new Map();
|
|
74
|
+
coercionNames = new Set();
|
|
75
|
+
coercionPaths = new Map();
|
|
76
|
+
constructor(spec) {
|
|
77
|
+
this.spec = spec;
|
|
78
|
+
this.scanner = new Scanner(spec);
|
|
79
|
+
for (const notation of spec.notations) {
|
|
80
|
+
const info = spec.terms.get(notation.term);
|
|
81
|
+
if (info === undefined) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (notation.form === "simple") {
|
|
85
|
+
if (notation.fixity === "prefix") {
|
|
86
|
+
this.heads.set(notation.token, {
|
|
87
|
+
kind: "prefix",
|
|
88
|
+
info,
|
|
89
|
+
prec: notation.prec,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else if (typeof notation.prec === "number") {
|
|
93
|
+
this.infixes.set(notation.token, {
|
|
94
|
+
kind: "infix",
|
|
95
|
+
info,
|
|
96
|
+
prec: notation.prec,
|
|
97
|
+
fixity: notation.fixity,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const head = notation.literals[0];
|
|
103
|
+
if (head === undefined || head.kind !== "constant") {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
// mm0.md's P(lits, q): a slot parses at max before a variable, at
|
|
107
|
+
// p+1 before a constant of precedence p, and at q (the head
|
|
108
|
+
// precedence) when it is the trailing slot.
|
|
109
|
+
const parts = [];
|
|
110
|
+
const rest = notation.literals.slice(1);
|
|
111
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
112
|
+
const literal = rest[i];
|
|
113
|
+
if (literal === undefined) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (literal.kind === "constant") {
|
|
117
|
+
parts.push({ kind: "constant", token: literal.token });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const index = notation.binders.findIndex((binder) => binder.name === literal.name);
|
|
121
|
+
const binder = notation.binders[index];
|
|
122
|
+
if (binder === undefined) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const following = rest[i + 1];
|
|
126
|
+
const prec = following === undefined
|
|
127
|
+
? precNum(head.prec)
|
|
128
|
+
: following.kind === "constant"
|
|
129
|
+
? precNum(following.prec) + 1
|
|
130
|
+
: Number.POSITIVE_INFINITY;
|
|
131
|
+
parts.push({ kind: "slot", slot: { binder, index, prec } });
|
|
132
|
+
}
|
|
133
|
+
this.heads.set(head.token, { kind: "general", info, parts });
|
|
134
|
+
}
|
|
135
|
+
for (const [open, close] of spec.groupingPairs) {
|
|
136
|
+
this.closeOf.set(open, close);
|
|
137
|
+
this.closers.add(close);
|
|
138
|
+
}
|
|
139
|
+
for (const term of spec.terms.values()) {
|
|
140
|
+
if (term.juxtaposed) {
|
|
141
|
+
this.juxtaposedOf.set(term.returnSort, term);
|
|
142
|
+
}
|
|
143
|
+
if (term.elided) {
|
|
144
|
+
this.elidedOf.set(term.returnSort, term);
|
|
145
|
+
}
|
|
146
|
+
for (const binder of term.binders) {
|
|
147
|
+
if (binder.binds) {
|
|
148
|
+
this.bindableSorts.add(binderSort(binder));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const notation of spec.notations) {
|
|
153
|
+
this.canonical.set(notation.term, notation);
|
|
154
|
+
}
|
|
155
|
+
for (const coercion of spec.coercions) {
|
|
156
|
+
this.coercionNames.add(coercion.name);
|
|
157
|
+
}
|
|
158
|
+
this.provableSort =
|
|
159
|
+
[...spec.sorts.values()].find((sort) => sort.modifiers.includes("provable"))?.name ?? null;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A sentential connective: an infix constructor joining two things of
|
|
163
|
+
* the provable sort. This is the class forallx's bracket convention and
|
|
164
|
+
* the display printer's spacing both key on — `∧` is one, `=` over
|
|
165
|
+
* terms is not.
|
|
166
|
+
*/
|
|
167
|
+
isConnective(term) {
|
|
168
|
+
const info = this.spec.terms.get(term);
|
|
169
|
+
const provable = this.provableSort;
|
|
170
|
+
const notation = this.canonical.get(term);
|
|
171
|
+
return (provable !== null &&
|
|
172
|
+
info !== undefined &&
|
|
173
|
+
notation !== undefined &&
|
|
174
|
+
notation.form === "simple" &&
|
|
175
|
+
notation.fixity !== "prefix" &&
|
|
176
|
+
info.binders.length === 2 &&
|
|
177
|
+
info.binders.every((binder) => !binder.binds &&
|
|
178
|
+
"sort" in binder.type &&
|
|
179
|
+
binder.type.sort === provable));
|
|
180
|
+
}
|
|
181
|
+
/** The coercion path from one sort to another; unique per mm0.md. */
|
|
182
|
+
coerce(from, to) {
|
|
183
|
+
if (from === to) {
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
const key = `${from}>${to}`;
|
|
187
|
+
const cached = this.coercionPaths.get(key);
|
|
188
|
+
if (cached !== undefined) {
|
|
189
|
+
return cached;
|
|
190
|
+
}
|
|
191
|
+
const previous = new Map();
|
|
192
|
+
const queue = [from];
|
|
193
|
+
while (queue.length > 0) {
|
|
194
|
+
const sort = queue.shift();
|
|
195
|
+
if (sort === undefined || sort === to) {
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
for (const coercion of this.spec.coercions) {
|
|
199
|
+
if (coercion.from === sort && !previous.has(coercion.to)) {
|
|
200
|
+
previous.set(coercion.to, { sort, name: coercion.name });
|
|
201
|
+
queue.push(coercion.to);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
let path = null;
|
|
206
|
+
if (previous.has(to)) {
|
|
207
|
+
path = [];
|
|
208
|
+
let at = to;
|
|
209
|
+
while (at !== from) {
|
|
210
|
+
const step = previous.get(at);
|
|
211
|
+
if (step === undefined) {
|
|
212
|
+
path = null;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
path.unshift(step.name);
|
|
216
|
+
at = step.sort;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
this.coercionPaths.set(key, path);
|
|
220
|
+
return path;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Parse surface text. `lints: false` skips the spec's refusal
|
|
224
|
+
* conventions (bracket discipline, chain refusal, closed sentences) —
|
|
225
|
+
* for reading text that is grammatical but not surface-idiomatic, such
|
|
226
|
+
* as this library's own engine-mode output.
|
|
227
|
+
*/
|
|
228
|
+
parse(text, options = {}) {
|
|
229
|
+
const lints = options.lints ?? true;
|
|
230
|
+
if (this.spec.elabRules.length === 0) {
|
|
231
|
+
return new Parse(this, text, lints).run();
|
|
232
|
+
}
|
|
233
|
+
// Elaborate first, then map every span in the outcome back through
|
|
234
|
+
// the origin map, so nothing downstream sees elaborated offsets.
|
|
235
|
+
const elaborated = elaborate(this, text);
|
|
236
|
+
const result = new Parse(this, elaborated.text, lints).run();
|
|
237
|
+
const diagnostics = result.diagnostics.map((diag) => ({
|
|
238
|
+
...diag,
|
|
239
|
+
span: remapSpan(elaborated.origin, diag.span),
|
|
240
|
+
}));
|
|
241
|
+
if (!result.ok) {
|
|
242
|
+
return { ok: false, diagnostics };
|
|
243
|
+
}
|
|
244
|
+
const remapTerm = (term) => term.kind === "variable"
|
|
245
|
+
? { ...term, span: remapSpan(elaborated.origin, term.span) }
|
|
246
|
+
: {
|
|
247
|
+
...term,
|
|
248
|
+
span: remapSpan(elaborated.origin, term.span),
|
|
249
|
+
args: term.args.map(remapTerm),
|
|
250
|
+
};
|
|
251
|
+
return { ok: true, term: remapTerm(result.term), diagnostics };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
class Parse {
|
|
255
|
+
lang;
|
|
256
|
+
text;
|
|
257
|
+
lintsEnabled;
|
|
258
|
+
position = 0;
|
|
259
|
+
diagnostics = [];
|
|
260
|
+
/**
|
|
261
|
+
* The deepest-reaching diagnostic a failed trial produced. When a
|
|
262
|
+
* fallback reading succeeds but leaves trailing input short of where a
|
|
263
|
+
* higher-priority reading got to, that earlier, more specific error is
|
|
264
|
+
* the one the writer should see — `F(a` is an unclosed argument list,
|
|
265
|
+
* not a stray `(` after the sentence letter F.
|
|
266
|
+
*/
|
|
267
|
+
bestTrialFailure = null;
|
|
268
|
+
constructor(lang, text, lintsEnabled) {
|
|
269
|
+
this.lang = lang;
|
|
270
|
+
this.text = text;
|
|
271
|
+
this.lintsEnabled = lintsEnabled;
|
|
272
|
+
}
|
|
273
|
+
run() {
|
|
274
|
+
const parsed = this.parseExpr(0);
|
|
275
|
+
if (parsed !== null) {
|
|
276
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
277
|
+
if (!point.atEnd) {
|
|
278
|
+
const trial = this.bestTrialFailure;
|
|
279
|
+
if (trial !== null && trial.end > point.start) {
|
|
280
|
+
this.diagnostics.push(trial.diag);
|
|
281
|
+
}
|
|
282
|
+
else if (point.readings.length === 0) {
|
|
283
|
+
this.report("unrecognized_character", { character: this.text[point.start] ?? "" }, { start: point.start, end: point.start + 1 });
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
const reading = point.readings[0];
|
|
287
|
+
const token = reading === undefined
|
|
288
|
+
? (this.text[point.start] ?? "")
|
|
289
|
+
: reading.kind === "token"
|
|
290
|
+
? reading.token
|
|
291
|
+
: reading.name;
|
|
292
|
+
this.report("unexpected_token", { token }, {
|
|
293
|
+
start: point.start,
|
|
294
|
+
end: point.start + (reading?.length ?? 1),
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return { ok: false, diagnostics: this.diagnostics };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (parsed === null) {
|
|
301
|
+
if (this.diagnostics.length === 0) {
|
|
302
|
+
this.report("expected_formula", {}, { start: 0, end: 0 });
|
|
303
|
+
}
|
|
304
|
+
return { ok: false, diagnostics: this.diagnostics };
|
|
305
|
+
}
|
|
306
|
+
let term = parsed;
|
|
307
|
+
const provable = this.lang.provableSort;
|
|
308
|
+
if (provable !== null && term.sort !== provable) {
|
|
309
|
+
const wrapped = this.coerceTerm(term, provable);
|
|
310
|
+
if (wrapped === null) {
|
|
311
|
+
this.report("term_not_sentence", { actual: term.sort }, term.span);
|
|
312
|
+
return { ok: false, diagnostics: this.diagnostics };
|
|
313
|
+
}
|
|
314
|
+
term = wrapped;
|
|
315
|
+
}
|
|
316
|
+
if (this.lintsEnabled) {
|
|
317
|
+
this.lint(term);
|
|
318
|
+
}
|
|
319
|
+
if (this.diagnostics.length > 0) {
|
|
320
|
+
return { ok: false, diagnostics: this.diagnostics };
|
|
321
|
+
}
|
|
322
|
+
return { ok: true, term, diagnostics: this.diagnostics };
|
|
323
|
+
}
|
|
324
|
+
// ── plumbing ──────────────────────────────────────────────────────────
|
|
325
|
+
report(id, params, span) {
|
|
326
|
+
this.diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span));
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
/** Run a trial parse; on failure, rewind input and drop its diagnostics. */
|
|
330
|
+
attempt(body) {
|
|
331
|
+
const position = this.position;
|
|
332
|
+
const kept = this.diagnostics.length;
|
|
333
|
+
const result = body();
|
|
334
|
+
if (result === null) {
|
|
335
|
+
for (const diag of this.diagnostics.slice(kept)) {
|
|
336
|
+
if (diag.span.end > (this.bestTrialFailure?.end ?? -1)) {
|
|
337
|
+
this.bestTrialFailure = { end: diag.span.end, diag };
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
this.position = position;
|
|
341
|
+
this.diagnostics.length = kept;
|
|
342
|
+
}
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
coerceTerm(term, target) {
|
|
346
|
+
const path = this.lang.coerce(term.sort, target);
|
|
347
|
+
if (path === null) {
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
let wrapped = term;
|
|
351
|
+
for (const name of path) {
|
|
352
|
+
const info = this.lang.spec.terms.get(name);
|
|
353
|
+
wrapped = {
|
|
354
|
+
kind: "app",
|
|
355
|
+
term: name,
|
|
356
|
+
args: [wrapped],
|
|
357
|
+
sort: info?.returnSort ?? target,
|
|
358
|
+
span: wrapped.span,
|
|
359
|
+
grouped: false,
|
|
360
|
+
fixity: null,
|
|
361
|
+
prec: null,
|
|
362
|
+
token: null,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
return wrapped;
|
|
366
|
+
}
|
|
367
|
+
coerceOrReport(term, target) {
|
|
368
|
+
const wrapped = this.coerceTerm(term, target);
|
|
369
|
+
if (wrapped === null) {
|
|
370
|
+
return this.report("sort_mismatch", { actual: term.sort, expected: target }, term.span);
|
|
371
|
+
}
|
|
372
|
+
return wrapped;
|
|
373
|
+
}
|
|
374
|
+
// ── the precedence parser ─────────────────────────────────────────────
|
|
375
|
+
parseExpr(min) {
|
|
376
|
+
let left = this.parsePrimary(min);
|
|
377
|
+
if (left === null) {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
for (;;) {
|
|
381
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
382
|
+
if (point.atEnd) {
|
|
383
|
+
return left;
|
|
384
|
+
}
|
|
385
|
+
const reading = point.readings.find((r) => r.kind === "token");
|
|
386
|
+
if (reading === undefined || reading.kind !== "token") {
|
|
387
|
+
return left;
|
|
388
|
+
}
|
|
389
|
+
const entry = this.lang.infixes.get(reading.token);
|
|
390
|
+
if (entry === undefined || entry.prec < min) {
|
|
391
|
+
return left;
|
|
392
|
+
}
|
|
393
|
+
this.position = point.start + reading.length;
|
|
394
|
+
const right = this.parseExpr(entry.fixity === "infixl" ? entry.prec + 1 : entry.prec);
|
|
395
|
+
if (right === null) {
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
const binders = entry.info.binders;
|
|
399
|
+
const leftSort = binderSort(binders[0]);
|
|
400
|
+
const rightSort = binderSort(binders[1]);
|
|
401
|
+
const coercedLeft = this.coerceOrReport(left, leftSort);
|
|
402
|
+
const coercedRight = this.coerceOrReport(right, rightSort);
|
|
403
|
+
if (coercedLeft === null || coercedRight === null) {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
left = {
|
|
407
|
+
kind: "app",
|
|
408
|
+
term: entry.info.name,
|
|
409
|
+
args: [coercedLeft, coercedRight],
|
|
410
|
+
sort: entry.info.returnSort,
|
|
411
|
+
span: { start: left.span.start, end: right.span.end },
|
|
412
|
+
grouped: false,
|
|
413
|
+
fixity: entry.fixity,
|
|
414
|
+
prec: entry.prec,
|
|
415
|
+
token: reading.token,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
parsePrimary(min) {
|
|
420
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
421
|
+
if (point.atEnd) {
|
|
422
|
+
return this.report("expected_formula", {}, {
|
|
423
|
+
start: point.start,
|
|
424
|
+
end: point.start,
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
if (point.readings.length === 0) {
|
|
428
|
+
return this.report("unrecognized_character", { character: this.text[point.start] ?? "" }, { start: point.start, end: point.start + 1 });
|
|
429
|
+
}
|
|
430
|
+
// Try each reading with backtracking; if none succeeds, re-run the
|
|
431
|
+
// first viable one so its diagnostics — the most specific available —
|
|
432
|
+
// are the ones the writer sees.
|
|
433
|
+
const viable = point.readings.filter((reading) => this.isViablePrimary(reading));
|
|
434
|
+
for (const reading of viable) {
|
|
435
|
+
const result = this.attempt(() => this.parsePrimaryReading(point.start, reading, min));
|
|
436
|
+
if (result !== null) {
|
|
437
|
+
return result;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
const first = viable[0];
|
|
441
|
+
if (first !== undefined) {
|
|
442
|
+
return this.parsePrimaryReading(point.start, first, min);
|
|
443
|
+
}
|
|
444
|
+
const reading = point.readings[0];
|
|
445
|
+
const token = reading?.kind === "token" ? reading.token : (reading?.name ?? "");
|
|
446
|
+
return this.report("expected_formula_found", { token }, {
|
|
447
|
+
start: point.start,
|
|
448
|
+
end: point.start + (reading?.length ?? 1),
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
isViablePrimary(reading) {
|
|
452
|
+
if (reading.kind === "name") {
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
return (this.lang.closeOf.has(reading.token) ||
|
|
456
|
+
this.lang.heads.has(reading.token));
|
|
457
|
+
}
|
|
458
|
+
parsePrimaryReading(start, reading, min) {
|
|
459
|
+
if (reading.kind === "name") {
|
|
460
|
+
return this.parseName(start, reading);
|
|
461
|
+
}
|
|
462
|
+
const close = this.lang.closeOf.get(reading.token);
|
|
463
|
+
if (close !== undefined) {
|
|
464
|
+
return this.parseGroup(start, close, reading.length);
|
|
465
|
+
}
|
|
466
|
+
const head = this.lang.heads.get(reading.token);
|
|
467
|
+
if (head === undefined) {
|
|
468
|
+
return this.report("expected_formula_found", { token: reading.token }, {
|
|
469
|
+
start,
|
|
470
|
+
end: start + reading.length,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
this.position = start + reading.length;
|
|
474
|
+
return head.kind === "prefix"
|
|
475
|
+
? this.parsePrefix(start, reading.token, head, min)
|
|
476
|
+
: this.parseGeneral(start, reading.token, head, min);
|
|
477
|
+
}
|
|
478
|
+
parseGroup(start, close, openLength) {
|
|
479
|
+
this.position = start + openLength;
|
|
480
|
+
const inner = this.parseExpr(0);
|
|
481
|
+
if (inner === null) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
485
|
+
const reading = point.readings.find((r) => r.kind === "token" && r.token === close);
|
|
486
|
+
if (reading === undefined) {
|
|
487
|
+
return this.report("expected_bracket", { bracket: close }, {
|
|
488
|
+
start: point.start,
|
|
489
|
+
end: point.start + 1,
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
this.position = point.start + reading.length;
|
|
493
|
+
return {
|
|
494
|
+
...inner,
|
|
495
|
+
grouped: true,
|
|
496
|
+
span: { start, end: this.position },
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
parsePrefix(start, token, entry, min) {
|
|
500
|
+
if (precNum(entry.prec) < min) {
|
|
501
|
+
return this.report("needs_parentheses", { token }, {
|
|
502
|
+
start,
|
|
503
|
+
end: this.position,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
const binders = entry.info.binders;
|
|
507
|
+
const args = [];
|
|
508
|
+
for (let i = 0; i < binders.length; i += 1) {
|
|
509
|
+
const binder = binders[i];
|
|
510
|
+
if (binder === undefined) {
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (binder.binds) {
|
|
514
|
+
const variable = this.expectBoundVariable(binderSort(binder));
|
|
515
|
+
if (variable === null) {
|
|
516
|
+
return null;
|
|
517
|
+
}
|
|
518
|
+
args.push(variable);
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
// mm0.md: a prefix notation's intermediate arguments parse at max,
|
|
522
|
+
// its last at the notation's own precedence.
|
|
523
|
+
const slotPrec = i === binders.length - 1
|
|
524
|
+
? precNum(entry.prec)
|
|
525
|
+
: Number.POSITIVE_INFINITY;
|
|
526
|
+
const arg = this.parseExpr(slotPrec);
|
|
527
|
+
if (arg === null) {
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
const coerced = this.coerceOrReport(arg, binderSort(binder));
|
|
531
|
+
if (coerced === null) {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
args.push(coerced);
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
kind: "app",
|
|
538
|
+
term: entry.info.name,
|
|
539
|
+
args,
|
|
540
|
+
sort: entry.info.returnSort,
|
|
541
|
+
span: {
|
|
542
|
+
start,
|
|
543
|
+
end: args[args.length - 1]?.span.end ?? this.position,
|
|
544
|
+
},
|
|
545
|
+
grouped: false,
|
|
546
|
+
fixity: "prefix",
|
|
547
|
+
prec: precNum(entry.prec),
|
|
548
|
+
token,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
parseGeneral(start, token, entry, min) {
|
|
552
|
+
const headPrec = this.headPrecOf(token);
|
|
553
|
+
if (headPrec !== null && headPrec < min) {
|
|
554
|
+
return this.report("needs_parentheses", { token }, {
|
|
555
|
+
start,
|
|
556
|
+
end: this.position,
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
const byIndex = new Map();
|
|
560
|
+
for (const part of entry.parts) {
|
|
561
|
+
if (part.kind === "constant") {
|
|
562
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
563
|
+
const reading = point.readings.find((r) => r.kind === "token" && r.token === part.token);
|
|
564
|
+
if (reading === undefined) {
|
|
565
|
+
return this.report("expected_bracket", { bracket: part.token }, {
|
|
566
|
+
start: point.start,
|
|
567
|
+
end: point.start + 1,
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
this.position = point.start + reading.length;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const { binder, index, prec } = part.slot;
|
|
574
|
+
if (binder.binds) {
|
|
575
|
+
const variable = this.expectBoundVariable(binderSort(binder));
|
|
576
|
+
if (variable === null) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
byIndex.set(index, variable);
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
const arg = this.parseExpr(prec);
|
|
583
|
+
if (arg === null) {
|
|
584
|
+
return null;
|
|
585
|
+
}
|
|
586
|
+
const coerced = this.coerceOrReport(arg, binderSort(binder));
|
|
587
|
+
if (coerced === null) {
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
byIndex.set(index, coerced);
|
|
591
|
+
}
|
|
592
|
+
const args = [];
|
|
593
|
+
for (let i = 0; i < entry.info.binders.length; i += 1) {
|
|
594
|
+
const arg = byIndex.get(i);
|
|
595
|
+
if (arg === undefined) {
|
|
596
|
+
return this.report("spec_notation_incomplete", { term: entry.info.name }, { start, end: this.position });
|
|
597
|
+
}
|
|
598
|
+
args.push(arg);
|
|
599
|
+
}
|
|
600
|
+
return {
|
|
601
|
+
kind: "app",
|
|
602
|
+
term: entry.info.name,
|
|
603
|
+
args,
|
|
604
|
+
sort: entry.info.returnSort,
|
|
605
|
+
span: { start, end: args[args.length - 1]?.span.end ?? this.position },
|
|
606
|
+
grouped: false,
|
|
607
|
+
fixity: "general",
|
|
608
|
+
prec: headPrec,
|
|
609
|
+
token,
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
headPrecOf(token) {
|
|
613
|
+
for (const notation of this.lang.spec.notations) {
|
|
614
|
+
if (notation.form === "general") {
|
|
615
|
+
const head = notation.literals[0];
|
|
616
|
+
if (head?.kind === "constant" && head.token === token) {
|
|
617
|
+
return precNum(head.prec);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
expectBoundVariable(sort) {
|
|
624
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
625
|
+
const reading = point.readings.find((r) => r.kind === "name" && r.ref.kind === "var" && r.ref.sort === sort);
|
|
626
|
+
if (reading === undefined || reading.kind !== "name") {
|
|
627
|
+
return this.report("expected_variable", {}, {
|
|
628
|
+
start: point.start,
|
|
629
|
+
end: point.start + 1,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
this.position = point.start + reading.length;
|
|
633
|
+
return {
|
|
634
|
+
kind: "variable",
|
|
635
|
+
name: reading.name,
|
|
636
|
+
sort,
|
|
637
|
+
span: { start: point.start, end: this.position },
|
|
638
|
+
grouped: false,
|
|
639
|
+
binder: true,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* A lexicon name: a `@vars` token (a variable of its sort), or a
|
|
644
|
+
* declared term with no notation of its own — a sentence letter,
|
|
645
|
+
* predicate, function symbol, or constant. A term's arguments come
|
|
646
|
+
* parenthesized (application syntax, so the argument is not marked
|
|
647
|
+
* `grouped` and the binary-only lint does not apply inside; the
|
|
648
|
+
* canonical pair is always the first declared one), or juxtaposed when
|
|
649
|
+
* the argument sort has an `@syntax juxtaposed` combiner, or not at all
|
|
650
|
+
* when it has an `@syntax elided` term. Context settles the `A`-as-∀
|
|
651
|
+
* ambiguity exactly as in Carnap: the quantifier reading was tried
|
|
652
|
+
* first, and fell through to here only if it failed.
|
|
653
|
+
*/
|
|
654
|
+
parseName(start, reading) {
|
|
655
|
+
this.position = start + reading.length;
|
|
656
|
+
if (reading.ref.kind === "var") {
|
|
657
|
+
return {
|
|
658
|
+
kind: "variable",
|
|
659
|
+
name: reading.name,
|
|
660
|
+
sort: reading.ref.sort,
|
|
661
|
+
span: { start, end: this.position },
|
|
662
|
+
grouped: false,
|
|
663
|
+
binder: false,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const info = this.lang.spec.terms.get(reading.ref.term);
|
|
667
|
+
if (info === undefined) {
|
|
668
|
+
return this.report("expected_formula_found", { token: reading.name }, { start, end: start + reading.length });
|
|
669
|
+
}
|
|
670
|
+
const argBinders = info.binders.filter((binder) => !binder.binds);
|
|
671
|
+
const args = [];
|
|
672
|
+
for (const binder of argBinders) {
|
|
673
|
+
const target = binderSort(binder);
|
|
674
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
675
|
+
const open = this.lang.spec.groupingPairs[0]?.[0] ?? "(";
|
|
676
|
+
const close = this.lang.spec.groupingPairs[0]?.[1] ?? ")";
|
|
677
|
+
const openReading = point.readings.find((r) => r.kind === "token" && r.token === open);
|
|
678
|
+
if (openReading !== undefined) {
|
|
679
|
+
this.position = point.start + openReading.length;
|
|
680
|
+
const arg = this.parseExpr(0);
|
|
681
|
+
if (arg === null) {
|
|
682
|
+
return null;
|
|
683
|
+
}
|
|
684
|
+
const closePoint = this.lang.scanner.at(this.text, this.position);
|
|
685
|
+
const closeReading = closePoint.readings.find((r) => r.kind === "token" && r.token === close);
|
|
686
|
+
if (closeReading === undefined) {
|
|
687
|
+
return this.report("expected_bracket", { bracket: close }, {
|
|
688
|
+
start: closePoint.start,
|
|
689
|
+
end: closePoint.start + 1,
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
this.position = closePoint.start + closeReading.length;
|
|
693
|
+
const coerced = this.coerceOrReport(arg, target);
|
|
694
|
+
if (coerced === null) {
|
|
695
|
+
return null;
|
|
696
|
+
}
|
|
697
|
+
args.push(coerced);
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
// Juxtaposed gluing — `Fxy`, the pre-2019 forallx shape. Operands
|
|
701
|
+
// (self-delimiting single-token expressions) are consumed greedily
|
|
702
|
+
// while they coerce into the argument sort; several fold through
|
|
703
|
+
// the sort's declared combiner.
|
|
704
|
+
const combiner = argBinders.length === 1
|
|
705
|
+
? this.lang.juxtaposedOf.get(target)
|
|
706
|
+
: undefined;
|
|
707
|
+
if (combiner !== undefined) {
|
|
708
|
+
const leaves = [];
|
|
709
|
+
for (;;) {
|
|
710
|
+
const leaf = this.glueOperand(target);
|
|
711
|
+
if (leaf === null) {
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
leaves.push(leaf);
|
|
715
|
+
}
|
|
716
|
+
const first = leaves[0];
|
|
717
|
+
if (first !== undefined) {
|
|
718
|
+
let folded = first;
|
|
719
|
+
for (const leaf of leaves.slice(1)) {
|
|
720
|
+
folded = {
|
|
721
|
+
kind: "app",
|
|
722
|
+
term: combiner.name,
|
|
723
|
+
args: [folded, leaf],
|
|
724
|
+
sort: combiner.returnSort,
|
|
725
|
+
span: { start: folded.span.start, end: leaf.span.end },
|
|
726
|
+
grouped: false,
|
|
727
|
+
fixity: null,
|
|
728
|
+
prec: null,
|
|
729
|
+
token: null,
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
args.push(folded);
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
// No operand at all: fall through to elision, so a bare letter
|
|
736
|
+
// can still be a sentence letter in a juxtaposed dialect.
|
|
737
|
+
}
|
|
738
|
+
const elided = this.lang.elidedOf.get(target);
|
|
739
|
+
if (elided !== undefined) {
|
|
740
|
+
args.push({
|
|
741
|
+
kind: "app",
|
|
742
|
+
term: elided.name,
|
|
743
|
+
args: [],
|
|
744
|
+
sort: elided.returnSort,
|
|
745
|
+
span: { start, end: start + reading.length },
|
|
746
|
+
grouped: false,
|
|
747
|
+
fixity: null,
|
|
748
|
+
prec: null,
|
|
749
|
+
token: null,
|
|
750
|
+
});
|
|
751
|
+
continue;
|
|
752
|
+
}
|
|
753
|
+
return this.report("expected_formula_found", { token: reading.name }, { start, end: start + reading.length });
|
|
754
|
+
}
|
|
755
|
+
return {
|
|
756
|
+
kind: "app",
|
|
757
|
+
term: reading.name,
|
|
758
|
+
args,
|
|
759
|
+
sort: info.returnSort,
|
|
760
|
+
span: { start, end: this.position },
|
|
761
|
+
grouped: false,
|
|
762
|
+
fixity: null,
|
|
763
|
+
prec: null,
|
|
764
|
+
token: null,
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* One juxtaposed operand: a self-delimiting single-token expression — a
|
|
769
|
+
* variable, a nullary lexicon name, or a nullary notation (an
|
|
770
|
+
* empty-set-style constant) — whose sort coerces into the target.
|
|
771
|
+
* Anything larger (a parenthesized group, a term applied to arguments
|
|
772
|
+
* of its own) is deliberately not an operand yet: nested juxtaposition
|
|
773
|
+
* is ambiguous under variadic sequences and stays deferred.
|
|
774
|
+
*/
|
|
775
|
+
glueOperand(target) {
|
|
776
|
+
const point = this.lang.scanner.at(this.text, this.position);
|
|
777
|
+
for (const reading of point.readings) {
|
|
778
|
+
if (reading.kind === "name") {
|
|
779
|
+
if (reading.ref.kind === "var") {
|
|
780
|
+
if (this.lang.coerce(reading.ref.sort, target) === null) {
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
this.position = point.start + reading.length;
|
|
784
|
+
return this.coerceOrReport({
|
|
785
|
+
kind: "variable",
|
|
786
|
+
name: reading.name,
|
|
787
|
+
sort: reading.ref.sort,
|
|
788
|
+
span: { start: point.start, end: this.position },
|
|
789
|
+
grouped: false,
|
|
790
|
+
binder: false,
|
|
791
|
+
}, target);
|
|
792
|
+
}
|
|
793
|
+
const info = this.lang.spec.terms.get(reading.ref.term);
|
|
794
|
+
if (info === undefined ||
|
|
795
|
+
info.binders.some((binder) => !binder.binds) ||
|
|
796
|
+
this.lang.coerce(info.returnSort, target) === null) {
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
this.position = point.start + reading.length;
|
|
800
|
+
return this.coerceOrReport({
|
|
801
|
+
kind: "app",
|
|
802
|
+
term: info.name,
|
|
803
|
+
args: [],
|
|
804
|
+
sort: info.returnSort,
|
|
805
|
+
span: { start: point.start, end: this.position },
|
|
806
|
+
grouped: false,
|
|
807
|
+
fixity: null,
|
|
808
|
+
prec: null,
|
|
809
|
+
token: null,
|
|
810
|
+
}, target);
|
|
811
|
+
}
|
|
812
|
+
const head = this.lang.heads.get(reading.token);
|
|
813
|
+
if (head?.kind === "general" &&
|
|
814
|
+
head.parts.length === 0 &&
|
|
815
|
+
head.info.binders.length === 0 &&
|
|
816
|
+
this.lang.coerce(head.info.returnSort, target) !== null) {
|
|
817
|
+
this.position = point.start + reading.length;
|
|
818
|
+
return this.coerceOrReport({
|
|
819
|
+
kind: "app",
|
|
820
|
+
term: head.info.name,
|
|
821
|
+
args: [],
|
|
822
|
+
sort: head.info.returnSort,
|
|
823
|
+
span: { start: point.start, end: this.position },
|
|
824
|
+
grouped: false,
|
|
825
|
+
fixity: "general",
|
|
826
|
+
prec: null,
|
|
827
|
+
token: reading.token,
|
|
828
|
+
}, target);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return null;
|
|
832
|
+
}
|
|
833
|
+
// ── lints ─────────────────────────────────────────────────────────────
|
|
834
|
+
lint(term) {
|
|
835
|
+
const lints = this.lang.spec.lints;
|
|
836
|
+
const assocNone = this.lang.spec.assocNone;
|
|
837
|
+
const provable = this.lang.provableSort;
|
|
838
|
+
walkTerm(term, (node, bound) => {
|
|
839
|
+
if (node.kind === "variable") {
|
|
840
|
+
// A leaf of a sort no quantifier binds — a constant from a
|
|
841
|
+
// `@vars` pool, like Calgary's names — cannot be *free*: freedom
|
|
842
|
+
// is only meaningful where binding is possible.
|
|
843
|
+
if (lints.includes("closed-sentences") &&
|
|
844
|
+
!node.binder &&
|
|
845
|
+
this.lang.bindableSorts.has(node.sort) &&
|
|
846
|
+
!bound.has(node.name)) {
|
|
847
|
+
this.report("free_variable", { name: node.name }, node.span);
|
|
848
|
+
}
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
// The chain refusal: an operand produced by an operator at the same
|
|
852
|
+
// assoc-none level, without its own brackets, has no reading.
|
|
853
|
+
if (typeof node.prec === "number" &&
|
|
854
|
+
assocNone.has(node.prec) &&
|
|
855
|
+
(node.fixity === "infixl" || node.fixity === "infixr")) {
|
|
856
|
+
for (const arg of node.args) {
|
|
857
|
+
if (arg.kind === "app" &&
|
|
858
|
+
typeof arg.prec === "number" &&
|
|
859
|
+
arg.prec === node.prec &&
|
|
860
|
+
(arg.fixity === "infixl" || arg.fixity === "infixr") &&
|
|
861
|
+
!arg.grouped) {
|
|
862
|
+
const operator = arg.token ?? this.canonicalToken(arg.term) ?? arg.term;
|
|
863
|
+
this.report("chain_refused", { operator }, arg.span);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
if (lints.includes("parenthesize-binary-only") && node.grouped) {
|
|
868
|
+
const binderSorts = this.lang.spec.terms
|
|
869
|
+
.get(node.term)
|
|
870
|
+
?.binders.map(binderSort);
|
|
871
|
+
const connective = (node.fixity === "infixl" || node.fixity === "infixr") &&
|
|
872
|
+
provable !== null &&
|
|
873
|
+
binderSorts !== undefined &&
|
|
874
|
+
binderSorts.length === 2 &&
|
|
875
|
+
binderSorts.every((sort) => sort === provable);
|
|
876
|
+
if (!connective) {
|
|
877
|
+
this.report("group_binary_only", {}, {
|
|
878
|
+
start: node.span.start,
|
|
879
|
+
end: node.span.start + 1,
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
/** The last-declared notation token for a term — its canonical spelling. */
|
|
886
|
+
canonicalToken(term) {
|
|
887
|
+
let token = null;
|
|
888
|
+
for (const notation of this.lang.spec.notations) {
|
|
889
|
+
if (notation.term === term && notation.form === "simple") {
|
|
890
|
+
token = notation.token;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
return token;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
//# sourceMappingURL=parse.js.map
|