What is the cheapest-by-allocation approach for 0-or-1 elements? #57
Unanswered
shanesveller
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I'd suggest something like this: a.then(b.then(c.then(d.then(e
.repeated())
.or_not())
.or_not())
.or_not()) (Chumsky's which will produce the following output type, as well as guaranteeing the order you mentioned. (A, Option<(B, Option<(C, Option<(D, Vec<E>)>)>)>) Provided none of EDIT: Having reread your comment, I realise that this doesn't do what you want (it doesn't allow for skipping letters). Something like this should work instead. a.then(b.or_not().then(c.or_not()).then(d.or_not()).then(e.repeated())) which will produce an output type of (((((A, Option<A>), Option<B>), Option<C>), Option<D>), Vec<E>) Again, this parser will not allocate if none of |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thanks for this library, I'm really enjoying it so far! I'm dabbling with Chumsky today and I have a pattern to match that loosely distills to input lines like:
producing a struct roughly like:
where A is strictly mandatory, and all of B-E are optional, but must appear in that particular order if present (i.e. D cannot appear before C if C is present). B is a single space-delimited string that is present/absent, same for C. D should hungrily consume the rest of the line until E matches (I'm about to read #39 for this one), and then E is 0-or-more delimited strings.
So these input lines are also semantically valid:
I currently have, in pseudocode:
with
padded
sprinkled in liberally. Then to build my struct at the end of the line, I'm doing a lot ofsome_field_result.into_iter().next()
to produce the desiredOption
. Did I overlook a more helpful method in the documentation to satisfy this goal? I believe my intent is comparable to nom'snom::combinator::opt
.Beta Was this translation helpful? Give feedback.
All reactions