001: package javaparser;
002:
003: import java.util.*;
004: import javaparser.javacc_gen.Token;
005:
006: /**
007: * Reusable through the RAWParserTreeNodeFactory.
008: * Two times quicker as MutableTreeNode !!
009: * Even quicker associated with the reusable nodes pool.
010: * Must be sed with the new SimplifiedSyntaxTree.
011: */
012: public final class RAWParserTreeNode {
013: // TODO.. throw away.
014: //public boolean isRawTreeRoot = false;
015:
016: // for leafs
017: Token t = null;
018:
019: // for nodes
020: String name = null;
021:
022: // tree structure
023: final public List<RAWParserTreeNode> childs = new ArrayList<RAWParserTreeNode>(
024: 1);
025: public RAWParserTreeNode parent = null;
026:
027: RAWParserTreeNode(Token t) {
028: this .t = t;
029: }
030:
031: RAWParserTreeNode(String name) {
032: this .name = name;
033: }
034:
035: /** Used by the simpifiedtree parser.
036: * Name if non null or the token image.
037: */
038: @Override
039: public String toString() {
040: if (name != null) {
041: return name;
042: }
043:
044: return t.image;
045: }
046:
047: /** Also sets child's parent to this.
048: */
049: public void add(RAWParserTreeNode child) {
050: childs.add(child);
051: child.parent = this ;
052: }
053:
054: /** Without any terminations !
055: */
056: public void removeFromParent() {
057: parent.childs.remove(this );
058: parent = null;
059: }
060:
061: public int getChildCount() {
062: return childs.size();
063: }
064:
065: /** -1 if not a token
066: */
067: public int getTokenKind() {
068: if (t == null)
069: return -1;
070: return t.kind;
071: }
072:
073: /** null if not a token
074: */
075: @edu.umd.cs.findbugs.annotations.CheckForNull
076: public Token getToken() {
077: return t;
078: }
079:
080: public boolean isToken() {
081: return t != null;
082: }
083:
084: public RAWParserTreeNode getChildNodeAt(int i) {
085: return childs.get(i);
086: }
087:
088: public RAWParserTreeNode getNextSibling() {
089: if (parent == null)
090: return null;
091: int tp = parent.childs.indexOf(this );
092: if (tp + 1 >= parent.childs.size())
093: return null;
094: return parent.childs.get(tp + 1);
095: }
096:
097: public int getIndex(RAWParserTreeNode child) {
098: return childs.indexOf(child);
099: }
100:
101: /** Not recurse.
102: */
103: public void terminateAndOfferReuse() {
104: childs.clear();
105: parent = null;
106:
107: name = null;
108: t = null;
109:
110: RAWParserTreeNodeFactory.getInstance().addFreed(this);
111: }
112:
113: }
|