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