001: /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
002:
003: package edu.umd.cs.findbugs.tools.patcomp;
004:
005: public class SimpleNode implements Node {
006: protected Node parent;
007: protected Node[] children;
008: protected int id;
009: protected PatternCompiler parser;
010:
011: /* DHH: added these fields to keep track of tokens */
012: private Token firstToken, lastToken;
013:
014: public SimpleNode(int i) {
015: id = i;
016: }
017:
018: public SimpleNode(PatternCompiler p, int i) {
019: this (i);
020: parser = p;
021: }
022:
023: /** Get the id of this node. */
024: public int getId() {
025: return id;
026: }
027:
028: /** Set first token. */
029: public void setFirstToken(Token t) {
030: if (t == null)
031: throw new IllegalStateException();
032: this .firstToken = t;
033: }
034:
035: /** Get the first token. */
036: public Token getFirstToken() {
037: return firstToken;
038: }
039:
040: /** Set last token. */
041: public void setLastToken(Token t) {
042: if (t == null)
043: throw new IllegalStateException();
044: this .lastToken = t;
045: }
046:
047: /** Get the last token. */
048: public Token getLastToken() {
049: return lastToken;
050: }
051:
052: /** Get number of tokens. */
053: public int getNumTokens() {
054: int count = 0;
055: Token cur = firstToken;
056: while (cur != lastToken) {
057: ++count;
058: cur = cur.next;
059: }
060: return count;
061: }
062:
063: /**
064: * Get <i>n</i>th token.
065: * Returns null if there are fewer than <i>n+1</i> tokens.
066: */
067: public Token getToken(int n) {
068: Token t = firstToken;
069: int count = 0;
070: while (t != null) {
071: if (count == n)
072: break;
073: ++count;
074: t = t.next;
075: }
076: return t;
077: }
078:
079: public void jjtOpen() {
080: }
081:
082: public void jjtClose() {
083: }
084:
085: public void jjtSetParent(Node n) {
086: parent = n;
087: }
088:
089: public Node jjtGetParent() {
090: return parent;
091: }
092:
093: public void jjtAddChild(Node n, int i) {
094: if (children == null) {
095: children = new Node[i + 1];
096: } else if (i >= children.length) {
097: Node c[] = new Node[i + 1];
098: System.arraycopy(children, 0, c, 0, children.length);
099: children = c;
100: }
101: children[i] = n;
102: }
103:
104: public Node jjtGetChild(int i) {
105: return children[i];
106: }
107:
108: public int jjtGetNumChildren() {
109: return (children == null) ? 0 : children.length;
110: }
111:
112: /* You can override these two methods in subclasses of SimpleNode to
113: customize the way the node appears when the tree is dumped. If
114: your output uses more than one line you should override
115: toString(String), otherwise overriding toString() is probably all
116: you need to do. */
117:
118: public String toString() {
119: return PatternCompilerTreeConstants.jjtNodeName[id];
120: }
121:
122: public String toString(String prefix) {
123: return prefix + toString();
124: }
125:
126: /* Override this method if you want to customize how the node dumps
127: out its children. */
128:
129: public void dump(String prefix) {
130: System.out.println(toString(prefix));
131: if (children != null) {
132: for (Node aChildren : children) {
133: SimpleNode n = (SimpleNode) aChildren;
134: if (n != null) {
135: n.dump(prefix + " ");
136: }
137: }
138: }
139: }
140: }
|