1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.AST; 10 import persistence.antlr.collections.impl.ASTArray; 11 12 import java.util.Hashtable ; 13 import java.lang.reflect.Constructor ; 14 15 25 public class ASTFactory { 26 31 protected String theASTNodeType = null; 32 protected Class theASTNodeTypeClass = null; 33 34 50 protected Hashtable tokenTypeToASTClassMap = null; 51 52 public ASTFactory() { 53 } 54 55 59 public ASTFactory(Hashtable tokenTypeToClassMap) { 60 setTokenTypeToASTClassMap(tokenTypeToClassMap); 61 } 62 63 79 public void setTokenTypeASTNodeType(int tokenType, String className) 80 throws IllegalArgumentException 81 { 82 if ( tokenTypeToASTClassMap==null ) { 83 tokenTypeToASTClassMap = new Hashtable (); 84 } 85 if ( className==null ) { 86 tokenTypeToASTClassMap.remove(new Integer (tokenType)); 87 return; 88 } 89 Class c = null; 90 try { 91 c = Class.forName(className); 92 tokenTypeToASTClassMap.put(new Integer (tokenType), c); 93 } 94 catch (Exception e) { 95 throw new IllegalArgumentException ("Invalid class, "+className); 96 } 97 } 98 99 103 public Class getASTNodeType(int tokenType) { 104 if ( tokenTypeToASTClassMap!=null ) { 106 Class c = (Class )tokenTypeToASTClassMap.get(new Integer (tokenType)); 107 if ( c!=null ) { 108 return c; 109 } 110 } 111 112 if (theASTNodeTypeClass != null) { 114 return theASTNodeTypeClass; 115 } 116 117 return CommonAST.class; 119 } 120 121 122 public void addASTChild(ASTPair currentAST, AST child) { 123 if (child != null) { 124 if (currentAST.root == null) { 125 currentAST.root = child; 127 } 128 else { 129 if (currentAST.child == null) { 130 currentAST.root.setFirstChild(child); 132 } 133 else { 134 currentAST.child.setNextSibling(child); 135 } 136 } 137 currentAST.child = child; 139 currentAST.advanceChildToEnd(); 140 } 141 } 142 143 146 public AST create() { 147 return create(Token.INVALID_TYPE); 148 } 149 150 public AST create(int type) { 151 Class c = getASTNodeType(type); 152 AST t = create(c); 153 if ( t!=null ) { 154 t.initialize(type, ""); 155 } 156 return t; 157 } 158 159 public AST create(int type, String txt) { 160 AST t = create(type); 161 if ( t!=null ) { 162 t.initialize(type, txt); 163 } 164 return t; 165 } 166 167 172 public AST create(int type, String txt, String className) { 173 AST t = create(className); 174 if ( t!=null ) { 175 t.initialize(type, txt); 176 } 177 return t; 178 } 179 180 183 public AST create(AST tr) { 184 if (tr == null) return null; AST t = create(tr.getType()); 186 if ( t!=null ) { 187 t.initialize(tr); 188 } 189 return t; 190 } 191 192 public AST create(Token tok) { 193 AST t = create(tok.getType()); 194 if ( t!=null ) { 195 t.initialize(tok); 196 } 197 return t; 198 } 199 200 208 public AST create(Token tok, String className) { 209 AST t = createUsingCtor(tok,className); 210 return t; 211 } 212 213 216 public AST create(String className) { 217 Class c = null; 218 try { 219 c = Class.forName(className); 220 } 221 catch (Exception e) { 222 throw new IllegalArgumentException ("Invalid class, "+className); 223 } 224 return create(c); 225 } 226 227 230 protected AST createUsingCtor(Token token, String className) { 231 Class c = null; 232 AST t = null; 233 try { 234 c = Class.forName(className); 235 Class [] tokenArgType = new Class [] { persistence.antlr.Token.class }; 236 try { 237 Constructor ctor = c.getConstructor(tokenArgType); 238 t = (AST)ctor.newInstance(new Object []{token}); } 240 catch (NoSuchMethodException e){ 241 t = create(c); 244 if ( t!=null ) { 245 t.initialize(token); 246 } 247 } 248 } 249 catch (Exception e) { 250 throw new IllegalArgumentException ("Invalid class or can't make instance, "+className); 251 } 252 return t; 253 } 254 255 258 protected AST create(Class c) { 259 AST t = null; 260 try { 261 t = (AST)c.newInstance(); } 263 catch (Exception e) { 264 error("Can't create AST Node " + c.getName()); 265 return null; 266 } 267 return t; 268 } 269 270 278 public AST dup(AST t) { 279 if ( t==null ) { 280 return null; 281 } 282 AST dup_t = create(t.getClass()); 283 dup_t.initialize(t); 284 return dup_t; 285 } 286 287 288 public AST dupList(AST t) { 289 AST result = dupTree(t); AST nt = result; 291 while (t != null) { t = t.getNextSibling(); 293 nt.setNextSibling(dupTree(t)); nt = nt.getNextSibling(); 295 } 296 return result; 297 } 298 299 302 public AST dupTree(AST t) { 303 AST result = dup(t); if (t != null) { 306 result.setFirstChild(dupList(t.getFirstChild())); 307 } 308 return result; 309 } 310 311 317 public AST make(AST[] nodes) { 318 if (nodes == null || nodes.length == 0) return null; 319 AST root = nodes[0]; 320 AST tail = null; 321 if (root != null) { 322 root.setFirstChild(null); } 324 for (int i = 1; i < nodes.length; i++) { 326 if (nodes[i] == null) continue; if (root == null) { 328 root = tail = nodes[i]; 330 } 331 else if (tail == null) { 332 root.setFirstChild(nodes[i]); 333 tail = root.getFirstChild(); 334 } 335 else { 336 tail.setNextSibling(nodes[i]); 337 tail = tail.getNextSibling(); 338 } 339 while (tail.getNextSibling() != null) { 341 tail = tail.getNextSibling(); 342 } 343 } 344 return root; 345 } 346 347 350 public AST make(ASTArray nodes) { 351 return make(nodes.array); 352 } 353 354 355 public void makeASTRoot(ASTPair currentAST, AST root) { 356 if (root != null) { 357 root.addChild(currentAST.root); 359 currentAST.child = currentAST.root; 361 currentAST.advanceChildToEnd(); 362 currentAST.root = root; 364 } 365 } 366 367 public void setASTNodeClass(String t) { 368 theASTNodeType = t; 369 try { 370 theASTNodeTypeClass = Class.forName(t); } 372 catch (Exception e) { 373 error("Can't find/access AST Node type" + t); 377 } 378 } 379 380 383 public void setASTNodeType(String t) { 384 setASTNodeClass(t); 385 } 386 387 public Hashtable getTokenTypeToASTClassMap() { 388 return tokenTypeToASTClassMap; 389 } 390 391 public void setTokenTypeToASTClassMap(Hashtable tokenTypeToClassMap) { 392 this.tokenTypeToASTClassMap = tokenTypeToClassMap; 393 } 394 395 399 public void error(String e) { 400 System.err.println(e); 401 } 402 } 403 | Popular Tags |