[1] | 1 | package antlr.preprocessor; |
---|
| 2 | |
---|
| 3 | /* ANTLR Translator Generator |
---|
| 4 | * Project led by Terence Parr at http://www.cs.usfca.edu |
---|
| 5 | * Software rights: http://www.antlr.org/license.html |
---|
| 6 | * |
---|
| 7 | * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/preprocessor/Tool.java#2 $ |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | import java.io.*; |
---|
| 11 | import antlr.collections.impl.Vector; |
---|
| 12 | import java.util.Enumeration; |
---|
| 13 | |
---|
| 14 | /** Tester for the preprocessor */ |
---|
| 15 | public class Tool { |
---|
| 16 | protected Hierarchy theHierarchy; |
---|
| 17 | protected String grammarFileName; |
---|
| 18 | protected String[] args; |
---|
| 19 | protected int nargs; // how many args in new args list |
---|
| 20 | 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 | // do the actual inheritance stuff |
---|
| 59 | boolean complete = theHierarchy.verifyThatHierarchyIsComplete(); |
---|
| 60 | if (!complete) |
---|
| 61 | return false; |
---|
| 62 | theHierarchy.expandGrammarsInFile(grammarFileName); |
---|
| 63 | GrammarFile gf = theHierarchy.getFile(grammarFileName); |
---|
| 64 | String expandedFileName = gf.nameForExpandedGrammarFile(grammarFileName); |
---|
| 65 | |
---|
| 66 | // generate the output file if necessary |
---|
| 67 | if (expandedFileName.equals(grammarFileName)) { |
---|
| 68 | args[nargs++] = grammarFileName; // add to argument list |
---|
| 69 | } |
---|
| 70 | else { |
---|
| 71 | try { |
---|
| 72 | gf.generateExpandedFile(); // generate file to feed ANTLR |
---|
| 73 | args[nargs++] = antlrTool.getOutputDirectory() + |
---|
| 74 | System.getProperty("file.separator") + |
---|
| 75 | expandedFileName; // add to argument list |
---|
| 76 | } |
---|
| 77 | catch (IOException io) { |
---|
| 78 | antlrTool.toolError("cannot write expanded grammar file " + expandedFileName); |
---|
| 79 | return false; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | return true; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** create new arg list with correct length to pass to ANTLR */ |
---|
| 86 | public String[] preprocessedArgList() { |
---|
| 87 | String[] a = new String[nargs]; |
---|
| 88 | System.arraycopy(args, 0, a, 0, nargs); |
---|
| 89 | args = a; |
---|
| 90 | return args; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** Process -glib options and grammar file. Create a new args list |
---|
| 94 | * that does not contain the -glib option. The grammar file name |
---|
| 95 | * might be modified and, hence, is not added yet to args list. |
---|
| 96 | */ |
---|
| 97 | private void processArguments(String[] incomingArgs) { |
---|
| 98 | this.nargs = 0; |
---|
| 99 | this.args = new String[incomingArgs.length]; |
---|
| 100 | for (int i = 0; i < incomingArgs.length; i++) { |
---|
| 101 | if ( incomingArgs[i].length() == 0 ) |
---|
| 102 | { |
---|
| 103 | antlrTool.warning("Zero length argument ignoring..."); |
---|
| 104 | continue; |
---|
| 105 | } |
---|
| 106 | if (incomingArgs[i].equals("-glib")) { |
---|
| 107 | // if on a pc and they use a '/', warn them |
---|
| 108 | if (File.separator.equals("\\") && |
---|
| 109 | incomingArgs[i].indexOf('/') != -1) { |
---|
| 110 | antlrTool.warning("-glib cannot deal with '/' on a PC: use '\\'; ignoring..."); |
---|
| 111 | } |
---|
| 112 | else { |
---|
| 113 | grammars = antlrTool.parseSeparatedList(incomingArgs[i + 1], ';'); |
---|
| 114 | i++; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | else if (incomingArgs[i].equals("-o")) { |
---|
| 118 | args[this.nargs++] = incomingArgs[i]; |
---|
| 119 | if (i + 1 >= incomingArgs.length) { |
---|
| 120 | antlrTool.error("missing output directory with -o option; ignoring"); |
---|
| 121 | } |
---|
| 122 | else { |
---|
| 123 | i++; |
---|
| 124 | args[this.nargs++] = incomingArgs[i]; |
---|
| 125 | antlrTool.setOutputDirectory(incomingArgs[i]); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | else if (incomingArgs[i].charAt(0) == '-') { |
---|
| 129 | args[this.nargs++] = incomingArgs[i]; |
---|
| 130 | } |
---|
| 131 | else { |
---|
| 132 | // Must be the grammar file |
---|
| 133 | grammarFileName = incomingArgs[i]; |
---|
| 134 | if (grammars == null) { |
---|
| 135 | grammars = new Vector(10); |
---|
| 136 | } |
---|
| 137 | grammars.appendElement(grammarFileName); // process it too |
---|
| 138 | if ((i + 1) < incomingArgs.length) { |
---|
| 139 | antlrTool.warning("grammar file must be last; ignoring other arguments..."); |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|