All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----epp.Tree
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 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.
Identifier
.
LiteralToken
that describes the contents of a
LiteralTree
if this node is a LiteralTree
.
Tree
with only args of this node modified.
Tree
with the property of this node
modified.
Tree
with only types and args of this
node modified.
Tree
s that have :name
tags.
Tree
that have :name
tags into a string.
Opts.out
in a human readable form.
Identifier
and LiteralToken
.
Tree
with no type information.
String
without indentation.
public Tree(Symbol tag, Tree args[])Tree
public Tree(Symbol tag, TreeVec args)Tree
public Tree(Symbol t)Tree
public Tree(Symbol t, Tree a1)Tree
public Tree(Symbol t, Tree a1, Tree a2)Tree
public Tree(Symbol t, Tree a1, Tree a2, Tree a3)Tree
public Tree(Symbol t, Tree a1, Tree a2, Tree a3, Tree a4)Tree
public Tree(Symbol t, Tree a1, Tree a2, Tree a3, Tree a4, Tree a5)Tree
public Tree(Symbol t, Tree a1, Tree a2, Tree a3, Tree a4, Tree a5, Tree a6)Tree
public Tree(Symbol t, Tree a1, Tree a2, Tree a3, Tree a4, Tree a5, Tree a6, Tree a7)
public static void main(String argv[])
public Symbol tag()
public Tree[] args()
public int lineNumber()
public int beginningPoint()
public int endPoint()
public Type type()
public Object property(Symbol key)
public Symbol idName()
Identifier
.
Otherwise, a fatal error will occur.
public LiteralToken literalToken()
LiteralToken
that describes the contents of a
LiteralTree
if this node is a LiteralTree
.
Otherwise, a fatal error will occur.
public Tree modifyArgs(Tree args[])
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
.
public boolean isTyped()
public Tree modifyTypeAndArgs(Type type, Tree args[])
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.
public Tree resetType()
Tree
with no type information.
All information other than type information are copied from the original
Tree
to the new Tree
.
public Tree modifyProperty(Symbol key, Object val)
Tree
with the property of this node
modified. All information other than additonal information are copied
from the original Tree
to the new Tree
.
public Tree modifyLineNumber(int x)modifyBeginningPoint
public Tree modifyBeginningPoint(int x)modifyEndPoint
public Tree modifyEndPoint(int x)nameEqual
public static boolean nameEqual(Tree name1, Tree name2)
Tree
s that have :name
tags.
public String nameToString()
Tree
that have :name
tags into a string.
For example, (name (id a) (id b) (id c)) will become "a.b.c".
public Tree print()
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.
public Tree print(PrintWriter out)print
public void print(PrintWriter out, String indent)printProp
public void printProp(PrintWriter out)toString
public String toString()
String
without indentation.
public static String quoteString(String in)
Identifier
and LiteralToken
.
Examples: "foo" -> "\"foo\"" "\n" -> "\"\\n\"" "\"" => "\"\\\"\""
public static Tree stringToNameTree(String str)
All Packages Class Hierarchy This Package Previous Next Index