Search in sources :

Example 61 with DFA

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

the class BasePythonTest 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) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 62 with DFA

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

the class BasePythonTest 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) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 63 with DFA

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

the class BaseCppTest 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) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 64 with DFA

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

the class BaseCppTest method getTokenTypes.

public List<String> getTokenTypes(LexerGrammar lg, ATN atn, CharStream input) {
    LexerATNSimulator interp = new LexerATNSimulator(atn, new DFA[] { new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, null);
    List<String> tokenTypes = new ArrayList<String>();
    int ttype;
    boolean hitEOF = false;
    do {
        if (hitEOF) {
            tokenTypes.add("EOF");
            break;
        }
        int t = input.LA(1);
        ttype = interp.match(input, Lexer.DEFAULT_MODE);
        if (ttype == Token.EOF) {
            tokenTypes.add("EOF");
        } else {
            tokenTypes.add(lg.typeToTokenList.get(ttype));
        }
        if (t == IntStream.EOF) {
            hitEOF = true;
        }
    } while (ttype != Token.EOF);
    return tokenTypes;
}
Also used : LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) ArrayList(java.util.ArrayList) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 65 with DFA

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

the class ParserATNSimulator method addDFAState.

/**
 * See comment on LexerInterpreter.addDFAState.
 */
@NotNull
protected DFAState addDFAState(@NotNull DFA dfa, @NotNull ATNConfigSet configs, PredictionContextCache contextCache) {
    final boolean enableDfa = enable_global_context_dfa || !configs.isOutermostConfigSet();
    if (enableDfa) {
        if (!configs.isReadOnly()) {
            configs.optimizeConfigs(this);
        }
        DFAState proposed = createDFAState(dfa, configs);
        DFAState existing = dfa.states.get(proposed);
        if (existing != null)
            return existing;
    }
    if (!configs.isReadOnly()) {
        if (configs.getConflictInfo() == null) {
            configs.setConflictInfo(isConflicted(configs, contextCache));
        }
    }
    DFAState newState = createDFAState(dfa, configs.clone(true));
    DecisionState decisionState = atn.getDecisionState(dfa.decision);
    int predictedAlt = getUniqueAlt(configs);
    if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
        newState.setAcceptState(new AcceptStateInfo(predictedAlt));
    } else if (configs.getConflictingAlts() != null) {
        newState.setAcceptState(new AcceptStateInfo(newState.configs.getConflictingAlts().nextSetBit(0)));
    }
    if (newState.isAcceptState() && configs.hasSemanticContext()) {
        predicateDFAState(newState, configs, decisionState.getNumberOfTransitions());
    }
    if (!enableDfa) {
        return newState;
    }
    DFAState added = dfa.addState(newState);
    if (debug && added == newState)
        System.out.println("adding new DFA state: " + newState);
    return added;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) AcceptStateInfo(org.antlr.v4.runtime.dfa.AcceptStateInfo) NotNull(org.antlr.v4.runtime.misc.NotNull)

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