Skip to content

Support ... | "" as alias for Opt(...) #412

@anntzer

Description

@anntzer

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

pyparsing/pyparsing/core.py

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))
to special-case 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions