KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > GlobalInformation


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.sablecc.sablecc;
19
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.SortedMap JavaDoc;
23 import java.util.SortedSet JavaDoc;
24 import java.util.TreeMap JavaDoc;
25 import java.util.TreeSet JavaDoc;
26
27 import org.sablecc.sablecc.exception.InternalException;
28 import org.sablecc.sablecc.exception.SemanticException;
29 import org.sablecc.sablecc.structure.Group;
30 import org.sablecc.sablecc.structure.Language;
31 import org.sablecc.sablecc.structure.Selector;
32 import org.sablecc.sablecc.structure.Token;
33 import org.sablecc.sablecc.syntax3.node.AGroup;
34 import org.sablecc.sablecc.syntax3.node.AHelper;
35 import org.sablecc.sablecc.syntax3.node.AInvestigator;
36 import org.sablecc.sablecc.syntax3.node.ASelection;
37 import org.sablecc.sablecc.syntax3.node.ASelectionToken;
38 import org.sablecc.sablecc.syntax3.node.ASelector;
39 import org.sablecc.sablecc.syntax3.node.ASimpleToken;
40 import org.sablecc.sablecc.syntax3.node.Start;
41 import org.sablecc.sablecc.syntax3.node.TIdentifier;
42
43 public class GlobalInformation {
44
45     private final Verbosity verbosity;
46
47     private final Start ast;
48
49     private Language language;
50
51     private SortedMap JavaDoc<String JavaDoc, Declaration> declarationMap = new TreeMap JavaDoc<String JavaDoc, Declaration>();
52
53     private List JavaDoc<HelperDeclaration> helpers = new LinkedList JavaDoc<HelperDeclaration>();
54
55     private List JavaDoc<SimpleTokenDeclaration> simpleTokens = new LinkedList JavaDoc<SimpleTokenDeclaration>();
56
57     private List JavaDoc<SelectionTokenDeclaration> selectionTokens = new LinkedList JavaDoc<SelectionTokenDeclaration>();
58
59     private List JavaDoc<GroupDeclaration> groups = new LinkedList JavaDoc<GroupDeclaration>();
60
61     private SortedSet JavaDoc<String JavaDoc> methodNames = new TreeSet JavaDoc<String JavaDoc>();
62
63     private SortedSet JavaDoc<String JavaDoc> stateNames = new TreeSet JavaDoc<String JavaDoc>();
64
65     public GlobalInformation(
66             Verbosity verbosity,
67             Start ast) {
68
69         if (verbosity == null) {
70             throw new InternalException("verbosity may not be null");
71         }
72
73         if (ast == null) {
74             throw new InternalException("ast may not be null");
75         }
76
77         this.verbosity = verbosity;
78         this.ast = ast;
79     }
80
81     public Verbosity getVerbosity() {
82
83         return this.verbosity;
84     }
85
86     public Start getAst() {
87
88         return this.ast;
89     }
90
91     public Language getLanguage() {
92
93         if (this.language == null) {
94             throw new InternalException("language is not set yet");
95         }
96
97         return this.language;
98     }
99
100     public void setLanguage(
101             Language language) {
102
103         if (language == null) {
104             throw new InternalException("language may not be null");
105         }
106
107         if (this.language != null) {
108             throw new InternalException("language is already set");
109         }
110
111         this.language = language;
112     }
113
114     public void addHelper(
115             AHelper aHelper)
116             throws SemanticException {
117
118         if (aHelper == null) {
119             throw new InternalException("aHelper may not be null");
120         }
121
122         if (this.declarationMap.containsKey(aHelper.getName().getText())) {
123             throw new SemanticException("redeclaration of '"
124                     + aHelper.getName().getText() + "'", aHelper.getName());
125         }
126
127         HelperDeclaration helperDeclaration = new HelperDeclaration(aHelper);
128
129         this.declarationMap.put(aHelper.getName().getText(), helperDeclaration);
130         this.helpers.add(helperDeclaration);
131     }
132
133     public void addGroup(
134             AGroup aGroup)
135             throws SemanticException {
136
137         if (aGroup == null) {
138             throw new InternalException("aGroup may not be null");
139         }
140
141         if (this.declarationMap.containsKey(aGroup.getName().getText())) {
142             throw new SemanticException("redeclaration of '"
143                     + aGroup.getName().getText() + "'", aGroup.getName());
144         }
145
146         GroupDeclaration groupDeclaration = new GroupDeclaration(aGroup);
147
148         this.declarationMap.put(aGroup.getName().getText(), groupDeclaration);
149         this.groups.add(groupDeclaration);
150
151         this.language.addGroup(aGroup.getName().getText());
152     }
153
154     public void addSimpleToken(
155             ASimpleToken aSimpleToken,
156             AGroup aGroup)
157             throws SemanticException {
158
159         if (aSimpleToken == null) {
160             throw new InternalException("aSimpleToken may not be null");
161         }
162
163         if (this.declarationMap.containsKey(aSimpleToken.getName().getText())) {
164             throw new SemanticException("redeclaration of '"
165                     + aSimpleToken.getName().getText() + "'", aSimpleToken
166                     .getName());
167         }
168
169         SimpleTokenDeclaration simpleTokenDeclaration = new SimpleTokenDeclaration(
170                 aSimpleToken);
171
172         this.declarationMap.put(aSimpleToken.getName().getText(),
173                 simpleTokenDeclaration);
174         this.simpleTokens.add(simpleTokenDeclaration);
175
176         Group group;
177
178         if (aGroup == null) {
179             group = this.language.getGroup(null);
180         }
181         else {
182             group = this.language.getGroup(aGroup.getName().getText());
183         }
184
185         this.language.addToken(aSimpleToken.getName().getText(), group);
186     }
187
188     public void addSelectionToken(
189             ASelectionToken aSelectionToken,
190             AGroup aGroup)
191             throws SemanticException {
192
193         if (aSelectionToken == null) {
194             throw new InternalException("aSelectionToken may not be null");
195         }
196
197         ASelection aSelection = (ASelection) aSelectionToken.getSelection();
198         ASelector aSelector = (ASelector) aSelectionToken.getSelector();
199
200         SelectionTokenDeclaration selectionTokenDeclaration = new SelectionTokenDeclaration(
201                 aSelectionToken);
202
203         this.selectionTokens.add(selectionTokenDeclaration);
204
205         if (this.methodNames.contains(aSelector.getName().getText())) {
206             throw new SemanticException("redeclaration of '"
207                     + aSelector.getName().getText() + "'", aSelector.getName());
208         }
209
210         this.methodNames.add(aSelector.getName().getText());
211
212         this.language.addSelector(aSelector.getName().getText());
213         Selector selector = this.language.getSelector(aSelector.getName()
214                 .getText());
215
216         Group group;
217
218         if (aGroup == null) {
219             group = this.language.getGroup(null);
220         }
221         else {
222             group = this.language.getGroup(aGroup.getName().getText());
223         }
224
225         for (TIdentifier tokenName : aSelection.getNames()) {
226
227             if (this.declarationMap.containsKey(tokenName.getText())) {
228                 throw new SemanticException("redeclaration of '"
229                         + tokenName.getText() + "'", tokenName);
230             }
231
232             this.declarationMap.put(tokenName.getText(),
233                     selectionTokenDeclaration);
234
235             this.language.addToken(tokenName.getText(), group);
236             Token token = this.language.getToken(tokenName.getText());
237
238             selector.addToken(token);
239         }
240     }
241
242     public void addInvestigator(
243             AInvestigator aInvestigator)
244             throws SemanticException {
245
246         if (aInvestigator == null) {
247             throw new InternalException("aInvestigator may not be null");
248         }
249
250         if (this.methodNames.contains(aInvestigator.getName().getText())) {
251             throw new SemanticException("redeclaration of '"
252                     + aInvestigator.getName().getText() + "'", aInvestigator
253                     .getName());
254         }
255
256         this.methodNames.add(aInvestigator.getName().getText());
257
258         this.language.addInvestigator(aInvestigator.getName().getText());
259     }
260
261     public void addState(
262             TIdentifier stateName)
263             throws SemanticException {
264
265         if (stateName == null) {
266             throw new InternalException("stateName may not be null");
267         }
268
269         if (this.stateNames.contains(stateName.getText())) {
270             throw new SemanticException("redeclaration of '"
271                     + stateName.getText() + "'", stateName);
272         }
273
274         this.stateNames.add(stateName.getText());
275
276         this.language.addState(stateName.getText());
277     }
278 }
279
Popular Tags