KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > output > VisualBasicAnalyzerFile


1 /*
2  * VisualBasicAnalyzerFile.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2004 Adrian Moore. All rights reserved.
20  * Copyright (c) 2004-2005 Per Cederberg. All rights reserved.
21  */

22
23 package net.percederberg.grammatica.output;
24
25 import java.io.IOException JavaDoc;
26
27 import net.percederberg.grammatica.code.visualbasic.VisualBasicClass;
28 import net.percederberg.grammatica.code.visualbasic.VisualBasicComment;
29 import net.percederberg.grammatica.code.visualbasic.VisualBasicFile;
30 import net.percederberg.grammatica.code.visualbasic.VisualBasicImports;
31 import net.percederberg.grammatica.code.visualbasic.VisualBasicMethod;
32 import net.percederberg.grammatica.code.visualbasic.VisualBasicNamespace;
33
34 import net.percederberg.grammatica.parser.ProductionPattern;
35 import net.percederberg.grammatica.parser.TokenPattern;
36
37 /**
38  * The Visual Basic analyzer file generator. This class encapsulates
39  * all the Visual Basic (.NET) code necessary for creating a analyzer
40  * class file.
41  *
42  * @author Adrian Moore, <adrianrob at hotmail dot com>
43  * @author Per Cederberg, <per at percederberg dot net>
44  * @version 1.5
45  * @since 1.5
46  */

47 class VisualBasicAnalyzerFile {
48
49     /**
50      * The class comment.
51      */

52     private static final String JavaDoc TYPE_COMMENT =
53         "<remarks>A class providing callback methods for the\n" +
54         "parser.</remarks>";
55
56     /**
57      * The enter method comment.
58      */

59     private static final String JavaDoc ENTER_COMMENT =
60         "<summary>Called when entering a parse tree node.</summary>\n\n" +
61         "<param name='node'>the node being entered</param>\n\n" +
62         "<exception cref='ParseException'>if the node analysis\n" +
63         "discovered errors</exception>";
64
65     /**
66      * The exit method comment.
67      */

68     private static final String JavaDoc EXIT_COMMENT =
69         "<summary>Called when exiting a parse tree node.</summary>\n\n" +
70         "<param name='node'>the node being exited</param>\n\n" +
71         "<returns>the node to add to the parse tree, or\n" +
72         " null if no parse tree should be created</returns>\n\n" +
73         "<exception cref='ParseException'>if the node analysis\n" +
74         "discovered errors</exception>";
75
76     /**
77      * The child method comment.
78      */

79     private static final String JavaDoc CHILD_COMMENT =
80         "<summary>Called when adding a child to a parse tree\n" +
81         "node.</summary>\n\n" +
82         "<param name='node'>the parent node</param>\n" +
83         "<param name='child'>the child node, or null</param>\n\n" +
84         "<exception cref='ParseException'>if the node analysis\n" +
85         "discovered errors</exception>";
86
87     /**
88      * The parser generator.
89      */

90     private VisualBasicParserGenerator gen;
91
92     /**
93      * The file to write.
94      */

95     private VisualBasicFile file;
96
97     /**
98      * The class.
99      */

100     private VisualBasicClass cls;
101
102     /**
103      * The enter method.
104      */

105     private VisualBasicMethod enter;
106
107     /**
108      * The exit method.
109      */

110     private VisualBasicMethod exit;
111
112     /**
113      * The child method.
114      */

115     private VisualBasicMethod child;
116
117     /**
118      * Creates a new analyzer file.
119      *
120      * @param gen the parser generator to use
121      */

122     public VisualBasicAnalyzerFile(VisualBasicParserGenerator gen) {
123         String JavaDoc name = gen.getBaseName() + "Analyzer";
124         int modifiers;
125
126         this.gen = gen;
127         this.file = new VisualBasicFile(gen.getBaseDir(), name);
128         modifiers = VisualBasicClass.MUST_INHERIT;
129         if (gen.getPublicAccess()) {
130             modifiers += VisualBasicClass.PUBLIC;
131         } else {
132             modifiers += VisualBasicClass.FRIEND;
133         }
134         this.cls = new VisualBasicClass(modifiers, name, "Analyzer");
135         modifiers = VisualBasicMethod.PUBLIC + VisualBasicMethod.OVERRIDES;
136         this.enter = new VisualBasicMethod(modifiers,
137                                            "Enter",
138                                            "ByVal node As Node",
139                                            "");
140         this.exit = new VisualBasicMethod(modifiers,
141                                           "[Exit]",
142                                           "ByVal node As Node",
143                                           "Node");
144         this.child = new VisualBasicMethod(modifiers,
145                                            "Child",
146                                            "ByVal node As Production, " +
147                                            "ByVal child As Node",
148                                            "");
149         initializeCode();
150     }
151
152     /**
153      * Initializes the source code objects.
154      */

155     private void initializeCode() {
156         VisualBasicNamespace n;
157         String JavaDoc str;
158
159         // Add using
160
file.addImports(new VisualBasicImports("PerCederberg.Grammatica.Runtime"));
161
162         // Add namespace
163
if (gen.getNamespace() == null) {
164             file.addClass(cls);
165         } else {
166             n = new VisualBasicNamespace(gen.getNamespace());
167             n.addClass(cls);
168             file.addNamespace(n);
169         }
170
171         // Add file comment
172
str = file.toString() + "\n\n" + gen.getFileComment();
173         file.addComment(new VisualBasicComment(VisualBasicComment.SINGLELINE,
174                                                str));
175
176         // Add type comment
177
cls.addComment(new VisualBasicComment(TYPE_COMMENT));
178
179         // Add enter method
180
enter.addComment(new VisualBasicComment(ENTER_COMMENT));
181         enter.addCode("Select Case node.Id");
182         cls.addMethod(enter);
183
184         // Add exit method
185
exit.addComment(new VisualBasicComment(EXIT_COMMENT));
186         exit.addCode("Select Case node.Id");
187         cls.addMethod(exit);
188
189         // Add child method
190
child.addComment(new VisualBasicComment(CHILD_COMMENT));
191         child.addCode("Select Case node.Id");
192         cls.addMethod(child);
193     }
194
195     /**
196      * Adds the token analysis methods to this file.
197      *
198      * @param pattern the token pattern
199      * @param constants the constants file
200      */

201     public void addToken(TokenPattern pattern,
202                          VisualBasicConstantsFile constants) {
203
204         String JavaDoc constant = constants.getConstant(pattern.getId());
205         String JavaDoc name;
206
207         if (!pattern.isIgnore()) {
208             name = gen.getCodeStyle().getMixedCase(pattern.getName(), true);
209             addEnterCase(constant, name, "Token");
210             addEnterMethod(name, "Token");
211             addExitCase(constant, name, "Token");
212             addExitMethod(name, "Token");
213         }
214     }
215
216     /**
217      * Adds the production analysis methods to this file.
218      *
219      * @param pattern the production pattern
220      * @param constants the constants file
221      */

222     public void addProduction(ProductionPattern pattern,
223                               VisualBasicConstantsFile constants) {
224
225         String JavaDoc constant = constants.getConstant(pattern.getId());
226         String JavaDoc name;
227
228         if (!pattern.isSynthetic()) {
229             name = gen.getCodeStyle().getMixedCase(pattern.getName(),
230                                                    true);
231             addEnterCase(constant, name, "Production");
232             addEnterMethod(name, "Production");
233             addExitCase(constant, name, "Production");
234             addExitMethod(name, "Production");
235             addChildCase(constant, name);
236             addChildMethod(name);
237         }
238     }
239
240     /**
241      * Adds an enter method switch case.
242      *
243      * @param constant the node constant
244      * @param name the node name
245      * @param type the node type
246      */

247     private void addEnterCase(String JavaDoc constant, String JavaDoc name, String JavaDoc type) {
248         enter.addCode("Case " + constant );
249         enter.addCode(" Enter" + name + "(CType(node," + type + "))");
250         enter.addCode("");
251     }
252
253     /**
254      * Adds an exit method switch case.
255      *
256      * @param constant the node constant
257      * @param name the node name
258      * @param type the node type
259      */

260     private void addExitCase(String JavaDoc constant, String JavaDoc name, String JavaDoc type) {
261         exit.addCode("Case " + constant );
262         exit.addCode(" return Exit" + name + "(CType(node," + type + "))");
263         exit.addCode("");
264     }
265
266     /**
267      * Adds a child method switch case.
268      *
269      * @param constant the node constant
270      * @param name the node name
271      */

272     private void addChildCase(String JavaDoc constant, String JavaDoc name) {
273         child.addCode("Case " + constant );
274         child.addCode(" Child" + name + "(node, child)");
275         child.addCode("");
276     }
277
278     /**
279      * Adds an enter node method to this file.
280      *
281      * @param name the node name
282      * @param type the node type
283      */

284     private void addEnterMethod(String JavaDoc name, String JavaDoc type) {
285         VisualBasicMethod m;
286
287         m = new VisualBasicMethod(VisualBasicMethod.PUBLIC +
288                                   VisualBasicMethod.OVERRIDABLE,
289                                   "Enter" + name,
290                                   "ByVal node As " + type,
291                                   "");
292         m.addComment(new VisualBasicComment(ENTER_COMMENT));
293         cls.addMethod(m);
294     }
295
296     /**
297      * Adds an exit node method to this file.
298      *
299      * @param name the node name
300      * @param type the node type
301      */

302     private void addExitMethod(String JavaDoc name, String JavaDoc type) {
303         VisualBasicMethod m;
304
305         m = new VisualBasicMethod(VisualBasicMethod.PUBLIC +
306                                   VisualBasicMethod.OVERRIDABLE,
307                                   "Exit" + name,
308                                   "ByVal node As " + type,
309                                   "Node");
310         m.addComment(new VisualBasicComment(EXIT_COMMENT));
311         m.addCode("Return node");
312         cls.addMethod(m);
313     }
314
315     /**
316      * Adds an add child method to this file.
317      *
318      * @param name the node name
319      */

320     private void addChildMethod(String JavaDoc name) {
321         VisualBasicMethod m;
322
323         m = new VisualBasicMethod(VisualBasicMethod.PUBLIC +
324                                   VisualBasicMethod.OVERRIDABLE,
325                                   "Child" + name,
326                                   "ByVal node As Production, " +
327                                   "ByVal child As Node",
328                                   "");
329         m.addComment(new VisualBasicComment(CHILD_COMMENT));
330         m.addCode("node.AddChild(child)");
331         cls.addMethod(m);
332     }
333
334     /**
335      * Writes the file source code.
336      *
337      * @throws IOException if the output file couldn't be created
338      * correctly
339      */

340     public void writeCode() throws IOException JavaDoc {
341         enter.addCode("End Select");
342         exit.addCode("End Select");
343         exit.addCode("Return node");
344         child.addCode("End Select");
345         file.writeCode(gen.getCodeStyle());
346     }
347 }
348
Popular Tags