From 8609d13ac8b561ee8b3ed58cc58ec38d710b5fe5 Mon Sep 17 00:00:00 2001 From: Igor Dejanovic <igor.dejanovic@gmail.com> Date: Sun, 10 Aug 2014 22:58:01 +0200 Subject: [PATCH] Change the way non-terminals are created in the parse method --- arpeggio/__init__.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arpeggio/__init__.py b/arpeggio/__init__.py index ac277d8..df37b93 100644 --- a/arpeggio/__init__.py +++ b/arpeggio/__init__.py @@ -245,17 +245,18 @@ class ParsingExpression(object): if parser.debug: print("<< Leaving rule {}".format(self.name)) - # Create terminal or non-terminal if result is not - # already a Terminal. + # For root rules flatten non-terminal/list if self.root and result and not isinstance(result, Terminal): - if parser.reduce_tree: - if isinstance(result, list): - result = flatten(result) - if len(result) == 1: - result = result[0] - else: - result = NonTerminal(self, c_pos, result) - else: + if not isinstance(result, NonTerminal): + result = flatten(result) + + # Tree reduction will eliminate Non-terminal with single child. + if parser.reduce_tree and len(result) == 1: + result = result[0] + + # If the result is not parse tree node it must be a plain list + # so create a new NonTerminal. + if not isinstance(result, ParseTreeNode): result = NonTerminal(self, c_pos, result) # Result caching for use by memoization. -- 2.18.0