Search in sources :

Example 1 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by antlr.

the class BaseBrowserTest method checkRuleATN.

void checkRuleATN(Grammar g, String ruleName, String expecting) {
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    DOTGenerator dot = new DOTGenerator(g);
    System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule(ruleName).index]));
    Rule r = g.getRule(ruleName);
    ATNState startState = atn.ruleToStartState[r.index];
    ATNPrinter serializer = new ATNPrinter(g, startState);
    String result = serializer.asString();
    //System.out.print(result);
    assertEquals(expecting, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) ATNPrinter(org.antlr.v4.automata.ATNPrinter) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) TestRule(org.junit.rules.TestRule) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 2 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class TestATNInterpreter method checkMatchedAlt.

public void checkMatchedAlt(LexerGrammar lg, final Grammar g, String inputString, int expected) {
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn);
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    System.out.println(types);
    g.importVocab(lg);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    IntTokenStream input = new IntTokenStream(types);
    System.out.println("input=" + input.types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    ATNState startState = atn.ruleToStartState[g.getRule("a").index];
    if (startState.transition(0).target instanceof BlockStartState) {
        startState = startState.transition(0).target;
    }
    DOTGenerator dot = new DOTGenerator(g);
    System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule("a").index]));
    Rule r = g.getRule("e");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    int result = interp.matchATN(input, startState);
    assertEquals(expected, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 3 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class TestATNLexerInterpreter method checkLexerMatches.

protected void checkLexerMatches(LexerGrammar lg, String inputString, String expecting) {
    ATN atn = createATN(lg, true);
    CharStream input = CharStreams.fromString(inputString);
    ATNState startState = atn.modeNameToStartState.get("DEFAULT_MODE");
    DOTGenerator dot = new DOTGenerator(lg);
    System.out.println(dot.getDOT(startState, true));
    List<String> tokenTypes = getTokenTypes(lg, atn, input);
    String result = Utils.join(tokenTypes.iterator(), ", ");
    System.out.println(tokenTypes);
    assertEquals(expecting, result);
}
Also used : DOTGenerator(org.antlr.v4.tool.DOTGenerator) ATN(org.antlr.v4.runtime.atn.ATN) CharStream(org.antlr.v4.runtime.CharStream) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 4 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class TestATNSerialization method testNot.

@Test
public void testNot() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A, B, C}\n" + "a : ~A ;");
    String expecting = "max type 3\n" + "0:RULE_START 0\n" + "1:RULE_STOP 0\n" + "2:BASIC 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:0\n" + "0:A..A\n" + "0->2 EPSILON 0,0,0\n" + "2->3 NOT_SET 0,0,0\n" + "3->1 EPSILON 0,0,0\n";
    ATN atn = createATN(g, true);
    DOTGenerator gen = new DOTGenerator(g);
    System.out.println(gen.getDOT(atn.ruleToStartState[0]));
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(g.getRuleNames()), Arrays.asList(g.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : DOTGenerator(org.antlr.v4.tool.DOTGenerator) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Example 5 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class Tool method generateATNs.

public void generateATNs(Grammar g) {
    DOTGenerator dotGenerator = new DOTGenerator(g);
    List<Grammar> grammars = new ArrayList<Grammar>();
    grammars.add(g);
    List<Grammar> imported = g.getAllImportedGrammars();
    if (imported != null)
        grammars.addAll(imported);
    for (Grammar ig : grammars) {
        for (Rule r : ig.rules.values()) {
            try {
                String dot = dotGenerator.getDOT(g.atn.ruleToStartState[r.index], g.isLexer());
                if (dot != null) {
                    writeDOTFile(g, r, dot);
                }
            } catch (IOException ioe) {
                errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
            }
        }
    }
}
Also used : DOTGenerator(org.antlr.v4.tool.DOTGenerator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) IOException(java.io.IOException)

Aggregations

DOTGenerator (org.antlr.v4.tool.DOTGenerator)16 ATN (org.antlr.v4.runtime.atn.ATN)15 Rule (org.antlr.v4.tool.Rule)13 ATNState (org.antlr.v4.runtime.atn.ATNState)11 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 ATNPrinter (org.antlr.v4.automata.ATNPrinter)7 STGroupString (org.stringtemplate.v4.STGroupString)7 Grammar (org.antlr.v4.tool.Grammar)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)4 IntegerList (org.antlr.v4.runtime.misc.IntegerList)4 LexerGrammar (org.antlr.v4.tool.LexerGrammar)4 Test (org.junit.Test)4 TokenStream (org.antlr.v4.runtime.TokenStream)3 DFA (org.antlr.v4.runtime.dfa.DFA)3 TestRule (org.junit.rules.TestRule)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 CharStream (org.antlr.v4.runtime.CharStream)2