1 package antlr; 2 3 9 10 import antlr.collections.AST; 11 import antlr.collections.impl.ASTArray; 12 13 23 public class ASTFactory { 24 28 protected String theASTNodeType = null; 29 protected Class theASTNodeTypeClass = null; 30 31 32 33 public void addASTChild(ASTPair currentAST, AST child) { 34 if (child != null) { 35 if (currentAST.root == null) { 36 currentAST.root = child; 38 } 39 else { 40 if (currentAST.child == null) { 41 currentAST.root.setFirstChild(child); 43 } 44 else { 45 currentAST.child.setNextSibling(child); 46 } 47 } 48 currentAST.child = child; 50 currentAST.advanceChildToEnd(); 51 } 52 } 53 54 57 public AST create() { 58 AST t = null; 59 if (theASTNodeTypeClass == null) { 60 t = new CommonAST(); 61 } 62 else { 63 try { 64 t = (AST)theASTNodeTypeClass.newInstance(); } 66 catch (Exception e) { 67 error("Can't create AST Node " + theASTNodeType); 68 return null; 69 } 70 } 71 return t; 72 } 73 74 public AST create(int type) { 75 AST t = create(); 76 t.initialize(type, ""); 77 return t; 78 } 79 80 public AST create(int type, String txt) { 81 AST t = create(); 82 t.initialize(type, txt); 83 return t; 84 } 85 86 89 public AST create(AST tr) { 90 if (tr == null) return null; AST t = create(); 92 t.initialize(tr); 93 return t; 94 } 95 96 public AST create(Token tok) { 97 AST t = create(); 98 t.initialize(tok); 99 return t; 100 } 101 102 108 public AST dup(AST t) { 109 return create(t); } 111 112 113 public AST dupList(AST t) { 114 AST result = dupTree(t); AST nt = result; 116 while (t != null) { t = t.getNextSibling(); 118 nt.setNextSibling(dupTree(t)); nt = nt.getNextSibling(); 120 } 121 return result; 122 } 123 124 127 public AST dupTree(AST t) { 128 AST result = dup(t); if (t != null) { 131 result.setFirstChild(dupList(t.getFirstChild())); 132 } 133 return result; 134 } 135 136 142 public AST make(AST[] nodes) { 143 if (nodes == null || nodes.length == 0) return null; 144 AST root = nodes[0]; 145 AST tail = null; 146 if (root != null) { 147 root.setFirstChild(null); } 149 for (int i = 1; i < nodes.length; i++) { 151 if (nodes[i] == null) continue; if (root == null) { 153 root = tail = nodes[i]; 155 } 156 else if (tail == null) { 157 root.setFirstChild(nodes[i]); 158 tail = root.getFirstChild(); 159 } 160 else { 161 tail.setNextSibling(nodes[i]); 162 tail = tail.getNextSibling(); 163 } 164 while (tail.getNextSibling() != null) { 166 tail = tail.getNextSibling(); 167 } 168 } 169 return root; 170 } 171 172 175 public AST make(ASTArray nodes) { 176 return make(nodes.array); 177 } 178 179 180 public void makeASTRoot(ASTPair currentAST, AST root) { 181 if (root != null) { 182 root.addChild(currentAST.root); 184 currentAST.child = currentAST.root; 186 currentAST.advanceChildToEnd(); 187 currentAST.root = root; 189 } 190 } 191 192 public void setASTNodeType(String t) { 193 theASTNodeType = t; 194 try { 195 theASTNodeTypeClass = Class.forName(t); } 197 catch (Exception e) { 198 error("Can't find/access AST Node type" + t); 202 } 203 } 204 205 209 public void error(String e) { 210 System.err.println(e); 211 } 212 } 213 | Popular Tags |