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 00029 package org.antlr.runtime; 00030 00048 public class CommonTokenStream extends BufferedTokenStream { 00050 protected int channel = Token.DEFAULT_CHANNEL; 00051 00052 public CommonTokenStream() { ; } 00053 00054 public CommonTokenStream(TokenSource tokenSource) { 00055 super(tokenSource); 00056 } 00057 00058 public CommonTokenStream(TokenSource tokenSource, int channel) { 00059 this(tokenSource); 00060 this.channel = channel; 00061 } 00062 00064 public void consume() { 00065 if ( p == -1 ) setup(); 00066 p++; 00067 sync(p); 00068 while ( tokens.get(p).getChannel()!=channel ) { 00069 p++; 00070 sync(p); 00071 } 00072 } 00073 00074 protected Token LB(int k) { 00075 if ( k==0 || (p-k)<0 ) return null; 00076 00077 int i = p; 00078 int n = 1; 00079 // find k good tokens looking backwards 00080 while ( n<=k ) { 00081 // skip off-channel tokens 00082 i = skipOffTokenChannelsReverse(i-1); 00083 n++; 00084 } 00085 if ( i<0 ) return null; 00086 return tokens.get(i); 00087 } 00088 00089 public Token LT(int k) { 00090 //System.out.println("enter LT("+k+")"); 00091 if ( p == -1 ) setup(); 00092 if ( k == 0 ) return null; 00093 if ( k < 0 ) return LB(-k); 00094 int i = p; 00095 int n = 1; // we know tokens[p] is a good one 00096 // find k good tokens 00097 while ( n<k ) { 00098 // skip off-channel tokens 00099 i = skipOffTokenChannels(i+1); 00100 n++; 00101 } 00102 if ( i>range ) range = i; 00103 return tokens.get(i); 00104 } 00105 00109 protected int skipOffTokenChannels(int i) { 00110 sync(i); 00111 while ( tokens.get(i).getChannel()!=channel ) { // also stops at EOF (it's onchannel) 00112 i++; 00113 sync(i); 00114 } 00115 return i; 00116 } 00117 00118 protected int skipOffTokenChannelsReverse(int i) { 00119 while ( i>=0 && ((Token)tokens.get(i)).getChannel()!=channel ) { 00120 i--; 00121 } 00122 return i; 00123 } 00124 00125 protected void setup() { 00126 p = 0; 00127 sync(0); 00128 int i = 0; 00129 while ( tokens.get(i).getChannel()!=channel ) { 00130 i++; 00131 sync(i); 00132 } 00133 p = i; 00134 } 00135 00137 public int getNumberOfOnChannelTokens() { 00138 int n = 0; 00139 fill(); 00140 for (int i = 0; i < tokens.size(); i++) { 00141 Token t = tokens.get(i); 00142 if ( t.getChannel()==channel ) n++; 00143 if ( t.getType()==Token.EOF ) break; 00144 } 00145 return n; 00146 } 00147 00149 public void setTokenSource(TokenSource tokenSource) { 00150 super.setTokenSource(tokenSource); 00151 channel = Token.DEFAULT_CHANNEL; 00152 } 00153 }
1.5.5