@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.
@@ -1 +1 @@
1
- {"version":3,"file":"annotations.js","sourceRoot":"","sources":["../../src/reader/annotations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAmB,UAAU,EAAa,MAAM,mBAAmB,CAAC;AAE3E,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,0BAA0B;IAC1B,kBAAkB;CACV,CAAC;AA2EX,MAAM,SAAS,GAA2B;IACxC,yBAAyB,EAAE,yCAAyC;IACpE,eAAe,EAAE,mCAAmC;IACpD,mBAAmB,EACjB,yDAAyD;IAC3D,qBAAqB,EAAE,gDAAgD;IACvE,mBAAmB,EAAE,2CAA2C;IAChE,kBAAkB,EAChB,qEAAqE;IACvE,eAAe,EACb,uEAAuE;IACzE,0BAA0B,EAAE,uCAAuC;IACnE,2BAA2B,EAAE,wCAAwC;IACrE,eAAe,EAAE,kCAAkC;CACpD,CAAC;AAEF,SAAS,IAAI,CACX,EAAU,EACV,MAA8B,EAC9B,IAAU;IAEV,MAAM,QAAQ,GAAG,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;IACrC,OAAO;QACL,UAAU,EAAE,IAAI;QAChB,UAAU,EAAE,UAAU,CAAC,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC;KACnD,CAAC;AACJ,CAAC;AAED,SAAS,EAAE,CAAC,UAA4B;IAItC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;AAC1C,CAAC;AAMD,SAAS,UAAU,CACjB,KAAwB;IAExB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACjD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,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C,CAAC;YAC3C,CAAC;YAED,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QACpC,CAAC;QAED;YACE,OAAO,IAAI,CACT,2BAA2B,EAC3B,EAAE,UAAU,EAAE,UAAU,IAAI,QAAQ,EAAE,EACtC,IAAI,CACL,CAAC;IACN,CAAC;AACH,CAAC","sourcesContent":["/**\n * The `@syntax` annotation vocabulary — the \"little extra metadata\" that\n * turns an MM0 theory into a full surface-language spec.\n *\n * Annotations ride MM0's `--|` doc-comment channel, the same one Aufbau uses\n * for `@acui`, `@congr`, `@rewrite`, and the rest. Anything that is not\n * `@syntax` is *foreign*: preserved untouched for whoever owns it, never\n * interpreted here. The library also owns stripping `@syntax` lines before a theory is\n * handed to the engine, which currently rejects annotations it does not\n * know.\n *\n * Attachment rules (checked by the spec builder, not here):\n * - `juxtaposed` attaches to a binary term whose arguments and result\n * share one sort (adjacency of that sort's expressions denotes it);\n * - `elided` attaches to a nullary term (supplied when an argument of\n * its sort is missing, dropped again when printing);\n * - `role` attaches to a `term` or `def`;\n * - everything else is spec-wide and may sit on any statement.\n *\n * The lexicon itself needs no `@syntax` at all: letters are ordinary term\n * declarations, and variables ride the engine's own `@vars` annotation.\n */\n\nimport { type Diagnostic, diagnostic, type Span } from \"../diagnostics.js\";\n\nexport const LINT_NAMES = [\n \"parenthesize-binary-only\",\n \"closed-sentences\",\n] as const;\n\nexport type LintName = (typeof LINT_NAMES)[number];\n\n/** A capture in an elab pattern: `?ts:tm+` — name, class, quantifier. */\nexport interface RuleCapture {\n readonly kind: \"capture\";\n readonly name: string;\n /** A sort name; a leaf whose sort coerces into it matches. */\n readonly class: string;\n readonly quantifier: \"\" | \"+\" | \"?\";\n}\n\nexport interface RuleLiteral {\n readonly kind: \"literal\";\n readonly token: string;\n}\n\nexport type RulePatternElement = RuleCapture | RuleLiteral;\n\n/** A capture reference in an elab template: `?ts` or joined `?ts,*`. */\nexport interface RuleReference {\n readonly kind: \"reference\";\n readonly name: string;\n /** For a `+` capture, the token to join repeats with; null joins bare. */\n readonly separator: string | null;\n}\n\nexport type RuleTemplateElement = RuleLiteral | RuleReference;\n\nexport interface ElabRule {\n readonly inputOnly: boolean;\n readonly pattern: readonly RulePatternElement[];\n readonly span: Span;\n readonly template: readonly RuleTemplateElement[];\n}\n\nexport type SyntaxAnnotation =\n | {\n readonly kind: \"assoc-none\";\n readonly prec: number;\n }\n | {\n readonly kind: \"brackets\";\n readonly pairs: readonly (readonly [string, string])[];\n }\n | {\n readonly kind: \"display-drop-outer-parens\";\n }\n | {\n readonly kind: \"display-rotate-brackets\";\n readonly pairs: readonly (readonly [string, string])[];\n }\n | {\n /** A bidirectional token rule: elaborate in, delaborate out. */\n readonly kind: \"elab\";\n readonly rule: ElabRule;\n }\n | {\n /** Supplied when an argument of its sort is missing; drops on print. */\n readonly kind: \"elided\";\n }\n | {\n /** Adjacency of this combiner's sort denotes it — `Fxy`, `ab`. */\n readonly kind: \"juxtaposed\";\n }\n | {\n readonly kind: \"lint\";\n readonly name: LintName;\n }\n | {\n readonly kind: \"role\";\n readonly role: string;\n };\n\nconst TEMPLATES: Record<string, string> = {\n syntax_unknown_subcommand: \"unknown @syntax subcommand {subcommand}\",\n syntax_bad_flag: \"@syntax {flag} takes no arguments\",\n syntax_bad_brackets:\n \"bracket pairs come as: <open> <close> [<open> <close>…]\",\n syntax_bad_assoc_none: \"@syntax assoc-none wants one precedence number\",\n syntax_unknown_lint: \"unknown lint {name}; known lints: {known}\",\n syntax_bad_display:\n \"@syntax display wants drop-outer-parens or rotate-brackets <pairs…>\",\n syntax_bad_elab:\n \"@syntax elab wants: elab $ <pattern> $ => $ <template> $ [input-only]\",\n syntax_bad_pattern_element: \"cannot read pattern element {element}\",\n syntax_bad_template_element: \"cannot read template element {element}\",\n syntax_bad_role: \"@syntax role wants one role name\",\n};\n\nfunction fail(\n id: string,\n params: Record<string, string>,\n span: Span,\n): { annotation: null; diagnostic: Diagnostic } {\n const template = TEMPLATES[id] ?? id;\n return {\n annotation: null,\n diagnostic: diagnostic(id, template, params, span),\n };\n}\n\nfunction ok(annotation: SyntaxAnnotation): {\n annotation: SyntaxAnnotation;\n diagnostic: null;\n} {\n return { annotation, diagnostic: null };\n}\n\nexport type SyntaxAnnotationResult =\n | { readonly annotation: SyntaxAnnotation; readonly diagnostic: null }\n | { readonly annotation: null; readonly diagnostic: Diagnostic };\n\nfunction parsePairs(\n words: readonly string[],\n): (readonly [string, string])[] | null {\n if (words.length === 0 || words.length % 2 !== 0) {\n return null;\n }\n\n const pairs: (readonly [string, string])[] = [];\n\n for (let i = 0; i < words.length; i += 2) {\n const open = words[i];\n const close = words[i + 1];\n\n if (open === undefined || close === undefined || open === close) {\n return null;\n }\n\n pairs.push([open, close]);\n }\n\n return pairs;\n}\n\nconst CAPTURE =\n /^\\?([A-Za-z_][A-Za-z0-9_]*):([A-Za-z_][A-Za-z0-9_]*)([+?]?)$/;\nconst REFERENCE = /^\\?([A-Za-z_][A-Za-z0-9_]*)(?:(.)\\*)?$/;\n\nfunction parsePattern(text: string): readonly RulePatternElement[] | string {\n const elements: RulePatternElement[] = [];\n\n for (const word of text.split(/\\s+/).filter((w) => w.length > 0)) {\n if (word.startsWith(\"?\")) {\n const match = CAPTURE.exec(word);\n\n if (match === null) {\n return word;\n }\n\n elements.push({\n kind: \"capture\",\n name: match[1] ?? \"\",\n class: match[2] ?? \"\",\n quantifier: (match[3] ?? \"\") as \"\" | \"+\" | \"?\",\n });\n continue;\n }\n\n elements.push({ kind: \"literal\", token: word });\n }\n\n return elements;\n}\n\nfunction parseTemplate(\n text: string,\n): readonly RuleTemplateElement[] | string {\n const elements: RuleTemplateElement[] = [];\n\n for (const word of text.split(/\\s+/).filter((w) => w.length > 0)) {\n if (word.startsWith(\"?\")) {\n const match = REFERENCE.exec(word);\n\n if (match === null) {\n return word;\n }\n\n elements.push({\n kind: \"reference\",\n name: match[1] ?? \"\",\n separator: match[2] ?? null,\n });\n continue;\n }\n\n elements.push({ kind: \"literal\", token: word });\n }\n\n return elements;\n}\n\nconst ELAB = /^\\$(.*?)\\$\\s*=>\\s*\\$(.*?)\\$\\s*(input-only)?\\s*$/s;\n\n/**\n * Parse one annotation payload (the text after `--|`). Returns null for a\n * foreign annotation — anything that does not begin `@syntax`.\n */\nexport function parseSyntaxAnnotation(\n text: string,\n span: Span,\n): SyntaxAnnotationResult | null {\n const trimmed = text.trim();\n\n if (!trimmed.startsWith(\"@syntax\")) {\n return null;\n }\n\n const rest = trimmed.slice(\"@syntax\".length).trim();\n const words = rest.split(/\\s+/).filter((w) => w.length > 0);\n const subcommand = words[0];\n\n switch (subcommand) {\n case \"juxtaposed\":\n case \"elided\": {\n if (words.length !== 1) {\n return fail(\"syntax_bad_flag\", { flag: subcommand }, span);\n }\n\n return ok({ kind: subcommand });\n }\n\n case \"brackets\": {\n const pairs = parsePairs(words.slice(1));\n\n if (pairs === null) {\n return fail(\"syntax_bad_brackets\", {}, span);\n }\n\n return ok({ kind: \"brackets\", pairs });\n }\n\n case \"assoc-none\": {\n const number = words[1];\n\n if (\n words.length !== 2 ||\n number === undefined ||\n !/^\\d+$/.test(number)\n ) {\n return fail(\"syntax_bad_assoc_none\", {}, span);\n }\n\n return ok({ kind: \"assoc-none\", prec: Number.parseInt(number, 10) });\n }\n\n case \"lint\": {\n const name = words[1];\n\n if (\n words.length !== 2 ||\n name === undefined ||\n !(LINT_NAMES as readonly string[]).includes(name)\n ) {\n return fail(\n \"syntax_unknown_lint\",\n { name: name ?? \"\", known: LINT_NAMES.join(\", \") },\n span,\n );\n }\n\n return ok({ kind: \"lint\", name: name as LintName });\n }\n\n case \"display\": {\n if (words[1] === \"drop-outer-parens\" && words.length === 2) {\n return ok({ kind: \"display-drop-outer-parens\" });\n }\n\n if (words[1] === \"rotate-brackets\") {\n const pairs = parsePairs(words.slice(2));\n\n if (pairs !== null) {\n return ok({ kind: \"display-rotate-brackets\", pairs });\n }\n }\n\n return fail(\"syntax_bad_display\", {}, span);\n }\n\n case \"elab\": {\n const match = ELAB.exec(rest.slice(\"elab\".length).trim());\n\n if (match === null) {\n return fail(\"syntax_bad_elab\", {}, span);\n }\n\n const pattern = parsePattern(match[1] ?? \"\");\n\n if (typeof pattern === \"string\") {\n return fail(\"syntax_bad_pattern_element\", { element: pattern }, span);\n }\n\n const template = parseTemplate(match[2] ?? \"\");\n\n if (typeof template === \"string\") {\n return fail(\n \"syntax_bad_template_element\",\n { element: template },\n span,\n );\n }\n\n return ok({\n kind: \"elab\",\n rule: {\n inputOnly: match[3] === \"input-only\",\n pattern,\n span,\n template,\n },\n });\n }\n\n case \"role\": {\n const role = words[1];\n\n if (words.length !== 2 || role === undefined) {\n return fail(\"syntax_bad_role\", {}, span);\n }\n\n return ok({ kind: \"role\", role });\n }\n\n default:\n return fail(\n \"syntax_unknown_subcommand\",\n { subcommand: subcommand ?? \"(none)\" },\n span,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"annotations.js","sourceRoot":"","sources":["../../src/reader/annotations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAmB,UAAU,EAAa,MAAM,mBAAmB,CAAC;AAE3E,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,0BAA0B;IAC1B,kBAAkB;CACV,CAAC;AAuFX,MAAM,SAAS,GAA2B;IACxC,yBAAyB,EAAE,yCAAyC;IACpE,eAAe,EAAE,mCAAmC;IACpD,mBAAmB,EACjB,yDAAyD;IAC3D,qBAAqB,EAAE,gDAAgD;IACvE,mBAAmB,EAAE,2CAA2C;IAChE,kBAAkB,EAChB,qEAAqE;IACvE,oBAAoB,EAClB,0FAA0F;IAC5F,sBAAsB,EAAE,mDAAmD;IAC3E,eAAe,EACb,uEAAuE;IACzE,0BAA0B,EAAE,uCAAuC;IACnE,2BAA2B,EAAE,wCAAwC;IACrE,eAAe,EAAE,kCAAkC;CACpD,CAAC;AAEF,SAAS,IAAI,CACX,EAAU,EACV,M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AAM;gBACZ,IAAI,EAAE;oBACJ,SAAS,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,YAAY;oBACpC,OAAO;oBACP,IAAI;oBACJ,QAAQ;iBACT;aACF,CAAC,CAAC;QACL,CAAC;QAED,KAAK,MAAM,CAAC,CAAC,CAAC;YACZ,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAEtB,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC7C,OAAO,IAAI,CAAC,iBAAiB,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;YAC3C,CAAC;YAED,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QACpC,CAAC;QAED;YACE,OAAO,IAAI,CACT,2BAA2B,EAC3B,EAAE,UAAU,EAAE,UAAU,IAAI,QAAQ,EAAE,EACtC,IAAI,CACL,CAAC;IACN,CAAC;AACH,CAAC","sourcesContent":["/**\n * The `@syntax` annotation vocabulary — the \"little extra metadata\" that\n * turns an MM0 theory into a full surface-language spec.\n *\n * Annotations ride MM0's `--|` doc-comment channel, the same one Aufbau uses\n * for `@acui`, `@congr`, `@rewrite`, and the rest. Anything that is not\n * `@syntax` is *foreign*: preserved untouched for whoever owns it, never\n * interpreted here. The library also owns stripping `@syntax` lines before a theory is\n * handed to the engine, which currently rejects annotations it does not\n * know.\n *\n * Attachment rules (checked by the spec builder, not here):\n * - `juxtaposed` attaches to a binary term whose arguments and result\n * share one sort (adjacency of that sort's expressions denotes it);\n * - `elided` attaches to a nullary term (supplied when an argument of\n * its sort is missing, dropped again when printing);\n * - `role` attaches to a `term` or `def`;\n * - everything else is spec-wide and may sit on any statement.\n *\n * The lexicon itself needs no `@syntax` at all: letters are ordinary term\n * declarations, and variables ride the engine's own `@vars` annotation.\n */\n\nimport { type Diagnostic, diagnostic, type Span } from \"../diagnostics.js\";\n\nexport const LINT_NAMES = [\n \"parenthesize-binary-only\",\n \"closed-sentences\",\n] as const;\n\nexport type LintName = (typeof LINT_NAMES)[number];\n\n/** A capture in an elab pattern: `?ts:tm+` — name, class, quantifier. */\nexport interface RuleCapture {\n readonly kind: \"capture\";\n readonly name: string;\n /** A sort name; a leaf whose sort coerces into it matches. */\n readonly class: string;\n readonly quantifier: \"\" | \"+\" | \"?\";\n}\n\nexport interface RuleLiteral {\n readonly kind: \"literal\";\n readonly token: string;\n}\n\nexport type RulePatternElement = RuleCapture | RuleLiteral;\n\n/** A capture reference in an elab template: `?ts` or joined `?ts,*`. */\nexport interface RuleReference {\n readonly kind: \"reference\";\n readonly name: string;\n /** For a `+` capture, the token to join repeats with; null joins bare. */\n readonly separator: string | null;\n}\n\nexport type RuleTemplateElement = RuleLiteral | RuleReference;\n\nexport interface ElabRule {\n readonly inputOnly: boolean;\n readonly pattern: readonly RulePatternElement[];\n readonly span: Span;\n readonly template: readonly RuleTemplateElement[];\n}\n\nexport type SyntaxAnnotation =\n | {\n readonly kind: \"assoc-none\";\n readonly prec: number;\n }\n | {\n readonly kind: \"brackets\";\n readonly pairs: readonly (readonly [string, string])[];\n }\n | {\n readonly kind: \"display-drop-outer-parens\";\n }\n | {\n readonly kind: \"display-rotate-brackets\";\n readonly pairs: readonly (readonly [string, string])[];\n }\n | {\n /** A bidirectional token rule: elaborate in, delaborate out. */\n readonly kind: \"elab\";\n readonly rule: ElabRule;\n }\n | {\n /**\n * Surface delimiters: where a chunk of *student* input ends. Same\n * shape and same meaning as MM0's own `delimiter` statement, and\n * unioned with it — but an entry may be any string, not just a single\n * byte, so `∧` and `<->` can self-delimit where the engine's\n * byte table cannot hold them.\n */\n readonly kind: \"delimiter\";\n readonly left: readonly string[];\n readonly right: readonly string[];\n }\n | {\n /** Supplied when an argument of its sort is missing; drops on print. */\n readonly kind: \"elided\";\n }\n | {\n /** Adjacency of this combiner's sort denotes it — `Fxy`, `ab`. */\n readonly kind: \"juxtaposed\";\n }\n | {\n readonly kind: \"lint\";\n readonly name: LintName;\n }\n | {\n readonly kind: \"role\";\n readonly role: string;\n };\n\nconst TEMPLATES: Record<string, string> = {\n syntax_unknown_subcommand: \"unknown @syntax subcommand {subcommand}\",\n syntax_bad_flag: \"@syntax {flag} takes no arguments\",\n syntax_bad_brackets:\n \"bracket pairs come as: <open> <close> [<open> <close>…]\",\n syntax_bad_assoc_none: \"@syntax assoc-none wants one precedence number\",\n syntax_unknown_lint: \"unknown lint {name}; known lints: {known}\",\n syntax_bad_display:\n \"@syntax display wants drop-outer-parens or rotate-brackets <pairs…>\",\n syntax_bad_delimiter:\n \"@syntax delimiter wants: delimiter $ <entries…> $, or delimiter $ <left…> $ $ <right…> $\",\n syntax_empty_delimiter: \"a @syntax delimiter math string must list entries\",\n syntax_bad_elab:\n \"@syntax elab wants: elab $ <pattern> $ => $ <template> $ [input-only]\",\n syntax_bad_pattern_element: \"cannot read pattern element {element}\",\n syntax_bad_template_element: \"cannot read template element {element}\",\n syntax_bad_role: \"@syntax role wants one role name\",\n};\n\nfunction fail(\n id: string,\n params: Record<string, string>,\n span: Span,\n): { annotation: null; diagnostic: Diagnostic } {\n const template = TEMPLATES[id] ?? id;\n return {\n annotation: null,\n diagnostic: diagnostic(id, template, params, span),\n };\n}\n\nfunction ok(annotation: SyntaxAnnotation): {\n annotation: SyntaxAnnotation;\n diagnostic: null;\n} {\n return { annotation, diagnostic: null };\n}\n\nexport type SyntaxAnnotationResult =\n | { readonly annotation: SyntaxAnnotation; readonly diagnostic: null }\n | { readonly annotation: null; readonly diagnostic: Diagnostic };\n\nfunction parsePairs(\n words: readonly string[],\n): (readonly [string, string])[] | null {\n if (words.length === 0 || words.length % 2 !== 0) {\n return null;\n }\n\n const pairs: (readonly [string, string])[] = [];\n\n for (let i = 0; i < words.length; i += 2) {\n const open = words[i];\n const close = words[i + 1];\n\n if (open === undefined || close === undefined || open === close) {\n return null;\n }\n\n pairs.push([open, close]);\n }\n\n return pairs;\n}\n\nconst CAPTURE =\n /^\\?([A-Za-z_][A-Za-z0-9_]*):([A-Za-z_][A-Za-z0-9_]*)([+?]?)$/;\nconst REFERENCE = /^\\?([A-Za-z_][A-Za-z0-9_]*)(?:(.)\\*)?$/;\n\nfunction parsePattern(text: string): readonly RulePatternElement[] | string {\n const elements: RulePatternElement[] = [];\n\n for (const word of text.split(/\\s+/).filter((w) => w.length > 0)) {\n if (word.startsWith(\"?\")) {\n const match = CAPTURE.exec(word);\n\n if (match === null) {\n return word;\n }\n\n elements.push({\n kind: \"capture\",\n name: match[1] ?? \"\",\n class: match[2] ?? \"\",\n quantifier: (match[3] ?? \"\") as \"\" | \"+\" | \"?\",\n });\n continue;\n }\n\n elements.push({ kind: \"literal\", token: word });\n }\n\n return elements;\n}\n\nfunction parseTemplate(\n text: string,\n): readonly RuleTemplateElement[] | string {\n const elements: RuleTemplateElement[] = [];\n\n for (const word of text.split(/\\s+/).filter((w) => w.length > 0)) {\n if (word.startsWith(\"?\")) {\n const match = REFERENCE.exec(word);\n\n if (match === null) {\n return word;\n }\n\n elements.push({\n kind: \"reference\",\n name: match[1] ?? \"\",\n separator: match[2] ?? null,\n });\n continue;\n }\n\n elements.push({ kind: \"literal\", token: word });\n }\n\n return elements;\n}\n\nconst ELAB = /^\\$(.*?)\\$\\s*=>\\s*\\$(.*?)\\$\\s*(input-only)?\\s*$/s;\n\n/** `$ … $`, or `$ left… $ $ right… $` — MM0's own two shapes. */\nconst DELIMITER = /^\\$([^$]*)\\$(?:\\s*\\$([^$]*)\\$)?\\s*$/;\n\n/**\n * Every entry written out, one at a time. There is deliberately no range\n * shorthand: a spike of `A-Z`-style ranges read `<->` as the range `<`…`>`\n * and silently turned one delimiter into three. A declaration that decides\n * how input is cut up is worth its verbosity.\n */\nfunction delimiterEntries(text: string): string[] {\n return text.split(/\\s+/).filter((entry) => entry.length > 0);\n}\n\n/**\n * Parse one annotation payload (the text after `--|`). Returns null for a\n * foreign annotation — anything that does not begin `@syntax`.\n */\nexport function parseSyntaxAnnotation(\n text: string,\n span: Span,\n): SyntaxAnnotationResult | null {\n const trimmed = text.trim();\n\n if (!trimmed.startsWith(\"@syntax\")) {\n return null;\n }\n\n const rest = trimmed.slice(\"@syntax\".length).trim();\n const words = rest.split(/\\s+/).filter((w) => w.length > 0);\n const subcommand = words[0];\n\n switch (subcommand) {\n case \"juxtaposed\":\n case \"elided\": {\n if (words.length !== 1) {\n return fail(\"syntax_bad_flag\", { flag: subcommand }, span);\n }\n\n return ok({ kind: subcommand });\n }\n\n case \"brackets\": {\n const pairs = parsePairs(words.slice(1));\n\n if (pairs === null) {\n return fail(\"syntax_bad_brackets\", {}, span);\n }\n\n return ok({ kind: \"brackets\", pairs });\n }\n\n case \"delimiter\": {\n const match = DELIMITER.exec(rest.slice(\"delimiter\".length).trim());\n\n if (match === null) {\n return fail(\"syntax_bad_delimiter\", {}, span);\n }\n\n const first = delimiterEntries(match[1] ?? \"\");\n const second =\n match[2] === undefined ? null : delimiterEntries(match[2]);\n\n if (first.length === 0 || second?.length === 0) {\n return fail(\"syntax_empty_delimiter\", {}, span);\n }\n\n // One list means both sides, as in MM0.\n return ok({ kind: \"delimiter\", left: first, right: second ?? first });\n }\n\n case \"assoc-none\": {\n const number = words[1];\n\n if (\n words.length !== 2 ||\n number === undefined ||\n !/^\\d+$/.test(number)\n ) {\n return fail(\"syntax_bad_assoc_none\", {}, span);\n }\n\n return ok({ kind: \"assoc-none\", prec: Number.parseInt(number, 10) });\n }\n\n case \"lint\": {\n const name = words[1];\n\n if (\n words.length !== 2 ||\n name === undefined ||\n !(LINT_NAMES as readonly string[]).includes(name)\n ) {\n return fail(\n \"syntax_unknown_lint\",\n { name: name ?? \"\", known: LINT_NAMES.join(\", \") },\n span,\n );\n }\n\n return ok({ kind: \"lint\", name: name as LintName });\n }\n\n case \"display\": {\n if (words[1] === \"drop-outer-parens\" && words.length === 2) {\n return ok({ kind: \"display-drop-outer-parens\" });\n }\n\n if (words[1] === \"rotate-brackets\") {\n const pairs = parsePairs(words.slice(2));\n\n if (pairs !== null) {\n return ok({ kind: \"display-rotate-brackets\", pairs });\n }\n }\n\n return fail(\"syntax_bad_display\", {}, span);\n }\n\n case \"elab\": {\n const match = ELAB.exec(rest.slice(\"elab\".length).trim());\n\n if (match === null) {\n return fail(\"syntax_bad_elab\", {}, span);\n }\n\n const pattern = parsePattern(match[1] ?? \"\");\n\n if (typeof pattern === \"string\") {\n return fail(\"syntax_bad_pattern_element\", { element: pattern }, span);\n }\n\n const template = parseTemplate(match[2] ?? \"\");\n\n if (typeof template === \"string\") {\n return fail(\n \"syntax_bad_template_element\",\n { element: template },\n span,\n );\n }\n\n return ok({\n kind: \"elab\",\n rule: {\n inputOnly: match[3] === \"input-only\",\n pattern,\n span,\n template,\n },\n });\n }\n\n case \"role\": {\n const role = words[1];\n\n if (words.length !== 2 || role === undefined) {\n return fail(\"syntax_bad_role\", {}, span);\n }\n\n return ok({ kind: \"role\", role });\n }\n\n default:\n return fail(\n \"syntax_unknown_subcommand\",\n { subcommand: subcommand ?? \"(none)\" },\n span,\n );\n }\n}\n"]}
@@ -7,6 +7,7 @@
7
7
  * dangling names, non-invertible elab rules); whether the theory proves
8
8
  * anything is the engine's business, and axiom bodies pass through opaque.
9
9
  */
10
+ import { type DelimiterSet } from "../delimiters.js";
10
11
  import { type Diagnostic, type Span } from "../diagnostics.js";
11
12
  import { type ElabRule, type LintName } from "./annotations.js";
12
13
  import { type Annotation, type Binder, type NotationLiteral, type Precedence, type Statement } from "./statements.js";
@@ -62,10 +63,11 @@ export type NotationInfo = {
62
63
  export interface Spec {
63
64
  readonly assocNone: ReadonlySet<number>;
64
65
  readonly coercions: readonly CoercionInfo[];
65
- readonly delimiters: {
66
- readonly left: ReadonlySet<string>;
67
- readonly right: ReadonlySet<string>;
68
- };
66
+ /**
67
+ * The theory's own `delimiter` statement — how the *engine* cuts up math
68
+ * strings. Single bytes, because that is all the engine's table holds.
69
+ */
70
+ readonly delimiters: DelimiterSet;
69
71
  readonly display: {
70
72
  readonly dropOuterParens: boolean;
71
73
  readonly rotateBrackets: readonly (readonly [string, string])[] | null;
@@ -79,6 +81,14 @@ export interface Spec {
79
81
  readonly sorts: ReadonlyMap<string, SortInfo>;
80
82
  /** Every statement, in order, annotations intact — full fidelity. */
81
83
  readonly statements: readonly Statement[];
84
+ /**
85
+ * How *student* input is cut up: `delimiters` unioned with every
86
+ * `@syntax delimiter` the spec declares. A spec that declares none gets
87
+ * the theory's set unchanged — so writing textbook notation tight
88
+ * (`~~P`, `Fxy`) is an explicit opt-in, never something a spec acquires
89
+ * by accident.
90
+ */
91
+ readonly surfaceDelimiters: DelimiterSet;
82
92
  readonly terms: ReadonlyMap<string, TermInfo>;
83
93
  }
84
94
  export interface SpecParse {
@@ -7,7 +7,9 @@
7
7
  * dangling names, non-invertible elab rules); whether the theory proves
8
8
  * anything is the engine's business, and axiom bodies pass through opaque.
9
9
  */
10
- import { diagnostic } from "../diagnostics.js";
10
+ import { delimiterRules, isReachableChunk, segment, } from "../delimiters.js";
11
+ import { diagnostic, } from "../diagnostics.js";
12
+ import { surfaceVocabulary } from "../vocabulary.js";
11
13
  import { parseSyntaxAnnotation, } from "./annotations.js";
12
14
  import { parseStatements, } from "./statements.js";
13
15
  const TEMPLATES = {
@@ -29,9 +31,12 @@ const TEMPLATES = {
29
31
  elided_duplicate: "sort {sort} already has an elided term",
30
32
  vars_term_conflict: "@vars token {token} is also a declared term; a name can be only one",
31
33
  role_target: "@syntax role must sit on a term",
34
+ delimiter_unknown: "delimiter {token} is neither a notation token, a bracket, nor a lexicon name, so no input can ever be read as it",
35
+ delimiter_token_not_delimited: "{token} is not a surface delimiter; it is only read when whitespace or a delimiter bounds it, so it will run into an adjacent token",
36
+ delimiter_unreachable_name: "the delimiters split {name} into {chunks}, so it can never be read as one name",
32
37
  };
33
- function report(diagnostics, id, params, span) {
34
- diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span));
38
+ function report(diagnostics, id, params, span, severity = "error") {
39
+ diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span, severity));
35
40
  }
36
41
  /** The binder count an application must fill (bound and regular alike). */
37
42
  function regularBinders(binders) {
@@ -58,6 +63,76 @@ function meaningOf(notation) {
58
63
  }
59
64
  return { key: "general", term: notation.term };
60
65
  }
66
+ /** Where each notation constant was declared, for pointing a diagnostic. */
67
+ function tokenSpans(spec) {
68
+ const spans = new Map();
69
+ for (const notation of spec.notations) {
70
+ const constants = notation.form === "simple"
71
+ ? [notation.token]
72
+ : notation.literals
73
+ .filter((literal) => literal.kind === "constant")
74
+ .map((literal) => literal.token);
75
+ for (const token of constants) {
76
+ if (!spans.has(token)) {
77
+ spans.set(token, notation.span);
78
+ }
79
+ }
80
+ }
81
+ return spans;
82
+ }
83
+ /**
84
+ * The surface delimiter set, checked against the vocabulary it has to cut
85
+ * up. Only runs when the spec declares `@syntax delimiter`: a spec that
86
+ * declares none is read under the theory's own delimiters, where tokens
87
+ * are whitespace-separated and none of this can bite.
88
+ */
89
+ function checkSurfaceDelimiters(spec, declarations, diagnostics) {
90
+ const fallback = declarations[0]?.span;
91
+ if (fallback === undefined) {
92
+ return;
93
+ }
94
+ const vocabulary = surfaceVocabulary(spec);
95
+ const rules = delimiterRules(spec.surfaceDelimiters);
96
+ const declared = new Set([
97
+ ...spec.surfaceDelimiters.left,
98
+ ...spec.surfaceDelimiters.right,
99
+ ]);
100
+ // A delimiter nothing can be classified as cuts input into pieces the
101
+ // parser must then reject — a boundary with no meaning behind it.
102
+ for (const { entries, span } of declarations) {
103
+ for (const entry of entries) {
104
+ if (!vocabulary.tokens.has(entry) && !vocabulary.names.has(entry)) {
105
+ report(diagnostics, "delimiter_unknown", { token: entry }, span);
106
+ }
107
+ }
108
+ }
109
+ // A token that is not itself a delimiter still reads when whitespace or a
110
+ // delimiter bounds it — `P->Q` is fine with the letters declared — so this
111
+ // is advice, not breakage. What it warns about is two such tokens
112
+ // meeting: `->~` is one chunk unless one of them delimits.
113
+ const spans = tokenSpans(spec);
114
+ for (const token of vocabulary.tokens) {
115
+ if (!declared.has(token)) {
116
+ report(diagnostics, "delimiter_token_not_delimited", { token }, spans.get(token) ?? fallback, "warning");
117
+ }
118
+ }
119
+ // A name the delimiters split apart is unreachable: segmentation happens
120
+ // before anything knows the name exists, so no bracketing recovers it.
121
+ // Elided terms are exempt — they are supplied by the parser and dropped
122
+ // by the printer, never typed.
123
+ for (const [name, ref] of vocabulary.names) {
124
+ if (ref.kind === "term" && spec.terms.get(ref.term)?.elided === true) {
125
+ continue;
126
+ }
127
+ if (isReachableChunk(name, rules)) {
128
+ continue;
129
+ }
130
+ const span = ref.kind === "term"
131
+ ? spec.terms.get(ref.term)?.span
132
+ : spec.sorts.get(ref.sort)?.span;
133
+ report(diagnostics, "delimiter_unreachable_name", { chunks: segment(name, rules).join(" "), name }, span ?? fallback);
134
+ }
135
+ }
61
136
  export function parseSpec(source) {
62
137
  const { statements, diagnostics: parseDiagnostics } = parseStatements(source);
63
138
  const diagnostics = [...parseDiagnostics];
@@ -70,6 +145,9 @@ export function parseSpec(source) {
70
145
  const assocNone = new Set();
71
146
  const delimitersLeft = new Set();
72
147
  const delimitersRight = new Set();
148
+ const surfaceLeft = new Set();
149
+ const surfaceRight = new Set();
150
+ const surfaceDeclarations = [];
73
151
  const groupingPairs = [["(", ")"]];
74
152
  let dropOuterParens = false;
75
153
  let rotateBrackets = null;
@@ -269,6 +347,19 @@ export function parseSpec(source) {
269
347
  }
270
348
  break;
271
349
  }
350
+ case "delimiter": {
351
+ for (const entry of annotation.left) {
352
+ surfaceLeft.add(entry);
353
+ }
354
+ for (const entry of annotation.right) {
355
+ surfaceRight.add(entry);
356
+ }
357
+ surfaceDeclarations.push({
358
+ entries: [...new Set([...annotation.left, ...annotation.right])],
359
+ span,
360
+ });
361
+ break;
362
+ }
272
363
  case "assoc-none": {
273
364
  assocNone.add(annotation.prec);
274
365
  break;
@@ -403,21 +494,25 @@ export function parseSpec(source) {
403
494
  }
404
495
  }
405
496
  }
406
- return {
407
- diagnostics,
408
- spec: {
409
- assocNone,
410
- coercions,
411
- delimiters: { left: delimitersLeft, right: delimitersRight },
412
- display: { dropOuterParens, rotateBrackets },
413
- groupingPairs,
414
- lints,
415
- notations,
416
- elabRules,
417
- sorts,
418
- statements,
419
- terms,
497
+ const spec = {
498
+ assocNone,
499
+ coercions,
500
+ delimiters: { left: delimitersLeft, right: delimitersRight },
501
+ display: { dropOuterParens, rotateBrackets },
502
+ groupingPairs,
503
+ lints,
504
+ notations,
505
+ elabRules,
506
+ sorts,
507
+ statements,
508
+ // The union: what the engine splits on, plus what the textbook does.
509
+ surfaceDelimiters: {
510
+ left: new Set([...delimitersLeft, ...surfaceLeft]),
511
+ right: new Set([...delimitersRight, ...surfaceRight]),
420
512
  },
513
+ terms,
421
514
  };
515
+ checkSurfaceDelimiters(spec, surfaceDeclarations, diagnostics);
516
+ return { diagnostics, spec };
422
517
  }
423
518
  //# sourceMappingURL=spec.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"spec.js","sourceRoot":"","sources":["../../src/reader/spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAmB,UAAU,EAAa,MAAM,mBAAmB,CAAC;AAC3E,OAAO,EAGL,qBAAqB,GAEtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAKL,eAAe,GAEhB,MAAM,iBAAiB,CAAC;AAqFzB,MAAM,SAAS,GAA2B;IACxC,qBAAqB,EAAE,4BAA4B;IACnD,YAAY,EAAE,6BAA6B;IAC3C,YAAY,EAAE,6BAA6B;IAC3C,WAAW,EAAE,uDAAuD;IACpE,yBAAyB,EACvB,kEAAkE;IACpE,cAAc,EACZ,uEAAuE;IACzE,8BAA8B,EAC5B,qDAAqD;IACvD,uBAAuB,EACrB,mEAAmE;IACrE,sBAAsB,EAAE,6CAA6C;IACrE,sBAAsB,EACpB,oEAAoE;IACtE,mBAAmB,EACjB,uGAAuG;IACzG,iBAAiB,EACf,wFAAwF;IAC1F,yBAAyB,EACvB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B,IAAI,CAAC,IAAI,CACV,CAAC;oBACF,SAAS;gBACX,CAAC;gBAED,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,QAAQ,EAAE,CAAC;gBACrC,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;oBAChB,MAAM,CAAC,WAAW,EAAE,qBAAqB,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;gBAClE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,WAAW;QACX,IAAI,EAAE;YACJ,SAAS;YACT,SAAS;YACT,UAAU,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,eAAe,EAAE;YAC5D,OAAO,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE;YAC5C,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;YACT,KAAK;YACL,UAAU;YACV,KAAK;SACN;KACF,CAAC;AACJ,CAAC","sourcesContent":["/**\n * From parsed statements to a `Spec` — the library's central object: the\n * signature, notations, coercions, and `@syntax` surface metadata of one\n * logical system, validated.\n *\n * Validation here is about the spec being *coherent* (token conflicts,\n * dangling names, non-invertible elab rules); whether the theory proves\n * anything is the engine's business, and axiom bodies pass through opaque.\n */\n\nimport { type Diagnostic, diagnostic, type Span } from \"../diagnostics.js\";\nimport {\n type ElabRule,\n type LintName,\n parseSyntaxAnnotation,\n type SyntaxAnnotation,\n} from \"./annotations.js\";\nimport {\n type Annotation,\n type Binder,\n type NotationLiteral,\n type Precedence,\n parseStatements,\n type Statement,\n} from \"./statements.js\";\n\nexport interface SortInfo {\n readonly foreignAnnotations: readonly Annotation[];\n readonly modifiers: readonly string[];\n readonly name: string;\n readonly span: Span;\n /**\n * The sort's variable tokens, read from the engine's own `@vars`\n * annotation (which stays in `foreignAnnotations` untouched — it is the\n * engine's, we only listen in). These are the surface lexicon's\n * identifiers of this sort; whether a quantifier can bind them falls\n * out of the quantifiers' binder sorts, not from here.\n */\n readonly vars: readonly string[];\n}\n\nexport interface TermInfo {\n readonly binders: readonly Binder[];\n /** `@syntax elided`: supplied when an argument of its sort is missing. */\n readonly elided: boolean;\n readonly foreignAnnotations: readonly Annotation[];\n /** True when this came from a `def` rather than a `term`. */\n readonly isDef: boolean;\n /** `@syntax juxtaposed`: adjacency of its sort's leaves denotes it. */\n readonly juxtaposed: boolean;\n readonly name: string;\n readonly returnSort: string;\n readonly roles: readonly string[];\n readonly span: Span;\n}\n\nexport interface CoercionInfo {\n readonly from: string;\n readonly name: string;\n readonly span: Span;\n readonly to: string;\n}\n\nexport type NotationInfo =\n | {\n readonly form: \"general\";\n /** The notation statement's own binders, which its variables name. */\n readonly binders: readonly Binder[];\n readonly literals: readonly NotationLiteral[];\n readonly span: Span;\n readonly term: string;\n }\n | {\n readonly form: \"simple\";\n readonly fixity: \"infixl\" | \"infixr\" | \"prefix\";\n readonly prec: Precedence;\n readonly span: Span;\n readonly term: string;\n readonly token: string;\n };\n\nexport interface Spec {\n readonly assocNone: ReadonlySet<number>;\n readonly coercions: readonly CoercionInfo[];\n readonly delimiters: {\n readonly left: ReadonlySet<string>;\n readonly right: ReadonlySet<string>;\n };\n readonly display: {\n readonly dropOuterParens: boolean;\n readonly rotateBrackets: readonly (readonly [string, string])[] | null;\n };\n /** Always starts with the built-in `(\"(\", \")\")` pair. */\n readonly groupingPairs: readonly (readonly [string, string])[];\n readonly lints: readonly LintName[];\n /** In declaration order; the last notation for a term is canonical. */\n readonly notations: readonly NotationInfo[];\n readonly elabRules: readonly ElabRule[];\n readonly sorts: ReadonlyMap<string, SortInfo>;\n /** Every statement, in order, annotations intact — full fidelity. */\n readonly statements: readonly Statement[];\n readonly terms: ReadonlyMap<string, TermInfo>;\n}\n\nexport interface SpecParse {\n readonly diagnostics: readonly Diagnostic[];\n readonly spec: Spec;\n}\n\nconst TEMPLATES: Record<string, string> = {\n duplicate_declaration: \"{name} is already declared\",\n unknown_sort: \"sort {name} is not declared\",\n unknown_term: \"term {name} is not declared\",\n infix_arity: \"an infix operator needs exactly two regular arguments\",\n unknown_notation_variable:\n \"notation variable {name} is not one of the declaration's binders\",\n token_conflict:\n \"token {token} already has a meaning; a token may carry one parse rule\",\n precedence_mixed_associativity:\n \"precedence {prec} mixes infixl and infixr operators\",\n grouping_token_conflict:\n \"{token} is a grouping bracket and cannot also be a notation token\",\n elab_duplicate_capture: \"capture {name} appears twice in the pattern\",\n elab_unknown_reference:\n \"the template references {name}, which the pattern does not capture\",\n elab_not_invertible:\n \"capture {name} is not used exactly once in the template; mark the rule input-only if that is intended\",\n juxtaposed_target:\n \"@syntax juxtaposed must sit on a binary term whose arguments and result share one sort\",\n juxtaposed_needs_notation:\n \"a juxtaposed term needs a declared notation, so the engine can read it\",\n juxtaposed_duplicate: \"sort {sort} already has a juxtaposed combiner\",\n elided_target: \"@syntax elided must sit on a term with no arguments\",\n elided_duplicate: \"sort {sort} already has an elided term\",\n vars_term_conflict:\n \"@vars token {token} is also a declared term; a name can be only one\",\n role_target: \"@syntax role must sit on a term\",\n};\n\nfunction report(\n diagnostics: Diagnostic[],\n id: string,\n params: Record<string, string>,\n span: Span,\n): void {\n diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span));\n}\n\n/** The binder count an application must fill (bound and regular alike). */\nfunction regularBinders(binders: readonly Binder[]): readonly Binder[] {\n return binders.filter((binder) => !binder.binds);\n}\n\nfunction binderSortOf(binder: Binder): string {\n return \"sort\" in binder.type ? binder.type.sort : \"\";\n}\n\ninterface TokenMeaning {\n readonly key: string;\n readonly term: string;\n}\n\n/**\n * One line per parse rule a token can carry. Two identical lines are a\n * pointless duplicate but not an ambiguity; anything else sharing a token\n * is a conflict.\n */\nfunction meaningOf(notation: NotationInfo): TokenMeaning | null {\n if (notation.form === \"simple\") {\n return {\n key: `${notation.fixity}:${String(notation.prec)}`,\n term: notation.term,\n };\n }\n\n const first = notation.literals[0];\n\n if (first === undefined || first.kind !== \"constant\") {\n return null;\n }\n\n return { key: \"general\", term: notation.term };\n}\n\nexport function parseSpec(source: string): SpecParse {\n const { statements, diagnostics: parseDiagnostics } =\n parseStatements(source);\n const diagnostics: Diagnostic[] = [...parseDiagnostics];\n\n const sorts = new Map<string, SortInfo>();\n const terms = new Map<string, TermInfo>();\n const coercions: CoercionInfo[] = [];\n const notations: NotationInfo[] = [];\n const elabRules: ElabRule[] = [];\n const lints: LintName[] = [];\n const assocNone = new Set<number>();\n const delimitersLeft = new Set<string>();\n const delimitersRight = new Set<string>();\n const groupingPairs: (readonly [string, string])[] = [[\"(\", \")\"]];\n let dropOuterParens = false;\n let rotateBrackets: readonly (readonly [string, string])[] | null = null;\n\n // ── First pass: split each statement's annotations, collect declarations.\n\n interface Attached {\n readonly annotation: SyntaxAnnotation;\n readonly span: Span;\n readonly statement: Statement;\n }\n\n const attached: Attached[] = [];\n\n for (const statement of statements) {\n const foreign: Annotation[] = [];\n\n for (const annotation of statement.annotations) {\n const parsed = parseSyntaxAnnotation(annotation.text, annotation.span);\n\n if (parsed === null) {\n foreign.push(annotation);\n continue;\n }\n\n if (parsed.diagnostic !== null) {\n diagnostics.push(parsed.diagnostic);\n continue;\n }\n\n attached.push({\n annotation: parsed.annotation,\n span: annotation.span,\n statement,\n });\n }\n\n switch (statement.kind) {\n case \"sort\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n // The engine's `@vars` pools are this library's variable lexicon\n // too; the lines stay foreign (they are the engine's to keep).\n const vars: string[] = [];\n\n for (const annotation of foreign) {\n const match = /^@vars\\s+(.+)$/.exec(annotation.text.trim());\n\n if (match !== null) {\n vars.push(\n ...(match[1] ?? \"\").split(/\\s+/).filter((w) => w.length > 0),\n );\n }\n }\n\n sorts.set(statement.name, {\n foreignAnnotations: foreign,\n modifiers: statement.modifiers,\n name: statement.name,\n span: statement.span,\n vars,\n });\n break;\n }\n\n case \"term\":\n case \"def\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n const returnSort =\n statement.kind === \"term\"\n ? (statement.returnChain[statement.returnChain.length - 1]\n ?.sort ?? \"\")\n : statement.returnType.sort;\n\n // Arrow sugar on a term (`a > b > c`) desugars to extra anonymous\n // regular binders so arity means one thing everywhere downstream.\n const extra: Binder[] =\n statement.kind === \"term\"\n ? statement.returnChain.slice(0, -1).map((type) => ({\n binds: false,\n dummy: false,\n name: \"_\",\n span: type.span,\n type,\n }))\n : [];\n\n terms.set(statement.name, {\n binders: [...statement.binders, ...extra],\n elided: false,\n foreignAnnotations: foreign,\n isDef: statement.kind === \"def\",\n juxtaposed: false,\n name: statement.name,\n returnSort,\n roles: [],\n span: statement.span,\n });\n break;\n }\n\n case \"coercion\": {\n coercions.push({\n from: statement.from,\n name: statement.name,\n span: statement.span,\n to: statement.to,\n });\n break;\n }\n\n case \"delimiter\": {\n for (const ch of statement.left) {\n delimitersLeft.add(ch);\n }\n for (const ch of statement.right) {\n delimitersRight.add(ch);\n }\n break;\n }\n\n case \"simple-notation\": {\n notations.push({\n form: \"simple\",\n fixity: statement.fixity,\n prec: statement.prec,\n span: statement.span,\n term: statement.term,\n token: statement.token,\n });\n break;\n }\n\n case \"notation\": {\n notations.push({\n form: \"general\",\n binders: statement.binders,\n literals: statement.literals,\n span: statement.span,\n term: statement.term,\n });\n\n for (const literal of statement.literals) {\n if (\n literal.kind === \"variable\" &&\n !statement.binders.some((b) => b.name === literal.name)\n ) {\n report(\n diagnostics,\n \"unknown_notation_variable\",\n { name: literal.name },\n literal.span,\n );\n }\n }\n break;\n }\n\n case \"axiom\":\n case \"theorem\":\n break;\n }\n }\n\n // ── Second pass: interpret @syntax annotations now that names resolve.\n\n const juxtaposedBySort = new Map<string, string>();\n const elidedBySort = new Map<string, string>();\n\n for (const { annotation, span, statement } of attached) {\n switch (annotation.kind) {\n case \"juxtaposed\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n const regular =\n info === undefined ? [] : regularBinders(info.binders);\n const sort = info?.returnSort ?? \"\";\n\n if (\n info === undefined ||\n info.binders.length !== 2 ||\n regular.length !== 2 ||\n regular.some((binder) => binderSortOf(binder) !== sort)\n ) {\n report(diagnostics, \"juxtaposed_target\", {}, span);\n break;\n }\n\n if (!notations.some((notation) => notation.term === info.name)) {\n report(diagnostics, \"juxtaposed_needs_notation\", {}, span);\n break;\n }\n\n if (juxtaposedBySort.has(sort)) {\n report(diagnostics, \"juxtaposed_duplicate\", { sort }, span);\n break;\n }\n\n juxtaposedBySort.set(sort, info.name);\n terms.set(info.name, { ...info, juxtaposed: true });\n break;\n }\n\n case \"elided\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n\n if (info === undefined || regularBinders(info.binders).length !== 0) {\n report(diagnostics, \"elided_target\", {}, span);\n break;\n }\n\n if (elidedBySort.has(info.returnSort)) {\n report(\n diagnostics,\n \"elided_duplicate\",\n { sort: info.returnSort },\n span,\n );\n break;\n }\n\n elidedBySort.set(info.returnSort, info.name);\n terms.set(info.name, { ...info, elided: true });\n break;\n }\n\n case \"role\": {\n if (statement.kind !== \"term\" && statement.kind !== \"def\") {\n report(diagnostics, \"role_target\", {}, span);\n break;\n }\n\n const info = terms.get(statement.name);\n\n if (info !== undefined) {\n terms.set(statement.name, {\n ...info,\n roles: [...info.roles, annotation.role],\n });\n }\n break;\n }\n\n case \"brackets\": {\n for (const pair of annotation.pairs) {\n const exists = groupingPairs.some(\n ([open, close]) => open === pair[0] && close === pair[1],\n );\n\n if (!exists) {\n groupingPairs.push(pair);\n }\n }\n break;\n }\n\n case \"assoc-none\": {\n assocNone.add(annotation.prec);\n break;\n }\n\n case \"lint\": {\n if (!lints.includes(annotation.name)) {\n lints.push(annotation.name);\n }\n break;\n }\n\n case \"display-drop-outer-parens\": {\n dropOuterParens = true;\n break;\n }\n\n case \"display-rotate-brackets\": {\n rotateBrackets = annotation.pairs;\n break;\n }\n\n case \"elab\": {\n elabRules.push(annotation.rule);\n break;\n }\n }\n }\n\n // ── Validation over the collected spec.\n\n for (const info of terms.values()) {\n for (const binder of info.binders) {\n if (\"sort\" in binder.type && !sorts.has(binder.type.sort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: binder.type.sort },\n binder.type.span,\n );\n }\n }\n\n if (info.returnSort !== \"\" && !sorts.has(info.returnSort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: info.returnSort },\n info.span,\n );\n }\n }\n\n for (const sort of sorts.values()) {\n for (const token of sort.vars) {\n if (terms.has(token)) {\n report(diagnostics, \"vars_term_conflict\", { token }, sort.span);\n }\n }\n }\n\n for (const coercion of coercions) {\n if (!terms.has(coercion.name)) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: coercion.name },\n coercion.span,\n );\n }\n for (const sort of [coercion.from, coercion.to]) {\n if (!sorts.has(sort)) {\n report(diagnostics, \"unknown_sort\", { name: sort }, coercion.span);\n }\n }\n }\n\n const tokenMeanings = new Map<string, { key: string; term: string }>();\n\n for (const notation of notations) {\n const info = terms.get(notation.term);\n\n if (info === undefined) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: notation.term },\n notation.span,\n );\n continue;\n }\n\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n (regularBinders(info.binders).length !== 2 ||\n info.binders.some((b) => b.binds))\n ) {\n report(diagnostics, \"infix_arity\", {}, notation.span);\n }\n\n const token =\n notation.form === \"simple\"\n ? notation.token\n : notation.literals[0]?.kind === \"constant\"\n ? notation.literals[0].token\n : null;\n const meaning = meaningOf(notation);\n\n if (token === null || meaning === null) {\n continue;\n }\n\n const existing = tokenMeanings.get(token);\n\n if (existing !== undefined) {\n if (existing.key !== meaning.key || existing.term !== meaning.term) {\n report(diagnostics, \"token_conflict\", { token }, notation.span);\n }\n continue;\n }\n\n tokenMeanings.set(token, meaning);\n }\n\n const precFixities = new Map<number, Set<\"infixl\" | \"infixr\">>();\n\n for (const notation of notations) {\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n typeof notation.prec === \"number\"\n ) {\n const set = precFixities.get(notation.prec) ?? new Set();\n set.add(notation.fixity);\n precFixities.set(notation.prec, set);\n\n if (set.size > 1) {\n report(\n diagnostics,\n \"precedence_mixed_associativity\",\n { prec: String(notation.prec) },\n notation.span,\n );\n }\n }\n }\n\n for (const [open, close] of groupingPairs) {\n for (const token of [open, close]) {\n if (tokenMeanings.has(token)) {\n report(\n diagnostics,\n \"grouping_token_conflict\",\n { token },\n {\n start: 0,\n end: 0,\n },\n );\n }\n }\n }\n\n for (const rule of elabRules) {\n const captures = new Map<string, number>();\n\n for (const element of rule.pattern) {\n if (element.kind === \"capture\") {\n if (captures.has(element.name)) {\n report(\n diagnostics,\n \"elab_duplicate_capture\",\n { name: element.name },\n rule.span,\n );\n }\n captures.set(element.name, 0);\n }\n }\n\n for (const element of rule.template) {\n if (element.kind === \"reference\") {\n const count = captures.get(element.name);\n\n if (count === undefined) {\n report(\n diagnostics,\n \"elab_unknown_reference\",\n { name: element.name },\n rule.span,\n );\n continue;\n }\n\n captures.set(element.name, count + 1);\n }\n }\n\n if (!rule.inputOnly) {\n for (const [name, count] of captures) {\n if (count !== 1) {\n report(diagnostics, \"elab_not_invertible\", { name }, rule.span);\n }\n }\n }\n }\n\n return {\n diagnostics,\n spec: {\n assocNone,\n coercions,\n delimiters: { left: delimitersLeft, right: delimitersRight },\n display: { dropOuterParens, rotateBrackets },\n groupingPairs,\n lints,\n notations,\n elabRules,\n sorts,\n statements,\n terms,\n },\n };\n}\n"]}
1
+ {"version":3,"file":"spec.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cAAc,EAAE,KAAK,EAAE,eAAe,EAAE;QAC5D,OAAO,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE;QAC5C,aAAa;QACb,KAAK;QACL,SAAS;QACT,SAAS;QACT,KAAK;QACL,UAAU;QACV,qEAAqE;QACrE,iBAAiB,EAAE;YACjB,IAAI,EAAE,IAAI,GAAG,CAAC,CAAC,GAAG,cAAc,EAAE,GAAG,WAAW,CAAC,CAAC;YAClD,KAAK,EAAE,IAAI,GAAG,CAAC,CAAC,GAAG,eAAe,EAAE,GAAG,YAAY,CAAC,CAAC;SACtD;QACD,KAAK;KACN,CAAC;IAEF,sBAAsB,CAAC,IAAI,EAAE,mBAAmB,EAAE,WAAW,CAAC,CAAC;IAE/D,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;AAC/B,CAAC","sourcesContent":["/**\n * From parsed statements to a `Spec` — the library's central object: the\n * signature, notations, coercions, and `@syntax` surface metadata of one\n * logical system, validated.\n *\n * Validation here is about the spec being *coherent* (token conflicts,\n * dangling names, non-invertible elab rules); whether the theory proves\n * anything is the engine's business, and axiom bodies pass through opaque.\n */\n\nimport {\n type DelimiterSet,\n delimiterRules,\n isReachableChunk,\n segment,\n} from \"../delimiters.js\";\nimport {\n type Diagnostic,\n diagnostic,\n type Severity,\n type Span,\n} from \"../diagnostics.js\";\nimport { surfaceVocabulary } from \"../vocabulary.js\";\nimport {\n type ElabRule,\n type LintName,\n parseSyntaxAnnotation,\n type SyntaxAnnotation,\n} from \"./annotations.js\";\nimport {\n type Annotation,\n type Binder,\n type NotationLiteral,\n type Precedence,\n parseStatements,\n type Statement,\n} from \"./statements.js\";\n\nexport interface SortInfo {\n readonly foreignAnnotations: readonly Annotation[];\n readonly modifiers: readonly string[];\n readonly name: string;\n readonly span: Span;\n /**\n * The sort's variable tokens, read from the engine's own `@vars`\n * annotation (which stays in `foreignAnnotations` untouched — it is the\n * engine's, we only listen in). These are the surface lexicon's\n * identifiers of this sort; whether a quantifier can bind them falls\n * out of the quantifiers' binder sorts, not from here.\n */\n readonly vars: readonly string[];\n}\n\nexport interface TermInfo {\n readonly binders: readonly Binder[];\n /** `@syntax elided`: supplied when an argument of its sort is missing. */\n readonly elided: boolean;\n readonly foreignAnnotations: readonly Annotation[];\n /** True when this came from a `def` rather than a `term`. */\n readonly isDef: boolean;\n /** `@syntax juxtaposed`: adjacency of its sort's leaves denotes it. */\n readonly juxtaposed: boolean;\n readonly name: string;\n readonly returnSort: string;\n readonly roles: readonly string[];\n readonly span: Span;\n}\n\nexport interface CoercionInfo {\n readonly from: string;\n readonly name: string;\n readonly span: Span;\n readonly to: string;\n}\n\nexport type NotationInfo =\n | {\n readonly form: \"general\";\n /** The notation statement's own binders, which its variables name. */\n readonly binders: readonly Binder[];\n readonly literals: readonly NotationLiteral[];\n readonly span: Span;\n readonly term: string;\n }\n | {\n readonly form: \"simple\";\n readonly fixity: \"infixl\" | \"infixr\" | \"prefix\";\n readonly prec: Precedence;\n readonly span: Span;\n readonly term: string;\n readonly token: string;\n };\n\nexport interface Spec {\n readonly assocNone: ReadonlySet<number>;\n readonly coercions: readonly CoercionInfo[];\n /**\n * The theory's own `delimiter` statement — how the *engine* cuts up math\n * strings. Single bytes, because that is all the engine's table holds.\n */\n readonly delimiters: DelimiterSet;\n readonly display: {\n readonly dropOuterParens: boolean;\n readonly rotateBrackets: readonly (readonly [string, string])[] | null;\n };\n /** Always starts with the built-in `(\"(\", \")\")` pair. */\n readonly groupingPairs: readonly (readonly [string, string])[];\n readonly lints: readonly LintName[];\n /** In declaration order; the last notation for a term is canonical. */\n readonly notations: readonly NotationInfo[];\n readonly elabRules: readonly ElabRule[];\n readonly sorts: ReadonlyMap<string, SortInfo>;\n /** Every statement, in order, annotations intact — full fidelity. */\n readonly statements: readonly Statement[];\n /**\n * How *student* input is cut up: `delimiters` unioned with every\n * `@syntax delimiter` the spec declares. A spec that declares none gets\n * the theory's set unchanged — so writing textbook notation tight\n * (`~~P`, `Fxy`) is an explicit opt-in, never something a spec acquires\n * by accident.\n */\n readonly surfaceDelimiters: DelimiterSet;\n readonly terms: ReadonlyMap<string, TermInfo>;\n}\n\nexport interface SpecParse {\n readonly diagnostics: readonly Diagnostic[];\n readonly spec: Spec;\n}\n\nconst TEMPLATES: Record<string, string> = {\n duplicate_declaration: \"{name} is already declared\",\n unknown_sort: \"sort {name} is not declared\",\n unknown_term: \"term {name} is not declared\",\n infix_arity: \"an infix operator needs exactly two regular arguments\",\n unknown_notation_variable:\n \"notation variable {name} is not one of the declaration's binders\",\n token_conflict:\n \"token {token} already has a meaning; a token may carry one parse rule\",\n precedence_mixed_associativity:\n \"precedence {prec} mixes infixl and infixr operators\",\n grouping_token_conflict:\n \"{token} is a grouping bracket and cannot also be a notation token\",\n elab_duplicate_capture: \"capture {name} appears twice in the pattern\",\n elab_unknown_reference:\n \"the template references {name}, which the pattern does not capture\",\n elab_not_invertible:\n \"capture {name} is not used exactly once in the template; mark the rule input-only if that is intended\",\n juxtaposed_target:\n \"@syntax juxtaposed must sit on a binary term whose arguments and result share one sort\",\n juxtaposed_needs_notation:\n \"a juxtaposed term needs a declared notation, so the engine can read it\",\n juxtaposed_duplicate: \"sort {sort} already has a juxtaposed combiner\",\n elided_target: \"@syntax elided must sit on a term with no arguments\",\n elided_duplicate: \"sort {sort} already has an elided term\",\n vars_term_conflict:\n \"@vars token {token} is also a declared term; a name can be only one\",\n role_target: \"@syntax role must sit on a term\",\n delimiter_unknown:\n \"delimiter {token} is neither a notation token, a bracket, nor a lexicon name, so no input can ever be read as it\",\n delimiter_token_not_delimited:\n \"{token} is not a surface delimiter; it is only read when whitespace or a delimiter bounds it, so it will run into an adjacent token\",\n delimiter_unreachable_name:\n \"the delimiters split {name} into {chunks}, so it can never be read as one name\",\n};\n\nfunction report(\n diagnostics: Diagnostic[],\n id: string,\n params: Record<string, string>,\n span: Span,\n severity: Severity = \"error\",\n): void {\n diagnostics.push(\n diagnostic(id, TEMPLATES[id] ?? id, params, span, severity),\n );\n}\n\n/** The binder count an application must fill (bound and regular alike). */\nfunction regularBinders(binders: readonly Binder[]): readonly Binder[] {\n return binders.filter((binder) => !binder.binds);\n}\n\nfunction binderSortOf(binder: Binder): string {\n return \"sort\" in binder.type ? binder.type.sort : \"\";\n}\n\ninterface TokenMeaning {\n readonly key: string;\n readonly term: string;\n}\n\n/**\n * One line per parse rule a token can carry. Two identical lines are a\n * pointless duplicate but not an ambiguity; anything else sharing a token\n * is a conflict.\n */\nfunction meaningOf(notation: NotationInfo): TokenMeaning | null {\n if (notation.form === \"simple\") {\n return {\n key: `${notation.fixity}:${String(notation.prec)}`,\n term: notation.term,\n };\n }\n\n const first = notation.literals[0];\n\n if (first === undefined || first.kind !== \"constant\") {\n return null;\n }\n\n return { key: \"general\", term: notation.term };\n}\n\n/** Where each notation constant was declared, for pointing a diagnostic. */\nfunction tokenSpans(spec: Spec): Map<string, Span> {\n const spans = new Map<string, Span>();\n\n for (const notation of spec.notations) {\n const constants =\n notation.form === \"simple\"\n ? [notation.token]\n : notation.literals\n .filter((literal) => literal.kind === \"constant\")\n .map((literal) => literal.token);\n\n for (const token of constants) {\n if (!spans.has(token)) {\n spans.set(token, notation.span);\n }\n }\n }\n\n return spans;\n}\n\n/**\n * The surface delimiter set, checked against the vocabulary it has to cut\n * up. Only runs when the spec declares `@syntax delimiter`: a spec that\n * declares none is read under the theory's own delimiters, where tokens\n * are whitespace-separated and none of this can bite.\n */\nfunction checkSurfaceDelimiters(\n spec: Spec,\n declarations: readonly { entries: readonly string[]; span: Span }[],\n diagnostics: Diagnostic[],\n): void {\n const fallback = declarations[0]?.span;\n\n if (fallback === undefined) {\n return;\n }\n\n const vocabulary = surfaceVocabulary(spec);\n const rules = delimiterRules(spec.surfaceDelimiters);\n const declared = new Set([\n ...spec.surfaceDelimiters.left,\n ...spec.surfaceDelimiters.right,\n ]);\n\n // A delimiter nothing can be classified as cuts input into pieces the\n // parser must then reject — a boundary with no meaning behind it.\n for (const { entries, span } of declarations) {\n for (const entry of entries) {\n if (!vocabulary.tokens.has(entry) && !vocabulary.names.has(entry)) {\n report(diagnostics, \"delimiter_unknown\", { token: entry }, span);\n }\n }\n }\n\n // A token that is not itself a delimiter still reads when whitespace or a\n // delimiter bounds it — `P->Q` is fine with the letters declared — so this\n // is advice, not breakage. What it warns about is two such tokens\n // meeting: `->~` is one chunk unless one of them delimits.\n const spans = tokenSpans(spec);\n\n for (const token of vocabulary.tokens) {\n if (!declared.has(token)) {\n report(\n diagnostics,\n \"delimiter_token_not_delimited\",\n { token },\n spans.get(token) ?? fallback,\n \"warning\",\n );\n }\n }\n\n // A name the delimiters split apart is unreachable: segmentation happens\n // before anything knows the name exists, so no bracketing recovers it.\n // Elided terms are exempt — they are supplied by the parser and dropped\n // by the printer, never typed.\n for (const [name, ref] of vocabulary.names) {\n if (ref.kind === \"term\" && spec.terms.get(ref.term)?.elided === true) {\n continue;\n }\n\n if (isReachableChunk(name, rules)) {\n continue;\n }\n\n const span =\n ref.kind === \"term\"\n ? spec.terms.get(ref.term)?.span\n : spec.sorts.get(ref.sort)?.span;\n\n report(\n diagnostics,\n \"delimiter_unreachable_name\",\n { chunks: segment(name, rules).join(\" \"), name },\n span ?? fallback,\n );\n }\n}\n\nexport function parseSpec(source: string): SpecParse {\n const { statements, diagnostics: parseDiagnostics } =\n parseStatements(source);\n const diagnostics: Diagnostic[] = [...parseDiagnostics];\n\n const sorts = new Map<string, SortInfo>();\n const terms = new Map<string, TermInfo>();\n const coercions: CoercionInfo[] = [];\n const notations: NotationInfo[] = [];\n const elabRules: ElabRule[] = [];\n const lints: LintName[] = [];\n const assocNone = new Set<number>();\n const delimitersLeft = new Set<string>();\n const delimitersRight = new Set<string>();\n const surfaceLeft = new Set<string>();\n const surfaceRight = new Set<string>();\n const surfaceDeclarations: {\n readonly entries: readonly string[];\n readonly span: Span;\n }[] = [];\n const groupingPairs: (readonly [string, string])[] = [[\"(\", \")\"]];\n let dropOuterParens = false;\n let rotateBrackets: readonly (readonly [string, string])[] | null = null;\n\n // ── First pass: split each statement's annotations, collect declarations.\n\n interface Attached {\n readonly annotation: SyntaxAnnotation;\n readonly span: Span;\n readonly statement: Statement;\n }\n\n const attached: Attached[] = [];\n\n for (const statement of statements) {\n const foreign: Annotation[] = [];\n\n for (const annotation of statement.annotations) {\n const parsed = parseSyntaxAnnotation(annotation.text, annotation.span);\n\n if (parsed === null) {\n foreign.push(annotation);\n continue;\n }\n\n if (parsed.diagnostic !== null) {\n diagnostics.push(parsed.diagnostic);\n continue;\n }\n\n attached.push({\n annotation: parsed.annotation,\n span: annotation.span,\n statement,\n });\n }\n\n switch (statement.kind) {\n case \"sort\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n // The engine's `@vars` pools are this library's variable lexicon\n // too; the lines stay foreign (they are the engine's to keep).\n const vars: string[] = [];\n\n for (const annotation of foreign) {\n const match = /^@vars\\s+(.+)$/.exec(annotation.text.trim());\n\n if (match !== null) {\n vars.push(\n ...(match[1] ?? \"\").split(/\\s+/).filter((w) => w.length > 0),\n );\n }\n }\n\n sorts.set(statement.name, {\n foreignAnnotations: foreign,\n modifiers: statement.modifiers,\n name: statement.name,\n span: statement.span,\n vars,\n });\n break;\n }\n\n case \"term\":\n case \"def\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n const returnSort =\n statement.kind === \"term\"\n ? (statement.returnChain[statement.returnChain.length - 1]\n ?.sort ?? \"\")\n : statement.returnType.sort;\n\n // Arrow sugar on a term (`a > b > c`) desugars to extra anonymous\n // regular binders so arity means one thing everywhere downstream.\n const extra: Binder[] =\n statement.kind === \"term\"\n ? statement.returnChain.slice(0, -1).map((type) => ({\n binds: false,\n dummy: false,\n name: \"_\",\n span: type.span,\n type,\n }))\n : [];\n\n terms.set(statement.name, {\n binders: [...statement.binders, ...extra],\n elided: false,\n foreignAnnotations: foreign,\n isDef: statement.kind === \"def\",\n juxtaposed: false,\n name: statement.name,\n returnSort,\n roles: [],\n span: statement.span,\n });\n break;\n }\n\n case \"coercion\": {\n coercions.push({\n from: statement.from,\n name: statement.name,\n span: statement.span,\n to: statement.to,\n });\n break;\n }\n\n case \"delimiter\": {\n for (const ch of statement.left) {\n delimitersLeft.add(ch);\n }\n for (const ch of statement.right) {\n delimitersRight.add(ch);\n }\n break;\n }\n\n case \"simple-notation\": {\n notations.push({\n form: \"simple\",\n fixity: statement.fixity,\n prec: statement.prec,\n span: statement.span,\n term: statement.term,\n token: statement.token,\n });\n break;\n }\n\n case \"notation\": {\n notations.push({\n form: \"general\",\n binders: statement.binders,\n literals: statement.literals,\n span: statement.span,\n term: statement.term,\n });\n\n for (const literal of statement.literals) {\n if (\n literal.kind === \"variable\" &&\n !statement.binders.some((b) => b.name === literal.name)\n ) {\n report(\n diagnostics,\n \"unknown_notation_variable\",\n { name: literal.name },\n literal.span,\n );\n }\n }\n break;\n }\n\n case \"axiom\":\n case \"theorem\":\n break;\n }\n }\n\n // ── Second pass: interpret @syntax annotations now that names resolve.\n\n const juxtaposedBySort = new Map<string, string>();\n const elidedBySort = new Map<string, string>();\n\n for (const { annotation, span, statement } of attached) {\n switch (annotation.kind) {\n case \"juxtaposed\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n const regular =\n info === undefined ? [] : regularBinders(info.binders);\n const sort = info?.returnSort ?? \"\";\n\n if (\n info === undefined ||\n info.binders.length !== 2 ||\n regular.length !== 2 ||\n regular.some((binder) => binderSortOf(binder) !== sort)\n ) {\n report(diagnostics, \"juxtaposed_target\", {}, span);\n break;\n }\n\n if (!notations.some((notation) => notation.term === info.name)) {\n report(diagnostics, \"juxtaposed_needs_notation\", {}, span);\n break;\n }\n\n if (juxtaposedBySort.has(sort)) {\n report(diagnostics, \"juxtaposed_duplicate\", { sort }, span);\n break;\n }\n\n juxtaposedBySort.set(sort, info.name);\n terms.set(info.name, { ...info, juxtaposed: true });\n break;\n }\n\n case \"elided\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n\n if (info === undefined || regularBinders(info.binders).length !== 0) {\n report(diagnostics, \"elided_target\", {}, span);\n break;\n }\n\n if (elidedBySort.has(info.returnSort)) {\n report(\n diagnostics,\n \"elided_duplicate\",\n { sort: info.returnSort },\n span,\n );\n break;\n }\n\n elidedBySort.set(info.returnSort, info.name);\n terms.set(info.name, { ...info, elided: true });\n break;\n }\n\n case \"role\": {\n if (statement.kind !== \"term\" && statement.kind !== \"def\") {\n report(diagnostics, \"role_target\", {}, span);\n break;\n }\n\n const info = terms.get(statement.name);\n\n if (info !== undefined) {\n terms.set(statement.name, {\n ...info,\n roles: [...info.roles, annotation.role],\n });\n }\n break;\n }\n\n case \"brackets\": {\n for (const pair of annotation.pairs) {\n const exists = groupingPairs.some(\n ([open, close]) => open === pair[0] && close === pair[1],\n );\n\n if (!exists) {\n groupingPairs.push(pair);\n }\n }\n break;\n }\n\n case \"delimiter\": {\n for (const entry of annotation.left) {\n surfaceLeft.add(entry);\n }\n\n for (const entry of annotation.right) {\n surfaceRight.add(entry);\n }\n\n surfaceDeclarations.push({\n entries: [...new Set([...annotation.left, ...annotation.right])],\n span,\n });\n break;\n }\n\n case \"assoc-none\": {\n assocNone.add(annotation.prec);\n break;\n }\n\n case \"lint\": {\n if (!lints.includes(annotation.name)) {\n lints.push(annotation.name);\n }\n break;\n }\n\n case \"display-drop-outer-parens\": {\n dropOuterParens = true;\n break;\n }\n\n case \"display-rotate-brackets\": {\n rotateBrackets = annotation.pairs;\n break;\n }\n\n case \"elab\": {\n elabRules.push(annotation.rule);\n break;\n }\n }\n }\n\n // ── Validation over the collected spec.\n\n for (const info of terms.values()) {\n for (const binder of info.binders) {\n if (\"sort\" in binder.type && !sorts.has(binder.type.sort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: binder.type.sort },\n binder.type.span,\n );\n }\n }\n\n if (info.returnSort !== \"\" && !sorts.has(info.returnSort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: info.returnSort },\n info.span,\n );\n }\n }\n\n for (const sort of sorts.values()) {\n for (const token of sort.vars) {\n if (terms.has(token)) {\n report(diagnostics, \"vars_term_conflict\", { token }, sort.span);\n }\n }\n }\n\n for (const coercion of coercions) {\n if (!terms.has(coercion.name)) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: coercion.name },\n coercion.span,\n );\n }\n for (const sort of [coercion.from, coercion.to]) {\n if (!sorts.has(sort)) {\n report(diagnostics, \"unknown_sort\", { name: sort }, coercion.span);\n }\n }\n }\n\n const tokenMeanings = new Map<string, { key: string; term: string }>();\n\n for (const notation of notations) {\n const info = terms.get(notation.term);\n\n if (info === undefined) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: notation.term },\n notation.span,\n );\n continue;\n }\n\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n (regularBinders(info.binders).length !== 2 ||\n info.binders.some((b) => b.binds))\n ) {\n report(diagnostics, \"infix_arity\", {}, notation.span);\n }\n\n const token =\n notation.form === \"simple\"\n ? notation.token\n : notation.literals[0]?.kind === \"constant\"\n ? notation.literals[0].token\n : null;\n const meaning = meaningOf(notation);\n\n if (token === null || meaning === null) {\n continue;\n }\n\n const existing = tokenMeanings.get(token);\n\n if (existing !== undefined) {\n if (existing.key !== meaning.key || existing.term !== meaning.term) {\n report(diagnostics, \"token_conflict\", { token }, notation.span);\n }\n continue;\n }\n\n tokenMeanings.set(token, meaning);\n }\n\n const precFixities = new Map<number, Set<\"infixl\" | \"infixr\">>();\n\n for (const notation of notations) {\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n typeof notation.prec === \"number\"\n ) {\n const set = precFixities.get(notation.prec) ?? new Set();\n set.add(notation.fixity);\n precFixities.set(notation.prec, set);\n\n if (set.size > 1) {\n report(\n diagnostics,\n \"precedence_mixed_associativity\",\n { prec: String(notation.prec) },\n notation.span,\n );\n }\n }\n }\n\n for (const [open, close] of groupingPairs) {\n for (const token of [open, close]) {\n if (tokenMeanings.has(token)) {\n report(\n diagnostics,\n \"grouping_token_conflict\",\n { token },\n {\n start: 0,\n end: 0,\n },\n );\n }\n }\n }\n\n for (const rule of elabRules) {\n const captures = new Map<string, number>();\n\n for (const element of rule.pattern) {\n if (element.kind === \"capture\") {\n if (captures.has(element.name)) {\n report(\n diagnostics,\n \"elab_duplicate_capture\",\n { name: element.name },\n rule.span,\n );\n }\n captures.set(element.name, 0);\n }\n }\n\n for (const element of rule.template) {\n if (element.kind === \"reference\") {\n const count = captures.get(element.name);\n\n if (count === undefined) {\n report(\n diagnostics,\n \"elab_unknown_reference\",\n { name: element.name },\n rule.span,\n );\n continue;\n }\n\n captures.set(element.name, count + 1);\n }\n }\n\n if (!rule.inputOnly) {\n for (const [name, count] of captures) {\n if (count !== 1) {\n report(diagnostics, \"elab_not_invertible\", { name }, rule.span);\n }\n }\n }\n }\n\n const spec: Spec = {\n assocNone,\n coercions,\n delimiters: { left: delimitersLeft, right: delimitersRight },\n display: { dropOuterParens, rotateBrackets },\n groupingPairs,\n lints,\n notations,\n elabRules,\n sorts,\n statements,\n // The union: what the engine splits on, plus what the textbook does.\n surfaceDelimiters: {\n left: new Set([...delimitersLeft, ...surfaceLeft]),\n right: new Set([...delimitersRight, ...surfaceRight]),\n },\n terms,\n };\n\n checkSurfaceDelimiters(spec, surfaceDeclarations, diagnostics);\n\n return { diagnostics, spec };\n}\n"]}
package/dist/scan.d.ts CHANGED
@@ -1,31 +1,29 @@
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 { type DelimiterSet } from "./delimiters.js";
20
23
  import type { Span } from "./diagnostics.js";
21
24
  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
- };
25
+ import { type NameRef } from "./vocabulary.js";
26
+ export type { NameRef } from "./vocabulary.js";
29
27
  export type Reading = {
30
28
  readonly kind: "name";
31
29
  readonly name: string;
@@ -37,25 +35,33 @@ export type Reading = {
37
35
  readonly length: number;
38
36
  };
39
37
  export interface ScanPoint {
40
- /** Position after leading whitespace; readings start here. */
38
+ /** Position after leading whitespace; the chunk starts here. */
41
39
  readonly start: number;
42
- /** Longest-first notation reading, then the longest name reading. Empty
43
- * at end of input or on an unrecognized character. */
40
+ /** The chunk at `start`, before classification. Empty at end of input. */
41
+ readonly chunk: string;
42
+ /** The notation reading, then the name reading — whichever the chunk
43
+ * answers to. Empty at end of input, or when it answers to neither. */
44
44
  readonly readings: readonly Reading[];
45
45
  readonly atEnd: boolean;
46
46
  }
47
47
  export declare class Scanner {
48
- /** Notation and grouping tokens, longest first. */
48
+ private readonly rules;
49
+ /** Notation and grouping tokens. */
49
50
  private readonly tokens;
50
- /** Lexicon names, longest first, each with what it refers to. */
51
+ /** Lexicon names, each with what it refers to. */
51
52
  private readonly names;
52
- constructor(spec: Spec);
53
+ constructor(spec: Spec, delimiters?: DelimiterSet);
53
54
  /**
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.
55
+ * The chunk at `position` in `text`, after skipping whitespace, and what
56
+ * it can be read as: the notation token first, then the lexicon name.
57
57
  */
58
58
  at(text: string, position: number): ScanPoint;
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: string): string[];
59
65
  /** The span a reading occupies from a scan point. */
60
66
  span(point: ScanPoint, reading: Reading): Span;
61
67
  }