All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class epp.Tree

java.lang.Object
   |
   +----epp.Tree

public class Tree
extends Object
implements Cloneable, Serializable
The Tree class is a class for describing a node of an abstract syntax tree.

For example, an if statment will be described as the following abstract syntax tree within EPP. (if condition then else ) This node can be generated using the following instance creation expression. new Tree(:if, condition, then, else) The portion that specifies the node type (:if, in this case) is called a tag, and all subtrees of a node (condition, then, else, in this case) are called args.

A Tree is an immutable object. You cannot modify tags and args. (Programmers must be careful not to overwrite the contents of the Tree array that the tree.args() method call returns. If you do overwrite by mistake, it may cause a very confusing bug.)

Tree has a subclass named Identifier and LiteralTree. All nodes of an abstract syntax tree are expressed with one of the three classes, Tree, Indentifier or LiteralTree. Plug-ins can extend the syntax and introduce new types of nodes. However, those nodes must also be expressed with on of the three classes mentioned above.

By calling a method named tree.print() with an instance tree of class Tree, an abstract syntax tree can be printed in the form of an S-expression. For example, if you parse following program,

	if (x == 0) System.out.println("Hello!");
the resulting abstract syntax tree will printed as follows.
      (if
       (==
        (id x)
        (literalTree int "0"))
       (expressionStatement
        (invokeLong
         (name
          (id System)
          (id out))
         (id println)
         (argumentList
          (literalTree string "Hello!"))))
       (emptyStatement))
For LiteralTree and Identifier, tree.args() will always return an array of length zero. Due to this design, recursive processing of a tree will be very simple. For example, a method that counts the number of times an if statement appears in a tree can simply be defined as follows.
int countIf(Tree tree){
  int c = 0;
  if (tree.tag() == :if) c++;
  Tree[] args = tree.args();
  for (int i = 0; i < args.length; i++){
    c += countIf(args[i]);
  }
  return c;
}

Other than tags and args, the Tree class has additional information such as line numbers. If you wish to generate a tree with only the args value modified while retaining other information, you use the modifyArgs method.

	Tree[] newArgs = {...};
	Tree newTree = tree.modifyArgs(newArgs);
This is the same as the following program, except for the fact that information of the tree, such as line numbers, will be copied to newTree.
	Tree[] newArgs = {...};
	Tree newTree = new Tree(tree.tag(), newArgs);

The BackQuote plug-in is provided to make it easier to write code that generates a large Tree. Please refer to ../BackQuote.html for further information. You can cut and paste an abstract syntax tree that was printed by tree.print() or the -tree option of epp, and use it directly in the BackQuote macro.

The TreeVec class is provided since you will frequently use a variable length array of Tree. This is more convenient than using java.lang.Vector since you do not need to do any casting.

See Also:
Identifier, LiteralTree, TreeVec

Constructor Index

 o Tree(Symbol)
 o Tree(Symbol, Tree)
 o Tree(Symbol, Tree, Tree)
 o Tree(Symbol, Tree, Tree, Tree)
 o Tree(Symbol, Tree, Tree, Tree, Tree)
 o Tree(Symbol, Tree, Tree, Tree, Tree, Tree)
 o Tree(Symbol, Tree, Tree, Tree, Tree, Tree, Tree)
 o Tree(Symbol, Tree, Tree, Tree, Tree, Tree, Tree, Tree)
 o Tree(Symbol, Tree[])
 o Tree(Symbol, TreeVec)

Method Index

 o args()
Retrieves a subtree of an abstract syntax tree.
 o beginningPoint()
Returns the point of the starting position of this node.
 o endPoint()
Returns the point of the ending position of this node.
 o idName()
Returns the name of a node if it is an Identifier.
 o isTyped()
Returns true if this method has type information, and false otherwise.
 o lineNumber()
Returns the line number information of this node.
 o literalToken()
Returns a LiteralToken that describes the contents of a LiteralTree if this node is a LiteralTree.
 o main(String[])
Used for testing.
 o modifyArgs(Tree[])
Creates and returns a Tree with only args of this node modified.
 o modifyBeginningPoint(int)
 o modifyEndPoint(int)
 o modifyLineNumber(int)
 o modifyProperty(Symbol, Object)
Creates and returns a Tree with the property of this node modified.
 o modifyTypeAndArgs(Type, Tree[])
Creates and returns a Tree with only types and args of this node modified.
 o nameEqual(Tree, Tree)
Compares Trees that have :name tags.
 o nameToString()
Converts Tree that have :name tags into a string.
 o print()
Prints the contents to Opts.out in a human readable form.
 o print(PrintWriter)
 o print(PrintWriter, String)
 o printProp(PrintWriter)
 o property(Symbol)
Searches and returns a property attached to a node.
 o quoteString(String)
Called by class Identifier and LiteralToken.
 o resetType()
Creates and returns a Tree with no type information.
 o stringToNameTree(String)
 o tag()
Retrieves a tag that describes the type of abstract syntax tree.
 o toString()
Converts a tree to String without indentation.
 o type()
Returns the node type.

Constructors

 o Tree
 public Tree(Symbol tag,
             Tree args[])
 o Tree
 public Tree(Symbol tag,
             TreeVec args)
 o Tree
 public Tree(Symbol t)
 o Tree
 public Tree(Symbol t,
             Tree a1)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2,
             Tree a3)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2,
             Tree a3,
             Tree a4)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2,
             Tree a3,
             Tree a4,
             Tree a5)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2,
             Tree a3,
             Tree a4,
             Tree a5,
             Tree a6)
 o Tree
 public Tree(Symbol t,
             Tree a1,
             Tree a2,
             Tree a3,
             Tree a4,
             Tree a5,
             Tree a6,
             Tree a7)

Methods

 o main
 public static void main(String argv[])
Used for testing.

 o tag
 public Symbol tag()
Retrieves a tag that describes the type of abstract syntax tree.

 o args
 public Tree[] args()
Retrieves a subtree of an abstract syntax tree.

 o lineNumber
 public int lineNumber()
Returns the line number information of this node. The line number information is the line number of the top of the syntax that described this node. Nodes generated in the macro expansion pass and not in the parsing pass do not have line number information. If there are no line number information, -1 is returned.

 o beginningPoint
 public int beginningPoint()
Returns the point of the starting position of this node. This method is not implemented yet.

See Also:
EppInputStream
 o endPoint
 public int endPoint()
Returns the point of the ending position of this node. This method is not implemented yet.

See Also:
EppInputStream
 o type
 public Type type()
Returns the node type. If the type if undetermined, a fatal error will occur.

 o property
 public Object property(Symbol key)
Searches and returns a property attached to a node.

See Also:
modifyProperty
 o idName
 public Symbol idName()
Returns the name of a node if it is an Identifier. Otherwise, a fatal error will occur.

 o literalToken
 public LiteralToken literalToken()
Returns a LiteralToken that describes the contents of a LiteralTree if this node is a LiteralTree. Otherwise, a fatal error will occur.

 o modifyArgs
 public Tree modifyArgs(Tree args[])
Creates and returns a Tree with only args of this node modified. All information other than args, such as line number information, will be copied to the new Tree.

 o isTyped
 public boolean isTyped()
Returns true if this method has type information, and false otherwise.

 o modifyTypeAndArgs
 public Tree modifyTypeAndArgs(Type type,
                               Tree args[])
Creates and returns a Tree with only types and args of this node modified. All other information, such as line number information, will be copied to the new Tree. If this Tree already has type information, a fatal error will occur.

 o resetType
 public Tree resetType()
Creates and returns a Tree with no type information. All information other than type information are copied from the original Tree to the new Tree.

 o modifyProperty
 public Tree modifyProperty(Symbol key,
                            Object val)
Creates and returns a Tree with the property of this node modified. All information other than additonal information are copied from the original Tree to the new Tree.

See Also:
property
 o modifyLineNumber
 public Tree modifyLineNumber(int x)
 o modifyBeginningPoint
 public Tree modifyBeginningPoint(int x)
 o modifyEndPoint
 public Tree modifyEndPoint(int x)
 o nameEqual
 public static boolean nameEqual(Tree name1,
                                 Tree name2)
Compares Trees that have :name tags.

 o nameToString
 public String nameToString()
Converts Tree that have :name tags into a string. For example, (name (id a) (id b) (id c)) will become "a.b.c".

 o print
 public Tree print()
Prints the contents to Opts.out in a human readable form. Used for debugging. If you write tree.print(), the tree will be printed with indentations so that it will be easier to read. This method returns itself as the return value.

Type information and line number information will also be printed according to the options specified to the epp command on invocation.

If these information are not attached, you may cut and paste the printed results and use them directly as part of the BackQuote macro.

 o print
 public Tree print(PrintWriter out)
 o print
 public void print(PrintWriter out,
                   String indent)
 o printProp
 public void printProp(PrintWriter out)
 o toString
 public String toString()
Converts a tree to String without indentation.

Overrides:
toString in class Object
 o quoteString
 public static String quoteString(String in)
Called by class Identifier and LiteralToken.
   Examples:
  	"foo" -> "\"foo\""
  	"\n" -> "\"\\n\""
  	"\"" => "\"\\\"\""

 o stringToNameTree
 public static Tree stringToNameTree(String str)

All Packages  Class Hierarchy  This Package  Previous  Next  Index