KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > preprocessor > Tool


1 package persistence.antlr.preprocessor;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.io.*;
10 import persistence.antlr.collections.impl.Vector;
11 import java.util.Enumeration JavaDoc;
12
13 /** Tester for the preprocessor */
14 public class Tool {
15     protected Hierarchy theHierarchy;
16     protected String JavaDoc grammarFileName;
17     protected String JavaDoc[] args;
18     protected int nargs; // how many args in new args list
19
protected Vector grammars;
20     protected persistence.antlr.Tool antlrTool;
21
22     public Tool(persistence.antlr.Tool t, String JavaDoc[] args) {
23         antlrTool = t;
24         processArguments(args);
25     }
26
27     public static void main(String JavaDoc[] args) {
28         persistence.antlr.Tool antlrTool = new persistence.antlr.Tool();
29         Tool theTool = new Tool(antlrTool, args);
30         theTool.preprocess();
31         String JavaDoc[] 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 JavaDoc e = grammars.elements(); e.hasMoreElements();) {
46                 String JavaDoc f = (String JavaDoc)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         // do the actual inheritance stuff
58
boolean complete = theHierarchy.verifyThatHierarchyIsComplete();
59         if (!complete)
60             return false;
61         theHierarchy.expandGrammarsInFile(grammarFileName);
62         GrammarFile gf = theHierarchy.getFile(grammarFileName);
63         String JavaDoc expandedFileName = gf.nameForExpandedGrammarFile(grammarFileName);
64
65         // generate the output file if necessary
66
if (expandedFileName.equals(grammarFileName)) {
67             args[nargs++] = grammarFileName; // add to argument list
68
}
69         else {
70             try {
71                 gf.generateExpandedFile(); // generate file to feed ANTLR
72
args[nargs++] = antlrTool.getOutputDirectory() +
73                     System.getProperty("file.separator") +
74                     expandedFileName; // add to argument list
75
}
76             catch (IOException io) {
77                 antlrTool.toolError("cannot write expanded grammar file " + expandedFileName);
78                 return false;
79             }
80         }
81         return true;
82     }
83
84     /** create new arg list with correct length to pass to ANTLR */
85     public String JavaDoc[] preprocessedArgList() {
86         String JavaDoc[] a = new String JavaDoc[nargs];
87         System.arraycopy(args, 0, a, 0, nargs);
88         args = a;
89         return args;
90     }
91
92     /** Process -glib options and grammar file. Create a new args list
93      * that does not contain the -glib option. The grammar file name
94      * might be modified and, hence, is not added yet to args list.
95      */

96     private void processArguments(String JavaDoc[] incomingArgs) {
97          this.nargs = 0;
98          this.args = new String JavaDoc[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 on a pc and they use a '/', warn them
107
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                  // Must be the grammar file
132
grammarFileName = incomingArgs[i];
133                  if (grammars == null) {
134                      grammars = new Vector(10);
135                  }
136                  grammars.appendElement(grammarFileName); // process it too
137
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