KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > input > DBInputProcessorTree


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 JavaDoc;
16 import java.util.Map JavaDoc;
17 /*
18
19 [The "BSD licence"]
20 Copyright (c) 2005-2006 Jean Bovet
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions
25 are met:
26
27 1. Redistributions of source code must retain the above copyright
28 notice, this list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright
30 notice, this list of conditions and the following disclaimer in the
31 documentation and/or other materials provided with the distribution.
32 3. The name of the author may not be used to endorse or promote products
33 derived from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
38 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
39 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 */

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     /** Map of token to tree node information */
58     public Map JavaDoc<Integer JavaDoc,NodeInfo> nodeInfoForToken = new HashMap JavaDoc<Integer JavaDoc, NodeInfo>();
59
60     /** Last position in the grammar received from the parser */
61     public int line, pos;
62
63     /** Node colors */
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         /** Check to see if the token has already been processed */
142         NodeInfo info = getNode(token);
143         if(info != null) {
144             /** Set the current position to the one when the node has been created */
145             setCurrentNode(info.currentNode);
146             /** Return the node itself */
147             return info.node;
148         }
149
150         /** The token hasn't been yet processed */
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         /** Add all new node to the map using their unique ID */
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 JavaDoc source, String JavaDoc 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         /** Token */
238         public Token token;
239
240         /** Node associated with the token. null if associated with token UP or DOWN */
241         public InputTreeNode node;
242
243         /** Current node when the associated node has been created */
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             /** Repaint the node to reflect the new state */
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 JavaDoc toString() {
274             if(token != null)
275                 return token.getText(); //+" <"+grammar.getTokenDisplayName(token.getType())+">"
276
else
277                 return "nil";
278         }
279
280     }
281 }
282
Popular Tags