1 19 20 package org.netbeans.api.languages; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 35 public final class ASTNode extends ASTItem { 36 37 48 public static ASTNode create ( 49 String mimeType, 50 String nt, 51 List <ASTItem> children, 52 int offset 53 ) { 54 return new ASTNode (mimeType, nt, offset, children); 55 } 56 57 67 public static ASTNode create ( 68 String mimeType, 69 String nt, 70 int offset 71 ) { 72 return new ASTNode (mimeType, nt, offset, Collections.<ASTItem>emptyList ()); 73 } 74 75 76 private String nt; 77 78 private ASTNode ( 79 String mimeType, 80 String nt, 81 int offset, 82 List <ASTItem> children 83 ) { 84 super (mimeType, offset, -1, children); 85 this.nt = nt; 86 } 87 88 93 public String getNT () { 94 return nt; 95 } 96 97 102 106 114 public ASTPath findToken (String type, String identifier) { 115 List <ASTItem> path = new ArrayList <ASTItem> (); 116 findToken (type, identifier, path); 117 if (path.isEmpty ()) return null; 118 return ASTPath.create (path); 119 } 120 121 private boolean findToken (String type, String identifier, List <ASTItem> path) { 122 path.add (this); 123 Iterator it = getChildren ().iterator (); 124 while (it.hasNext ()) { 125 Object e = it.next (); 126 if (e instanceof ASTToken) { 127 ASTToken t = (ASTToken) e; 128 if (type != null && !type.equals (t.getType ())) continue; 129 if (identifier != null && !identifier.equals (t.getIdentifier ())) continue; 130 return true; 131 } else 132 if (((ASTNode) e).findToken (type, identifier, path)) 133 return true; 134 } 135 path.remove (path.size () - 1); 136 return false; 137 } 138 139 149 public ASTNode findNode (String nt, int offset) { 150 if (nt.equals (getNT ())) return this; 151 Iterator it = getChildren ().iterator (); 152 while (it.hasNext ()) { 153 Object e = (Object ) it.next (); 154 if (e instanceof ASTNode) { 155 ASTNode node = (ASTNode) e; 156 if (node.getOffset () <= offset && 157 offset < node.getEndOffset () 158 ) 159 return node.findNode (nt, offset); 160 } 161 } 162 return null; 163 } 164 165 172 public String getTokenTypeIdentifier (String type) { 173 ASTToken token = getTokenType (type); 174 if (token == null) return null; 175 return token.getIdentifier (); 176 } 177 178 185 public ASTToken getTokenType (String type) { 186 ASTNode node = this; 187 int i = type.lastIndexOf ('.'); 188 if (i >= 0) 189 node = getNode (type.substring (0, i)); 190 if (node == null) return null; 191 Object o = node.getChild ("token-type-" + type.substring (i + 1)); 192 if (o == null) return null; 193 if (!(o instanceof ASTToken)) return null; 194 return (ASTToken) o; 195 } 196 197 204 public ASTNode getNode (String path) { 205 ASTNode node = this; 206 int s = 0, e = path.indexOf ('.'); 207 while (e >= 0) { 208 node = (ASTNode) node.getChild ("node-" + path.substring (s, e)); 209 if (node == null) return null; 210 s = e + 1; 211 e = path.indexOf ('.', s); 212 } 213 return (ASTNode) node.getChild ("node-" + path.substring (s)); 214 } 215 216 private Map <String ,ASTItem> nameToChild = null; 217 218 private Object getChild (String name) { 219 if (nameToChild == null) { 220 nameToChild = new HashMap <String ,ASTItem> (); 221 Iterator <ASTItem> it = getChildren ().iterator (); 222 while (it.hasNext ()) { 223 ASTItem item = it.next (); 224 if (item instanceof ASTToken) { 225 ASTToken t = (ASTToken) item; 226 nameToChild.put ("token-type-" + t.getType (), t); 227 } else { 228 nameToChild.put ( 229 "node-" + ((ASTNode) item).getNT (), 230 item 231 ); 232 } 233 } 234 } 235 return nameToChild.get (name); 236 } 237 238 243 public String print () { 244 return print (""); 245 } 246 247 private String print (String indent) { 248 StringBuilder sb = new StringBuilder (); 249 sb.append (indent).append ("ASTNode ").append (getNT ()).append (' '). 250 append (getOffset ()).append ('-').append (getEndOffset ()); 251 indent = " " + indent; 252 Iterator it = getChildren ().iterator (); 253 while (it.hasNext ()) { 254 Object elem = it.next (); 255 if (elem instanceof ASTNode) { 256 sb.append ('\n').append (((ASTNode) elem).print (indent)); 257 } else 258 sb.append ('\n').append (indent).append (elem); 259 } 260 return sb.toString (); 261 } 262 263 268 public String getAsText () { 269 StringBuilder sb = new StringBuilder (); 270 Iterator it = getChildren ().iterator (); 271 while (it.hasNext ()) { 272 Object elem = it.next (); 273 if (elem instanceof ASTNode) 274 sb.append (((ASTNode) elem).getAsText ()); 275 else 276 sb.append (((ASTToken) elem).getIdentifier ()); 277 } 278 return sb.toString (); 279 } 280 281 286 public String toString () { 287 StringBuilder sb = new StringBuilder (); 288 sb.append ("ASTNode ").append (getNT ()).append (' '). 289 append (getOffset ()).append ('-').append (getEndOffset ()); 290 Iterator it = getChildren ().iterator (); 291 while (it.hasNext ()) { 292 Object elem = it.next (); 293 if (elem instanceof ASTNode) 294 sb.append ("\n ").append (((ASTNode) elem).getNT () + "..."); 295 else 296 sb.append ("\n ").append (elem); 297 } 298 return sb.toString (); 299 } 300 } 301 | Popular Tags |