Releases: FelipeSharkao/chunky-parser
2.0.0
This version reworks how tokens and recursive parsers works.
Instead of having many primitive parsers, we only have TokenParser
. This parser parses a string or a regex, always have a name and is automatically cached.
const num = new TokenParser("number", /\d+/)
const plus = new TokenParser("plus", "+")
const minus = new TokenParser("minus", "-")
const lparen = new TokenParser("left parenthesis", "(")
const rparen = new TokenParser("right parenthesis", ")")
Recursive parsers are now handled by withPrecedence
instead of oneOf
(oneOf
being reserved for simple rules), left and right recursive nodes are accessed by .left
and .right
.
const expr: ParserWithPrecedence<...> = withPrecedence(
seq(lparen, () => expr, rparen),
num,
seq(() => expr.left, oneOf(plus, minus), () => expr.right),
)
This is a little more word-y, but allows for greater performance improvement and matching edge cases, since the behavior is explicit.
1.2.1
Improve the recursion handling of oneOf
, so left recursive tree are compiled correctly.
Before, if there was a rule like that:
const expr = oneOf(() => sum, num)
const sum = map(
seq(expr, str("+"), expr),
(res) => ({ left: res.value[0], right : res.value[2] })
)
An input of 1 + 2 + 3
would result in a tree like:
{
left: "1",
right: { left: "2", right: "3" },
}
Which is clearly the inverse of what should happen. This is fine for a sum, but for some operations (like a Haskell-like function call) it is not. Now it parses the correct tree:
{
left: { left: "1", right: "2" },
right: "3"
}
The approach used still need improvements, as it is currently creating too many stack frames (I had to put a hard limit on them as it would give me a stack overflow), but any work related to it will come in the next big version (I will have to re-work some aspects of how the parser works, so it will be a breaking change)
1.1.0
This version adds an internal stack and counter to oneOf
, so recursion patters are handle gracefully