@AutoFinal
@CompileStatic
class ASTMatcher
extends ContextualClassCodeVisitor
Matches AST nodes against pattern ASTs.
| Type Params | Return Type | Name and description |
|---|---|---|
|
static List<TreeContext> |
find(ASTNode node, ASTNode pattern)Locates all nodes in the given AST which match the pattern AST. |
|
protected SourceUnit |
getSourceUnit()Returns no source unit because AST matching is source-independent. |
<T> |
T |
ifConstraint(T defaultValue, Closure<T> code)Executes the supplied closure with the current matching constraints as delegate. |
|
static boolean |
matches(ASTNode node, ASTNode pattern)Matches an AST with another AST (pattern). |
|
void |
visitAnnotations(AnnotatedNode node)Matches annotations on the current annotated node. |
|
void |
visitArrayExpression(ArrayExpression expression)Matches an array expression against the current candidate. |
|
void |
visitAttributeExpression(AttributeExpression expression)Matches an attribute expression against the current candidate. |
|
void |
visitBinaryExpression(BinaryExpression expression)Matches a binary expression against the current candidate. |
|
void |
visitBitwiseNegationExpression(BitwiseNegationExpression expression)Matches a bitwise-negation expression against the current candidate. |
|
void |
visitBlockStatement(BlockStatement block)Matches a block statement against the current candidate. |
|
void |
visitBooleanExpression(BooleanExpression expression)Matches a boolean expression against the current candidate. |
|
void |
visitCastExpression(CastExpression expression)Matches a cast expression against the current candidate. |
|
void |
visitClass(ClassNode node)Matches a class node against the current candidate. |
|
protected void |
visitClassCodeContainer(Statement code)Matches a class code container against the current candidate. |
|
void |
visitClassExpression(ClassExpression expression)Matches a class expression against the current candidate. |
|
void |
visitClosureExpression(ClosureExpression expression)Matches a closure expression against the current candidate. |
|
void |
visitClosureListExpression(ClosureListExpression cle)Matches a closure-list expression against the current candidate. |
|
void |
visitConstantExpression(ConstantExpression expression)Matches a constant expression against the current candidate. |
|
void |
visitConstructorCallExpression(ConstructorCallExpression call)Matches a constructor call expression against the current candidate. |
|
protected void |
visitConstructorOrMethod(MethodNode node, boolean isConstructor)Matches a constructor or method node against the current candidate. |
|
void |
visitDeclarationExpression(DeclarationExpression expression)Matches a declaration expression against the current candidate. |
|
void |
visitExpressionStatement(ExpressionStatement statement)Matches an expression statement against the current candidate. |
|
void |
visitField(FieldNode node)Matches a field node against the current candidate. |
|
void |
visitForLoop(ForStatement forLoop)Matches a for loop against the current candidate. |
|
void |
visitGStringExpression(GStringExpression expression)Matches a GString expression against the current candidate. |
|
void |
visitIfElse(IfStatement ifElse)Matches an if/else statement against the current candidate. |
|
void |
visitImports(ModuleNode node)Matches module imports against the current candidate. |
|
void |
visitListExpression(ListExpression expression)Matches a list expression against the current candidate. |
|
void |
visitListOfExpressions(List<? extends Expression> list)Matches a list of expressions against the current candidate list. |
|
void |
visitMapEntryExpression(MapEntryExpression expression)Matches a map-entry expression against the current candidate. |
|
void |
visitMapExpression(MapExpression expression)Matches a map expression against the current candidate. |
|
void |
visitMethodCallExpression(MethodCallExpression call)Matches a method call expression against the current candidate. |
|
void |
visitMethodPointerExpression(MethodPointerExpression expression)Matches a method-pointer expression against the current candidate. |
|
void |
visitNotExpression(NotExpression expression)Matches a not expression against the current candidate. |
|
protected void |
visitObjectInitializerStatements(ClassNode node)Matches object initializer statements for the current class candidate. |
|
void |
visitPackage(PackageNode node)Matches a package node against the current candidate. |
|
void |
visitPostfixExpression(PostfixExpression expression)Matches a postfix expression against the current candidate. |
|
void |
visitPrefixExpression(PrefixExpression expression)Matches a prefix expression against the current candidate. |
|
void |
visitProperty(PropertyNode node)Matches a property node against the current candidate. |
|
void |
visitPropertyExpression(PropertyExpression expression)Matches a property expression against the current candidate. |
|
void |
visitRangeExpression(RangeExpression expression)Matches a range expression against the current candidate. |
|
void |
visitSpreadExpression(SpreadExpression expression)Matches a spread expression against the current candidate. |
|
void |
visitTernaryExpression(TernaryExpression expression)Matches a ternary expression against the current candidate. |
|
void |
visitTupleExpression(TupleExpression expression)Matches a tuple expression against the current candidate. |
|
void |
visitUnaryMinusExpression(UnaryMinusExpression expression)Matches a unary-minus expression against the current candidate. |
|
void |
visitUnaryPlusExpression(UnaryPlusExpression expression)Matches a unary-plus expression against the current candidate. |
|
void |
visitVariableExpression(VariableExpression expression)Matches a variable expression against the current candidate. |
|
void |
visitWhileLoop(WhileStatement loop)Matches a while loop against the current candidate. |
|
static ASTNode |
withConstraints(ASTNode pattern, Closure constraintsSpec)TODO: experimental! |
Wildcard marker used in matcher patterns.
Locates all nodes in the given AST which match the pattern AST. This operation can cost a lot, because it tries to match a sub-tree to every node of the AST.
node - an AST Nodepattern - a pattern to be found somewhere in the ASTReturns no source unit because AST matching is source-independent.
Executes the supplied closure with the current matching constraints as delegate.
defaultValue - the value to return when no constraints are availablecode - the code to execute with the current constraintsdefaultValue when no constraints are presentMatches an AST with another AST (pattern). It will return true if the AST matches all the nodes from the pattern AST.
node - the AST we want to match withpattern - the pattern AST we want to match toMatches annotations on the current annotated node.
Matches an array expression against the current candidate.
Matches an attribute expression against the current candidate.
Matches a binary expression against the current candidate.
Matches a bitwise-negation expression against the current candidate.
Matches a block statement against the current candidate.
Matches a boolean expression against the current candidate.
Matches a cast expression against the current candidate.
Matches a class code container against the current candidate.
Matches a class expression against the current candidate.
Matches a closure expression against the current candidate.
Matches a closure-list expression against the current candidate.
Matches a constant expression against the current candidate.
Matches a constructor call expression against the current candidate.
Matches a constructor or method node against the current candidate.
Matches a declaration expression against the current candidate.
Matches an expression statement against the current candidate.
Matches a for loop against the current candidate.
Matches a GString expression against the current candidate.
Matches an if/else statement against the current candidate.
Matches module imports against the current candidate.
Matches a list expression against the current candidate.
Matches a list of expressions against the current candidate list.
Matches a map-entry expression against the current candidate.
Matches a map expression against the current candidate.
Matches a method call expression against the current candidate.
Matches a method-pointer expression against the current candidate.
Matches a not expression against the current candidate.
Matches object initializer statements for the current class candidate.
Matches a package node against the current candidate.
Matches a postfix expression against the current candidate.
Matches a prefix expression against the current candidate.
Matches a property node against the current candidate.
Matches a property expression against the current candidate.
Matches a range expression against the current candidate.
Matches a spread expression against the current candidate.
Matches a ternary expression against the current candidate.
Matches a tuple expression against the current candidate.
Matches a unary-minus expression against the current candidate.
Matches a unary-plus expression against the current candidate.
Matches a variable expression against the current candidate.
Matches a while loop against the current candidate.
TODO: experimental! Annotates an AST node with matching contraints. This method should be called on an AST intended to be used as a pattern only. It will put node metadata on the AST node allowing customized behavior in pattern matching.
pattern - a pattern ASTconstraintsSpec - a closure specification of matching constraints