Search in sources :

Example 16 with ATN

use of org.antlr.v4.runtime.atn.ATN in project antlr4 by antlr.

the class BaseGoTest 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 17 with ATN

use of org.antlr.v4.runtime.atn.ATN in project antlr4 by antlr.

the class BaseJavaTest 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) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 18 with ATN

use of org.antlr.v4.runtime.atn.ATN in project antlr4 by antlr.

the class BaseJavaTest method checkRuleDFA.

List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(gtext, equeue);
    ATN atn = createATN(g, false);
    ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
    if (s == null) {
        System.err.println("no such rule: " + ruleName);
        return null;
    }
    ATNState t = s.transition(0).target;
    if (!(t instanceof DecisionState)) {
        System.out.println(ruleName + " has no decision");
        return null;
    }
    DecisionState blk = (DecisionState) t;
    checkRuleDFA(g, blk, expecting);
    return equeue.all;
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) DecisionState(org.antlr.v4.runtime.atn.DecisionState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 19 with ATN

use of org.antlr.v4.runtime.atn.ATN in project antlr4 by antlr.

the class BaseCppTest method checkRuleDFA.

List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(gtext, equeue);
    ATN atn = createATN(g, false);
    ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
    if (s == null) {
        System.err.println("no such rule: " + ruleName);
        return null;
    }
    ATNState t = s.transition(0).target;
    if (!(t instanceof DecisionState)) {
        System.out.println(ruleName + " has no decision");
        return null;
    }
    DecisionState blk = (DecisionState) t;
    checkRuleDFA(g, blk, expecting);
    return equeue.all;
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) DecisionState(org.antlr.v4.runtime.atn.DecisionState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 20 with ATN

use of org.antlr.v4.runtime.atn.ATN in project antlr4 by antlr.

the class BaseCppTest method createATN.

protected ATN createATN(Grammar g, boolean useSerializer) {
    if (g.atn == null) {
        semanticProcess(g);
        assertEquals(0, g.tool.getNumErrors());
        ParserATNFactory f;
        if (g.isLexer()) {
            f = new LexerATNFactory((LexerGrammar) g);
        } else {
            f = new ParserATNFactory(g);
        }
        g.atn = f.createATN();
        assertEquals(0, g.tool.getNumErrors());
    }
    ATN atn = g.atn;
    if (useSerializer) {
        char[] serialized = ATNSerializer.getSerializedAsChars(atn);
        return new ATNDeserializer().deserialize(serialized);
    }
    return atn;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Aggregations

ATN (org.antlr.v4.runtime.atn.ATN)73 LexerGrammar (org.antlr.v4.tool.LexerGrammar)48 Test (org.junit.Test)41 ATNState (org.antlr.v4.runtime.atn.ATNState)23 Grammar (org.antlr.v4.tool.Grammar)20 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)18 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)16 ArrayList (java.util.ArrayList)13 DFA (org.antlr.v4.runtime.dfa.DFA)13 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)12 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)11 STGroupString (org.stringtemplate.v4.STGroupString)11 DecisionState (org.antlr.v4.runtime.atn.DecisionState)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 Rule (org.antlr.v4.tool.Rule)10 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)8 IntegerList (org.antlr.v4.runtime.misc.IntegerList)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 BitSet (java.util.BitSet)5