Class LexerActionExecutor
- java.lang.Object
-
- org.antlr.v4.runtime.atn.LexerActionExecutor
-
public class LexerActionExecutor extends Object
Represents an executor for a sequence of lexer actions which traversed during the matching operation of a lexer rule (token).The executor tracks position information for position-dependent lexer actions efficiently, ensuring that actions appearing only at the end of the rule do not cause bloating of the
DFA
created for the lexer.- Since:
- 4.2
- Author:
- Sam Harwell
-
-
Constructor Summary
Constructors Constructor Description LexerActionExecutor(LexerAction[] lexerActions)
Constructs an executor for a sequence ofLexerAction
actions.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static LexerActionExecutor
append(LexerActionExecutor lexerActionExecutor, LexerAction lexerAction)
Creates aLexerActionExecutor
which executes the actions for the inputlexerActionExecutor
followed by a specifiedlexerAction
.boolean
equals(Object obj)
void
execute(Lexer lexer, CharStream input, int startIndex)
Execute the actions encapsulated by this executor within the context of a particularLexer
.LexerActionExecutor
fixOffsetBeforeMatch(int offset)
Creates aLexerActionExecutor
which encodes the current offset for position-dependent lexer actions.LexerAction[]
getLexerActions()
Gets the lexer actions to be executed by this executor.int
hashCode()
-
-
-
Constructor Detail
-
LexerActionExecutor
public LexerActionExecutor(LexerAction[] lexerActions)
Constructs an executor for a sequence ofLexerAction
actions.- Parameters:
lexerActions
- The lexer actions to execute.
-
-
Method Detail
-
append
public static LexerActionExecutor append(LexerActionExecutor lexerActionExecutor, LexerAction lexerAction)
Creates aLexerActionExecutor
which executes the actions for the inputlexerActionExecutor
followed by a specifiedlexerAction
.- Parameters:
lexerActionExecutor
- The executor for actions already traversed by the lexer while matching a token within a particularLexerATNConfig
. If this isnull
, the method behaves as though it were an empty executor.lexerAction
- The lexer action to execute after the actions specified inlexerActionExecutor
.- Returns:
- A
LexerActionExecutor
for executing the combine actions oflexerActionExecutor
andlexerAction
.
-
fixOffsetBeforeMatch
public LexerActionExecutor fixOffsetBeforeMatch(int offset)
Creates aLexerActionExecutor
which encodes the current offset for position-dependent lexer actions.Normally, when the executor encounters lexer actions where
LexerAction.isPositionDependent()
returnstrue
, it callsIntStream.seek(int)
on the inputCharStream
to set the input position to the end of the current token. This behavior provides for efficient DFA representation of lexer actions which appear at the end of a lexer rule, even when the lexer rule matches a variable number of characters.Prior to traversing a match transition in the ATN, the current offset from the token start index is assigned to all position-dependent lexer actions which have not already been assigned a fixed offset. By storing the offsets relative to the token start index, the DFA representation of lexer actions which appear in the middle of tokens remains efficient due to sharing among tokens of the same length, regardless of their absolute position in the input stream.
If the current executor already has offsets assigned to all position-dependent lexer actions, the method returns
this
.- Parameters:
offset
- The current offset to assign to all position-dependent lexer actions which do not already have offsets assigned.- Returns:
- A
LexerActionExecutor
which stores input stream offsets for all position-dependent lexer actions.
-
getLexerActions
public LexerAction[] getLexerActions()
Gets the lexer actions to be executed by this executor.- Returns:
- The lexer actions to be executed by this executor.
-
execute
public void execute(Lexer lexer, CharStream input, int startIndex)
Execute the actions encapsulated by this executor within the context of a particularLexer
.This method calls
IntStream.seek(int)
to set the position of theinput
CharStream
prior to callingLexerAction.execute(org.antlr.v4.runtime.Lexer)
on a position-dependent action. Before the method returns, the input position will be restored to the same position it was in when the method was invoked.- Parameters:
lexer
- The lexer instance.input
- The input stream which is the source for the current token. When this method is called, the currentIntStream.index()
forinput
should be the start of the following token, i.e. 1 character past the end of the current token.startIndex
- The token start index. This value may be passed toIntStream.seek(int)
to set theinput
position to the beginning of the token.
-
-