1 19 20 package org.netbeans.spi.lexer; 21 22 import java.util.Collection ; 23 import java.util.Map ; 24 import org.netbeans.api.lexer.InputAttributes; 25 import org.netbeans.api.lexer.Language; 26 import org.netbeans.api.lexer.LanguagePath; 27 import org.netbeans.api.lexer.Token; 28 import org.netbeans.api.lexer.TokenId; 29 import org.netbeans.lib.lexer.CharPreprocessorOperation; 30 import org.netbeans.lib.lexer.CharProvider; 31 import org.netbeans.lib.lexer.LanguageOperation; 32 import org.netbeans.lib.lexer.LexerInputOperation; 33 import org.netbeans.lib.lexer.LexerSpiPackageAccessor; 34 import org.netbeans.lib.lexer.TokenIdImpl; 35 36 104 105 public abstract class LanguageHierarchy<T extends TokenId> { 106 107 static { 108 LexerSpiPackageAccessor.register(new Accessor()); 109 } 110 111 115 public static TokenId newId(String name, int ordinal) { 116 return newId(name, ordinal, null); 117 } 118 119 123 public static TokenId newId(String name, int ordinal, String primaryCategory) { 124 return new TokenIdImpl(name, ordinal, primaryCategory); 125 } 126 127 128 129 LanguageOperation<T> operation = new LanguageOperation<T>(this); 130 131 149 protected abstract Collection <T> createTokenIds(); 150 151 172 protected Map <String ,Collection <T>> createTokenCategories() { 173 return null; } 175 176 183 protected abstract Lexer<T> createLexer(LexerRestartInfo<T> info); 184 185 191 protected abstract String mimeType(); 192 193 221 protected LanguageEmbedding<? extends TokenId> embedding(Token<T> token, 222 LanguagePath languagePath, InputAttributes inputAttributes) { 223 return null; } 225 226 232 protected CharPreprocessor createCharPreprocessor() { 233 return null; } 235 236 243 protected TokenValidator<T> createTokenValidator(T tokenId) { 244 return null; 245 } 246 247 276 protected boolean isRetainTokenText(T tokenId) { 277 return false; 278 } 279 280 281 287 public final Language<T> language() { 288 return operation.language(); 289 } 290 291 292 public final int hashCode() { 293 return super.hashCode(); 294 } 295 296 297 public final boolean equals(Object o) { 298 return super.equals(o); 299 } 300 301 public String toString() { 302 return getClass().getName(); } 304 305 306 private static final class Accessor extends LexerSpiPackageAccessor { 307 308 public <T extends TokenId> Collection <T> createTokenIds(LanguageHierarchy<T> languageHierarchy) { 309 return languageHierarchy.createTokenIds(); 310 } 311 312 public <T extends TokenId> Map <String ,Collection <T>> createTokenCategories(LanguageHierarchy<T> languageHierarchy) { 313 return languageHierarchy.createTokenCategories(); 314 } 315 316 public String mimeType(LanguageHierarchy<? extends TokenId> languageHierarchy) { 317 return languageHierarchy.mimeType(); 318 } 319 320 public <T extends TokenId> LanguageOperation<T> operation(LanguageHierarchy<T> languageHierarchy) { 321 return languageHierarchy.operation; 322 } 323 324 public <T extends TokenId> LanguageEmbedding<? extends TokenId> embedding(LanguageHierarchy<T> languageHierarchy, 325 Token<T> token, LanguagePath languagePath, InputAttributes inputAttributes) { 326 return languageHierarchy.embedding(token, languagePath, inputAttributes); 327 } 328 329 public <T extends TokenId> Lexer<T> createLexer( 330 LanguageHierarchy<T> languageHierarchy, LexerRestartInfo<T> info) { 331 return languageHierarchy.createLexer(info); 332 } 333 334 public <T extends TokenId> LexerRestartInfo<T> createLexerRestartInfo( 335 LexerInput input, TokenFactory<T> tokenFactory, Object state, 336 LanguagePath languagePath, InputAttributes inputAttributes) { 337 return new LexerRestartInfo<T>(input, tokenFactory, state, languagePath, inputAttributes); 338 } 339 340 public <T extends TokenId> TokenValidator<T> createTokenValidator( 341 LanguageHierarchy<T> languageHierarchy, T id) { 342 return languageHierarchy.createTokenValidator(id); 343 } 344 345 public <T extends TokenId> boolean isRetainTokenText( 346 LanguageHierarchy<T> languageHierarchy, T id) { 347 return languageHierarchy.isRetainTokenText(id); 348 } 349 350 public CharPreprocessor createCharPreprocessor(LanguageHierarchy languageHierarchy) { 351 return languageHierarchy.createCharPreprocessor(); 352 } 353 354 public LexerInput createLexerInput(CharProvider charProvider) { 355 return new LexerInput(charProvider); 356 } 357 358 public void init(CharPreprocessor preprocessor, CharPreprocessorOperation operation) { 359 preprocessor.init(operation); 360 } 361 362 public void preprocessChar(CharPreprocessor preprocessor) { 363 preprocessor.preprocessChar(); 364 } 365 366 public Language<? extends TokenId> language(MutableTextInput<?> mti) { 367 return mti.language(); 368 } 369 370 public <T extends TokenId> LanguageEmbedding<T> createLanguageEmbedding( 371 Language<T> language, int startSkipLength, int endSkipLength, boolean joinSections) { 372 return new LanguageEmbedding<T>(language, startSkipLength, endSkipLength, joinSections); 373 } 374 375 public CharSequence text(MutableTextInput<?> mti) { 376 return mti.text(); 377 } 378 379 public InputAttributes inputAttributes(MutableTextInput<?> mti) { 380 return mti.inputAttributes(); 381 } 382 383 public <I> I inputSource(MutableTextInput<I> mti) { 384 return mti.inputSource(); 385 } 386 387 public <T extends TokenId> TokenFactory<T> createTokenFactory( 388 LexerInputOperation<T> lexerInputOperation) { 389 return new TokenFactory<T>(lexerInputOperation); 390 } 391 392 } 393 394 } 395 | Popular Tags |