the greater parser combinator library

npm install combigreater
2 downloads in the last week
4 downloads in the last month

What is Combigreater?

Combigreater is a parser combinator library for JavaScript. Parsers it produces are functions that take a string, array, or any other array-like and return an object indicating whether parsing succeeded, and, if so, the resulting value and input left unconsumed. e.g.,

symbols('abc')('abcdef') /* { succeeded: true
                            , value: 'abc'
                            , rest: 'def'
                            } */

Furthermore, these parsers can be combined with combinators to create new parsers:

var notAbc = not(symbols('abc'))
notAbc('abc') // failure
notAbc('def') // success
notAbc('abd') // success
notAbc('')    // success

var vowelThenDigit = all(
    [ oneOf('aeiou')
    , oneOf('0123456789')
vowelThenDigit('aa') // failure
vowelThenDigit('c2') // failure
vowelThenDigit('a0') // success

Why use Combigreater?

JavaScript's RegExps can be useful as a parsing aid, but the major problem with them is that they aren't very composable. You can't touch the constituent parts of a RegExp, and you can't take two RegExps and combine them (straightforwardly). You also can't give names to repetitive patterns and reuse them – short of doing some string mincing on them, you have to copy and paste, which is something you should never have to do.

Combigreater solves this problem by making every parser first-class (specifically, a function) and providing combinators that transform them. You can assign parsers to variables and reuse them when building new parsers.

With the help of some user-provided predicates and transforming functions, Combigreater's parsers end up being more powerful than RegExps – not only can a parser return a value based on what it matched, a parser can be transformed so that it does something extra with that value, thus resulting in any sort of value you want. So you have a parser that can not only match text, but spit out an arbitrary value accumulated from it. e.g.,

var digit = oneOf('0123456789')
  , digits = many1(digit)
  , number = map(function (array) {
    return Number(array.join(''))
  }, digits)
number('12') // value is 12
npm loves you