[英]In parboiled2, how should I report an error in a parser action?
What's the best way to report an error in a parser action in parboiled2 (I'm using v 2.1.4)? 在parboiled2中报告解析器操作中的错误的最佳方法是什么(我使用的是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. 我尝试调用
fail
,但在解析器操作中似乎没有效果。 Also, I can't tell how I should provide the stack value to the test
rule. 另外,我无法告诉我应该如何为
test
规则提供堆栈值。 Do I simply throw a ParseError
exception? 我只是抛出一个
ParseError
异常吗?
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. 您可以使用
test
。 The trick is that actions can also return a Rule
. 诀窍是动作也可以返回
Rule
。
def Index = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> {s =>
val i = s.toInt
test(i <= SomeMaxIndexValue) ~ push(i)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.