Class DFAState
I use a set of ATNConfig objects not simple states. An ATNConfig is both a state (ala normal conversion) and a RuleContext describing the chain of rules (if any) followed to arrive at that state.
A DFA state may have multiple references to a particular state, but with different ATN contexts (with same or different alts) meaning that state was reached via a different set of rule invocations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMap a predicate to a predicted alternative. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AcceptStateInfoprivate DFAState[]ASCII lexer edges (0..127), ornulluntil the first such edge is installed.final ATNConfigSetprivate AbstractEdgeMap<DFAState>These keys for these edges are the top level element of the global context.private BitSetSymbols that require a global context transition before the symbol edge.private Object[]Identity-indexed lexer edges for code points outside the ASCII table, ornullwhenedgesis not that layout.private AbstractEdgeMap<DFAState>edges.get(symbol)points to target of symbol.int -
Constructor Summary
ConstructorsConstructorDescriptionDFAState(DFA dfa, ATNConfigSet configs) Constructs a newDFAStatefor a DFA.DFAState(EmptyEdgeMap<DFAState> emptyEdges, EmptyEdgeMap<DFAState> emptyContextEdges, ATNConfigSet configs) Constructs a newDFAStatewith explicit initial values for the outgoing edge and context edge maps. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleancontextSymbolAfterEdge(int symbol) Same test asisContextSymbol(int)for a caller that has already readedges(the warm LL loop, which would otherwise load the map twice).private static Object[]directValuesOf(AbstractEdgeMap<DFAState> edgeMap) booleanTwoDFAStateinstances are equal if their ATN configuration sets are the same.final AcceptStateInfogetContextTarget(int invokingState) final LexerActionExecutorfinal DFAStategetLexerTarget(int symbol) Lexer edge lookup.final intfinal DFAStategetTarget(int symbol) PERF:finalso warmexecDFAedge walks can monomorphically inline through toEdgeMap.get(int)(Hash/Array) without a virtual call at this layer.inthashCode()final booleanfinal booleanReturnstrueif this state has no context edges.final booleanfinal booleanisContextSymbol(int symbol) final booleanReturnstrueif this state has no outgoing symbol edges.final voidsetAcceptState(AcceptStateInfo acceptStateInfo) voidsetContextSensitive(ATN atn) final voidsetContextSymbol(int symbol) voidsetContextTarget(int invokingState, DFAState target) final voidtoString()
-
Field Details
-
stateNumber
public int stateNumber -
configs
-
edges
edges.get(symbol)points to target of symbol. -
asciiEdges
ASCII lexer edges (0..127), ornulluntil the first such edge is installed. Lexer source is overwhelmingly in this range (identifiers, whitespace, operators). A hit is a volatile read plus one array load — no hash probe and no second volatile key check.Only lexer maps (
maxIndex > 0xFFFF) publish this. Parser maps and precedence-start maps staynull, sogetTarget(int)is unchanged. Slot writes happen-before the volatile store of this reference;getLexerTarget(int)re-reads it on every character. -
directValues
Identity-indexed lexer edges for code points outside the ASCII table, ornullwhenedgesis not that layout. Parser states leave itnullandgetTarget(int)never reads it. -
acceptStateInfo
-
contextEdges
These keys for these edges are the top level element of the global context. -
contextSymbols
Symbols that require a global context transition before the symbol edge. Copy-on-write so a reader never sees a half-updatedBitSet. Not volatile: the LL walk readsedgesfirst, andsetContextSymbol(int)stores the bit beforesetTarget(int, org.antlr.v4.runtime.dfa.DFAState)publishes that map.nullmeans the state is not context sensitive. SLL does not read this field. -
predicates
-
-
Constructor Details
-
DFAState
Constructs a newDFAStatefor a DFA.This constructor initializes the DFA state using empty edge maps provided by the specified DFA.
- Parameters:
dfa- The DFA.configs- The set of ATN configurations defining this state.
-
DFAState
public DFAState(@NotNull EmptyEdgeMap<DFAState> emptyEdges, @NotNull EmptyEdgeMap<DFAState> emptyContextEdges, @NotNull ATNConfigSet configs) Constructs a newDFAStatewith explicit initial values for the outgoing edge and context edge maps.The empty maps provided to this constructor contain information about the range of edge values which are allowed to be stored in the map. Since edges outside the allowed range are simply dropped from the DFA, this offers several potential benefits:
- In the general case, empty edge maps are initialized to support the range of values expected to be seen during prediction, which is in turn used to minimize the memory overhead associated with a dynamically-constructed DFA.
- As a special case, empty edge maps can be intentionally constructed so the DFA will not store edges with specific values. In the limit, a range which does not contain any allowed edges can be used to prevent the DFA from storing any edges, forcing the prediction algorithm to recompute all transitions as they are needed.
- Parameters:
emptyEdges- The empty edge map.emptyContextEdges- The empty context edge map.configs- The set of ATN configurations defining this state.
-
-
Method Details
-
isContextSensitive
public final boolean isContextSensitive() -
isContextSymbol
public final boolean isContextSymbol(int symbol) -
contextSymbolAfterEdge
public final boolean contextSymbolAfterEdge(int symbol) Same test asisContextSymbol(int)for a caller that has already readedges(the warm LL loop, which would otherwise load the map twice). -
setContextSymbol
public final void setContextSymbol(int symbol) -
setContextSensitive
-
getAcceptStateInfo
-
setAcceptState
-
isAcceptState
public final boolean isAcceptState() -
getPrediction
public final int getPrediction() -
getLexerActionExecutor
-
getTarget
PERF:finalso warmexecDFAedge walks can monomorphically inline through toEdgeMap.get(int)(Hash/Array) without a virtual call at this layer. -
getLexerTarget
Lexer edge lookup. ASCII symbols hitasciiEdges(one volatile read and an array load). Wider code points use an identity table when one has been published, otherwiseEdgeMap.get(int).HashEdgeMap.get(int)stays branch-free so parser edge walks do not pay a layout test. -
setTarget
-
directValuesOf
-
isEdgesEmpty
public final boolean isEdgesEmpty()Returnstrueif this state has no outgoing symbol edges.PERF: Prefer this over
getEdgeMap().isEmpty()on hot paths.getEdgeMap()materializes a boxedMap(historically aTreeMapforHashEdgeMap) solely to answer a boolean question — profiled at ~17% of end-to-end Java parse CPU whenDFA.isEmpty()used that pattern for every precedence-DFA prediction.- Returns:
trueif there are no symbol edges from this state
-
isContextEdgesEmpty
public final boolean isContextEdgesEmpty()Returnstrueif this state has no context edges.Same allocation-avoidance rationale as
isEdgesEmpty().- Returns:
trueif there are no context edges from this state
-
getEdgeMap
-
getContextTarget
-
setContextTarget
-
getContextEdgeMap
-
hashCode
public int hashCode() -
equals
TwoDFAStateinstances are equal if their ATN configuration sets are the same. This method is used to see if a state already exists.Because the number of alternatives and number of ATN configurations are finite, there is a finite number of DFA states that can be processed. This is necessary to show that the algorithm terminates.
Cannot test the DFA state numbers here because in
ParserATNSimulator.addDFAState(org.antlr.v4.runtime.dfa.DFA, org.antlr.v4.runtime.atn.ATNConfigSet, org.antlr.v4.runtime.atn.PredictionContextCache)we need to know if any other state exists that has this exact set of ATN configurations. ThestateNumberis irrelevant. -
toString
-