1 17 18 package org.sablecc.sablecc; 19 20 import java.util.LinkedList ; 21 import java.util.List ; 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 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 <String , Declaration> declarationMap = new TreeMap <String , Declaration>(); 52 53 private List <HelperDeclaration> helpers = new LinkedList <HelperDeclaration>(); 54 55 private List <SimpleTokenDeclaration> simpleTokens = new LinkedList <SimpleTokenDeclaration>(); 56 57 private List <SelectionTokenDeclaration> selectionTokens = new LinkedList <SelectionTokenDeclaration>(); 58 59 private List <GroupDeclaration> groups = new LinkedList <GroupDeclaration>(); 60 61 private SortedSet <String > methodNames = new TreeSet <String >(); 62 63 private SortedSet <String > stateNames = new TreeSet <String >(); 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 |