Top Description Fields Constructors Methods
org.antlr.v4.runtime

public abstract Class Lexer

extends Recognizer<Integer, LexerATNSimulator>
implements TokenSource
Class Inheritance
All Implemented Interfaces
org.antlr.v4.runtime.TokenSource
Known Direct Subclasses
org.antlr.v4.runtime.LexerInterpreter, org.antlr.v4.runtime.tree.xpath.XPathLexer
Imports
org.antlr.v4.runtime.atn.ATN, .LexerATNSimulator, org.antlr.v4.runtime.misc.IntegerStack, .Interval, .Nullable, .Tuple, .Tuple2, java.util.ArrayList, .EmptyStackException, .List

A lexer is recognizer that draws input symbols from a character stream. lexer grammars result in a subclass of this object. A Lexer object uses simplified match() and error recovery mechanisms in the interest of speed.

Field Summary

Modifier and TypeField and Description
public int
_channel

The channel number for the current token

protected TokenFactory
_factory

How to create token objects

public boolean
_hitEOF

Once we see EOF on char stream, next token will be EOF.

public CharStream
public int
public final IntegerStack
public String
_text

You can set the text for the current token to override what is in the input char buffer.

public Token
_token

The goal of all lexer rules/methods is to create a token object.

protected Tuple2<? extends TokenSource, CharStream>
public int
_tokenStartCharIndex

What character index in the stream did the current token start at?

public int
_tokenStartCharPositionInLine

The character position of first character within the line

public int
_tokenStartLine

The line on which the first character of the token resides

public int
_type

The token type for the current token

public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
public static final int
Inherited from org.antlr.v4.runtime.Recognizer:
_interpEOF

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
emit(Token token)

By default does not support multiple emits per nextToken invocation for efficiency reasons.

public Token
emit()

The standard method called to automatically emit a token at the outermost lexical rule.

public Token
public List<? extends Token>
getAllTokens()

Return a list of all Token objects in input char stream.

public int
public String[]
public String
public int
getCharIndex()

What is the index of the current character of lookahead?

public int
getCharPositionInLine()

Implements org.antlr.v4.runtime.TokenSource.getCharPositionInLine.

Get the index into the current line for the current position in the input stream.
public String
public String
public CharStream
getInputStream()

Implements abstract org.antlr.v4.runtime.Recognizer.getInputStream.

Implements org.antlr.v4.runtime.TokenSource.getInputStream.

Get the CharStream from which this token source is currently providing tokens.
public int
getLine()

Implements org.antlr.v4.runtime.TokenSource.getLine.

Get the line number for the current position in the input stream.
public String[]
public String
getSourceName()

Implements org.antlr.v4.runtime.TokenSource.getSourceName.

Gets the name of the underlying input source.
public String
getText()

Return the text matched so far for the current token or any text override.

public Token
getToken()

Override if emitting multiple tokens.

public TokenFactory
getTokenFactory()

Implements org.antlr.v4.runtime.TokenSource.getTokenFactory.

Gets the TokenFactory this token source is currently using for creating Token objects from the input.
public String[]
getTokenNames()

Implements abstract org.antlr.v4.runtime.Recognizer.getTokenNames.

Deprecated
Used to print out token names like ID during debugging and error reporting.
public int
public void
mode(int m)

public void
more()

public Token
nextToken()

Implements org.antlr.v4.runtime.TokenSource.nextToken.

Return a token from this source; i.e., match a token on the char stream.
public void
public int
public void
pushMode(int m)

public void
public void
recover(RecognitionException re)

Lexers can normally match any char in it's vocabulary after matching a token, so do the easy thing and just kill a character and hope it all works out.

public void
public void
setChannel(int channel)

public void
setCharPositionInLine(int charPositionInLine)

public void
setInputStream(CharStream input)

Set the char stream and reset the lexer

public void
setLine(int line)

public void
setText(String text)

Set the complete text of this token; it wipes any previous changes to the text.

public void
setToken(Token _token)

public void
setTokenFactory(TokenFactory
The TokenFactory to use for creating tokens.
factory
)

Implements org.antlr.v4.runtime.TokenSource.setTokenFactory.

Set the TokenFactory this token source should use for creating Token objects from the input.
public void
setType(int ttype)

public void
skip()

Instruct the lexer to skip creating a token for current lexer rule and look for another token.

protected void
Inherited from org.antlr.v4.runtime.Recognizer:
actionaddErrorListenergetATNgetErrorHeadergetErrorListenerDispatchgetErrorListenersgetGrammarFileNamegetInterpretergetParseInfogetRuleIndexMapgetRuleNamesgetSerializedATNgetStategetTokenErrorDisplaygetTokenTypegetTokenTypeMapgetVocabularyprecpredremoveErrorListenerremoveErrorListenerssempredsetInterpretersetState

Field Detail

_channelback to summary
public int _channel

The channel number for the current token

_factoryback to summary
protected TokenFactory _factory

How to create token objects

_hitEOFback to summary
public boolean _hitEOF

Once we see EOF on char stream, next token will be EOF. If you have DONE : EOF ; then you see DONE EOF.

_inputback to summary
public CharStream _input
_modeback to summary
public int _mode
_modeStackback to summary
public final IntegerStack _modeStack
_textback to summary
public String _text

You can set the text for the current token to override what is in the input char buffer. Use setText() or can set this instance var.

_tokenback to summary
public Token _token

The goal of all lexer rules/methods is to create a token object. This is an instance variable as multiple rules may collaborate to create a single token. nextToken will return this object after matching lexer rule(s). If you subclass to allow multiple token emissions, then set this to the last token to be matched or something nonnull so that the auto token emit mechanism will not emit another token.

_tokenFactorySourcePairback to summary
protected Tuple2<? extends TokenSource, CharStream> _tokenFactorySourcePair
_tokenStartCharIndexback to summary
public int _tokenStartCharIndex

What character index in the stream did the current token start at? Needed, for example, to get the text for current token. Set at the start of nextToken.

_tokenStartCharPositionInLineback to summary
public int _tokenStartCharPositionInLine

The character position of first character within the line

_tokenStartLineback to summary
public int _tokenStartLine

The line on which the first character of the token resides

_typeback to summary
public int _type

The token type for the current token

DEFAULT_MODEback to summary
public static final int DEFAULT_MODE
DEFAULT_TOKEN_CHANNELback to summary
public static final int DEFAULT_TOKEN_CHANNEL
HIDDENback to summary
public static final int HIDDEN
MAX_CHAR_VALUEback to summary
public static final int MAX_CHAR_VALUE
MIN_CHAR_VALUEback to summary
public static final int MIN_CHAR_VALUE
MOREback to summary
public static final int MORE
SKIPback to summary
public static final int SKIP

Constructor Detail

Lexerback to summary
public Lexer(CharStream input)

Method Detail

emitback to summary
public void emit(Token token)

By default does not support multiple emits per nextToken invocation for efficiency reasons. Subclass and override this method, nextToken, and getToken (to push tokens into a list and pull from that list rather than a single variable as this implementation does).

emitback to summary
public Token emit()

The standard method called to automatically emit a token at the outermost lexical rule. The token object should point into the char buffer start..stop. If there is a text override in 'text', use that to set the token's text. Override this method to emit custom Token objects or provide a new factory.

emitEOFback to summary
public Token emitEOF()
getAllTokensback to summary
public List<? extends Token> getAllTokens()

Return a list of all Token objects in input char stream. Forces load of all tokens. Does not include EOF token.

getChannelback to summary
public int getChannel()
getChannelNamesback to summary
public String[] getChannelNames()
Annotations
@Nullable
getCharErrorDisplayback to summary
public String getCharErrorDisplay(int c)
getCharIndexback to summary
public int getCharIndex()

What is the index of the current character of lookahead?

getCharPositionInLineback to summary
public int getCharPositionInLine()

Implements org.antlr.v4.runtime.TokenSource.getCharPositionInLine.

Doc from org.antlr.v4.runtime.TokenSource.getCharPositionInLine.

Get the index into the current line for the current position in the input stream. The first character on a line has position 0.

Returns:int

The line number for the current position in the input stream, or -1 if the current token source does not track character positions.

Annotations
@Override
getErrorDisplayback to summary
public String getErrorDisplay(String s)
getErrorDisplayback to summary
public String getErrorDisplay(int c)
getInputStreamback to summary
public CharStream getInputStream()

Implements abstract org.antlr.v4.runtime.Recognizer.getInputStream.

Implements org.antlr.v4.runtime.TokenSource.getInputStream.

Doc from org.antlr.v4.runtime.TokenSource.getInputStream.

Get the CharStream from which this token source is currently providing tokens.

Returns:CharStream

The CharStream associated with the current position in the input, or null if no input stream is available for the token source.

Annotations
@Override
getLineback to summary
public int getLine()

Implements org.antlr.v4.runtime.TokenSource.getLine.

Doc from org.antlr.v4.runtime.TokenSource.getLine.

Get the line number for the current position in the input stream. The first line in the input is line 1.

Returns:int

The line number for the current position in the input stream, or 0 if the current token source does not track line numbers.

Annotations
@Override
getModeNamesback to summary
public String[] getModeNames()
Annotations
@Nullable
getSourceNameback to summary
public String getSourceName()

Implements org.antlr.v4.runtime.TokenSource.getSourceName.

Doc from org.antlr.v4.runtime.TokenSource.getSourceName.

Gets the name of the underlying input source. This method returns a non-null, non-empty string. If such a name is not known, this method returns IntStream#UNKNOWN_SOURCE_NAME.

Annotations
@Override
getTextback to summary
public String getText()

Return the text matched so far for the current token or any text override.

getTokenback to summary
public Token getToken()

Override if emitting multiple tokens.

getTokenFactoryback to summary
public TokenFactory getTokenFactory()

Implements org.antlr.v4.runtime.TokenSource.getTokenFactory.

Doc from org.antlr.v4.runtime.TokenSource.getTokenFactory.

Gets the TokenFactory this token source is currently using for creating Token objects from the input.

Returns:TokenFactory

The TokenFactory currently used by this token source.

Annotations
@Override
getTokenNamesback to summary
public String[] getTokenNames()

Implements abstract org.antlr.v4.runtime.Recognizer.getTokenNames.

Deprecated

Used to print out token names like ID during debugging and error reporting. The generated parsers implement a method that overrides this to point to their String[] tokenNames.

Annotations
@Override
@Deprecated
getTypeback to summary
public int getType()
modeback to summary
public void mode(int m)
moreback to summary
public void more()
nextTokenback to summary
public Token nextToken()

Implements org.antlr.v4.runtime.TokenSource.nextToken.

Return a token from this source; i.e., match a token on the char stream.

Annotations
@Override
notifyListenersback to summary
public void notifyListeners(LexerNoViableAltException e)
popModeback to summary
public int popMode()
pushModeback to summary
public void pushMode(int m)
recoverback to summary
public void recover(LexerNoViableAltException e)
recoverback to summary
public void recover(RecognitionException re)

Lexers can normally match any char in it's vocabulary after matching a token, so do the easy thing and just kill a character and hope it all works out. You can instead use the rule invocation stack to do sophisticated error recovery if you are in a fragment rule.

resetback to summary
public void reset()
setChannelback to summary
public void setChannel(int channel)
setCharPositionInLineback to summary
public void setCharPositionInLine(int charPositionInLine)
setInputStreamback to summary
public void setInputStream(CharStream input)

Set the char stream and reset the lexer

setLineback to summary
public void setLine(int line)
setTextback to summary
public void setText(String text)

Set the complete text of this token; it wipes any previous changes to the text.

setTokenback to summary
public void setToken(Token _token)
setTokenFactoryback to summary
public void setTokenFactory(TokenFactory factory)

Implements org.antlr.v4.runtime.TokenSource.setTokenFactory.

Doc from org.antlr.v4.runtime.TokenSource.setTokenFactory.

Set the TokenFactory this token source should use for creating Token objects from the input.

Parameters
factory:TokenFactory

The TokenFactory to use for creating tokens.

Annotations
@Override
setTypeback to summary
public void setType(int ttype)
skipback to summary
public void skip()

Instruct the lexer to skip creating a token for current lexer rule and look for another token. nextToken() knows to keep looking when a lexer rule finishes with token set to SKIP_TOKEN. Recall that if token==null at end of any token rule, it creates one for you and emits it.

validateInputStreamback to summary
protected void validateInputStream(ATN atn, CharStream input)