separated{0,1}
could accept non-consuming sep
if parser
is consuming.
#325
Labels
C-enhancement
Category: Raise on the bar on expectations
Please complete the following tasks
winnow version
0.5.15
Describe your use case
I have:
parser
that is guarenteed to consume 1 to N token.sep
that may consume 0 to N token.I want to use
separated{0,1}
such thatsep
is the separator andparser
the element.Currently, such a setup may panic in winnow if
sep
matches 0 tokens.Click for minimal reproducible example
Describe the solution you'd like
The implementation is trivial.
In the implementation of
separated1
(andseparated0
) functions, move theif i.eof_offset() == len
block to within theparser.parse_next(i)
match, just before theres.accumulate(o)
, so that the infinite loop check accounts for the option of theparser
being consuming, rather than just thesep
.This successfully prevent infinite loops, as we still guarantee the parser advances and never reaches a fixed point.
A side effect is that it makes
rustfmt
happy and reduces 1 level of indentation.Alternatives, if applicable
For my specific use-case, the
sep
in question are whitespaces, and all I need to do is trim the parser output. I also am considering implementing a streaming tokenizer through theStream
trait to not have to care about whitespaces (and comments) in my parser.Additional Context
When seeing
separated
, people often think of comma-separated list of elements as you'd see them in ALGOL-like languages. But I think whitespace delimited languages (say LISP) it makes sense to have a whitespace separator.This use-case came in as I was designing a simple version of the rust TokenTree syntax element.
The text was updated successfully, but these errors were encountered: