happy-parser
v1.0.25
Published
Package for easy parsing using a monadic parser combinator
Downloads
34
Readme
Node parser combinator
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