1 17 18 package org.sablecc.sablecc.structure; 19 20 import java.util.Collections ; 21 import java.util.Comparator ; 22 import java.util.SortedMap ; 23 import java.util.SortedSet ; 24 import java.util.TreeMap ; 25 import java.util.TreeSet ; 26 27 import org.sablecc.sablecc.exception.InternalException; 28 29 public class Language { 30 31 private final String name; 32 33 private SortedSet <Token> tokens = new TreeSet <Token>(); 34 35 private SortedMap <String , Token> tokenMap = new TreeMap <String , Token>(); 36 37 private SortedSet <Group> groups = new TreeSet <Group>(); 38 39 private SortedMap <String , Group> groupMap = new TreeMap <String , Group>( 40 stringComparator); 41 42 private SortedSet <Selector> selectors = new TreeSet <Selector>(); 43 44 private SortedMap <String , Selector> selectorMap = new TreeMap <String , Selector>(); 45 46 private SortedSet <Investigator> investigators = new TreeSet <Investigator>(); 47 48 private SortedMap <String , Investigator> investigatorMap = new TreeMap <String , Investigator>(); 49 50 private SortedSet <State> states = new TreeSet <State>(); 51 52 private SortedMap <String , State> stateMap = new TreeMap <String , State>( 53 stringComparator); 54 55 private boolean isStable = false; 56 57 58 private static final Comparator <String > stringComparator = new Comparator <String >() { 59 60 public int compare( 62 String string1, 63 String 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 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 getName() { 88 89 return this.name; 90 } 91 92 public Token getToken( 93 String name) { 94 95 return this.tokenMap.get(name); 96 } 97 98 public SortedSet <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 name) { 109 110 return this.groupMap.get(name); 111 } 112 113 public SortedSet <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 name) { 124 125 return this.selectorMap.get(name); 126 } 127 128 public SortedSet <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 name) { 139 140 return this.investigatorMap.get(name); 141 } 142 143 public SortedSet <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 name) { 159 160 return this.stateMap.get(name); 161 } 162 163 public SortedSet <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 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 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 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 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 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 |