001: package antlr.preprocessor;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import java.io.*;
009: import antlr.collections.impl.Vector;
010: import java.util.Enumeration;
011:
012: /** Tester for the preprocessor */
013: public class Tool {
014: protected Hierarchy theHierarchy;
015: protected String grammarFileName;
016: protected String[] args;
017: protected int nargs; // how many args in new args list
018: protected Vector grammars;
019: protected antlr.Tool antlrTool;
020:
021: public Tool(antlr.Tool t, String[] args) {
022: antlrTool = t;
023: processArguments(args);
024: }
025:
026: public static void main(String[] args) {
027: antlr.Tool antlrTool = new antlr.Tool();
028: Tool theTool = new Tool(antlrTool, args);
029: theTool.preprocess();
030: String[] a = theTool.preprocessedArgList();
031: for (int i = 0; i < a.length; i++) {
032: System.out.print(" " + a[i]);
033: }
034: System.out.println();
035: }
036:
037: public boolean preprocess() {
038: if (grammarFileName == null) {
039: antlrTool.toolError("no grammar file specified");
040: return false;
041: }
042: if (grammars != null) {
043: theHierarchy = new Hierarchy(antlrTool);
044: for (Enumeration e = grammars.elements(); e
045: .hasMoreElements();) {
046: String f = (String) e.nextElement();
047: try {
048: theHierarchy.readGrammarFile(f);
049: } catch (FileNotFoundException fe) {
050: antlrTool.toolError("file " + f + " not found");
051: return false;
052: }
053: }
054: }
055:
056: // do the actual inheritance stuff
057: boolean complete = theHierarchy.verifyThatHierarchyIsComplete();
058: if (!complete)
059: return false;
060: theHierarchy.expandGrammarsInFile(grammarFileName);
061: GrammarFile gf = theHierarchy.getFile(grammarFileName);
062: String expandedFileName = gf
063: .nameForExpandedGrammarFile(grammarFileName);
064:
065: // generate the output file if necessary
066: if (expandedFileName.equals(grammarFileName)) {
067: args[nargs++] = grammarFileName; // add to argument list
068: } else {
069: try {
070: gf.generateExpandedFile(); // generate file to feed ANTLR
071: args[nargs++] = antlrTool.getOutputDirectory()
072: + System.getProperty("file.separator")
073: + expandedFileName; // add to argument list
074: } catch (IOException io) {
075: antlrTool
076: .toolError("cannot write expanded grammar file "
077: + expandedFileName);
078: return false;
079: }
080: }
081: return true;
082: }
083:
084: /** create new arg list with correct length to pass to ANTLR */
085: public String[] preprocessedArgList() {
086: String[] a = new String[nargs];
087: System.arraycopy(args, 0, a, 0, nargs);
088: args = a;
089: return args;
090: }
091:
092: /** Process -glib options and grammar file. Create a new args list
093: * that does not contain the -glib option. The grammar file name
094: * might be modified and, hence, is not added yet to args list.
095: */
096: private void processArguments(String[] incomingArgs) {
097: this .nargs = 0;
098: this .args = new String[incomingArgs.length];
099: for (int i = 0; i < incomingArgs.length; i++) {
100: if (incomingArgs[i].length() == 0) {
101: antlrTool.warning("Zero length argument ignoring...");
102: continue;
103: }
104: if (incomingArgs[i].equals("-glib")) {
105: // if on a pc and they use a '/', warn them
106: if (File.separator.equals("\\")
107: && incomingArgs[i].indexOf('/') != -1) {
108: antlrTool
109: .warning("-glib cannot deal with '/' on a PC: use '\\'; ignoring...");
110: } else {
111: grammars = antlrTool.parseSeparatedList(
112: incomingArgs[i + 1], ';');
113: i++;
114: }
115: } else if (incomingArgs[i].equals("-o")) {
116: args[this .nargs++] = incomingArgs[i];
117: if (i + 1 >= incomingArgs.length) {
118: antlrTool
119: .error("missing output directory with -o option; ignoring");
120: } else {
121: i++;
122: args[this .nargs++] = incomingArgs[i];
123: antlrTool.setOutputDirectory(incomingArgs[i]);
124: }
125: } else if (incomingArgs[i].charAt(0) == '-') {
126: args[this .nargs++] = incomingArgs[i];
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
136: .warning("grammar file must be last; ignoring other arguments...");
137: break;
138: }
139: }
140: }
141: }
142: }
|