Class ListNode

java.lang.Object
  extended by TreeNode
      extended by ListNode
Direct Known Subclasses:
Cases, Classes, Expressions, Features, Formals

abstract class ListNode
extends TreeNode

Base class for lists of AST elements.

(See TreeNode for a discussion of AST nodes in general)

List phyla have a distinct set of operations for constructing and accessing lists. For each phylum named X there is a phylum called Xs (except for Classes, which is a list of Class_ nodes) of type List[X].

An empty list is created with new Xs(lineno). Elements may be appended to the list using either addElement() or appendElement(). appendElement returns the list itself, so calls to it may be chained, as in list.appendElement(Foo).appendElement(Bar).appendElement(Baz).

You can use java.util.Enumeration to iterate through lists. If you are not familiar with that interface, look it up in the Java API documentation. Here's an example of iterating through a list:

  for (Enumeration e = list.getElements(); e.hasMoreElements(); ) {
    Object n = e.nextElement();
    ... do something with n ...
  }
Alternatively, it is possible to iterate using an integer index:
  for (int i = 0; i < list.getLength(); i++) {
    ... do something with list.getNth(i) ...
  }
Note: nextElement() returns the value of type Object and getNth() returns the value of type TreeNode. You will most likely need to cast it to the type appropriate for the list element. This is one of the very few cases where casting is actually necessary and appropriate.


Field Summary
private  java.util.Vector elements
           
 
Fields inherited from class TreeNode
lineNumber
 
Constructor Summary
protected ListNode(int lineNumber)
          Builds a new list node
protected ListNode(int lineNumber, java.util.Vector elements)
           
 
Method Summary
 void addElement(TreeNode node)
          Appends an element to the list.
protected  java.util.Vector copyElements()
          Creates a deep copy of this list.
 void dump(java.io.PrintStream out, int n)
          Pretty-prints this list to this output stream.
abstract  java.lang.Class getElementClass()
          Returns the class of list elements.
 java.util.Enumeration getElements()
          Retreives the elements of the list as Enumeration.
 int getLength()
          Retreives the length of the list.
 TreeNode getNth(int n)
          Retreives nth element of the list.
 java.lang.String toString()
          Returns a string representation of this list.
 
Methods inherited from class TreeNode
copy_AbstractSymbol, copy_Boolean, copy, dump_AbstractSymbol, dump_Boolean, dump_line, getLineNumber, set
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

elements

private java.util.Vector elements
Constructor Detail

ListNode

protected ListNode(int lineNumber,
                   java.util.Vector elements)

ListNode

protected ListNode(int lineNumber)
Builds a new list node

Parameters:
lineNumber - line in the source file from which this node came.
Method Detail

copyElements

protected java.util.Vector copyElements()
Creates a deep copy of this list. None of the elements are shared between the lists, e.g. all elements are duplicated (which is what "deep copy" means).

Returns:
a copy of this elements vector

getElementClass

public abstract java.lang.Class getElementClass()
Returns the class of list elements.

Returns:
the element class

getNth

public TreeNode getNth(int n)
Retreives nth element of the list.

Parameters:
n - the index of the element
Returns:
the element

getLength

public int getLength()
Retreives the length of the list.

Returns:
the length of the list

getElements

public java.util.Enumeration getElements()
Retreives the elements of the list as Enumeration.

Returns:
the elements

addElement

public void addElement(TreeNode node)
Appends an element to the list.

Note: each generated subclass of ListNode also has an appendElement() method, which calls addElement() and returns the list of the appropriate type, so that it can be used like this: l.appendElement(i).appendElement(j).appendElement(k);

Parameters:
node - a node to append

dump

public void dump(java.io.PrintStream out,
                 int n)
Pretty-prints this list to this output stream.

Specified by:
dump in class TreeNode
Parameters:
out - the output stream
n - the number of spaces to indent the output

toString

public java.lang.String toString()
Returns a string representation of this list.

Overrides:
toString in class java.lang.Object
Returns:
a string representation