-
-
Notifications
You must be signed in to change notification settings - Fork 291
Closed
Description
Depending on the context, <something> | ""
(or "" | <something>
) may be a clearer way to spell out Opt(...)
; I believe this almost works except for the fact that Literal("")
currently raises an error asking to use Empty()
instead. Could this restriction be lifted?
I see various ways to lift the restruction, depending on what you are exactly willing to accept. One would be to just directly allow Literal("")
(if you worry about the loss in perf, you could define Literal.__new__
to return Empty()
in that case, although that's a bit too magical in my taste). Or you could change ParseElementEnhance.__init__
at
Lines 4345 to 4353 in 966d6fd
def __init__(self, expr: Union[ParserElement, str], savelist: bool = False): | |
super().__init__(savelist) | |
if isinstance(expr, str_type): | |
if issubclass(self._literalStringClass, Token): | |
expr = self._literalStringClass(expr) | |
elif issubclass(type(self), self._literalStringClass): | |
expr = Literal(expr) | |
else: | |
expr = self._literalStringClass(Literal(expr)) |
expr == ""
there (or rather len(expr) == 0
to support bytes too) and return Empty()
. Finally, you could also change ParserElement.__or__/__ror__
to detect that case and construct an Optional(...)
as needed (this may be the simplest from an explanation point of view: "|
constructs a MatchFirst, except that | ""
constructs an Optional").
I can make a PR if you agree with any of these solutions.
Metadata
Metadata
Assignees
Labels
No labels