# dunsparse

A parser library using parser combinators, callbacks, and memoization to transparently apply arbitrary parsing and transformation rules.

## Usage no npm install needed!

``````<script type="module">
import dunsparse from 'https://cdn.skypack.dev/dunsparse';
</script>``````

# Dunsparse  Dunsparse is a parser library. It allows you to create parsers by building them up using special functions called parser combinators. For example, if we have two parsing functions a and b where a parses "a" and b parser "b", there is a combinator called sepBy such that sepBy(a, b) will parse "a", "aba", "ababa", and so on. Both successes and errors are cached so that parsing is polynomial instead of exponential. The return value of the parser combinators can be used to do further computations in a parser callback. If the parsing step fails, the further computation will be skipped by thrown error. If it succeeds, the transformed output will be returned.

# Example:

``````//Import the Dunsparse library from the local version in tests
const Parser = require("./parser.js")

var calculator_parser = new Parser({
//the transformative part of the additive parse function works by reducing over a value, operator array.  Every time a value is
//passed, the accumulator is a value.  Every time an operator is passed, the accumulator is a function that can be applied to
//the next value to apply the operation to it and the previous value.
additive: (s) => s.sepBy(s.multiplicative, s.regex(/^[+-]/))().reduce((l, e, i) => i%2 ? n => e === "+" ? l + n : l - n : l(e)),
multiplicative: (s) => s.sepBy(s.atom, s.regex(/^[*/]/))().reduce((l, e, i) => i%2 ? n => e === "*" ? l * n : l / n : l(e)),
atom: (s) => s.any(s.number, s.parens)(),
number: (s) => +s.regex(/^\d+/)(),//convert strings to numbers using unary + coercion
parens: (s) => s.chain(s.str("("), s.additive, s.str(")"))(),//the value of parentheses is the value inside
})

console.log(calculator_parser.parse("6*7+(2+3)*2"))
``````

Documentation for the library can be found in the parser.js file in the github.