Package org.antlr.v4.runtime.atn
Class LexerCustomAction
java.lang.Object
org.antlr.v4.runtime.atn.LexerCustomAction
- All Implemented Interfaces:
LexerAction
Executes a custom lexer action by calling
Recognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
with the
rule and action indexes assigned to the custom action. The implementation of
a custom action is added to the generated code for the lexer in an override
of Recognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
when the grammar is compiled.
This class may represent embedded actions created with the {...}
syntax in ANTLR 4, as well as actions created for lexer commands where the
command argument could not be evaluated when the grammar was compiled.
- Since:
- 4.2
- Author:
- Sam Harwell
-
Constructor Summary
ConstructorsConstructorDescriptionLexerCustomAction
(int ruleIndex, int actionIndex) Constructs a custom lexer action with the specified rule and action indexes. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
Execute the lexer action in the context of the specifiedLexer
.int
Gets the action index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.Gets the serialization type of the lexer action.int
Gets the rule index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.int
hashCode()
boolean
Gets whether the lexer action is position-dependent.
-
Constructor Details
-
LexerCustomAction
public LexerCustomAction(int ruleIndex, int actionIndex) Constructs a custom lexer action with the specified rule and action indexes.- Parameters:
ruleIndex
- The rule index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.actionIndex
- The action index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.
-
-
Method Details
-
getRuleIndex
public int getRuleIndex()Gets the rule index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.- Returns:
- The rule index for the custom action.
-
getActionIndex
public int getActionIndex()Gets the action index to use for calls toRecognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
.- Returns:
- The action index for the custom action.
-
getActionType
Gets the serialization type of the lexer action.- Specified by:
getActionType
in interfaceLexerAction
- Returns:
- This method returns
LexerActionType.CUSTOM
.
-
isPositionDependent
public boolean isPositionDependent()Gets whether the lexer action is position-dependent. Position-dependent actions may have different semantics depending on theCharStream
index at the time the action is executed.Custom actions are position-dependent since they may represent a user-defined embedded action which makes calls to methods like
Lexer.getText()
.- Specified by:
isPositionDependent
in interfaceLexerAction
- Returns:
- This method returns
true
.
-
execute
Execute the lexer action in the context of the specifiedLexer
.For position-dependent actions, the input stream must already be positioned correctly prior to calling this method.
Custom actions are implemented by calling
Recognizer.action(org.antlr.v4.runtime.RuleContext, int, int)
with the appropriate rule and action indexes.- Specified by:
execute
in interfaceLexerAction
- Parameters:
lexer
- The lexer instance.
-
hashCode
public int hashCode() -
equals
-