我希望应用一系列 nom解析器并返回匹配的完整 &str
。我想匹配 a+bc+
形式的字符串。使用现有的 chain!
macro我可以很接近:
named!(aaabccc <&[u8], &str>,
map_res!(
chain!(
a: take_while!(is_a) ~
tag!("b") ~
take_while!(is_c) ,
|| {a}
),
from_utf8
));
在哪里
fn is_a(l: u8) -> bool {
match l {
b'a' => true,
_ => false,
}
}
fn is_c(l: u8) -> bool {
match l {
b'c' => true,
_ => false,
}
}
假设我们有“aaabccc”作为输入。上面的解析器将匹配输入,但只会返回 'aaa'。我想做的是返回原始输入“aaabccc”。
chain!
不是正确的宏,但没有其他宏看起来更正确。最好的方法是什么?
在撰写本文时,我正在使用 nom 1.2.2
和 rustc 1.9.0-nightly (a1e29daf1 2016-03-25)
。
最佳答案
看起来好像你想要recognized!
:
if the child parser was successful, return the consumed input as produced value
还有一个例子:
#[macro_use]
extern crate nom;
use nom::IResult;
fn main() {
assert_eq!(aaabccc(b"aaabcccddd"), IResult::Done(&b"ddd"[..], "aaabccc"));
}
named!(aaabccc <&[u8], &str>,
map_res!(
recognize!(
chain!(
take_while!(is_a) ~
tag!("b") ~
take_while!(is_c),
|| {}
)
),
std::str::from_utf8
)
);
fn is_a(l: u8) -> bool {
match l {
b'a' => true,
_ => false,
}
}
fn is_c(l: u8) -> bool {
match l {
b'c' => true,
_ => false,
}
}
如果您不关心这些值,我不确定 chain!
是否是组合顺序解析器的最佳方式,但它在这种情况下有效。
关于rust - 使用 nom 捕获整个连续的匹配输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36300373/