KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSharpConstantsFile.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.csharp.CSharpComment;
28 import net.percederberg.grammatica.code.csharp.CSharpEnumeration;
29 import net.percederberg.grammatica.code.csharp.CSharpFile;
30 import net.percederberg.grammatica.code.csharp.CSharpNamespace;
31 import net.percederberg.grammatica.parser.ProductionPattern;
32 import net.percederberg.grammatica.parser.TokenPattern;
33
34 /**
35  * The C# constants file generator. This class encapsulates all the
36  * C# code necessary for creating a constants enumeration file.
37  *
38  * @author Per Cederberg, <per at percederberg dot net>
39  * @version 1.0
40  */

41 class CSharpConstantsFile {
42
43     /**
44      * The enumeration comment.
45      */

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

53     private CSharpParserGenerator gen;
54
55     /**
56      * The file to write.
57      */

58     private CSharpFile file;
59
60     /**
61      * The enumeration declaration.
62      */

63     private CSharpEnumeration enm;
64
65     /**
66      * The mapping from id to constant name. This map contains all
67      * tokens and productions added to the file.
68      */

69     private HashMap JavaDoc constantNames = new HashMap JavaDoc();
70
71     /**
72      * Creates a new constants file.
73      *
74      * @param gen the parser generator to use
75      */

76     public CSharpConstantsFile(CSharpParserGenerator gen) {
77         String JavaDoc name = gen.getBaseName() + "Constants";
78         int modifiers;
79
80         this.gen = gen;
81         this.file = new CSharpFile(gen.getBaseDir(), name);
82         if (gen.getPublicAccess()) {
83             modifiers = CSharpEnumeration.PUBLIC;
84         } else {
85             modifiers = CSharpEnumeration.INTERNAL;
86         }
87         this.enm = new CSharpEnumeration(modifiers, name);
88         initializeCode();
89     }
90
91     /**
92      * Initializes the source code objects.
93      */

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

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

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

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

168     public void writeCode() throws IOException JavaDoc {
169         file.writeCode(gen.getCodeStyle());
170     }
171 }
172
Popular Tags