use of org.antlr.v4.runtime.atn.ATNState 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);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.
the class TestAmbigParseTrees method testInterpAtSpecificAlt.
void testInterpAtSpecificAlt(LexerGrammar lg, Grammar g, String startRule, int startAlt, String input, String expectedParseTree) {
LexerInterpreter lexEngine = lg.createLexerInterpreter(CharStreams.fromString(input));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
ParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
RuleStartState ruleStartState = g.atn.ruleToStartState[g.getRule(startRule).index];
Transition tr = ruleStartState.transition(0);
ATNState t2 = tr.target;
if (!(t2 instanceof BasicBlockStartState)) {
throw new IllegalArgumentException("rule has no decision: " + startRule);
}
parser.addDecisionOverride(((DecisionState) t2).decision, 0, startAlt);
ParseTree t = parser.parse(g.rules.get(startRule).index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
assertEquals(expectedParseTree, Trees.toStringTree(t, nodeTextProvider));
}
use of org.antlr.v4.runtime.atn.ATNState 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);
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.
the class TestATNConstruction method testAplusSingleAltHasPlusASTPointingAtLoopBackState.
@Test
public void testAplusSingleAltHasPlusASTPointingAtLoopBackState() throws Exception {
Grammar g = new Grammar("parser grammar P;\n" + // (RULE a (BLOCK (ALT (+ (BLOCK (ALT A))))))
"s : a B ;\n" + "a : A+;");
String expecting = "RuleStart_a_2->PlusBlockStart_8\n" + "PlusBlockStart_8->s7\n" + "s7-A->BlockEnd_9\n" + "BlockEnd_9->PlusLoopBack_10\n" + "PlusLoopBack_10->PlusBlockStart_8\n" + "PlusLoopBack_10->s11\n" + "s11->RuleStop_a_3\n" + "RuleStop_a_3->s5\n";
checkRuleATN(g, "a", expecting);
// Get all AST -> ATNState relationships. Make sure loopback is covered when no loop entry decision
List<GrammarAST> ruleNodes = g.ast.getNodesWithType(ANTLRParser.RULE);
RuleAST a = (RuleAST) ruleNodes.get(1);
List<GrammarAST> nodesInRule = a.getNodesWithType(null);
Map<GrammarAST, ATNState> covered = new LinkedHashMap<GrammarAST, ATNState>();
for (GrammarAST node : nodesInRule) {
if (node.atnState != null) {
covered.put(node, node.atnState);
}
}
assertEquals("{RULE=2, BLOCK=8, +=10, BLOCK=8, A=7}", covered.toString());
}
use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.
the class BaseTest 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;
}
Aggregations