1 19 20 package org.netbeans.modules.languages; 21 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import org.netbeans.api.languages.ASTItem; 25 import org.netbeans.api.languages.ParseException; 26 import org.netbeans.api.languages.CharInput; 27 import org.netbeans.modules.languages.parser.TokenInput; 28 import org.netbeans.api.languages.ASTToken; 29 import java.io.BufferedReader ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.InputStreamReader ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import org.netbeans.api.languages.ASTNode; 37 import org.netbeans.api.languages.ParseException; 38 import org.netbeans.modules.languages.parser.Pattern; 39 import org.netbeans.api.languages.ASTToken; 40 import org.netbeans.modules.languages.parser.TokenInput; 41 import org.netbeans.modules.languages.parser.StringInput; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileObject; 44 45 46 47 51 public class NBSLanguageReader { 52 53 public static Language readLanguage ( 54 FileObject fo, 55 String mimeType 56 ) throws ParseException, IOException { 57 BufferedReader reader = null; 58 try { 59 return readLanguage (fo.toString (), fo.getInputStream (), mimeType); 60 } finally { 61 if (reader != null) 62 reader.close (); 63 } 64 } 65 66 public static Language readLanguage ( 67 String fileName, 68 InputStream inputStream, 69 String mimeType 70 ) throws ParseException, IOException { 71 BufferedReader reader = null; 72 try { 73 InputStreamReader r = new InputStreamReader (inputStream); 74 reader = new BufferedReader (r); 75 StringBuilder sb = new StringBuilder (); 76 String line = reader.readLine (); 77 while (line != null) { 78 sb.append (line).append ('\n'); 79 line = reader.readLine (); 80 } 81 return readLanguage (fileName, sb.toString (), mimeType); 82 } finally { 83 if (reader != null) 84 reader.close (); 85 } 86 } 87 88 public static Language readLanguage ( 89 String fo, 90 String s, 91 String mimeType 92 ) { 93 CharInput input = new StringInput (s, fo.toString ()); 94 try { 95 Language language = new Language (mimeType); 96 Language nbsLanguage = NBSLanguage.getNBSLanguage (); 97 TokenInput tokenInput = TokenInput.create ( 98 mimeType, 99 nbsLanguage.getParser (), 100 input, 101 Collections.EMPTY_SET 102 ); 103 ASTNode node = nbsLanguage.getAnalyser ().read (tokenInput, false); 104 if (node == null) 105 System.out.println("Can not parse " + fo); 106 else 107 if (node.getChildren ().isEmpty ()) 108 System.out.println("Can not parse " + fo + " " + node.getNT ()); 109 readBody (node, language); 110 return language; 111 } catch (ParseException ex) { 112 ErrorManager.getDefault ().notify (ex); 113 return new Language (mimeType); 114 } 115 } 116 117 private static void readBody ( 118 ASTNode root, 119 Language language 120 ) throws ParseException { 121 Iterator it = root.getChildren ().iterator (); 122 while (it.hasNext ()) { 123 Object o = it.next (); 124 if (o instanceof ASTToken) continue; 125 ASTNode node = (ASTNode) o; 126 if (node.getNT ().equals ("token")) 127 readToken (node, language, null); 128 else 129 if (node.getNT ().equals ("tokenState")) 130 readTokenState (node, language); 131 else 132 if (node.getNT ().equals ("grammarRule")) 133 readGrammarRule (node, language); 134 else 135 if (node.getNT ().equals ("command")) 136 readCommand (node, language); 137 else 138 throw new ParseException ( 139 "Unknown grammar rule (" + node.getNT () + ")." 140 ); 141 } 142 } 143 144 private static void readToken ( 145 ASTNode node, 146 Language language, 147 String state 148 ) throws ParseException { 149 String startState = null; 150 String endState = null; 151 Pattern pattern = null; 152 Feature properties = null; 153 String name = node.getTokenType ("identifier").getIdentifier (); 154 ASTNode pnode = node.getNode ("token2.properties"); 155 if (pnode != null) { 156 properties = readProperties (null, null, pnode); 157 startState = (String ) properties.getValue ("start_state"); 161 endState = (String ) properties.getValue ("end_state"); 162 pattern = properties.getPattern ("pattern"); 163 } else { 164 String patternString = node.getNode ("token2.regularExpression").getAsText ().trim (); 165 endState = node.getTokenTypeIdentifier ("token2.token3.state.identifier"); 166 pattern = Pattern.create (patternString); 167 } 168 if (startState != null && state != null) 169 throw new ParseException ("Start state should not be specified inside token group block!"); 170 if (startState == null) startState = state; 171 if (endState == null) endState = state; 172 language.addToken ( 173 startState, 174 name, 175 pattern, 176 endState, 177 properties 178 ); 179 } 180 181 private static void readGrammarRule ( 182 ASTNode node, 183 Language language 184 ) throws ParseException { 185 language.addRule (node); 188 } 189 190 private static void readTokenState ( 191 ASTNode node, 192 Language language 193 ) throws ParseException { 194 String startState = node.getTokenTypeIdentifier ("state.identifier"); 195 ASTNode n = node.getNode ("tokenState1.token"); 196 if (n != null) 197 readToken (n, language, startState); 198 else 199 readTokenGroup (node.getNode ("tokenState1.tokenGroup"), language, startState); 200 } 201 202 private static void readTokenGroup ( 203 ASTNode node, 204 Language language, 205 String startState 206 ) throws ParseException { 207 Iterator it = node.getNode ("tokensInGroup").getChildren ().iterator (); 208 while (it.hasNext ()) { 209 Object o = it.next (); 210 if (o instanceof ASTToken) continue; 211 ASTNode n = (ASTNode) o; 212 readToken (n, language, startState); 213 } 214 } 215 216 private static void readCommand ( 217 ASTNode commandNode, 218 Language language 219 ) throws ParseException { 220 String keyword = commandNode.getTokenTypeIdentifier ("keyword"); 221 ASTNode command0Node = commandNode.getNode ("command0"); 222 ASTNode selectorNode = command0Node.getNode ("selector"); 223 Selector selector = null; 224 Feature feature = null; 225 if (selectorNode != null) { 226 ASTNode classNode = selectorNode.getNode ("class"); 227 selector = Selector.create (readClass (classNode)); 228 ASTNode command1Node = command0Node.getNode ("command1"); 229 ASTNode valueNode = command1Node.getNode ("value"); 230 if (valueNode != null) 231 feature = readValue (keyword, selector, valueNode); 232 else 233 feature = Feature.create (keyword, selector); 234 } else { 235 ASTNode valueNode = command0Node.getNode ("value"); 236 feature = readValue (keyword, selector, valueNode); 237 } 238 language.addFeature (feature); 239 } 240 241 private static Feature readValue ( 242 String keyword, 243 Selector selector, 244 ASTNode valueNode 245 ) throws ParseException { 246 ASTNode propertiesNode = valueNode.getNode ("properties"); 247 if (propertiesNode != null) 248 return readProperties (keyword, selector, propertiesNode); 249 ASTNode classNode = valueNode.getNode ("class"); 250 if (classNode != null) 251 return Feature.createMethodCallFeature (keyword, selector, readClass (classNode)); 252 String s = valueNode.getTokenTypeIdentifier ("string"); 253 s = s.substring (1, s.length () - 1); 254 return Feature.createExpressionFeature (keyword, selector, c (s)); 255 } 256 257 private static Feature readProperties ( 258 String keyword, 259 Selector selector, 260 ASTNode node 261 ) throws ParseException { 262 Map <String ,String > methods = new HashMap <String ,String > (); 263 Map <String ,String > expressions = new HashMap <String ,String > (); 264 Map <String ,Pattern> patterns = new HashMap <String ,Pattern> (); 265 266 Iterator it = node.getChildren ().iterator (); 267 while (it.hasNext ()) { 268 Object o = it.next (); 269 if (o instanceof ASTToken) continue; 270 ASTNode n = (ASTNode) o; 271 String key = n.getTokenTypeIdentifier ("identifier"); 272 String value = n.getTokenTypeIdentifier ("propertyValue.string"); 273 if (value != null) { 274 value = value.substring (1, value.length () - 1); 275 expressions.put (key, c (value)); 276 } else 277 if (n.getNode ("propertyValue.class") != null) { 278 value = readClass (n.getNode ("propertyValue.class")); 279 methods.put (key, value); 280 } else { 281 value = n.getNode ("propertyValue.regularExpression").getAsText ().trim (); 282 Pattern pattern = Pattern.create (value); 283 patterns.put (key, pattern); 284 } 285 } 286 return Feature.create (keyword, selector, expressions, methods, patterns); 287 } 288 289 private static String readClass (ASTNode cls) { 290 StringBuilder sb = new StringBuilder (); 291 sb.append (cls.getTokenTypeIdentifier ("identifier")); 292 Iterator <ASTItem> it = cls.getNode ("class1").getChildren ().iterator (); 293 while (it.hasNext ()) { 294 ASTToken token = (ASTToken) it.next (); 295 if (token.getIdentifier ().equals (".")) 296 sb.append ('.'); 297 else 298 if (token.getType ().equals ("identifier")) 299 sb.append (token.getIdentifier ()); 300 } 301 return sb.toString (); 302 } 303 304 305 private static String c (String s) { 306 s = s.replace ("\\n", "\n"); 307 s = s.replace ("\\r", "\r"); 308 s = s.replace ("\\t", "\t"); 309 s = s.replace ("\\\"", "\""); 310 s = s.replace ("\\\'", "\'"); 311 s = s.replace ("\\\\", "\\"); 312 return s; 313 } 314 } 315 | Popular Tags |