KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VisualBasicParserGenerator.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.Grammar;
28 import net.percederberg.grammatica.code.CodeStyle;
29 import net.percederberg.grammatica.parser.ProductionPattern;
30 import net.percederberg.grammatica.parser.TokenPattern;
31
32 /**
33  * A Visual Basic parser generator. This class generates the source
34  * code files needed for a Visual Basic (.NET) parser.
35  *
36  * @author Adrian Moore, <adrianrob at hotmail dot com>
37  * @author Per Cederberg, <per at percederberg dot net>
38  * @version 1.5
39  * @since 1.5
40  */

41 public class VisualBasicParserGenerator extends ParserGenerator {
42
43     /**
44      * The class name prefix.
45      */

46     private String JavaDoc baseName = null;
47
48     /**
49      * The namespace to use.
50      */

51     private String JavaDoc namespace = null;
52
53     /**
54      * The public class and interface access flag.
55      */

56     private boolean publicAccess = false;
57
58     /**
59      * Creates a new parser generator.
60      *
61      * @param grammar the grammar to use
62      */

63     public VisualBasicParserGenerator(Grammar grammar) {
64         super(grammar);
65         initialize();
66     }
67
68     /**
69      * Initializes various instance variables.
70      */

71     private void initialize() {
72         String JavaDoc str;
73
74         // Set base name
75
str = getGrammar().getFileName();
76         if (str.indexOf('/') >= 0) {
77             str = str.substring(str.lastIndexOf('/') + 1);
78         }
79         if (str.indexOf('\\') >= 0) {
80             str = str.substring(str.lastIndexOf('\\') + 1);
81         }
82         if (str.indexOf('.') > 0) {
83             str = str.substring(0, str.indexOf('.'));
84         }
85         if (Character.isLowerCase(str.charAt(0))) {
86             str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
87         }
88         baseName = str;
89     }
90
91     /**
92      * Returns the namespace used for the classes.
93      *
94      * @return the fully qualified namespace, or
95      * null for none
96      */

97     public String JavaDoc getNamespace() {
98         return namespace;
99     }
100
101     /**
102      * Sets the namespace to use for the classes.
103      *
104      * @param namespace the fully qualified namespace
105      */

106     public void setNamespace(String JavaDoc namespace) {
107         this.namespace = namespace;
108     }
109
110     /**
111      * Returns the class name prefix.
112      *
113      * @return the class name prefix
114      */

115     public String JavaDoc getBaseName() {
116         return baseName;
117     }
118
119     /**
120      * Sets the class name prefix.
121      *
122      * @param name the class name prefix
123      */

124     public void setBaseName(String JavaDoc name) {
125         this.baseName = name;
126     }
127
128     /**
129      * Returns the public access flag.
130      *
131      * @return true if the classes should have public access, or
132      * false otherwise
133      */

134     public boolean getPublicAccess() {
135         return publicAccess;
136     }
137
138     /**
139      * Sets the public access flag.
140      *
141      * @param flag the new public access flag value
142      */

143     public void setPublicAccess(boolean flag) {
144         publicAccess = flag;
145     }
146
147     /**
148      * Returns the code style to use.
149      *
150      * @return the code style to use
151      */

152     public CodeStyle getCodeStyle() {
153         return CodeStyle.VISUAL_BASIC;
154     }
155
156     /**
157      * Writes the source code files.
158      *
159      * @throws IOException if the files couldn't be written correctly
160      */

161     public void write() throws IOException JavaDoc {
162         Grammar grammar = getGrammar();
163         VisualBasicConstantsFile constants;
164         VisualBasicTokenizerFile tokenizer;
165         VisualBasicParserFile parser;
166         VisualBasicAnalyzerFile analyzer;
167         TokenPattern token;
168         ProductionPattern production;
169         int i;
170
171         // Create output files
172
constants = new VisualBasicConstantsFile(this);
173         tokenizer = new VisualBasicTokenizerFile(this);
174         parser = new VisualBasicParserFile(this, tokenizer);
175         analyzer = new VisualBasicAnalyzerFile(this);
176
177         // Create token declarations
178
for (i = 0; i < grammar.getTokenPatternCount(); i++) {
179             token = grammar.getTokenPattern(i);
180             constants.addToken(token);
181             tokenizer.addToken(token, constants);
182             analyzer.addToken(token, constants);
183         }
184
185         // Create production constants
186
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
187             production = grammar.getProductionPattern(i);
188             constants.addProduction(production);
189             parser.addProductionConstant(production);
190             analyzer.addProduction(production, constants);
191         }
192
193         // Create production declarations
194
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
195             production = grammar.getProductionPattern(i);
196             parser.addProduction(production, constants);
197         }
198
199         // Write source code files
200
constants.writeCode();
201         tokenizer.writeCode();
202         parser.writeCode();
203         analyzer.writeCode();
204     }
205 }
206
Popular Tags