-
I am trying to attach some context to an error and have it propagate throughout the parser but it seems I don't have the right way. MRE https://www.rustexplorer.com/b/9v4c7c (refresh if it shows an error) The final error does not contain the context information when it's the first parser from the
Any help would be appreciated! Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
epage
May 1, 2025
Replies: 1 comment 5 replies
-
The problem is One option is to use /*
[dependencies]
winnow = "0.7"
*/
use winnow::{
combinator::{alt, not, cut_err},
error::{StrContext, StrContextValue},
Parser as _, ModalResult,
};
fn multi<'s>(input: &mut &'s str) -> ModalResult<(&'s str, ())> {
(
"/**",
cut_err(not('*'))
.context(StrContext::Label("delimiter"))
.context(StrContext::Expected(StrContextValue::Description("/**"))),
)
.parse_next(input)
}
fn single<'s>(input: &mut &'s str) -> ModalResult<(&'s str, ())> {
("///", cut_err(not('/'))).parse_next(input)
}
fn either<'s>(input: &mut &'s str) -> ModalResult<(&'s str, ())> {
alt((multi, single)).parse_next(input)
}
fn main() {
let res = either.parse("/***");
println!("{res:?}");
} Another option is to do something like (
'/',
dispatch!{any;
'/' => not('/'),
'*' => not('*'),
_ => fail,
}
) |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
beeb
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is
alt
. It always ends with the last error.One option is to use
cut_err
to "commit" to the result of one branch ofalt