use of org.antlr.v4.runtime.dfa.DFAState in project antlr4 by tunnelvisionlabs.
the class LexerATNSimulator method 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.
*/
@NotNull
protected DFAState addDFAState(@NotNull ATNConfigSet configs) {
/* the lexer evaluates predicates on-the-fly; by this point configs
* should not contain any configurations with unevaluated predicates.
*/
assert !configs.hasSemanticContext();
DFAState proposed = new DFAState(atn.modeToDFA[mode], configs);
DFAState existing = atn.modeToDFA[mode].states.get(proposed);
if (existing != null)
return existing;
configs.optimizeConfigs(this);
DFAState newState = new DFAState(atn.modeToDFA[mode], configs.clone(true));
ATNConfig firstConfigWithRuleStopState = null;
for (ATNConfig c : configs) {
if (c.getState() instanceof RuleStopState) {
firstConfigWithRuleStopState = c;
break;
}
}
if (firstConfigWithRuleStopState != null) {
int prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.getState().ruleIndex];
LexerActionExecutor lexerActionExecutor = firstConfigWithRuleStopState.getLexerActionExecutor();
newState.setAcceptState(new AcceptStateInfo(prediction, lexerActionExecutor));
}
return atn.modeToDFA[mode].addState(newState);
}
use of org.antlr.v4.runtime.dfa.DFAState in project antlr4 by tunnelvisionlabs.
the class LexerATNSimulator method matchATN.
protected int matchATN(@NotNull CharStream input) {
ATNState startState = atn.modeToStartState.get(mode);
if (debug) {
System.out.format(Locale.getDefault(), "matchATN mode %d start: %s\n", mode, startState);
}
int old_mode = mode;
ATNConfigSet s0_closure = computeStartState(input, startState);
boolean suppressEdge = s0_closure.hasSemanticContext();
if (suppressEdge) {
s0_closure.clearExplicitSemanticContext();
}
DFAState next = addDFAState(s0_closure);
if (!suppressEdge) {
if (!atn.modeToDFA[mode].s0.compareAndSet(null, next)) {
next = atn.modeToDFA[mode].s0.get();
}
}
int predict = execATN(input, next);
if (debug) {
System.out.format(Locale.getDefault(), "DFA after matchATN: %s\n", atn.modeToDFA[old_mode].toLexerString());
}
return predict;
}
use of org.antlr.v4.runtime.dfa.DFAState in project antlr4 by tunnelvisionlabs.
the class LexerATNSimulator method addDFAEdge.
@NotNull
protected DFAState addDFAEdge(@NotNull DFAState from, int t, @NotNull ATNConfigSet q) {
/* leading to this call, ATNConfigSet.hasSemanticContext is used as a
* marker indicating dynamic predicate evaluation makes this edge
* dependent on the specific input sequence, so the static edge in the
* DFA should be omitted. The target DFAState is still created since
* execATN has the ability to resynchronize with the DFA state cache
* following the predicate evaluation step.
*
* TJP notes: next time through the DFA, we see a pred again and eval.
* If that gets us to a previously created (but dangling) DFA
* state, we can continue in pure DFA mode from there.
*/
boolean suppressEdge = q.hasSemanticContext();
if (suppressEdge) {
q.clearExplicitSemanticContext();
}
@NotNull DFAState to = addDFAState(q);
if (suppressEdge) {
return to;
}
addDFAEdge(from, t, to);
return to;
}
use of org.antlr.v4.runtime.dfa.DFAState in project antlr4 by tunnelvisionlabs.
the class ProfilingATNSimulator method getExistingTargetState.
@Override
protected DFAState getExistingTargetState(DFAState previousD, int t) {
// this method is called after each time the input position advances
if (currentState.useContext) {
_llStopIndex = _input.index();
} else {
_sllStopIndex = _input.index();
}
DFAState existingTargetState = super.getExistingTargetState(previousD, t);
if (existingTargetState != null) {
// this method is directly called by execDFA; must construct a SimulatorState
// to represent the current state for this case
currentState = new SimulatorState(currentState.outerContext, existingTargetState, currentState.useContext, currentState.remainingOuterContext);
if (currentState.useContext) {
decisions[currentDecision].LL_DFATransitions++;
} else {
// count only if we transition over a DFA state
decisions[currentDecision].SLL_DFATransitions++;
}
if (existingTargetState == ERROR) {
SimulatorState state = new SimulatorState(currentState.outerContext, previousD, currentState.useContext, currentState.remainingOuterContext);
decisions[currentDecision].errors.add(new ErrorInfo(currentDecision, state, _input, _startIndex, _input.index()));
}
}
return existingTargetState;
}
Aggregations