added convenience getText(ctx) method to TokenStream. unlikley to need in CharStream and can get another way

This commit is contained in:
Terence Parr 2012-04-01 12:23:45 -07:00
parent 588e30bfe1
commit e1870d16e9
4 changed files with 18 additions and 7 deletions

View File

@ -267,6 +267,9 @@ public class BufferedTokenStream<T extends Token> implements TokenStream {
return buf.toString();
}
@Override
public String getText(RuleContext ctx) { return getText(ctx.getSourceInterval()); }
@Override
public String getText(Token start, Token stop) {
if ( start!=null && stop!=null ) {

View File

@ -43,11 +43,6 @@ public interface TokenStream extends SymbolStream<Token> {
@Override
public Token LT(int k);
/** How far ahead has the stream been asked to look? The return
* value is a valid index from 0..n-1.
*/
// int range();
/** Get a token at an absolute index i; 0..n-1. This is really only
* needed for profiling and debugging and token stream rewriting.
* If you don't want to buffer up tokens, then this method makes no
@ -72,6 +67,8 @@ public interface TokenStream extends SymbolStream<Token> {
*/
public String getText(Interval interval);
public String getText(RuleContext ctx);
/** Because the user is not required to use a token with an index stored
* in it, we must provide a means for two token objects themselves to
* indicate the start/end location. Most often this will just delegate

View File

@ -88,7 +88,12 @@ public class UnbufferedTokenStream<T extends Token>
throw new UnsupportedOperationException("unbuffered stream can't give strings");
}
@Override
@Override
public String getText(RuleContext ctx) {
throw new UnsupportedOperationException("unbuffered stream can't give strings");
}
@Override
public int LA(int i) { return LT(i).getType(); }
@Override

View File

@ -40,6 +40,7 @@ import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenSource;
import org.antlr.v4.runtime.TokenStream;
@ -1098,7 +1099,12 @@ public abstract class BaseTest {
@Override
public String getText(Interval interval) {
return null;
throw new UnsupportedOperationException("can't give strings");
}
@Override
public String getText(RuleContext ctx) {
throw new UnsupportedOperationException("can't give strings");
}
@Override