Custom error type of nom to take a deepest error.
The default error types of nom ( (I, ErrorKind)
and VerboseError
) take a last challenged error at alt
combinator.
Alternatively GreedyError
of nom-greedyerror take a deepest error.
nom must be 5.0.0 or later.
Cargo.toml
[dependencies]
nom-greedyerror = "0.1.2"
```rust use super::*; use nom::branch::alt; use nom::character::complete::{alpha1, digit1}; use nom::error::{ParseError, VerboseError}; use nom::sequence::tuple; use nom::IResult; use nom_locate::LocatedSpan;
type Span<'a> = LocatedSpan<&'a str>;
fn parser<'a, E: ParseError>>( input: Span<'a>, ) -> IResult, (Span<'a>, Span<'a>, Span<'a>), E> { alt(( tuple((alpha1, digit1, alpha1)), tuple((digit1, alpha1, digit1)), ))(input) }
fn test() {
// VerboseError failed at
// abc012:::
// ^
let error = parser::
// GreedyError failed at
// abc012:::
// ^
let error = parser::<GreedyError<Span>>(Span::new("abc012:::"));
match error {
Err(nom::Err::Error(e)) => assert_eq!(error_position(&e), Some(6)),
_ => (),
};
} ```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.