edu.mit.ai.psg.jeva
Class JevaNode

java.lang.Object
  |
  +--edu.mit.ai.psg.jeva.SimpleNode
        |
        +--edu.mit.ai.psg.jeva.JevaNode
Direct Known Subclasses:
ExpressionNode, JNCompilationUnit, JNDClassBody, JNDClassDeclaration, JNDConstructorDeclaration, JNDFieldDeclaration, JNDImportDeclaration, JNDInitializer, JNDMethodDeclaration, JNDPackageDeclaration, JNEArguments, JNEArrayDimensions, JNEArrayInitializer, JNEAssignmentOperator, JNFormalParameter, JNFormalParameterList, JNInitializer, JNNameList, JNPrimitiveType, JNResultType, JNSBlock, JNSBreakStatement, JNSContinueStatement, JNSDoStatement, JNSEmptyStatement, JNSExplicitConstructorInvocation, JNSExpressionStatement, JNSExpressionStatementList, JNSForStatement, JNSIfStatement, JNSLabeledStatement, JNSLocalVariableDeclaration, JNSReturnStatement, JNSSwitchLabel, JNSSwitchStatement, JNSSynchronizedStatement, JNSThrowStatement, JNSTryStatement, JNSVariableDeclarator, JNSWhileStatement, JNType

public class JevaNode
extends SimpleNode
implements IJevaNode

JevaNode extends SimpleNode so each node can echo back its token stream of Java code. Adds first and last+1 token of the parsed syntax, so the syntax can be regenerated, and adds print methods to echo back the parsed java code, with or without comments. (This is an extension class so that it is easier to upgrade to new versions of JavaCC that generate new versions of SimpleNode.)

Author:
CarlManning, caroma@ai.mit.edu
Copyright (c) 1997, 1998, 1999 Massachusetts Institute of Technology

Fields inherited from class edu.mit.ai.psg.jeva.SimpleNode
children, id, parent
 
Constructor Summary
JevaNode(int id)
           
 
Method Summary
static void checkInsertionPoint(Token beginHole, IJevaNode node, int index)
          check that beginHole is a valid insertion point for inserting child tokens into node's tokens for a new child at index
static Token find2ndTokenBeforeEndBody(IJevaNode bodyNode)
          find token before last '}' of a body node.
static int findChildIndex(IJevaNode parent, IJevaNode child)
           
static Token findPrecedingToken(Token startToken, Token findToken)
          search token chain starting from startToken, returning token just before findToken
static Token findTokenBeforeChild(IJevaNode parent, IJevaNode child)
          find the token just before this child's beginToken by walking up parse tree to find previous leaf.
static Token findTokenBeforeChild(IJevaNode parent, IJevaNode child, int childIndex)
           
static Token findTokenBeforeEnd(IJevaNode node)
          find the token just before the end token of this node.
 Token getBeginToken()
           
 Token getEndToken()
           
static void insertChildTokens(IJevaNode parent, IJevaNode newChild, int index, Token beginHole)
          Insert tokens of new child into token chain after token beginHole.
 void insertNewChildAfter(IJevaNode newChild, IJevaNode oldChild)
          Insert new child, linking in token stream just after old child.
 void insertNewChildAt(IJevaNode newChild, int index, Token beginHole)
          insert new child, linking in token stream just after beginHole.
 void insertNewChildBefore(IJevaNode newChild, IJevaNode oldChild)
          Insert new child, linking in token stream just before old child.
 Object jjtAccept(IJevaVisitor visitor, Object data)
          Accept a visitor.
 IJevaNode jnGetChild(int i)
          jnGetChild is like Node.jjtGetChild except it returns an IJevaNode
 IJevaNode jnGetParent()
          jnGetParent is like Node.jjtGetParent except it returns an IJevaNode
 void print(PrintWriter stream)
          Print tokens of parsed node's syntax, including children, to stream.
 void print(PrintWriter stream, boolean includeSpecialTokens)
           
static void printToken(Token t, PrintWriter stream, boolean includeSpecialTokens)
           
 String printToString()
          Print tokens of parsed node's syntax, including children, to string.
 String printToString(boolean includeSpecialTokens)
           
 void removeChild(IJevaNode child)
          remove a child node and remove its tokens from token chain.
static void removeChildTokens(IJevaNode parent, IJevaNode child, int childIndex)
          Remove tokens of child from token chain.
 void replaceChild(IJevaNode oldChild, IJevaNode newChild)
           
 void replaceChild(int index, IJevaNode oldChild, IJevaNode newChild)
           
 void setBeginToken(Token t)
          Called by the parser to set the first token of the parsed syntax
 void setEndToken(Token t)
          Called by the parser to set the last+1 token of the parsed syntax
static void updateEndTokens(IJevaNode node, Token oldEndToken, Token newEndToken)
          if node's end token is oldEndToken, update it to newEndToken.
static void updateEndTokensPriorChild(IJevaNode parent, int childIndex, Token oldEndToken, Token newEndToken)
          update the endTokens of node preceding node at childIndex of parent
static void updateEndTokensPriorNode(IJevaNode parent, int index, Token oldEndToken, Token newEndToken)
           
static void updateEndTokensPriorNode(IJevaNode node, Token oldEndToken, Token newEndToken)
          update preceding nodes that used to have endHole as their end token set parent* node begin tokens as needed.
 
Methods inherited from class edu.mit.ai.psg.jeva.SimpleNode
dump, jjtAddChild, jjtClose, jjtGetChild, jjtGetNumChildren, jjtGetParent, jjtOpen, jjtSetParent, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JevaNode

public JevaNode(int id)
Method Detail

getBeginToken

public final Token getBeginToken()
Specified by:
getBeginToken in interface IJevaNode
Returns:
the first token (in token chain) of the parsed syntax

getEndToken

public final Token getEndToken()
Specified by:
getEndToken in interface IJevaNode
Returns:
the token after the last token of the parsed syntax

setBeginToken

public final void setBeginToken(Token t)
Called by the parser to set the first token of the parsed syntax
Specified by:
setBeginToken in interface IJevaNode

setEndToken

public final void setEndToken(Token t)
Called by the parser to set the last+1 token of the parsed syntax
Specified by:
setEndToken in interface IJevaNode

print

public void print(PrintWriter stream)
Print tokens of parsed node's syntax, including children, to stream. also prints special tokens (whitespace and comments)
Specified by:
print in interface IJevaNode

print

public void print(PrintWriter stream,
                  boolean includeSpecialTokens)
Specified by:
print in interface IJevaNode

printToken

public static final void printToken(Token t,
                                    PrintWriter stream,
                                    boolean includeSpecialTokens)

printToString

public String printToString()
Print tokens of parsed node's syntax, including children, to string. Includes special tokens (whitespace and comments).
Specified by:
printToString in interface IJevaNode
Returns:
a string recreating node's syntax

printToString

public String printToString(boolean includeSpecialTokens)
Specified by:
printToString in interface IJevaNode

jjtAccept

public Object jjtAccept(IJevaVisitor visitor,
                        Object data)
Accept a visitor. For Visitor design pattern.
Specified by:
jjtAccept in interface IJevaNode

jnGetChild

public IJevaNode jnGetChild(int i)
jnGetChild is like Node.jjtGetChild except it returns an IJevaNode
Specified by:
jnGetChild in interface IJevaNode

jnGetParent

public IJevaNode jnGetParent()
jnGetParent is like Node.jjtGetParent except it returns an IJevaNode
Specified by:
jnGetParent in interface IJevaNode

removeChild

public void removeChild(IJevaNode child)
remove a child node and remove its tokens from token chain.
Specified by:
removeChild in interface IJevaNode

replaceChild

public void replaceChild(IJevaNode oldChild,
                         IJevaNode newChild)
Specified by:
replaceChild in interface IJevaNode

replaceChild

public void replaceChild(int index,
                         IJevaNode oldChild,
                         IJevaNode newChild)
Specified by:
replaceChild in interface IJevaNode

insertNewChildAfter

public void insertNewChildAfter(IJevaNode newChild,
                                IJevaNode oldChild)
Insert new child, linking in token stream just after old child.
Specified by:
insertNewChildAfter in interface IJevaNode

insertNewChildBefore

public void insertNewChildBefore(IJevaNode newChild,
                                 IJevaNode oldChild)
Insert new child, linking in token stream just before old child.
Specified by:
insertNewChildBefore in interface IJevaNode

insertNewChildAt

public void insertNewChildAt(IJevaNode newChild,
                             int index,
                             Token beginHole)
insert new child, linking in token stream just after beginHole.
Specified by:
insertNewChildAt in interface IJevaNode

removeChildTokens

public static void removeChildTokens(IJevaNode parent,
                                     IJevaNode child,
                                     int childIndex)
Remove tokens of child from token chain. Child node must still be one of parent node's children.

insertChildTokens

public static void insertChildTokens(IJevaNode parent,
                                     IJevaNode newChild,
                                     int index,
                                     Token beginHole)
Insert tokens of new child into token chain after token beginHole. NewChild must not yet be added to parent's children at index. BeginHole must be a token in parent's chain, not it's children's (except may be last token in prior child, or prior node if inserted at start). Parent's jnGetChild(index) may contain next child (to be moved later), may be null (newChild will overwrite it later), or it may be that index == parent.jjtGetNumChildren() (will be extended later). Child tokens will be added between token beginHole and token beginHole.next.

updateEndTokensPriorNode

public static void updateEndTokensPriorNode(IJevaNode node,
                                            Token oldEndToken,
                                            Token newEndToken)
update preceding nodes that used to have endHole as their end token set parent* node begin tokens as needed.

updateEndTokensPriorNode

public static void updateEndTokensPriorNode(IJevaNode parent,
                                            int index,
                                            Token oldEndToken,
                                            Token newEndToken)

updateEndTokens

public static void updateEndTokens(IJevaNode node,
                                   Token oldEndToken,
                                   Token newEndToken)
if node's end token is oldEndToken, update it to newEndToken. Also update any decendents of this node that end at oldEndToken.

updateEndTokensPriorChild

public static void updateEndTokensPriorChild(IJevaNode parent,
                                             int childIndex,
                                             Token oldEndToken,
                                             Token newEndToken)
update the endTokens of node preceding node at childIndex of parent

findTokenBeforeChild

public static Token findTokenBeforeChild(IJevaNode parent,
                                         IJevaNode child)
find the token just before this child's beginToken by walking up parse tree to find previous leaf.

findTokenBeforeChild

public static Token findTokenBeforeChild(IJevaNode parent,
                                         IJevaNode child,
                                         int childIndex)

findTokenBeforeEnd

public static Token findTokenBeforeEnd(IJevaNode node)
find the token just before the end token of this node.

findPrecedingToken

public static Token findPrecedingToken(Token startToken,
                                       Token findToken)
search token chain starting from startToken, returning token just before findToken

find2ndTokenBeforeEndBody

public static Token find2ndTokenBeforeEndBody(IJevaNode bodyNode)
find token before last '}' of a body node. for inserting a new child at end of body

findChildIndex

public static int findChildIndex(IJevaNode parent,
                                 IJevaNode child)

checkInsertionPoint

public static void checkInsertionPoint(Token beginHole,
                                       IJevaNode node,
                                       int index)
check that beginHole is a valid insertion point for inserting child tokens into node's tokens for a new child at index

Copyright (c) 1996-1999
Massachusetts Institute of Technology

Feedback: jeva-bugs@ai.mit.edu