nom::try_parse! [] [src]

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

A bit like std::try!, this macro will return the remaining input and parsed value if the child parser returned Done, and will do an early return for Error and Incomplete this can provide more flexibility than chain! if needed


 fn take_add(input:&[u8], size: u8) -> IResult<&[u8],&[u8]> {
   let (i1, sz)     = try_parse!(input, be_u8);
   let (i2, length) = try_parse!(i1, expr_opt!(size.checked_add(sz)));
   let (i3, data)   = try_parse!(i2, take!(length));
   return Done(i3, data);
 }
let arr1 = [1, 2, 3, 4, 5];
let r1 = take_add(&arr1[..], 1);
assert_eq!(r1, Done(&[4,5][..], &[2,3][..]));

let arr2 = [0xFE, 2, 3, 4, 5];
// size is overflowing
let r1 = take_add(&arr2[..], 42);
assert_eq!(r1, Error(Position(ErrorKind::ExprOpt,&[2,3,4,5][..])));