Class LexerATNSimulator
PERF: Reach config sets are retained on the simulator and cleared between
edge computations so the common DFA-fill path avoids per-character
OrderedATNConfigSet allocation. Clear of the retained pool is
occupancy-aware (see ATNConfigSet.clear() /
RetainedConfigSet): empty clear is O(1) for the merge map (no bulk
Arrays.fill); non-empty clear nulls only the used list prefix and,
when few configs remain in a grown open-addressed table, removes known merge
keys (O(n)) instead of filling the full table capacity. Obtain/release under
computeTargetState(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.dfa.DFAState, int) therefore does not pay repeated full-capacity
zeroing. A single lexer (and its simulator) is not used concurrently for
matching, so reuse is safe. Stored DFA states always receive a readonly
ATNConfigSet.clone(boolean) of the reach set, never the reusable
buffer itself.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classWhen we hit an accept state in either the DFA or the ATN, we have to notify the character stream to start buffering characters viaIntStream.mark()and record the current state. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe index of the character relative to the beginning of the line 0..n-1private static final intClosure frames beforeclosureBusyis consulted.Closure busy set for the current outermostclosure(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.atn.ATNConfig, org.antlr.v4.runtime.atn.ATNConfigSet, boolean, boolean, boolean)call.private intstatic final booleanstatic final booleanprotected intline number 1..n within the inputstatic intDeprecated.This field is no longer used.static final intstatic final intprotected intbooleanprotected final LexerATNSimulator.SimStateUsed during DFA/ATN exec to record the most recent accept configuration infoprotected final Lexerprivate final RetainedConfigSetRetained reach set forcomputeTargetState(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.dfa.DFAState, int).protected intThe current token's starting index into the character stream.Fields inherited from class org.antlr.v4.runtime.atn.ATNSimulator
atn, ERROR, RULE_LF_VARIANT_MARKER, RULE_NOLF_VARIANT_MARKER, RULE_VARIANT_DELIMITER, SERIALIZED_UUID, SERIALIZED_VERSION -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaccept(CharStream input, LexerActionExecutor lexerActionExecutor, int startIndex, int index, int line, int charPos) protected DFAStateaddDFAEdge(DFAState from, int t, ATNConfigSet q) protected voidaddDFAEdge(DFAState p, int t, DFAState q) protected DFAStateaddDFAState(ATNConfigSet configs) Add a new DFA state if there isn't one with this set of configurations already.protected voidcaptureSimState(LexerATNSimulator.SimState settings, CharStream input, DFAState dfaState) protected booleanclosure(CharStream input, ATNConfig config, ATNConfigSet configs, boolean currentAltReachedAcceptState, boolean speculative, boolean treatEofAsEpsilon) Since the alternatives within any lexer decision are ordered by preference, this method stops pursuing the closure as soon as an accept state is reached.protected ATNConfigSetcomputeStartState(CharStream input, ATNState p) protected DFAStatecomputeTargetState(CharStream input, DFAState s, int t) Compute a target state for an edge in the DFA, and attempt to add the computed state and corresponding edge to the DFA.voidconsume(CharStream input) (package private) final voidconsume(CharStream input, int curChar) ConsumecurChar(alreadyinput.LA(1)) and update line/column.voidcopyState(LexerATNSimulator simulator) protected booleanevaluatePredicate(CharStream input, int ruleIndex, int predIndex, boolean speculative) Evaluate a predicate specified in the lexer.protected intexecATN(CharStream input, DFAState ds0) protected intfailOrAccept(LexerATNSimulator.SimState prevAccept, CharStream input, ATNConfigSet reach, int t) intfinal DFAgetDFA(int mode) protected ATNConfiggetEpsilonTarget(CharStream input, ATNConfig config, Transition t, ATNConfigSet configs, boolean speculative, boolean treatEofAsEpsilon) protected DFAStategetExistingTargetState(DFAState s, int t) Get an existing target state for an edge in the DFA.intgetLine()protected voidgetReachableConfigSet(CharStream input, ATNConfigSet closure, ATNConfigSet reach, int t) Given a starting configuration set, figure out all ATN configurations we can reach upon inputt.protected ATNStategetReachableTarget(Transition trans, int t) getText(CharStream input) Get the text matched so far for the current token.getTokenName(int t) intmatch(CharStream input, int mode) protected intmatchATN(CharStream input) voidreset()(package private) final ATNConfigSetUnderlying retained reach buffer after firstcomputeTargetState(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.dfa.DFAState, int), elsenull.private booleanreuseClosure(ATNConfig config, boolean currentAltReachedAcceptState) voidsetCharPositionInLine(int charPositionInLine) voidsetLine(int line) Methods inherited from class org.antlr.v4.runtime.atn.ATNSimulator
checkCondition, checkCondition, clearDFA, deserialize, edgeFactory, stateFactory, toInt, toInt32, toLong, toUUID
-
Field Details
-
debug
public static final boolean debug- See Also:
-
dfa_debug
public static final boolean dfa_debug- See Also:
-
MIN_DFA_EDGE
public static final int MIN_DFA_EDGE- See Also:
-
MAX_DFA_EDGE
public static final int MAX_DFA_EDGE- See Also:
-
optimize_tail_calls
public boolean optimize_tail_calls -
recog
-
startIndex
protected int startIndexThe current token's starting index into the character stream. Shared across DFA to ATN simulation in case the ATN fails and the DFA did not have a previous accept state. In this case, we use the ATN-generated exception object. -
line
protected int lineline number 1..n within the input -
charPositionInLine
protected int charPositionInLineThe index of the character relative to the beginning of the line 0..n-1 -
mode
protected int mode -
prevAccept
Used during DFA/ATN exec to record the most recent accept configuration info -
retainedReach
Retained reach set forcomputeTargetState(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.dfa.DFAState, int).RetainedConfigSet.obtain(int)always returns empty;RetainedConfigSet.release()runs infinallyafter each edge. -
closureBusy
Closure busy set for the current outermostclosure(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.atn.ATNConfig, org.antlr.v4.runtime.atn.ATNConfigSet, boolean, boolean, boolean)call. The value is thecurrentAltReachedAcceptStateflag of the most permissive visit (falsemeans the config was added). A revisit is skipped unless a later path is more permissive, which stopsEOF*and epsilon cycles without dropping a diamond that still has to add the config. Warm DFA edge walks do not enterclosure(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.atn.ATNConfig, org.antlr.v4.runtime.atn.ATNConfigSet, boolean, boolean, boolean). -
closureDepth
private int closureDepthNesting depth ofclosure(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.atn.ATNConfig, org.antlr.v4.runtime.atn.ATNConfigSet, boolean, boolean, boolean). Zero outside a closure root. -
CLOSURE_BUSY_DEPTH
private static final int CLOSURE_BUSY_DEPTHClosure frames beforeclosureBusyis consulted. Shallow decisions pay no map probe. Deep enough that a same-config cycle stops well short of the thread stack, including when the ATN itself is large.- See Also:
-
match_calls
Deprecated.This field is no longer used.
-
-
Constructor Details
-
LexerATNSimulator
-
LexerATNSimulator
-
-
Method Details
-
copyState
-
match
-
reset
public void reset()- Specified by:
resetin classATNSimulator
-
matchATN
-
execATN
-
getExistingTargetState
Get an existing target state for an edge in the DFA. If the target state for the edge has not yet been computed or is otherwise not available, this method returnsnull.- Parameters:
s- The current DFA statet- The next input symbol- Returns:
- The existing target DFA state for the given input symbol
t, ornullif the target state for this edge is not already cached
-
consume
ConsumecurChar(alreadyinput.LA(1)) and update line/column. Avoids a secondIntStream.LA(int)on the per-character DFA walk.- Parameters:
input- character streamcurChar- the code point just matched; must not be EOF
-
computeTargetState
@NotNull protected DFAState computeTargetState(@NotNull CharStream input, @NotNull DFAState s, int t) Compute a target state for an edge in the DFA, and attempt to add the computed state and corresponding edge to the DFA.- Parameters:
input- The input streams- The current DFA statet- The next input symbol- Returns:
- The computed target DFA state for the given input symbol
t. Iftdoes not lead to a valid DFA state, this method returnsATNSimulator.ERROR.
-
retainedReachBuffer
Underlying retained reach buffer after firstcomputeTargetState(org.antlr.v4.runtime.CharStream, org.antlr.v4.runtime.dfa.DFAState, int), elsenull. Same-package tests may observe identity and emptiness. -
failOrAccept
protected int failOrAccept(LexerATNSimulator.SimState prevAccept, CharStream input, ATNConfigSet reach, int t) -
getReachableConfigSet
protected void getReachableConfigSet(@NotNull CharStream input, @NotNull ATNConfigSet closure, @NotNull ATNConfigSet reach, int t) Given a starting configuration set, figure out all ATN configurations we can reach upon inputt. Parameterreachis a return parameter. -
accept
protected void accept(@NotNull CharStream input, LexerActionExecutor lexerActionExecutor, int startIndex, int index, int line, int charPos) -
getReachableTarget
-
computeStartState
-
closure
protected boolean closure(@NotNull CharStream input, @NotNull ATNConfig config, @NotNull ATNConfigSet configs, boolean currentAltReachedAcceptState, boolean speculative, boolean treatEofAsEpsilon) Since the alternatives within any lexer decision are ordered by preference, this method stops pursuing the closure as soon as an accept state is reached. After the first accept state is reached by depth-first search fromconfig, all other (potentially reachable) states for this rule would have a lower priority.- Returns:
trueif an accept state is reached, otherwisefalse.
-
reuseClosure
- Returns:
truewhenconfigwas already closed under an equally or more permissive accept flag
-
getEpsilonTarget
@Nullable protected ATNConfig getEpsilonTarget(@NotNull CharStream input, @NotNull ATNConfig config, @NotNull Transition t, @NotNull ATNConfigSet configs, boolean speculative, boolean treatEofAsEpsilon) -
evaluatePredicate
protected boolean evaluatePredicate(@NotNull CharStream input, int ruleIndex, int predIndex, boolean speculative) Evaluate a predicate specified in the lexer.If
speculativeistrue, this method was called beforeconsume(org.antlr.v4.runtime.CharStream, int)for the matched character. This method should callconsume(org.antlr.v4.runtime.CharStream, int)before evaluating the predicate to ensure position sensitive values, includingLexer.getText(),Lexer.getLine(), andLexer.getCharPositionInLine(), properly reflect the current lexer state. This method should restoreinputand the simulator to the original state before returning (i.e. undo the actions made by the call toconsume(org.antlr.v4.runtime.CharStream, int).- Parameters:
input- The input stream.ruleIndex- The rule containing the predicate.predIndex- The index of the predicate within the rule.speculative-trueif the current index ininputis one character before the predicate's location.- Returns:
trueif the specified predicate evaluates totrue.
-
captureSimState
protected void captureSimState(@NotNull LexerATNSimulator.SimState settings, @NotNull CharStream input, @NotNull DFAState dfaState) -
addDFAEdge
-
addDFAEdge
-
addDFAState
Add a new DFA state if there isn't one with this set of configurations already. This method also detects the first configuration containing an ATN rule stop state. Later, when traversing the DFA, we will know which rule to accept. -
getDFA
-
getText
Get the text matched so far for the current token. -
getLine
public int getLine() -
setLine
public void setLine(int line) -
getCharPositionInLine
public int getCharPositionInLine() -
setCharPositionInLine
public void setCharPositionInLine(int charPositionInLine) -
consume
-
getTokenName
-