TreeUtils.groovy

Utilities for working with the syntax tree from ProRefactor. (Download below)

static printTree(JPNode n)

Prints the tree under the given node, output to stdout.

NOTE: At time of writing, this is simply dumping the token names from Proparse's API. This script should be changed to be sure to get the valid TokenTypes.TOKEN_NAME. See the notes at the top of http://www.joanju.com/dist/index.html.

static boolean matchNode(JPNode firstNode, List specList)

Test if part of the AST matches a given specification.

The specification is a list. Each element in the list may be an integer node type, a closure, or another nested
list describing the previous node's children.

Example:

import org.prorefactor.core.TokenTypes as T
[
T.RUN, [
  T.VALUE, [
    T.LEFTPAREN,
    T.Widget_ref, [
      T.THISPROCEDURE,
      T.OBJCOLON,
      {it.getText().equalsIgnoreCase('PRIVATE-DATA')} ],
    T.RIGHTPAREN ],
  T.PERIOD ]
]

Note how the example looks a lot like a simple printout from printTree():

RUN
  VALUE
    LEFTPAREN
    Widget_ref
      THISPROCEDURE
      OBJCOLON
      ID "private-data"
    RIGHTPAREN
  PERIOD

Also note that in the example, we are using a Groovy closure to test a node's text, rather than the node type. Anything from the node could be tested, like filename, line, etc. (The "it" variable is the implicit single argument in a Groovy closure, see the Groovy User's Guide for details.)

Once "import static" is supported in Groovy, we won't have to write "T." for every node type, and the specifications will be easier to read.

A null in the list will match any node. An empty sub-list will cause a node's children to match without checking.

The first element of any list must not be a sub-list.

Typically, the input spec list will contain two elements: a spec for a head node, and a sub list containing the specs for its children. But, it's also perfectly valid to specify n sibling nodes in the spec list, and maybe their children.


AttachmentSize
TreeUtils.groovy3.99 KB