-
Is is possible to ensure complete parsing without the use of Background: My actual use case is much more complex but let's suppose I have a grammar like
I want to use this grammar to parse both numbers and sums. When parsing a number I do not want to allow something like Of course, I can work around this problem by adding a rule
and run Anyhow, let's now suppose I also want to parse sums and do not want to accept, e.g., "1+1+1". I would end up with yet another rule
which is (mostly) a duplication of Is there any possibility to avoid all these Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Not sure what all the rules look like, but if they are non-overlapping, one could perhaps have a single joint rule |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer. Unfortunately, the rules are overlapping. To solve the issue, I went the long way and implemented my own custom "parser extension". use pest::{
error::{Error, ErrorVariant},
iterators::Pairs,
Parser, Position, RuleType,
};
pub trait CompleteParser<R: RuleType>: Parser<R> {
fn parse_complete(rule: R, input: &str) -> Result<Pairs<'_, R>, Error<R>> {
Self::parse(rule, input).and_then(|pairs| {
if pairs.as_str() == input {
Ok(pairs)
} else {
Err(Error::new_from_pos(
ErrorVariant::CustomError {
message: String::from("expected EOI"),
},
Position::new(input, pairs.as_str().len()).unwrap(),
))
}
})
}
}
impl<P: Parser<R>, R: RuleType> CompleteParser<R> for P {} |
Beta Was this translation helpful? Give feedback.
Thanks for the answer. Unfortunately, the rules are overlapping. To solve the issue, I went the long way and implemented my own custom "parser extension".