@aufbau/syntax 0.0.2 → 0.0.3
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 +52 -0
- package/dist/elab.d.ts +24 -3
- package/dist/elab.js +32 -6
- package/dist/elab.js.map +1 -1
- package/dist/parse.d.ts +34 -7
- package/dist/parse.js +55 -27
- package/dist/parse.js.map +1 -1
- package/dist/reader/annotations.d.ts +1 -1
- package/dist/reader/annotations.js +1 -1
- package/dist/reader/annotations.js.map +1 -1
- package/dist/reader/spec.d.ts +1 -0
- package/dist/reader/spec.js +56 -1
- package/dist/reader/spec.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.0.3
|
|
4
|
+
|
|
5
|
+
**The sort input is read at is a `parse` argument.**
|
|
6
|
+
`parse(text, { sort })` names it; the text must parse to that sort or
|
|
7
|
+
coerce into it. The default is unchanged — `provableSort`, the first sort
|
|
8
|
+
the file marks `provable` — so no existing call behaves differently. What
|
|
9
|
+
this is for is a file that declares several provable sorts, which is what
|
|
10
|
+
a merged theory-and-language artifact does when its judgements are
|
|
11
|
+
`Γ ⊢ φ`: the caller knows whether it wants a formula or a sequent, and
|
|
12
|
+
declaration order is not an answer. `term_not_sentence` now carries the
|
|
13
|
+
sort it wanted as well as the sort it got.
|
|
14
|
+
|
|
15
|
+
**`@syntax role` may sit on a sort** (`SortInfo.roles`, symmetric with
|
|
16
|
+
`TermInfo`), and is still **interpreted nowhere**. It is the recommended
|
|
17
|
+
place for an application to record which sort *it* means to read at, so
|
|
18
|
+
that stays in the spec — which is data — instead of hard-coded per
|
|
19
|
+
language id in the caller. The library reading such a role itself was
|
|
20
|
+
considered and rejected: a general-purpose parser should not privilege
|
|
21
|
+
one sort name.
|
|
22
|
+
|
|
23
|
+
**`isConnective` no longer keys on a distinguished sort.** A connective is
|
|
24
|
+
now an infix constructor *closed over* a `provable` sort — two arguments
|
|
25
|
+
of that sort and a result of the same one. `∧` qualifies; `=` over terms
|
|
26
|
+
does not (its arguments are of another sort); a turnstile from two
|
|
27
|
+
formulas to a judgement does not (it does not return what it takes); the
|
|
28
|
+
argument comma does not (`seq` is not provable). A judgement-level
|
|
29
|
+
conjunction now gets the same spacing and bracketing as a formula-level
|
|
30
|
+
one, which it should. The `parenthesize-binary-only` lint goes through the
|
|
31
|
+
same predicate rather than its own copy of it.
|
|
32
|
+
|
|
33
|
+
**An elab pattern literal is any chunk, declared or not.** It used to have
|
|
34
|
+
to be a declared notation token, which it enforced by not matching: a rule
|
|
35
|
+
whose literal was a lexicon name read clean and quietly did nothing. The
|
|
36
|
+
guard was also only half-applied (template literals were never checked)
|
|
37
|
+
and it was not what held chunk anchoring — the delimiters do that. So it
|
|
38
|
+
is gone. This is what lets a merged theory-and-language file spell a
|
|
39
|
+
quantifier `A` while `A` is also a predicate letter: MM0 gives a math
|
|
40
|
+
token one meaning, so the notation has to go, and elab puts the spelling
|
|
41
|
+
back.
|
|
42
|
+
|
|
43
|
+
Two read-time diagnostics take its place, both aimed at rules that would
|
|
44
|
+
otherwise be silently dead: `elab_literal_split` for a literal the surface
|
|
45
|
+
delimiters cut in two (write it as separate elements), and
|
|
46
|
+
`elab_literal_looks_like_capture` for a literal containing `?`, which is a
|
|
47
|
+
mis-spaced capture — patterns split on whitespace, so `$ (?x:var) $` is
|
|
48
|
+
one literal. Template literals are checked as well for any rule that is
|
|
49
|
+
not `input-only`, since delaboration matches them.
|
|
50
|
+
|
|
51
|
+
`docs/syntax-annotations.md` now also states the three commitments the
|
|
52
|
+
layer makes — leftmost, greedy, one sweep per rule — and the shape of rule
|
|
53
|
+
to prefer, since a rule can eat a span that had another reading.
|
|
54
|
+
|
|
3
55
|
## 0.0.2 — 2026-08-24
|
|
4
56
|
|
|
5
57
|
**Breaking: student input is segmented by declared delimiters, not by the
|
package/dist/elab.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The bidirectional token-elaboration layer — `@syntax elab` rules.
|
|
3
3
|
*
|
|
4
4
|
* Forward (elaboration, before the parser): each rule makes one
|
|
5
|
-
* left-to-right pass over the text, matching at
|
|
5
|
+
* left-to-right pass over the text, matching at chunk boundaries only.
|
|
6
6
|
* Where a rule matches, its template is spliced in — literals in their own
|
|
7
7
|
* spelling, captures copied from the source — separated by spaces so the
|
|
8
8
|
* result re-scans cleanly. No fixpoints: matching resumes *after* each
|
|
@@ -18,8 +18,29 @@
|
|
|
18
18
|
* the display convention that writes `∀x` as `(x)`.
|
|
19
19
|
*
|
|
20
20
|
* Captures match single lexicon names — a `@vars` token or a nullary
|
|
21
|
-
* declared term — whose sort coerces into the capture's named sort.
|
|
22
|
-
*
|
|
21
|
+
* declared term — whose sort coerces into the capture's named sort. A
|
|
22
|
+
* literal matches a whole chunk, which need not be declared vocabulary at
|
|
23
|
+
* all: the layer exists to accept surface forms the MM0 grammar rejects.
|
|
24
|
+
* It is deliberately regular — nesting facts belong to the parser.
|
|
25
|
+
*
|
|
26
|
+
* **The three commitments.** What an author has to reason about is that
|
|
27
|
+
* this layer decides, and the parser then inherits the decision as fact —
|
|
28
|
+
* the parser backtracks over ambiguity, but only over what reaches it.
|
|
29
|
+
*
|
|
30
|
+
* - *Leftmost*: the first match at the earliest scan point wins, and the
|
|
31
|
+
* span it consumed is replaced and never re-examined.
|
|
32
|
+
* - *Greedy*: a `+` capture takes the maximum it can and never gives one
|
|
33
|
+
* back to let the rest of the pattern match.
|
|
34
|
+
* - *One sweep per rule*, in declaration order, so a later rule sees
|
|
35
|
+
* what earlier rules emitted, and never the other way round.
|
|
36
|
+
*
|
|
37
|
+
* The hazard those three add up to: a rule may eat a span that had another
|
|
38
|
+
* reading. Prefer patterns anchored by a leading literal — a spelling the
|
|
39
|
+
* author chose — over capture-initial ones, which fire wherever the sorts
|
|
40
|
+
* line up. The dialect where even that is not enough is one with both
|
|
41
|
+
* letter-spelled quantifiers and juxtaposed predication, where `Ax` is
|
|
42
|
+
* genuinely both `∀x` and A-applied-to-x; there, juxtaposition has to be a
|
|
43
|
+
* parser behavior (`@syntax juxtaposed`), as it is for Magnus.
|
|
23
44
|
*/
|
|
24
45
|
import type { SurfaceLanguage } from "./parse.js";
|
|
25
46
|
export interface Elaborated {
|
package/dist/elab.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The bidirectional token-elaboration layer — `@syntax elab` rules.
|
|
3
3
|
*
|
|
4
4
|
* Forward (elaboration, before the parser): each rule makes one
|
|
5
|
-
* left-to-right pass over the text, matching at
|
|
5
|
+
* left-to-right pass over the text, matching at chunk boundaries only.
|
|
6
6
|
* Where a rule matches, its template is spliced in — literals in their own
|
|
7
7
|
* spelling, captures copied from the source — separated by spaces so the
|
|
8
8
|
* result re-scans cleanly. No fixpoints: matching resumes *after* each
|
|
@@ -18,8 +18,29 @@
|
|
|
18
18
|
* the display convention that writes `∀x` as `(x)`.
|
|
19
19
|
*
|
|
20
20
|
* Captures match single lexicon names — a `@vars` token or a nullary
|
|
21
|
-
* declared term — whose sort coerces into the capture's named sort.
|
|
22
|
-
*
|
|
21
|
+
* declared term — whose sort coerces into the capture's named sort. A
|
|
22
|
+
* literal matches a whole chunk, which need not be declared vocabulary at
|
|
23
|
+
* all: the layer exists to accept surface forms the MM0 grammar rejects.
|
|
24
|
+
* It is deliberately regular — nesting facts belong to the parser.
|
|
25
|
+
*
|
|
26
|
+
* **The three commitments.** What an author has to reason about is that
|
|
27
|
+
* this layer decides, and the parser then inherits the decision as fact —
|
|
28
|
+
* the parser backtracks over ambiguity, but only over what reaches it.
|
|
29
|
+
*
|
|
30
|
+
* - *Leftmost*: the first match at the earliest scan point wins, and the
|
|
31
|
+
* span it consumed is replaced and never re-examined.
|
|
32
|
+
* - *Greedy*: a `+` capture takes the maximum it can and never gives one
|
|
33
|
+
* back to let the rest of the pattern match.
|
|
34
|
+
* - *One sweep per rule*, in declaration order, so a later rule sees
|
|
35
|
+
* what earlier rules emitted, and never the other way round.
|
|
36
|
+
*
|
|
37
|
+
* The hazard those three add up to: a rule may eat a span that had another
|
|
38
|
+
* reading. Prefer patterns anchored by a leading literal — a spelling the
|
|
39
|
+
* author chose — over capture-initial ones, which fire wherever the sorts
|
|
40
|
+
* line up. The dialect where even that is not enough is one with both
|
|
41
|
+
* letter-spelled quantifiers and juxtaposed predication, where `Ax` is
|
|
42
|
+
* genuinely both `∀x` and A-applied-to-x; there, juxtaposition has to be a
|
|
43
|
+
* parser behavior (`@syntax juxtaposed`), as it is for Magnus.
|
|
23
44
|
*/
|
|
24
45
|
function nameMatchesSort(lang, reading, sort) {
|
|
25
46
|
if (reading.ref.kind === "var") {
|
|
@@ -51,11 +72,16 @@ function matchRule(lang, pattern, text, position) {
|
|
|
51
72
|
for (const element of pattern) {
|
|
52
73
|
if (element.kind === "literal") {
|
|
53
74
|
const point = lang.scanner.at(text, at);
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
// A literal matches the *chunk*, declared vocabulary or not. Elab
|
|
76
|
+
// exists to accept surface forms the MM0 grammar rejects — the Quine
|
|
77
|
+
// rule makes `(x)` mean something no declaration mentions — so
|
|
78
|
+
// constraining its pieces to declared tokens would be a half-measure
|
|
79
|
+
// with nothing behind it. Anchoring is unaffected: the chunk's
|
|
80
|
+
// boundaries were fixed by the delimiters before any lookup.
|
|
81
|
+
if (point.chunk !== element.token) {
|
|
56
82
|
return null;
|
|
57
83
|
}
|
|
58
|
-
at = point.start +
|
|
84
|
+
at = point.start + point.chunk.length;
|
|
59
85
|
continue;
|
|
60
86
|
}
|
|
61
87
|
const taken = [];
|
package/dist/elab.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elab.js","sourceRoot":"","sources":["../src/elab.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAoCH,SAAS,eAAe,CACtB,IAAqB,EACrB,OAAmC,EACnC,IAA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M,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 chunk, so the pass only ever resumes on a\n // segmentation boundary; a chunk the vocabulary does not recognize\n // passes through untouched.\n const step = Math.max(1, point.chunk.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"]}
|
|
1
|
+
{"version":3,"file":"elab.js","sourceRoot":"","sources":["../src/elab.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAoCH,SAAS,eAAe,CACtB,IAAqB,EACrB,OAAmC,EACnC,IAAY;IAEZ,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;QAC/B,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QAE9B,OAAO,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,IAAI,CAAC;IAC3D,CAAC;IAED,uEAAuE;IACvE,sCAAsC;IACtC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAEnD,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QACvE,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,C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nB,SAAS;QACX,CAAC;QAED,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;QAErC,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACrB,SAAS;QACX,CAAC;QAED,OAAO,GAAG,SAAS,CACjB,IAAI,EACJ,OAAO,EACP,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EACrC,OAAO,EACP,aAAa,CAAC,IAAI,CAAC,EACnB,EAAE,CACH,CAAC,IAAI,CAAC;IACT,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,SAAS,CACvB,MAAyB,EACzB,IAAsD;IAEtD,MAAM,IAAI,GAAG,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 chunk 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. A\n * literal matches a whole chunk, which need not be declared vocabulary at\n * all: the layer exists to accept surface forms the MM0 grammar rejects.\n * It is deliberately regular — nesting facts belong to the parser.\n *\n * **The three commitments.** What an author has to reason about is that\n * this layer decides, and the parser then inherits the decision as fact —\n * the parser backtracks over ambiguity, but only over what reaches it.\n *\n * - *Leftmost*: the first match at the earliest scan point wins, and the\n * span it consumed is replaced and never re-examined.\n * - *Greedy*: a `+` capture takes the maximum it can and never gives one\n * back to let the rest of the pattern match.\n * - *One sweep per rule*, in declaration order, so a later rule sees\n * what earlier rules emitted, and never the other way round.\n *\n * The hazard those three add up to: a rule may eat a span that had another\n * reading. Prefer patterns anchored by a leading literal — a spelling the\n * author chose — over capture-initial ones, which fire wherever the sorts\n * line up. The dialect where even that is not enough is one with both\n * letter-spelled quantifiers and juxtaposed predication, where `Ax` is\n * genuinely both `∀x` and A-applied-to-x; there, juxtaposition has to be a\n * parser behavior (`@syntax juxtaposed`), as it is for Magnus.\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\n // A literal matches the *chunk*, declared vocabulary or not. Elab\n // exists to accept surface forms the MM0 grammar rejects — the Quine\n // rule makes `(x)` mean something no declaration mentions — so\n // constraining its pieces to declared tokens would be a half-measure\n // with nothing behind it. Anchoring is unaffected: the chunk's\n // boundaries were fixed by the delimiters before any lookup.\n if (point.chunk !== element.token) {\n return null;\n }\n\n at = point.start + point.chunk.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 chunk, so the pass only ever resumes on a\n // segmentation boundary; a chunk the vocabulary does not recognize\n // passes through untouched.\n const step = Math.max(1, point.chunk.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/parse.d.ts
CHANGED
|
@@ -96,6 +96,12 @@ export declare class SurfaceLanguage {
|
|
|
96
96
|
readonly elidedOf: Map<string, TermInfo>;
|
|
97
97
|
/** Sorts some quantifier binds — whose variables *can* be captured. */
|
|
98
98
|
readonly bindableSorts: Set<string>;
|
|
99
|
+
/**
|
|
100
|
+
* The first sort the file marks `provable` — MM0's own way of saying
|
|
101
|
+
* "assertable". It is `parse`'s default target sort and nothing else; a
|
|
102
|
+
* caller that wants another sort names it, and a file with several
|
|
103
|
+
* provable sorts is expected to.
|
|
104
|
+
*/
|
|
99
105
|
readonly provableSort: string | null;
|
|
100
106
|
/** Per term, its last-declared notation — the canonical spelling. */
|
|
101
107
|
readonly canonical: Map<string, NotationInfo>;
|
|
@@ -103,18 +109,38 @@ export declare class SurfaceLanguage {
|
|
|
103
109
|
private readonly coercionPaths;
|
|
104
110
|
constructor(spec: Spec);
|
|
105
111
|
/**
|
|
106
|
-
* A
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* terms is not
|
|
112
|
+
* A connective: an infix constructor closed over a `provable` sort —
|
|
113
|
+
* two arguments of that sort, and a result of the same one. This is the
|
|
114
|
+
* class forallx's bracket convention and the display printer's spacing
|
|
115
|
+
* both key on: `∧` is one; `=` over terms is not (its arguments are of
|
|
116
|
+
* another sort); a turnstile from two formulas to a judgement is not
|
|
117
|
+
* (it does not return what it takes); and the argument comma is not
|
|
118
|
+
* (`seq` is not provable), which is what keeps `R(a,b)` from printing
|
|
119
|
+
* as `R((a , b))`.
|
|
120
|
+
*
|
|
121
|
+
* Nothing here privileges one sort. A theory that states judgements in
|
|
122
|
+
* a provable sort of their own gets a judgement-level conjunction
|
|
123
|
+
* spaced and bracketed on the same terms as a formula-level one, which
|
|
124
|
+
* is right.
|
|
110
125
|
*/
|
|
111
126
|
isConnective(term: string): boolean;
|
|
127
|
+
/** Whether the sort carries MM0's `provable` modifier. */
|
|
128
|
+
isProvableSort(sort: string): boolean;
|
|
112
129
|
/** The coercion path from one sort to another; unique per mm0.md. */
|
|
113
130
|
coerce(from: string, to: string): readonly string[] | null;
|
|
114
131
|
/**
|
|
115
|
-
* Parse surface text. `
|
|
116
|
-
*
|
|
117
|
-
*
|
|
132
|
+
* Parse surface text. `sort` is what the result is read at: the text
|
|
133
|
+
* must parse to that sort, or coerce into it, or it is refused. It
|
|
134
|
+
* defaults to `provableSort` — the first sort the file marks `provable`,
|
|
135
|
+
* MM0's own way of saying "assertable" — which is the whole story for a
|
|
136
|
+
* spec with one such sort. A file with several (a theory whose
|
|
137
|
+
* judgements are `Γ ⊢ φ` in a sort of their own) has a genuine choice to
|
|
138
|
+
* make per call, and the caller is who knows: a translation exercise
|
|
139
|
+
* reads at the formula sort, a proof widget at the judgement sort.
|
|
140
|
+
*
|
|
141
|
+
* `lints: false` skips the spec's refusal conventions (bracket
|
|
142
|
+
* discipline, chain refusal, closed sentences) — for reading text that
|
|
143
|
+
* is grammatical but not surface-idiomatic.
|
|
118
144
|
*
|
|
119
145
|
* `mode: "engine"` reads this library's own engine-mode output instead:
|
|
120
146
|
* the theory's delimiters rather than the surface set, no elaboration,
|
|
@@ -126,6 +152,7 @@ export declare class SurfaceLanguage {
|
|
|
126
152
|
parse(text: string, options?: {
|
|
127
153
|
readonly lints?: boolean;
|
|
128
154
|
readonly mode?: ParseMode;
|
|
155
|
+
readonly sort?: string;
|
|
129
156
|
}): ParseResult;
|
|
130
157
|
}
|
|
131
158
|
export {};
|
package/dist/parse.js
CHANGED
|
@@ -72,6 +72,12 @@ export class SurfaceLanguage {
|
|
|
72
72
|
elidedOf = new Map();
|
|
73
73
|
/** Sorts some quantifier binds — whose variables *can* be captured. */
|
|
74
74
|
bindableSorts = new Set();
|
|
75
|
+
/**
|
|
76
|
+
* The first sort the file marks `provable` — MM0's own way of saying
|
|
77
|
+
* "assertable". It is `parse`'s default target sort and nothing else; a
|
|
78
|
+
* caller that wants another sort names it, and a file with several
|
|
79
|
+
* provable sorts is expected to.
|
|
80
|
+
*/
|
|
75
81
|
provableSort;
|
|
76
82
|
/** Per term, its last-declared notation — the canonical spelling. */
|
|
77
83
|
canonical = new Map();
|
|
@@ -164,24 +170,36 @@ export class SurfaceLanguage {
|
|
|
164
170
|
[...spec.sorts.values()].find((sort) => sort.modifiers.includes("provable"))?.name ?? null;
|
|
165
171
|
}
|
|
166
172
|
/**
|
|
167
|
-
* A
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* terms is not
|
|
173
|
+
* A connective: an infix constructor closed over a `provable` sort —
|
|
174
|
+
* two arguments of that sort, and a result of the same one. This is the
|
|
175
|
+
* class forallx's bracket convention and the display printer's spacing
|
|
176
|
+
* both key on: `∧` is one; `=` over terms is not (its arguments are of
|
|
177
|
+
* another sort); a turnstile from two formulas to a judgement is not
|
|
178
|
+
* (it does not return what it takes); and the argument comma is not
|
|
179
|
+
* (`seq` is not provable), which is what keeps `R(a,b)` from printing
|
|
180
|
+
* as `R((a , b))`.
|
|
181
|
+
*
|
|
182
|
+
* Nothing here privileges one sort. A theory that states judgements in
|
|
183
|
+
* a provable sort of their own gets a judgement-level conjunction
|
|
184
|
+
* spaced and bracketed on the same terms as a formula-level one, which
|
|
185
|
+
* is right.
|
|
171
186
|
*/
|
|
172
187
|
isConnective(term) {
|
|
173
188
|
const info = this.spec.terms.get(term);
|
|
174
|
-
const provable = this.provableSort;
|
|
175
189
|
const notation = this.canonical.get(term);
|
|
176
|
-
return (
|
|
177
|
-
info
|
|
190
|
+
return (info !== undefined &&
|
|
191
|
+
this.isProvableSort(info.returnSort) &&
|
|
178
192
|
notation !== undefined &&
|
|
179
193
|
notation.form === "simple" &&
|
|
180
194
|
notation.fixity !== "prefix" &&
|
|
181
195
|
info.binders.length === 2 &&
|
|
182
196
|
info.binders.every((binder) => !binder.binds &&
|
|
183
197
|
"sort" in binder.type &&
|
|
184
|
-
binder.type.sort ===
|
|
198
|
+
binder.type.sort === info.returnSort));
|
|
199
|
+
}
|
|
200
|
+
/** Whether the sort carries MM0's `provable` modifier. */
|
|
201
|
+
isProvableSort(sort) {
|
|
202
|
+
return this.spec.sorts.get(sort)?.modifiers.includes("provable") === true;
|
|
185
203
|
}
|
|
186
204
|
/** The coercion path from one sort to another; unique per mm0.md. */
|
|
187
205
|
coerce(from, to) {
|
|
@@ -225,9 +243,18 @@ export class SurfaceLanguage {
|
|
|
225
243
|
return path;
|
|
226
244
|
}
|
|
227
245
|
/**
|
|
228
|
-
* Parse surface text. `
|
|
229
|
-
*
|
|
230
|
-
*
|
|
246
|
+
* Parse surface text. `sort` is what the result is read at: the text
|
|
247
|
+
* must parse to that sort, or coerce into it, or it is refused. It
|
|
248
|
+
* defaults to `provableSort` — the first sort the file marks `provable`,
|
|
249
|
+
* MM0's own way of saying "assertable" — which is the whole story for a
|
|
250
|
+
* spec with one such sort. A file with several (a theory whose
|
|
251
|
+
* judgements are `Γ ⊢ φ` in a sort of their own) has a genuine choice to
|
|
252
|
+
* make per call, and the caller is who knows: a translation exercise
|
|
253
|
+
* reads at the formula sort, a proof widget at the judgement sort.
|
|
254
|
+
*
|
|
255
|
+
* `lints: false` skips the spec's refusal conventions (bracket
|
|
256
|
+
* discipline, chain refusal, closed sentences) — for reading text that
|
|
257
|
+
* is grammatical but not surface-idiomatic.
|
|
231
258
|
*
|
|
232
259
|
* `mode: "engine"` reads this library's own engine-mode output instead:
|
|
233
260
|
* the theory's delimiters rather than the surface set, no elaboration,
|
|
@@ -239,16 +266,17 @@ export class SurfaceLanguage {
|
|
|
239
266
|
parse(text, options = {}) {
|
|
240
267
|
const mode = options.mode ?? "surface";
|
|
241
268
|
const lints = options.lints ?? mode === "surface";
|
|
269
|
+
const sort = options.sort ?? this.provableSort;
|
|
242
270
|
if (mode === "engine") {
|
|
243
|
-
return new Parse(this, this.engineScanner, text, lints).run();
|
|
271
|
+
return new Parse(this, this.engineScanner, text, lints, sort).run();
|
|
244
272
|
}
|
|
245
273
|
if (this.spec.elabRules.length === 0) {
|
|
246
|
-
return new Parse(this, this.scanner, text, lints).run();
|
|
274
|
+
return new Parse(this, this.scanner, text, lints, sort).run();
|
|
247
275
|
}
|
|
248
276
|
// Elaborate first, then map every span in the outcome back through
|
|
249
277
|
// the origin map, so nothing downstream sees elaborated offsets.
|
|
250
278
|
const elaborated = elaborate(this, text);
|
|
251
|
-
const result = new Parse(this, this.scanner, elaborated.text, lints).run();
|
|
279
|
+
const result = new Parse(this, this.scanner, elaborated.text, lints, sort).run();
|
|
252
280
|
const diagnostics = result.diagnostics.map((diag) => ({
|
|
253
281
|
...diag,
|
|
254
282
|
span: remapSpan(elaborated.origin, diag.span),
|
|
@@ -271,6 +299,7 @@ class Parse {
|
|
|
271
299
|
scanner;
|
|
272
300
|
text;
|
|
273
301
|
lintsEnabled;
|
|
302
|
+
sort;
|
|
274
303
|
position = 0;
|
|
275
304
|
diagnostics = [];
|
|
276
305
|
/**
|
|
@@ -281,11 +310,14 @@ class Parse {
|
|
|
281
310
|
* not a stray `(` after the sentence letter F.
|
|
282
311
|
*/
|
|
283
312
|
bestTrialFailure = null;
|
|
284
|
-
constructor(lang, scanner, text, lintsEnabled
|
|
313
|
+
constructor(lang, scanner, text, lintsEnabled,
|
|
314
|
+
/** The sort the result is read at; null leaves the target open. */
|
|
315
|
+
sort) {
|
|
285
316
|
this.lang = lang;
|
|
286
317
|
this.scanner = scanner;
|
|
287
318
|
this.text = text;
|
|
288
319
|
this.lintsEnabled = lintsEnabled;
|
|
320
|
+
this.sort = sort;
|
|
289
321
|
}
|
|
290
322
|
run() {
|
|
291
323
|
const parsed = this.parseExpr(0);
|
|
@@ -321,11 +353,11 @@ class Parse {
|
|
|
321
353
|
return { ok: false, diagnostics: this.diagnostics };
|
|
322
354
|
}
|
|
323
355
|
let term = parsed;
|
|
324
|
-
const
|
|
325
|
-
if (
|
|
326
|
-
const wrapped = this.coerceTerm(term,
|
|
356
|
+
const target = this.sort;
|
|
357
|
+
if (target !== null && term.sort !== target) {
|
|
358
|
+
const wrapped = this.coerceTerm(term, target);
|
|
327
359
|
if (wrapped === null) {
|
|
328
|
-
this.report("term_not_sentence", { actual: term.sort }, term.span);
|
|
360
|
+
this.report("term_not_sentence", { actual: term.sort, expected: target }, term.span);
|
|
329
361
|
return { ok: false, diagnostics: this.diagnostics };
|
|
330
362
|
}
|
|
331
363
|
term = wrapped;
|
|
@@ -854,7 +886,6 @@ class Parse {
|
|
|
854
886
|
lint(term) {
|
|
855
887
|
const lints = this.lang.spec.lints;
|
|
856
888
|
const assocNone = this.lang.spec.assocNone;
|
|
857
|
-
const provable = this.lang.provableSort;
|
|
858
889
|
walkTerm(term, (node, bound) => {
|
|
859
890
|
if (node.kind === "variable") {
|
|
860
891
|
// A leaf of a sort no quantifier binds — a constant from a
|
|
@@ -885,14 +916,11 @@ class Parse {
|
|
|
885
916
|
}
|
|
886
917
|
}
|
|
887
918
|
if (lints.includes("parenthesize-binary-only") && node.grouped) {
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
919
|
+
// The same class the printer brackets — see `isConnective`, which
|
|
920
|
+
// this reads through the parse's own fixity rather than the
|
|
921
|
+
// canonical notation's.
|
|
891
922
|
const connective = (node.fixity === "infixl" || node.fixity === "infixr") &&
|
|
892
|
-
|
|
893
|
-
binderSorts !== undefined &&
|
|
894
|
-
binderSorts.length === 2 &&
|
|
895
|
-
binderSorts.every((sort) => sort === provable);
|
|
923
|
+
this.lang.isConnective(node.term);
|
|
896
924
|
if (!connective) {
|
|
897
925
|
this.report("group_binary_only", {}, {
|
|
898
926
|
start: node.span.start,
|