KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 class Grammar {
16     protected String JavaDoc name;
17     protected String JavaDoc fileName; // where does it come from?
18
protected String JavaDoc superGrammar; // null if no super class
19
protected String JavaDoc type; // lexer? parser? tree parser?
20
protected IndexedVector rules; // text of rules as they were read in
21
protected IndexedVector options;// rule options
22
protected String JavaDoc tokenSection; // the tokens{} stuff
23
protected String JavaDoc preambleAction;// action right before grammar
24
protected String JavaDoc memberAction; // action inside grammar
25
protected Hierarchy hier; // hierarchy of grammars
26
protected boolean predefined = false; // one of the predefined grammars?
27
protected boolean alreadyExpanded = false;
28     protected boolean specifiedVocabulary = false; // found importVocab option?
29

30     /** if not derived from another grammar, might still specify a non-ANTLR
31      * class to derive from like this "class T extends Parser(MyParserClass);"
32      */

33     protected String JavaDoc superClass = null;
34
35     protected String JavaDoc importVocab = null;
36     protected String JavaDoc exportVocab = null;
37     protected persistence.antlr.Tool antlrTool;
38
39     public Grammar(persistence.antlr.Tool tool, String JavaDoc name, String JavaDoc superGrammar, IndexedVector rules) {
40         this.name = name;
41         this.superGrammar = superGrammar;
42         this.rules = rules;
43         this.antlrTool = tool;
44     }
45
46     public void addOption(Option o) {
47         if (options == null) { // if not already there, create it
48
options = new IndexedVector();
49         }
50         options.appendElement(o.getName(), o);
51     }
52
53     public void addRule(Rule r) {
54         rules.appendElement(r.getName(), r);
55     }
56
57     /** Copy all nonoverridden rules, vocabulary, and options into this grammar from
58      * supergrammar chain. The change is made in place; e.g., this grammar's vector
59      * of rules gets bigger. This has side-effects: all grammars on path to
60      * root of hierarchy are expanded also.
61      */

62     public void expandInPlace() {
63         // if this grammar already expanded, just return
64
if (alreadyExpanded) {
65             return;
66         }
67
68         // Expand super grammar first (unless it's a predefined or subgrammar of predefined)
69
Grammar superG = getSuperGrammar();
70         if (superG == null)
71             return; // error (didn't provide superclass)
72
if (exportVocab == null) {
73             // if no exportVocab for this grammar, make it same as grammar name
74
exportVocab = getName();
75         }
76         if (superG.isPredefined())
77             return; // can't expand Lexer, Parser, ...
78
superG.expandInPlace();
79
80         // expand current grammar now.
81
alreadyExpanded = true;
82         // track whether a grammar file needed to have a grammar expanded
83
GrammarFile gf = hier.getFile(getFileName());
84         gf.setExpanded(true);
85
86         // Copy rules from supergrammar into this grammar
87
IndexedVector inhRules = superG.getRules();
88         for (Enumeration JavaDoc e = inhRules.elements(); e.hasMoreElements();) {
89             Rule r = (Rule)e.nextElement();
90             inherit(r, superG);
91         }
92
93         // Copy options from supergrammar into this grammar
94
// Modify tokdef options so that they point to dir of enclosing grammar
95
IndexedVector inhOptions = superG.getOptions();
96         if (inhOptions != null) {
97             for (Enumeration JavaDoc e = inhOptions.elements(); e.hasMoreElements();) {
98                 Option o = (Option)e.nextElement();
99                 inherit(o, superG);
100             }
101         }
102
103         // add an option to load the superGrammar's output vocab
104
if ((options != null && options.getElement("importVocab") == null) || options == null) {
105             // no importVocab found, add one that grabs superG's output vocab
106
Option inputV = new Option("importVocab", superG.exportVocab + ";", this);
107             addOption(inputV);
108             // copy output vocab file to current dir
109
String JavaDoc originatingGrFileName = superG.getFileName();
110             String JavaDoc path = antlrTool.pathToFile(originatingGrFileName);
111             String JavaDoc superExportVocabFileName = path + superG.exportVocab +
112                 persistence.antlr.CodeGenerator.TokenTypesFileSuffix +
113                 persistence.antlr.CodeGenerator.TokenTypesFileExt;
114             String JavaDoc newImportVocabFileName = antlrTool.fileMinusPath(superExportVocabFileName);
115             if (path.equals("." + System.getProperty("file.separator"))) {
116                 // don't copy tokdef file onto itself (must be current directory)
117
// System.out.println("importVocab file same dir; leaving as " + superExportVocabFileName);
118
}
119             else {
120                 try {
121                     antlrTool.copyFile(superExportVocabFileName, newImportVocabFileName);
122                 }
123                 catch (IOException JavaDoc io) {
124                     antlrTool.toolError("cannot find/copy importVocab file " + superExportVocabFileName);
125                     return;
126                 }
127             }
128         }
129
130         // copy member action from supergrammar into this grammar
131
inherit(superG.memberAction, superG);
132     }
133
134     public String JavaDoc getFileName() {
135         return fileName;
136     }
137
138     public String JavaDoc getName() {
139         return name;
140     }
141
142     public IndexedVector getOptions() {
143         return options;
144     }
145
146     public IndexedVector getRules() {
147         return rules;
148     }
149
150     public Grammar getSuperGrammar() {
151         if (superGrammar == null) return null;
152         Grammar g = (Grammar)hier.getGrammar(superGrammar);
153         return g;
154     }
155
156     public String JavaDoc getSuperGrammarName() {
157         return superGrammar;
158     }
159
160     public String JavaDoc getType() {
161         return type;
162     }
163
164     public void inherit(Option o, Grammar superG) {
165         // do NOT inherit importVocab/exportVocab options under any circumstances
166
if (o.getName().equals("importVocab") ||
167             o.getName().equals("exportVocab")) {
168             return;
169         }
170
171         Option overriddenOption = null;
172         if (options != null) { // do we even have options?
173
overriddenOption = (Option)options.getElement(o.getName());
174         }
175         // if overridden, do not add to this grammar
176
if (overriddenOption == null) { // not overridden
177
addOption(o); // copy option into this grammar--not overridden
178
}
179     }
180
181     public void inherit(Rule r, Grammar superG) {
182         // if overridden, do not add to this grammar
183
Rule overriddenRule = (Rule)rules.getElement(r.getName());
184         if (overriddenRule != null) {
185             // rule is overridden in this grammar.
186
if (!overriddenRule.sameSignature(r)) {
187                 // warn if different sig
188
antlrTool.warning("rule " + getName() + "." + overriddenRule.getName() +
189                                    " has different signature than " +
190                                    superG.getName() + "." + overriddenRule.getName());
191             }
192         }
193         else { // not overridden, copy rule into this
194
addRule(r);
195         }
196     }
197
198     public void inherit(String JavaDoc memberAction, Grammar superG) {
199         if (this.memberAction != null) return; // do nothing if already have member action
200
if (memberAction != null) { // don't have one here, use supergrammar's action
201
this.memberAction = memberAction;
202         }
203     }
204
205     public boolean isPredefined() {
206         return predefined;
207     }
208
209     public void setFileName(String JavaDoc f) {
210         fileName = f;
211     }
212
213     public void setHierarchy(Hierarchy hier) {
214         this.hier = hier;
215     }
216
217     public void setMemberAction(String JavaDoc a) {
218         memberAction = a;
219     }
220
221     public void setOptions(IndexedVector options) {
222         this.options = options;
223     }
224
225     public void setPreambleAction(String JavaDoc a) {
226         preambleAction = a;
227     }
228
229     public void setPredefined(boolean b) {
230         predefined = b;
231     }
232
233     public void setTokenSection(String JavaDoc tk) {
234         tokenSection = tk;
235     }
236
237     public void setType(String JavaDoc t) {
238         type = t;
239     }
240
241     public String JavaDoc toString() {
242         StringBuffer JavaDoc s = new StringBuffer JavaDoc(10000);
243         if (preambleAction != null) {
244             s.append(preambleAction);
245         }
246         if (superGrammar == null) {
247             return "class " + name + ";";
248         }
249         if ( superClass!=null ) {
250             // replace with specified superclass not actual grammar
251
// user must make sure that the superclass derives from super grammar class
252
s.append("class " + name + " extends " + superClass + ";");
253         }
254         else {
255             s.append("class " + name + " extends " + type + ";");
256         }
257         s.append(
258             System.getProperty("line.separator") +
259             System.getProperty("line.separator"));
260         if (options != null) {
261             s.append(Hierarchy.optionsToString(options));
262         }
263         if (tokenSection != null) {
264             s.append(tokenSection + "\n");
265         }
266         if (memberAction != null) {
267             s.append(memberAction + System.getProperty("line.separator"));
268         }
269         for (int i = 0; i < rules.size(); i++) {
270             Rule r = (Rule)rules.elementAt(i);
271             if (!getName().equals(r.enclosingGrammar.getName())) {
272                 s.append("// inherited from grammar " + r.enclosingGrammar.getName() + System.getProperty("line.separator"));
273             }
274             s.append(r +
275                 System.getProperty("line.separator") +
276                 System.getProperty("line.separator"));
277         }
278         return s.toString();
279     }
280 }
281
Popular Tags