@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/scan.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The surface scanner: character-level maximal munch over a spec's token
|
|
3
|
+
* vocabulary and lexicon names.
|
|
4
|
+
*
|
|
5
|
+
* Where MM0's own lexer splits math strings on whitespace and single-byte
|
|
6
|
+
* delimiters, students write textbook notation with no spaces at all —
|
|
7
|
+
* `∀xF(x)`, `~~P`, `AxEy~R(x,y)`. So this scanner asks, at each position:
|
|
8
|
+
* which declared notation token starts here (longest match wins), and which
|
|
9
|
+
* lexicon name? A position can answer *both* — Calgary's `A` is at once a
|
|
10
|
+
* spelling of ∀ and the predicate letter A — so the scanner reports
|
|
11
|
+
* alternative readings, each with its own length, and the parser picks by
|
|
12
|
+
* context.
|
|
13
|
+
*
|
|
14
|
+
* The name vocabulary is derived, not annotated: every token in a sort's
|
|
15
|
+
* `@vars` pool, and every declared term that has no notation of its own,
|
|
16
|
+
* binds nothing, and is not a coercion. A term *with* a notation is
|
|
17
|
+
* spelled by that notation; a term without one is spelled by its name —
|
|
18
|
+
* MM0's own application rule, read character-level.
|
|
19
|
+
*/
|
|
20
|
+
import type { Span } from "./diagnostics.js";
|
|
21
|
+
import type { Spec } from "./reader/spec.js";
|
|
22
|
+
export type NameRef = {
|
|
23
|
+
readonly kind: "term";
|
|
24
|
+
readonly term: string;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: "var";
|
|
27
|
+
readonly sort: string;
|
|
28
|
+
};
|
|
29
|
+
export type Reading = {
|
|
30
|
+
readonly kind: "name";
|
|
31
|
+
readonly name: string;
|
|
32
|
+
readonly ref: NameRef;
|
|
33
|
+
readonly length: number;
|
|
34
|
+
} | {
|
|
35
|
+
readonly kind: "token";
|
|
36
|
+
readonly token: string;
|
|
37
|
+
readonly length: number;
|
|
38
|
+
};
|
|
39
|
+
export interface ScanPoint {
|
|
40
|
+
/** Position after leading whitespace; readings start here. */
|
|
41
|
+
readonly start: number;
|
|
42
|
+
/** Longest-first notation reading, then the longest name reading. Empty
|
|
43
|
+
* at end of input or on an unrecognized character. */
|
|
44
|
+
readonly readings: readonly Reading[];
|
|
45
|
+
readonly atEnd: boolean;
|
|
46
|
+
}
|
|
47
|
+
export declare class Scanner {
|
|
48
|
+
/** Notation and grouping tokens, longest first. */
|
|
49
|
+
private readonly tokens;
|
|
50
|
+
/** Lexicon names, longest first, each with what it refers to. */
|
|
51
|
+
private readonly names;
|
|
52
|
+
constructor(spec: Spec);
|
|
53
|
+
/**
|
|
54
|
+
* The alternative readings at `position` in `text`, after skipping
|
|
55
|
+
* whitespace. The list is ordered: the longest matching notation token
|
|
56
|
+
* first, then the longest matching name.
|
|
57
|
+
*/
|
|
58
|
+
at(text: string, position: number): ScanPoint;
|
|
59
|
+
/** The span a reading occupies from a scan point. */
|
|
60
|
+
span(point: ScanPoint, reading: Reading): Span;
|
|
61
|
+
}
|
package/dist/scan.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The surface scanner: character-level maximal munch over a spec's token
|
|
3
|
+
* vocabulary and lexicon names.
|
|
4
|
+
*
|
|
5
|
+
* Where MM0's own lexer splits math strings on whitespace and single-byte
|
|
6
|
+
* delimiters, students write textbook notation with no spaces at all —
|
|
7
|
+
* `∀xF(x)`, `~~P`, `AxEy~R(x,y)`. So this scanner asks, at each position:
|
|
8
|
+
* which declared notation token starts here (longest match wins), and which
|
|
9
|
+
* lexicon name? A position can answer *both* — Calgary's `A` is at once a
|
|
10
|
+
* spelling of ∀ and the predicate letter A — so the scanner reports
|
|
11
|
+
* alternative readings, each with its own length, and the parser picks by
|
|
12
|
+
* context.
|
|
13
|
+
*
|
|
14
|
+
* The name vocabulary is derived, not annotated: every token in a sort's
|
|
15
|
+
* `@vars` pool, and every declared term that has no notation of its own,
|
|
16
|
+
* binds nothing, and is not a coercion. A term *with* a notation is
|
|
17
|
+
* spelled by that notation; a term without one is spelled by its name —
|
|
18
|
+
* MM0's own application rule, read character-level.
|
|
19
|
+
*/
|
|
20
|
+
export class Scanner {
|
|
21
|
+
/** Notation and grouping tokens, longest first. */
|
|
22
|
+
tokens;
|
|
23
|
+
/** Lexicon names, longest first, each with what it refers to. */
|
|
24
|
+
names;
|
|
25
|
+
constructor(spec) {
|
|
26
|
+
const vocabulary = new Set();
|
|
27
|
+
const notated = new Set();
|
|
28
|
+
for (const notation of spec.notations) {
|
|
29
|
+
notated.add(notation.term);
|
|
30
|
+
if (notation.form === "simple") {
|
|
31
|
+
vocabulary.add(notation.token);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
for (const literal of notation.literals) {
|
|
35
|
+
if (literal.kind === "constant") {
|
|
36
|
+
vocabulary.add(literal.token);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
for (const [open, close] of spec.groupingPairs) {
|
|
42
|
+
vocabulary.add(open);
|
|
43
|
+
vocabulary.add(close);
|
|
44
|
+
}
|
|
45
|
+
this.tokens = [...vocabulary].sort((a, b) => b.length - a.length);
|
|
46
|
+
const coercions = new Set(spec.coercions.map((c) => c.name));
|
|
47
|
+
const names = new Map();
|
|
48
|
+
for (const sort of spec.sorts.values()) {
|
|
49
|
+
for (const token of sort.vars) {
|
|
50
|
+
names.set(token, { kind: "var", sort: sort.name });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const term of spec.terms.values()) {
|
|
54
|
+
if (!notated.has(term.name) &&
|
|
55
|
+
!coercions.has(term.name) &&
|
|
56
|
+
!term.binders.some((binder) => binder.binds) &&
|
|
57
|
+
!names.has(term.name)) {
|
|
58
|
+
names.set(term.name, { kind: "term", term: term.name });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this.names = [...names.entries()].sort((a, b) => b[0].length - a[0].length);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The alternative readings at `position` in `text`, after skipping
|
|
65
|
+
* whitespace. The list is ordered: the longest matching notation token
|
|
66
|
+
* first, then the longest matching name.
|
|
67
|
+
*/
|
|
68
|
+
at(text, position) {
|
|
69
|
+
let start = position;
|
|
70
|
+
while (start < text.length && /\s/.test(text[start] ?? "")) {
|
|
71
|
+
start += 1;
|
|
72
|
+
}
|
|
73
|
+
if (start >= text.length) {
|
|
74
|
+
return { start, readings: [], atEnd: true };
|
|
75
|
+
}
|
|
76
|
+
const readings = [];
|
|
77
|
+
for (const token of this.tokens) {
|
|
78
|
+
if (text.startsWith(token, start)) {
|
|
79
|
+
readings.push({ kind: "token", token, length: token.length });
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const [name, ref] of this.names) {
|
|
84
|
+
if (text.startsWith(name, start)) {
|
|
85
|
+
readings.push({ kind: "name", name, ref, length: name.length });
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { start, readings, atEnd: false };
|
|
90
|
+
}
|
|
91
|
+
/** The span a reading occupies from a scan point. */
|
|
92
|
+
span(point, reading) {
|
|
93
|
+
return { start: point.start, end: point.start + reading.length };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=scan.js.map
|
package/dist/scan.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scan.js","sourceRoot":"","sources":["../src/scan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AA+BH,MAAM,OAAO,OAAO;IAClB,mDAAmD;IAClC,MAAM,CAAoB;IAC3C,iEAAiE;IAChD,KAAK,CAA0C;IAEhE,YAAY,IAAU;QACpB,MAAM,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;QACrC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;QAElC,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACtC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAE3B,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC/B,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YACjC,CAAC;iBAAM,CAAC;gBACN,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;oBACxC,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;wBACh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AAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACrC,IAAI,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC;gBACjC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;gBAChE,MAAM;YACR,CAAC;QACH,CAAC;QAED,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IAC3C,CAAC;IAED,qDAAqD;IACrD,IAAI,CAAC,KAAgB,EAAE,OAAgB;QACrC,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACnE,CAAC;CACF","sourcesContent":["/**\n * The surface scanner: character-level maximal munch over a spec's token\n * vocabulary and lexicon names.\n *\n * Where MM0's own lexer splits math strings on whitespace and single-byte\n * delimiters, students write textbook notation with no spaces at all —\n * `∀xF(x)`, `~~P`, `AxEy~R(x,y)`. So this scanner asks, at each position:\n * which declared notation token starts here (longest match wins), and which\n * lexicon name? A position can answer *both* — Calgary's `A` is at once a\n * spelling of ∀ and the predicate letter A — so the scanner reports\n * alternative readings, each with its own length, and the parser picks by\n * context.\n *\n * The name vocabulary is derived, not annotated: every token in a sort's\n * `@vars` pool, and every declared term that has no notation of its own,\n * binds nothing, and is not a coercion. A term *with* a notation is\n * spelled by that notation; a term without one is spelled by its name —\n * MM0's own application rule, read character-level.\n */\n\nimport type { Span } from \"./diagnostics.js\";\nimport type { Spec } from \"./reader/spec.js\";\n\nexport type NameRef =\n | { readonly kind: \"term\"; readonly term: string }\n | { readonly kind: \"var\"; readonly sort: string };\n\nexport type Reading =\n | {\n readonly kind: \"name\";\n readonly name: string;\n readonly ref: NameRef;\n readonly length: number;\n }\n | {\n readonly kind: \"token\";\n readonly token: string;\n readonly length: number;\n };\n\nexport interface ScanPoint {\n /** Position after leading whitespace; readings start here. */\n readonly start: number;\n /** Longest-first notation reading, then the longest name reading. Empty\n * at end of input or on an unrecognized character. */\n readonly readings: readonly Reading[];\n readonly atEnd: boolean;\n}\n\nexport class Scanner {\n /** Notation and grouping tokens, longest first. */\n private readonly tokens: readonly string[];\n /** Lexicon names, longest first, each with what it refers to. */\n private readonly names: readonly (readonly [string, NameRef])[];\n\n constructor(spec: Spec) {\n const vocabulary = new Set<string>();\n const notated = new Set<string>();\n\n for (const notation of spec.notations) {\n notated.add(notation.term);\n\n if (notation.form === \"simple\") {\n vocabulary.add(notation.token);\n } else {\n for (const literal of notation.literals) {\n if (literal.kind === \"constant\") {\n vocabulary.add(literal.token);\n }\n }\n }\n }\n\n for (const [open, close] of spec.groupingPairs) {\n vocabulary.add(open);\n vocabulary.add(close);\n }\n\n this.tokens = [...vocabulary].sort((a, b) => b.length - a.length);\n\n const coercions = new Set(spec.coercions.map((c) => c.name));\n const names = new Map<string, NameRef>();\n\n for (const sort of spec.sorts.values()) {\n for (const token of sort.vars) {\n names.set(token, { kind: \"var\", sort: sort.name });\n }\n }\n\n for (const term of spec.terms.values()) {\n if (\n !notated.has(term.name) &&\n !coercions.has(term.name) &&\n !term.binders.some((binder) => binder.binds) &&\n !names.has(term.name)\n ) {\n names.set(term.name, { kind: \"term\", term: term.name });\n }\n }\n\n this.names = [...names.entries()].sort(\n (a, b) => b[0].length - a[0].length,\n );\n }\n\n /**\n * The alternative readings at `position` in `text`, after skipping\n * whitespace. The list is ordered: the longest matching notation token\n * first, then the longest matching name.\n */\n at(text: string, position: number): ScanPoint {\n let start = position;\n\n while (start < text.length && /\\s/.test(text[start] ?? \"\")) {\n start += 1;\n }\n\n if (start >= text.length) {\n return { start, readings: [], atEnd: true };\n }\n\n const readings: Reading[] = [];\n\n for (const token of this.tokens) {\n if (text.startsWith(token, start)) {\n readings.push({ kind: \"token\", token, length: token.length });\n break;\n }\n }\n\n for (const [name, ref] of this.names) {\n if (text.startsWith(name, start)) {\n readings.push({ kind: \"name\", name, ref, length: name.length });\n break;\n }\n }\n\n return { start, readings, atEnd: false };\n }\n\n /** The span a reading occupies from a scan point. */\n span(point: ScanPoint, reading: Reading): Span {\n return { start: point.start, end: point.start + reading.length };\n }\n}\n"]}
|
package/dist/term.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The term tree a surface parse produces: constructor applications over the
|
|
3
|
+
* spec's signature, with variables as leaves. No hardcoded connective set —
|
|
4
|
+
* what a node *means* is the consumer's business, via `@syntax role`.
|
|
5
|
+
*/
|
|
6
|
+
import type { Span } from "./diagnostics.js";
|
|
7
|
+
export type Fixity = "general" | "infixl" | "infixr" | "prefix";
|
|
8
|
+
export type Term = AppTerm | VariableTerm;
|
|
9
|
+
export interface AppTerm {
|
|
10
|
+
readonly kind: "app";
|
|
11
|
+
/** The constructor: a declared term, def, or coercion name. */
|
|
12
|
+
readonly term: string;
|
|
13
|
+
readonly args: readonly Term[];
|
|
14
|
+
readonly sort: string;
|
|
15
|
+
readonly span: Span;
|
|
16
|
+
/** True when the source wrapped this node in its own bracket pair. */
|
|
17
|
+
readonly grouped: boolean;
|
|
18
|
+
/** How the node was written, for printing and for the tree lints. */
|
|
19
|
+
readonly fixity: Fixity | null;
|
|
20
|
+
readonly prec: number | "max" | null;
|
|
21
|
+
/**
|
|
22
|
+
* The notation token as the source spelled it (`->`, not the canonical
|
|
23
|
+
* `→`), so diagnostics can quote what the writer typed. Null when the
|
|
24
|
+
* node was not written through a token: letters and coercions.
|
|
25
|
+
*/
|
|
26
|
+
readonly token: string | null;
|
|
27
|
+
}
|
|
28
|
+
export interface VariableTerm {
|
|
29
|
+
readonly kind: "variable";
|
|
30
|
+
/** The token as written — one of its sort's `@vars` pool. */
|
|
31
|
+
readonly name: string;
|
|
32
|
+
readonly sort: string;
|
|
33
|
+
readonly span: Span;
|
|
34
|
+
readonly grouped: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* True when this occurrence fills a *binding* slot of its parent — the x
|
|
37
|
+
* of `∀x`, which scopes over the arguments after it. Set by the parser
|
|
38
|
+
* from the constructor's binder; the tree shape alone cannot tell `∀x …`
|
|
39
|
+
* from `x = y`.
|
|
40
|
+
*/
|
|
41
|
+
readonly binder: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Depth-first walk, parents before children. `bound` holds the variable
|
|
45
|
+
* names in scope at each node; a binding occurrence extends it for the
|
|
46
|
+
* arguments that follow it.
|
|
47
|
+
*/
|
|
48
|
+
export declare function walkTerm(term: Term, visit: (node: Term, bound: ReadonlySet<string>) => void, bound?: ReadonlySet<string>): void;
|
package/dist/term.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The term tree a surface parse produces: constructor applications over the
|
|
3
|
+
* spec's signature, with variables as leaves. No hardcoded connective set —
|
|
4
|
+
* what a node *means* is the consumer's business, via `@syntax role`.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Depth-first walk, parents before children. `bound` holds the variable
|
|
8
|
+
* names in scope at each node; a binding occurrence extends it for the
|
|
9
|
+
* arguments that follow it.
|
|
10
|
+
*/
|
|
11
|
+
export function walkTerm(term, visit, bound = new Set()) {
|
|
12
|
+
visit(term, bound);
|
|
13
|
+
if (term.kind !== "app") {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
let scope = bound;
|
|
17
|
+
for (const arg of term.args) {
|
|
18
|
+
if (arg.kind === "variable" && arg.binder) {
|
|
19
|
+
visit(arg, scope);
|
|
20
|
+
const next = new Set(scope);
|
|
21
|
+
next.add(arg.name);
|
|
22
|
+
scope = next;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
walkTerm(arg, visit, scope);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=term.js.map
|
package/dist/term.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"term.js","sourceRoot":"","sources":["../src/term.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA4CH;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CACtB,IAAU,EACV,KAAuD,EACvD,QAA6B,IAAI,GAAG,EAAE;IAEtC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IAEnB,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;QACxB,OAAO;IACT,CAAC;IAED,IAAI,KAAK,GAAG,KAAK,CAAC;IAElB,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QAC5B,IAAI,GAAG,CAAC,IAAI,KAAK,UAAU,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;YAC1C,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAClB,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;YAC5B,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACnB,KAAK,GAAG,IAAI,CAAC;YACb,SAAS;QACX,CAAC;QAED,QAAQ,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;AACH,CAAC","sourcesContent":["/**\n * The term tree a surface parse produces: constructor applications over the\n * spec's signature, with variables as leaves. No hardcoded connective set —\n * what a node *means* is the consumer's business, via `@syntax role`.\n */\n\nimport type { Span } from \"./diagnostics.js\";\n\nexport type Fixity = \"general\" | \"infixl\" | \"infixr\" | \"prefix\";\n\nexport type Term = AppTerm | VariableTerm;\n\nexport interface AppTerm {\n readonly kind: \"app\";\n /** The constructor: a declared term, def, or coercion name. */\n readonly term: string;\n readonly args: readonly Term[];\n readonly sort: string;\n readonly span: Span;\n /** True when the source wrapped this node in its own bracket pair. */\n readonly grouped: boolean;\n /** How the node was written, for printing and for the tree lints. */\n readonly fixity: Fixity | null;\n readonly prec: number | \"max\" | null;\n /**\n * The notation token as the source spelled it (`->`, not the canonical\n * `→`), so diagnostics can quote what the writer typed. Null when the\n * node was not written through a token: letters and coercions.\n */\n readonly token: string | null;\n}\n\nexport interface VariableTerm {\n readonly kind: \"variable\";\n /** The token as written — one of its sort's `@vars` pool. */\n readonly name: string;\n readonly sort: string;\n readonly span: Span;\n readonly grouped: boolean;\n /**\n * True when this occurrence fills a *binding* slot of its parent — the x\n * of `∀x`, which scopes over the arguments after it. Set by the parser\n * from the constructor's binder; the tree shape alone cannot tell `∀x …`\n * from `x = y`.\n */\n readonly binder: boolean;\n}\n\n/**\n * Depth-first walk, parents before children. `bound` holds the variable\n * names in scope at each node; a binding occurrence extends it for the\n * arguments that follow it.\n */\nexport function walkTerm(\n term: Term,\n visit: (node: Term, bound: ReadonlySet<string>) => void,\n bound: ReadonlySet<string> = new Set(),\n): void {\n visit(term, bound);\n\n if (term.kind !== \"app\") {\n return;\n }\n\n let scope = bound;\n\n for (const arg of term.args) {\n if (arg.kind === \"variable\" && arg.binder) {\n visit(arg, scope);\n const next = new Set(scope);\n next.add(arg.name);\n scope = next;\n continue;\n }\n\n walkTerm(arg, visit, scope);\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aufbau/syntax",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Surface syntax over MM0: a statement reader, a character-level scanner, a faithful math-string parser, a pretty-printer, and a bidirectional token-elaboration layer for textbook logical notations.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/gleachkr/aufbau-syntax.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
31
|
+
"check": "tsc --noEmit",
|
|
32
|
+
"format": "biome format --write .",
|
|
33
|
+
"lint": "biome lint .",
|
|
34
|
+
"lint:check": "biome ci .",
|
|
35
|
+
"prepack": "bun run build",
|
|
36
|
+
"test": "bun test",
|
|
37
|
+
"validate": "bun test && bun run check && bun run lint:check"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@aufbau/compiler": "0.0.6",
|
|
41
|
+
"@biomejs/biome": "^2.5.10",
|
|
42
|
+
"@types/bun": "^1.3.14",
|
|
43
|
+
"typescript": "^6.0.3"
|
|
44
|
+
}
|
|
45
|
+
}
|