What's the best way to report an error in a parser action in parboiled2 (I'm using v 2.1.4)?
For example, say I want to read an integer value and report an error if its not within the expected range? I tried calling fail
, but that doesn't appear to be valid within a parser action. Also, I can't tell how I should provide the stack value to the test
rule. Do I simply throw a ParseError
exception?
To be a little more specific, consider the following rule:
def Index = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> {s => // s is a String
val i = s.toInt
if(i > SomeMaxIndexValue) ??? // What do I put here?
else i
}
}
You can use test
for that. The trick is that actions can also return a Rule
.
def Index = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> {s =>
val i = s.toInt
test(i <= SomeMaxIndexValue) ~ push(i)
}
}