1 2 3 package edu.umd.cs.findbugs.tools.patcomp; 4 5 public class SimpleNode implements Node { 6 protected Node parent; 7 protected Node[] children; 8 protected int id; 9 protected PatternCompiler parser; 10 11 12 private Token firstToken, lastToken; 13 14 public SimpleNode(int i) { 15 id = i; 16 } 17 18 public SimpleNode(PatternCompiler p, int i) { 19 this(i); 20 parser = p; 21 } 22 23 24 public int getId() { 25 return id; 26 } 27 28 29 public void setFirstToken(Token t) { 30 if (t == null) throw new IllegalStateException (); 31 this.firstToken = t; 32 } 33 34 35 public Token getFirstToken() { 36 return firstToken; 37 } 38 39 40 public void setLastToken(Token t) { 41 if (t == null) throw new IllegalStateException (); 42 this.lastToken = t; 43 } 44 45 46 public Token getLastToken() { 47 return lastToken; 48 } 49 50 51 public int getNumTokens() { 52 int count = 0; 53 Token cur = firstToken; 54 while (cur != lastToken) { 55 ++count; 56 cur = cur.next; 57 } 58 return count; 59 } 60 61 65 public Token getToken(int n) { 66 Token t = firstToken; 67 int count = 0; 68 while (t != null) { 69 if (count == n) 70 break; 71 ++count; 72 t = t.next; 73 } 74 return t; 75 } 76 77 public void jjtOpen() { 78 } 79 80 public void jjtClose() { 81 } 82 83 public void jjtSetParent(Node n) { parent = n; } 84 public Node jjtGetParent() { return parent; } 85 86 public void jjtAddChild(Node n, int i) { 87 if (children == null) { 88 children = new Node[i + 1]; 89 } else if (i >= children.length) { 90 Node c[] = new Node[i + 1]; 91 System.arraycopy(children, 0, c, 0, children.length); 92 children = c; 93 } 94 children[i] = n; 95 } 96 97 public Node jjtGetChild(int i) { 98 return children[i]; 99 } 100 101 public int jjtGetNumChildren() { 102 return (children == null) ? 0 : children.length; 103 } 104 105 110 111 public String toString() { return PatternCompilerTreeConstants.jjtNodeName[id]; } 112 public String toString(String prefix) { return prefix + toString(); } 113 114 116 117 public void dump(String prefix) { 118 System.out.println(toString(prefix)); 119 if (children != null) { 120 for (Node aChildren : children) { 121 SimpleNode n = (SimpleNode) aChildren; 122 if (n != null) { 123 n.dump(prefix + " "); 124 } 125 } 126 } 127 } 128 } 129 130 | Popular Tags |