From b570641e2b94c345420cd888d365c33c9d69e947 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 4 Mar 2012 12:17:43 -0600 Subject: [PATCH] Simplify ParseTree.visitChildren; ParseTree.visit and visitChildren take a RuleNode parameter; update comments --- .../v4/runtime/tree/ParseTreeVisitor.java | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java index 2ee8adccd..84c448c7e 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java +++ b/runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java @@ -3,38 +3,26 @@ package org.antlr.v4.runtime.tree; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; -/** T is return type of visit methods. Use T=Void for no return type. */ +/** {@code T} is return type of {@code visit} methods. Use {@link Void} for no return type. + */ public class ParseTreeVisitor { - public T visit(ParseTree ctx) { - return ctx.accept(this); + public T visit(ParseTree.RuleNode node) { + return node.accept(this); } - /** Visit all rule, nonleaf children. Not that useful if you are using T as - * non-Void. This returns value returned from last child visited, - * losing all computations from first n-1 children. Works fine for - * ctxs with one child then. + /** Visit all rule, non-leaf children. This returns value returned from last + * child visited, losing all computations from first n-1 children. Works + * fine for contexts with one child then. * Handy if you are just walking the tree with a visitor and only - * care about some nodes. The ParserRuleContext.accept() method + * care about some nodes. The {@link ParserRuleContext#accept} method * walks all children by default; i.e., calls this method. */ - public T visitChildren(ParseTree tree) { + public T visitChildren(ParseTree.RuleNode node) { T result = null; - int n = tree.getChildCount(); + int n = node.getChildCount(); for (int i=0; i rctx = (ParserRuleContext)r.getRuleContext(); - result = visit(rctx); - } - else { - if ( c instanceof ParseTree.ErrorNodeImpl) { - result = visitErrorNode((ParseTree.ErrorNode)c); - } - else { - result = visitTerminal((ParseTree.TerminalNode)c); - } - } + ParseTree c = node.getChild(i); + result = c.accept(this); } return result; }