01: package persistence.antlr.preprocessor;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: import persistence.antlr.collections.impl.IndexedVector;
10:
11: import java.util.Enumeration;
12: import java.io.*;
13:
14: /** Stores header action, grammar preamble, file options, and
15: * list of grammars in the file
16: */
17: public class GrammarFile {
18: protected String fileName;
19: protected String headerAction = "";
20: protected IndexedVector options;
21: protected IndexedVector grammars;
22: protected boolean expanded = false; // any grammars expanded within?
23: protected persistence.antlr.Tool tool;
24:
25: public GrammarFile(persistence.antlr.Tool tool, String f) {
26: fileName = f;
27: grammars = new IndexedVector();
28: this .tool = tool;
29: }
30:
31: public void addGrammar(Grammar g) {
32: grammars.appendElement(g.getName(), g);
33: }
34:
35: public void generateExpandedFile() throws IOException {
36: if (!expanded) {
37: return; // don't generate if nothing got expanded
38: }
39: String expandedFileName = nameForExpandedGrammarFile(this
40: .getName());
41:
42: // create the new grammar file with expanded grammars
43: PrintWriter expF = tool.openOutputFile(expandedFileName);
44: expF.println(toString());
45: expF.close();
46: }
47:
48: public IndexedVector getGrammars() {
49: return grammars;
50: }
51:
52: public String getName() {
53: return fileName;
54: }
55:
56: public String nameForExpandedGrammarFile(String f) {
57: if (expanded) {
58: // strip path to original input, make expanded file in current dir
59: return "expanded" + tool.fileMinusPath(f);
60: } else {
61: return f;
62: }
63: }
64:
65: public void setExpanded(boolean exp) {
66: expanded = exp;
67: }
68:
69: public void addHeaderAction(String a) {
70: headerAction += a + System.getProperty("line.separator");
71: }
72:
73: public void setOptions(IndexedVector o) {
74: options = o;
75: }
76:
77: public String toString() {
78: String h = headerAction == null ? "" : headerAction;
79: String o = options == null ? "" : Hierarchy
80: .optionsToString(options);
81:
82: StringBuffer s = new StringBuffer(10000);
83: s.append(h);
84: s.append(o);
85: for (Enumeration e = grammars.elements(); e.hasMoreElements();) {
86: Grammar g = (Grammar) e.nextElement();
87: s.append(g.toString());
88: }
89: return s.toString();
90: }
91: }
|