/*
 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.runtime.tree;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;

public class ParseTreeWalker {
    public static final ParseTreeWalker DEFAULT = new ParseTreeWalker();

    public void walk(ParseTreeListener listener, ParseTree t) {
		if ( t instanceof ErrorNode) {
			listener.visitErrorNode((ErrorNode)t);
			return;
		}
		else if ( t instanceof TerminalNode) {
			listener.visitTerminal((TerminalNode)t);
			return;
		}
		RuleNode r = (RuleNode)t;
        enterRule(listener, r);
        int n = r.getChildCount();
        for (int i = 0; i<n; i++) {
            walk(listener, r.getChild(i));
        }
		exitRule(listener, r);
    }

	/**
	 * The discovery of a rule node, involves sending two events: the generic
	 * {@link ParseTreeListener#enterEveryRule} and a
	 * {@link RuleContext}-specific event. First we trigger the generic and then
	 * the rule specific. We to them in reverse order upon finishing the node.
	 */
    protected void enterRule(ParseTreeListener listener, RuleNode r) {
		ParserRuleContext ctx = (ParserRuleContext)r.getRuleContext();
		listener.enterEveryRule(ctx);
		ctx.enterRule(listener);
    }

    protected void exitRule(ParseTreeListener listener, RuleNode r) {
		ParserRuleContext ctx = (ParserRuleContext)r.getRuleContext();
		ctx.exitRule(listener);
		listener.exitEveryRule(ctx);
    }
}

@@ -280,49 +280,8 @@
 ext;
-%0Aimport org.antlr.v4.runtime.RuleContext;
 %0A%0Apu
@@ -383,20 +383,471 @@
 ker();%0A%0A
-    
+%0A%09/**%0A%09 * Performs a walk on the given parse tree starting at the root and going down recursively%0A%09 * with depth-first search. On each node, %7B@link ParseTreeWalker#enterRule%7D is called before%0A%09 * recursively walking down into child nodes, then%0A%09 * %7B@link ParseTreeWalker#exitRule%7D is called after the recursive call to wind up.%0A%09 * @param listener The listener used by the walker to process grammar rules%0A%09 * @param t The parse tree to be walked on%0A%09 */%0A%09
 public v
@@ -1317,66 +1317,49 @@
 %09 * 
-The discovery of a rule node, involves sending two events:
+Enters a grammar rule by first triggering
  the
@@ -1366,20 +1366,22 @@
  generic
-%0A%09 *
+ event
  %7B@link 
@@ -1417,168 +1417,203 @@
 ule%7D
- and a%0A%09 * %7B@link RuleContext%7D-specific event. First we trigger the generic and then%0A%09 * the rule specific. We to them in reverse order upon finishing the node.
+%0A%09 * then by triggering the event specific to the given parse tree node%0A%09 * @param listener The listener responding to the trigger events%0A%09 * @param r The grammar rule containing the rule context
 %0A%09 *
@@ -1816,20 +1816,327 @@
 %0A    %7D%0A%0A
-    
+%0A%09/**%0A%09 * Exits a grammar rule by first triggering the event specific to the given parse tree node%0A%09 * then by triggering the generic event %7B@link ParseTreeListener#enterEveryRule%7D%0A%09 * @param listener The listener responding to the trigger events%0A%09 * @param r The grammar rule containing the rule context%0A%09 */%0A%09
 protecte

Add documentation to ParseTreeWalker