• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  [The "BSD license"]
3  Copyright (c) 2005-2009 Terence Parr
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14  3. The name of the author may not be used to endorse or promote products
15      derived from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.runtime.debug;
29 
30 import org.antlr.runtime.*;
31 
32 import java.util.List;
33 
34 public class DebugTokenStream implements TokenStream {
35 	protected DebugEventListener dbg;
36 	public TokenStream input;
37 	protected boolean initialStreamState = true;
38 
39 	/** Track the last mark() call result value for use in rewind(). */
40 	protected int lastMarker;
41 
DebugTokenStream(TokenStream input, DebugEventListener dbg)42 	public DebugTokenStream(TokenStream input, DebugEventListener dbg) {
43 		this.input = input;
44 		setDebugListener(dbg);
45 		// force TokenStream to get at least first valid token
46 		// so we know if there are any hidden tokens first in the stream
47 		input.LT(1);
48 	}
49 
setDebugListener(DebugEventListener dbg)50 	public void setDebugListener(DebugEventListener dbg) {
51 		this.dbg = dbg;
52 	}
53 
consume()54 	public void consume() {
55 		if ( initialStreamState ) {
56 			consumeInitialHiddenTokens();
57 		}
58 		int a = input.index();
59 		Token t = input.LT(1);
60 		input.consume();
61 		int b = input.index();
62 		dbg.consumeToken(t);
63 		if ( b>a+1 ) {
64 			// then we consumed more than one token; must be off channel tokens
65 			for (int i=a+1; i<b; i++) {
66 				dbg.consumeHiddenToken(input.get(i));
67 			}
68 		}
69 	}
70 
71 	/* consume all initial off-channel tokens */
consumeInitialHiddenTokens()72 	protected void consumeInitialHiddenTokens() {
73 		int firstOnChannelTokenIndex = input.index();
74 		for (int i=0; i<firstOnChannelTokenIndex; i++) {
75 			dbg.consumeHiddenToken(input.get(i));
76 		}
77 		initialStreamState = false;
78 	}
79 
LT(int i)80 	public Token LT(int i) {
81 		if ( initialStreamState ) {
82 			consumeInitialHiddenTokens();
83 		}
84 		dbg.LT(i, input.LT(i));
85 		return input.LT(i);
86 	}
87 
LA(int i)88 	public int LA(int i) {
89 		if ( initialStreamState ) {
90 			consumeInitialHiddenTokens();
91 		}
92 		dbg.LT(i, input.LT(i));
93 		return input.LA(i);
94 	}
95 
get(int i)96 	public Token get(int i) {
97 		return input.get(i);
98 	}
99 
mark()100 	public int mark() {
101 		lastMarker = input.mark();
102 		dbg.mark(lastMarker);
103 		return lastMarker;
104 	}
105 
index()106 	public int index() {
107 		return input.index();
108 	}
109 
range()110 	public int range() {
111 		return input.range();
112 	}
113 
rewind(int marker)114 	public void rewind(int marker) {
115 		dbg.rewind(marker);
116 		input.rewind(marker);
117 	}
118 
rewind()119 	public void rewind() {
120 		dbg.rewind();
121 		input.rewind(lastMarker);
122 	}
123 
release(int marker)124 	public void release(int marker) {
125 	}
126 
seek(int index)127 	public void seek(int index) {
128 		// TODO: implement seek in dbg interface
129 		// db.seek(index);
130 		input.seek(index);
131 	}
132 
size()133 	public int size() {
134 		return input.size();
135 	}
136 
getTokenSource()137 	public TokenSource getTokenSource() {
138 		return input.getTokenSource();
139 	}
140 
getSourceName()141 	public String getSourceName() {
142 		return getTokenSource().getSourceName();
143 	}
144 
toString()145 	public String toString() {
146 		return input.toString();
147 	}
148 
toString(int start, int stop)149 	public String toString(int start, int stop) {
150 		return input.toString(start,stop);
151 	}
152 
toString(Token start, Token stop)153 	public String toString(Token start, Token stop) {
154 		return input.toString(start,stop);
155 	}
156 }
157