Class ATN
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentMap<PredictionContext,PredictionContext> DFA[]final List<DecisionState>Each subrule/rule is a decision point and we must track them so we can go back later and build DFA predictors for them.final ATNTypeThe type of the ATN.private booleanstatic final intFor lexer ATNs, this is an array ofLexerActionobjects which may be referenced by action transitions in the ATN.(package private) final ConcurrentIntIntMapPackage-private primitive LL(1) cache forParserATNSimulator.(package private) short[]Dense LL(1) table:decision * ll1Stride + token → alt.(package private) intColumn count ofll1Dense(maxTokenType + 1).protected final ConcurrentMap<Integer,Integer> LL(1) prediction cache exposed as a JDKConcurrentMapso subclasses and tests keep a stableprotectedsurface.(package private) static final intHighest precedence stored inprecedenceLL1.private static final intSkip precedence tables wider than this many token types.final intThe maximum value for any symbol recognized by a transition in the ATN.final Map<String,TokensStartState> DFA[]final List<TokensStartState>(package private) short[][]Per-decision precedence LL(1) answers.private final ObjectPublish guard forprecedenceLL1.Maps from rule index to starting state number.Maps from rule index to stop state number.int[]For lexer ATNs, this maps the rule index to the resulting token type.(package private) ATNState[]Snapshot ofstatestaken after ATN deserialization. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid(package private) final intcachedPrecedenceAlt(int decision, int precedence, int token) Precedence LL(1) answer learned for(decision, precedence, token), orINVALID_ALT_NUMBERon a miss.(package private) final voidcachePrecedenceAlt(int decision, int precedence, int token, int alt) Record a precedence LL(1) answer.final voidclearDFA()Clears every decision/mode DFA and the LL(1) / context caches.intvoiddefineMode(String name, TokensStartState s) (package private) final voidensureLl1Dense(int decisionCount) (package private) final voidRebuildsstatesSnapshotfromstates.getCachedContext(PredictionContext context) (package private) final ATNStategetCachedState(int stateNumber) State atstateNumber, preferring the frozen snapshot.intgetDecisionState(int decision) final DFA[]getExpectedTokens(int stateNumber, RuleContext context) Computes the set of input symbols which could follow ATN state numberstateNumberin the specified fullcontext.intbooleanCompute the set of valid tokens that can occur starting insand staying in same rule.nextTokens(ATNState s, PredictionContext ctx) Compute the set of valid tokens that can occur starting in states.voidremoveState(ATNState state) voidsetHasUnicodeSMPTransitions(boolean value) (package private) final voidsyncLl1Dense(int packedKey, int alt) Mirror a packed(decision, token)entry intoll1Dense.
-
Field Details
-
INVALID_ALT_NUMBER
public static final int INVALID_ALT_NUMBER- See Also:
-
states
-
statesSnapshot
ATNState[] statesSnapshotSnapshot ofstatestaken after ATN deserialization. Closure pop-return uses this array (plain load) instead ofList.get(int). Invalidated byaddState(org.antlr.v4.runtime.atn.ATNState)/removeState(org.antlr.v4.runtime.atn.ATNState); rebuilt byfreezeStatesSnapshot(). -
decisionToState
Each subrule/rule is a decision point and we must track them so we can go back later and build DFA predictors for them. This includes all the rules, subrules, optional blocks, ()+, ()* etc... -
ruleToStartState
Maps from rule index to starting state number. -
ruleToStopState
Maps from rule index to stop state number. -
modeNameToStartState
-
grammarType
The type of the ATN. -
maxTokenType
public final int maxTokenTypeThe maximum value for any symbol recognized by a transition in the ATN. -
hasUnicodeSMPTransitions
private boolean hasUnicodeSMPTransitions -
ruleToTokenType
public int[] ruleToTokenTypeFor lexer ATNs, this maps the rule index to the resulting token type. For parser ATNs, this maps the rule index to the generated bypass token type if theATNDeserializationOptions.isGenerateRuleBypassTransitions()deserialization option was specified; otherwise, this isnull. -
lexerActions
For lexer ATNs, this is an array ofLexerActionobjects which may be referenced by action transitions in the ATN. -
modeToStartState
-
contextCache
-
decisionToDFA
-
modeToDFA
-
ll1Cache
Package-private primitive LL(1) cache forParserATNSimulator. Copy-on-write, lock-free reads; never appears in public/protected API. -
ll1Dense
short[] ll1DenseDense LL(1) table:decision * ll1Stride + token → alt. Zero is a miss (INVALID_ALT_NUMBER). Package-private; the publicLL1Tableview still wrapsll1Cache. HotParserATNSimulator.adaptivePredict(org.antlr.v4.runtime.TokenStream, int, org.antlr.v4.runtime.ParserRuleContext)reads this array instead of hashing the packed key. -
ll1Stride
int ll1StrideColumn count ofll1Dense(maxTokenType + 1). -
MAX_PRECEDENCE_LL1
static final int MAX_PRECEDENCE_LL1Highest precedence stored inprecedenceLL1. The precedence edge map uses the same closed range0..200.- See Also:
-
MAX_PRECEDENCE_LL1_STRIDE
private static final int MAX_PRECEDENCE_LL1_STRIDESkip precedence tables wider than this many token types.201rows times this stride is about 1.6MB of shorts; real Java and Groovy vocabularies sit far under it.- See Also:
-
precedenceLL1
volatile short[][] precedenceLL1Per-decision precedence LL(1) answers. For decisiond,precedenceLL1[d][precedence * stride + token]is the alternative, and zero is a miss.strideistable.length / (MAX_PRECEDENCE_LL1 + 1)so a lookup does not re-readll1Stride.Filled only when a precedence decision accepts without consuming input and without semantic context, which is the steady state of left-recursive expression loops (the operator-or-stop decision). Shared by every parser that uses this ATN, including parallel file-granularity parses. Element stores are plain shorts (atomic on the JVM); a racing reader may still observe zero and take the DFA. Cleared by
clearDFA(). -
precedenceLL1Lock
Publish guard forprecedenceLL1. Lookups do not take it. -
LL1Table
LL(1) prediction cache exposed as a JDKConcurrentMapso subclasses and tests keep a stableprotectedsurface. Backed byll1Cache(single store): external boxed access pays boxing only when this map is used; the hot path never touchesInteger.The view honors core
Map/ConcurrentMapcontracts (entrySet/keySet/valuesstay consistent withsize;putIfAbsent/replace/removeare atomic with respect to other writers). Iterators are weakly consistent snapshots of the published COW table.
-
-
Constructor Details
-
ATN
Used for runtime deserialization of ATNs from strings
-
-
Method Details
-
clearDFA
public final void clearDFA()Clears every decision/mode DFA and the LL(1) / context caches.When the DFA arrays already match the decision/mode counts, existing
DFAinstances are cleared in place viaDFA.clear()to avoid allocating a new DFA object per decision on every flush. Arrays are only reallocated when their length is stale (for example after ATN structural changes during deserialization). -
cachedPrecedenceAlt
final int cachedPrecedenceAlt(int decision, int precedence, int token) Precedence LL(1) answer learned for(decision, precedence, token), orINVALID_ALT_NUMBERon a miss. -
cachePrecedenceAlt
final void cachePrecedenceAlt(int decision, int precedence, int token, int alt) Record a precedence LL(1) answer. No-op for out-of-range keys, a non-positive alternative, or a stride this table will not hold. The first alternative stored for a pair sticks. -
freezeStatesSnapshot
final void freezeStatesSnapshot()RebuildsstatesSnapshotfromstates. Called after ATN deserialization so prediction can index states withoutList.get(int). -
getCachedState
State atstateNumber, preferring the frozen snapshot.- Parameters:
stateNumber- ATN state number- Returns:
- the state, or
nullif the slot is empty
-
syncLl1Dense
final void syncLl1Dense(int packedKey, int alt) -
ensureLl1Dense
final void ensureLl1Dense(int decisionCount) -
getContextCacheSize
public int getContextCacheSize() -
getCachedContext
-
getDecisionToDFA
-
nextTokens
Compute the set of valid tokens that can occur starting in states. IfctxisPredictionContext.EMPTY_LOCAL, the set of tokens will not include what can follow the rule surroundings. In other words, the set will be restricted to tokens reachable staying withins's rule. -
nextTokens
Compute the set of valid tokens that can occur starting insand staying in same rule.Token.EPSILONis in set if we reach end of rule. -
addState
-
removeState
-
defineMode
-
defineDecisionState
-
getDecisionState
-
getNumberOfDecisions
public int getNumberOfDecisions() -
getExpectedTokens
Computes the set of input symbols which could follow ATN state numberstateNumberin the specified fullcontext. This method considers the complete parser context, but does not evaluate semantic predicates (i.e. all predicates encountered during the calculation are assumed true). If a path in the ATN exists from the starting state to theRuleStopStateof the outermost context without matching any symbols,Token.EOFis added to the returned set.If
contextisnull, it is treated asParserRuleContext.EMPTY.Note that this does NOT give you the set of all tokens that could appear at a given token position in the input phrase. In other words, it does not answer:
"Given a specific partial input phrase, return the set of all tokens that can follow the last token in the input phrase."
The big difference is that with just the input, the parser could land right in the middle of a lookahead decision. Getting all possible tokens given a partial input stream is a separate computation. See https://github.com/antlr/antlr4/issues/1428
For this function, we are specifying an ATN state and call stack to compute what token(s) can come next and specifically: outside of a lookahead decision. That is what you want for error reporting and recovery upon parse error.
- Parameters:
stateNumber- the ATN state numbercontext- the full parse context- Returns:
- The set of potentially valid input symbols which could follow the specified state in the specified context.
- Throws:
IllegalArgumentException- if the ATN does not contain a state with numberstateNumber
-
hasUnicodeSMPTransitions
public boolean hasUnicodeSMPTransitions() -
setHasUnicodeSMPTransitions
public void setHasUnicodeSMPTransitions(boolean value)
-