KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > preprocessor > GrammarFile


1 package persistence.antlr.preprocessor;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.IndexedVector;
10
11 import java.util.Enumeration JavaDoc;
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 JavaDoc fileName;
19     protected String JavaDoc 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 JavaDoc 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 JavaDoc expandedFileName = nameForExpandedGrammarFile(this.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 JavaDoc getName() {
52         return fileName;
53     }
54
55     public String JavaDoc nameForExpandedGrammarFile(String JavaDoc f) {
56         if (expanded) {
57             // strip path to original input, make expanded file in current dir
58
return "expanded" + tool.fileMinusPath(f);
59         }
60         else {
61             return f;
62         }
63     }
64
65     public void setExpanded(boolean exp) {
66         expanded = exp;
67     }
68
69     public void addHeaderAction(String JavaDoc a) {
70         headerAction += a + System.getProperty("line.separator");
71     }
72
73     public void setOptions(IndexedVector o) {
74         options = o;
75     }
76
77     public String JavaDoc toString() {
78         String JavaDoc h = headerAction == null ? "" : headerAction;
79         String JavaDoc o = options == null ? "" : Hierarchy.optionsToString(options);
80
81         StringBuffer JavaDoc s = new StringBuffer JavaDoc(10000); s.append(h); s.append(o);
82         for (Enumeration JavaDoc e = grammars.elements(); e.hasMoreElements();) {
83             Grammar g = (Grammar)e.nextElement();
84             s.append(g.toString());
85         }
86         return s.toString();
87     }
88 }
89
Popular Tags