1 19 20 package org.netbeans.modules.languages.lexer; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import org.netbeans.api.languages.LanguagesManager; 26 import org.netbeans.api.languages.ParseException; 27 import org.netbeans.api.lexer.InputAttributes; 28 import org.netbeans.api.lexer.Language; 29 import org.netbeans.api.lexer.LanguagePath; 30 import org.netbeans.api.lexer.Token; 31 import org.netbeans.api.lexer.TokenId; 32 import org.netbeans.modules.languages.Feature; 33 import org.netbeans.modules.languages.LanguagesManagerImpl; 34 import org.netbeans.spi.lexer.LanguageEmbedding; 35 import org.netbeans.spi.lexer.LanguageProvider; 36 37 38 42 public class SLanguageProvider extends LanguageProvider { 43 44 45 public SLanguageProvider () { 46 } 47 48 public Language<STokenId> findLanguage (String mimePath) { 49 if (LanguagesManager.getDefault ().getSupportedMimeTypes ().contains (mimePath)) 51 return new SLanguageHierarchy (mimePath).language (); 52 return null; 53 } 54 55 public LanguageEmbedding<? extends TokenId> findLanguageEmbedding ( 56 Token token, 57 LanguagePath languagePath, 58 InputAttributes inputAttributes 59 ) { 60 String mimeType = languagePath.innerLanguage ().mimeType (); 61 Language<STokenId> language = getTokenImport (mimeType, token); 62 if (language == null) 63 language = getPreprocessorImport (languagePath, token); 64 if (language == null) return null; 65 Integer i = (Integer ) token.getProperty ("startSkipLength"); 66 int startSkipLength = i == null ? 0 : i.intValue (); 67 i = (Integer ) token.getProperty ("endSkipLength"); 68 int endSkipLength = i == null ? 0 : i.intValue (); 69 return LanguageEmbedding.create ( 70 language, 71 startSkipLength, 72 endSkipLength 73 ); 74 } 75 76 77 79 private static Map <String ,Language<STokenId>> preprocessorImport = new HashMap <String ,Language<STokenId>> (); 80 81 private static Language<STokenId> getPreprocessorImport (LanguagePath languagePath, Token token) { 82 String tokenType = token.id ().name (); 83 if (!tokenType.equals ("PE")) return null; 84 String mimeType = languagePath.topLanguage ().mimeType (); 85 if (!preprocessorImport.containsKey (mimeType)) { 86 try { 87 org.netbeans.modules.languages.Language language = 88 ((LanguagesManagerImpl) LanguagesManager.getDefault ()). 89 getLanguage (mimeType); 90 Feature properties = language.getPreprocessorImport (); 91 if (properties != null) { 92 String innerMT = (String ) properties.getValue ("mimeType"); 93 preprocessorImport.put ( 94 mimeType, 95 (Language<STokenId>)Language.find (innerMT) 96 ); 97 } 98 } catch (ParseException ex) { 99 } 100 } 101 return preprocessorImport.get (mimeType); 102 } 103 104 private static Map <String ,Map <String ,Language<STokenId>>> tokenImports = new HashMap <String ,Map <String ,Language<STokenId>>> (); 105 106 private static Language<STokenId> getTokenImport (String mimeType, Token token) { 107 String tokenType = token.id ().name (); 108 Map <String ,Language<STokenId>> tokenTypeToLanguage = tokenImports.get (mimeType); 109 if (tokenTypeToLanguage == null) { 110 tokenTypeToLanguage = new HashMap <String ,Language<STokenId>> (); 111 tokenImports.put (mimeType, tokenTypeToLanguage); 112 try { 113 org.netbeans.modules.languages.Language language = 114 ((LanguagesManagerImpl) LanguagesManager.getDefault ()). 115 getLanguage (mimeType); 116 Map <String ,Feature> tokenImports = language.getTokenImports (); 117 if (tokenImports != null) { 118 Iterator <String > it = tokenImports.keySet ().iterator (); 119 while (it.hasNext ()) { 120 String tokenType2 = it.next (); 121 Feature properties = tokenImports.get (tokenType2); 122 String innerMT = (String ) properties.getValue ("mimeType"); 123 tokenTypeToLanguage.put ( 124 tokenType2, 125 (Language<STokenId>) Language.find (innerMT) 126 ); 127 } 128 } 129 } catch (ParseException ex) { 130 } 131 } 132 return tokenTypeToLanguage.get (tokenType); 133 } 134 } 135 | Popular Tags |