happy-parser

1.0.25 • Public • Published

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

Package Sidebar

Install

npm i happy-parser

Weekly Downloads

4

Version

1.0.25

License

GPL-2.0

Last publish

Collaborators

  • agustincb