KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaParserGenerator.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.Grammar;
27 import net.percederberg.grammatica.code.CodeStyle;
28 import net.percederberg.grammatica.code.java.JavaFile;
29 import net.percederberg.grammatica.code.java.JavaPackage;
30 import net.percederberg.grammatica.parser.ProductionPattern;
31 import net.percederberg.grammatica.parser.TokenPattern;
32
33 /**
34  * A Java parser generator. This class generates the source code files
35  * needed for a Java parser.
36  *
37  * @author Per Cederberg, <per at percederberg dot net>
38  * @version 1.0
39  */

40 public class JavaParserGenerator extends ParserGenerator {
41
42     /**
43      * The fully qualified Java package name.
44      */

45     private String JavaDoc basePackage = null;
46
47     /**
48      * The Java class name prefix.
49      */

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

55     private boolean publicAccess = false;
56
57     /**
58      * The Java class comment.
59      */

60     private String JavaDoc classComment = null;
61
62     /**
63      * Creates a new Java parser generator.
64      *
65      * @param grammar the grammar to use
66      */

67     public JavaParserGenerator(Grammar grammar) {
68         super(grammar);
69         initialize();
70     }
71
72     /**
73      * Initializes various instance variables.
74      */

75     private void initialize() {
76         StringBuffer JavaDoc buffer;
77         String JavaDoc str;
78
79         // Set base name
80
str = getGrammar().getFileName();
81         if (str.indexOf('/') >= 0) {
82             str = str.substring(str.lastIndexOf('/') + 1);
83         }
84         if (str.indexOf('\\') >= 0) {
85             str = str.substring(str.lastIndexOf('\\') + 1);
86         }
87         if (str.indexOf('.') > 0) {
88             str = str.substring(0, str.indexOf('.'));
89         }
90         if (Character.isLowerCase(str.charAt(0))) {
91             str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
92         }
93         baseName = str;
94
95         // Create class comment
96
buffer = new StringBuffer JavaDoc();
97         str = getGrammar().getDeclaration(Grammar.AUTHOR_DECLARATION);
98         if (str != null) {
99             buffer.append("@author ");
100             buffer.append(str);
101         }
102         str = getGrammar().getDeclaration(Grammar.VERSION_DECLARATION);
103         if (str != null) {
104             if (buffer.length() > 0) {
105                 buffer.append("\n");
106             }
107             buffer.append("@version ");
108             buffer.append(str);
109         }
110         classComment = buffer.toString();
111     }
112
113     /**
114      * Returns the Java package where the classes will be created.
115      *
116      * @return the fully qualified Java package name
117      */

118     public String JavaDoc getBasePackage() {
119         return basePackage;
120     }
121
122     /**
123      * Sets the Java package name where the classes will be created.
124      *
125      * @param pkg the fully qualified package name
126      */

127     public void setBasePackage(String JavaDoc pkg) {
128         this.basePackage = pkg;
129     }
130
131     /**
132      * Returns the Java class name prefix.
133      *
134      * @return the Java class name prefix
135      */

136     public String JavaDoc getBaseName() {
137         return baseName;
138     }
139
140     /**
141      * Sets the Java class name prefix.
142      *
143      * @param name the Java class name prefix
144      */

145     public void setBaseName(String JavaDoc name) {
146         this.baseName = name;
147     }
148
149     /**
150      * Returns the public access flag.
151      *
152      * @return true if the classes should have public access, or
153      * false otherwise
154      */

155     public boolean getPublicAccess() {
156         return publicAccess;
157     }
158
159     /**
160      * Sets the public access flag.
161      *
162      * @param flag the new public access flag value
163      */

164     public void setPublicAccess(boolean flag) {
165         publicAccess = flag;
166     }
167
168     /**
169      * Returns the Java code style to use.
170      *
171      * @return the Java code style to use
172      */

173     public CodeStyle getCodeStyle() {
174         return CodeStyle.JAVA;
175     }
176
177     /**
178      * Returns the Java class comment.
179      *
180      * @return the Java class comment
181      */

182     public String JavaDoc getClassComment() {
183         return classComment;
184     }
185
186     /**
187      * Writes the Java source code files.
188      *
189      * @throws IOException if the files couldn't be written correctly
190      */

191     public void write() throws IOException JavaDoc {
192         Grammar grammar = getGrammar();
193         JavaConstantsFile constants = new JavaConstantsFile(this);
194         JavaTokenizerFile tokenizer = new JavaTokenizerFile(this);
195         JavaParserFile parser = new JavaParserFile(this, tokenizer);
196         JavaAnalyzerFile analyzer = new JavaAnalyzerFile(this);
197         TokenPattern token;
198         ProductionPattern production;
199         int i;
200
201         // Create token declarations
202
for (i = 0; i < grammar.getTokenPatternCount(); i++) {
203             token = grammar.getTokenPattern(i);
204             constants.addToken(token);
205             tokenizer.addToken(token, constants);
206             analyzer.addToken(token, constants);
207         }
208
209         // Create production constants
210
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
211             production = grammar.getProductionPattern(i);
212             constants.addProduction(production);
213             parser.addProductionConstant(production);
214             analyzer.addProduction(production, constants);
215         }
216
217         // Create production definitions
218
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
219             production = grammar.getProductionPattern(i);
220             parser.addProduction(production, constants);
221         }
222
223         // Write source code files
224
constants.writeCode();
225         tokenizer.writeCode();
226         parser.writeCode();
227         analyzer.writeCode();
228     }
229
230     /**
231      * Creates a Java file in the correct base directory. The package
232      * will be set if applicable.
233      *
234      * @return a new Java file
235      */

236     public JavaFile createJavaFile() {
237         if (basePackage == null) {
238             return new JavaFile(getBaseDir());
239         } else {
240             return new JavaFile(getBaseDir(),
241                                 new JavaPackage(getBasePackage()));
242         }
243     }
244 }
245
Popular Tags