-
The readme has the example of a parser that matches an identifier: final id = letter() & (letter() | digit()).star(); I happened to need something equivalent to a reverse identifier: something that can have foos and bars up until the end, where it must end with a foo. I tried to do something like final id = (letter() | digit()).star() & letter(); But I understand that this doesn't work because there is no backtracking, correct? My instinct was to use final id =
(letter() | digit()).starLazy(letter() & ((letter() | digit()).not())) &
letter(); Is there an idiomatic or egonomic way to write this? I did come up with the following, but I always feel like final id = [letter(), digit()]
.toChoiceParser()
.plus()
.where((values) => letter().accept(values.last)); (Using |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Your example with Parser reverseId(Parser inner, Parser terminator) =>
inner.starLazy(terminator & inner.not()) & terminator;
final id = reverseId(letter() | digit(), letter()); |
Beta Was this translation helpful? Give feedback.
-
I have a new twist on this: Is it possible to match a "reverse ID" up to a trailing "inner" character? In other words:
I am reimplementing a feature that was originally using regex and this case was handled by backtracking. I can't see a way to fix this without breaking other edge cases. |
Beta Was this translation helpful? Give feedback.
Your example with
starLazy
is what I would have suggested. Only thing I would do is to extract a helper method, to make it more readable, i.e.