1 19 20 package org.netbeans.modules.languages.lexer; 21 22 import org.netbeans.api.languages.LanguagesManager; 23 import org.netbeans.api.languages.ParseException; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import org.netbeans.modules.languages.Language; 29 import org.netbeans.modules.languages.Language.TokenType; 30 import org.netbeans.api.languages.ParseException; 31 import org.netbeans.modules.languages.LanguagesManagerImpl; 32 import org.netbeans.spi.lexer.LanguageHierarchy; 33 import org.netbeans.spi.lexer.Lexer; 34 import org.netbeans.spi.lexer.LexerRestartInfo; 35 36 37 41 public class SLanguageHierarchy extends LanguageHierarchy<STokenId> { 42 43 private String mimeType; 44 private Collection <STokenId> tokenIds; 45 private HashMap <String ,STokenId> tokensMap; 46 47 48 SLanguageHierarchy (String mimeType) { 49 this.mimeType = mimeType; 50 } 51 52 protected Collection <STokenId> createTokenIds () { 53 if (tokenIds == null) { 54 tokenIds = new ArrayList <STokenId> (); 55 tokensMap = new HashMap <String ,STokenId> (); 56 Iterator <TokenType> it = getLanguage ().getTokenTypes ().iterator (); 57 while (it.hasNext ()) { 58 TokenType t = it.next (); 59 if (tokensMap.containsKey (t.getType ())) continue; 60 STokenId tokenId = new STokenId ( 61 t.getType (), 62 tokenIds.size (), 63 t.getType () 64 ); 65 tokenIds.add (tokenId); 66 tokensMap.put (t.getType (), tokenId); 67 } 68 STokenId errorTokenId = new STokenId ( 69 "error", 70 tokenIds.size (), 71 "error" 72 ); 73 tokenIds.add (errorTokenId); 74 tokensMap.put ("error", errorTokenId); 75 STokenId embeddingTokenId = new STokenId ( 76 "PE", 77 tokenIds.size (), 78 "PE" 79 ); 80 tokenIds.add (embeddingTokenId); 81 tokensMap.put ("PE", embeddingTokenId); 82 } 83 return tokenIds; 84 } 85 86 protected Lexer<STokenId> createLexer (LexerRestartInfo<STokenId> info) { 87 if (tokensMap == null) createTokenIds (); 88 return new SLexer ( 89 getLanguage (), 90 tokensMap, 91 info 92 ); 93 } 94 95 protected String mimeType () { 96 return mimeType; 97 } 98 99 100 102 private Language language; 103 104 private Language getLanguage () { 105 if (language == null) 106 try { 107 language = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType); 108 } catch (ParseException ex) { 109 language = new Language (mimeType); 110 } 111 return language; 112 } 113 } 114 | Popular Tags |