1 package antlr.preprocessor;
2
3 * Tester for the preprocessor
5 *
6 */
7 import java.io.*;
8 import antlr.collections.impl.Vector;
9 import java.util.Enumeration;
10
11 public class Tool {
12 protected Hierarchy theHierarchy;
13 protected String grammarFileName;
14 protected String[] args;
15 protected int nargs; protected Vector grammars;
17 protected Toolr.Tool antlrTool;
18 public Tool(Toolr.Tool t, String[] args) {
19 antlrTool = t;
20 processArguments(args);
21 }
22 public static void main(String[] args) {
23 Toolr.Tool aTool = new antlr.Tool();
24 Tool theTool = new Tool(aTool, args);
25 theTool.preprocess();
26 String[] a = theTool.preprocessedArgList();
27 for (int i=0; i<a.length; i++) {
28 System.out.print(" "+a[i]);
29 }
30 System.out.println();
31 }
32 public boolean preprocess() {
33 if ( grammarFileName == null ) {
34 antlr.Tool.toolError("no grammar file specified");
35 return false;
36 }
37 if ( grammars!=null ) {
38 theHierarchy = new Hierarchy();
39 for (Enumeration e=grammars.elements(); e.hasMoreElements(); ) {
40 String f = (String)e.nextElement();
41 try {
42 theHierarchy.readGrammarFile(f);
43 }
44 catch (FileNotFoundException fe) {
45 antlr.Tool.toolError("file "+f+" not found");
46 return false;
47 }
48 }
49 }
50
51
52 boolean complete = theHierarchy.verifyThatHierarchyIsComplete();
54 if ( !complete ) return false;
55 theHierarchy.expandGrammarsInFile(grammarFileName);
56
57 GrammarFile gf = theHierarchy.getFile(grammarFileName);
58 String expandedFileName = gf.nameForExpandedGrammarFile(grammarFileName);
59
60 try {
62 gf.generateExpandedFile(); args[nargs++] = expandedFileName; }
65 catch (IOException io) {
66 antlr.Tool.toolError("cannot write expanded grammar file "+expandedFileName);
67 return false;
68 }
69 return true;
70 }
71
72 public String[] preprocessedArgList() {
73 String[] a = new String[nargs];
74 System.arraycopy(args, 0, a, 0, nargs);
75 args = a;
76 return args;
77 }
78 * that does not contain the -glib option. The grammar file name
80 * might be modified and, hence, is not added yet to args list.
81 */
82 private void processArguments(String[] incomingArgs)
83 {
84 this.nargs = 0;
85 this.args = new String[incomingArgs.length];
86 for (int i=0; i<incomingArgs.length; i++) {
87 if ( incomingArgs[i].equals("-glib") ) {
88 grammars = antlr.Tool.parseSeparatedList(incomingArgs[i+1],';');
89 i++;
90 }
91 else if ( incomingArgs[i].equals("-o") ) {
92 args[this.nargs++] = incomingArgs[i];
93 if (i + 1 >= incomingArgs.length) {
94 antlrTool.error("missing output directory with -o option; ignoring");
95 } else {
96 i++;
97 args[this.nargs++] = incomingArgs[i];
98 antlrTool.setOutputDirectory(incomingArgs[i]);
99 }
100 }
101 else if (incomingArgs[i].charAt(0) == '-') {
102 args[this.nargs++] = incomingArgs[i];
103 }
104 else {
105 grammarFileName = incomingArgs[i];
107 if ( grammars==null ) {
108 grammars = new Vector(10);
109 }
110 grammars.appendElement(grammarFileName); }
112 }
113 }
114 }
115