Class Matcher<T extends ASTNode>
java.lang.Object
io.github.douira.glsl_transformer.ast.transform.Matcher<T>
Instances of the matcher can match a node against a stored pattern. This
avoids a separate equality implementation for each node type.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMatcher(String input, Function<io.github.douira.glsl_transformer.GLSLParser, RuleType> parseMethod, BiFunction<ASTBuilder, RuleType, T> visitMethod) Creates a new matcher that matches the pattern parsed from the given string, parser method and visitor method.Matcher(String input, Function<io.github.douira.glsl_transformer.GLSLParser, RuleType> parseMethod, BiFunction<ASTBuilder, RuleType, T> visitMethod, String wildcardIdentifier) Creates a new matcher that matches the pattern parsed from the given string, parser method and visitor method.Creates a new matcher for the given pattern and no wildcard prefix.Creates a new matcher for the given pattern and wildcard prefix. -
Method Summary
Modifier and TypeMethodDescriptiongetDataMatch(String name) Gets a data match with the given name.Returns the data match map.getNodeMatch(String name) Gets a node match with the given name.<R extends ASTNode>
RgetNodeMatch(String name, Class<R> clazz) Gets a node match with the given name if it is available as the given class.Returns the node match map.getStringDataMatch(String name) Gets a data match with the given name as a string.voidmarkAnyWildcard(String name, ASTNode patternNode) Marks the given node as an any wildcard with the given name.<R extends ASTNode>
voidmarkClassedPredicateWildcard(String name, ASTNode patternNode, Class<R> clazz, Predicate<R> predicate) Marks the given node as a classed predicate wildcard with the given name.voidmarkClassWildcard(String name, ASTNode patternNode) Marks the given node as a class wildcard with the given name where the class is extracted from the pattern node itself.voidmarkClassWildcard(String name, ASTNode patternNode, Class<? extends ASTNode> clazz) Marks the given node as a class wildcard with the given name.voidmarkPredicatedWildcard(String name, ASTNode patternNode, Predicate<ASTNode> matchPredicate) Marks the given node as a predicate wildcard with the given name.booleanTraverse the given tree and the pattern at the same time and make sure they are the same at each visit step.booleanmatchesExtract(T tree) Matches the given tree and collect matching string wildcards, data wildcard and node wildcards.booleanMatches the given tree and collect matching string wildcards, data wildcard and node wildcards using the given data and node match maps.voidPrepares the matcher for matching.
-
Field Details
-
pattern
The node of the pattern being matched.
-
-
Constructor Details
-
Matcher
Creates a new matcher for the given pattern and wildcard prefix.- Parameters:
pattern- The pattern to matchwildcardIdentifier- The prefix for wildcard identifiers
-
Matcher
Creates a new matcher for the given pattern and no wildcard prefix.- Parameters:
pattern- The pattern to match
-
Matcher
public Matcher(String input, Function<io.github.douira.glsl_transformer.GLSLParser, RuleType> parseMethod, BiFunction<ASTBuilder, RuleType, T> visitMethod, String wildcardIdentifier) Creates a new matcher that matches the pattern parsed from the given string, parser method and visitor method. There is also a given wildcard prefix.- Type Parameters:
RuleType- The type of the parser rule context- Parameters:
input- The string to parseparseMethod- The parser method to usevisitMethod- The build visitor method to usewildcardIdentifier- The wildcard prefix
-
Matcher
public Matcher(String input, Function<io.github.douira.glsl_transformer.GLSLParser, RuleType> parseMethod, BiFunction<ASTBuilder, RuleType, T> visitMethod) Creates a new matcher that matches the pattern parsed from the given string, parser method and visitor method. There is no wildcard prefix.- Type Parameters:
RuleType- The type of the parser rule context- Parameters:
input- The string to parseparseMethod- The parser method to usevisitMethod- The build visitor method to use
-
-
Method Details
-
preparePatternItems
public void preparePatternItems()Prepares the matcher for matching. It parses the pattern and stores the resulting items. This can be used to pre-compute this list of items. Otherwise, this will be calculated on demand -
matches
Traverse the given tree and the pattern at the same time and make sure they are the same at each visit step.- Parameters:
tree- The tree to match- Returns:
- True if the tree matches the pattern, false otherwise
-
matchesExtract
Matches the given tree and collect matching string wildcards, data wildcard and node wildcards. It uses the default data and node wildcard maps.- Parameters:
tree- The tree to match- Returns:
- True if the tree matches the pattern, false otherwise
-
matchesExtract
public boolean matchesExtract(T tree, Map<String, Object> dataMatches, Map<String, ASTNode> nodeMatches) Matches the given tree and collect matching string wildcards, data wildcard and node wildcards using the given data and node match maps.- Parameters:
tree- The tree to matchdataMatches- The data match map to usenodeMatches- The node match map to use- Returns:
-
getDataMatches
Returns the data match map.- Returns:
- The data match map
-
getNodeMatches
Returns the node match map.- Returns:
- The node match map
-
getDataMatch
Gets a data match with the given name.- Parameters:
name- The name of the data match- Returns:
- The data match or null if not found
-
getStringDataMatch
Gets a data match with the given name as a string.- Parameters:
name- The name of the data match- Returns:
- The data match or null if either not found or not a string
-
getNodeMatch
Gets a node match with the given name.- Parameters:
name- The name of the node match- Returns:
- The node match or null if not found
-
getNodeMatch
Gets a node match with the given name if it is available as the given class.- Type Parameters:
R- The type of the node match- Parameters:
name- The name of the node matchclazz- The class of the node match- Returns:
- The node match or null if not found or not of the given class
-
markAnyWildcard
Marks the given node as an any wildcard with the given name. The wildcard will match any node in the same position in the tree.- Parameters:
name- The name of the wildcardpatternNode- The node to mark as an any wildcard
-
markPredicatedWildcard
public void markPredicatedWildcard(String name, ASTNode patternNode, Predicate<ASTNode> matchPredicate) Marks the given node as a predicate wildcard with the given name. The predicate wildcard will match any node in the same position in the tree that matches the predicate.- Parameters:
name- The name of the wildcardpatternNode- The node to mark as a predicate wildcardmatchPredicate- The predicate to match the node with
-
markClassWildcard
Marks the given node as a class wildcard with the given name. The wildcard will match any node in the same position in the tree that is an instance of the given class. If any subclass should be matched, use a superclass for matching.- Parameters:
name- The name of the wildcardpatternNode- The node to mark as a class wildcardclazz- The class to match the node with
-
markClassWildcard
Marks the given node as a class wildcard with the given name where the class is extracted from the pattern node itself. This will match any node in the same position in the tree that is the same class as the pattern node.- Parameters:
name- The name of the wildcardpatternNode- The node to mark as a class wildcard
-
markClassedPredicateWildcard
public <R extends ASTNode> void markClassedPredicateWildcard(String name, ASTNode patternNode, Class<R> clazz, Predicate<R> predicate) Marks the given node as a classed predicate wildcard with the given name. The wildcard will match any node in the same position in the tree that is an instance of the given class and matches the predicate.- Type Parameters:
R- The type of the node match- Parameters:
name- The name of the wildcardpatternNode- The node to mark as a classed predicate wildcardclazz- The class to match the node withpredicate- The predicate to match the node with
-