disnet / parser-lang

A parser combinator library with declarative superpowers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version Build Status Contributor Covenant

ParserLang

ParserLang is parser combinator library. It lets you make parsers by combining other parsers.

Its primary superpower is the ability to define parsers declaratively with template literals:

import { lang } from 'parser-lang';

let { calc } = lang`
  num = /[0-9]+/ > ${ch => parseInt(ch, 10)};

  addExpr = num '+' multExpr > ${([left, op, right]) => left + right}
          | num ;

  multExpr = addExpr '*' multExpr > ${([left, op, right]) => left * right}
           | addExpr ;
  
  calc = multExpr ;
`;

calc.tryParse('1+1*2');
// 3

It's monadic and stuff but don't get too hung up on that. It tries to be very friendly.

Installing

npm install parser-lang

Documentation

Related Projects/Papers

  • Parsimmon - a JavaScript parser combinator library. ParserLang is heavily inspired by Parsimmon. Parsimmon is more coupled to parsing strings (ParserLang uses the Context protocol to support a variety of input types) but also supports a wider variety of JavaScript runtimes.
  • Parsec - a Haskell parser combinator library
  • Monadic Parser Combinators - one of the seminal papers describing parser combinators

About

A parser combinator library with declarative superpowers


Languages

Language:JavaScript 100.0%