npm.io
1.0.25 • Published 9 years ago

happy-parser

Licence
GPL-2.0
Version
1.0.25
Deps
0
Vulns
0
Weekly
0
Stars
1

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

Keywords