1 package antlr; 2 3 9 10 import java.io.*; 11 12 import antlr.collections.impl.BitSet; 13 import antlr.collections.impl.Vector; 14 import antlr.PreservingFileWriter; 15 import antlr.Version; 16 17 public class Tool { 18 public static String version = ""; 19 20 21 ToolErrorHandler errorHandler; 22 23 24 protected boolean hasError = false; 25 26 27 boolean genDiagnostics = false; 28 29 30 boolean genDocBook = false; 31 32 33 boolean genHTML = false; 34 35 36 protected static String outputDir = "."; 37 38 protected String grammarFile; 40 transient Reader f = new InputStreamReader(System.in); 41 44 protected static String literalsPrefix = "LITERAL_"; 45 protected static boolean upperCaseMangledLiterals = false; 46 47 48 protected NameSpace nameSpace = null; 49 protected String namespaceAntlr = null; 50 protected String namespaceStd = null; 51 protected boolean genHashLines = true; 52 53 private BitSet cmdLineArgValid = new BitSet(); 54 55 56 public Tool() { 57 errorHandler = new DefaultToolErrorHandler(); 58 } 59 60 public String getGrammarFile() { 61 return grammarFile; 62 } 63 64 public boolean hasError() { 65 return hasError; 66 } 67 68 public NameSpace getNameSpace() { 69 return nameSpace; 70 } 71 72 public String getNamespaceStd() { 73 return namespaceStd; 74 } 75 76 public String getNamespaceAntlr() { 77 return namespaceAntlr; 78 } 79 80 public boolean getGenHashLines() { 81 return genHashLines; 82 } 83 84 public String getLiteralsPrefix() { 85 return literalsPrefix; 86 } 87 88 public boolean getUpperCaseMangledLiterals() { 89 return upperCaseMangledLiterals; 90 } 91 92 public void setFileLineFormatter(FileLineFormatter formatter) { 93 FileLineFormatter.setFormatter(formatter); 94 } 95 96 protected void checkForInvalidArguments(String [] args, BitSet cmdLineArgValid) { 97 for (int a = 0; a < args.length; a++) { 99 if (!cmdLineArgValid.member(a)) { 100 warning("invalid command-line argument: " + args[a] + "; ignored"); 101 } 102 } 103 } 104 105 110 public void copyFile(String source_name, String dest_name) 111 throws IOException { 112 File source_file = new File(source_name); 113 File destination_file = new File(dest_name); 114 Reader source = null; 115 Writer destination = null; 116 char[] buffer; 117 int bytes_read; 118 119 try { 120 if (!source_file.exists() || !source_file.isFile()) 123 throw new FileCopyException("FileCopy: no such source file: " + 124 source_name); 125 if (!source_file.canRead()) 126 throw new FileCopyException("FileCopy: source file " + 127 "is unreadable: " + source_name); 128 129 if (destination_file.exists()) { 133 if (destination_file.isFile()) { 134 DataInputStream in = new DataInputStream(System.in); 135 String response; 136 137 if (!destination_file.canWrite()) 138 throw new FileCopyException("FileCopy: destination " + 139 "file is unwriteable: " + dest_name); 140 148 } 149 else { 150 throw new FileCopyException("FileCopy: destination " 151 + "is not a file: " + dest_name); 152 } 153 } 154 else { 155 File parentdir = parent(destination_file); 156 if (!parentdir.exists()) 157 throw new FileCopyException("FileCopy: destination " 158 + "directory doesn't exist: " + dest_name); 159 if (!parentdir.canWrite()) 160 throw new FileCopyException("FileCopy: destination " 161 + "directory is unwriteable: " + dest_name); 162 } 163 164 source = new BufferedReader(new FileReader(source_file)); 167 destination = new BufferedWriter(new FileWriter(destination_file)); 168 169 buffer = new char[1024]; 170 while (true) { 171 bytes_read = source.read(buffer, 0, 1024); 172 if (bytes_read == -1) break; 173 destination.write(buffer, 0, bytes_read); 174 } 175 } 176 finally { 178 if (source != null) { 179 try { 180 source.close(); 181 } 182 catch (IOException e) { 183 ; 184 } 185 } 186 if (destination != null) { 187 try { 188 destination.close(); 189 } 190 catch (IOException e) { 191 ; 192 } 193 } 194 } 195 } 196 197 201 public void doEverythingWrapper(String [] args) { 202 int exitCode = doEverything(args); 203 System.exit(exitCode); 204 } 205 206 209 public int doEverything(String [] args) { 210 212 antlr.preprocessor.Tool preTool = new antlr.preprocessor.Tool(this, args); 215 216 boolean preprocess_ok = preTool.preprocess(); 217 String [] modifiedArgs = preTool.preprocessedArgList(); 218 219 processArguments(modifiedArgs); 221 if (!preprocess_ok) { 222 return 1; 223 } 224 225 f = getGrammarReader(); 226 227 ANTLRLexer lexer = new ANTLRLexer(f); 228 TokenBuffer tokenBuf = new TokenBuffer(lexer); 229 LLkAnalyzer analyzer = new LLkAnalyzer(this); 230 MakeGrammar behavior = new MakeGrammar(this, args, analyzer); 231 232 try { 233 ANTLRParser p = new ANTLRParser(tokenBuf, behavior, this); 234 p.grammar(); 236 if (hasError()) { 237 System.err.println("Exiting due to errors."); 238 return 1; 239 } 240 checkForInvalidArguments(modifiedArgs, cmdLineArgValid); 241 242 CodeGenerator codeGen; 244 245 String codeGenClassName = "antlr." + getLanguage(behavior) + "CodeGenerator"; 248 try { 249 Class codeGenClass = Class.forName(codeGenClassName); 250 codeGen = (CodeGenerator)codeGenClass.newInstance(); 251 codeGen.setBehavior(behavior); 252 codeGen.setAnalyzer(analyzer); 253 codeGen.setTool(this); 254 codeGen.gen(); 255 } 256 catch (ClassNotFoundException cnfe) { 257 panic("Cannot instantiate code-generator: " + codeGenClassName); 258 } 259 catch (InstantiationException ie) { 260 panic("Cannot instantiate code-generator: " + codeGenClassName); 261 } 262 catch (IllegalArgumentException ie) { 263 panic("Cannot instantiate code-generator: " + codeGenClassName); 264 } 265 catch (IllegalAccessException iae) { 266 panic("code-generator class '" + codeGenClassName + "' is not accessible"); 267 } 268 } 269 catch (RecognitionException pe) { 270 System.err.println("Unhandled parser error: " + pe.getMessage()); 271 return 1; 272 } 273 catch (TokenStreamException io) { 274 System.err.println("TokenStreamException: " + io.getMessage()); 275 return 1; 276 } 277 return 0; 278 } 279 280 283 public void error(String s) { 284 hasError = true; 285 System.err.println("error: " + s); 286 } 287 288 294 public void error(String s, String file, int line, int column) { 295 hasError = true; 296 System.err.println(FileLineFormatter.getFormatter(). 297 getFormatString(file, line, column) + s); 298 } 299 300 319 public Object factory(String p) { 320 Class c; 321 Object o = null; 322 try { 323 c = Class.forName(p); o = c.newInstance(); } 326 catch (Exception e) { 327 warning("Can't create an object of type " + p); 331 return null; 332 } 333 return o; 334 } 335 336 public String fileMinusPath(String f) { 337 String separator = System.getProperty("file.separator"); 338 int endOfPath = f.lastIndexOf(separator); 339 if (endOfPath == -1) { 340 return f; } 342 return f.substring(endOfPath + 1); 343 } 344 345 348 public String getLanguage(MakeGrammar behavior) { 349 if (genDiagnostics) { 350 return "Diagnostic"; 351 } 352 if (genHTML) { 353 return "HTML"; 354 } 355 if (genDocBook) { 356 return "DocBook"; 357 } 358 return behavior.language; 359 } 360 361 public String getOutputDirectory() { 362 return outputDir; 363 } 364 365 private static void help() { 366 System.err.println("usage: java antlr.Tool [args] file.g"); 367 System.err.println(" -o outputDir specify output directory where all output generated."); 368 System.err.println(" -glib superGrammar specify location of supergrammar file."); 369 System.err.println(" -debug launch the ParseView debugger upon parser invocation."); 370 System.err.println(" -html generate a html file from your grammar."); 371 System.err.println(" -docbook generate a docbook sgml file from your grammar."); 372 System.err.println(" -diagnostic generate a textfile with diagnostics."); 373 System.err.println(" -trace have all rules call traceIn/traceOut."); 374 System.err.println(" -traceLexer have lexer rules call traceIn/traceOut."); 375 System.err.println(" -traceParser have parser rules call traceIn/traceOut."); 376 System.err.println(" -traceTreeParser have tree parser rules call traceIn/traceOut."); 377 System.err.println(" -h|-help|--help this message"); 378 } 379 380 public static void main(String [] args) { 381 System.err.println("ANTLR Parser Generator Version " + 382 Version.project_version + " 1989-2002 jGuru.com"); 383 version = Version.project_version; 384 385 try { 386 if (args.length == 0) { 387 help(); 388 } 389 Tool theTool = new Tool(); 390 theTool.doEverything(args); 391 theTool = null; 392 } 393 catch (Exception e) { 394 System.err.println(System.getProperty("line.separator") + 395 System.getProperty("line.separator")); 396 System.err.println("#$%%*&@# internal error: " + e.toString()); 397 System.err.println("[complain to nearest government official"); 398 System.err.println(" or send hate-mail to parrt@jguru.com;"); 399 System.err.println(" please send stack trace with report.]" + 400 System.getProperty("line.separator")); 401 e.printStackTrace(); 402 } 403 System.exit(0); 404 } 405 406 409 public PrintWriter openOutputFile(String f) throws IOException { 410 return new PrintWriter(new PreservingFileWriter(outputDir + System.getProperty("file.separator") + f)); 411 } 412 413 public Reader getGrammarReader() { 414 try { 415 if (grammarFile != null) { 416 f = new BufferedReader(new FileReader(grammarFile)); 417 } 418 } 419 catch (IOException e) { 420 panic("Error: cannot open grammar file " + grammarFile); 421 help(); 422 System.exit(1); 423 } 424 return f; 425 } 426 427 428 public void panic() { 429 System.err.println("panic"); 430 System.exit(1); 431 } 432 433 436 public void panic(String s) { 437 System.err.println("panic: " + s); 438 System.exit(1); 439 } 440 441 public File parent(File f) { 445 String dirname = f.getParent(); 446 if (dirname == null) { 447 if (f.isAbsolute()) 448 return new File(File.separator); 449 else 450 return new File(System.getProperty("user.dir")); 451 } 452 return new File(dirname); 453 } 454 455 458 public static Vector parseSeparatedList(String list, char separator) { 459 Vector v = new Vector(10); 460 StringBuffer buf = new StringBuffer (100); 461 int i = 0; 462 while (i < list.length()) { 463 while (i < list.length() && list.charAt(i) != separator) { 464 buf.append(list.charAt(i)); 465 i++; 466 } 467 v.appendElement(buf.toString()); 469 buf.setLength(0); 470 if (i < list.length()) { 472 i++; } 475 } 476 if (v.size() == 0) return null; 477 return v; 478 } 479 480 483 public String pathToFile(String f) { 484 String separator = System.getProperty("file.separator"); 485 int endOfPath = f.lastIndexOf(separator); 486 if (endOfPath == -1) { 487 return "." + System.getProperty("file.separator"); 489 } 490 return f.substring(0, endOfPath + 1); 491 } 492 493 497 protected void processArguments(String [] args) { 498 for (int i = 0; i < args.length; i++) { 499 if (args[i].equals("-diagnostic")) { 500 genDiagnostics = true; 501 genHTML = false; 502 setArgOK(i); 503 } 504 else if (args[i].equals("-o")) { 505 setArgOK(i); 506 if (i + 1 >= args.length) { 507 error("missing output directory with -o option; ignoring"); 508 } 509 else { 510 i++; 511 setOutputDirectory(args[i]); 512 setArgOK(i); 513 } 514 } 515 else if (args[i].equals("-html")) { 516 genHTML = true; 517 genDiagnostics = false; 518 setArgOK(i); 519 } 520 else if (args[i].equals("-docbook")) { 521 genDocBook = true; 522 genDiagnostics = false; 523 setArgOK(i); 524 } 525 else if (args[i].equals("-h") 526 || args[i].equals("-help") 527 || args[i].equals("--help") 528 ) { 529 help(); 530 System.exit(0); 531 } 532 else { 533 if (args[i].charAt(0) != '-') { 534 grammarFile = args[i]; 536 setArgOK(i); 537 } 538 } 539 } 540 } 541 542 public void setArgOK(int i) { 543 cmdLineArgValid.add(i); 544 } 545 546 public void setOutputDirectory(String o) { 547 outputDir = o; 548 } 549 550 553 public void toolError(String s) { 554 System.err.println("error: " + s); 555 } 556 557 560 public void warning(String s) { 561 System.err.println("warning: " + s); 562 } 563 564 570 public void warning(String s, String file, int line, int column) { 571 System.err.println(FileLineFormatter.getFormatter(). 572 getFormatString(file, line, column) + "warning:" + s); 573 } 574 575 580 public void warning(String [] s, String file, int line, int column) { 581 if (s == null || s.length == 0) { 582 panic("bad multi-line message to Tool.warning"); 583 } 584 System.err.println(FileLineFormatter.getFormatter(). 585 getFormatString(file, line, column) + "warning:" + s[0]); 586 for (int i = 1; i < s.length; i++) { 587 System.err.println(FileLineFormatter.getFormatter(). 588 getFormatString(file, line, column) + s[i]); 589 } 590 } 591 592 597 public void setNameSpace(String name) { 598 if (null == nameSpace) 599 nameSpace = new NameSpace(StringUtils.stripFrontBack(name, "\"", "\"")); 600 } 601 } 602 | Popular Tags |