dsel-glr-parser
v0.1.1
Published
Production-ready GLR parser (with parenthesis recovery) for the DSEL minimal arrow language.
Maintainers
Readme
dsel-glr-parser
A 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:
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.)
Install
npm i dsel-glr-parserUsage
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);API
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: defaulttruemaxRecoveries: default50(limits skip/insert recovery actions)
Development
npm i
npm test
npm run buildNotes on recovery
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
"<missing>"when required, - ignoring extra arguments.
In mode: "strict", these are errors.
