1 19 20 package org.netbeans.modules.lexer.gen; 21 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import org.netbeans.spi.lexer.util.LexerUtilities; 30 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities; 31 32 46 47 public class LanguageData { 48 49 public static final Comparator IDS_FIELD_NAME_COMPARATOR = new IdsFieldNameComparator(); 50 51 public static final Comparator IDS_INT_ID_COMPARATOR = new IdsIntIdComparator(); 52 53 private TokenTypes tokenTypes; 54 55 private String languageClassName; 56 57 private String lexerClassName; 58 59 private List ids; 60 61 private List unmodifiableIds; 62 63 private int uniqueIntId; 64 65 public LanguageData() { 66 69 uniqueIntId = 1; 70 ids = new ArrayList (); 71 unmodifiableIds = Collections.unmodifiableList(ids); 72 } 73 74 77 public final TokenTypes getTokenTypes() { 78 return tokenTypes; 79 } 80 81 85 public void registerTokenTypes(TokenTypes tokenTypes) { 86 this.tokenTypes = tokenTypes; 87 88 tokenTypes.updateData(this); 90 } 91 92 95 public String getLanguageClassName() { 96 return languageClassName; 97 } 98 99 public void setLanguageClassName(String languageClassName) { 100 this.languageClassName = languageClassName; 101 } 102 103 106 public String getLexerClassName() { 107 return lexerClassName; 108 } 109 110 public void setLexerClassName(String lexerClassName) { 111 this.lexerClassName = lexerClassName; 112 } 113 114 119 public List getIds() { 120 return unmodifiableIds; 121 } 122 123 129 public List getSortedIds(Comparator c) { 130 List idsCopy = new ArrayList (ids); 131 Collections.sort(idsCopy, c); 132 return idsCopy; 133 } 134 135 139 public MutableTokenId newId(String name) { 140 if (findId(name) != null) { 141 throw new IllegalArgumentException ("Id named " + name + " already exists."); 142 } 143 144 MutableTokenId id = createId(name); 145 ids.add(id); 146 147 return id; 148 } 149 150 153 public void remove(MutableTokenId id) { 154 for (Iterator it = ids.iterator(); it.hasNext();) { 155 if (id == it.next()) { 156 it.remove(); 157 return; 158 } 159 } 160 throw new IllegalArgumentException ("id=" + id + " not found"); 161 } 162 163 167 public MutableTokenId findId(String name) { 168 for (Iterator it = getIds().iterator(); it.hasNext();) { 169 MutableTokenId id = (MutableTokenId)it.next(); 170 if (name.equals(id.getName())) { 171 return id; 172 } 173 } 174 return null; 175 } 176 177 182 public MutableTokenId findId(int intId) { 183 List ids = getIds(); 184 int cnt = ids.size(); 185 for (int i = 0; i < cnt; i++) { 186 MutableTokenId id = (MutableTokenId)ids.get(i); 187 if (id.getIntId() == intId) { 188 return id; 189 } 190 } 191 return null; 192 } 193 194 200 public MutableTokenId findIdByTokenTypeName(String tokenTypeName) { 201 List ids = getIds(); 202 int cnt = ids.size(); 203 for (int i = 0; i < cnt; i++) { 204 MutableTokenId id = (MutableTokenId)ids.get(i); 205 if (tokenTypeName.equals(id.getTokenTypeName())) { 206 return id; 207 } 208 } 209 return null; 210 } 211 212 220 public int findMaxIntId() { 221 List ids = getIds(); 222 int cnt = ids.size(); 223 int maxIntId = 0; 224 for (int i = 0; i < cnt; i++) { 225 MutableTokenId id = (MutableTokenId)ids.get(i); 226 maxIntId = Math.max(maxIntId, id.getIntId()); 227 } 228 229 return maxIntId; 230 } 231 232 235 public int uniqueIntId() { 236 return uniqueIntId++; 237 } 238 239 244 public void skipIntId(int intId) { 245 uniqueIntId = Math.max(uniqueIntId, ++intId); 246 } 247 248 253 protected MutableTokenId createId(String name) { 254 return new MutableTokenId(this, name); 255 } 256 257 263 public void updateUnassignedIntIds() { 264 for (Iterator it = getIds().iterator(); it.hasNext();) { 265 MutableTokenId id = (MutableTokenId)it.next(); 266 if (id.getIntId() == -1) { 267 id.assignUniqueIntId(); 268 } 269 } 270 } 271 272 276 public void check() { 277 Map info = new HashMap (); 278 279 for (Iterator it = getIds().iterator(); it.hasNext();) { 280 MutableTokenId id = (MutableTokenId)it.next(); 281 int intId = id.getIntId(); 282 if (intId < 0) { 283 throw new IllegalStateException ( 284 "Id " + id.getName() 285 + " has invalid intId=" + intId 286 + "\nLanguage data dump follows:\n" 287 + toString() 288 289 ); 290 } 291 292 MutableTokenId dupIntId = (MutableTokenId)info.put(new Integer (intId), id); 293 if (dupIntId != null) { 294 throw new IllegalStateException ("Ids " + id.getName() 295 + " and " + dupIntId.getName() 296 + " have the same intId=" + intId 297 + "\nLanguage data dump follows:\n" 298 + toString()); 299 } 300 } 301 } 302 303 public String toString() { 304 StringBuffer sb = new StringBuffer (); 305 List mtids = getIds(); 306 int mtidsCount = mtids.size(); 307 for (int i = 0; i < mtidsCount; i++) { 308 sb.append(mtids.get(i)); 309 sb.append('\n'); 310 } 311 312 return sb.toString(); 313 } 314 315 316 322 public String createDescription() { 323 324 StringBuffer sb = new StringBuffer (); 325 appendDescriptionStart(sb); 326 327 for (Iterator idsIterator = getIds().iterator(); 328 idsIterator.hasNext();) { 329 330 MutableTokenId id = (MutableTokenId)idsIterator.next(); 331 String idName = id.getName(); 332 if (idName != null) { 333 LexerGenUtilities.appendSpaces(sb, 4); 334 sb.append("<TokenId name=\""); 335 sb.append(LexerGenUtilities.idToLowerCase(idName)); 336 sb.append("\">\n"); 337 LexerGenUtilities.appendSpaces(sb, 4); 338 sb.append("</TokenId>\n"); 339 } 340 } 341 342 appendDescriptionEnd(sb); 343 344 return sb.toString(); 345 } 346 347 protected void appendDescriptionStart(StringBuffer sb) { 348 sb.append("<?xml version=\"1.0\"?>\n"); 349 sb.append("<!DOCTYPE Language SYSTEM "); 350 sb.append("\"???/src/org/netbeans/lexer/language.dtd\">\n"); 351 sb.append("<Language>\n"); 352 } 353 354 protected void appendDescriptionEnd(StringBuffer sb) { 355 sb.append("</Language>\n"); 356 } 357 358 private static final class IdsFieldNameComparator implements Comparator { 359 360 public int compare(Object o1, Object o2) { 361 MutableTokenId id1 = (MutableTokenId)o1; 362 MutableTokenId id2 = (MutableTokenId)o2; 363 return id1.getFieldName().compareTo(id2.getFieldName()); 364 } 365 366 } 367 368 private static final class IdsIntIdComparator implements Comparator { 369 370 public int compare(Object o1, Object o2) { 371 MutableTokenId id1 = (MutableTokenId)o1; 372 MutableTokenId id2 = (MutableTokenId)o2; 373 return id1.getIntId() - id2.getIntId(); 374 } 375 376 } 377 378 } 379 380 | Popular Tags |