Package org.antlr.v4.runtime.tree
Class Trees
- java.lang.Object
-
- org.antlr.v4.runtime.tree.Trees
-
public class Trees extends Object
A set of utility routines useful for all kinds of ANTLR trees.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
_findAllNodes(ParseTree t, int index, boolean findTokens, List<? super ParseTree> nodes)
static List<ParseTree>
descendants(ParseTree t)
Deprecated.static List<ParseTree>
findAllNodes(ParseTree t, int index, boolean findTokens)
static Collection<ParseTree>
findAllRuleNodes(ParseTree t, int ruleIndex)
static Collection<ParseTree>
findAllTokenNodes(ParseTree t, int ttype)
static Tree
findNodeSuchThat(Tree t, Predicate<Tree> pred)
Return first node satisfying the predstatic List<? extends Tree>
getAncestors(Tree t)
Return a list of all ancestors of this node.static List<Tree>
getChildren(Tree t)
Return ordered list of all children of this nodestatic List<ParseTree>
getDescendants(ParseTree t)
Get all descendents; includes t itself.static String
getNodeText(Tree t, List<String> ruleNames)
static String
getNodeText(Tree t, Parser recog)
static ParserRuleContext
getRootOfSubtreeEnclosingRegion(ParseTree t, int startTokenIndex, int stopTokenIndex)
Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex inclusively using postorder traversal.static boolean
isAncestorOf(Tree t, Tree u)
Return true if t is u's parent or a node on path to root from u.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.static String
toStringTree(Tree t)
Print out a whole tree in LISP form.static String
toStringTree(Tree t, List<String> ruleNames)
Print out a whole tree in LISP form.static String
toStringTree(Tree t, Parser recog)
Print out a whole tree in LISP form.
-
-
-
Method Detail
-
toStringTree
public static String toStringTree(Tree t)
Print out a whole tree in LISP form.getNodeText(org.antlr.v4.runtime.tree.Tree, org.antlr.v4.runtime.Parser)
is used on the node payloads to get the text for the nodes. Detect parse trees and extract data appropriately.
-
toStringTree
public static String toStringTree(Tree t, Parser recog)
Print out a whole tree in LISP form.getNodeText(org.antlr.v4.runtime.tree.Tree, org.antlr.v4.runtime.Parser)
is used on the node payloads to get the text for the nodes. Detect parse trees and extract data appropriately.
-
toStringTree
public static String toStringTree(Tree t, List<String> ruleNames)
Print out a whole tree in LISP form.getNodeText(org.antlr.v4.runtime.tree.Tree, org.antlr.v4.runtime.Parser)
is used on the node payloads to get the text for the nodes.
-
getChildren
public static List<Tree> getChildren(Tree t)
Return ordered list of all children of this node
-
getAncestors
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.- Since:
- 4.5.1
-
isAncestorOf
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
-
findAllTokenNodes
public static Collection<ParseTree> findAllTokenNodes(ParseTree t, int ttype)
-
findAllRuleNodes
public static Collection<ParseTree> findAllRuleNodes(ParseTree t, int ruleIndex)
-
findAllNodes
public static List<ParseTree> findAllNodes(ParseTree t, int index, boolean findTokens)
-
_findAllNodes
public static void _findAllNodes(ParseTree t, int index, boolean findTokens, List<? super ParseTree> nodes)
-
getDescendants
public static List<ParseTree> getDescendants(ParseTree t)
Get all descendents; includes t itself.- Since:
- 4.5.1
-
descendants
@Deprecated public static List<ParseTree> descendants(ParseTree t)
Deprecated.
-
getRootOfSubtreeEnclosingRegion
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.- Since:
- 4.5.1
-
stripChildrenOutOfRange
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
-
-