Top Description Constructors Methods
org.antlr.v4.runtime.tree

public Class Trees

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.CommonToken, .Parser, .ParserRuleContext, .RuleContext, .Token, org.antlr.v4.runtime.atn.ATN, org.antlr.v4.runtime.misc.Interval, .NotNull, .Nullable, .Predicate, .Utils, java.util.ArrayList, .Arrays, .Collection, .Collections, .List

A set of utility routines useful for all kinds of ANTLR trees.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
_findAllNodes(ParseTree t, int index, boolean findTokens, List<? super ParseTree> nodes)

public static List<ParseTree>
descendants(ParseTree t)

Deprecated
public static List<ParseTree>
findAllNodes(ParseTree t, int index, boolean findTokens)

public static Collection<ParseTree>
findAllRuleNodes(ParseTree t, int ruleIndex)

public static Collection<ParseTree>
public static Tree
findNodeSuchThat(Tree t, Predicate<Tree> pred)

Return first node satisfying the pred

public static List<? extends Tree>
getAncestors(Tree t)

Return a list of all ancestors of this node.

public static List<Tree>
getChildren(Tree t)

Return ordered list of all children of this node

public static List<ParseTree>
getDescendants(ParseTree t)

Get all descendents; includes t itself.

public static String
public static String
getNodeText(Tree t, List<String> ruleNames)

public static ParserRuleContext
getRootOfSubtreeEnclosingRegion(ParseTree t, int startTokenIndex, int stopTokenIndex)

Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex inclusively using postorder traversal.

public static boolean
isAncestorOf(Tree t, Tree u)

Return true if t is u's parent or a node on path to root from u.

public static void
stripChildrenOutOfRange(ParserRuleContext t, ParserRuleContext root, int startIndex, int stopIndex)

Replace any subtree siblings of root that are completely to left or right of lookahead range with a CommonToken(Token.INVALID_TYPE,"...") node.

public static String
toStringTree(Tree t)

Print out a whole tree in LISP form.

public static String
toStringTree(Tree t, Parser recog)

Print out a whole tree in LISP form.

public static String
toStringTree(final Tree t, final List<String> ruleNames)

Print out a whole tree in LISP form.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Treesback to summary
private Trees()

Method Detail

_findAllNodesback to summary
public static void _findAllNodes(ParseTree t, int index, boolean findTokens, List<? super ParseTree> nodes)
descendantsback to summary
public static List<ParseTree> descendants(ParseTree t)

Deprecated

Annotations
@Deprecated
findAllNodesback to summary
public static List<ParseTree> findAllNodes(ParseTree t, int index, boolean findTokens)
findAllRuleNodesback to summary
public static Collection<ParseTree> findAllRuleNodes(ParseTree t, int ruleIndex)
findAllTokenNodesback to summary
public static Collection<ParseTree> findAllTokenNodes(ParseTree t, int ttype)
findNodeSuchThatback to summary
public static Tree findNodeSuchThat(Tree t, Predicate<Tree> pred)

Return first node satisfying the pred

Since
4.5.1
getAncestorsback to summary
public static List<? extends Tree> getAncestors(Tree t)

Return a list of all ancestors of this node. The first node of list is the root and the last is the parent of this node.

Annotations
@NotNull
Since
4.5.1
getChildrenback to summary
public static List<Tree> getChildren(Tree t)

Return ordered list of all children of this node

getDescendantsback to summary
public static List<ParseTree> getDescendants(ParseTree t)

Get all descendents; includes t itself.

Since
4.5.1
getNodeTextback to summary
public static String getNodeText(Tree t, Parser recog)
getNodeTextback to summary
public static String getNodeText(Tree t, List<String> ruleNames)
getRootOfSubtreeEnclosingRegionback to summary
public static ParserRuleContext getRootOfSubtreeEnclosingRegion(ParseTree t, int startTokenIndex, int stopTokenIndex)

Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex inclusively using postorder traversal. Recursive depth-first-search.

Annotations
@Nullable
Since
4.5
isAncestorOfback to summary
public static boolean isAncestorOf(Tree t, Tree u)

Return true if t is u's parent or a node on path to root from u. Use == not equals().

Since
4.5.1
stripChildrenOutOfRangeback to summary
public static void stripChildrenOutOfRange(ParserRuleContext t, ParserRuleContext root, int startIndex, int stopIndex)

Replace any subtree siblings of root that are completely to left or right of lookahead range with a CommonToken(Token.INVALID_TYPE,"...") node. The source interval for t is not altered to suit smaller range!

Warning

destructive to t.

Since
4.5.1
toStringTreeback to summary
public static String toStringTree(Tree t)

Print out a whole tree in LISP form. getNodeText is used on the node payloads to get the text for the nodes. Detect parse trees and extract data appropriately.

toStringTreeback to summary
public static String toStringTree(Tree t, Parser recog)

Print out a whole tree in LISP form. getNodeText is used on the node payloads to get the text for the nodes. Detect parse trees and extract data appropriately.

toStringTreeback to summary
public static String toStringTree(final Tree t, final List<String> ruleNames)

Print out a whole tree in LISP form. getNodeText is used on the node payloads to get the text for the nodes.