kschiess / parslet

A small PEG based parser library. See the Hacking page in the Wiki as well.
kschiess.github.com/parslet
MIT License
809 stars 95 forks source link

Raise if transform skips subtree #164

Closed ravinggenius closed 7 years ago

ravinggenius commented 8 years ago

For my application if a subtree doesn't get matched by a transform rule, I have an error. It would be nice if Parslet could optionally raise an exception to alert when no transform rule matches. The exception should include the unmatched keys and ideally the classes and/or contents of the corresponding values.

I'm sure not everyone would want this behavior, so it should be opt-in somehow. This would be extremely useful to me.

ravinggenius commented 8 years ago

Comments? Thoughts? I'm happy to tackle this if that helps.

kschiess commented 8 years ago

Would have to be opt-in. Have you got an idea on how to implement this?

ravinggenius commented 8 years ago

I would change transform_elt to check raise_on_unmatch and either return the node or raise an exception. Sample usage below:

class MyTransform < Parslet::Transform
  def self.apply(raw_tree)
    new(raise_on_unmatch: true).apply(raw_tree)
  end
end