A lightweight, type-safe parser combinator library for building efficient parsers in TypeScript.
- 🧩 Intuitive combinator API
- 🚀 Recursive parser support
- 📍 Precise error locations
- 🧠 Zero dependencies
- 📚 100% test coverage
npm install combo-parser
# or
bun add combo-parserimport { char, either, sequence, many, map } from 'combo-parser';
// Parse either a quoted string or unquoted word
const stringParser = either(
sequence(char('"'), many(char.match(/[^"]/)), char('"')),
many(char.match(/\w/))
).map(([_, content]) => content.join(''));
// Parse "hello" or hello
console.log(stringParser('"hello"', 0)); // { success: true, value: 'hello' }
console.log(stringParser('world', 0)); // { success: true, value: 'world' }import { char, digit, letters } from 'combo-parser';
char('a') // Match exact character
digit() // Match 0-9
letters() // Match a-zA-Zimport { either, sequence, many, optional } from 'combo-parser';
either(a, b) // Try parser A, then B
sequence(a, b) // Match A then B
many(a) // Match 0+ occurrences
optional(a) // Make parser optionalconst parser = either(digit(), letters());
const result = parser('!', 0);
if (!result.success) {
console.log(`Error at position ${result.index}:`);
result.errors.forEach(err =>
console.log(`- Expected ${err.expected}`)
);
}See our step-by-step guide to build a complete JSON parser.
Full documentation available at docs.api.md