nom::error [] [src]

macro_rules! 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 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"), error!(ErrorKind::Custom(42),
          chain!(
                 tag!("ijkl")              ~
            res: error!(ErrorKind::Custom(128), tag!("mnop")) ,
            || { res }
          )
        )
      )
    ));
    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(NodePosition(ErrorKind::Custom(42), blah, Box::new(Position(ErrorKind::Tag, blah)))));
    assert_eq!(res_b, Error(NodePosition(ErrorKind::Custom(42), &b"ijklblah"[..],
      Box::new(NodePosition(ErrorKind::Custom(128), blah, Box::new(Position(ErrorKind::Tag, blah))))))
    );