KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > preprocessor > Tool


1 package 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/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/preprocessor/Tool.java#5 $
8  */

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

98     private void processArguments(String JavaDoc[] incomingArgs) {
99         this.nargs = 0;
100         this.args = new String JavaDoc[incomingArgs.length];
101         for (int i = 0; i < incomingArgs.length; i++) {
102             if (incomingArgs[i].equals("-glib")) {
103                 // if on a pc and they use a '/', warn them
104
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                 // Must be the grammar file
129
grammarFileName = incomingArgs[i];
130                 if (grammars == null) {
131                     grammars = new Vector(10);
132                 }
133                 grammars.appendElement(grammarFileName); // process it too
134
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