KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VisualBasicTokenizerFile.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.VisualBasicConstructor;
30 import net.percederberg.grammatica.code.visualbasic.VisualBasicFile;
31 import net.percederberg.grammatica.code.visualbasic.VisualBasicImports;
32 import net.percederberg.grammatica.code.visualbasic.VisualBasicNamespace;
33 import net.percederberg.grammatica.code.visualbasic.VisualBasicMethod;
34 import net.percederberg.grammatica.parser.TokenPattern;
35
36 /**
37  * The Visual Basic tokenizer file generator. This class encapsulates
38  * all the Visual Basic (.NET) code necessary for creating a tokenizer.
39  *
40  * @author Adrian Moore, <adrianrob at hotmail dot com>
41  * @author Per Cederberg, <per at percederberg dot net>
42  * @version 1.5
43  * @since 1.5
44  */

45 class VisualBasicTokenizerFile {
46
47     /**
48      * The tokenizer type comment.
49      */

50     private static final String JavaDoc TYPE_COMMENT =
51         "<remarks>A character stream tokenizer.</remarks>";
52
53     /**
54      * The tokenizer constructor comment.
55      */

56     private static final String JavaDoc CONSTRUCTOR_COMMENT =
57         "<summary>Creates a new tokenizer for the specified input\n" +
58         "stream.</summary>\n\n" +
59         "<param name='input'>the input stream to read</param>\n\n" +
60         "<exception cref='ParserCreationException'>if the tokenizer\n" +
61         "couldn't be initialized correctly</exception>";
62
63     /**
64      * The init method comment.
65      */

66     private static final String JavaDoc INIT_METHOD_COMMENT =
67         "<summary>Initializes the tokenizer by creating all the token\n" +
68         "patterns.</summary>\n\n" +
69         "<exception cref='ParserCreationException'>if the tokenizer\n" +
70         "couldn't be initialized correctly</exception>";
71
72     /**
73      * The parser generator.
74      */

75     private VisualBasicParserGenerator gen;
76
77     /**
78      * The file to write.
79      */

80     private VisualBasicFile file;
81
82     /**
83      * The class to write.
84      */

85     private VisualBasicClass cls;
86
87     /**
88      * The class initializer method.
89      */

90     private VisualBasicMethod initMethod;
91
92     /**
93      * Creates a new tokenizer file.
94      *
95      * @param gen the parser generator to use
96      */

97     public VisualBasicTokenizerFile(VisualBasicParserGenerator gen) {
98         String JavaDoc name = gen.getBaseName() + "Tokenizer";
99         int modifiers;
100
101         this.gen = gen;
102         this.file = new VisualBasicFile(gen.getBaseDir(), name);
103         if (gen.getPublicAccess()) {
104             modifiers = VisualBasicClass.PUBLIC;
105         } else {
106             modifiers = VisualBasicClass.FRIEND;
107         }
108         this.cls = new VisualBasicClass(modifiers, name, "Tokenizer");
109         this.initMethod = new VisualBasicMethod(VisualBasicMethod.PRIVATE,
110                                                 "CreatePatterns",
111                                                 "",
112                                                 "");
113         initializeCode();
114     }
115
116     /**
117      * Initializes the source code objects.
118      */

119     private void initializeCode() {
120         VisualBasicConstructor constr;
121         VisualBasicNamespace n;
122         String JavaDoc str;
123
124         // Add using
125
file.addImports(new VisualBasicImports("System.IO"));
126         file.addImports(new VisualBasicImports("PerCederberg.Grammatica.Runtime"));
127
128         // Add namespace
129
if (gen.getNamespace() == null) {
130             file.addClass(cls);
131         } else {
132             n = new VisualBasicNamespace(gen.getNamespace());
133             n.addClass(cls);
134             file.addNamespace(n);
135         }
136
137         // Add file comment
138
str = file.toString() + "\n\n" + gen.getFileComment();
139         file.addComment(new VisualBasicComment(VisualBasicComment.SINGLELINE,
140                                                str));
141
142         // Add type comment
143
cls.addComment(new VisualBasicComment(TYPE_COMMENT));
144
145         // Add constructor
146
constr = new VisualBasicConstructor("ByVal input As TextReader");
147         cls.addConstructor(constr);
148         constr.addComment(new VisualBasicComment(CONSTRUCTOR_COMMENT));
149         constr.addCode("MyBase.New(input, " +
150                        getBoolean(!gen.getGrammar().getCaseSensitive()) +
151                        ")");
152         constr.addCode("CreatePatterns()");
153
154         // Add init method
155
cls.addMethod(initMethod);
156         initMethod.addComment(new VisualBasicComment(INIT_METHOD_COMMENT));
157         initMethod.addCode("Dim pattern as TokenPattern");
158     }
159
160     /**
161      * Adds a token pattern definition to this file.
162      *
163      * @param pattern the token pattern
164      * @param constants the constants file generator
165      */

166     public void addToken(TokenPattern pattern,
167                          VisualBasicConstantsFile constants) {
168
169         StringBuffer JavaDoc code = new StringBuffer JavaDoc();
170         String JavaDoc str;
171
172         // Create new pattern
173
code.append("pattern = New TokenPattern(CInt(");
174         code.append(constants.getConstant(pattern.getId()));
175         code.append("), \"");
176         code.append(pattern.getName());
177         code.append("\", TokenPattern.PatternType.");
178         switch (pattern.getType()) {
179         case TokenPattern.STRING_TYPE:
180             code.append("STRING");
181             break;
182         case TokenPattern.REGEXP_TYPE:
183             code.append("REGEXP");
184             break;
185         }
186         code.append(", ");
187         str = pattern.getPattern();
188         code.append(gen.getCodeStyle().getStringConstant(str, '"'));
189         code.append(")\n");
190
191         // Add error and ignore messages
192
if (pattern.isError()) {
193             if (pattern.getErrorMessage() == null) {
194                 code.append("pattern.Error = True");
195             } else {
196                 code.append("pattern.ErrorMessage = ");
197                 str = pattern.getErrorMessage();
198                 code.append(gen.getCodeStyle().getStringConstant(str, '"'));
199             }
200             code.append("\n");
201         }
202         if (pattern.isIgnore()) {
203             if (pattern.getIgnoreMessage() == null) {
204                 code.append("pattern.Ignore = True");
205             } else {
206                 code.append("pattern.IgnoreMessage = ");
207                 str = pattern.getIgnoreMessage();
208                 code.append(gen.getCodeStyle().getStringConstant(str, '"'));
209             }
210             code.append("\n");
211         }
212
213         // Add pattern to tokenizer
214
code.append("AddPattern(pattern)");
215         initMethod.addCode("");
216         initMethod.addCode(code.toString());
217     }
218
219     /**
220      * Creates source code performing a call to the constructor for
221      * the tokenizer.
222      *
223      * @param param the input parameters
224      *
225      * @return the source code for the call
226      */

227     protected String JavaDoc getConstructorCall(String JavaDoc param) {
228         return "New " + gen.getBaseName() + "Tokenizer(" + param + ")";
229     }
230
231     /**
232      * Returns the source code representation of a boolean value.
233      *
234      * @param value the boolean value
235      *
236      * @return the source code for the boolean constant
237      */

238     private String JavaDoc getBoolean(boolean value) {
239         return value ? "True" : "False";
240     }
241
242     /**
243      * Writes the file source code.
244      *
245      * @throws IOException if the output file couldn't be created
246      * correctly
247      */

248     public void writeCode() throws IOException JavaDoc {
249         file.writeCode(gen.getCodeStyle());
250     }
251 }
252
Popular Tags