Search in sources :

Example 21 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class BaseTest method checkRuleDFA.

void checkRuleDFA(Grammar g, DecisionState blk, String expecting) throws Exception {
    DFA dfa = createDFA(g, blk);
    String result = null;
    if (dfa != null)
        result = dfa.toString();
    assertEquals(expecting, result);
}
Also used : STGroupString(org.stringtemplate.v4.STGroupString) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 22 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class BaseTest method getTypesFromString.

// public void minimizeDFA(DFA dfa) {
// DFAMinimizer dmin = new DFAMinimizer(dfa);
// dfa.minimized = dmin.minimize();
// }
IntegerList getTypesFromString(Grammar g, String expecting) {
    IntegerList expectingTokenTypes = new IntegerList();
    if (expecting != null && !expecting.trim().isEmpty()) {
        for (String tname : expecting.replace(" ", "").split(",")) {
            int ttype = g.getTokenType(tname);
            expectingTokenTypes.add(ttype);
        }
    }
    return expectingTokenTypes;
}
Also used : IntegerList(org.antlr.v4.runtime.misc.IntegerList) STGroupString(org.stringtemplate.v4.STGroupString)

Example 23 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class LexerATNFactory method stringLiteral.

/**
 * For a lexer, a string is a sequence of char to match.  That is,
 *  "fog" is treated as 'f' 'o' 'g' not as a single transition in
 *  the DFA.  Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
 *  for n characters.
 */
@Override
public Handle stringLiteral(TerminalAST stringLiteralAST) {
    String chars = stringLiteralAST.getText();
    ATNState left = newState(stringLiteralAST);
    ATNState right;
    String s = CharSupport.getStringFromGrammarStringLiteral(chars);
    if (s == null) {
        // the lexer will already have given an error
        return new Handle(left, left);
    }
    int n = s.length();
    ATNState prev = left;
    right = null;
    for (int i = 0; i < n; ) {
        right = newState(stringLiteralAST);
        int codePoint = s.codePointAt(i);
        prev.addTransition(CodePointTransitions.createWithCodePoint(right, codePoint));
        prev = right;
        i += Character.charCount(codePoint);
    }
    stringLiteralAST.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 24 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class DOTGenerator method getDOT.

public String getDOT(DFA dfa, boolean isLexer) {
    if (dfa.s0.get() == null)
        return null;
    ST dot = stlib.getInstanceOf("dfa");
    dot.add("name", "DFA" + dfa.decision);
    dot.add("startState", dfa.s0.get().stateNumber);
    // dot.add("useBox", Tool.internalOption_ShowATNConfigsInDFA);
    dot.add("rankdir", rankdir);
    // define stop states first; seems to be a bug in DOT where doublecircle
    for (DFAState d : dfa.states.keySet()) {
        if (!d.isAcceptState())
            continue;
        ST st = stlib.getInstanceOf("stopstate");
        st.add("name", "s" + d.stateNumber);
        st.add("label", getStateLabel(d));
        dot.add("states", st);
    }
    for (DFAState d : dfa.states.keySet()) {
        if (d.isAcceptState())
            continue;
        if (d.stateNumber == Integer.MAX_VALUE)
            continue;
        ST st = stlib.getInstanceOf("state");
        st.add("name", "s" + d.stateNumber);
        st.add("label", getStateLabel(d));
        dot.add("states", st);
    }
    for (DFAState d : dfa.states.keySet()) {
        Map<Integer, DFAState> edges = d.getEdgeMap();
        for (Map.Entry<Integer, DFAState> entry : edges.entrySet()) {
            DFAState target = entry.getValue();
            if (target == null)
                continue;
            if (target.stateNumber == Integer.MAX_VALUE)
                continue;
            int ttype = entry.getKey();
            String label = String.valueOf(ttype);
            if (isLexer)
                label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(entry.getKey()).toString()) + "'";
            else if (grammar != null)
                label = grammar.getTokenDisplayName(ttype);
            ST st = stlib.getInstanceOf("edge");
            st.add("label", label);
            st.add("src", "s" + d.stateNumber);
            st.add("target", "s" + target.stateNumber);
            st.add("arrowhead", arrowhead);
            dot.add("edges", st);
        }
    }
    String output = dot.render();
    return Utils.sortLinesInString(output);
}
Also used : ST(org.stringtemplate.v4.ST) DFAState(org.antlr.v4.runtime.dfa.DFAState) Map(java.util.Map)

Example 25 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class ParserATNSimulator method getStartState.

protected SimulatorState getStartState(@NotNull DFA dfa, @NotNull TokenStream input, @NotNull ParserRuleContext outerContext, boolean useContext) {
    if (!useContext) {
        if (dfa.isPrecedenceDfa()) {
            // the start state for a precedence DFA depends on the current
            // parser precedence, and is provided by a DFA method.
            DFAState state = dfa.getPrecedenceStartState(parser.getPrecedence(), false);
            if (state == null) {
                return null;
            }
            return new SimulatorState(outerContext, state, false, outerContext);
        } else {
            if (dfa.s0.get() == null) {
                return null;
            }
            return new SimulatorState(outerContext, dfa.s0.get(), false, outerContext);
        }
    }
    if (!enable_global_context_dfa) {
        return null;
    }
    ParserRuleContext remainingContext = outerContext;
    assert outerContext != null;
    DFAState s0;
    if (dfa.isPrecedenceDfa()) {
        s0 = dfa.getPrecedenceStartState(parser.getPrecedence(), true);
    } else {
        s0 = dfa.s0full.get();
    }
    while (remainingContext != null && s0 != null && s0.isContextSensitive()) {
        remainingContext = skipTailCalls(remainingContext);
        s0 = s0.getContextTarget(getReturnState(remainingContext));
        if (remainingContext.isEmpty()) {
            assert s0 == null || !s0.isContextSensitive();
        } else {
            remainingContext = remainingContext.getParent();
        }
    }
    if (s0 == null) {
        return null;
    }
    return new SimulatorState(outerContext, s0, useContext, remainingContext);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DFAState(org.antlr.v4.runtime.dfa.DFAState)

Aggregations

DFA (org.antlr.v4.runtime.dfa.DFA)37 DFAState (org.antlr.v4.runtime.dfa.DFAState)28 Test (org.junit.Test)19 Grammar (org.antlr.v4.tool.Grammar)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)16 ArrayList (java.util.ArrayList)14 STGroupString (org.stringtemplate.v4.STGroupString)14 CharStream (org.antlr.v4.runtime.CharStream)13 IOException (java.io.IOException)10 IntegerList (org.antlr.v4.runtime.misc.IntegerList)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)9 InputStream (java.io.InputStream)8 BitSet (java.util.BitSet)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)7 ATN (org.antlr.v4.runtime.atn.ATN)7 Interval (org.antlr.v4.runtime.misc.Interval)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5