1 package org.antlr.works.debugger.tree; 2 3 import org.antlr.runtime.Token; 4 import org.antlr.works.debugger.Debugger; 5 6 import java.util.*; 7 37 38 public class DBASTModel { 39 40 41 public Stack<Rule> rules = new Stack<Rule>(); 42 43 44 public Map<Integer , ASTNode> nodesMap = new HashMap<Integer , ASTNode>(); 45 46 public List<DBASTModelListener> listeners = new ArrayList<DBASTModelListener>(); 47 48 private Debugger debugger; 49 50 public DBASTModel(Debugger debugger) { 51 this.debugger = debugger; 52 } 53 54 public void addListener(DBASTModelListener listener) { 55 listeners.add(listener); 56 } 57 58 public void fireDataChanged() { 59 for (DBASTModelListener listener : listeners) { 60 listener.modelChanged(this); 61 } 62 } 63 64 public void clear() { 65 rules.clear(); 66 nodesMap.clear(); 67 fireDataChanged(); 68 } 69 70 71 72 public int getRuleCount() { 73 return rules.size(); 74 } 75 76 public Rule getRuleAtIndex(int index) { 77 if(index < 0 || index >= rules.size()) 78 return null; 79 else 80 return rules.get(index); 81 } 82 83 public int getRootCount() { 84 return getRoots().size(); 85 } 86 87 88 89 public void pushRule(String name) { 90 rules.push(new Rule(name, new Stack<ASTNode>())); 91 } 92 93 public void popRule() { 94 96 if(rules.size() > 1) 97 rules.pop(); 98 } 99 100 public void pushRoot(ASTNode node) { 101 getRoots().push(node); 102 } 103 104 105 public void replaceRoot(ASTNode oldRoot, ASTNode newRoot) { 106 Stack<ASTNode> roots = getRoots(); 107 int index = roots.indexOf(oldRoot); 108 roots.remove(index); 109 roots.add(index, newRoot); 110 } 111 112 113 public void removeRoot(ASTNode node) { 114 getRoots().remove(node); 115 } 116 117 118 119 public void nilNode(int id) { 120 pushRoot(createNilTreeNode(id)); 121 } 122 123 public void createNode(int id, Token token) { 124 createTreeNode(id, token); 125 } 126 127 public void becomeRoot(int newRootID, int oldRootID) { 128 ASTNode newRoot = getTreeNode(newRootID); 129 ASTNode oldRoot = getTreeNode(oldRootID); 130 if(newRoot == null) { 131 debugger.warning(this, "[becomeRoot] New root node "+newRootID+" not found, ignoring."); 132 return; 133 } 134 if(oldRoot == null) { 135 debugger.warning(this, "[becomeRoot] Old root node "+oldRootID+" not found, ignoring."); 136 return; 137 } 138 oldRoot.becomeParent(newRoot); 139 replaceRoot(oldRoot, newRoot); 140 } 141 142 public void addChild(int rootID, int childID) { 143 ASTNode root = getTreeNode(rootID); 144 ASTNode child = getTreeNode(childID); 145 if(root == null) { 146 debugger.warning(this, "[addChild] Root node "+rootID+" not found, ignoring."); 147 return; 148 } 149 if(child == null) { 150 debugger.warning(this, "[addChild] Child node "+childID+" not found, ignoring."); 151 return; 152 } 153 154 removeRoot(child); 155 root.addChild(child); 156 } 157 158 159 160 protected ASTNode createNilTreeNode(int id) { 161 ASTNode node = createTreeNode(id); 162 node.nil = true; 163 return node; 164 } 165 166 protected ASTNode createTreeNode(int id, Token token) { 167 ASTNode node = createTreeNode(id); 168 node.token = token; 169 return node; 170 } 171 172 protected ASTNode createTreeNode(int id) { 173 ASTNode node = new ASTNode(id); 174 nodesMap.put(id, node); 175 return node; 176 } 177 178 protected ASTNode getTreeNode(int id) { 179 return nodesMap.get(id); 180 } 181 182 protected Stack<ASTNode> getRoots() { 183 if(rules.isEmpty()) 184 return null; 185 else 186 return (rules.peek()).roots; 187 } 188 189 public class Rule { 190 191 public String name; 192 public Stack<ASTNode> roots; 193 194 public Rule(String name, Stack<ASTNode> roots) { 195 this.name = name; 196 this.roots = roots; 197 } 198 199 public ASTNode getRootAtIndex(int index) { 200 return roots.get(index); 201 } 202 203 public Stack<ASTNode> getRoots() { 204 return roots; 205 } 206 } 207 208 public class ASTNode extends DBTreeNode { 209 210 public int id; 211 public boolean nil = false; 212 213 public ASTNode parentNode = null; 214 215 public ASTNode(int id) { 216 this.id = id; 217 218 children = new Vector(); 219 } 220 221 222 public void addChild(ASTNode node) { 223 if(node.nil) { 224 225 for (int i = 0; i < node.children.size(); i++) { 226 ASTNode child = (ASTNode) node.children.get(i); 227 child.parentNode = this; 228 children.add(child); 229 } 230 } else { 231 node.parentNode = this; 232 children.add(node); 233 } 234 } 235 236 237 public void removeChild(ASTNode node) { 238 children.remove(node); 239 } 240 241 242 public void becomeParent(ASTNode node) { 243 node.detach(); 244 if(parentNode != null) { 245 parentNode.replaceChild(this, node); 248 } 249 250 node.addChild(this); 251 } 252 253 254 public void replaceChild(ASTNode oldNode, ASTNode newNode) { 255 int index = children.indexOf(oldNode); 256 children.remove(index); 257 if(newNode.nil) { 258 259 children.addAll(index, newNode.children); 260 } else 261 children.add(index, newNode); 262 } 263 264 265 public void detach() { 266 if(parentNode != null) { 267 parentNode.removeChild(this); 268 parentNode = null; 269 } 270 } 271 272 public String toString() { 273 if(nil) 274 return "nil"; 275 else if(token == null) 276 return String.valueOf(id); 277 else 278 return token.getText(); 279 } 280 } 281 } 282 | Popular Tags |