gpt4 book ai didi

parsing - 使用 nom 匹配行尾或文件尾

转载 作者:行者123 更新时间:2023-11-29 08:07:52 25 4
gpt4 key购买 nike

我正在尝试使用 nom 解析一个字符串,该字符串要么被换行符终止,要么在使用后到达输入末尾。我有以下似乎应该编译的代码:

named!(am_implied <AddressingMode>,
do_parse!(
opt!(space) >>
alt!(
line_ending |
eof!()
) >>
(AddressingMode::Implied)
)
);

失败并显示以下消息:

error[E0282]: unable to infer enough type information about `E`
--> src/lib.rs:181:1
|
181 | named!(am_implied <AddressingMode>,
| ^ cannot infer type for `E`
|
= note: type annotations or generic parameter binding required

我相信上面的代码应该可以编译,因为下面的代码可以编译:

named!(am_implied <AddressingMode>,
do_parse!(
opt!(space) >>
line_ending >>
eof!() >>
(AddressingMode::Implied)
)
);

line_endingeof! 解析器未在 alt! 解析器中使用时,我很困惑为什么这会起作用,但是当他们失败时。我想知道在 line_endingeof! 上匹配的正确解决方案。

最佳答案

这看起来像是 this issue in nom ,还有这个 WIP PR .本质上,一些 nom 宏没有提供足够的类型提示,因此推断失败。

建议的解决方法是将一些子解析器拆分为单独的解析器,以帮助进行类型推断,但在这种情况下我尝试这样做时,这对我不起作用。

关于parsing - 使用 nom 匹配行尾或文件尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41658386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com