V
- the value type of the tree than can be matched by this patternpublic final class TreePattern<V> extends Object implements Serializable
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 aTreeMatcher
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)");
final TreePattern<String> p1 = TreePattern.compile("concat($x,\\$foo)");
$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 = new HashMap<>();
vars.put(Var.of("x"), TreeNode.parse("sin(x)"));
vars.put(Var.of("y"), TreeNode.parse("sin(y)"));
final Tree<String, ?> tree = pattern.expand(vars);
assertEquals(tree.toParenthesesString(), "add(sin(x),sin(y),1)");
TreeRewriteRule
,
Tree.toParenthesesString()
,
TreeMatcher
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
TreePattern.Decl<V>
A sealed class, which constitutes the nodes of a pattern tree.
|
static class |
TreePattern.Val<V>
This class represents a constant pattern value, which can be part of a
whole sub-tree.
|
static class |
TreePattern.Var<V>
Represents a placeholder (variable) for an arbitrary sub-tree.
|
Constructor and Description |
---|
TreePattern(Tree<TreePattern.Decl<V>,?> pattern)
Create a new tree-pattern object from the given pattern tree.
|
Modifier and Type | Method and Description |
---|---|
static TreePattern<String> |
compile(String pattern)
Compiles the given tree pattern string.
|
static <V> TreePattern<V> |
compile(String pattern,
Function<? super String,? extends V> mapper) |
boolean |
equals(Object obj) |
TreeNode<V> |
expand(Map<TreePattern.Var<V>,Tree<V,?>> vars)
Expands
this pattern with the given variable mapping. |
int |
hashCode() |
<B> TreePattern<B> |
map(Function<? super V,? extends B> mapper)
Maps
this tree-pattern from type V to type B . |
Optional<TreeMatchResult<V>> |
match(Tree<V,?> tree)
Try to match the given
tree against this pattern. |
TreeMatcher<V> |
matcher(Tree<V,?> tree)
Creates a matcher that will match the given input tree against
this pattern. |
boolean |
matches(Tree<V,?> tree)
Tests whether the given input
tree matches this pattern. |
String |
toString() |
SortedSet<TreePattern.Var<V>> |
vars()
Return the unmodifiable set of variables, defined in
this
pattern. |
public TreePattern(Tree<TreePattern.Decl<V>,?> pattern)
pattern
- the pattern-treeNullPointerException
- if the given pattern
is null
IllegalArgumentException
- if TreePattern.Var
nodes are not leaf nodes;
Tree.isLeaf()
is false
public SortedSet<TreePattern.Var<V>> vars()
this
pattern. The variables are returned without the angle brackets.public <B> TreePattern<B> map(Function<? super V,? extends B> mapper)
this
tree-pattern from type V
to type B
.B
- the target typemapper
- the type mapperpublic TreeMatcher<V> matcher(Tree<V,?> tree)
this
pattern.tree
- the tree to be matchedthis
patternNullPointerException
- if the arguments is null
public Optional<TreeMatchResult<V>> match(Tree<V,?> tree)
tree
against this
pattern.tree
- the tree to be matchedOptional.empty()
if the given
tree
doesn't matchNullPointerException
- if the arguments is null
public boolean matches(Tree<V,?> tree)
tree
matches this
pattern.tree
- the tree to be matchedtrue
if the tree
matches this
pattern,
false
otherwiseNullPointerException
- if one of the arguments is null
public TreeNode<V> expand(Map<TreePattern.Var<V>,Tree<V,?>> vars)
this
pattern with the given variable mapping.vars
- the variables to use for expanding this
patternNullPointerException
- if one of the arguments is null
IllegalArgumentException
- if not all needed variables are part
of the variables
mappublic static TreePattern<String> compile(String pattern)
pattern
- the tree pattern stringNullPointerException
- 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) identifierpublic static <V> TreePattern<V> compile(String pattern, Function<? super String,? extends V> mapper)
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)