Search in sources :

Example 31 with DFAState

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);
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) AcceptStateInfo(org.antlr.v4.runtime.dfa.AcceptStateInfo) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 32 with DFAState

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;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Example 33 with DFAState

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;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) NotNull(org.antlr.v4.runtime.misc.NotNull) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 34 with DFAState

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;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Aggregations

DFAState (org.antlr.v4.runtime.dfa.DFAState)28 NotNull (org.antlr.v4.runtime.misc.NotNull)7 BitSet (java.util.BitSet)6 ArrayList (java.util.ArrayList)5 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5 ATNConfig (org.antlr.v4.runtime.atn.ATNConfig)5 DFA (org.antlr.v4.runtime.dfa.DFA)4 Interval (org.antlr.v4.runtime.misc.Interval)3 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 CharStream (org.antlr.v4.runtime.CharStream)2 Lexer (org.antlr.v4.runtime.Lexer)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 Parser (org.antlr.v4.runtime.Parser)2 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)2 ParserATNSimulator (org.antlr.v4.runtime.atn.ParserATNSimulator)2 AcceptStateInfo (org.antlr.v4.runtime.dfa.AcceptStateInfo)2 IntegerList (org.antlr.v4.runtime.misc.IntegerList)2