Skip to content

Commit

Permalink
Fixed Discard, broken in 0.6.6 (Issue #337)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Mar 9, 2019
1 parent e16bb35 commit 2b3fabd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lark/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _call_userfunc(self, tree, new_children=None):
return f(tree)
else:
return f(children)
except GrammarError:
except (GrammarError, Discard):
raise
except Exception as e:
raise VisitError(tree, e)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pickle

from lark.tree import Tree
from lark.visitors import Transformer, Interpreter, visit_children_decor, v_args
from lark.visitors import Transformer, Interpreter, visit_children_decor, v_args, Discard


class TestTrees(TestCase):
Expand Down Expand Up @@ -144,6 +144,26 @@ def add(self, values):
res = T().transform(t)
self.assertEqual(res, 2.9)

def test_discard(self):
class MyTransformer(Transformer):
def a(self, args):
return 1 # some code here

def b(cls, args):
raise Discard()

t = Tree('root', [
Tree('b', []),
Tree('a', []),
Tree('b', []),
Tree('c', []),
Tree('b', []),
])
t2 = Tree('root', [1, Tree('c', [])])

x = MyTransformer().transform( t )
self.assertEqual(x, t2)


if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit 2b3fabd

Please sign in to comment.