This repository was archived by the owner on Apr 2, 2026. It is now read-only.
Choice between ignored and non-ignored parsers, when padded_by fails #967
Unanswered
BinaryQuantumSoul
asked this question in
Q&A
Replies: 1 comment 2 replies
-
|
I found a way better workaround: add For first example, we have: let val = text::int::<_, extra::Err<Simple<char>>>(10);
let comment = just("//").then(none_of('\n').repeated());
let parser = val
.map(Some)
.or(comment.to(None))
.padded()
.repeated()
.collect::<Vec<_>>()
.map(|v| v.into_iter().flatten().collect());
assert_eq!(parser.parse("123 //abc").into_output(), Some(vec!["123"]));
assert_eq!(parser.parse("//abc").into_output(), Some(vec![]));For second: let parser = none_of::<_, _, extra::Err<Simple<char>>>('\\')
.map(Some)
.or(
just('\\').ignore_then(
just('n')
.to('\n')
.map(Some)
.or(just('0').to(None)),
),
)
.repeated()
.collect::<Vec<_>>()
.map(|v| v.into_iter().flatten().collect());
assert_eq!(parser.parse("a\\n\\0b").into_output(), Some("a\nb".to_string()));This is still quite verbose and I think chumsky would benefit form some sort of |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I experience a recurring issue when trying to
choicebetween some parsers that ouput()and others that do not.Issue
For example, I have comments, that should be ignored and integers that should not.
Intuitively, if would use an
oror achoice, but the types don't match:It seems, from the examples, that the recommended way would be:
But, as you can see it breaks when we don't have any int at all, but only one comment.
Workaround case 1
Right now, I have to use a workaround.
I map comments to a special
Skipvariant of my token enum and filter out the skips after parsingWorkaround case 2
The workaround definitely won't work for everything, for example, let's say I want to replace, during parsing, occurrences of
\\nwith\nand remove occurrences of\\0.This parser will not type check because first has
charreturn type and second has().The workaround here is even less elegant: convert everything to string and concatenate:
TL;DR
Basically I want a way to allow for
choicesororto have ignored()parsers which are ignored in the case of arepeat().collect().Is there a more idiomatic, less cumbersome way?
Beta Was this translation helpful? Give feedback.
All reactions