1 19 20 package org.netbeans.lib.lexer; 21 22 import org.netbeans.api.lexer.InputAttributes; 23 import org.netbeans.api.lexer.Language; 24 import org.netbeans.api.lexer.LanguagePath; 25 import org.netbeans.api.lexer.Token; 26 import org.netbeans.api.lexer.TokenHierarchy; 27 import org.netbeans.api.lexer.TokenId; 28 import org.netbeans.lib.editor.util.ArrayUtilities; 29 import org.netbeans.lib.lexer.inc.SnapshotTokenList; 30 import org.netbeans.spi.lexer.LanguageHierarchy; 31 import org.netbeans.spi.lexer.LexerInput; 32 import org.netbeans.lib.lexer.token.AbstractToken; 33 import org.netbeans.spi.lexer.LanguageEmbedding; 34 import org.netbeans.spi.lexer.TokenPropertyProvider; 35 36 42 43 public final class LexerUtilsConstants { 44 45 52 public static final int MAX_FLY_SEQUENCE_LENGTH = 5; 53 54 64 public static final int MIN_LEXED_AREA_LENGTH = 4096; 65 66 72 public static final int LEXED_AREA_INPUT_SIZE_FRACTION = 10; 73 74 78 public static void checkLexerInputFinished(CharProvider input, LexerInputOperation operation) { 79 if (input.read() != LexerInput.EOF) { 80 throw new IllegalStateException ( 81 "Lexer " + operation.lexer() + " returned null token" + " but EOF was not read from lexer input yet." + " Fix the lexer." ); 86 } 87 if (input.readIndex() > 0) { 88 throw new IllegalStateException ( 89 "Lexer " + operation.lexer() + " returned null token but lexerInput.readLength()=" + input.readIndex() + 92 " - these characters need to be tokenized." + " Fix the lexer." ); 95 } 96 } 97 98 public static void tokenLengthZeroOrNegative(int tokenLength) { 99 if (tokenLength == 0) { 100 throw new IllegalArgumentException ( 101 "Tokens with zero length are not supported by the framework." + " Fix the lexer." ); 104 } else { throw new IllegalArgumentException ( 106 "Negative token length " + tokenLength ); 108 } 109 } 110 111 public static void throwFlyTokenProhibited() { 112 throw new IllegalStateException ("Flyweight token created but prohibited." + " Lexer needs to check lexerInput.isFlyTokenAllowed()."); } 115 116 public static void throwBranchTokenFlyProhibited(AbstractToken token) { 117 throw new IllegalStateException ("Language embedding cannot be created" + " for flyweight token=" + token + "\nFix the lexer to not create flyweight token instance when" 120 + " language embedding exists for the token." 121 ); 122 } 123 124 public static void checkValidBackup(int count, int maxCount) { 125 if (count > maxCount) { 126 throw new IndexOutOfBoundsException ("Cannot backup " + count + " characters. Maximum: " + maxCount + '.'); 129 } 130 } 131 132 public static <T extends TokenId> LanguageHierarchy<T> languageHierarchy(Language<T> language) { 133 return LexerApiPackageAccessor.get().languageHierarchy(language); 134 } 135 136 public static <T extends TokenId> LanguageOperation<T> languageOperation(Language<T> language) { 137 return LexerSpiPackageAccessor.get().operation(languageHierarchy(language)); 138 } 139 140 145 public static <T extends TokenId> Language<T> mostEmbeddedLanguage(LanguagePath languagePath) { 146 @SuppressWarnings ("unchecked") 147 Language<T> l = (Language<T>)languagePath.innerLanguage(); 148 return l; 149 } 150 151 156 public static <T extends TokenId> LanguageHierarchy<T> mostEmbeddedLanguageHierarchy(LanguagePath languagePath) { 157 @SuppressWarnings ("unchecked") 158 LanguageHierarchy<T> lh = (LanguageHierarchy<T>)languageHierarchy(languagePath.innerLanguage()); 159 return lh; 160 } 161 162 167 public static <T extends TokenId> LanguageOperation<T> mostEmbeddedLanguageOperation(LanguagePath languagePath) { 168 @SuppressWarnings ("unchecked") 169 LanguageOperation<T> lo = (LanguageOperation<T>)LexerSpiPackageAccessor.get().operation( 170 mostEmbeddedLanguageHierarchy(languagePath)); 171 return lo; 172 } 173 174 180 public static <T extends TokenId> LanguageEmbedding<? extends TokenId> 181 findEmbedding(Token<T> token, LanguagePath languagePath, InputAttributes inputAttributes) { 182 LanguageHierarchy<T> languageHierarchy = mostEmbeddedLanguageHierarchy(languagePath); 183 LanguageEmbedding<? extends TokenId> embedding = 184 LexerSpiPackageAccessor.get().embedding( 185 languageHierarchy, token, languagePath, inputAttributes); 186 187 if (embedding == null) { 188 embedding = LanguageManager.getInstance().findLanguageEmbedding( 190 token, languagePath, inputAttributes); 191 } 192 return embedding; 193 } 194 195 201 public static <T extends TokenId> AbstractToken<T> token(Object tokenOrEmbeddingContainer) { 202 @SuppressWarnings ("unchecked") 203 AbstractToken<T> token = (AbstractToken<T>) 204 ((tokenOrEmbeddingContainer.getClass() == EmbeddingContainer.class) 205 ? ((EmbeddingContainer)tokenOrEmbeddingContainer).token() 206 : (AbstractToken<? extends TokenId>)tokenOrEmbeddingContainer); 207 return token; 208 } 209 210 public static <T extends TokenId> AbstractToken<T> token(TokenList<T> tokenList, int index) { 211 return token(tokenList.tokenOrEmbeddingContainer(index)); 212 } 213 214 public static <T extends TokenId> StringBuilder appendTokenList(StringBuilder sb, 215 TokenList<T> tokenList, int currentIndex) { 216 if (sb == null) { 217 sb = new StringBuilder (); 218 } 219 TokenHierarchy<?> tokenHierarchy; 220 if (tokenList instanceof SnapshotTokenList) { 221 tokenHierarchy = ((SnapshotTokenList<T>)tokenList).snapshot().tokenHierarchy(); 222 sb.append(tokenList).append('\n'); 223 } else { 224 tokenHierarchy = null; 225 } 226 227 int tokenCount = tokenList.tokenCountCurrent(); 228 int digitCount = ArrayUtilities.digitCount(tokenCount); 229 for (int i = 0; i < tokenCount; i++) { 230 sb.append((i == currentIndex) ? '*' : ' '); 231 ArrayUtilities.appendBracketedIndex(sb, i, digitCount); 232 Object tokenOrEmbeddingContainer = tokenList.tokenOrEmbeddingContainer(i); 233 if (tokenOrEmbeddingContainer == null) { 234 System.err.println("tokenList=" + tokenList + ", i=" + i); 235 } 236 sb.append((tokenOrEmbeddingContainer.getClass() == EmbeddingContainer.class) ? '<' : ' '); 237 sb.append(": "); 238 AbstractToken token = token(tokenOrEmbeddingContainer); 239 sb.append(token.dumpInfo(tokenHierarchy)); 240 int la = tokenList.lookahead(i); 241 if (la != 0) { 242 sb.append(", la="); 243 sb.append(la); 244 } 245 Object state= tokenList.state(i); 246 if (state != null) { 247 sb.append(", s="); 248 sb.append(state); 249 } 250 sb.append('\n'); 251 } 252 return sb; 253 } 254 255 public static boolean statesEqual(Object state1, Object state2) { 256 return (state1 == null && state2 == null) 257 || (state1 != null && state1.equals(state2)); 258 } 259 260 public static String idToString(TokenId id) { 261 return id.name() + '[' + id.ordinal() + ']'; } 263 264 private LexerUtilsConstants() { 265 } 267 268 } 269 | Popular Tags |