KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSharpParserGenerator.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.parser.ProductionPattern;
29 import net.percederberg.grammatica.parser.TokenPattern;
30
31 /**
32  * A C# parser generator. This class generates the source code files
33  * needed for a C# parser.
34  *
35  * @author Per Cederberg, <per at percederberg dot net>
36  * @version 1.0
37  */

38 public class CSharpParserGenerator extends ParserGenerator {
39
40     /**
41      * The class name prefix.
42      */

43     private String JavaDoc baseName = null;
44
45     /**
46      * The namespace to use.
47      */

48     private String JavaDoc namespace = null;
49
50     /**
51      * The public class and interface access flag.
52      */

53     private boolean publicAccess = false;
54
55     /**
56      * Creates a new parser generator.
57      *
58      * @param grammar the grammar to use
59      */

60     public CSharpParserGenerator(Grammar grammar) {
61         super(grammar);
62         initialize();
63     }
64
65     /**
66      * Initializes various instance variables.
67      */

68     private void initialize() {
69         String JavaDoc str;
70
71         // Set base name
72
str = getGrammar().getFileName();
73         if (str.indexOf('/') >= 0) {
74             str = str.substring(str.lastIndexOf('/') + 1);
75         }
76         if (str.indexOf('\\') >= 0) {
77             str = str.substring(str.lastIndexOf('\\') + 1);
78         }
79         if (str.indexOf('.') > 0) {
80             str = str.substring(0, str.indexOf('.'));
81         }
82         if (Character.isLowerCase(str.charAt(0))) {
83             str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
84         }
85         baseName = str;
86     }
87
88     /**
89      * Returns the namespace used for the classes.
90      *
91      * @return the fully qualified namespace, or
92      * null for none
93      */

94     public String JavaDoc getNamespace() {
95         return namespace;
96     }
97
98     /**
99      * Sets the namespace to use for the classes.
100      *
101      * @param namespace the fully qualified namespace
102      */

103     public void setNamespace(String JavaDoc namespace) {
104         this.namespace = namespace;
105     }
106
107     /**
108      * Returns the class name prefix.
109      *
110      * @return the class name prefix
111      */

112     public String JavaDoc getBaseName() {
113         return baseName;
114     }
115
116     /**
117      * Sets the class name prefix.
118      *
119      * @param name the class name prefix
120      */

121     public void setBaseName(String JavaDoc name) {
122         this.baseName = name;
123     }
124
125     /**
126      * Returns the public access flag.
127      *
128      * @return true if the classes should have public access, or
129      * false otherwise
130      */

131     public boolean getPublicAccess() {
132         return publicAccess;
133     }
134
135     /**
136      * Sets the public access flag.
137      *
138      * @param flag the new public access flag value
139      */

140     public void setPublicAccess(boolean flag) {
141         publicAccess = flag;
142     }
143
144     /**
145      * Returns the code style to use.
146      *
147      * @return the code style to use
148      */

149     public CodeStyle getCodeStyle() {
150         return CodeStyle.CSHARP;
151     }
152
153     /**
154      * Writes the source code files.
155      *
156      * @throws IOException if the files couldn't be written correctly
157      */

158     public void write() throws IOException JavaDoc {
159         Grammar grammar = getGrammar();
160         CSharpConstantsFile constants = new CSharpConstantsFile(this);
161         CSharpTokenizerFile tokenizer = new CSharpTokenizerFile(this);
162         CSharpParserFile parser = new CSharpParserFile(this, tokenizer);
163         CSharpAnalyzerFile analyzer = new CSharpAnalyzerFile(this);
164         TokenPattern token;
165         ProductionPattern production;
166         int i;
167
168         // Create token declarations
169
for (i = 0; i < grammar.getTokenPatternCount(); i++) {
170             token = grammar.getTokenPattern(i);
171             constants.addToken(token);
172             tokenizer.addToken(token, constants);
173             analyzer.addToken(token, constants);
174         }
175
176         // Create production constants
177
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
178             production = grammar.getProductionPattern(i);
179             constants.addProduction(production);
180             parser.addProductionConstant(production);
181             analyzer.addProduction(production, constants);
182         }
183
184         // Create production declarations
185
for (i = 0; i < grammar.getProductionPatternCount(); i++) {
186             production = grammar.getProductionPattern(i);
187             parser.addProduction(production, constants);
188         }
189
190         // Write source code files
191
constants.writeCode();
192         tokenizer.writeCode();
193         parser.writeCode();
194         analyzer.writeCode();
195     }
196 }
197
Popular Tags