Tiny parser combinators library
npm install prsc

Tiny parser combinators library for JavaScript and TypeScript. Heavily
inspired by nom.
The prsc library can be installed using npm or yarn:
``bat`
npm install --save prsc
or
`bat`
yarn add prsc
The package includes both a UMD bundle (dist/prsc.js), compatible withdist/prsc.mjs
Node.js, and an ES6 module (). TypeScript typings are includeddist/prsc.d.ts
() and should work automatically in most cases.
This library exports a number of functions that make it
easy to build parsers for input represented as a string. Start from primitive
parsers such as token or your own functions matching the Parser type,map
transform results using and filter and combine them using the otherthen
functions such as and star.
The following parser accepts a simple arithmetic language consisting of the
+ and * operators:
`javascript
// Create a primitive parser that accepts a single digit
const digit = (input, offset) => {
if (/^[0-9]$/.test(input[offset])) {
return ok(offset + 1);
}
return error(offset, ['digit']);
};
// Use that to accept a string of one or more digits
const digits = plus(digit);
// Then use recognize to get the matching string and use map to parse that into a number
const number = map(recognize(digits), (str) => parseInt(str, 10));
// Multiplication
const term = then(
number,
star(preceded(token('*'), number)),
// Multiply everything together
(num, factors) => factors.reduce((product, factor) => product * factor, num)
);
// Addition
const expression = then(term, star(preceded(token('+'), term)), (num, terms) =>
terms.reduce((sum, term) => sum + term, num)
);
// Parsing some input
console.log(expression('23+45', 0));
// > { success: true, offset: 7, value: 26 }
`
Use functions to provide a layer of indirection for recursive definitions:
`javascript`
const term = then(number, optional(preceded(token('*'), termIndirect)), ...);
function termIndirect(input, offset) {
return term(input, offset);
}
Use the typings when working in TypeScript for strongly-typed parsers:
`typescript``
const digit: Parser
const digits = plus(digit); // Parser
const number = map(
digits,
strs => parseInt(strs.join(''), 10)
); // Parser