Class Lexer
java.lang.Object
org.graalvm.shadowed.org.antlr.v4.runtime.Recognizer<Integer,LexerATNSimulator>
org.graalvm.shadowed.org.antlr.v4.runtime.Lexer
- All Implemented Interfaces:
TokenSource
- Direct Known Subclasses:
LexerInterpreter,XPathLexer
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
FieldsModifier and TypeFieldDescriptionintThe channel number for the current tokenprotected TokenFactory<?> How to create token objectsbooleanOnce we see EOF on char stream, next token will be EOF.intfinal IntegerStackYou can set the text for the current token to override what is in the input char buffer.The goal of all lexer rules/methods is to create a token object.protected Pair<TokenSource, CharStream> intWhat character index in the stream did the current token start at? Needed, for example, to get the text for current token.intThe character position of first character within the lineintThe line on which the first character of the token residesintThe token type for the current tokenstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intFields inherited from class org.graalvm.shadowed.org.antlr.v4.runtime.Recognizer
_interp, EOF -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionemit()The standard method called to automatically emit a token at the outermost lexical rule.voidBy default does not support multiple emits per nextToken invocation for efficiency reasons.emitEOF()Return a list of all Token objects in input char stream.intString[]getCharErrorDisplay(int c) intWhat is the index of the current character of lookahead?intGet the index into the current line for the current position in the input stream.getErrorDisplay(int c) Get theCharStreamfrom which this token source is currently providing tokens.intgetLine()Get the line number for the current position in the input stream.String[]Gets the name of the underlying input source.getText()Return the text matched so far for the current token or any text override.getToken()Override if emitting multiple tokens.TokenFactory<? extends Token> Gets theTokenFactorythis token source is currently using for creatingTokenobjects from the input.String[]Deprecated.intgetType()voidmode(int m) voidmore()Return a token from this source; i.e., match a token on the char stream.voidintpopMode()voidpushMode(int m) voidvoidLexers 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.voidreset()voidsetChannel(int channel) voidsetCharPositionInLine(int charPositionInLine) voidsetInputStream(IntStream input) Set the char stream and reset the lexervoidsetLine(int line) voidSet the complete text of this token; it wipes any previous changes to the text.voidvoidsetTokenFactory(TokenFactory<?> factory) Set theTokenFactorythis token source should use for creatingTokenobjects from the input.voidsetType(int ttype) voidskip()Instruct the lexer to skip creating a token for current lexer rule and look for another token.Methods inherited from class org.graalvm.shadowed.org.antlr.v4.runtime.Recognizer
action, addErrorListener, getATN, getErrorHeader, getErrorListenerDispatch, getErrorListeners, getGrammarFileName, getInterpreter, getParseInfo, getRuleIndexMap, getRuleNames, getSerializedATN, getState, getTokenErrorDisplay, getTokenType, getTokenTypeMap, getVocabulary, precpred, removeErrorListener, removeErrorListeners, sempred, setInterpreter, setState
-
Field Details
-
DEFAULT_MODE
public static final int DEFAULT_MODE- See Also:
-
MORE
public static final int MORE- See Also:
-
SKIP
public static final int SKIP- See Also:
-
DEFAULT_TOKEN_CHANNEL
public static final int DEFAULT_TOKEN_CHANNEL- See Also:
-
HIDDEN
public static final int HIDDEN- See Also:
-
MIN_CHAR_VALUE
public static final int MIN_CHAR_VALUE- See Also:
-
MAX_CHAR_VALUE
public static final int MAX_CHAR_VALUE- See Also:
-
_input
-
_tokenFactorySourcePair
-
_factory
How to create token objects -
_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. -
_tokenStartCharIndex
public int _tokenStartCharIndexWhat 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. -
_tokenStartLine
public int _tokenStartLineThe line on which the first character of the token resides -
_tokenStartCharPositionInLine
public int _tokenStartCharPositionInLineThe character position of first character within the line -
_hitEOF
public boolean _hitEOFOnce we see EOF on char stream, next token will be EOF. If you have DONE : EOF ; then you see DONE EOF. -
_channel
public int _channelThe channel number for the current token -
_type
public int _typeThe token type for the current token -
_modeStack
-
_mode
public int _mode -
_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.
-
-
Constructor Details
-
Lexer
public Lexer() -
Lexer
-
-
Method Details
-
reset
public void reset() -
nextToken
Return a token from this source; i.e., match a token on the char stream.- Specified by:
nextTokenin interfaceTokenSource
-
skip
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. -
more
public void more() -
mode
public void mode(int m) -
pushMode
public void pushMode(int m) -
popMode
public int popMode() -
setTokenFactory
Description copied from interface:TokenSourceSet theTokenFactorythis token source should use for creatingTokenobjects from the input.- Specified by:
setTokenFactoryin interfaceTokenSource- Specified by:
setTokenFactoryin classRecognizer<Integer,LexerATNSimulator> - Parameters:
factory- TheTokenFactoryto use for creating tokens.
-
getTokenFactory
Description copied from interface:TokenSourceGets theTokenFactorythis token source is currently using for creatingTokenobjects from the input.- Specified by:
getTokenFactoryin interfaceTokenSource- Specified by:
getTokenFactoryin classRecognizer<Integer,LexerATNSimulator> - Returns:
- The
TokenFactorycurrently used by this token source.
-
setInputStream
Set the char stream and reset the lexer- Specified by:
setInputStreamin classRecognizer<Integer,LexerATNSimulator>
-
getSourceName
Description copied from interface:TokenSourceGets 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 returnsIntStream.UNKNOWN_SOURCE_NAME.- Specified by:
getSourceNamein interfaceTokenSource
-
getInputStream
Description copied from interface:TokenSourceGet theCharStreamfrom which this token source is currently providing tokens.- Specified by:
getInputStreamin interfaceTokenSource- Specified by:
getInputStreamin classRecognizer<Integer,LexerATNSimulator> - Returns:
- The
CharStreamassociated with the current position in the input, ornullif no input stream is available for the token source.
-
emit
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). -
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. -
emitEOF
-
getLine
public int getLine()Description copied from interface:TokenSourceGet the line number for the current position in the input stream. The first line in the input is line 1.- Specified by:
getLinein interfaceTokenSource- Returns:
- The line number for the current position in the input stream, or 0 if the current token source does not track line numbers.
-
getCharPositionInLine
public int getCharPositionInLine()Description copied from interface:TokenSourceGet the index into the current line for the current position in the input stream. The first character on a line has position 0.- Specified by:
getCharPositionInLinein interfaceTokenSource- Returns:
- The line number for the current position in the input stream, or -1 if the current token source does not track character positions.
-
setLine
public void setLine(int line) -
setCharPositionInLine
public void setCharPositionInLine(int charPositionInLine) -
getCharIndex
public int getCharIndex()What is the index of the current character of lookahead? -
getText
Return the text matched so far for the current token or any text override. -
setText
Set the complete text of this token; it wipes any previous changes to the text. -
getToken
Override if emitting multiple tokens. -
setToken
-
setType
public void setType(int ttype) -
getType
public int getType() -
setChannel
public void setChannel(int channel) -
getChannel
public int getChannel() -
getChannelNames
-
getModeNames
-
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.- Specified by:
getTokenNamesin classRecognizer<Integer,LexerATNSimulator>
-
getAllTokens
-
recover
-
notifyListeners
-
getErrorDisplay
-
getErrorDisplay
-
getCharErrorDisplay
-
recover
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.
-