Simplify ParseTree.visitChildren; ParseTree.visit and visitChildren take a RuleNode parameter; update comments

This commit is contained in:
Sam Harwell 2012-03-04 12:17:43 -06:00
parent a948736729
commit b570641e2b
1 changed files with 12 additions and 24 deletions

View File

@ -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<T> {
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<n; i++) {
ParseTree c = tree.getChild(i);
if ( c instanceof ParseTree.RuleNode ) {
ParseTree.RuleNode r = (ParseTree.RuleNode)c;
ParserRuleContext<?> rctx = (ParserRuleContext<? extends Token>)r.getRuleContext();
result = visit(rctx);
}
else {
if ( c instanceof ParseTree.ErrorNodeImpl) {
result = visitErrorNode((ParseTree.ErrorNode<? extends Token>)c);
}
else {
result = visitTerminal((ParseTree.TerminalNode<? extends Token>)c);
}
}
ParseTree c = node.getChild(i);
result = c.accept(this);
}
return result;
}