1 20 21 package JFlex; 22 23 24 38 public abstract class PackEmitter { 39 40 41 protected String name; 42 43 44 private int UTF8Length; 45 46 47 private int linepos; 48 49 50 private static final int maxEntries = 16; 51 52 53 protected StringBuffer out = new StringBuffer (); 54 55 56 protected int chunks; 57 58 59 private static final int maxSize = 0xFFFF-6; 63 64 65 private static final String indent = " "; 66 67 72 public PackEmitter(String name) { 73 this.name = name; 74 } 75 76 83 protected String constName() { 84 return "ZZ_" + name.toUpperCase(); 85 } 86 87 90 public String toString() { 91 return out.toString(); 92 } 93 94 97 public void emitInit() { 98 out.append(" private static final int [] "); 99 out.append(constName()); 100 out.append(" = zzUnpack"); 101 out.append(name); 102 out.append("();"); 103 nl(); 104 nextChunk(); 105 } 106 107 115 public void emitUC(int i) { 116 if (i < 0 || i > 0xFFFF) 117 throw new IllegalArgumentException ("character value expected"); 118 119 char c = (char) i; 121 122 printUC(c); 123 UTF8Length += UTF8Length(c); 124 linepos++; 125 } 126 127 131 public void breaks() { 132 if (UTF8Length >= maxSize) { 133 out.append("\";"); 135 nl(); 136 137 nextChunk(); 138 } 139 else { 140 if (linepos >= maxEntries) { 141 out.append("\"+"); 143 nl(); 144 out.append(indent); 145 out.append("\""); 146 linepos = 0; 147 } 148 } 149 } 150 151 154 public abstract void emitUnpack(); 155 156 159 private void nextChunk() { 160 nl(); 161 out.append(" private static final String "); 162 out.append(constName()); 163 out.append("_PACKED_"); 164 out.append(chunks); 165 out.append(" ="); 166 nl(); 167 out.append(indent); 168 out.append("\""); 169 170 UTF8Length = 0; 171 linepos = 0; 172 chunks++; 173 } 174 175 178 protected void nl() { 179 out.append(Out.NL); 180 } 181 182 188 private void printUC(char c) { 189 if (c > 255) { 190 out.append("\\u"); 191 if (c < 0x1000) out.append("0"); 192 out.append(Integer.toHexString(c)); 193 } 194 else { 195 out.append("\\"); 196 out.append(Integer.toOctalString(c)); 197 } 198 } 199 200 209 private int UTF8Length(char value) { 210 212 if (value == 0) return 2; 214 if (value <= 0x7F) return 1; 215 216 if (value < 0x0400) return 2; 218 if (value <= 0x07FF) return 3; 219 220 return 3; 223 } 224 225 protected void println(String s) { 227 out.append(s); 228 nl(); 229 } 230 } 231 | Popular Tags |