1 22 23 package org.javacc.parser; 24 25 import java.util.Vector ; 26 import java.io.*; 27 import org.javacc.Version; 28 29 34 public class JavaCCGlobals { 35 36 39 protected static final String toolName = "JavaCC"; 40 41 44 static public String fileName; 45 46 50 static public String origFileName; 51 52 55 static public boolean jjtreeGenerated; 56 57 60 static public boolean jjcovGenerated; 61 62 66 static public Vector toolNames; 67 68 72 static public void bannerLine(String fullName, String ver) { 73 System.out.print("Java Compiler Compiler Version " + Version.version + " (" + fullName); 74 if (!ver.equals("")) { 75 System.out.print(" Version " + ver); 76 } 77 System.out.println(")"); 78 } 80 81 84 static public String cu_name; 85 86 90 static public java.util.Vector cu_to_insertion_point_1 = new java.util.Vector (); 91 92 97 static public java.util.Vector cu_to_insertion_point_2 = new java.util.Vector (); 98 99 103 static public java.util.Vector cu_from_insertion_point_2 = new java.util.Vector (); 104 105 110 static public java.util.Vector bnfproductions = new java.util.Vector (); 111 112 117 static public java.util.Hashtable production_table = new java.util.Hashtable (); 118 119 123 static public java.util.Hashtable lexstate_S2I = new java.util.Hashtable (); 124 125 129 static public java.util.Hashtable lexstate_I2S = new java.util.Hashtable (); 130 131 134 static public java.util.Vector token_mgr_decls; 135 136 141 static public java.util.Vector rexprlist = new java.util.Vector (); 142 143 147 static public int tokenCount; 148 149 154 static public java.util.Hashtable named_tokens_table = new java.util.Hashtable (); 155 156 160 static public java.util.Vector ordered_named_tokens = new java.util.Vector (); 161 162 169 static public java.util.Hashtable names_of_tokens = new java.util.Hashtable (); 170 171 175 static public java.util.Hashtable rexps_of_tokens = new java.util.Hashtable (); 176 177 186 static public java.util.Hashtable simple_tokens_table = new java.util.Hashtable (); 187 188 192 static protected int maskindex = 0; 193 static protected int jj2index = 0; 194 static protected Vector maskVals = new Vector (); 195 196 static Action actForEof; 197 static String nextStateForEof; 198 199 200 202 206 public static String getIdString(String toolName, String fileName) { 207 Vector toolNames = new Vector (); 208 toolNames.addElement(toolName); 209 return getIdString(toolNames, fileName); 210 } 211 212 216 public static String getIdString(Vector toolNames, String fileName) { 217 int i; 218 String toolNamePrefix = "Generated By:"; 219 220 for (i = 0; i < toolNames.size() - 1; i++) 221 toolNamePrefix += (String )toolNames.elementAt(i) + "&"; 222 toolNamePrefix += (String )toolNames.elementAt(i) + ":"; 223 224 if (toolNamePrefix.length() > 200) 225 { 226 System.out.println("Tool names too long."); 227 throw new Error (); 228 } 229 230 return toolNamePrefix + " Do not edit this line. " + addUnicodeEscapes(fileName); 231 } 232 233 237 public static boolean isGeneratedBy(String toolName, String fileName) { 238 Vector v = getToolNames(fileName); 239 240 for (int i = 0; i < v.size(); i++) 241 if (toolName.equals(v.elementAt(i))) 242 return true; 243 244 return false; 245 } 246 247 private static Vector makeToolNameVector(String str) { 248 Vector retVal = new Vector (); 249 250 int limit1 = str.indexOf('\n'); 251 if (limit1 == -1) limit1 = 1000; 252 int limit2 = str.indexOf('\r'); 253 if (limit2 == -1) limit2 = 1000; 254 int limit = (limit1 < limit2) ? limit1 : limit2; 255 256 String tmp; 257 if (limit == 1000) { 258 tmp = str; 259 } else { 260 tmp = str.substring(0, limit); 261 } 262 263 if (tmp.indexOf(':') == -1) 264 return retVal; 265 266 tmp = tmp.substring(tmp.indexOf(':') + 1); 267 268 if (tmp.indexOf(':') == -1) 269 return retVal; 270 271 tmp = tmp.substring(0, tmp.indexOf(':')); 272 273 int i = 0, j = 0; 274 275 while (j < tmp.length() && (i = tmp.indexOf('&', j)) != -1) 276 { 277 retVal.addElement(tmp.substring(j, i)); 278 j = i + 1; 279 } 280 281 if (j < tmp.length()) 282 retVal.addElement(tmp.substring(j)); 283 284 return retVal; 285 } 286 287 291 public static Vector getToolNames(String fileName) { 292 char[] buf = new char[256]; 293 java.io.FileReader stream = null; 294 int read, total = 0; 295 296 try { 297 stream = new java.io.FileReader (fileName); 298 299 for (;;) 300 if ((read = stream.read(buf, total, buf.length - total)) != -1) 301 { 302 if ((total += read) == buf.length) 303 break; 304 } 305 else 306 break; 307 308 return makeToolNameVector(new String (buf, 0, total)); 309 } catch(java.io.FileNotFoundException e1) { 310 } catch(java.io.IOException e2) { 311 if (total > 0) 312 return makeToolNameVector(new String (buf, 0, total)); 313 } 314 finally { 315 try { stream.close(); } 316 catch (Exception e3) { } 317 } 318 319 return new Vector (); 320 } 321 322 public static void createOutputDir(File outputDir) { 323 if (!outputDir.exists()) { 324 JavaCCErrors.warning("Output directory \"" + outputDir + "\" does not exist. Creating the directory."); 325 326 if (!outputDir.mkdirs()) { 327 JavaCCErrors.semantic_error("Cannot create the output directory : " + outputDir); 328 return; 329 } 330 } 331 332 if (!outputDir.isDirectory()) { 333 JavaCCErrors.semantic_error("\"" + outputDir + " is not a valid output directory."); 334 return; 335 } 336 337 if (!outputDir.canWrite()) { 338 JavaCCErrors.semantic_error("Cannot write to the output output directory : \"" + outputDir + "\""); 339 return; 340 } 341 } 342 343 static public String staticOpt() { 344 if (Options.getStatic()) { 345 return "static "; 346 } else { 347 return ""; 348 } 349 } 350 351 static public String add_escapes(String str) { 352 String retval = ""; 353 char ch; 354 for (int i = 0; i < str.length(); i++) { 355 ch = str.charAt(i); 356 if (ch == '\b') { 357 retval += "\\b"; 358 } else if (ch == '\t') { 359 retval += "\\t"; 360 } else if (ch == '\n') { 361 retval += "\\n"; 362 } else if (ch == '\f') { 363 retval += "\\f"; 364 } else if (ch == '\r') { 365 retval += "\\r"; 366 } else if (ch == '\"') { 367 retval += "\\\""; 368 } else if (ch == '\'') { 369 retval += "\\\'"; 370 } else if (ch == '\\') { 371 retval += "\\\\"; 372 } else if (ch < 0x20 || ch > 0x7e) { 373 String s = "0000" + Integer.toString(ch, 16); 374 retval += "\\u" + s.substring(s.length() - 4, s.length()); 375 } else { 376 retval += ch; 377 } 378 } 379 return retval; 380 } 381 382 static public String addUnicodeEscapes(String str) { 383 String retval = ""; 384 char ch; 385 for (int i = 0; i < str.length(); i++) { 386 ch = str.charAt(i); 387 if (ch < 0x20 || ch > 0x7e) { 388 String s = "0000" + Integer.toString(ch, 16); 389 retval += "\\u" + s.substring(s.length() - 4, s.length()); 390 } else { 391 retval += ch; 392 } 393 } 394 return retval; 395 } 396 397 static protected int cline, ccol; 398 399 static protected void printTokenSetup(Token t) { 400 Token tt = t; 401 while (tt.specialToken != null) tt = tt.specialToken; 402 cline = tt.beginLine; 403 ccol = tt.beginColumn; 404 } 405 406 static protected void printTokenOnly(Token t, java.io.PrintWriter ostr) { 407 for (; cline < t.beginLine; cline++) { 408 ostr.println(""); ccol = 1; 409 } 410 for (; ccol < t.beginColumn; ccol++) { 411 ostr.print(" "); 412 } 413 if (t.kind == JavaCCParserConstants.STRING_LITERAL || 414 t.kind == JavaCCParserConstants.CHARACTER_LITERAL) 415 ostr.print(addUnicodeEscapes(t.image)); 416 else 417 ostr.print(t.image); 418 cline = t.endLine; 419 ccol = t.endColumn+1; 420 char last = t.image.charAt(t.image.length()-1); 421 if (last == '\n' || last == '\r') { 422 cline++; ccol = 1; 423 } 424 } 425 426 static protected void printToken(Token t, java.io.PrintWriter ostr) { 427 Token tt = t.specialToken; 428 if (tt != null) { 429 while (tt.specialToken != null) tt = tt.specialToken; 430 while (tt != null) { 431 printTokenOnly(tt, ostr); 432 tt = tt.next; 433 } 434 } 435 printTokenOnly(t, ostr); 436 } 437 438 static protected void printLeadingComments(Token t, java.io.PrintWriter ostr) { 439 if (t.specialToken == null) return; 440 Token tt = t.specialToken; 441 while (tt.specialToken != null) tt = tt.specialToken; 442 while (tt != null) { 443 printTokenOnly(tt, ostr); 444 tt = tt.next; 445 } 446 if (ccol != 1 && cline != t.beginLine) { 447 ostr.println(""); 448 cline++; ccol = 1; 449 } 450 } 451 452 static protected void printTrailingComments(Token t, java.io.PrintWriter ostr) { 453 if (t.next == null) return; 454 printLeadingComments(t.next); 455 } 456 457 static protected String printTokenOnly(Token t) { 458 String retval = ""; 459 for (; cline < t.beginLine; cline++) { 460 retval += "\n"; ccol = 1; 461 } 462 for (; ccol < t.beginColumn; ccol++) { 463 retval += " "; 464 } 465 if (t.kind == JavaCCParserConstants.STRING_LITERAL || 466 t.kind == JavaCCParserConstants.CHARACTER_LITERAL) 467 retval += addUnicodeEscapes(t.image); 468 else 469 retval += t.image; 470 cline = t.endLine; 471 ccol = t.endColumn+1; 472 char last = t.image.charAt(t.image.length()-1); 473 if (last == '\n' || last == '\r') { 474 cline++; ccol = 1; 475 } 476 return retval; 477 } 478 479 static protected String printToken(Token t) { 480 String retval = ""; 481 Token tt = t.specialToken; 482 if (tt != null) { 483 while (tt.specialToken != null) tt = tt.specialToken; 484 while (tt != null) { 485 retval += printTokenOnly(tt); 486 tt = tt.next; 487 } 488 } 489 retval += printTokenOnly(t); 490 return retval; 491 } 492 493 static protected String printLeadingComments(Token t) { 494 String retval = ""; 495 if (t.specialToken == null) return retval; 496 Token tt = t.specialToken; 497 while (tt.specialToken != null) tt = tt.specialToken; 498 while (tt != null) { 499 retval += printTokenOnly(tt); 500 tt = tt.next; 501 } 502 if (ccol != 1 && cline != t.beginLine) { 503 retval += "\n"; 504 cline++; ccol = 1; 505 } 506 return retval; 507 } 508 509 static protected String printTrailingComments(Token t) { 510 if (t.next == null) return ""; 511 return printLeadingComments(t.next); 512 } 513 514 public static void reInit() 515 { 516 fileName = null; 517 origFileName = null; 518 jjtreeGenerated = false; 519 jjcovGenerated = false; 520 toolNames = null; 521 cu_name = null; 522 cu_to_insertion_point_1 = new java.util.Vector (); 523 cu_to_insertion_point_2 = new java.util.Vector (); 524 cu_from_insertion_point_2 = new java.util.Vector (); 525 bnfproductions = new java.util.Vector (); 526 production_table = new java.util.Hashtable (); 527 lexstate_S2I = new java.util.Hashtable (); 528 lexstate_I2S = new java.util.Hashtable (); 529 token_mgr_decls = null; 530 rexprlist = new java.util.Vector (); 531 tokenCount = 0; 532 named_tokens_table = new java.util.Hashtable (); 533 ordered_named_tokens = new java.util.Vector (); 534 names_of_tokens = new java.util.Hashtable (); 535 rexps_of_tokens = new java.util.Hashtable (); 536 simple_tokens_table = new java.util.Hashtable (); 537 maskindex = 0; 538 jj2index = 0; 539 maskVals = new Vector (); 540 cline = 0; 541 ccol = 0; 542 actForEof = null; 543 nextStateForEof = null; 544 } 545 546 } 547 | Popular Tags |