1 package org.antlr.works.debugger.input; 2 3 import org.antlr.runtime.Token; 4 import org.antlr.tool.Grammar; 5 import org.antlr.works.awtree.AWTreePanel; 6 import org.antlr.works.debugger.Debugger; 7 import org.antlr.works.debugger.tree.DBTreeNode; 8 import org.antlr.works.debugger.tree.DBTreeToken; 9 import org.antlr.works.prefs.AWPrefs; 10 import org.antlr.works.prefs.AWPrefsDialog; 11 import org.antlr.xjlib.foundation.notification.XJNotificationCenter; 12 import org.antlr.xjlib.foundation.notification.XJNotificationObserver; 13 14 import java.awt.*; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 47 48 public class DBInputProcessorTree implements DBInputProcessor, XJNotificationObserver { 49 50 public AWTreePanel treePanel; 51 public Debugger debugger; 52 53 public InputTreeNode rootNode; 54 public InputTreeNode currentNode; 55 public InputTreeNode lastNode; 56 57 58 public Map <Integer ,NodeInfo> nodeInfoForToken = new HashMap <Integer , NodeInfo>(); 59 60 61 public int line, pos; 62 63 64 public Color nonConsumedColor; 65 public Color consumedColor; 66 public Color ltColor; 67 68 public DBInputProcessorTree(AWTreePanel treePanel, Debugger debugger) { 69 this.treePanel = treePanel; 70 this.debugger = debugger; 71 72 createColors(); 73 74 XJNotificationCenter.defaultCenter().addObserver(this, AWPrefsDialog.NOTIF_PREFS_APPLIED); 75 } 76 77 public void close() { 78 XJNotificationCenter.defaultCenter().removeObserver(this); 79 } 80 81 public void updateTreePanel() { 82 treePanel.refresh(); 83 treePanel.scrollNodeToVisible(lastNode); 84 } 85 86 public void createColors() { 87 nonConsumedColor = AWPrefs.getNonConsumedTokenColor(); 88 consumedColor = AWPrefs.getConsumedTokenColor(); 89 ltColor = AWPrefs.getLookaheadTokenColor(); 90 } 91 92 public void applyColor(Color c) { 93 for (NodeInfo info : nodeInfoForToken.values()) { 94 if (info.node != null) 95 info.node.setColor(c); 96 } 97 } 98 99 public void reset() { 100 nodeInfoForToken.clear(); 101 102 rootNode = createNode(null); 103 treePanel.setRoot(rootNode); 104 105 currentNode = rootNode; 106 lastNode = currentNode; 107 } 108 109 public void removeAllLT() { 110 111 } 112 113 public void rewind(int i) { 114 115 } 116 117 public void rewindAll() { 118 applyColor(nonConsumedColor); 119 120 currentNode = rootNode; 121 lastNode = currentNode; 122 } 123 124 public void LT(Token token) { 125 InputTreeNode node = processToken(token); 126 if(node != null) { 127 lastNode = node; 128 node.setColor(ltColor); 129 } 130 } 131 132 public void consumeToken(Token token, int flavor) { 133 InputTreeNode node = processToken(token); 134 if(node != null) { 135 lastNode = node; 136 node.setColor(consumedColor); 137 } 138 } 139 140 public InputTreeNode processToken(Token token) { 141 142 NodeInfo info = getNode(token); 143 if(info != null) { 144 145 setCurrentNode(info.currentNode); 146 147 return info.node; 148 } 149 150 151 info = new NodeInfo(token); 152 153 switch(token.getType()) { 154 case Token.DOWN: 155 setCurrentNode((InputTreeNode)currentNode.getLastChild()); 156 info.currentNode = currentNode; 157 break; 158 159 case Token.UP: 160 if(currentNode == rootNode) { 161 debugger.warning(this, "UP token applied to the root node!"); 162 } 163 if(currentNode == null) { 164 debugger.warning(this, "CurrentNode is null, use rootNode instead."); 165 setCurrentNode(rootNode); 166 } else { 167 setCurrentNode((InputTreeNode)currentNode.getParent()); 168 } 169 info.currentNode = currentNode; 170 break; 171 172 default: 173 if(currentNode == null) { 174 debugger.warning(this, "CurrentNode is null, use rootNode instead."); 175 setCurrentNode(rootNode); 176 } 177 currentNode.add(info.node = createNode(token)); 178 info.currentNode = currentNode; 179 break; 180 } 181 182 183 DBTreeToken tt = (DBTreeToken)token; 184 nodeInfoForToken.put(tt.ID, info); 185 return info.node; 186 } 187 188 public void setCurrentNode(InputTreeNode node) { 189 currentNode = node; 190 } 191 192 public InputTreeNode createNode(Token token) { 193 InputTreeNode node = new InputTreeNode((DBTreeToken)token, debugger.getGrammar().getANTLRGrammar()); 194 node.setPosition(line, pos); 195 return node; 196 } 197 198 public NodeInfo getNode(Token token) { 199 DBTreeToken tt = (DBTreeToken)token; 200 NodeInfo info = nodeInfoForToken.get(tt.ID); 201 if(info != null && !info.token.toString().equals(token.toString())) { 202 debugger.warning(this, "Duplicate token ID "+tt.ID+" for "+info.token+" <-> "+token); 203 } 204 return info; 205 } 206 207 public void setLocation(int line, int pos) { 208 this.line = line; 209 this.pos = pos; 210 } 211 212 public int getCurrentTokenIndex() { 213 return 0; 214 } 215 216 public DBInputTextTokenInfo getTokenInfoAtTokenIndex(int index) { 217 return null; 218 } 219 220 public DBInputTextTokenInfo getTokenInfoForToken(Token token) { 221 return null; 222 } 223 224 public void notificationFire(Object source, String name) { 225 if(name.equals(AWPrefsDialog.NOTIF_PREFS_APPLIED)) { 226 createColors(); 227 } 228 } 229 230 public boolean isBreakpointAtToken(Token token) { 231 NodeInfo info = getNode(token); 232 return !(info == null || info.node == null) && info.node.breakpoint; 233 } 234 235 public static class NodeInfo { 236 237 238 public Token token; 239 240 241 public InputTreeNode node; 242 243 244 public InputTreeNode currentNode; 245 246 public NodeInfo(Token token) { 247 this.token = token; 248 } 249 250 } 251 252 public class InputTreeNode extends DBTreeNode { 253 254 public boolean breakpoint = false; 255 256 public InputTreeNode(DBTreeToken token, Grammar grammar) { 257 super(token, grammar); 258 } 259 260 public void toggleBreakpoint() { 261 breakpoint = !breakpoint; 262 263 treePanel.getGraphView().repaintNode(this); 264 } 265 266 public Color getColor() { 267 if(breakpoint) 268 return Color.red; 269 else 270 return super.getColor(); 271 } 272 273 public String toString() { 274 if(token != null) 275 return token.getText(); else 277 return "nil"; 278 } 279 280 } 281 } 282 | Popular Tags |