Class AbstractParseTreeVisitor<T>
- java.lang.Object
-
- org.antlr.v4.runtime.tree.AbstractParseTreeVisitor<T>
-
- All Implemented Interfaces:
ParseTreeVisitor<T>
public abstract class AbstractParseTreeVisitor<T> extends Object implements ParseTreeVisitor<T>
-
-
Constructor Summary
Constructors Constructor Description AbstractParseTreeVisitor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected T
aggregateResult(T aggregate, T nextResult)
Aggregates the results of visiting multiple children of a node.protected T
defaultResult()
Gets the default value returned by visitor methods.protected boolean
shouldVisitNextChild(RuleNode node, T currentResult)
This method is called after visiting each child invisitChildren(org.antlr.v4.runtime.tree.RuleNode)
.T
visit(ParseTree tree)
Visit a parse tree, and return a user-defined result of the operation.T
visitChildren(RuleNode node)
Visit the children of a node, and return a user-defined result of the operation.T
visitErrorNode(ErrorNode node)
Visit an error node, and return a user-defined result of the operation.T
visitTerminal(TerminalNode node)
Visit a terminal node, and return a user-defined result of the operation.
-
-
-
Method Detail
-
visit
public T visit(ParseTree tree)
Visit a parse tree, and return a user-defined result of the operation.The default implementation calls
ParseTree.accept(org.antlr.v4.runtime.tree.ParseTreeVisitor<? extends T>)
on the specified tree.- Specified by:
visit
in interfaceParseTreeVisitor<T>
- Parameters:
tree
- TheParseTree
to visit.- Returns:
- The result of visiting the parse tree.
-
visitChildren
public T visitChildren(RuleNode node)
Visit the children of a node, and return a user-defined result of the operation.The default implementation initializes the aggregate result to
defaultResult()
. Before visiting each child, it callsshouldVisitNextChild
; if the result isfalse
no more children are visited and the current aggregate result is returned. After visiting a child, the aggregate result is updated by callingaggregateResult
with the previous aggregate result and the result of visiting the child.The default implementation is not safe for use in visitors that modify the tree structure. Visitors that modify the tree should override this method to behave properly in respect to the specific algorithm in use.
- Specified by:
visitChildren
in interfaceParseTreeVisitor<T>
- Parameters:
node
- TheRuleNode
whose children should be visited.- Returns:
- The result of visiting the children of the node.
-
visitTerminal
public T visitTerminal(TerminalNode node)
Visit a terminal node, and return a user-defined result of the operation.The default implementation returns the result of
defaultResult
.- Specified by:
visitTerminal
in interfaceParseTreeVisitor<T>
- Parameters:
node
- TheTerminalNode
to visit.- Returns:
- The result of visiting the node.
-
visitErrorNode
public T visitErrorNode(ErrorNode node)
Visit an error node, and return a user-defined result of the operation.The default implementation returns the result of
defaultResult
.- Specified by:
visitErrorNode
in interfaceParseTreeVisitor<T>
- Parameters:
node
- TheErrorNode
to visit.- Returns:
- The result of visiting the node.
-
defaultResult
protected T defaultResult()
Gets the default value returned by visitor methods. This value is returned by the default implementations ofvisitTerminal
,visitErrorNode
. The default implementation ofvisitChildren
initializes its aggregate result to this value.The base implementation returns
null
.- Returns:
- The default value returned by visitor methods.
-
aggregateResult
protected T aggregateResult(T aggregate, T nextResult)
Aggregates the results of visiting multiple children of a node. After either all children are visited orshouldVisitNextChild(org.antlr.v4.runtime.tree.RuleNode, T)
returnsfalse
, the aggregate value is returned as the result ofvisitChildren(org.antlr.v4.runtime.tree.RuleNode)
.The default implementation returns
nextResult
, meaningvisitChildren(org.antlr.v4.runtime.tree.RuleNode)
will return the result of the last child visited (or return the initial value if the node has no children).- Parameters:
aggregate
- The previous aggregate value. In the default implementation, the aggregate value is initialized todefaultResult()
, which is passed as theaggregate
argument to this method after the first child node is visited.nextResult
- The result of the immediately preceeding call to visit a child node.- Returns:
- The updated aggregate result.
-
shouldVisitNextChild
protected boolean shouldVisitNextChild(RuleNode node, T currentResult)
This method is called after visiting each child invisitChildren(org.antlr.v4.runtime.tree.RuleNode)
. This method is first called before the first child is visited; at that pointcurrentResult
will be the initial value (in the default implementation, the initial value is returned by a call todefaultResult()
. This method is not called after the last child is visited.The default implementation always returns
true
, indicating thatvisitChildren
should only return after all children are visited. One reason to override this method is to provide a "short circuit" evaluation option for situations where the result of visiting a single child has the potential to determine the result of the visit operation as a whole.- Parameters:
node
- TheRuleNode
whose children are currently being visited.currentResult
- The current aggregate result of the children visited to the current point.- Returns:
true
to continue visiting children. Otherwise returnfalse
to stop visiting children and immediately return the current aggregate result fromvisitChildren(org.antlr.v4.runtime.tree.RuleNode)
.
-
-