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 public class DebugTokenStream implements TokenStream { 33 protected DebugEventListener dbg; 34 public TokenStream input; 35 protected boolean initialStreamState = true; 36 37 /** Track the last mark() call result value for use in rewind(). */ 38 protected int lastMarker; 39 DebugTokenStream(TokenStream input, DebugEventListener dbg)40 public DebugTokenStream(TokenStream input, DebugEventListener dbg) { 41 this.input = input; 42 setDebugListener(dbg); 43 // force TokenStream to get at least first valid token 44 // so we know if there are any hidden tokens first in the stream 45 input.LT(1); 46 } 47 setDebugListener(DebugEventListener dbg)48 public void setDebugListener(DebugEventListener dbg) { 49 this.dbg = dbg; 50 } 51 52 @Override consume()53 public void consume() { 54 if ( initialStreamState ) { 55 consumeInitialHiddenTokens(); 56 } 57 int a = input.index(); 58 Token t = input.LT(1); 59 input.consume(); 60 int b = input.index(); 61 dbg.consumeToken(t); 62 if ( b>a+1 ) { 63 // then we consumed more than one token; must be off channel tokens 64 for (int i=a+1; i<b; i++) { 65 dbg.consumeHiddenToken(input.get(i)); 66 } 67 } 68 } 69 70 /* consume all initial off-channel tokens */ consumeInitialHiddenTokens()71 protected void consumeInitialHiddenTokens() { 72 int firstOnChannelTokenIndex = input.index(); 73 for (int i=0; i<firstOnChannelTokenIndex; i++) { 74 dbg.consumeHiddenToken(input.get(i)); 75 } 76 initialStreamState = false; 77 } 78 79 @Override 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 88 @Override LA(int i)89 public int LA(int i) { 90 if ( initialStreamState ) { 91 consumeInitialHiddenTokens(); 92 } 93 dbg.LT(i, input.LT(i)); 94 return input.LA(i); 95 } 96 97 @Override get(int i)98 public Token get(int i) { 99 return input.get(i); 100 } 101 102 @Override mark()103 public int mark() { 104 lastMarker = input.mark(); 105 dbg.mark(lastMarker); 106 return lastMarker; 107 } 108 109 @Override index()110 public int index() { 111 return input.index(); 112 } 113 114 @Override range()115 public int range() { 116 return input.range(); 117 } 118 119 @Override rewind(int marker)120 public void rewind(int marker) { 121 dbg.rewind(marker); 122 input.rewind(marker); 123 } 124 125 @Override rewind()126 public void rewind() { 127 dbg.rewind(); 128 input.rewind(lastMarker); 129 } 130 131 @Override release(int marker)132 public void release(int marker) { 133 } 134 135 @Override seek(int index)136 public void seek(int index) { 137 // TODO: implement seek in dbg interface 138 // db.seek(index); 139 input.seek(index); 140 } 141 142 @Override size()143 public int size() { 144 return input.size(); 145 } 146 147 @Override getTokenSource()148 public TokenSource getTokenSource() { 149 return input.getTokenSource(); 150 } 151 152 @Override getSourceName()153 public String getSourceName() { 154 return getTokenSource().getSourceName(); 155 } 156 157 @Override toString()158 public String toString() { 159 return input.toString(); 160 } 161 162 @Override toString(int start, int stop)163 public String toString(int start, int stop) { 164 return input.toString(start,stop); 165 } 166 167 @Override toString(Token start, Token stop)168 public String toString(Token start, Token stop) { 169 return input.toString(start,stop); 170 } 171 } 172