00001 /* 00002 [The "BSD license"] 00003 Copyright (c) 2005-2009 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime.debug; 00029 00030 import org.antlr.runtime.*; 00031 00032 import java.util.List; 00033 00034 public class DebugTokenStream implements TokenStream { 00035 protected DebugEventListener dbg; 00036 public TokenStream input; 00037 protected boolean initialStreamState = true; 00038 00040 protected int lastMarker; 00041 00042 public DebugTokenStream(TokenStream input, DebugEventListener dbg) { 00043 this.input = input; 00044 setDebugListener(dbg); 00045 // force TokenStream to get at least first valid token 00046 // so we know if there are any hidden tokens first in the stream 00047 input.LT(1); 00048 } 00049 00050 public void setDebugListener(DebugEventListener dbg) { 00051 this.dbg = dbg; 00052 } 00053 00054 public void consume() { 00055 if ( initialStreamState ) { 00056 consumeInitialHiddenTokens(); 00057 } 00058 int a = input.index(); 00059 Token t = input.LT(1); 00060 input.consume(); 00061 int b = input.index(); 00062 dbg.consumeToken(t); 00063 if ( b>a+1 ) { 00064 // then we consumed more than one token; must be off channel tokens 00065 for (int i=a+1; i<b; i++) { 00066 dbg.consumeHiddenToken(input.get(i)); 00067 } 00068 } 00069 } 00070 00071 /* consume all initial off-channel tokens */ 00072 protected void consumeInitialHiddenTokens() { 00073 int firstOnChannelTokenIndex = input.index(); 00074 for (int i=0; i<firstOnChannelTokenIndex; i++) { 00075 dbg.consumeHiddenToken(input.get(i)); 00076 } 00077 initialStreamState = false; 00078 } 00079 00080 public Token LT(int i) { 00081 if ( initialStreamState ) { 00082 consumeInitialHiddenTokens(); 00083 } 00084 dbg.LT(i, input.LT(i)); 00085 return input.LT(i); 00086 } 00087 00088 public int LA(int i) { 00089 if ( initialStreamState ) { 00090 consumeInitialHiddenTokens(); 00091 } 00092 dbg.LT(i, input.LT(i)); 00093 return input.LA(i); 00094 } 00095 00096 public Token get(int i) { 00097 return input.get(i); 00098 } 00099 00100 public int mark() { 00101 lastMarker = input.mark(); 00102 dbg.mark(lastMarker); 00103 return lastMarker; 00104 } 00105 00106 public int index() { 00107 return input.index(); 00108 } 00109 00110 public int range() { 00111 return input.range(); 00112 } 00113 00114 public void rewind(int marker) { 00115 dbg.rewind(marker); 00116 input.rewind(marker); 00117 } 00118 00119 public void rewind() { 00120 dbg.rewind(); 00121 input.rewind(lastMarker); 00122 } 00123 00124 public void release(int marker) { 00125 } 00126 00127 public void seek(int index) { 00128 // TODO: implement seek in dbg interface 00129 // db.seek(index); 00130 input.seek(index); 00131 } 00132 00133 public int size() { 00134 return input.size(); 00135 } 00136 00137 public TokenSource getTokenSource() { 00138 return input.getTokenSource(); 00139 } 00140 00141 public String getSourceName() { 00142 return getTokenSource().getSourceName(); 00143 } 00144 00145 public String toString() { 00146 return input.toString(); 00147 } 00148 00149 public String toString(int start, int stop) { 00150 return input.toString(start,stop); 00151 } 00152 00153 public String toString(Token start, Token stop) { 00154 return input.toString(start,stop); 00155 } 00156 }
1.5.5