Search in sources :

Example 1 with ATNPrinter

use of org.antlr.v4.automata.ATNPrinter 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 ATNPrinter

use of org.antlr.v4.automata.ATNPrinter in project antlr4 by tunnelvisionlabs.

the class TestATNConstruction method checkTokensRule.

void checkTokensRule(LexerGrammar g, String modeName, String expecting) {
    // }
    if (modeName == null)
        modeName = "DEFAULT_MODE";
    if (g.modes.get(modeName) == null) {
        System.err.println("no such mode " + modeName);
        return;
    }
    ParserATNFactory f = new LexerATNFactory(g);
    ATN nfa = f.createATN();
    ATNState startState = nfa.modeNameToStartState.get(modeName);
    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) ATNPrinter(org.antlr.v4.automata.ATNPrinter) ATN(org.antlr.v4.runtime.atn.ATN) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 3 with ATNPrinter

use of org.antlr.v4.automata.ATNPrinter in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method rule.

/* start->ruleblock->end */
@NotNull
@Override
public Handle rule(@NotNull GrammarAST ruleAST, @NotNull String name, @NotNull Handle blk) {
    Rule r = g.getRule(name);
    RuleStartState start = atn.ruleToStartState[r.index];
    epsilon(start, blk.left);
    RuleStopState stop = atn.ruleToStopState[r.index];
    epsilon(blk.right, stop);
    Handle h = new Handle(start, stop);
    // ATNPrinter ser = new ATNPrinter(g, h.left);
    // System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
    ruleAST.atnState = start;
    return h;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 4 with ATNPrinter

use of org.antlr.v4.automata.ATNPrinter in project antlr4 by antlr.

the class ParserATNFactory method rule.

/* start->ruleblock->end */
@Override
public Handle rule(GrammarAST ruleAST, String name, Handle blk) {
    Rule r = g.getRule(name);
    RuleStartState start = atn.ruleToStartState[r.index];
    epsilon(start, blk.left);
    RuleStopState stop = atn.ruleToStopState[r.index];
    epsilon(blk.right, stop);
    Handle h = new Handle(start, stop);
    // ATNPrinter ser = new ATNPrinter(g, h.left);
    // System.out.println(ruleAST.toStringTree()+":\n"+ser.asString());
    ruleAST.atnState = start;
    return h;
}
Also used : RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule)

Example 5 with ATNPrinter

use of org.antlr.v4.automata.ATNPrinter 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)

Aggregations

ATNPrinter (org.antlr.v4.automata.ATNPrinter)9 ATNState (org.antlr.v4.runtime.atn.ATNState)9 Rule (org.antlr.v4.tool.Rule)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 ATN (org.antlr.v4.runtime.atn.ATN)7 STGroupString (org.stringtemplate.v4.STGroupString)7 DOTGenerator (org.antlr.v4.tool.DOTGenerator)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 TestRule (org.junit.rules.TestRule)3 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2 RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 NotNull (org.antlr.v4.runtime.misc.NotNull)1