在 parboiled2 中,我应该如何报告解析器操作中的错误?
In parboiled2, how should I report an error in a parser action?
在 parboiled2(我使用的是 v 2.1.4)中报告解析器操作错误的最佳方式是什么?
比如我想读取一个整数值,如果不在预期范围内就报错?我尝试调用 fail
,但这在解析器操作中似乎无效。另外,我不知道应该如何为 test
规则提供堆栈值。我只是抛出一个 ParseError
异常吗?
更具体一点,考虑以下规则:
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
}
}
您可以使用 test
。诀窍是动作也可以 return a Rule
.
def Index = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> {s =>
val i = s.toInt
test(i <= SomeMaxIndexValue) ~ push(i)
}
}
在 parboiled2(我使用的是 v 2.1.4)中报告解析器操作错误的最佳方式是什么?
比如我想读取一个整数值,如果不在预期范围内就报错?我尝试调用 fail
,但这在解析器操作中似乎无效。另外,我不知道应该如何为 test
规则提供堆栈值。我只是抛出一个 ParseError
异常吗?
更具体一点,考虑以下规则:
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
}
}
您可以使用 test
。诀窍是动作也可以 return a Rule
.
def Index = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> {s =>
val i = s.toInt
test(i <= SomeMaxIndexValue) ~ push(i)
}
}