Add convenience method.

This commit is contained in:
Terence Parr 2012-07-01 17:36:22 -07:00
parent 37396d6040
commit bb5790d6a9
1 changed files with 15 additions and 0 deletions

View File

@ -32,7 +32,9 @@ import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Interval;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.EmptyStackException; import java.util.EmptyStackException;
import java.util.List;
/** A lexer is recognizer that draws input symbols from a character stream. /** A lexer is recognizer that draws input symbols from a character stream.
* lexer grammars result in a subclass of this object. A Lexer object * lexer grammars result in a subclass of this object. A Lexer object
@ -337,6 +339,19 @@ public abstract class Lexer extends Recognizer<Integer, LexerATNSimulator>
return null; return null;
} }
/** Return a list of all Token objects in input char stream.
* Forces load of all tokens. Does not include EOF token.
*/
public List<? extends Token> getAllTokens() {
List<Token> tokens = new ArrayList<Token>();
Token t = nextToken();
while ( t.getType()!=Token.EOF ) {
tokens.add(t);
t = nextToken();
}
return tokens;
}
public void recover(LexerNoViableAltException e) { public void recover(LexerNoViableAltException e) {
getInterpreter().consume(_input); // skip a char and try again getInterpreter().consume(_input); // skip a char and try again
} }