live-parser is a JavaScript parser combinator library designed for streaming data inputs. It is based on packrat parsing which ensure parsing with linear time complexity.
- Incremental parsing without waiting for full input.
- Build complex parsers from simple composable primitives.
- Fully type safe
npm install live-parserimport { literal, charFrom, oneOf, sequenceOf, zeroOrMany } from "live-parser";
const join = (sep: string) => (values: string[]) => values.join(sep);
const zero = literal("0");
const nonZeroDigit = charFrom("1-9");
const digit = oneOf(zero, nonZeroDigit);
const positiveInteger = sequenceOf(nonZeroDigit, zeroOrMany(digit).map(join(""))).map(join(""));
const negativeInteger = sequenceOf(literal("-"), positiveInteger).map(join(""));
const integer = oneOf(zero, positiveInteger, negativeInteger).map(Number);
// Parse a string
const result = integer.parseString("-123");
console.log(result);
// {
// status: "success",
// offset: 4,
// value: -123
// error: null
// }This library was inspired by the following projects and resources:
- Arcsecond
- Parser Combinators From Scratch
- PEG Parsing Series by Guido van Rossum
- Packrat Parsing from Scratch by Bruce Hill
- Packrat Parsers Can Support Left Recursion: Warth et al.
MIT