@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/elab.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bidirectional token-elaboration layer — `@syntax elab` rules.
|
|
3
|
+
*
|
|
4
|
+
* Forward (elaboration, before the parser): each rule makes one
|
|
5
|
+
* left-to-right pass over the text, matching at token boundaries only.
|
|
6
|
+
* Where a rule matches, its template is spliced in — literals in their own
|
|
7
|
+
* spelling, captures copied from the source — separated by spaces so the
|
|
8
|
+
* result re-scans cleanly. No fixpoints: matching resumes *after* each
|
|
9
|
+
* replacement, and rules run in declaration order, so the layer terminates
|
|
10
|
+
* by construction. An origin map carries every emitted offset back to
|
|
11
|
+
* the source, so diagnostics and term spans point at what the writer
|
|
12
|
+
* actually typed.
|
|
13
|
+
*
|
|
14
|
+
* Backward (delaboration, after the display printer): the same rules run
|
|
15
|
+
* inverted — template as pattern, pattern as replacement — in reverse
|
|
16
|
+
* declaration order, joined tight, skipping `input-only` rules. A linear
|
|
17
|
+
* rule is a lens: the Quine rule `( ?x:var ) => ∀ ?x` read backward *is*
|
|
18
|
+
* the display convention that writes `∀x` as `(x)`.
|
|
19
|
+
*
|
|
20
|
+
* Captures match single lexicon names — a `@vars` token or a nullary
|
|
21
|
+
* declared term — whose sort coerces into the capture's named sort. The
|
|
22
|
+
* layer is deliberately regular — nesting facts belong to the parser.
|
|
23
|
+
*/
|
|
24
|
+
function nameMatchesSort(lang, reading, sort) {
|
|
25
|
+
if (reading.ref.kind === "var") {
|
|
26
|
+
const from = reading.ref.sort;
|
|
27
|
+
return from === sort || lang.coerce(from, sort) !== null;
|
|
28
|
+
}
|
|
29
|
+
// A *leaf* term: nullary, its sort coercing into the named one. A term
|
|
30
|
+
// that takes arguments is not a leaf.
|
|
31
|
+
const info = lang.spec.terms.get(reading.ref.term);
|
|
32
|
+
if (info === undefined || info.binders.some((binder) => !binder.binds)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return (info.returnSort === sort || lang.coerce(info.returnSort, sort) !== null);
|
|
36
|
+
}
|
|
37
|
+
function matchCapture(lang, text, position, klass) {
|
|
38
|
+
const point = lang.scanner.at(text, position);
|
|
39
|
+
const reading = point.readings.find((r) => r.kind === "name" && nameMatchesSort(lang, r, klass));
|
|
40
|
+
if (reading === undefined || reading.kind !== "name") {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
captured: { start: point.start, text: reading.name },
|
|
45
|
+
end: point.start + reading.length,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function matchRule(lang, pattern, text, position) {
|
|
49
|
+
const captures = new Map();
|
|
50
|
+
let at = position;
|
|
51
|
+
for (const element of pattern) {
|
|
52
|
+
if (element.kind === "literal") {
|
|
53
|
+
const point = lang.scanner.at(text, at);
|
|
54
|
+
const reading = point.readings.find((r) => r.kind === "token" && r.token === element.token);
|
|
55
|
+
if (reading === undefined) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
at = point.start + reading.length;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const taken = [];
|
|
62
|
+
const first = matchCapture(lang, text, at, element.class);
|
|
63
|
+
if (first !== null) {
|
|
64
|
+
taken.push(first.captured);
|
|
65
|
+
at = first.end;
|
|
66
|
+
if (element.quantifier === "+") {
|
|
67
|
+
const separator = "separator" in element ? (element.separator ?? null) : null;
|
|
68
|
+
for (;;) {
|
|
69
|
+
let next = at;
|
|
70
|
+
if (separator !== null) {
|
|
71
|
+
const point = lang.scanner.at(text, next);
|
|
72
|
+
const sepReading = point.readings.find((r) => r.kind === "token" && r.token === separator);
|
|
73
|
+
if (sepReading === undefined) {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
next = point.start + sepReading.length;
|
|
77
|
+
}
|
|
78
|
+
const more = matchCapture(lang, text, next, element.class);
|
|
79
|
+
if (more === null) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
taken.push(more.captured);
|
|
83
|
+
at = more.end;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (taken.length === 0 && element.quantifier !== "?") {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
captures.set(element.name, taken);
|
|
91
|
+
}
|
|
92
|
+
return { captures, end: at };
|
|
93
|
+
}
|
|
94
|
+
function emitTemplate(template, captures, matchStart, originOf, separator) {
|
|
95
|
+
const pieces = [];
|
|
96
|
+
for (const element of template) {
|
|
97
|
+
if (element.kind === "literal") {
|
|
98
|
+
pieces.push({ text: element.token, start: matchStart });
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const taken = captures.get(element.name) ?? [];
|
|
102
|
+
taken.forEach((captured, index) => {
|
|
103
|
+
if (index > 0 && element.separator !== null) {
|
|
104
|
+
pieces.push({ text: element.separator, start: matchStart });
|
|
105
|
+
}
|
|
106
|
+
pieces.push({ text: captured.text, start: captured.start });
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
let text = "";
|
|
110
|
+
const origin = [];
|
|
111
|
+
pieces.forEach((piece, index) => {
|
|
112
|
+
if (index > 0 && separator === " ") {
|
|
113
|
+
text += " ";
|
|
114
|
+
origin.push(originOf(piece.start));
|
|
115
|
+
}
|
|
116
|
+
for (let i = 0; i < piece.text.length; i += 1) {
|
|
117
|
+
origin.push(originOf(piece.start + i));
|
|
118
|
+
}
|
|
119
|
+
text += piece.text;
|
|
120
|
+
});
|
|
121
|
+
return { text, origin };
|
|
122
|
+
}
|
|
123
|
+
/** One pass of one rule, in the given direction. */
|
|
124
|
+
function applyRule(lang, text, origin, pattern, template, separator) {
|
|
125
|
+
const originOf = (index) => origin[index] ?? origin[origin.length - 1] ?? 0;
|
|
126
|
+
let out = "";
|
|
127
|
+
const outOrigin = [];
|
|
128
|
+
let position = 0;
|
|
129
|
+
const copy = (from, to) => {
|
|
130
|
+
for (let i = from; i < to && i < text.length; i += 1) {
|
|
131
|
+
out += text[i];
|
|
132
|
+
outOrigin.push(originOf(i));
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
while (position < text.length) {
|
|
136
|
+
const point = lang.scanner.at(text, position);
|
|
137
|
+
copy(position, point.start);
|
|
138
|
+
if (point.atEnd) {
|
|
139
|
+
position = text.length;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
const match = matchRule(lang, pattern, text, point.start);
|
|
143
|
+
if (match !== null && match.end > point.start) {
|
|
144
|
+
const emitted = emitTemplate(template, match.captures, point.start, originOf, separator);
|
|
145
|
+
out += emitted.text;
|
|
146
|
+
outOrigin.push(...emitted.origin);
|
|
147
|
+
position = match.end;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
// Advance one whole token (the longest reading), staying on
|
|
151
|
+
// boundaries; an unrecognized character passes through untouched.
|
|
152
|
+
const step = Math.max(1, ...point.readings.map((r) => r.length));
|
|
153
|
+
copy(point.start, point.start + step);
|
|
154
|
+
position = point.start + step;
|
|
155
|
+
}
|
|
156
|
+
return { text: out, origin: outOrigin };
|
|
157
|
+
}
|
|
158
|
+
/** The template read as a pattern: references become captures. */
|
|
159
|
+
function invertTemplate(rule) {
|
|
160
|
+
const classes = new Map();
|
|
161
|
+
for (const element of rule.pattern) {
|
|
162
|
+
if (element.kind === "capture") {
|
|
163
|
+
classes.set(element.name, {
|
|
164
|
+
class: element.class,
|
|
165
|
+
plural: element.quantifier === "+",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const inverted = [];
|
|
170
|
+
for (const element of rule.template) {
|
|
171
|
+
if (element.kind === "literal") {
|
|
172
|
+
inverted.push(element);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const known = classes.get(element.name);
|
|
176
|
+
if (known === undefined) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
inverted.push({
|
|
180
|
+
kind: "capture",
|
|
181
|
+
name: element.name,
|
|
182
|
+
class: known.class,
|
|
183
|
+
quantifier: known.plural ? "+" : "",
|
|
184
|
+
separator: element.separator,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return inverted;
|
|
188
|
+
}
|
|
189
|
+
/** The pattern read as a template: captures become plain references. */
|
|
190
|
+
function invertPattern(rule) {
|
|
191
|
+
return rule.pattern.map((element) => element.kind === "literal"
|
|
192
|
+
? element
|
|
193
|
+
: { kind: "reference", name: element.name, separator: null });
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Apply every elaboration rule forward, in declaration order, returning
|
|
197
|
+
* the elaborated text and the offset map back to the source.
|
|
198
|
+
*/
|
|
199
|
+
export function elaborate(lang, text) {
|
|
200
|
+
let current = {
|
|
201
|
+
text,
|
|
202
|
+
origin: [...text].map((_, index) => index),
|
|
203
|
+
};
|
|
204
|
+
for (const rule of lang.spec.elabRules) {
|
|
205
|
+
current = applyRule(lang, current.text, current.origin, rule.pattern, rule.template, " ");
|
|
206
|
+
}
|
|
207
|
+
return current;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Apply every invertible rule backward over display text, last declared
|
|
211
|
+
* first, joining replacements tight (display is character-level anyway).
|
|
212
|
+
*/
|
|
213
|
+
export function delaborate(lang, text) {
|
|
214
|
+
let current = text;
|
|
215
|
+
for (const rule of [...lang.spec.elabRules].reverse()) {
|
|
216
|
+
if (rule.inputOnly) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const pattern = invertTemplate(rule);
|
|
220
|
+
if (pattern === null) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
current = applyRule(lang, current, [...current].map((_, index) => index), pattern, invertPattern(rule), "").text;
|
|
224
|
+
}
|
|
225
|
+
return current;
|
|
226
|
+
}
|
|
227
|
+
/** An elaborated-text span mapped back to source offsets. */
|
|
228
|
+
export function remapSpan(origin, span) {
|
|
229
|
+
const last = origin[origin.length - 1] ?? 0;
|
|
230
|
+
const start = origin[span.start] ?? last + 1;
|
|
231
|
+
const end = span.end > span.start ? (origin[span.end - 1] ?? last) + 1 : start;
|
|
232
|
+
return { start, end };
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=elab.js.map
|
package/dist/elab.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elab.js","sourceRoot":"","sources":["../src/elab.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAoCH,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AAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;IAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC;IAC7C,MAAM,GAAG,GACP,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAErE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;AACxB,CAAC","sourcesContent":["/**\n * The bidirectional token-elaboration layer — `@syntax elab` rules.\n *\n * Forward (elaboration, before the parser): each rule makes one\n * left-to-right pass over the text, matching at token boundaries only.\n * Where a rule matches, its template is spliced in — literals in their own\n * spelling, captures copied from the source — separated by spaces so the\n * result re-scans cleanly. No fixpoints: matching resumes *after* each\n * replacement, and rules run in declaration order, so the layer terminates\n * by construction. An origin map carries every emitted offset back to\n * the source, so diagnostics and term spans point at what the writer\n * actually typed.\n *\n * Backward (delaboration, after the display printer): the same rules run\n * inverted — template as pattern, pattern as replacement — in reverse\n * declaration order, joined tight, skipping `input-only` rules. A linear\n * rule is a lens: the Quine rule `( ?x:var ) => ∀ ?x` read backward *is*\n * the display convention that writes `∀x` as `(x)`.\n *\n * Captures match single lexicon names — a `@vars` token or a nullary\n * declared term — whose sort coerces into the capture's named sort. The\n * layer is deliberately regular — nesting facts belong to the parser.\n */\n\nimport type { SurfaceLanguage } from \"./parse.js\";\nimport type {\n ElabRule,\n RuleCapture,\n RuleLiteral,\n RuleTemplateElement,\n} from \"./reader/annotations.js\";\nimport type { Reading } from \"./scan.js\";\n\n/**\n * The matcher's pattern element: the reader's, plus an optional separator\n * on a capture — inverted `?ts,*` references match separator-interleaved\n * repeats (`x , y`), which the forward syntax has no way to write.\n */\ntype MatchElement =\n | RuleLiteral\n | (RuleCapture & { readonly separator?: string | null });\n\nexport interface Elaborated {\n /** For each offset of `text`, the source offset it came from. */\n readonly origin: readonly number[];\n readonly text: string;\n}\n\ninterface Captured {\n readonly start: number;\n readonly text: string;\n}\n\ninterface RuleMatch {\n readonly captures: ReadonlyMap<string, readonly Captured[]>;\n readonly end: number;\n}\n\nfunction nameMatchesSort(\n lang: SurfaceLanguage,\n reading: Reading & { kind: \"name\" },\n sort: string,\n): boolean {\n if (reading.ref.kind === \"var\") {\n const from = reading.ref.sort;\n\n return from === sort || lang.coerce(from, sort) !== null;\n }\n\n // A *leaf* term: nullary, its sort coercing into the named one. A term\n // that takes arguments is not a leaf.\n const info = lang.spec.terms.get(reading.ref.term);\n\n if (info === undefined || info.binders.some((binder) => !binder.binds)) {\n return false;\n }\n\n return (\n info.returnSort === sort || lang.coerce(info.returnSort, sort) !== null\n );\n}\n\nfunction matchCapture(\n lang: SurfaceLanguage,\n text: string,\n position: number,\n klass: string,\n): { captured: Captured; end: number } | null {\n const point = lang.scanner.at(text, position);\n const reading = point.readings.find(\n (r): r is Reading & { kind: \"name\" } =>\n r.kind === \"name\" && nameMatchesSort(lang, r, klass),\n );\n\n if (reading === undefined || reading.kind !== \"name\") {\n return null;\n }\n\n return {\n captured: { start: point.start, text: reading.name },\n end: point.start + reading.length,\n };\n}\n\nfunction matchRule(\n lang: SurfaceLanguage,\n pattern: readonly MatchElement[],\n text: string,\n position: number,\n): RuleMatch | null {\n const captures = new Map<string, Captured[]>();\n let at = position;\n\n for (const element of pattern) {\n if (element.kind === \"literal\") {\n const point = lang.scanner.at(text, at);\n const reading = point.readings.find(\n (r) => r.kind === \"token\" && r.token === element.token,\n );\n\n if (reading === undefined) {\n return null;\n }\n\n at = point.start + reading.length;\n continue;\n }\n\n const taken: Captured[] = [];\n const first = matchCapture(lang, text, at, element.class);\n\n if (first !== null) {\n taken.push(first.captured);\n at = first.end;\n\n if (element.quantifier === \"+\") {\n const separator =\n \"separator\" in element ? (element.separator ?? null) : null;\n\n for (;;) {\n let next = at;\n\n if (separator !== null) {\n const point = lang.scanner.at(text, next);\n const sepReading = point.readings.find(\n (r) => r.kind === \"token\" && r.token === separator,\n );\n\n if (sepReading === undefined) {\n break;\n }\n\n next = point.start + sepReading.length;\n }\n\n const more = matchCapture(lang, text, next, element.class);\n\n if (more === null) {\n break;\n }\n\n taken.push(more.captured);\n at = more.end;\n }\n }\n }\n\n if (taken.length === 0 && element.quantifier !== \"?\") {\n return null;\n }\n\n captures.set(element.name, taken);\n }\n\n return { captures, end: at };\n}\n\ninterface Emission {\n readonly origin: number[];\n readonly text: string;\n}\n\nfunction emitTemplate(\n template: readonly RuleTemplateElement[],\n captures: ReadonlyMap<string, readonly Captured[]>,\n matchStart: number,\n originOf: (index: number) => number,\n separator: \" \" | \"\",\n): Emission {\n const pieces: { text: string; start: number }[] = [];\n\n for (const element of template) {\n if (element.kind === \"literal\") {\n pieces.push({ text: element.token, start: matchStart });\n continue;\n }\n\n const taken = captures.get(element.name) ?? [];\n\n taken.forEach((captured, index) => {\n if (index > 0 && element.separator !== null) {\n pieces.push({ text: element.separator, start: matchStart });\n }\n\n pieces.push({ text: captured.text, start: captured.start });\n });\n }\n\n let text = \"\";\n const origin: number[] = [];\n\n pieces.forEach((piece, index) => {\n if (index > 0 && separator === \" \") {\n text += \" \";\n origin.push(originOf(piece.start));\n }\n\n for (let i = 0; i < piece.text.length; i += 1) {\n origin.push(originOf(piece.start + i));\n }\n\n text += piece.text;\n });\n\n return { text, origin };\n}\n\n/** One pass of one rule, in the given direction. */\nfunction applyRule(\n lang: SurfaceLanguage,\n text: string,\n origin: readonly number[],\n pattern: readonly MatchElement[],\n template: readonly RuleTemplateElement[],\n separator: \" \" | \"\",\n): Elaborated {\n const originOf = (index: number): number =>\n origin[index] ?? origin[origin.length - 1] ?? 0;\n let out = \"\";\n const outOrigin: number[] = [];\n let position = 0;\n\n const copy = (from: number, to: number): void => {\n for (let i = from; i < to && i < text.length; i += 1) {\n out += text[i];\n outOrigin.push(originOf(i));\n }\n };\n\n while (position < text.length) {\n const point = lang.scanner.at(text, position);\n\n copy(position, point.start);\n\n if (point.atEnd) {\n position = text.length;\n break;\n }\n\n const match = matchRule(lang, pattern, text, point.start);\n\n if (match !== null && match.end > point.start) {\n const emitted = emitTemplate(\n template,\n match.captures,\n point.start,\n originOf,\n separator,\n );\n\n out += emitted.text;\n outOrigin.push(...emitted.origin);\n position = match.end;\n continue;\n }\n\n // Advance one whole token (the longest reading), staying on\n // boundaries; an unrecognized character passes through untouched.\n const step = Math.max(1, ...point.readings.map((r) => r.length));\n\n copy(point.start, point.start + step);\n position = point.start + step;\n }\n\n return { text: out, origin: outOrigin };\n}\n\n/** The template read as a pattern: references become captures. */\nfunction invertTemplate(rule: ElabRule): readonly MatchElement[] | null {\n const classes = new Map<string, { class: string; plural: boolean }>();\n\n for (const element of rule.pattern) {\n if (element.kind === \"capture\") {\n classes.set(element.name, {\n class: element.class,\n plural: element.quantifier === \"+\",\n });\n }\n }\n\n const inverted: MatchElement[] = [];\n\n for (const element of rule.template) {\n if (element.kind === \"literal\") {\n inverted.push(element);\n continue;\n }\n\n const known = classes.get(element.name);\n\n if (known === undefined) {\n return null;\n }\n\n inverted.push({\n kind: \"capture\",\n name: element.name,\n class: known.class,\n quantifier: known.plural ? \"+\" : \"\",\n separator: element.separator,\n });\n }\n\n return inverted;\n}\n\n/** The pattern read as a template: captures become plain references. */\nfunction invertPattern(rule: ElabRule): readonly RuleTemplateElement[] {\n return rule.pattern.map((element) =>\n element.kind === \"literal\"\n ? element\n : { kind: \"reference\", name: element.name, separator: null },\n );\n}\n\n/**\n * Apply every elaboration rule forward, in declaration order, returning\n * the elaborated text and the offset map back to the source.\n */\nexport function elaborate(lang: SurfaceLanguage, text: string): Elaborated {\n let current: Elaborated = {\n text,\n origin: [...text].map((_, index) => index),\n };\n\n for (const rule of lang.spec.elabRules) {\n current = applyRule(\n lang,\n current.text,\n current.origin,\n rule.pattern,\n rule.template,\n \" \",\n );\n }\n\n return current;\n}\n\n/**\n * Apply every invertible rule backward over display text, last declared\n * first, joining replacements tight (display is character-level anyway).\n */\nexport function delaborate(lang: SurfaceLanguage, text: string): string {\n let current = text;\n\n for (const rule of [...lang.spec.elabRules].reverse()) {\n if (rule.inputOnly) {\n continue;\n }\n\n const pattern = invertTemplate(rule);\n\n if (pattern === null) {\n continue;\n }\n\n current = applyRule(\n lang,\n current,\n [...current].map((_, index) => index),\n pattern,\n invertPattern(rule),\n \"\",\n ).text;\n }\n\n return current;\n}\n\n/** An elaborated-text span mapped back to source offsets. */\nexport function remapSpan(\n origin: readonly number[],\n span: { readonly start: number; readonly end: number },\n): { start: number; end: number } {\n const last = origin[origin.length - 1] ?? 0;\n const start = origin[span.start] ?? last + 1;\n const end =\n span.end > span.start ? (origin[span.end - 1] ?? last) + 1 : start;\n\n return { start, end };\n}\n"]}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for handing work to the Aufbau engine.
|
|
3
|
+
*
|
|
4
|
+
* The engine rejects annotations it does not know, so a spec file goes to
|
|
5
|
+
* it with the `@syntax` lines stripped — the library owns that boundary.
|
|
6
|
+
* Everything else in the spec is already the engine's language: the
|
|
7
|
+
* lexicon is ordinary term declarations, and variables are `@vars` pools,
|
|
8
|
+
* which the engine understands natively. `boundVariableBinders` writes
|
|
9
|
+
* the `{x: var}` binder list a theorem statement needs for the variables
|
|
10
|
+
* (bound or free — Calgary's names included) a formula mentions.
|
|
11
|
+
*/
|
|
12
|
+
import type { Term } from "./term.js";
|
|
13
|
+
/** The spec source with every `@syntax` annotation line removed. */
|
|
14
|
+
export declare function stripSyntaxAnnotations(source: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* The variable binder list for a theorem statement over these terms,
|
|
17
|
+
* e.g. `{x y: var} {a: name}` — one group per sort, empty string when
|
|
18
|
+
* there are no variables.
|
|
19
|
+
*/
|
|
20
|
+
export declare function boundVariableBinders(terms: readonly Term[]): string;
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for handing work to the Aufbau engine.
|
|
3
|
+
*
|
|
4
|
+
* The engine rejects annotations it does not know, so a spec file goes to
|
|
5
|
+
* it with the `@syntax` lines stripped — the library owns that boundary.
|
|
6
|
+
* Everything else in the spec is already the engine's language: the
|
|
7
|
+
* lexicon is ordinary term declarations, and variables are `@vars` pools,
|
|
8
|
+
* which the engine understands natively. `boundVariableBinders` writes
|
|
9
|
+
* the `{x: var}` binder list a theorem statement needs for the variables
|
|
10
|
+
* (bound or free — Calgary's names included) a formula mentions.
|
|
11
|
+
*/
|
|
12
|
+
import { walkTerm } from "./term.js";
|
|
13
|
+
/** The spec source with every `@syntax` annotation line removed. */
|
|
14
|
+
export function stripSyntaxAnnotations(source) {
|
|
15
|
+
return source
|
|
16
|
+
.split("\n")
|
|
17
|
+
.filter((line) => !line.trim().startsWith("--| @syntax"))
|
|
18
|
+
.join("\n");
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The variable binder list for a theorem statement over these terms,
|
|
22
|
+
* e.g. `{x y: var} {a: name}` — one group per sort, empty string when
|
|
23
|
+
* there are no variables.
|
|
24
|
+
*/
|
|
25
|
+
export function boundVariableBinders(terms) {
|
|
26
|
+
const bySort = new Map();
|
|
27
|
+
for (const term of terms) {
|
|
28
|
+
walkTerm(term, (node) => {
|
|
29
|
+
if (node.kind === "variable") {
|
|
30
|
+
const names = bySort.get(node.sort) ?? new Set();
|
|
31
|
+
names.add(node.name);
|
|
32
|
+
bySort.set(node.sort, names);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return [...bySort.entries()]
|
|
37
|
+
.map(([sort, names]) => `{${[...names].join(" ")}: ${sort}}`)
|
|
38
|
+
.join(" ");
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
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for handing work to the Aufbau engine.\n *\n * The engine rejects annotations it does not know, so a spec file goes to\n * it with the `@syntax` lines stripped — the library owns that boundary.\n * Everything else in the spec is already the engine's language: the\n * lexicon is ordinary term declarations, and variables are `@vars` pools,\n * which the engine understands natively. `boundVariableBinders` writes\n * the `{x: var}` binder list a theorem statement needs for the variables\n * (bound or free — Calgary's names included) a formula mentions.\n */\n\nimport type { Term } from \"./term.js\";\nimport { walkTerm } from \"./term.js\";\n\n/** The spec source with every `@syntax` annotation line removed. */\nexport function stripSyntaxAnnotations(source: string): string {\n return source\n .split(\"\\n\")\n .filter((line) => !line.trim().startsWith(\"--| @syntax\"))\n .join(\"\\n\");\n}\n\n/**\n * The variable binder list for a theorem statement over these terms,\n * e.g. `{x y: var} {a: name}` — one group per sort, empty string when\n * there are no variables.\n */\nexport function boundVariableBinders(terms: readonly Term[]): string {\n const bySort = new Map<string, Set<string>>();\n\n for (const term of terms) {\n walkTerm(term, (node) => {\n if (node.kind === \"variable\") {\n const names = bySort.get(node.sort) ?? new Set();\n names.add(node.name);\n bySort.set(node.sort, names);\n }\n });\n }\n\n return [...bySort.entries()]\n .map(([sort, names]) => `{${[...names].join(\" \")}: ${sort}}`)\n .join(\" \");\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @aufbau/syntax — surface syntax over MM0.
|
|
3
|
+
*
|
|
4
|
+
* The public API grows stage by stage; see the README for the layer stack.
|
|
5
|
+
*/
|
|
6
|
+
export type { Diagnostic, Span } from "./diagnostics.js";
|
|
7
|
+
export { boundVariableBinders, stripSyntaxAnnotations } from "./engine.js";
|
|
8
|
+
export type { ParseFailure, ParseResult, ParseSuccess, } from "./parse.js";
|
|
9
|
+
export { SurfaceLanguage } from "./parse.js";
|
|
10
|
+
export type { PrintMode } from "./print.js";
|
|
11
|
+
export { printTerm } from "./print.js";
|
|
12
|
+
export type { ElabRule, LintName, RuleCapture, RuleLiteral, RulePatternElement, RuleReference, RuleTemplateElement, SyntaxAnnotation, } from "./reader/annotations.js";
|
|
13
|
+
export type { CoercionInfo, NotationInfo, SortInfo, Spec, SpecParse, TermInfo, } from "./reader/spec.js";
|
|
14
|
+
export { parseSpec } from "./reader/spec.js";
|
|
15
|
+
export type { Annotation, AssertStatement, Binder, CoercionStatement, DefStatement, DelimiterStatement, GenNotationStatement, MathString, NotationLiteral, Precedence, SimpleNotationStatement, SortStatement, Statement, StatementParse, TermStatement, TypeRef, } from "./reader/statements.js";
|
|
16
|
+
export { parseStatements } from "./reader/statements.js";
|
|
17
|
+
export type { NameRef, Reading, ScanPoint } from "./scan.js";
|
|
18
|
+
export { Scanner } from "./scan.js";
|
|
19
|
+
export type { AppTerm, Fixity, Term, VariableTerm } from "./term.js";
|
|
20
|
+
export { walkTerm } from "./term.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @aufbau/syntax — surface syntax over MM0.
|
|
3
|
+
*
|
|
4
|
+
* The public API grows stage by stage; see the README for the layer stack.
|
|
5
|
+
*/
|
|
6
|
+
export { boundVariableBinders, stripSyntaxAnnotations } from "./engine.js";
|
|
7
|
+
export { SurfaceLanguage } from "./parse.js";
|
|
8
|
+
export { printTerm } from "./print.js";
|
|
9
|
+
export { parseSpec } from "./reader/spec.js";
|
|
10
|
+
export { parseStatements } from "./reader/statements.js";
|
|
11
|
+
export { Scanner } from "./scan.js";
|
|
12
|
+
export { walkTerm } from "./term.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAM3E,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAmBvC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAmB7C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC","sourcesContent":["/**\n * @aufbau/syntax — surface syntax over MM0.\n *\n * The public API grows stage by stage; see the README for the layer stack.\n */\n\nexport type { Diagnostic, Span } from \"./diagnostics.js\";\nexport { boundVariableBinders, stripSyntaxAnnotations } from \"./engine.js\";\nexport type {\n ParseFailure,\n ParseResult,\n ParseSuccess,\n} from \"./parse.js\";\nexport { SurfaceLanguage } from \"./parse.js\";\nexport type { PrintMode } from \"./print.js\";\nexport { printTerm } from \"./print.js\";\nexport type {\n ElabRule,\n LintName,\n RuleCapture,\n RuleLiteral,\n RulePatternElement,\n RuleReference,\n RuleTemplateElement,\n SyntaxAnnotation,\n} from \"./reader/annotations.js\";\nexport type {\n CoercionInfo,\n NotationInfo,\n SortInfo,\n Spec,\n SpecParse,\n TermInfo,\n} from \"./reader/spec.js\";\nexport { parseSpec } from \"./reader/spec.js\";\nexport type {\n Annotation,\n AssertStatement,\n Binder,\n CoercionStatement,\n DefStatement,\n DelimiterStatement,\n GenNotationStatement,\n MathString,\n NotationLiteral,\n Precedence,\n SimpleNotationStatement,\n SortStatement,\n Statement,\n StatementParse,\n TermStatement,\n TypeRef,\n} from \"./reader/statements.js\";\nexport { parseStatements } from \"./reader/statements.js\";\nexport type { NameRef, Reading, ScanPoint } from \"./scan.js\";\nexport { Scanner } from \"./scan.js\";\nexport type { AppTerm, Fixity, Term, VariableTerm } from \"./term.js\";\nexport { walkTerm } from \"./term.js\";\n"]}
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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 { type Diagnostic } from "./diagnostics.js";
|
|
30
|
+
import type { NotationInfo, Spec, TermInfo } from "./reader/spec.js";
|
|
31
|
+
import type { Binder } from "./reader/statements.js";
|
|
32
|
+
import { Scanner } from "./scan.js";
|
|
33
|
+
import type { Term } from "./term.js";
|
|
34
|
+
interface PrefixEntry {
|
|
35
|
+
readonly kind: "prefix";
|
|
36
|
+
readonly info: TermInfo;
|
|
37
|
+
readonly prec: number | "max";
|
|
38
|
+
}
|
|
39
|
+
interface InfixEntry {
|
|
40
|
+
readonly kind: "infix";
|
|
41
|
+
readonly info: TermInfo;
|
|
42
|
+
readonly prec: number;
|
|
43
|
+
readonly fixity: "infixl" | "infixr";
|
|
44
|
+
}
|
|
45
|
+
interface GeneralSlot {
|
|
46
|
+
readonly binder: Binder;
|
|
47
|
+
/** Index into the term's binder list this slot fills. */
|
|
48
|
+
readonly index: number;
|
|
49
|
+
readonly prec: number;
|
|
50
|
+
}
|
|
51
|
+
interface GeneralEntry {
|
|
52
|
+
readonly kind: "general";
|
|
53
|
+
readonly info: TermInfo;
|
|
54
|
+
/** Interleaved constants (to expect) and slots (to parse), head dropped. */
|
|
55
|
+
readonly parts: readonly ({
|
|
56
|
+
readonly kind: "constant";
|
|
57
|
+
readonly token: string;
|
|
58
|
+
} | {
|
|
59
|
+
readonly kind: "slot";
|
|
60
|
+
readonly slot: GeneralSlot;
|
|
61
|
+
})[];
|
|
62
|
+
}
|
|
63
|
+
type HeadEntry = GeneralEntry | PrefixEntry;
|
|
64
|
+
export interface ParseSuccess {
|
|
65
|
+
readonly ok: true;
|
|
66
|
+
readonly term: Term;
|
|
67
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
68
|
+
}
|
|
69
|
+
export interface ParseFailure {
|
|
70
|
+
readonly ok: false;
|
|
71
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
72
|
+
}
|
|
73
|
+
export type ParseResult = ParseFailure | ParseSuccess;
|
|
74
|
+
/** Everything derived from a Spec that parsing needs, built once. */
|
|
75
|
+
export declare class SurfaceLanguage {
|
|
76
|
+
readonly spec: Spec;
|
|
77
|
+
readonly scanner: Scanner;
|
|
78
|
+
readonly heads: Map<string, HeadEntry>;
|
|
79
|
+
readonly infixes: Map<string, InfixEntry>;
|
|
80
|
+
readonly closeOf: Map<string, string>;
|
|
81
|
+
readonly closers: Set<string>;
|
|
82
|
+
/** Per sort, its `@syntax juxtaposed` combiner — adjacency's meaning. */
|
|
83
|
+
readonly juxtaposedOf: Map<string, TermInfo>;
|
|
84
|
+
/** Per sort, its `@syntax elided` term — the unwritten argument. */
|
|
85
|
+
readonly elidedOf: Map<string, TermInfo>;
|
|
86
|
+
/** Sorts some quantifier binds — whose variables *can* be captured. */
|
|
87
|
+
readonly bindableSorts: Set<string>;
|
|
88
|
+
readonly provableSort: string | null;
|
|
89
|
+
/** Per term, its last-declared notation — the canonical spelling. */
|
|
90
|
+
readonly canonical: Map<string, NotationInfo>;
|
|
91
|
+
readonly coercionNames: Set<string>;
|
|
92
|
+
private readonly coercionPaths;
|
|
93
|
+
constructor(spec: Spec);
|
|
94
|
+
/**
|
|
95
|
+
* A sentential connective: an infix constructor joining two things of
|
|
96
|
+
* the provable sort. This is the class forallx's bracket convention and
|
|
97
|
+
* the display printer's spacing both key on — `∧` is one, `=` over
|
|
98
|
+
* terms is not.
|
|
99
|
+
*/
|
|
100
|
+
isConnective(term: string): boolean;
|
|
101
|
+
/** The coercion path from one sort to another; unique per mm0.md. */
|
|
102
|
+
coerce(from: string, to: string): readonly string[] | null;
|
|
103
|
+
/**
|
|
104
|
+
* Parse surface text. `lints: false` skips the spec's refusal
|
|
105
|
+
* conventions (bracket discipline, chain refusal, closed sentences) —
|
|
106
|
+
* for reading text that is grammatical but not surface-idiomatic, such
|
|
107
|
+
* as this library's own engine-mode output.
|
|
108
|
+
*/
|
|
109
|
+
parse(text: string, options?: {
|
|
110
|
+
readonly lints?: boolean;
|
|
111
|
+
}): ParseResult;
|
|
112
|
+
}
|
|
113
|
+
export {};
|