如何在Scala FastParse库中提供明确的错误/失败消息?
我正在使用Li Haoyi的Fastparse库。我有几种情况,我想提供明确的故障消息。
例如:
def courseRE[p: P]: P[Regex] =
P(CharIn("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.|*+[]()-^$").repX(1).!).map { re =>
try { re.r }
catch { case e => failure(s"Ill-formed regular expression: ${re}.") }
}
但是(显然)没有失败
函数。
是否有建议的工作?简单地提出异常不会提供任何上下文信息。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有找到一个很好的解决方案。我不喜欢 @user2963757提出的解决方案,因为它丢失了解析器中有关其想要的内容,所寻找的位置等的所有信息。
在Github上的FastParse问题列表中,这是多次提出的。请参阅问题213 ,问题187 ,第243页和拉动请求24444 。有一些模糊的建议,但据我所知,尚未采取拉动请求(截至2023-02-09)。
到目前为止,我发现的最好的最好的是在可访问的位置定义:
要使用它:
尝试分析
“^cs1 [1345“
y得出来)请注意,必须按照什么来说明故障消息是预期的,不是实际问题。在这种情况下,例外引发的实际错误消息通常无法正常工作。我也没有得到它发现的碎片。
不幸的是,即使是此消息通常都不可用。例如,解析我的一个更大的输入结果,
我希望能够表现出更精确的“未封闭字符类”的误差,但看似不能。
顺便说一句,我查看了文档,源代码和样本解析器(Pythonparse和scalaparse)中的使用示例
fail> fail
parser。找不到任何。唯一的是文档中显示的一个,该文件不与另一个解析器组成。如果有人有更好的解决方案,我仍然很乐意听到它。
I haven't yet found a good solution. I don't like the solution proposed by @user2963757 because it loses all the information from the parser about what it was looking for, where it was looking, etc.
This is raised a number of times in the FastParse issues list on GitHub. See issue 213, issue 187, issue 243, and pull request 244. There are a few vague suggestions but as far as I can tell the pull request hasn't been acted on (as of 2023-02-09).
The best I've found so far is defining this in an accessible location:
To use it:
Trying to parse
"^CS1[1345"
yieldsNotice that the failure message has to be stated in terms of what was expected, not the actual problem. The actual error message thrown by the exception usually doesn't work well in this situation. I'm also not getting the fragment that it found.
Unfortunately, even this message is usually unavailable. For example, parsing a larger piece of my input results in
I'd like to be able to surface the more exact error of "Unclosed character class" but seemingly can't.
By the way, I looked in the documentation, source code, and the sample parsers (PythonParse and ScalaParse) for examples of the use of the
Fail
parser. Can't find any. The only one is the one shown in the documentation, which doesn't compose with another parser.If anyone has a better solution, I'd still love to hear it.
从FastParse 2开始,有一个官方
fail
构造函数以及Pass
构造函数。参见 https://github.com/com-lihaoyi/com-lihaoyi/fastparse/fastparse/fastparse/187As of FastParse 2 there is an official
Fail
constructor as well as aPass
constructor. See https://github.com/com-lihaoyi/fastparse/issues/187