1 19 20 package org.netbeans.modules.lexer.gen; 21 22 import java.lang.reflect.Field ; 23 import java.lang.SecurityException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.List ; 32 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities; 33 34 50 51 public class TokenTypes { 52 53 private final Class tokenTypesClass; 54 55 private boolean inspected; 56 57 58 protected final Map name2value = new HashMap (); 59 60 61 protected final Map value2name = new HashMap (); 62 63 public TokenTypes(Class tokenTypesClass) { 64 this.tokenTypesClass = tokenTypesClass; 65 } 66 67 74 protected void updateData(LanguageData languageData) { 75 inspect(); 76 77 for (Iterator it = tokenTypeNamesIterator(); it.hasNext();) { 78 String tokenTypeName = (String )it.next(); 79 MutableTokenId id = languageData.findIdByTokenTypeName(tokenTypeName); 80 81 if (id == null) { 82 String idName = LexerGenUtilities.idToLowerCase(tokenTypeName); 83 id = languageData.newId(idName); 84 id.updateByTokenType(tokenTypeName); 86 } else { 87 updateId(id); 88 } 89 } 90 } 91 92 98 protected void updateId(MutableTokenId id) { 99 String tokenTypeName = id.getTokenTypeName(); 100 if (tokenTypeName != null) { Integer value = getTokenTypeValue(tokenTypeName); 102 if (value == null) { 103 throw new IllegalArgumentException ("tokenTypeName=" + tokenTypeName 104 + " is not declared in " + getTokenTypesClass().getName()); 105 } 106 107 id.setIntId(value.intValue()); 109 } 110 } 111 112 public Class getTokenTypesClass() { 113 return tokenTypesClass; 114 } 115 116 120 public Integer getTokenTypeValue(String tokenTypeName) { 121 inspect(); 122 123 return (Integer )name2value.get(tokenTypeName); 124 } 125 126 public String getTokenTypeName(int tokenTypeValue) { 127 inspect(); 128 129 return (String )value2name.get(new Integer (tokenTypeValue)); 130 } 131 132 135 public Iterator tokenTypeNamesIterator() { 136 inspect(); 137 138 return name2value.keySet().iterator(); 139 } 140 141 142 public int findMaxTokenTypeValue() { 143 inspect(); 144 145 int maxValue = 0; 146 for (Iterator it = value2name.keySet().iterator(); it.hasNext();) { 147 Integer i = (Integer )it.next(); 148 maxValue = Math.max(maxValue, i.intValue()); 149 } 150 return maxValue; 151 } 152 153 166 protected boolean inspect() { 167 if (inspected) { 168 return false; 169 } 170 inspected = true; 171 172 try { 173 Field [] fields = getTokenTypesClass().getDeclaredFields(); 174 for (int i = 0; i < fields.length; i++) { 175 Field f = fields[i]; 176 if (f.getType() == int.class) { 177 int value = f.getInt(null); 178 String fieldName = f.getName(); 179 if (isAccepted(fieldName, value)) { 180 Integer valueInteger = new Integer (value); 181 name2value.put(fieldName, valueInteger); 182 value2name.put(valueInteger, fieldName); 183 } 184 } 185 } 186 187 } catch (SecurityException e) { 188 e.printStackTrace(); 189 throw new IllegalStateException (e.toString()); 190 191 } catch (IllegalAccessException e) { 192 e.printStackTrace(); 193 throw new IllegalStateException (e.toString()); 194 } 195 196 return true; } 198 199 204 protected boolean isAccepted(String tokenTypeName, int tokenTypeValue) { 205 return true; 206 } 207 208 } 209 210 | Popular Tags |