Macro nom::cond[][src]

macro_rules! cond {
    ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { ... };
    ($i:expr, $cond:expr, $f:expr) => { ... };
}

cond!(bool, I -> IResult<I,O>) => I -> IResult<I, Option<O>> Conditional combinator

Wraps another parser and calls it if the condition is met. This combinator returns an Option of the return type of the child parser.

This is especially useful if a parser depends on the value returned by a preceding parser in a do_parse!.

 let b = true;
 let f: Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
   cond!( b, tag!("abcd") ))
 );

 let a = b"abcdef";
 assert_eq!(f(&a[..]), Ok((&b"ef"[..], Some(&b"abcd"[..]))));

 let b2 = false;
 let f2:Box<Fn(&'static [u8]) -> IResult<&[u8],Option<&[u8]>>> = Box::new(closure!(&'static[u8],
   cond!( b2, tag!("abcd") ))
 );
 assert_eq!(f2(&a[..]), Ok((&b"abcdef"[..], None)));