Search in sources :

Example 6 with DOTGenerator

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

the class BaseJavaTest method checkRuleATN.

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

Example 7 with DOTGenerator

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

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

Example 8 with DOTGenerator

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

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);
    String result = ATNSerializer.getDecoded(atn, 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 9 with DOTGenerator

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

the class BasePythonTest 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 10 with DOTGenerator

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

the class TestExpectedTokens method testFollowIncludedInLeftRecursiveRule.

// Test for https://github.com/antlr/antlr4/issues/1480
// can't reproduce
@Test
public void testFollowIncludedInLeftRecursiveRule() throws Exception {
    String gtext = "grammar T;\n" + "s : expr EOF ;\n" + "expr : L expr R\n" + "     | expr PLUS expr\n" + "     | ID\n" + "     ;\n";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_expr_2->BlockStart_13\n" + "BlockStart_13->s7\n" + "BlockStart_13->s12\n" + "s7-action_1:-1->s8\n" + "s12-ID->BlockEnd_14\n" + "s8-L->s9\n" + "BlockEnd_14->StarLoopEntry_20\n" + "s9-expr->RuleStart_expr_2\n" + "StarLoopEntry_20->StarBlockStart_18\n" + "StarLoopEntry_20->s21\n" + "s10-R->s11\n" + "StarBlockStart_18->s15\n" + "s21->RuleStop_expr_3\n" + "s11->BlockEnd_14\n" + "s15-2 >= _p->s16\n" + "RuleStop_expr_3->s5\n" + "RuleStop_expr_3->s10\n" + "RuleStop_expr_3->BlockEnd_19\n" + "s16-PLUS->s17\n" + "s17-expr->RuleStart_expr_2\n" + "BlockEnd_19->StarLoopBack_22\n" + "StarLoopBack_22->StarLoopEntry_20\n";
    checkRuleATN(g, "expr", atnText);
    ATN atn = g.getATN();
    //		DOTGenerator gen = new DOTGenerator(g);
    //		String dot = gen.getDOT(atn.states.get(2), g.getRuleNames(), false);
    //		System.out.println(dot);
    // Simulate call stack after input '(x' from rule s
    ParserRuleContext callStackFrom_s = new ParserRuleContext(null, 4);
    ParserRuleContext callStackFrom_expr = new ParserRuleContext(callStackFrom_s, 9);
    int afterID = 14;
    IntervalSet tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
    // Simulate call stack after input '(x' from within rule expr
    callStackFrom_expr = new ParserRuleContext(null, 9);
    tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getTokenNames()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) BaseJavaTest(org.antlr.v4.test.runtime.java.BaseJavaTest) Test(org.junit.Test)

Aggregations

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