KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * VisualBasicConstantsFile.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 import java.util.HashMap JavaDoc;
27
28 import net.percederberg.grammatica.code.visualbasic.VisualBasicComment;
29 import net.percederberg.grammatica.code.visualbasic.VisualBasicEnumeration;
30 import net.percederberg.grammatica.code.visualbasic.VisualBasicFile;
31 import net.percederberg.grammatica.code.visualbasic.VisualBasicNamespace;
32 import net.percederberg.grammatica.parser.ProductionPattern;
33 import net.percederberg.grammatica.parser.TokenPattern;
34
35 /**
36  * The Visual Basic constants file generator. This class encapsulates
37  * all the Visual Basic (.NET) code necessary for creating a constants
38  * enumeration file.
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 VisualBasicConstantsFile {
46
47     /**
48      * The enumeration comment.
49      */

50     private static final String JavaDoc TYPE_COMMENT =
51         "<remarks>An enumeration with token and production node\n" +
52         "constants.</remarks>";
53
54     /**
55      * The parser generator.
56      */

57     private VisualBasicParserGenerator gen;
58
59     /**
60      * The file to write.
61      */

62     private VisualBasicFile file;
63
64     /**
65      * The enumeration declaration.
66      */

67     private VisualBasicEnumeration enm;
68
69     /**
70      * The mapping from id to constant name. This map contains all
71      * tokens and productions added to the file.
72      */

73     private HashMap JavaDoc constantNames = new HashMap JavaDoc();
74
75     /**
76      * Creates a new constants file.
77      *
78      * @param gen the parser generator to use
79      */

80     public VisualBasicConstantsFile(VisualBasicParserGenerator gen) {
81         String JavaDoc name = gen.getBaseName() + "Constants";
82         int modifiers;
83
84         this.gen = gen;
85         this.file = new VisualBasicFile(gen.getBaseDir(), name);
86         if (gen.getPublicAccess()) {
87             modifiers = VisualBasicEnumeration.PUBLIC;
88         } else {
89             modifiers = VisualBasicEnumeration.FRIEND;
90         }
91         this.enm = new VisualBasicEnumeration(modifiers, name);
92         initializeCode();
93     }
94
95     /**
96      * Initializes the source code objects.
97      */

98     private void initializeCode() {
99         String JavaDoc str;
100         VisualBasicNamespace n;
101
102         // Add namespace
103
if (gen.getNamespace() == null) {
104             file.addEnumeration(enm);
105         } else {
106             n = new VisualBasicNamespace(gen.getNamespace());
107             n.addEnumeration(enm);
108             file.addNamespace(n);
109         }
110
111         // Add file comment
112
str = file.toString() + "\n\n" + gen.getFileComment();
113         file.addComment(new VisualBasicComment(VisualBasicComment.SINGLELINE,
114                                                str));
115
116         // Add type comment
117
enm.addComment(new VisualBasicComment(TYPE_COMMENT));
118     }
119
120     /**
121      * Adds a token constant definition to this file.
122      *
123      * @param pattern the token pattern
124      */

125     public void addToken(TokenPattern pattern) {
126         String JavaDoc constant;
127
128         constant = gen.getCodeStyle().getUpperCase(pattern.getName());
129         enm.addConstant(constant, String.valueOf(pattern.getId()));
130         constantNames.put(new Integer JavaDoc(pattern.getId()), constant);
131     }
132
133     /**
134      * Adds a production constant definition to this file. This method
135      * checks if the production pattern has already been added.
136      *
137      * @param pattern the production pattern
138      */

139     public void addProduction(ProductionPattern pattern) {
140         String JavaDoc constant;
141
142         if (!pattern.isSynthetic()) {
143             constant = gen.getCodeStyle().getUpperCase(pattern.getName());
144             enm.addConstant(constant, String.valueOf(pattern.getId()));
145             constantNames.put(new Integer JavaDoc(pattern.getId()), constant);
146         }
147     }
148
149     /**
150      * Creates source code for accessing one of the constants in this
151      * file.
152      *
153      * @param id the node type (pattern) id
154      *
155      * @return the constant name, or
156      * null if not found
157      */

158     public String JavaDoc getConstant(int id) {
159         String JavaDoc name = (String JavaDoc) constantNames.get(new Integer JavaDoc(id));
160
161         if (name == null) {
162             return null;
163         } else {
164             return enm.toString() + "." + name;
165         }
166     }
167
168     /**
169      * Writes the file source code.
170      *
171      * @throws IOException if the output file couldn't be created
172      * correctly
173      */

174     public void writeCode() throws IOException JavaDoc {
175         file.writeCode(gen.getCodeStyle());
176     }
177 }
178
Popular Tags