KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaConstantsFile.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 import java.util.HashMap JavaDoc;
26
27 import net.percederberg.grammatica.code.java.JavaComment;
28 import net.percederberg.grammatica.code.java.JavaFile;
29 import net.percederberg.grammatica.code.java.JavaInterface;
30 import net.percederberg.grammatica.code.java.JavaVariable;
31 import net.percederberg.grammatica.parser.ProductionPattern;
32 import net.percederberg.grammatica.parser.TokenPattern;
33
34 /**
35  * The Java constants file generator. This class encapsulates all the
36  * Java code necessary for creating a constants interface file.
37  *
38  * @author Per Cederberg, <per at percederberg dot net>
39  * @version 1.0
40  */

41 class JavaConstantsFile {
42
43     /**
44      * The interface comment.
45      */

46     private static final String JavaDoc TYPE_COMMENT =
47         "An interface with constants for the parser and tokenizer.";
48
49     /**
50      * The token constant comment.
51      */

52     private static final String JavaDoc TOKEN_COMMENT =
53         "A token identity constant.";
54
55     /**
56      * The production constant comment.
57      */

58     private static final String JavaDoc PRODUCTION_COMMENT =
59         "A production node identity constant.";
60
61     /**
62      * The Java parser generator.
63      */

64     private JavaParserGenerator gen;
65
66     /**
67      * The Java file to write.
68      */

69     private JavaFile file;
70
71     /**
72      * The Java constants interface.
73      */

74     private JavaInterface ifc;
75
76     /**
77      * The mapping from id to constant name. This map contains all
78      * tokens and productions added to the file.
79      */

80     private HashMap JavaDoc constantNames = new HashMap JavaDoc();
81
82     /**
83      * Creates a new constants file.
84      *
85      * @param gen the parser generator to use
86      */

87     public JavaConstantsFile(JavaParserGenerator gen) {
88         int modifiers;
89
90         this.gen = gen;
91         this.file = gen.createJavaFile();
92         if (gen.getPublicAccess()) {
93             modifiers = JavaInterface.PUBLIC;
94         } else {
95             modifiers = JavaInterface.PACKAGE_LOCAL;
96         }
97         this.ifc = new JavaInterface(modifiers,
98                                      gen.getBaseName() + "Constants");
99         initializeCode();
100     }
101
102     /**
103      * Initializes the source code objects.
104      */

105     private void initializeCode() {
106         String JavaDoc str;
107
108         // Add interface
109
file.addInterface(ifc);
110
111         // Add file comment
112
str = file.toString() + "\n\n" + gen.getFileComment();
113         file.addComment(new JavaComment(JavaComment.BLOCK, str));
114
115         // Add interface comment
116
str = TYPE_COMMENT;
117         if (gen.getClassComment() != null) {
118             str += "\n\n" + gen.getClassComment();
119         }
120         ifc.addComment(new JavaComment(str));
121     }
122
123     /**
124      * Adds a token constant definition to this file.
125      *
126      * @param pattern the token pattern
127      */

128     public void addToken(TokenPattern pattern) {
129         String JavaDoc constant;
130         JavaVariable var;
131         int modifiers;
132
133         constant = gen.getCodeStyle().getUpperCase(pattern.getName());
134         modifiers = JavaVariable.PUBLIC + JavaVariable.STATIC +
135                     JavaVariable.FINAL;
136         var = new JavaVariable(modifiers,
137                                "int",
138                                constant,
139                                "" + pattern.getId());
140         var.addComment(new JavaComment(TOKEN_COMMENT));
141         ifc.addVariable(var);
142         constantNames.put(new Integer JavaDoc(pattern.getId()), constant);
143     }
144
145     /**
146      * Adds a production constant definition to this file. This method
147      * checks if the production pattern has already been added.
148      *
149      * @param pattern the production pattern
150      */

151     public void addProduction(ProductionPattern pattern) {
152         String JavaDoc constant;
153         JavaVariable var;
154         int modifiers;
155
156         if (!pattern.isSynthetic()) {
157             constant = gen.getCodeStyle().getUpperCase(pattern.getName());
158             modifiers = JavaVariable.PUBLIC + JavaVariable.STATIC +
159                         JavaVariable.FINAL;
160             var = new JavaVariable(modifiers,
161                                    "int",
162                                    constant,
163                                    "" + pattern.getId());
164             var.addComment(new JavaComment(PRODUCTION_COMMENT));
165             ifc.addVariable(var);
166             constantNames.put(new Integer JavaDoc(pattern.getId()), constant);
167         }
168     }
169
170     /**
171      * Creates source code for accessing one of the constants in this
172      * file.
173      *
174      * @param id the node type (pattern) id
175      *
176      * @return the constant name, or
177      * null if not found
178      */

179     public String JavaDoc getConstant(int id) {
180         String JavaDoc name = (String JavaDoc) constantNames.get(new Integer JavaDoc(id));
181
182         if (name == null) {
183             return null;
184         } else {
185             return ifc.toString() + "." + name;
186         }
187     }
188
189     /**
190      * Writes the file source code.
191      *
192      * @throws IOException if the output file couldn't be created
193      * correctly
194      */

195     public void writeCode() throws IOException JavaDoc {
196         file.writeCode(gen.getCodeStyle());
197     }
198 }
199
Popular Tags