======================
Matching Python Syntax
======================
The ``peak.rules.syntax`` module allows you to define pattern-matching
predicates against snippets of parameterized Python code, such that a
rule expression like::
syntax.match(expr, type(`x`) is `y`) and y in Const
Will return true if ``expr`` is a PEAK-Rules AST of the form::
Compare(Call(Const(type), (v1,)), ('is', Const(v2)))
(where v1 and v2 are arbitrary values).
Bind Variables
==============
Bind variables are placeholders in a pattern that "bind" themselves to the
value found in that location in the matched data structure. Thus, in the
example above, ``\`x\``` and ``\`y\``` are bind variables, and cause "y"
in the later part of the expression to refer to the right-hand side of the
``is`` operator being matched. (The arbitrary value ``v2`` in the example
above.)
Bind variables are represented within a tree as an AST node created from the
variable name::
>>> from peak.rules.syntax import Bind
>>> Bind('x')
Bind('x')
Compiling Tree-Match Predicates
===============================
The ``match_predicate(pattern, expr, binds)`` function is used to combine a
pattern AST and an expression AST to create a PEAK-Rules predicate object that
will match the specified pattern. The `binds` argument is a dictionary mapping
from bind-variable names to lists of expression ASTs, and is modified in-place
as the predicate is assembled::
>>> from peak.rules.syntax import match_predicate
Rules defined for this function will determine what to do based on the type of
`pattern`. If `pattern` is a bind variable, the `binds` dictionary is updated
in-place, inserting `expr` under the bind variable's name, and ``True`` is
returned, indicating that this part of the pattern will always match::
>>> from peak.util.assembler import Local
>>> b = {}
>>> match_predicate(Bind('x'), Local('y'), b)
True
>>> b
{'x': [Local('y')]}
If there's already an entry for that variable in the `binds` dictionary, a more
complex predicate is returned, performing an equality comparison between the
new binding and the old binding of the variable, and the value in `binds` is
updated::
>>> match_predicate(Bind('x'), Local('z'), b)
Test(Truth(Compare(Local('z'), (('==', Local('y')),))), True)
This is so that patterns like "`x` is not `x`" will actually compare the two
"x"s and see if they're equal. Of course, if you bind the same variable more
than once to equal expression ASTs, you will not get back a comparison, and
the `binds` will be unchanged::
>>> match_predicate(Bind('x'), Local('z'), b)
True
>>> b
{'x': [Local('y'), Local('z')]}
Finally, there is a special exception for bind variables named ``\`_\```: that
is, a single underscore. Bind variables of this name are never stored in the
`binds`, and always return ``True`` as a predicate, allowing you to use them as
"don't care" placeholders::
>>> any = Bind('_')
>>> match_predicate(any, Local('q'), b)
True
>>> b
{'x': [Local('y'), Local('z')]}
Matching Structures and AST nodes
---------------------------------
For most node types other than ``Bind``, the predicates are a bit more complex.
By default, the predicate should be an exact (``istype``) match of the node
type, intersected with a recursive application of ``match_predicate()`` to each
of the target node's children. For example::
>>> b = {}
>>> from peak.util.assembler import *
>>> from peak.rules.codegen import *
>>> match_predicate(Add(any, any), Local('q'), b)
Test(IsInstance(Local('q')), istype(