XML patterns

Whenever users need to refer to some source code construct (declaration, function call, assignment, etc.), they can use Stanse's XML pattern technique. The reasons are as follows.

Programming language expressions can be represented in an AST form. Stanse represents AST of source code using XML. Thus users can easy refer to the original source code through Stanse uniform form of AST by defining appropriate XML file. In addition, Stanse XML patterns allow the users to parametrize AST representations and thus increase expressivity.

First, users should be familiar with Stanse XML representation of AST before proceeding to XML patterns. For more information on Stanse AST see ???.

XML patterns form a superset of Stanse AST XML representation. Here is a complete list of XML expressions which extend the representation.

In the following enumeration, let S and S' be two different ASTs of source code to undergo pattern matching. And let P be the XML pattern to be explained (coze?). Let X be some uninterpreted XML pattern.

  • Matching a nested element
    P = … <nested exclude1="astNode1" … excludeN="astNodeN">X</nested> …
    P matches S iff S contains subtree R' whose root is not equal to neither astNode1,...,astNodeN and R' matches X.
  • Skipping one element
    P = … <ignore/> …
    P matches S iff S consists of exactly same XML nodes as P, the only difference can be in node related to the <ignore/> node in P.
  • Skipping all elements on the same level (i.e. siblings)
    P = … <any/> …
    Let Ns be a parent node of <any/> node of P and Np be related node to Ns in S. Then P matches S iff S consists of exactly same XML nodes as P, the only difference can be in subtrees Ns and Np, where child nodes of Np before <any/> must exactly match child nodes of Ns, but other nodes may differ, or can be missing, or there may be nodes which does not have related nodes in Np.
  • Defining a variable
    P = … <var name="nm"/> …
    P matches S iff S consists of exactly same XML nodes as P, the only difference can be in node related to <var name="nm"/> node in P.

    Let's suppose that both S and S' matches P. Let Ns be a node in S related to <var name="nm"/> and Ns' be a node in S' related to <var name="nm"/>. Stanse automatically remembers nodes related to <var name="nm"/>, so the user can distinguish between all matching ASTs of P, here between S and S', by comparing those related nodes, here Ns and Ns'.

  • Defining a variable with pattern matching
    P = … <var name="nm" match="A"/> …
    P matches S iff P matches S just like in case where P = … <var name="nm"/> …, but in addition, node in S related to <var name ="nm" match="A"/> must contain an element named 'A'.
  • Defining a variable with multiple pattern matching
    P = … <var name="nm" match="{A}{B}{C}"/> …
    P matches S iff P matches S just like in case where P = … <var name="nm"/> …, but in addition, node in S related to <var name="nm" match="{A}{B}{C}"/> must contain an element named 'A', or 'B', or 'C'.
  • Defining a variable with element excluding
    P = … <var name="nm" except="A"/> …
    P matches S iff P matches S just like in case where P = … <var name="nm"/> …, but in addition, node in S related to <var name="nm" match="A"/> must not contain an element named 'A'.
  • Defining a variable with multiple element excluding
    P = … <var name="nm" except="{A}{B}{C}"/> …
    P matches S iff P matches S just like in case where P = … <var name="nm"/> …, but in addition, node in S related to <var name="nm" match="{A}{B}{C}"/> must not contain an element named 'A', nor 'B', nor 'C'.