Open eikendev opened 1 year ago
Background: nom does not (fully) support backtracking, meaning a valid input might not be accepted.
use nom::{
branch::alt,
bytes::complete::tag,
combinator::{all_consuming, recognize},
multi::many0_count,
sequence::terminated,
IResult,
};
use regex::Regex;
fn main() {
let input = "aaaaab";
println!("{:?}", parser(input));
let re = Regex::new(r"^(ab|a)*b$").unwrap();
println!("{:?}", re.find(input));
}
fn parser(i: &str) -> IResult<&str, &str> {
all_consuming(terminated(
recognize(many0_count(alt((tag("ab"), tag("a"))))),
tag("b"),
))(i)
}
This will print
$ cargo run
Err(Error(Error { input: "", code: Tag }))
Some(Match { text: "aaaaab", start: 0, end: 6 })
Of course, in this simple example there would be workarounds, such as swapping ab
and a
. However, this is not possible in the general case, leaving the problem unsolved for more complex parser combinators.
For now, there are no plans to support the obsolete syntax.
See Section 4 for some background:
The obsolete syntax leaves room for ambiguity, which nom cannot deal with. Instead, a more powerful parser would be required. Options are: