00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 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 public class DebugTokenStream implements TokenStream { 00033 protected DebugEventListener dbg; 00034 public TokenStream input; 00035 protected boolean initialStreamState = true; 00036 00038 protected int lastMarker; 00039 00040 public DebugTokenStream(TokenStream input, DebugEventListener dbg) { 00041 this.input = input; 00042 setDebugListener(dbg); 00043 // force TokenStream to get at least first valid token 00044 // so we know if there are any hidden tokens first in the stream 00045 input.LT(1); 00046 } 00047 00048 public void setDebugListener(DebugEventListener dbg) { 00049 this.dbg = dbg; 00050 } 00051 00052 public void consume() { 00053 if ( initialStreamState ) { 00054 consumeInitialHiddenTokens(); 00055 } 00056 int a = input.index(); 00057 Token t = input.LT(1); 00058 input.consume(); 00059 int b = input.index(); 00060 dbg.consumeToken(t); 00061 if ( b>a+1 ) { 00062 // then we consumed more than one token; must be off channel tokens 00063 for (int i=a+1; i<b; i++) { 00064 dbg.consumeHiddenToken(input.get(i)); 00065 } 00066 } 00067 } 00068 00069 /* consume all initial off-channel tokens */ 00070 protected void consumeInitialHiddenTokens() { 00071 int firstOnChannelTokenIndex = input.index(); 00072 for (int i=0; i<firstOnChannelTokenIndex; i++) { 00073 dbg.consumeHiddenToken(input.get(i)); 00074 } 00075 initialStreamState = false; 00076 } 00077 00078 public Token LT(int i) { 00079 if ( initialStreamState ) { 00080 consumeInitialHiddenTokens(); 00081 } 00082 dbg.LT(i, input.LT(i)); 00083 return input.LT(i); 00084 } 00085 00086 public int LA(int i) { 00087 if ( initialStreamState ) { 00088 consumeInitialHiddenTokens(); 00089 } 00090 dbg.LT(i, input.LT(i)); 00091 return input.LA(i); 00092 } 00093 00094 public Token get(int i) { 00095 return input.get(i); 00096 } 00097 00098 public int mark() { 00099 lastMarker = input.mark(); 00100 dbg.mark(lastMarker); 00101 return lastMarker; 00102 } 00103 00104 public int index() { 00105 return input.index(); 00106 } 00107 00108 public void rewind(int marker) { 00109 dbg.rewind(marker); 00110 input.rewind(marker); 00111 } 00112 00113 public void rewind() { 00114 dbg.rewind(); 00115 input.rewind(lastMarker); 00116 } 00117 00118 public void release(int marker) { 00119 } 00120 00121 public void seek(int index) { 00122 // TODO: implement seek in dbg interface 00123 // db.seek(index); 00124 input.seek(index); 00125 } 00126 00127 public int size() { 00128 return input.size(); 00129 } 00130 00131 public TokenSource getTokenSource() { 00132 return input.getTokenSource(); 00133 } 00134 00135 public String getSourceName() { 00136 return getTokenSource().getSourceName(); 00137 } 00138 00139 public String toString() { 00140 return input.toString(); 00141 } 00142 00143 public String toString(int start, int stop) { 00144 return input.toString(start,stop); 00145 } 00146 00147 public String toString(Token start, Token stop) { 00148 return input.toString(start,stop); 00149 } 00150 }
1.5.5