ParserLang is parser combinator library. It lets you make parsers by combining other parsers.
Its primary superpower is the ability to define parsers declaratively with template literals:
import { lang } from 'parser-lang';
let { calc } = lang`
num = /[0-9]+/ > ${ch => parseInt(ch, 10)};
addExpr = num '+' multExpr > ${([left, op, right]) => left + right}
| num ;
multExpr = addExpr '*' multExpr > ${([left, op, right]) => left * right}
| addExpr ;
calc = multExpr ;
`;
calc.tryParse('1+1*2');
// 3
It's monadic and stuff but don't get too hung up on that. It tries to be very friendly.
npm install parser-lang
- Parsimmon - a JavaScript parser combinator library. ParserLang is heavily inspired by Parsimmon. Parsimmon is more coupled to parsing strings (ParserLang uses the Context protocol to support a variety of input types) but also supports a wider variety of JavaScript runtimes.
- Parsec - a Haskell parser combinator library
- Monadic Parser Combinators - one of the seminal papers describing parser combinators