Macro nom::return_error [] [src]

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

Prevents backtracking if the child parser fails

This parser will do an early return instead of sending its result to the parent parser.

If another return_error! combinator is present in the parent chain, the error will be wrapped and another early return will be made.

This makes it easy to build report on which parser failed, where it failed in the input, and the chain of parsers that led it there.

Additionally, the error chain contains number identifiers that can be matched to provide useful error messages.

    named!(err_test, alt!(
      tag!("abcd") |
      preceded!(tag!("efgh"), return_error!(ErrorKind::Custom(42),
                 tag!("ijkl")                                        >>
            res: return_error!(ErrorKind::Custom(128), tag!("mnop")) >>
    let a = &b"efghblah"[..];
    let b = &b"efghijklblah"[..];
    let c = &b"efghijklmnop"[..];

    let blah = &b"blah"[..];

    let res_a = err_test(a);
    let res_b = err_test(b);
    let res_c = err_test(c);
    assert_eq!(res_a, Error(error_node_position!(ErrorKind::Custom(42), blah, error_position!(ErrorKind::Tag, blah))));
    assert_eq!(res_b, Error(error_node_position!(ErrorKind::Custom(42), &b"ijklblah"[..],
      error_node_position!(ErrorKind::Custom(128), blah, error_position!(ErrorKind::Tag, blah))))