nom::cond_reduce! [] [src]

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

cond_reduce!(bool, I -> IResult<I,O>) => I -> IResult<I, O> Conditional combinator with error

Wraps another parser and calls it if the condition is met. This combinator returns an error if the condition is false

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

 let b = true;
 let f = closure!(&'static[u8],
   cond_reduce!( b, tag!("abcd") )
 );

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

 let b2 = false;
 let f2 = closure!(&'static[u8],
   cond_reduce!( b2, tag!("abcd") )
 );
 assert_eq!(f2(&a[..]), Error(Err::Position(ErrorKind::CondReduce, &a[..])));