@aufbau/syntax 0.0.1 → 0.0.2

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/dist/scan.js CHANGED
@@ -1,69 +1,41 @@
1
1
  /**
2
- * The surface scanner: character-level maximal munch over a spec's token
3
- * vocabulary and lexicon names.
2
+ * The surface scanner: segment, then classify.
4
3
  *
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.
4
+ * Segmentation comes from the declared delimiter set alone — MM0's own
5
+ * tokenizer rule, in `delimiters.ts` — and never consults the term or
6
+ * notation tables. Only once a chunk's boundaries are fixed is the chunk
7
+ * looked up: as a notation token, as a lexicon name, or as both. Calgary's
8
+ * `A` is at once a spelling of ∀ and the predicate letter A, so the scanner
9
+ * reports both readings and the parser picks by backtracking.
13
10
  *
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.
11
+ * The order is the whole point. Under the character-level maximal munch
12
+ * this replaces, declaring a term could silently re-read an existing
13
+ * string: adding `term ab: tm;` to Magnus turned `Fab` from `F` of `a` and
14
+ * `b` into `F` of `ab`. Boundaries fixed before any lookup cannot do that,
15
+ * which is exactly what MM0 buys by keeping `delimiter` separate from
16
+ * `notation`.
17
+ *
18
+ * The delimiter set is a parameter: student input is read under the spec's
19
+ * `surfaceDelimiters`, and this library's own engine-mode output under the
20
+ * theory's own `delimiters`.
19
21
  */
22
+ import { chunkAt, delimiterRules, segment, } from "./delimiters.js";
23
+ import { surfaceVocabulary } from "./vocabulary.js";
20
24
  export class Scanner {
21
- /** Notation and grouping tokens, longest first. */
25
+ rules;
26
+ /** Notation and grouping tokens. */
22
27
  tokens;
23
- /** Lexicon names, longest first, each with what it refers to. */
28
+ /** Lexicon names, each with what it refers to. */
24
29
  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);
30
+ constructor(spec, delimiters = spec.surfaceDelimiters) {
31
+ const { names, tokens } = surfaceVocabulary(spec);
32
+ this.rules = delimiterRules(delimiters);
33
+ this.names = names;
34
+ this.tokens = tokens;
62
35
  }
63
36
  /**
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.
37
+ * The chunk at `position` in `text`, after skipping whitespace, and what
38
+ * it can be read as: the notation token first, then the lexicon name.
67
39
  */
68
40
  at(text, position) {
69
41
  let start = position;
@@ -71,22 +43,26 @@ export class Scanner {
71
43
  start += 1;
72
44
  }
73
45
  if (start >= text.length) {
74
- return { start, readings: [], atEnd: true };
46
+ return { start, chunk: "", readings: [], atEnd: true };
75
47
  }
48
+ const chunk = chunkAt(text, start, this.rules);
76
49
  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
- }
50
+ if (this.tokens.has(chunk)) {
51
+ readings.push({ kind: "token", token: chunk, length: chunk.length });
82
52
  }
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
- }
53
+ const ref = this.names.get(chunk);
54
+ if (ref !== undefined) {
55
+ readings.push({ kind: "name", name: chunk, ref, length: chunk.length });
88
56
  }
89
- return { start, readings, atEnd: false };
57
+ return { start, chunk, readings, atEnd: false };
58
+ }
59
+ /**
60
+ * The chunk sequence of `text`, before any classification — the view
61
+ * that shows what the delimiters did, independently of what the spec's
62
+ * vocabulary happens to contain.
63
+ */
64
+ chunks(text) {
65
+ return segment(text, this.rules);
90
66
  }
91
67
  /** The span a reading occupies from a scan point. */
92
68
  span(point, reading) {
package/dist/scan.js.map CHANGED
@@ -1 +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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CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,CAAC;gBAClC,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;gBAC9D,MAAM;YACR,CAAC;QACH,CAAC;QAED,KAAK,MAAM,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"]}
1
+ {"version":3,"file":"scan.js","sourceRoot":"","sources":["../src/scan.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EACL,OAAO,EAGP,cAAc,EACd,OAAO,GACR,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAgB,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AA4BlE,MAAM,OAAO,OAAO;IACD,KAAK,CAAiB;IACvC,oCAAoC;IACnB,MAAM,CAAsB;IAC7C,kDAAkD;IACjC,KAAK,CAA+B;IAErD,YAAY,IAAU,EAAE,aAA2B,IAAI,CAAC,iBAAiB;QACvE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;QAElD,IAAI,CAAC,KAAK,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;OAGG;IACH,EAAE,CAAC,IAAY,EAAE,QAAgB;QAC/B,IAAI,KAAK,GAAG,QAAQ,CAAC;QAErB,OAAO,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI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,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACnE,CAAC;CACF","sourcesContent":["/**\n * The surface scanner: segment, then classify.\n *\n * Segmentation comes from the declared delimiter set alone — MM0's own\n * tokenizer rule, in `delimiters.ts` — and never consults the term or\n * notation tables. Only once a chunk's boundaries are fixed is the chunk\n * looked up: as a notation token, as a lexicon name, or as both. Calgary's\n * `A` is at once a spelling of ∀ and the predicate letter A, so the scanner\n * reports both readings and the parser picks by backtracking.\n *\n * The order is the whole point. Under the character-level maximal munch\n * this replaces, declaring a term could silently re-read an existing\n * string: adding `term ab: tm;` to Magnus turned `Fab` from `F` of `a` and\n * `b` into `F` of `ab`. Boundaries fixed before any lookup cannot do that,\n * which is exactly what MM0 buys by keeping `delimiter` separate from\n * `notation`.\n *\n * The delimiter set is a parameter: student input is read under the spec's\n * `surfaceDelimiters`, and this library's own engine-mode output under the\n * theory's own `delimiters`.\n */\n\nimport {\n chunkAt,\n type DelimiterRules,\n type DelimiterSet,\n delimiterRules,\n segment,\n} from \"./delimiters.js\";\nimport type { Span } from \"./diagnostics.js\";\nimport type { Spec } from \"./reader/spec.js\";\nimport { type NameRef, surfaceVocabulary } from \"./vocabulary.js\";\n\nexport type { NameRef } from \"./vocabulary.js\";\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; the chunk starts here. */\n readonly start: number;\n /** The chunk at `start`, before classification. Empty at end of input. */\n readonly chunk: string;\n /** The notation reading, then the name reading — whichever the chunk\n * answers to. Empty at end of input, or when it answers to neither. */\n readonly readings: readonly Reading[];\n readonly atEnd: boolean;\n}\n\nexport class Scanner {\n private readonly rules: DelimiterRules;\n /** Notation and grouping tokens. */\n private readonly tokens: ReadonlySet<string>;\n /** Lexicon names, each with what it refers to. */\n private readonly names: ReadonlyMap<string, NameRef>;\n\n constructor(spec: Spec, delimiters: DelimiterSet = spec.surfaceDelimiters) {\n const { names, tokens } = surfaceVocabulary(spec);\n\n this.rules = delimiterRules(delimiters);\n this.names = names;\n this.tokens = tokens;\n }\n\n /**\n * The chunk at `position` in `text`, after skipping whitespace, and what\n * it can be read as: the notation token first, then the lexicon 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, chunk: \"\", readings: [], atEnd: true };\n }\n\n const chunk = chunkAt(text, start, this.rules);\n const readings: Reading[] = [];\n\n if (this.tokens.has(chunk)) {\n readings.push({ kind: \"token\", token: chunk, length: chunk.length });\n }\n\n const ref = this.names.get(chunk);\n\n if (ref !== undefined) {\n readings.push({ kind: \"name\", name: chunk, ref, length: chunk.length });\n }\n\n return { start, chunk, readings, atEnd: false };\n }\n\n /**\n * The chunk sequence of `text`, before any classification — the view\n * that shows what the delimiters did, independently of what the spec's\n * vocabulary happens to contain.\n */\n chunks(text: string): string[] {\n return segment(text, this.rules);\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"]}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The surface vocabulary a spec declares: the tokens and the lexicon names
3
+ * a chunk of input can be classified as.
4
+ *
5
+ * Derived, never annotated. The tokens are the constants of every notation
6
+ * plus the grouping brackets. The names are every token in a sort's `@vars`
7
+ * pool, and every declared term that has no notation of its own, binds
8
+ * nothing, and is not a coercion — a term *with* a notation is spelled by
9
+ * that notation; a term without one is spelled by its name, which is MM0's
10
+ * own application rule read character-level.
11
+ *
12
+ * It lives apart from the scanner because the spec reader needs it too: a
13
+ * declared delimiter that names nothing here is an authoring error, and a
14
+ * lexicon name that the delimiters would split is unreachable.
15
+ */
16
+ import type { Spec } from "./reader/spec.js";
17
+ export type NameRef = {
18
+ readonly kind: "term";
19
+ readonly term: string;
20
+ } | {
21
+ readonly kind: "var";
22
+ readonly sort: string;
23
+ };
24
+ export interface Vocabulary {
25
+ /** Lexicon names, and what each refers to. */
26
+ readonly names: ReadonlyMap<string, NameRef>;
27
+ /** Notation constants and grouping brackets. */
28
+ readonly tokens: ReadonlySet<string>;
29
+ }
30
+ export declare function surfaceVocabulary(spec: Spec): Vocabulary;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The surface vocabulary a spec declares: the tokens and the lexicon names
3
+ * a chunk of input can be classified as.
4
+ *
5
+ * Derived, never annotated. The tokens are the constants of every notation
6
+ * plus the grouping brackets. The names are every token in a sort's `@vars`
7
+ * pool, and every declared term that has no notation of its own, binds
8
+ * nothing, and is not a coercion — a term *with* a notation is spelled by
9
+ * that notation; a term without one is spelled by its name, which is MM0's
10
+ * own application rule read character-level.
11
+ *
12
+ * It lives apart from the scanner because the spec reader needs it too: a
13
+ * declared delimiter that names nothing here is an authoring error, and a
14
+ * lexicon name that the delimiters would split is unreachable.
15
+ */
16
+ export function surfaceVocabulary(spec) {
17
+ const tokens = new Set();
18
+ const notated = new Set();
19
+ for (const notation of spec.notations) {
20
+ notated.add(notation.term);
21
+ if (notation.form === "simple") {
22
+ tokens.add(notation.token);
23
+ }
24
+ else {
25
+ for (const literal of notation.literals) {
26
+ if (literal.kind === "constant") {
27
+ tokens.add(literal.token);
28
+ }
29
+ }
30
+ }
31
+ }
32
+ for (const [open, close] of spec.groupingPairs) {
33
+ tokens.add(open);
34
+ tokens.add(close);
35
+ }
36
+ const coercions = new Set(spec.coercions.map((c) => c.name));
37
+ const names = new Map();
38
+ for (const sort of spec.sorts.values()) {
39
+ for (const token of sort.vars) {
40
+ names.set(token, { kind: "var", sort: sort.name });
41
+ }
42
+ }
43
+ for (const term of spec.terms.values()) {
44
+ if (!notated.has(term.name) &&
45
+ !coercions.has(term.name) &&
46
+ !term.binders.some((binder) => binder.binds) &&
47
+ !names.has(term.name)) {
48
+ names.set(term.name, { kind: "term", term: term.name });
49
+ }
50
+ }
51
+ return { names, tokens };
52
+ }
53
+ //# sourceMappingURL=vocabulary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocabulary.js","sourceRoot":"","sources":["../src/vocabulary.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAeH,MAAM,UAAU,iBAAiB,CAAC,IAAU;IAC1C,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IACjC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAElC,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACtC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE3B,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC/B,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;gBACxC,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;oBAChC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBAC5B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;QAC/C,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACjB,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7D,MAAM,KAAK,GAAG,IAAI,GAAG,EAAmB,CAAC;IAEzC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;QACvC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YAC9B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QACrD,CAAC;IACH,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;QACvC,IACE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;YACvB,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;YACzB,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;YAC5C,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EACrB,CAAC;YACD,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;AAC3B,CAAC","sourcesContent":["/**\n * The surface vocabulary a spec declares: the tokens and the lexicon names\n * a chunk of input can be classified as.\n *\n * Derived, never annotated. The tokens are the constants of every notation\n * plus the grouping brackets. The names are every token in a sort's `@vars`\n * pool, and every declared term that has no notation of its own, binds\n * nothing, and is not a coercion — a term *with* a notation is spelled by\n * that notation; a term without one is spelled by its name, which is MM0's\n * own application rule read character-level.\n *\n * It lives apart from the scanner because the spec reader needs it too: a\n * declared delimiter that names nothing here is an authoring error, and a\n * lexicon name that the delimiters would split is unreachable.\n */\n\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 interface Vocabulary {\n /** Lexicon names, and what each refers to. */\n readonly names: ReadonlyMap<string, NameRef>;\n /** Notation constants and grouping brackets. */\n readonly tokens: ReadonlySet<string>;\n}\n\nexport function surfaceVocabulary(spec: Spec): Vocabulary {\n const tokens = 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 tokens.add(notation.token);\n } else {\n for (const literal of notation.literals) {\n if (literal.kind === \"constant\") {\n tokens.add(literal.token);\n }\n }\n }\n }\n\n for (const [open, close] of spec.groupingPairs) {\n tokens.add(open);\n tokens.add(close);\n }\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 return { names, tokens };\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
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.",
3
+ "version": "0.0.2",
4
+ "description": "Surface syntax over MM0: a statement reader, a delimiter scanner, a faithful math-string parser, a pretty-printer, and a bidirectional token-elaboration layer for textbook logical notations.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",