happy-parser

Package for easy parsing using a monadic parser combinator

Usage no npm install needed!

<script type="module">
  import happyParser from 'https://cdn.skypack.dev/happy-parser';
</script>

README

Node parser combinator

travis-ci

Immutable monadic parser combinator for node javascript.

Example

const parsec = require('happy-parser');

const sum = (x, y) => x + y, 
      rest = (x, y) => x - y

const
  termOperations = parsec.Parser.operations([ parsec.char('+'), sum ], [ parsec.char('-'), rest ]).trim(),
  factorOperations = parsec.Parser.operations([ parsec.char('^'), Math.pow.bind() ]).trim()

const factor = parsec.lazy(() => 
            parsec.int.or(expr.between(parsec.char('('), parsec.char(')'))).trim()
          ),
      term = parsec.lazy(() => factor.chainRight(factorOperations)),
      expr = term.chain(termOperations).trim()

console.log(expr.parse(' 3^2 + (4 - 7) '))

Check also the documentation and tests