Package com.tonic.analysis.source.ast
Class ASTUtils
- java.lang.Object
-
- com.tonic.analysis.source.ast.ASTUtils
-
public final class ASTUtils extends Object
Visitor-backed helpers for traversing and querying AST nodes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Statement>
List<T>collect(Statement root, Class<T> type)Collects the statements under a root that are instances of one type.static <T extends Expression>
List<T>collectExpressions(Statement root, Class<T> type)Collects the expressions in a statement tree that are instances of one type.static List<Statement>filter(Statement root, Predicate<Statement> pred)Collects the statements under a root that satisfy a predicate.static List<Expression>filterExpressions(Statement root, Predicate<Expression> pred)Collects the expressions in a statement tree that satisfy a predicate.static voidforEachExpression(Statement root, Consumer<Expression> action)Walks every expression under a statement tree in depth-first order.static voidforEachStatement(Statement root, Consumer<Statement> action)Walks every statement under a root in depth-first order.
-
-
-
Method Detail
-
forEachStatement
public static void forEachStatement(Statement root, Consumer<Statement> action)
Walks every statement under a root in depth-first order.- Parameters:
root- statement to walkaction- invoked once per visited statement
-
collect
public static <T extends Statement> List<T> collect(Statement root, Class<T> type)
Collects the statements under a root that are instances of one type.- Type Parameters:
T- statement type collected- Parameters:
root- statement to walktype- type to match- Returns:
- the matching statements in visit order
-
filter
public static List<Statement> filter(Statement root, Predicate<Statement> pred)
Collects the statements under a root that satisfy a predicate.- Parameters:
root- statement to walkpred- test applied to each visited statement- Returns:
- the accepted statements in visit order
-
forEachExpression
public static void forEachExpression(Statement root, Consumer<Expression> action)
Walks every expression under a statement tree in depth-first order.- Parameters:
root- statement to walkaction- invoked once per visited expression
-
collectExpressions
public static <T extends Expression> List<T> collectExpressions(Statement root, Class<T> type)
Collects the expressions in a statement tree that are instances of one type.- Type Parameters:
T- expression type collected- Parameters:
root- statement to walktype- type to match- Returns:
- the matching expressions in visit order
-
filterExpressions
public static List<Expression> filterExpressions(Statement root, Predicate<Expression> pred)
Collects the expressions in a statement tree that satisfy a predicate.- Parameters:
root- statement to walkpred- test applied to each visited expression- Returns:
- the accepted expressions in visit order
-
-