Scala PackratParser ignores failure parser
Asked Answered
E

1

43

I have a parser that was written using Scala's RegexParsers - link

It had some serious performance problems when parsing a grammar which had deeply nested expressions. As such I have created a version where I mix in Scala's PackratParsers - link

The Packrat version does not exhibit the same performance issue and correctly parses the grammar. However, when I provide an invalid grammar for testing, e.g. this

The old (non-packrat) parser used to correctly report the 'Invalid rule' failure, via the failure parser combinator | failure("Invalid rule") here - link

When using the packrat-parser version, if I enable tracing I can see from the trace that the failure is created just as it is in the non-packrat version, however the PackratParser seems to ignore this and always return failure: Base Failure instead.

Is there something different about failure handling when using PackratParsers which I need to understand?

Ebsen answered 27/3, 2015 at 12:40 Comment(0)
L
1

Seems that you need to use err("Invalid rule") instead of failure, as it guarantees that no backtracking will be performed.

Liva answered 24/7, 2015 at 1:17 Comment(1)
If I use err("Invalid rule") instead of failure("Invalid rule") then because backtracking has stopped, many of my other rules that were previously valid start returning err("Invalid rule") which is not correct.Ebsen

© 2022 - 2024 — McMap. All rights reserved.