Two exactly opposite errors trying to use map_ctx
#487
Replies: 4 comments 1 reply
-
Edit: It could possibly be intended behavior, but I feel like that it's certainly odd, especially given that I am 99% certain this is a bug in the definition of impl<'a, I, O, E, A, F, Ctx> ParserSealed<'a, I, O, E> for MapCtx<A, F>
where
I: Input<'a>,
E: ParserExtra<'a, I>,
A: Parser<'a, I, O, extra::Full<E::Error, E::State, Ctx>>,
F: Fn(&E::Context) -> Ctx,
Ctx: 'a,
{
/* snip */
} This means that it only implements This also doesn't have a trivial solution at this time because if you try and implement the correct
This doesn't work for any types which are going to map the context, because then we are implementing it where This little snippet below is enough to make the compiler error, and shows how why this errors: fn test<'a, F, P>(f: F, p: P) -> impl Parser<'a, &'a str, (), extra::Context<usize>> + Clone
where
P: Clone,
F: Clone,
MapCtx<F, P>: Parser<'a, &'a str, (), extra::Context<usize>> + Clone,
{
MapCtx {
parser: p,
mapper: f,
}
} @zesterer Is it intended that you cannot map from one type to another? I notice that the example compiles just fine, up until you add a little let upper = just(b'0').configure(|cfg, ctx: &u8| cfg.seq(*ctx));
// Uncomment the `as char` for a load of fun
let mapped = map_ctx(|c: &u8| c.to_ascii_uppercase() /* as char */, upper);
let inc = one_of::<_, _, extra::Default>(b'a'..=b'z')
.ignore_with_ctx(mapped)
.slice()
.repeated()
.at_least(1)
.collect::<Vec<_>>();
assert_eq!(
inc.parse(b"aAbB" as &[_]).into_result(),
Ok(vec![b"aA" as &[_], b"bB"])
);
assert!(inc.parse(b"aB").has_errors()); If this is intended behavior, I would probably want to consider renaming @Cookie04DE Sorry for the multiple tags, but it to sum up a long wall of text. Currently what you are after isn't supported. You can only modify the value that you are mapping, and therefor cannot change it's type. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for the quick and thorough answer @Zij-IT! |
Beta Was this translation helpful? Give feedback.
-
I'm just glad I could figure out what was going on to be honest. Those compiler messages were weird as weird gets. Out of curiosity, what are you currently attempting to parse that is hindered by this? |
Beta Was this translation helpful? Give feedback.
-
I am parsing something akin to Rust's raw string literals where the amount of hashtags you use decides how many hashtags followed by a double quote you can use inside the literal. |
Beta Was this translation helpful? Give feedback.
-
I have the following:
I get this error:
This suggests to me that instead of the closure taking a
&usize
it should instead take&String
. That's why I wrote the (currently commented out) line below.This doesn't work either however:
It's the exact oppsite error.
How do I use
map_ctx
?Beta Was this translation helpful? Give feedback.
All reactions