Type-safe, zero-copy parser combinators for TypeScript. Inspired by nom.
parsercombinatorszero-copytypescriptrust-inspired
typescript 3 0 0 2 files
README.md
zero-copy-parser 🦾
"Parse, don't validate."
A strict, type-safe parser combinator library for TypeScript.
Philosophy
- Correctness: If it compiles, it parses (mostly).
- Zero-Copy: We pass indices, not substrings. Allocation only happens on yield.
- Explicit: Errors are values, not exceptions.
Usage
import {
tag,
sequence,
map,
digit1,
separatedList0,
delimited,
multispace0
} from 'zero-copy-parser';
// Parse "[1, 2, 3]"
const numberParser = map(digit1, Number);
const arrayParser = delimited(
tag("["),
separatedList0(
numberParser,
sequence(tag(","), multispace0)
),
tag("]")
);
const result = arrayParser("[1, 2, 3]", 0);
// Result: Ok([1, 2, 3])
Combinators
tag(str): Match exact stringsequence(...parsers): Match in orderalt(...parsers): Match first successmap(parser, fn): Transform resultmany0(parser): 0 or moretakeWhile(pred): 0 or more chars matching predicatetakeWhile1(pred): 1 or more charsoptional(parser): Match or nulloneOf(chars)/noneOf(chars): Character classseparatedList0(parser, separator): List with separatordelimited(start, parser, end): Between brackets/tags
Pre-built
digit1: One or more digitsalpha1: One or more lettersmultispace0: Whitespace
Contributions
PRs welcome. Keep it strict. Keep it typed.