https://github.com/dalance/nom-greedyerror
Custom error type of nom to improve accuracy of error position
https://github.com/dalance/nom-greedyerror
nom rust rust-library
Last synced: 2 months ago
JSON representation
Custom error type of nom to improve accuracy of error position
- Host: GitHub
- URL: https://github.com/dalance/nom-greedyerror
- Owner: dalance
- License: apache-2.0
- Created: 2019-10-16T03:11:10.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-23T00:26:31.000Z (almost 3 years ago)
- Last Synced: 2025-06-26T05:45:38.488Z (7 months ago)
- Topics: nom, rust, rust-library
- Language: Rust
- Homepage:
- Size: 21.5 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# nom-greedyerror
Custom error type of [nom](https://github.com/Geal/nom) to improve accuracy of error position.
[](https://github.com/dalance/nom-greedyerror/actions)
[](https://crates.io/crates/nom-greedyerror)
[](https://docs.rs/nom-greedyerror)
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`.
```rust
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
```Cargo.toml
[dependencies]
nom-greedyerror = "0.5.0"
```
## Example
```rust
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>>(
input: Span<'a>,
) -> IResult, (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::>(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::>(Span::new("abc012:::"));
dbg!(&error);
match error {
Err(Error(e)) => assert_eq!(error_position(&e), Some(6)),
_ => (),
};
}
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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.