8 releases (4 breaking)

0.5.0 Mar 23, 2023
0.4.0 Aug 31, 2021
0.3.1 Nov 27, 2020
0.2.0 Apr 1, 2020
0.1.2 Oct 16, 2019

#4 in #accuracy

Download history 990/week @ 2024-08-01 912/week @ 2024-08-08 1048/week @ 2024-08-15 1062/week @ 2024-08-22 1045/week @ 2024-08-29 959/week @ 2024-09-05 1447/week @ 2024-09-12 1209/week @ 2024-09-19 1270/week @ 2024-09-26 1181/week @ 2024-10-03 826/week @ 2024-10-10 882/week @ 2024-10-17 950/week @ 2024-10-24 957/week @ 2024-10-31 859/week @ 2024-11-07 841/week @ 2024-11-14

3,757 downloads per month
Used in 16 crates (6 directly)

MIT/Apache

14KB
207 lines

nom-greedyerror

Custom error type of nom to improve accuracy of error position.

Actions Status Crates.io Docs.rs

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.

For example, the following parser accepts string like abc012abc or 012abc012.

alt((
    tuple((alpha1, digit1, alpha1)),
    tuple((digit1, alpha1, digit1)),
))(input)

If abc012::: is provided, we expect that the parse error happens at:

abc012:::
      ^

But VerboseError reports the parse error at:

abc012:::
^

This is because the last challenged parser is tuple((digit1, alpha1, digit1)) and it is failed. GreedyError reports the parse error at the expected position.

Requirement

nom must be 5.0.0 or later.

Usage

[dependencies]
nom-greedyerror = "0.5.0"

Example

use nom::branch::alt;
use nom::character::complete::{alpha1, digit1};
use nom::error::{ErrorKind, ParseError, VerboseError};
use nom::sequence::tuple;
use nom::Err::Error;
use nom::IResult;
use nom_greedyerror::{error_position, GreedyError, Position};
use nom_locate::LocatedSpan;

type Span<'a> = LocatedSpan<&'a str>;

fn parser<'a, E: ParseError<Span<'a>>>(
    input: Span<'a>,
) -> IResult<Span<'a>, (Span<'a>, Span<'a>, Span<'a>), E> {
    alt((
        tuple((alpha1, digit1, alpha1)),
        tuple((digit1, alpha1, digit1)),
    ))(input)
}

fn main() {
    // VerboseError failed at
    //   abc012:::
    //   ^
    let error = parser::<VerboseError<Span>>(Span::new("abc012:::"));
    dbg!(&error);
    match error {
        Err(Error(e)) => assert_eq!(e.errors.first().map(|x| x.0.position()), Some(0)),
        _ => (),
    };

    // GreedyError failed at
    //   abc012:::
    //         ^
    let error = parser::<GreedyError<Span, ErrorKind>>(Span::new("abc012:::"));
    dbg!(&error);
    match error {
        Err(Error(e)) => assert_eq!(error_position(&e), Some(6)),
        _ => (),
    };
}

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~1MB
~21K SLoC