KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSharpTokenizerFile.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) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.output;
23
24 import java.io.IOException JavaDoc;
25
26 import net.percederberg.grammatica.code.csharp.CSharpClass;
27 import net.percederberg.grammatica.code.csharp.CSharpComment;
28 import net.percederberg.grammatica.code.csharp.CSharpConstructor;
29 import net.percederberg.grammatica.code.csharp.CSharpFile;
30 import net.percederberg.grammatica.code.csharp.CSharpNamespace;
31 import net.percederberg.grammatica.code.csharp.CSharpMethod;
32 import net.percederberg.grammatica.code.csharp.CSharpUsing;
33 import net.percederberg.grammatica.parser.TokenPattern;
34
35 /**
36  * The C# tokenizer file generator. This class encapsulates all the
37  * C# code necessary for creating a tokenizer.
38  *
39  * @author Per Cederberg, <per at percederberg dot net>
40  * @version 1.5
41  */

42 class CSharpTokenizerFile {
43
44     /**
45      * The tokenizer type comment.
46      */

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

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

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

72     private CSharpParserGenerator gen;
73
74     /**
75      * The file to write.
76      */

77     private CSharpFile file;
78
79     /**
80      * The class to write.
81      */

82     private CSharpClass cls;
83
84     /**
85      * The class initializer method.
86      */

87     private CSharpMethod initMethod;
88
89     /**
90      * Creates a new tokenizer file.
91      *
92      * @param gen the parser generator to use
93      */

94     public CSharpTokenizerFile(CSharpParserGenerator gen) {
95         String JavaDoc name = gen.getBaseName() + "Tokenizer";
96         int modifiers;
97
98         this.gen = gen;
99         this.file = new CSharpFile(gen.getBaseDir(), name);
100         if (gen.getPublicAccess()) {
101             modifiers = CSharpClass.PUBLIC;
102         } else {
103             modifiers = CSharpClass.INTERNAL;
104         }
105         this.cls = new CSharpClass(modifiers, name, "Tokenizer");
106         this.initMethod = new CSharpMethod(CSharpMethod.PRIVATE,
107                                            "CreatePatterns",
108                                            "",
109                                            "void");
110         initializeCode();
111     }
112
113     /**
114      * Initializes the source code objects.
115      */

116     private void initializeCode() {
117         CSharpConstructor constr;
118         String JavaDoc str;
119
120         // Add using
121
file.addUsing(new CSharpUsing("System.IO"));
122         file.addUsing(new CSharpUsing("PerCederberg.Grammatica.Runtime"));
123
124         // Add namespace
125
if (gen.getNamespace() == null) {
126             file.addClass(cls);
127         } else {
128             CSharpNamespace n = new CSharpNamespace(gen.getNamespace());
129             n.addClass(cls);
130             file.addNamespace(n);
131         }
132
133         // Add file comment
134
str = file.toString() + "\n\n" + gen.getFileComment();
135         file.addComment(new CSharpComment(CSharpComment.BLOCK, str));
136
137         // Add type comment
138
cls.addComment(new CSharpComment(TYPE_COMMENT));
139
140         // Add constructor
141
constr = new CSharpConstructor("TextReader input");
142         cls.addConstructor(constr);
143         constr.addComment(new CSharpComment(CONSTRUCTOR_COMMENT));
144         constr.addInitializer("base(input, " +
145                               !gen.getGrammar().getCaseSensitive() +
146                               ")");
147         constr.addCode("CreatePatterns();");
148
149         // Add init method
150
cls.addMethod(initMethod);
151         initMethod.addComment(new CSharpComment(INIT_METHOD_COMMENT));
152         initMethod.addCode("TokenPattern pattern;");
153     }
154
155     /**
156      * Adds a token pattern definition to this file.
157      *
158      * @param pattern the token pattern
159      * @param constants the constants file generator
160      */

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

225     protected String JavaDoc getConstructorCall(String JavaDoc param) {
226         return "new " + gen.getBaseName() + "Tokenizer(" + param + ")";
227     }
228
229     /**
230      * Writes the file source code.
231      *
232      * @throws IOException if the output file couldn't be created
233      * correctly
234      */

235     public void writeCode() throws IOException JavaDoc {
236         file.writeCode(gen.getCodeStyle());
237     }
238 }
239
Popular Tags