rust - 使用 nom 捕获整个连续的匹配输入

标签 rust parser-combinators

我希望应用一系列 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/

相关文章:

arrays - 在 Rust 中,我可以在不对值进行硬编码的情况下实例化我的 const 数组吗?编译时评估?

scala - 如何过滤解析器组合器中的保留字?

scala - 在 Scala 中为解析器组合器创建测试

f# - FParsec:如何从错误消息中省略 `many` 解析器失败

windows - 为什么 usize::max_value() 在 64 位 Windows 上返回无符号 32 位整数的最大值?

rust - std::cell::Ref <T>和&T的区别

macros - 如何在宏中匹配 Rust 的 `if` 表达式?

rust - 我如何借用 RefCell<HashMap>、找到一个键并返回对结果的引用?

f# - FParsec:回溯 `sepBy`

scala - 如何跳过空格但将其用作解析器组合器中的标记分隔符