1 2 23 24 25 26 27 28 import java.io.PrintWriter ; 29 30 public class SimpleNode implements Node { 31 protected Node parent; 32 protected Node[] children; 33 protected int id; 34 35 public SimpleNode(int i) { 36 id = i; 37 } 38 39 public void jjtOpen() { 40 } 41 42 public void jjtClose() { 43 } 44 45 public void jjtSetParent(Node n) { parent = n; } 46 public Node jjtGetParent() { return parent; } 47 48 public void jjtAddChild(Node n, int i) { 49 if (children == null) { 50 children = new Node[i + 1]; 51 } else if (i >= children.length) { 52 Node c[] = new Node[i + 1]; 53 System.arraycopy(children, 0, c, 0, children.length); 54 children = c; 55 } 56 children[i] = n; 57 } 58 59 public Node jjtGetChild(int i) { 60 return children[i]; 61 } 62 63 public int jjtGetNumChildren() { 64 return (children == null) ? 0 : children.length; 65 } 66 67 72 73 public String toString() { return ToyParserTreeConstants.jjtNodeName[id]; } 74 public String toString(String prefix) { return prefix + toString(); } 75 76 78 79 public void dump(String prefix) { 80 System.out.println(toString(prefix)); 81 if (children != null) { 82 for (int i = 0; i < children.length; ++i) { 83 SimpleNode n = (SimpleNode)children[i]; 84 if (n != null) { 85 n.dump(prefix + " "); 86 } 87 } 88 } 89 } 90 91 93 protected Token begin, end; 94 public void setFirstToken(Token t) { begin = t; } 95 public void setLastToken(Token t) { end = t; } 96 97 public void process (PrintWriter ostr) { 98 System.out.println("Error - this should not be called"); 99 throw new Error (); 100 } 101 102 105 protected void print(Token t, PrintWriter ostr) { 106 Token tt = t.specialToken; 107 if (tt != null) { 108 while (tt.specialToken != null) tt = tt.specialToken; 109 while (tt != null) { 110 ostr.print(addUnicodeEscapes(tt.image)); 111 tt = tt.next; 112 } 113 } 114 ostr.print(addUnicodeEscapes(t.image)); 115 } 116 117 private String addUnicodeEscapes(String str) { 118 String retval = ""; 119 char ch; 120 for (int i = 0; i < str.length(); i++) { 121 ch = str.charAt(i); 122 if ((ch < 0x20 || ch > 0x7e) && ch != '\t' && ch != '\n' && ch != '\r' && ch != '\f') { 123 String s = "0000" + Integer.toString(ch, 16); 124 retval += "\\u" + s.substring(s.length() - 4, s.length()); 125 } else { 126 retval += ch; 127 } 128 } 129 return retval; 130 } 131 } 132 133 | Popular Tags |