def visit(self, node): """Walk a parse tree, transforming it into another representation. Recursively descend a parse tree, dispatching to the method named after the rule in the :class:`~parsimonious.grammar.Grammar` that produced each node. If, for example, a rule was... :: bold = '<b>' ...the ``visit_bold()`` method would be called. It is your responsibility to subclass :class:`NodeVisitor` and implement those methods. """ method = getattr(self, 'visit_' + node.expr_name, self.generic_visit) # Call that method, and show where in the tree it failed if it blows # up. try: return method(node, [self.visit(n) for n in node]) except (VisitationError, UndefinedLabel): # Don't catch and re-wrap already-wrapped exceptions. raise except self.unwrapped_exceptions: raise except Exception: # Catch any exception, and tack on a parse tree so it's easier to # see where it went wrong. exc_class, exc, tb = exc_info() reraise(VisitationError, VisitationError(exc, exc_class, node), tb)
def visit(self, node): """Rewrite original method to use lower-case method, and not "generic" function.""" try: # Get the "visit_%s" method, using the lower case version of the rule. method = getattr(self, 'visit_%s' % node.expr_name.lower()) except AttributeError: # If the method is not defined, we do nothing for this node. return # Below is untouched code from the original ``visit`` method. # Call that method, and show where in the tree it failed if it blows up. try: return method(node, [self.visit(n) for n in node]) except (VisitationError, UndefinedLabel): # Don't catch and re-wrap already-wrapped exceptions. raise except self.unwrapped_exceptions: raise except Exception: # Catch any exception, and tack on a parse tree so it's easier to # see where it went wrong. exc_class, exc, trace = sys.exc_info() raise VisitationError(exc, exc_class, node).with_traceback(trace)
def visit(self, node): method = getattr(self, 'visit_' + node.expr_name, self.generic_visit) # Call that method, and show where in the tree it failed if it blows # up. try: return method(node, [self.visit(n) for n in node]) except (VisitationError, UndefinedLabel, RecursiveLabel): # Don't catch and re-wrap already-wrapped exceptions. raise except Exception: # Catch any exception, and tack on a parse tree so it's easier to # see where it went wrong. exc_class, exc, tb = sys.exc_info() raise VisitationError(exc, exc_class, node).with_traceback(tb)
def visit(self, node): """ See the ``NodeVisitor`` visit method. This just changes the order in which we visit nonterminals from right to left to left to right. """ method = getattr(self, 'visit_' + node.expr_name, self.generic_visit) # Call that method, and show where in the tree it failed if it blows # up. try: # Changing this to reverse here! return method(node, [self.visit(child) for child in reversed(list(node))]) except (VisitationError, UndefinedLabel): # Don't catch and re-wrap already-wrapped exceptions. raise except self.unwrapped_exceptions: raise except Exception: # pylint: disable=broad-except # Catch any exception, and tack on a parse tree so it's easier to # see where it went wrong. exc_class, exc, traceback = exc_info() reraise(VisitationError, VisitationError(exc, exc_class, node), traceback)