Production-ready GLR parser (with parenthesis recovery) for the DSEL minimal arrow language.
npm install dsel-glr-parserA small, dependency-free GLR (generalized LR) parser for the DSEL language described in the prompt:
- S-expression-ish lists with ( / )
- reserved head tokens: ->, id->
- labels: bare or backticked (with escapes)
- arrows are first-class terms (arrow endpoints can be arrows)
- parenthesis mismatch recovery (drops extra ) and inserts missing ) at EOF)
It produces the minimal AST form:
``ts`
export type Thing =
| { kind: "atom"; id: string; label: string }
| { kind: "box"; id: string; label: string; items: Thing[] }
| { kind: "arrow"; id: string; label: string | null; from: Thing; to: Thing };
(Self-loops are arrows with from === to.)
`bash`
npm i dsel-glr-parser
`ts
import { parse } from "dsel-glr-parser";
const src = (-> is_a square rectangle);
const res = parse(src, { mode: "recover" });
console.log(res.ast);
console.log(res.diagnostics);
`
- tokenize(input: string): { tokens: Token[]; diagnostics: Diagnostic[] }parse(input: string, options?): ParseResult
-
Options:
- mode: "strict" throws on diagnostics that make the document invalid; "recover" returns best-effort AST.recoverParentheses
- : default truemaxRecoveries
- : default 50 (limits skip/insert recovery actions)
`bash`
npm i
npm test
npm run build
This library explicitly focuses on mismatched parenthesis recovery:
- If there are extra ), they are dropped (diagnostic emitted).)
- If there are missing , they are inserted at EOF (diagnostic emitted).
For other syntax issues (like invalid arrow arity), in mode: "recover" it emits diagnostics and produces a best-effort AST by:"
- inserting placeholder atoms labeled when required,
- ignoring extra arguments.
In mode: "strict"`, these are errors.