Capturing all adjacent matching input with number

I want to apply a series of nom parses and return the full &strone that matches. I want to match form lines a+bc+. Using an existing macro chain!, I can get pretty close:

named!(aaabccc <&[u8], &str>,
   map_res!(
       chain!(
           a: take_while!(is_a) ~
               tag!("b") ~
               take_while!(is_c) ,
           || {a}
           ),
       from_utf8
   ));

Where

fn is_a(l: u8) -> bool {
   match l {
       b'a' => true,
       _ => false,
   }
}

fn is_c(l: u8) -> bool {
    match l {
        b'c' => true,
        _ => false,
    }
}

Say we have aaabccc. The above parser will match the input, but only "aaa" will be returned. What I would like to do is return 'aaabccc', the original input.

chain!for this is not a suitable macro, but there was no other that would seem more correct. What would be the best way to do this?


At the time of this writing, I used nom 1.2.2and rustc 1.9.0-nightly (a1e29daf1 2016-03-25).

+4
1

, recognized!:

,

:

#[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! - , , .

+3

All Articles