1 9 10 15 16 19 20 package jasmin; 21 22 import jas.*; 23 import java_cup.runtime.*; 24 import java.util.*; 25 import java.io.InputStream ; 26 27 class Scanner implements java_cup.runtime.Scanner { 28 InputStream inp; 29 30 int next_char; 32 33 char chars[]; 35 char secondChars[]; 36 char[] unicodeBuffer; 37 38 boolean is_first_sep; 44 45 static final String WHITESPACE = " \n\t\r"; 47 48 static final String SEPARATORS = WHITESPACE + ":="; 50 51 52 public int line_num, char_num, token_line_num; 54 public StringBuffer line; 55 56 public Hashtable dict = new Hashtable(); 58 59 protected static boolean whitespace(int c) { 63 return (WHITESPACE.indexOf(c) != -1); 64 } 65 66 protected static boolean separator(int c) { 70 return (SEPARATORS.indexOf(c) != -1); 71 } 72 73 74 protected void advance() throws java.io.IOException 78 { 79 next_char = inp.read(); 80 if (next_char == '\n') { 81 line_num++; 83 char_num = 0; 84 line.setLength(0); 85 } else { 86 line.append((char)next_char); 87 char_num++; 88 } 89 } 90 91 final static int BIGNUM=65000; 95 public Scanner(InputStream i) throws java.io.IOException 96 { 97 inp = i; 98 line_num = 1; 99 char_num = 0; 100 line = new StringBuffer (); 101 chars = new char[BIGNUM]; 102 secondChars = new char[BIGNUM]; 103 unicodeBuffer = new char[4]; 104 is_first_sep = true; 105 advance(); 106 } 107 108 int readOctal(int firstChar) throws java.io.IOException { 109 int d1, d2, d3; 110 d1 = firstChar; 111 advance(); 112 d2 = next_char; 113 advance(); 114 d3 = next_char; 115 return ((d1-'0')&7) * 64 + ((d2-'0')&7) * 8 + ((d3-'0')&7); 116 } 117 118 public Symbol next_token() 122 throws java.io.IOException , jasError 123 { 124 125 token_line_num = line_num; 126 127 for (;;) { 128 switch (next_char) { 129 130 case ';': 131 do { advance(); } while (next_char != '\n'); 133 134 case '\n': 135 for (;;) { 138 do { advance(); } while (whitespace(next_char)); 139 if (next_char == ';') { 140 do { advance(); } while (next_char != '\n'); 141 } else { 142 break; 143 } 144 } 145 if (is_first_sep) { 146 return next_token(); 147 } 148 token_line_num = line_num; 149 return new Symbol(sym.SEP); 150 151 case '0': case '1': case '2': case '3': case '4': 152 case '5': case '6': case '7': case '8': case '9': 153 case '-': case '+': 154 case '.': { 156 int pos = 0; 157 158 is_first_sep = false; 160 161 chars[0] = (char)next_char; 162 pos++; 163 for (;;) { 164 advance(); 165 if (separator(next_char)) { 166 break; 167 } 168 try { 169 chars[pos] = (char)next_char; 170 } catch (ArrayIndexOutOfBoundsException abe) { 171 char[] tmparray = new char[chars.length*2]; 172 System.arraycopy(chars, 0, 173 tmparray, 0, 174 chars.length); 175 chars = tmparray; 176 chars[pos] = (char)next_char; 177 } 178 pos++; 179 } 180 String str = new String (chars, 0, pos); 181 Symbol tok; 182 183 if(str.equals("+DoubleInfinity")) 184 return new Symbol(sym.Num, new Double (1.0/0.0)); 185 186 if(str.equals("+DoubleNaN")) 187 return new Symbol(sym.Num, new Double (0.0d/0.0)); 188 189 if(str.equals("+FloatNaN")) 190 return new Symbol(sym.Num, new Float (0.0f/0.0)); 191 192 if(str.equals("-DoubleInfinity")) 193 return new Symbol(sym.Num, new Double (-1.0/0.0)); 194 195 if(str.equals("+FloatInfinity")) 196 return new Symbol(sym.Num, new Float (1.0f/0.0f)); 197 198 if(str.equals("-FloatInfinity")) 199 return new Symbol(sym.Num, new Float (-1.0f/0.0f)); 200 201 202 203 if ((tok = ReservedWords.get(str)) != null) { 205 return tok; 206 } 207 208 Number num; 209 try { 210 num = ScannerUtils.convertNumber(str); 211 } catch (NumberFormatException e) { 212 if (chars[0] == '.') { 213 throw new jasError("Unknown directive or badly formed number."); 214 } else { 215 throw new jasError("Badly formatted number"); 216 } 217 } 218 219 if (num instanceof Integer ) { 220 return new Symbol(sym.Int, new Integer (num.intValue())); 221 } else { 222 return new Symbol(sym.Num, num); 223 } 224 } 225 226 case '"': { 228 int pos = 0; 229 230 is_first_sep = false; 231 232 for (;;) { 233 advance(); 234 if (next_char == '\\') { 235 advance(); 236 switch (next_char) { 237 case 'n': next_char = '\n'; break; 238 case 'r': next_char = '\r'; break; 239 case 't': next_char = '\t'; break; 240 case 'f': next_char = '\f'; break; 241 case 'b': next_char = '\b'; break; 242 case 'u': 243 { 244 advance(); 245 unicodeBuffer[0] = (char) next_char; 246 advance(); 247 unicodeBuffer[1] = (char) next_char; 248 advance(); 249 unicodeBuffer[2] = (char) next_char; 250 advance(); 251 unicodeBuffer[3] = (char) next_char; 252 253 255 next_char = (char) Integer.parseInt(new String (unicodeBuffer, 0, 4), 16); 256 break; 258 } 259 case '"': next_char = '"'; break; 260 case '\'': next_char = '\''; break; 261 case '\\': next_char = '\\'; break; 262 263 case '0': case '1': case '2': case '3': case '4': 264 case '5': case '6': case '7': 265 next_char = readOctal(next_char); 266 break; 267 default: 268 throw new jasError("Bad backslash escape sequence"); 269 } 270 } else if (next_char == '"') { 271 break; 272 } 273 274 try { 275 chars[pos] = (char)next_char; 276 } catch (ArrayIndexOutOfBoundsException abe) { 277 char[] tmparray = new char[chars.length*2]; 278 System.arraycopy(chars, 0, 279 tmparray, 0, 280 chars.length); 281 chars = tmparray; 282 chars[pos] = (char)next_char; 283 } 284 pos++; 285 } 286 advance(); return new Symbol(sym.Str, new String (chars, 0, pos)); 288 } 289 290 case ' ': 291 case '\t': 292 case '\r': advance(); 294 break; 295 296 case '=': advance(); 298 is_first_sep = false; 299 return new Symbol(sym.EQ); 300 301 case ':': advance(); 303 is_first_sep = false; 304 return new Symbol(sym.COLON); 305 306 case -1: is_first_sep = false; 308 char_num = -1; 309 line.setLength(0); 310 return new Symbol(sym.EOF); 311 312 default: 313 { 314 316 int pos = 0; 317 int secondPos = 0; 318 chars[0] = (char)next_char; 319 is_first_sep = false; 320 321 pos++; 322 for (;;) { 323 advance(); 324 if (separator(next_char)) { 325 break; 326 } 327 try { 328 chars[pos] = (char)next_char; 329 } catch (ArrayIndexOutOfBoundsException abe) { 330 char[] tmparray = new char[chars.length*2]; 331 System.arraycopy(chars, 0, 332 tmparray, 0, 333 chars.length); 334 chars = tmparray; 335 chars[pos] = (char)next_char; 336 } 337 pos++; 338 } 339 340 secondPos = 0; 341 342 for(int i = 0; i < pos; i++) 344 { 345 if(chars[i] == '\\' && (i + 5) < pos && 346 chars[i+1] == 'u') { 347 int intValue = 348 Integer.parseInt(new String (chars, i+2, 4), 16); 349 350 try { 351 secondChars[secondPos] = (char) intValue; 352 } catch (ArrayIndexOutOfBoundsException abe) { 353 char[] tmparray = 354 new char[secondChars.length*2]; 355 System.arraycopy(secondChars, 0, 356 tmparray, 0, 357 secondChars.length); 358 secondChars = tmparray; 359 secondChars[secondPos] = (char)intValue; 360 } 361 secondPos++; 362 363 i += 5; 364 } else { 365 try { 366 secondChars[secondPos] = chars[i]; 367 } catch (ArrayIndexOutOfBoundsException abe) { 368 char[] tmparray = 369 new char[secondChars.length*2]; 370 System.arraycopy(secondChars, 0, 371 tmparray, 0, 372 secondChars.length); 373 secondChars = tmparray; 374 secondChars[secondPos] = chars[i]; 375 } 376 secondPos++; 377 } 378 } 379 380 String str = new String (secondChars, 0, secondPos); 382 383 Symbol tok; 384 if ((tok = ReservedWords.get(str)) != null) { 385 return tok; 387 } else if (InsnInfo.contains(str)) { 388 return new Symbol(sym.Insn, str); 390 } else { 397 return new Symbol(sym.Word, str); 399 } 400 401 } 402 } 403 } 404 } 405 406 }; 407 408 420 | Popular Tags |