KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > tree > DBASTModel


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

37
38 public class DBASTModel {
39
40     /** Stack of rule. Each rule contains a stack of roots */
41     public Stack<Rule> rules = new Stack<Rule>();
42
43     /** Map of nodes */
44     public Map<Integer JavaDoc, ASTNode> nodesMap = new HashMap<Integer JavaDoc, 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     /* Methods used to query the model */
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     /* Methods used by the debugger */
88
89     public void pushRule(String JavaDoc name) {
90         rules.push(new Rule(name, new Stack<ASTNode>()));
91     }
92
93     public void popRule() {
94         /* Do not pop the start rule: we want to be able to see the AST
95         tree at the end of event stream so we keep the start rule on the stack. */

96         if(rules.size() > 1)
97             rules.pop();
98     }
99
100     public void pushRoot(ASTNode node) {
101         getRoots().push(node);
102     }
103
104     /** Replace a root node by another one */
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     /** Remove a root node */
113     public void removeRoot(ASTNode node) {
114         getRoots().remove(node);
115     }
116
117     /* Methods used by the protocol */
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     /* Utility methods */
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 JavaDoc name;
192         public Stack<ASTNode> roots;
193
194         public Rule(String JavaDoc 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             /** Children is defined in DefaultMutableTreeNode */
218             children = new Vector();
219         }
220
221         /** Add a child */
222         public void addChild(ASTNode node) {
223             if(node.nil) {
224                 /** If the child node is a nil node, add its children only */
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         /** Remove a child */
237         public void removeChild(ASTNode node) {
238             children.remove(node);
239         }
240
241         /** Replace the current parent node with another one */
242         public void becomeParent(ASTNode node) {
243             node.detach();
244             if(parentNode != null) {
245                 // If a node has a parent, replace itself by the new parent node
246
// in the list of its parent's children
247
parentNode.replaceChild(this, node);
248             }
249
250             node.addChild(this);
251         }
252
253         /** Replace a child with another one */
254         public void replaceChild(ASTNode oldNode, ASTNode newNode) {
255             int index = children.indexOf(oldNode);
256             children.remove(index);
257             if(newNode.nil) {
258                 /** If the new node is a nil node, add its children only */
259                 children.addAll(index, newNode.children);
260             } else
261                 children.add(index, newNode);
262         }
263
264         /** Detach this node from its parent */
265         public void detach() {
266             if(parentNode != null) {
267                 parentNode.removeChild(this);
268                 parentNode = null;
269             }
270         }
271
272         public String JavaDoc 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