erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.8k stars 126 forks source link

implement Node.__bool__? #184

Open smason opened 2 years ago

smason commented 2 years ago

I've just been using your library for parsing some text and it's been very useful. One feature I missed was being able to handle missing values in my NodeVisitor nicely (e.g. from a ? or * pattern that doesn't match). Being able to do:

if not node:
  vals = []

would be a convenient idiom to use when the pattern didn't match. It could also be used to do the right thing if visited_children was used as well.

I think bool(node) would just be:

def __bool__(self):
  """Returns True when one or more characters were matched.

  Returns False for ? and * Patterns that fail to match."""
  return self.start != self.end

I'm happy to turn this into a (trivial) pull request, posting as an issue in case I'm missing something!