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