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