-
|
Hi! Thanks for this excellent crate. I have a question about error handling when using The ProblemI'm trying to parse integer literals. The parser should handle two cases:
I've implemented this as Here is a minimal example: use chumsky::prelude::*;
#[derive(Debug)]
enum Token {
Int(i32),
Long(i64),
}
fn main() {
let int = text::int::<&str, extra::Err<Rich<char>>>(10)
.then_ignore(any().filter(char::is_ascii_alphanumeric).not().rewind())
.try_map(|s: &str, span| {
s.parse::<i32>()
.map(Token::Int)
.map_err(|e| Rich::custom(span, e))
});
let long = text::int(10)
.then_ignore(one_of("lL"))
.then_ignore(any().filter(char::is_ascii_alphanumeric).not().rewind())
.try_map(|s: &str, span| {
s.parse::<i64>()
.map(Token::Long)
.map_err(|e| Rich::custom(span, e))
});
let literal = choice((int, long));
// let literal = int.or(long); // yielded the same result.
println!("{:?}", literal.parse("10000000000000000000000l")); // doesn't fit in i64
}This produces the following output: The desired error would be something like "number too large to fit in target type" from the Analysis and QuestionsMy understanding is that the
This leads to my questions:
Thanks for your help! p.s. A related question about error spans with the
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
What you're looking for is the Thanks for the note about the |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your suggestion! I will try to explain the issue briefly. |
Beta Was this translation helpful? Give feedback.
-
|
I see. I understand that this behavior is intentional, and I didn’t mean to suggest it was a bug. Regarding the To control the consumption state, I think a sequential parser combinator could be a good approach, something like For example: |
Beta Was this translation helpful? Give feedback.
I don't think there is a specific notion of 'consumption state' that exists as you might imagine it.
After doing some thinking, I think the correct way to solve it would be to associate an error not just with a location in the input, but a location span, which would allow us to correctly disentangle the
try_mapcase more cleanly and hopefully satisfy both constraints. Unfortunately, this would likely have performance ramifications and would require some non-trivial changes to the guts of the crate. I'll open a long-term issue for the problem.In the meantime, a relatively clean solution might be something like the following pseudocode: