edu.hendrix.grambler
Class Tree

java.lang.Object
  extended by edu.hendrix.grambler.Tree

public abstract class Tree
extends java.lang.Object


Constructor Summary
Tree(java.lang.String name, edu.hendrix.grambler.Input in, int start, int length)
           
 
Method Summary
 Tree getChild(int n)
          Returns the nth child from this Tree.
 int getEndingIndex()
          Returns the index into the parsed text of the final character matched by this Tree.
abstract  java.lang.String getErrorMessage()
          Returns an error message.
 int getFirstLine()
          Returns the line number of the first input character matched by this Tree.
 Tree getLastChild()
          Returns the rightmost child of this Tree.
 int getLength()
          Returns the number of characters matched by this Tree.
 java.lang.String getName()
          Returns the name of the nonterminal or terminal that corresponds to this node.
 Tree getNamedChild(java.lang.String name)
          Returns the first child with the given name.
 Tree getNamedChild(java.lang.String name, int n)
          Returns the nth child with the given name.
 int getNumChildren()
          Returns number of child nodes.
 int getNumNamed(java.lang.String name)
          Returns the number of children for whom getName().equals(name).
 int getStartingIndex()
          Returns the index into the parsed text of the first character matched by this Tree.
 boolean hasNamed(java.lang.String name)
          Returns whether there are any children for whom name().equals(name).
 boolean isEmpty()
          Indicates whether this Tree matches a zero-length input.
abstract  boolean isError()
          Indicates whether this is a syntax error.
 boolean isLeaf()
          Indicates whether this is a leaf.
 boolean isNamed(java.lang.String name)
          Returns true if getName().equals(name); returns false otherwise
 java.lang.String toString()
          Returns the substring of the input text matched by this Tree.
 java.lang.String toTextTree()
          Returns a String corresponding to a preorder traversal of this Tree.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tree

public Tree(java.lang.String name,
            edu.hendrix.grambler.Input in,
            int start,
            int length)
Method Detail

getName

public java.lang.String getName()
Returns the name of the nonterminal or terminal that corresponds to this node.

Returns:
The name of the nonterminal or terminal that corresponds to this node.

isNamed

public boolean isNamed(java.lang.String name)
Returns true if getName().equals(name); returns false otherwise

Returns:
Whether this Tree has the given name

getFirstLine

public int getFirstLine()
Returns the line number of the first input character matched by this Tree.

Returns:
The line number of the first input character matched by this Tree.

getStartingIndex

public int getStartingIndex()
Returns the index into the parsed text of the first character matched by this Tree.

Returns:
The index into the parsed text of the first character matched by this Tree.

getEndingIndex

public int getEndingIndex()
Returns the index into the parsed text of the final character matched by this Tree.

Returns:
The index into the parsed text of the final character matched by this Tree.

getLength

public int getLength()
Returns the number of characters matched by this Tree.

Returns:
The number of characters matched by this Tree.

isLeaf

public boolean isLeaf()
Indicates whether this is a leaf.

Returns:
true if this is a leaf node, false otherwise.

isError

public abstract boolean isError()
Indicates whether this is a syntax error.

Returns:
true if this tree represents a syntax error, false if it is a valid parse.

getErrorMessage

public abstract java.lang.String getErrorMessage()
Returns an error message. In the event of a syntax error, an error message is returned containing the following information:
1. The line number where the error occurred.
2. All of the successfully-matched text on the line where the error occurred.
If this Tree represents a valid parse, an exception is thrown.

Returns:
error message, only if this tree represents a syntax error
Throws:
java.lang.IllegalStateException - if isError() is false

isEmpty

public boolean isEmpty()
Indicates whether this Tree matches a zero-length input. Some grammars include regular expressions that can successfully match zero characters. This method returns true when nodes corresponding to those matches are encountered.

Returns:
true if this Tree has a zero-length matching input, false otherwise

toString

public java.lang.String toString()
Returns the substring of the input text matched by this Tree.

Overrides:
toString in class java.lang.Object
Returns:
the substring of the input text matched by this Tree.

getNumChildren

public int getNumChildren()
Returns number of child nodes.

Returns:
the number of child nodes this Tree has.

hasNamed

public boolean hasNamed(java.lang.String name)
Returns whether there are any children for whom name().equals(name).

Parameters:
name - name of a terminal or nonterminal symbol
Returns:
true if children are present with name, false otherwise

getNumNamed

public int getNumNamed(java.lang.String name)
Returns the number of children for whom getName().equals(name).

Parameters:
name - name of a terminal or nonterminal symbol
Returns:
number of child nodes with the given name

getChild

public Tree getChild(int n)
Returns the nth child from this Tree.

Parameters:
n - index value, 0 <= n < getNumChildren()
Returns:
tree corresponding to nth child
Throws:
java.lang.UnsupportedOperationException - if not an Interior node
java.lang.ArrayIndexOutOfBoundsException - if n is out of bounds

getLastChild

public Tree getLastChild()
Returns the rightmost child of this Tree.

Returns:
Rightmost child of this tree
Throws:
java.lang.UnsupportedOperationException - if not an Interior node
java.lang.ArrayIndexOutOfBoundsException - if there are no children

getNamedChild

public Tree getNamedChild(java.lang.String name)
Returns the first child with the given name.

Parameters:
name - name of a terminal or nonterminal symbol
Returns:
first child with the given name

getNamedChild

public Tree getNamedChild(java.lang.String name,
                          int n)
Returns the nth child with the given name.

Parameters:
name - name of a terminal or nonterminal symbol
n - 0 <= n < numNamed(name)
Returns:
nth child with the given name
Throws:
java.lang.UnsupportedOperationException - if not an Interior node
java.lang.ArrayIndexOutOfBoundsException - if n is out of bounds

toTextTree

public java.lang.String toTextTree()
Returns a String corresponding to a preorder traversal of this Tree.

Returns:
a String corresponding to a preorder traversal of this Tree.