Class Matcher<T extends ASTNode>

java.lang.Object
io.github.douira.glsl_transformer.ast.transform.Matcher<T>

public class Matcher<T extends ASTNode> extends Object
Instances of the matcher can match a node against a stored pattern. This avoids a separate equality implementation for each node type.
  • Field Details

    • pattern

      protected final T extends ASTNode pattern
      The node of the pattern being matched.
  • Constructor Details

    • Matcher

      public Matcher(T pattern, String wildcardIdentifier)
      Creates a new matcher for the given pattern and wildcard prefix.
      Parameters:
      pattern - The pattern to match
      wildcardIdentifier - The prefix for wildcard identifiers
    • Matcher

      public Matcher(T pattern)
      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 parse
      parseMethod - The parser method to use
      visitMethod - The build visitor method to use
      wildcardIdentifier - 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 parse
      parseMethod - The parser method to use
      visitMethod - 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

      public boolean matches(T tree)
      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

      public boolean matchesExtract(T tree)
      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 match
      dataMatches - The data match map to use
      nodeMatches - The node match map to use
      Returns:
    • getDataMatches

      public Map<String,Object> getDataMatches()
      Returns the data match map.
      Returns:
      The data match map
    • getNodeMatches

      public Map<String,ASTNode> getNodeMatches()
      Returns the node match map.
      Returns:
      The node match map
    • getDataMatch

      public Object getDataMatch(String name)
      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

      public String getStringDataMatch(String name)
      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

      public ASTNode getNodeMatch(String name)
      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

      public <R extends ASTNode> R getNodeMatch(String name, Class<R> clazz)
      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 match
      clazz - The class of the node match
      Returns:
      The node match or null if not found or not of the given class
    • markAnyWildcard

      public void markAnyWildcard(String name, ASTNode patternNode)
      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 wildcard
      patternNode - 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 wildcard
      patternNode - The node to mark as a predicate wildcard
      matchPredicate - The predicate to match the node with
    • markClassWildcard

      public void markClassWildcard(String name, ASTNode patternNode, Class<? extends ASTNode> clazz)
      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 wildcard
      patternNode - The node to mark as a class wildcard
      clazz - The class to match the node with
    • markClassWildcard

      public void markClassWildcard(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. 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 wildcard
      patternNode - 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 wildcard
      patternNode - The node to mark as a classed predicate wildcard
      clazz - The class to match the node with
      predicate - The predicate to match the node with