ptolemy.data.expr
Class ASTPtRootNode

java.lang.Object
  extended by ptolemy.data.expr.ASTPtRootNode
All Implemented Interfaces:
java.lang.Cloneable, Node
Direct Known Subclasses:
ASTPtArrayConstructNode, ASTPtAssignmentNode, ASTPtBitwiseNode, ASTPtFunctionalIfNode, ASTPtFunctionApplicationNode, ASTPtFunctionDefinitionNode, ASTPtLeafNode, ASTPtLogicalNode, ASTPtMatrixConstructNode, ASTPtMethodCallNode, ASTPtPowerNode, ASTPtProductNode, ASTPtRecordConstructNode, ASTPtRelationalNode, ASTPtShiftNode, ASTPtSumNode, ASTPtUnaryNode, ASTPtUnionConstructNode

public class ASTPtRootNode
extends java.lang.Object
implements Node, java.lang.Cloneable

The parse tree created from the expression string consists of a hierarchy of node objects, each of which is an instance of a class derived from this class. This is because each node is a root node for the portion of the parse tree below it.

Each node in the parse tree stores its type and state information in a ptolemy.data.Token variable. A parent node uses the type and value of the ptolemy.data.Tokens contained in its child nodes to evaluate the type and value of the ptolemy.data.Token it should contain.

When a node has more than one child nodes, the lexical tokens relating the child nodes are stored in the parent node. Thus if we parsed a string such as "2+4-9", the child nodes would be leaf nodes containing ptolemy.data.Token's with values 2, 4 and 9, and the parent node would store the lexical tokens representing the "+" and the "-".

The tree is evaluated in a top down manner, calling evaluateParseTree() on the children of each node before resolving the type of the current node.

Since:
Ptolemy II 0.2
Version:
$Id: ASTPtRootNode.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Neil Smyth
See Also:
PtParser, Token
Accepted Rating:
Red (cxh)
Proposed Rating:
Yellow (nsmyth)

Field Summary
protected  java.util.ArrayList _children
           
protected  int _id
           
protected  boolean _isConstant
          Flags whether the parse tree under this root evaluates to a constant.
protected  Node _parent
           
protected  Token _ptToken
          Each node stores its type and state information in this variable.
protected  Type _ptType
          The type of this node.
 
Constructor Summary
ASTPtRootNode(int i)
           
ASTPtRootNode(PtParser p, int i)
           
 
Method Summary
 java.lang.Object clone()
          Clone the parse tree node by invoking the clone() method of the base class (java.lang.Object).
 void displayParseTree(java.lang.String prefix)
          Override this method if you want to customize how the node dumps out its children.
 Token evaluateParseTree()
          Deprecated. Use a ParseTreeEvaluator instead.
 Token getToken()
          Return the evaluated token value of this node.
 Type getType()
          Return the type of this node.
 boolean isCongruent(ASTPtRootNode node, java.util.Map renaming)
          Return true if this node is (hierarchically) congruent to the given node, under the given renaming of bound identifiers.
 boolean isConstant()
          Return true if this node represents a constant value.
 boolean isEvaluated()
          Return true if this node has had its token value set to something other than null.
 void jjtAddChild(Node n, int i)
          This method tells the node to add its argument to the node's list of children.
 void jjtClose()
          This method is called after all the child nodes have been added.
 Node jjtGetChild(int i)
          This method returns a child node.
 int jjtGetNumChildren()
          Return the number of children the node has.
 Node jjtGetParent()
           
 void jjtOpen()
          This method is called after the node has been made the current node.
 void jjtSetParent(Node n)
          This pair of methods are used to inform the node of its parent.
 void setConstant(boolean flag)
          Set whether this node is a constant.
 void setToken(Token token)
          Set the value of this node.
 void setType(Type type)
          Set the value of this node.
 java.lang.String toString()
          You can override these two methods in subclasses of RootNode to customize the way the node appears when the tree is dumped.
 java.lang.String toString(java.lang.String prefix)
           
 void visit(ParseTreeVisitor visitor)
          Traverse this node with the given visitor.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_parent

protected Node _parent

_children

protected java.util.ArrayList _children

_id

protected int _id

_ptToken

protected Token _ptToken
Each node stores its type and state information in this variable.


_ptType

protected Type _ptType
The type of this node.


_isConstant

protected boolean _isConstant
Flags whether the parse tree under this root evaluates to a constant.

Constructor Detail

ASTPtRootNode

public ASTPtRootNode(int i)

ASTPtRootNode

public ASTPtRootNode(PtParser p,
                     int i)
Method Detail

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Clone the parse tree node by invoking the clone() method of the base class (java.lang.Object).

Overrides:
clone in class java.lang.Object
Returns:
A new parse tree node.
Throws:
java.lang.CloneNotSupportedException - If the superclass clone() method throws it.

displayParseTree

public void displayParseTree(java.lang.String prefix)
Override this method if you want to customize how the node dumps out its children.


evaluateParseTree

public Token evaluateParseTree()
                        throws IllegalActionException
Deprecated. Use a ParseTreeEvaluator instead.

Evaluate the parse tree.

Returns:
The token contained by the root node for the parse tree.
Throws:
IllegalActionException - If an error occurs trying to evaluate the PtToken type and/or value to be stored in node in the tree.

getToken

public Token getToken()
Return the evaluated token value of this node. This value may be set during parsing, if this represents a constant value, or may be set during parse tree evaluation.


getType

public Type getType()
Return the type of this node. This value may be set during parsing, if this represents a constant value, or may be set during parse tree evaluation.


isCongruent

public boolean isCongruent(ASTPtRootNode node,
                           java.util.Map renaming)
Return true if this node is (hierarchically) congruent to the given node, under the given renaming of bound identifiers. Derived classes should extend this method to add additional necessary congruency checks.

Parameters:
node - The node to compare to.
renaming - A map from String to String that gives a renaming from identifiers in this node to identifiers in the given node.

isConstant

public boolean isConstant()
Return true if this node represents a constant value. This will be set to true if the node is a constant leaf node (either it is a constant leaf node, or a pure function with constant children.)


isEvaluated

public boolean isEvaluated()
Return true if this node has had its token value set to something other than null.


jjtAddChild

public void jjtAddChild(Node n,
                        int i)
Description copied from interface: Node
This method tells the node to add its argument to the node's list of children.

Specified by:
jjtAddChild in interface Node

jjtClose

public void jjtClose()
Description copied from interface: Node
This method is called after all the child nodes have been added.

Specified by:
jjtClose in interface Node

jjtGetChild

public Node jjtGetChild(int i)
Description copied from interface: Node
This method returns a child node. The children are numbered from zero, left to right.

Specified by:
jjtGetChild in interface Node

jjtGetNumChildren

public int jjtGetNumChildren()
Description copied from interface: Node
Return the number of children the node has.

Specified by:
jjtGetNumChildren in interface Node

jjtGetParent

public Node jjtGetParent()
Specified by:
jjtGetParent in interface Node

jjtOpen

public void jjtOpen()
Description copied from interface: Node
This method is called after the node has been made the current node. It indicates that child nodes can now be added to it.

Specified by:
jjtOpen in interface Node

jjtSetParent

public void jjtSetParent(Node n)
Description copied from interface: Node
This pair of methods are used to inform the node of its parent.

Specified by:
jjtSetParent in interface Node

setConstant

public void setConstant(boolean flag)
Set whether this node is a constant. In almost all cases this is statically determined when the parse tree is first parsed, and this method is not normally called. However, it can be useful to transform a parse tree, which can make parts of the parse tree constant which were not before. This method is provided for those transformations.


setToken

public void setToken(Token token)
Set the value of this node. This may be set during parsing, if the node is a constant node, or during evaluation of the expression.


setType

public void setType(Type type)
Set the value of this node. This may be set during parsing, if the node is a constant node, or during evaluation of the expression.


toString

public java.lang.String toString()
You can override these two methods in subclasses of RootNode to customize the way the node appears when the tree is dumped. If your output uses more than one line you should override toString(String), otherwise overriding toString() is probably all you need to do.

Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.lang.String prefix)

visit

public void visit(ParseTreeVisitor visitor)
           throws IllegalActionException
Traverse this node with the given visitor. subclasses should override this method to invoke the appropriate method in the visitor.

Throws:
IllegalActionException