xtc.tree
Class Transducer

java.lang.Object
  extended by xtc.tree.Visitor
      extended by xtc.tree.Transducer

public class Transducer
extends Visitor

Visitor to convert trees of generic nodes into methods that programmatically create the trees. Trees may contain generic nodes that represent pattern variables. During transduction, pattern variable are replaced with method arguments of the same names. In general, a variable represents a single child of a newly created generic node. However, if the variable's type is List<T>, all of the list's elements are directly added as children to the newly created generic node. Any node representing a pattern variable must have exactly one child that is a string representing the variable's name.

Version:
$Revision: 1.5 $
Author:
Robert Grimm

Field Summary
protected  Printer printer
          The printer.
protected  int varcount
          The temporary variable count.
protected  java.util.Map<java.lang.String,java.lang.String> variables
          The mapping from pattern variable names to their types.
 
Constructor Summary
Transducer(Printer printer)
          Create a new transducer.
Transducer(Printer printer, java.util.Map<java.lang.String,java.lang.String> variables)
          Create a new transducer.
 
Method Summary
 java.lang.String getVariableName(Node n)
          Get the pattern variable's name.
 java.lang.String getVariableType(Node n)
          Get the pattern variable's type.
 boolean isListType(java.lang.String t)
          Determine whether the specified type is a list type.
 boolean isPatternVariable(Node n)
          Determine whether the specified node represents a pattern variable.
 void process(java.lang.String method, Node n)
          Process the specified node.
 java.lang.String toLiteral(java.lang.Object o, java.lang.String var)
          Convert the specified object to a literal.
 GNode toPatternVariable(Node n)
          Ensure that the specified node is a well-formed pattern variable.
 java.lang.String visit(GNode n)
          Visit the specified generic node.
 
Methods inherited from class xtc.tree.Visitor
dispatch, equals, hashCode, iterate, map, mapInPlace, unableToVisit, visit
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

printer

protected final Printer printer
The printer.


variables

protected final java.util.Map<java.lang.String,java.lang.String> variables
The mapping from pattern variable names to their types.


varcount

protected int varcount
The temporary variable count.

Constructor Detail

Transducer

public Transducer(Printer printer)
Create a new transducer. The newly created transducer treats all trees as literals, without pattern variables.

Parameters:
printer - The printer.

Transducer

public Transducer(Printer printer,
                  java.util.Map<java.lang.String,java.lang.String> variables)
Create a new transducer. The mapping from pattern variable names to types may be empty (but not null) to indicate that all tree nodes are literal, i.e., do not contain any "holes" that are filled during creation.

Parameters:
printer - The printer.
variables - The mapping from node names representing pattern variables to their values' types.
Method Detail

isPatternVariable

public boolean isPatternVariable(Node n)
Determine whether the specified node represents a pattern variable.

Parameters:
n - The node.
Returns:
true if it represents a pattern variable.

toPatternVariable

public GNode toPatternVariable(Node n)
Ensure that the specified node is a well-formed pattern variable.

Parameters:
n - The node.
Returns:
The node as a pattern variable.
Throws:
java.lang.IllegalArgumentException - Signals that the specified node is not a pattern variable or is a malformed pattern variable.

getVariableName

public java.lang.String getVariableName(Node n)
Get the pattern variable's name.

Parameters:
n - The node.
Returns:
The corresponding variable name.
Throws:
java.lang.IllegalArgumentException - Signals that the node does not represent a pattern variable or is malformed.

getVariableType

public java.lang.String getVariableType(Node n)
Get the pattern variable's type.

Parameters:
n - The node.
Returns:
The corresponding variable type.
Throws:
java.lang.IllegalArgumentException - Signals that the node does not represent a pattern variable or is malformed.

isListType

public boolean isListType(java.lang.String t)
Determine whether the specified type is a list type.

Parameters:
t - The type.
Returns:
true if the specified type is a list type.

toLiteral

public java.lang.String toLiteral(java.lang.Object o,
                                  java.lang.String var)
Convert the specified object to a literal. If the specified variable name is null and the object is a node, the object must be a pattern variable.

Parameters:
o - The object.
var - The variable name for nodes that are not pattern variables.
Returns:
The corresponding literal.
Throws:
java.lang.IllegalArgumentException - Signals that the specified object is not recognized.

process

public void process(java.lang.String method,
                    Node n)
Process the specified node.

Parameters:
method - The method name.
n - The node.
Throws:
java.lang.IllegalArgumentException - Signals that the specified is not a generic node or that it is a pattern variable.

visit

public java.lang.String visit(GNode n)
Visit the specified generic node.



Copyright © 2012. All Rights Reserved.