Class TreePattern<V>

  • Type Parameters:
    V - the value type of the tree than can be matched by this pattern
    All Implemented Interfaces:
    Serializable

    public final class TreePattern<V>
    extends Object
    implements Serializable
    This class serves two purposes. Firstly, it is used as a classical pattern, which is used to find matches against a matching tree. Secondly, it can expand a given pattern to a full tree with a given pattern variable to sub-tree mapping.

    Matching trees

    A compiled representation of a tree pattern. A tree pattern, specified as a parentheses string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a TreeMatcher object that can match arbitrary trees against the tree pattern. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

    The string representation of a tree pattern is a parenthesis tree string, with a special wildcard syntax for arbitrary sub-trees. The sub-trees variables are prefixed with a '$' and must be a valid Java identifier.

    final TreePattern<String> p1 = TreePattern.compile("add($a,add($b,sin(x)))"); final TreePattern<String> p2 = TreePattern.compile("pow($x,$y)");
    If you need to have values which starts with a '$' character, you can escape it with a '\'.
    final TreePattern<String> p1 = TreePattern.compile("concat($x,\\$foo)");
    The second value, $foo, of the concat function is not treated as pattern variable.

    If you want to match against trees with a different value type than String, you have to specify an additional type mapper function when compiling the pattern string.

    final TreePattern<Op<Double>> p = TreePattern.compile( "add($a,add($b,sin(x)))", MathOp::toMathOp );

    Expanding trees

    The second functionality of the tree pattern is to expand a pattern to a whole tree with a given pattern variable to sub-tree mapping.
    final TreePattern<String> pattern = TreePattern.compile("add($x,$y,1)"); final Map<Var<String>, Tree<String, ?>> vars = Map.of( Var.of("x"), TreeNode.parse("sin(x)"), Var.of("y"), TreeNode.parse("sin(y)") ); final Tree<String, ?> tree = pattern.expand(vars); assertEquals(tree.toParenthesesString(), "add(sin(x),sin(y),1)");
    Since:
    5.0
    Version:
    5.0
    See Also:
    TreeRewriteRule, Tree.toParenthesesString(), TreeMatcher, Serialized Form
    • Method Detail

      • vars

        public SortedSet<TreePattern.Var<V>> vars()
        Return the unmodifiable set of variables, defined in this pattern. The variables are returned without the angle brackets.
        Returns:
        the variables, defined in this pattern
      • map

        public <B> TreePattern<B> map​(Function<? super V,​? extends B> mapper)
        Maps this tree-pattern from type V to type B.
        Type Parameters:
        B - the target type
        Parameters:
        mapper - the type mapper
        Returns:
        a new tree-pattern for the mapped type
      • matcher

        public TreeMatcher<Vmatcher​(Tree<V,​?> tree)
        Creates a matcher that will match the given input tree against this pattern.
        Parameters:
        tree - the tree to be matched
        Returns:
        a new matcher for this pattern
        Throws:
        NullPointerException - if the arguments is null
      • matches

        public boolean matches​(Tree<V,​?> tree)
        Tests whether the given input tree matches this pattern.
        Parameters:
        tree - the tree to be matched
        Returns:
        true if the tree matches this pattern, false otherwise
        Throws:
        NullPointerException - if one of the arguments is null
      • compile

        public static TreePattern<Stringcompile​(String pattern)
        Compiles the given tree pattern string.
        Parameters:
        pattern - the tree pattern string
        Returns:
        the compiled pattern
        Throws:
        NullPointerException - if the given pattern is null
        IllegalArgumentException - if the given parentheses tree string doesn't represent a valid pattern tree or one of the variable name is not a valid (Java) identifier
      • compile

        public static <V> TreePattern<V> compile​(String pattern,
                                                 Function<? super String,​? extends V> mapper)
        Compiles the given tree pattern string.
        Type Parameters:
        V - the value type of the tree than can be matched by the pattern
        Parameters:
        pattern - the tree pattern string
        mapper - the mapper which converts the serialized string value to the desired type
        Returns:
        the compiled pattern
        Throws:
        NullPointerException - if the given pattern is null
        IllegalArgumentException - if the given parentheses tree string doesn't represent a valid pattern tree or one of the variable name is not a valid (Java) identifier