FParsec 默认错误消息

发布于 2024-12-01 13:29:19 字数 443 浏览 2 评论 0原文

假设我正在定义以下解析器:

let identifier = many1Satisfy isLetter //match an identifier
let parser = identifier //our parser is only to match identifiers
test parser " abc" //the text to parse contains a leading space which should yield us an error

解析时,正如人们所期望的那样,发生了错误:

Failure: Error in Ln: 1 Col: 1
 abc
^
Unknown Error(s)

我很好奇为什么它不能确定问题是他正在等待一封信但找不到一封信。我是否希望自己以某种方式将该信息添加到解析器中?

Let's say I am defining the following parser:

let identifier = many1Satisfy isLetter //match an identifier
let parser = identifier //our parser is only to match identifiers
test parser " abc" //the text to parse contains a leading space which should yield us an error

When parsing, an error ocurrs, as one would expect:

Failure: Error in Ln: 1 Col: 1
 abc
^
Unknown Error(s)

I am curious on why can't it decide the problem is that he's expecting a letter and can't find one. Am I expected to add that info somehow to the parser by myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-12-08 13:29:19

至于为什么它不能告诉你出了什么问题:
我猜这是由于“many1Satisfy” - 你看到这个组合器包装了另一个解析器,我猜它只是不知道在“many1”的哪个状态下发生了错误,而不是什么错误 - 所以它说“未知错误(s )”

这应该有效:

let ws = spaces
let identifier = ws >>. (many1Satisfy isLetter) //match an identifier, ignore whitespaces infront
let parser = identifier //our parser is only to match identifiers
test parser " abc"

as to why it can't tell you whats wrong:
I guess this is due to the "many1Satisfy" - you see this combinator wraps another parser and I guess it just don't know in which state of "many1" an error occured and not what error - so it says "Unknown Error(s)"

this should work:

let ws = spaces
let identifier = ws >>. (many1Satisfy isLetter) //match an identifier, ignore whitespaces infront
let parser = identifier //our parser is only to match identifiers
test parser " abc"
青巷忧颜 2024-12-08 13:29:19

空格不是常规字符。在您的情况下,您需要忽略空格,为此您需要使用忽略空格的解析器来组合解析器,并使用这个新的组合解析器来解析标识符。

检查4.6 处理空格

White space is not a regular character. In your case you need to ignore the white spaces and for that you need to compose your parser with the parser which ignores white spaces and use this new composed parser to parse the identifier.

Check 4.6 Handling whitespace

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文