KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaTokenizerFile.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.java.JavaClass;
27 import net.percederberg.grammatica.code.java.JavaComment;
28 import net.percederberg.grammatica.code.java.JavaConstructor;
29 import net.percederberg.grammatica.code.java.JavaFile;
30 import net.percederberg.grammatica.code.java.JavaImport;
31 import net.percederberg.grammatica.code.java.JavaMethod;
32 import net.percederberg.grammatica.parser.TokenPattern;
33
34 /**
35  * The Java tokenizer file generator. This class encapsulates all the
36  * Java code necessary for creating a tokenizer.
37  *
38  * @author Per Cederberg, <per at percederberg dot net>
39  * @version 1.5
40  */

41 class JavaTokenizerFile {
42
43     /**
44      * The tokenizer class comment.
45      */

46     private static final String JavaDoc CLASS_COMMENT =
47         "A character stream tokenizer.";
48
49     /**
50      * The tokenizer constructor comment.
51      */

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

61     private static final String JavaDoc INIT_METHOD_COMMENT =
62         "Initializes the tokenizer by creating all the token patterns.\n\n" +
63         "@throws ParserCreationException if the tokenizer couldn't be\n" +
64         " initialized correctly";
65
66     /**
67      * The Java parser generator.
68      */

69     private JavaParserGenerator gen;
70
71     /**
72      * The Java file to write.
73      */

74     private JavaFile file;
75
76     /**
77      * The Java class to write.
78      */

79     private JavaClass cls;
80
81     /**
82      * The Java class initializer method.
83      */

84     private JavaMethod initMethod;
85
86     /**
87      * Creates a new tokenizer file.
88      *
89      * @param gen the parser generator to use
90      */

91     public JavaTokenizerFile(JavaParserGenerator gen) {
92         int modifiers;
93
94         this.gen = gen;
95         this.file = gen.createJavaFile();
96         if (gen.getPublicAccess()) {
97             modifiers = JavaClass.PUBLIC;
98         } else {
99             modifiers = JavaClass.PACKAGE_LOCAL;
100         }
101         this.cls = new JavaClass(modifiers,
102                                  gen.getBaseName() + "Tokenizer",
103                                  "Tokenizer");
104         this.initMethod = new JavaMethod(JavaMethod.PRIVATE,
105                                          "createPatterns",
106                                          "",
107                                          "void");
108         initializeCode();
109     }
110
111     /**
112      * Initializes the source code objects.
113      */

114     private void initializeCode() {
115         JavaConstructor constr;
116         String JavaDoc str;
117
118         // Add imports
119
file.addImport(new JavaImport("java.io", "Reader"));
120         file.addImport(new JavaImport("net.percederberg.grammatica.parser",
121                                       "ParserCreationException"));
122         file.addImport(new JavaImport("net.percederberg.grammatica.parser",
123                                       "TokenPattern"));
124         file.addImport(new JavaImport("net.percederberg.grammatica.parser",
125                                       "Tokenizer"));
126
127         // Add class
128
file.addClass(cls);
129         str = CLASS_COMMENT;
130         if (gen.getClassComment() != null) {
131             str += "\n\n" + gen.getClassComment();
132         }
133         cls.addComment(new JavaComment(str));
134
135         // Add file comment
136
str = file.toString() + "\n\n" + gen.getFileComment();
137         file.addComment(new JavaComment(JavaComment.BLOCK, str));
138
139         // Add constructor
140
constr = new JavaConstructor("Reader input");
141         cls.addConstructor(constr);
142         constr.addComment(new JavaComment(CONSTRUCTOR_COMMENT));
143         constr.addThrows("ParserCreationException");
144         constr.addCode("super(input, " +
145                        !gen.getGrammar().getCaseSensitive() +
146                        ");");
147         constr.addCode("createPatterns();");
148
149         // Add init method
150
cls.addMethod(initMethod);
151         initMethod.addComment(new JavaComment(INIT_METHOD_COMMENT));
152         initMethod.addThrows("ParserCreationException");
153         initMethod.addCode("TokenPattern pattern;");
154     }
155
156     /**
157      * Adds a token pattern definition to this file.
158      *
159      * @param pattern the token pattern
160      * @param constants the constants file generator
161      */

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

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

230     public void writeCode() throws IOException JavaDoc {
231         file.writeCode(gen.getCodeStyle());
232     }
233 }
234
Popular Tags