Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 1.02 KB

README.md

File metadata and controls

40 lines (30 loc) · 1.02 KB

This is a parser combinator library written in typescript. You can find a writeup about it here. Design goals:

  • Produces recursive descent parsers capable of parsing PEG grammars.
  • For throwaway projects only. Will never grow big, have complex optimizations, or other fancy featues.
  • Small, so I can understand every detail. The library is under 500 lines of code and took maybe a couple of days to write.
  • Type safe. The syntax tree types are inferred from the combinators. It's beautiful and really fun to use.

Installation

npm install @spakhm/ts-parsec

Example

Here is a simple example:

const digit = range('0', '9');
const lower = range('a', 'z');
const upper = range('A', 'Z');
const alpha = either(lower, upper);
const alnum = either(alpha, digit);

const ident = lex(seq(alpha, many(alnum))).map(([first, rest]) =>
  [first, ...rest].join(""));

const input = "Hello";
const stream = fromString(input);
ident(stream);

Limitations

  • No error reporting at all.