1 package antlr.preprocessor; 2 3 9 10 import java.io.*; 11 import antlr.collections.impl.Vector; 12 import java.util.Enumeration ; 13 14 15 public class Tool { 16 protected Hierarchy theHierarchy; 17 protected String grammarFileName; 18 protected String [] args; 19 protected int nargs; protected Vector grammars; 21 protected antlr.Tool antlrTool; 22 23 public Tool(antlr.Tool t, String [] args) { 24 antlrTool = t; 25 processArguments(args); 26 } 27 28 public static void main(String [] args) { 29 antlr.Tool antlrTool = new antlr.Tool(); 30 Tool theTool = new Tool(antlrTool, args); 31 theTool.preprocess(); 32 String [] a = theTool.preprocessedArgList(); 33 for (int i = 0; i < a.length; i++) { 34 System.out.print(" " + a[i]); 35 } 36 System.out.println(); 37 } 38 39 public boolean preprocess() { 40 if (grammarFileName == null) { 41 antlrTool.toolError("no grammar file specified"); 42 return false; 43 } 44 if (grammars != null) { 45 theHierarchy = new Hierarchy(antlrTool); 46 for (Enumeration e = grammars.elements(); e.hasMoreElements();) { 47 String f = (String )e.nextElement(); 48 try { 49 theHierarchy.readGrammarFile(f); 50 } 51 catch (FileNotFoundException fe) { 52 antlrTool.toolError("file " + f + " not found"); 53 return false; 54 } 55 } 56 } 57 58 59 boolean complete = theHierarchy.verifyThatHierarchyIsComplete(); 61 if (!complete) 62 return false; 63 theHierarchy.expandGrammarsInFile(grammarFileName); 64 GrammarFile gf = theHierarchy.getFile(grammarFileName); 65 String expandedFileName = gf.nameForExpandedGrammarFile(grammarFileName); 66 67 if (expandedFileName.equals(grammarFileName)) { 69 args[nargs++] = grammarFileName; } 71 else { 72 try { 73 gf.generateExpandedFile(); args[nargs++] = antlrTool.getOutputDirectory() + 75 System.getProperty("file.separator") + 76 expandedFileName; } 78 catch (IOException io) { 79 antlrTool.toolError("cannot write expanded grammar file " + expandedFileName); 80 return false; 81 } 82 } 83 return true; 84 } 85 86 87 public String [] preprocessedArgList() { 88 String [] a = new String [nargs]; 89 System.arraycopy(args, 0, a, 0, nargs); 90 args = a; 91 return args; 92 } 93 94 98 private void processArguments(String [] incomingArgs) { 99 this.nargs = 0; 100 this.args = new String [incomingArgs.length]; 101 for (int i = 0; i < incomingArgs.length; i++) { 102 if (incomingArgs[i].equals("-glib")) { 103 if (File.separator.equals("\\") && 105 incomingArgs[i].indexOf('/') != -1) { 106 antlrTool.warning("-glib cannot deal with '/' on a PC: use '\\'; ignoring..."); 107 } 108 else { 109 grammars = antlrTool.parseSeparatedList(incomingArgs[i + 1], ';'); 110 i++; 111 } 112 } 113 else if (incomingArgs[i].equals("-o")) { 114 args[this.nargs++] = incomingArgs[i]; 115 if (i + 1 >= incomingArgs.length) { 116 antlrTool.error("missing output directory with -o option; ignoring"); 117 } 118 else { 119 i++; 120 args[this.nargs++] = incomingArgs[i]; 121 antlrTool.setOutputDirectory(incomingArgs[i]); 122 } 123 } 124 else if (incomingArgs[i].charAt(0) == '-') { 125 args[this.nargs++] = incomingArgs[i]; 126 } 127 else { 128 grammarFileName = incomingArgs[i]; 130 if (grammars == null) { 131 grammars = new Vector(10); 132 } 133 grammars.appendElement(grammarFileName); if ((i + 1) < incomingArgs.length) { 135 antlrTool.warning("grammar file must be last; ignoring other arguments..."); 136 break; 137 } 138 } 139 } 140 } 141 } 142 | Popular Tags |