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. */
public class ParseTreeVisitor<T> {
	public T visit(ParseTree ctx) {
		return ctx.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.
	 *  Handy if you are just walking the tree with a visitor and only
	 *  care about some nodes.  The ParserRuleContext.accept() method
	 *  walks all children by default; i.e., calls this method.
	 */
	public T visitChildren(ParseTree tree) {
		T result = null;
		int n = tree.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);
				}
			}
		}
		return result;
	}

	public T visitTerminal(ParseTree.TerminalNode<? extends Token> node) { return null; }
	public T visitErrorNode(ParseTree.ErrorNode<? extends Token> node) { return null; }
}

@@ -116,17 +116,25 @@
 n;%0A%0A/** 
-T
+%7B@code T%7D
  is retu
@@ -144,21 +144,29 @@
 type of 
+%7B@code 
 visit
+%7D
  methods
@@ -175,14 +175,20 @@
 Use 
-T=
+%7B@link 
 Void
+%7D
  for
@@ -203,16 +203,17 @@
 rn type.
+%0A
  */%0Apubl
@@ -268,20 +268,30 @@
 arseTree
- ctx
+.RuleNode node
 ) %7B%0A%09%09re
@@ -295,19 +295,20 @@
 %09return 
-ctx
+node
 .accept(
@@ -342,16 +342,17 @@
 ule, non
+-
 leaf chi
@@ -361,62 +361,8 @@
 ren.
- Not that useful if you are using T as%0A%09 *  non-Void. 
  Thi
@@ -395,16 +395,21 @@
 rom last
+%0A%09 * 
  child v
@@ -415,21 +415,16 @@
 visited,
-%0A%09 * 
  losing 
@@ -471,16 +471,21 @@
 .  Works
+%0A%09 * 
  fine fo
@@ -489,17 +489,16 @@
  for
-%0A%09 *  ctx
+ context
 s wi
@@ -617,16 +617,23 @@
 s.  The 
+%7B@link 
 ParserRu
@@ -645,17 +645,16 @@
 text
-.
+#
 accept
-()
+%7D
  met
@@ -756,20 +756,29 @@
 arseTree
- tre
+.RuleNode nod
 e) %7B%0A%09%09T
@@ -803,19 +803,19 @@
 int n = 
-tre
+nod
 e.getChi
@@ -870,19 +870,19 @@
 ree c = 
-tre
+nod
 e.getChi
@@ -895,448 +895,32 @@
 %0A%09%09%09
-if ( c instanceof ParseTree.RuleNode ) %7B%0A%09%09%09%09ParseTree.RuleNode r = (ParseTree.RuleNode)c;%0A%09%09%09%09ParserRuleContext%3C?%3E rctx = (ParserRuleContext%3C? extends Token%3E)r.getRuleContext();%0A%09%09%09%09result = visit(rctx);%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09if ( c instanceof ParseTree.ErrorNodeImpl) %7B%0A%09%09%09%09%09result = visitErrorNode((ParseTree.ErrorNode%3C? extends Token%3E)c);%0A%09%09%09%09%7D%0A%09%09%09%09else %7B%0A%09%09%09%09%09result = visitTerminal((ParseTree.TerminalNode%3C? extends Token%3E)c);%0A%09%09%09%09%7D%0A%09%09%09%7D
+result = c.accept(this);
 %0A%09%09%7D

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