KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > structure > Language


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.structure;
19
20 import java.util.Collections JavaDoc;
21 import java.util.Comparator 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
29 public class Language {
30
31     private final String JavaDoc name;
32
33     private SortedSet JavaDoc<Token> tokens = new TreeSet JavaDoc<Token>();
34
35     private SortedMap JavaDoc<String JavaDoc, Token> tokenMap = new TreeMap JavaDoc<String JavaDoc, Token>();
36
37     private SortedSet JavaDoc<Group> groups = new TreeSet JavaDoc<Group>();
38
39     private SortedMap JavaDoc<String JavaDoc, Group> groupMap = new TreeMap JavaDoc<String JavaDoc, Group>(
40             stringComparator);
41
42     private SortedSet JavaDoc<Selector> selectors = new TreeSet JavaDoc<Selector>();
43
44     private SortedMap JavaDoc<String JavaDoc, Selector> selectorMap = new TreeMap JavaDoc<String JavaDoc, Selector>();
45
46     private SortedSet JavaDoc<Investigator> investigators = new TreeSet JavaDoc<Investigator>();
47
48     private SortedMap JavaDoc<String JavaDoc, Investigator> investigatorMap = new TreeMap JavaDoc<String JavaDoc, Investigator>();
49
50     private SortedSet JavaDoc<State> states = new TreeSet JavaDoc<State>();
51
52     private SortedMap JavaDoc<String JavaDoc, State> stateMap = new TreeMap JavaDoc<String JavaDoc, State>(
53             stringComparator);
54
55     private boolean isStable = false;
56
57     /** A comparator for strings. */
58     private static final Comparator JavaDoc<String JavaDoc> stringComparator = new Comparator JavaDoc<String JavaDoc>() {
59
60         // allows comparison of null strings
61
public int compare(
62                 String JavaDoc string1,
63                 String JavaDoc string2) {
64
65             if (string1 == null) {
66                 return string2 == null ? 0 : -1;
67             }
68
69             if (string2 == null) {
70                 return 1;
71             }
72
73             return string1.compareTo(string2);
74         }
75     };
76
77     public Language(
78             String JavaDoc name) {
79
80         if (name == null) {
81             throw new InternalException("name may not be null");
82         }
83
84         this.name = name;
85     }
86
87     public String JavaDoc getName() {
88
89         return this.name;
90     }
91
92     public Token getToken(
93             String JavaDoc name) {
94
95         return this.tokenMap.get(name);
96     }
97
98     public SortedSet JavaDoc<Token> getTokens() {
99
100         if (!this.isStable) {
101             throw new InternalException("this language is not stable yet");
102         }
103
104         return this.tokens;
105     }
106
107     public Group getGroup(
108             String JavaDoc name) {
109
110         return this.groupMap.get(name);
111     }
112
113     public SortedSet JavaDoc<Group> getGroups() {
114
115         if (!this.isStable) {
116             throw new InternalException("this language is not stable yet");
117         }
118
119         return this.groups;
120     }
121
122     public Selector getSelector(
123             String JavaDoc name) {
124
125         return this.selectorMap.get(name);
126     }
127
128     public SortedSet JavaDoc<Selector> getSelectors() {
129
130         if (!this.isStable) {
131             throw new InternalException("this language is not stable yet");
132         }
133
134         return this.selectors;
135     }
136
137     public Investigator getInvestigator(
138             String JavaDoc name) {
139
140         return this.investigatorMap.get(name);
141     }
142
143     public SortedSet JavaDoc<Investigator> getInvestigators() {
144
145         if (!this.isStable) {
146             throw new InternalException("this language is not stable yet");
147         }
148
149         return this.investigators;
150     }
151
152     public boolean isStable() {
153
154         return this.isStable;
155     }
156
157     public State getState(
158             String JavaDoc name) {
159
160         return this.stateMap.get(name);
161     }
162
163     public SortedSet JavaDoc<State> getStates() {
164
165         if (!this.isStable) {
166             throw new InternalException("this language is not stable yet");
167         }
168
169         return this.states;
170     }
171
172     public void stabilize() {
173
174         if (this.isStable) {
175             throw new InternalException("this language is already stable");
176         }
177
178         for (Group group : this.groups) {
179             group.stabilize();
180         }
181
182         this.tokens = Collections.unmodifiableSortedSet(this.tokens);
183         this.tokenMap = Collections.unmodifiableSortedMap(this.tokenMap);
184         this.groups = Collections.unmodifiableSortedSet(this.groups);
185         this.groupMap = Collections.unmodifiableSortedMap(this.groupMap);
186         this.selectors = Collections.unmodifiableSortedSet(this.selectors);
187         this.selectorMap = Collections.unmodifiableSortedMap(this.selectorMap);
188         this.investigators = Collections
189                 .unmodifiableSortedSet(this.investigators);
190         this.investigatorMap = Collections
191                 .unmodifiableSortedMap(this.investigatorMap);
192         this.states = Collections.unmodifiableSortedSet(this.states);
193         this.stateMap = Collections.unmodifiableSortedMap(this.stateMap);
194
195         this.isStable = true;
196     }
197
198     public void addGroup(
199             String JavaDoc name) {
200
201         if (this.isStable) {
202             throw new InternalException("a stable language may not be modified");
203         }
204
205         if (this.groupMap.containsKey(name)) {
206             throw new InternalException(
207                     "this language already includes the group");
208         }
209
210         Group group = new Group(name);
211
212         this.groups.add(group);
213         this.groupMap.put(name, group);
214     }
215
216     public void addToken(
217             String JavaDoc name,
218             Group group) {
219
220         if (this.isStable) {
221             throw new InternalException("a stable language may not be modified");
222         }
223
224         if (this.tokenMap.containsKey(name)) {
225             throw new InternalException(
226                     "this language already includes the token");
227         }
228
229         Token token = new Token(name, group);
230
231         this.tokens.add(token);
232         this.tokenMap.put(name, token);
233     }
234
235     public void addSelector(
236             String JavaDoc name) {
237
238         if (this.isStable) {
239             throw new InternalException("a stable language may not be modified");
240         }
241
242         if (this.selectorMap.containsKey(name)) {
243             throw new InternalException(
244                     "this language already includes the selector");
245         }
246
247         Selector selector = new Selector(name);
248
249         this.selectors.add(selector);
250         this.selectorMap.put(name, selector);
251     }
252
253     public void addInvestigator(
254             String JavaDoc name) {
255
256         if (this.isStable) {
257             throw new InternalException("a stable language may not be modified");
258         }
259
260         if (this.investigatorMap.containsKey(name)) {
261             throw new InternalException(
262                     "this language already includes the investigator");
263         }
264
265         Investigator investigator = new Investigator(name);
266
267         this.investigators.add(investigator);
268         this.investigatorMap.put(name, investigator);
269     }
270
271     public void addState(
272             String JavaDoc name) {
273
274         if (this.isStable) {
275             throw new InternalException("a stable language may not be modified");
276         }
277
278         if (this.stateMap.containsKey(name)) {
279             throw new InternalException(
280                     "this language already includes the state");
281         }
282
283         State state = new State(name);
284
285         this.states.add(state);
286         this.stateMap.put(name, state);
287     }
288 }
289
Popular Tags