KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > tivo > DBPlayer


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

31
32 package org.antlr.works.debugger.tivo;
33
34 import org.antlr.runtime.Token;
35 import org.antlr.works.debugger.Debugger;
36 import org.antlr.works.debugger.events.*;
37 import org.antlr.works.debugger.input.DBInputProcessor;
38 import org.antlr.works.debugger.input.DBInputTextTokenInfo;
39
40 import java.util.List JavaDoc;
41 import java.util.Stack JavaDoc;
42
43 public class DBPlayer {
44
45     protected Debugger debugger;
46     protected DBInputProcessor processor;
47
48     protected DBPlayerContextInfo contextInfo;
49     protected Stack JavaDoc<Integer JavaDoc> markStack;
50
51     protected int resyncing = 0;
52     protected int eventPlayedCount = 0;
53
54     public DBPlayer(Debugger debugger) {
55         this.debugger = debugger;
56         contextInfo = new DBPlayerContextInfo();
57         markStack = new Stack JavaDoc<Integer JavaDoc>();
58     }
59
60     public void setInputBuffer(DBInputProcessor processor) {
61         this.processor = processor;
62     }
63
64     public DBPlayerContextInfo getContextInfo() {
65         return contextInfo;
66     }
67     
68     public synchronized void resetPlayEvents(boolean first) {
69         debugger.resetGUI();
70
71         /** Only reset the input text the first time
72             the events are reset (when the debugger starts).
73             Then, keep rewinding the input text so already received
74             tokens are displayed */

75         if(first)
76             processor.reset();
77         else
78             processor.rewindAll();
79
80         contextInfo.clear();
81         markStack.clear();
82
83         resyncing = 0;
84         eventPlayedCount = 0;
85     }
86
87     public void playEvents(List JavaDoc events, boolean reset) {
88         if(reset)
89             resetPlayEvents(false);
90
91         for(int i=eventPlayedCount; i<events.size(); i++) {
92             DBEvent event = (DBEvent)events.get(i);
93
94             try {
95                 playEvent(event);
96             } catch(Exception JavaDoc e) {
97                 debugger.getConsole().print(e);
98             }
99
100             debugger.addEvent(event, contextInfo);
101             if(i == events.size()-1) {
102                 // Last event, play the location
103
playLocation();
104             }
105         }
106         eventPlayedCount = events.size();
107     }
108
109     public void playEvent(DBEvent event) {
110         switch(event.getEventType()) {
111             case DBEvent.ENTER_RULE:
112                 playEnterRule((DBEventEnterRule)event);
113                 break;
114
115             case DBEvent.EXIT_RULE:
116                 playExitRule((DBEventExitRule)event);
117                 break;
118
119             case DBEvent.ENTER_SUBRULE:
120                 playEnterSubrule((DBEventEnterSubRule)event);
121                 break;
122
123             case DBEvent.EXIT_SUBRULE:
124                 playExitSubrule((DBEventExitSubRule)event);
125                 break;
126
127             case DBEvent.ENTER_DECISION:
128                 playEnterDecision((DBEventEnterDecision)event);
129                 break;
130
131             case DBEvent.EXIT_DECISION:
132                 playExitDecision((DBEventExitDecision)event);
133                 break;
134
135             case DBEvent.ENTER_ALT:
136                 playEnterAlt((DBEventEnterAlt)event);
137                 break;
138
139             case DBEvent.LT:
140                 playLT((DBEventLT)event);
141                 break;
142
143             case DBEvent.CONSUME_TOKEN:
144                 playConsumeToken((DBEventConsumeToken)event);
145                 break;
146
147             case DBEvent.CONSUME_HIDDEN_TOKEN:
148                 playConsumeToken((DBEventConsumeHiddenToken)event);
149                 break;
150
151             case DBEvent.LOCATION:
152                 playLocation((DBEventLocation)event);
153                 break;
154
155             case DBEvent.MARK:
156                 playMark((DBEventMark)event);
157                 break;
158
159             case DBEvent.REWIND:
160                 playRewind((DBEventRewind)event);
161                 break;
162
163             case DBEvent.BEGIN_BACKTRACK:
164                 playBeginBacktrack((DBEventBeginBacktrack)event);
165                 break;
166
167             case DBEvent.END_BACKTRACK:
168                 playEndBacktrack((DBEventEndBacktrack)event);
169                 break;
170
171             case DBEvent.RECOGNITION_EXCEPTION:
172                 playRecognitionException((DBEventRecognitionException)event);
173                 break;
174
175             case DBEvent.BEGIN_RESYNC:
176                 playBeginResync();
177                 break;
178
179             case DBEvent.END_RESYNC:
180                 playEndResync();
181                 break;
182
183             case DBEvent.NIL_NODE:
184                 playNilNode((DBEventNilNode)event);
185                 break;
186
187             case DBEvent.CREATE_NODE:
188                 playCreateNode((DBEventCreateNode)event);
189                 break;
190
191             case DBEvent.BECOME_ROOT:
192                 playBecomeRoot((DBEventBecomeRoot)event);
193                 break;
194
195             case DBEvent.ADD_CHILD:
196                 playAddChild((DBEventAddChild)event);
197                 break;
198
199             case DBEvent.SET_TOKEN_BOUNDARIES:
200                 playSetTokenBoundaries((DBEventSetTokenBoundaries)event);
201                 break;
202
203             case DBEvent.TERMINATE:
204                 break;
205         }
206     }
207
208     public void playEnterRule(DBEventEnterRule event) {
209         debugger.playerPushRule(event.name);
210         processor.removeAllLT();
211     }
212
213     public void playExitRule(DBEventExitRule event) {
214         debugger.playerPopRule(event.name);
215         processor.removeAllLT();
216     }
217
218     public void playEnterSubrule(DBEventEnterSubRule event) {
219         contextInfo.enterSubrule(event.decision);
220         processor.removeAllLT();
221     }
222
223     public void playExitSubrule(DBEventExitSubRule event) {
224         contextInfo.exitSubrule();
225         processor.removeAllLT();
226     }
227
228     public void playEnterDecision(DBEventEnterDecision event) {
229         contextInfo.enterDecision(event.decision);
230         processor.removeAllLT();
231     }
232
233     public void playExitDecision(DBEventExitDecision event) {
234         contextInfo.exitDecision();
235         processor.removeAllLT();
236     }
237
238     public void playEnterAlt(DBEventEnterAlt event) {
239         /* Currently ignored */
240     }
241
242     public void playLT(DBEventLT event) {
243         /* Ignore EOF LT */
244         if(event.token.getType() == Token.EOF)
245             return;
246
247         /* Ignore LT with negative index (i.e. LT(-1)) */
248         if(event.index < 0)
249             return;
250
251         /* Ignore LT if they are not part of a decision. contextinfo returns
252          -1 if the stack of decision is empty. */

253         if(contextInfo.getDecision() == -1)
254             return;
255
256         processor.LT(event.token);
257     }
258
259     public void playConsumeToken(DBEventConsumeToken event) {
260         playConsumeToken(event.token, false);
261     }
262
263     public void playConsumeToken(DBEventConsumeHiddenToken event) {
264         playConsumeToken(event.token, true);
265     }
266
267     public void playConsumeToken(Token token, boolean hidden) {
268         if(resyncing > 0) {
269             processor.consumeToken(token, DBInputProcessor.TOKEN_DEAD);
270             return;
271         }
272
273         /* If backtracking add token only */
274         if(contextInfo.isBacktracking()) {
275             debugger.playerConsumeToken(token);
276             return;
277         }
278
279         /* Ignore consume token between mark/rewind */
280         if(!markStack.isEmpty())
281             return;
282
283         /* Add only visible token */
284         if(!hidden)
285             debugger.playerConsumeToken(token);
286
287         /* Consume the token */
288         processor.consumeToken(token, hidden?DBInputProcessor.TOKEN_HIDDEN:DBInputProcessor.TOKEN_NORMAL);
289     }
290
291     protected int lastLocationLine;
292     protected int lastLocationPos;
293
294     public void playLocation(DBEventLocation event) {
295         // Remember the last position in order to display
296
// it when the events are all consumed. This allows
297
// to remove the fast backward/forward movement of the cursor
298
// in the grammar (not needed)
299
lastLocationLine = event.line;
300         lastLocationPos = event.pos;
301
302         debugger.playerSetLocation(lastLocationLine, lastLocationPos);
303         processor.setLocation(lastLocationLine, lastLocationPos);
304     }
305
306     public void playLocation() {
307         debugger.resetMarkLocationInGrammar();
308
309         int index = debugger.computeAbsoluteGrammarIndex(lastLocationLine, lastLocationPos);
310         if(index < 0)
311             return;
312
313         debugger.markLocationInGrammar(index);
314     }
315
316     public void playMark(DBEventMark event) {
317         contextInfo.mark(event.id);
318         markStack.push(processor.getCurrentTokenIndex());
319     }
320
321     public void playRewind(DBEventRewind event) {
322         processor.rewind(markStack.peek());
323         if(!event.rewindToLastMark()) {
324             markStack.pop();
325             contextInfo.rewind();
326         }
327     }
328
329     public void playBeginBacktrack(DBEventBeginBacktrack event) {
330         contextInfo.beginBacktrack(event.level);
331
332         /* Tell the debugger about the backtracking so the parse
333         tree coloring can be properly done */

334         debugger.playerBeginBacktrack(event.level);
335     }
336
337     public void playEndBacktrack(DBEventEndBacktrack event) {
338         contextInfo.endBacktrack();
339
340         /* Tell the debugger about the backtracking so the parse
341         tree coloring can be properly done */

342         debugger.playerEndBacktrack(event.level, event.successful);
343     }
344
345     public void playRecognitionException(DBEventRecognitionException event) {
346         debugger.playerRecognitionException(event.e);
347     }
348
349     public void playBeginResync() {
350         resyncing++;
351     }
352
353     public void playEndResync() {
354         resyncing--;
355     }
356
357     public void playNilNode(DBEventNilNode event) {
358         debugger.playerNilNode(event.id);
359     }
360
361     public void playCreateNode(DBEventCreateNode event) {
362         if(event.tokenIndex == -1) {
363             /** Imaginary token. Use the 'text' and 'type' info instead. */
364             debugger.playerCreateNode(event.id, event.text, event.type);
365         } else {
366             DBInputTextTokenInfo info = processor.getTokenInfoAtTokenIndex(event.tokenIndex);
367             if(info == null)
368                 debugger.getConsole().println("No token info for token index "+event.tokenIndex);
369             else
370                 debugger.playerCreateNode(event.id, info.token);
371         }
372     }
373
374     public void playBecomeRoot(DBEventBecomeRoot event) {
375         debugger.playerBecomeRoot(event.newRootID, event.oldRootID);
376     }
377
378     public void playAddChild(DBEventAddChild event) {
379         debugger.playerAddChild(event.rootID, event.childID);
380     }
381
382     public void playSetTokenBoundaries(DBEventSetTokenBoundaries event) {
383         debugger.playerSetTokenBoundaries(event.id, event.startIndex, event.stopIndex);
384     }
385
386 }
387
Popular Tags