use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestLookaheadTrees method testAlts.
@Test
public void testAlts() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : e SEMI EOF ;\n" + "e : ID DOT ID\n" + " | ID LPAREN RPAREN\n" + " ;\n", lg);
String startRuleName = "s";
int decision = 0;
testLookaheadTrees(lg, g, "a.b;", startRuleName, decision, new String[] { "(e:1 a . b)", "(e:2 a <error .>)" });
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestLookaheadTrees method testIncludeEOF.
@Test
public void testIncludeEOF() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : e ;\n" + "e : ID DOT ID EOF\n" + " | ID DOT ID EOF\n" + " ;\n", lg);
int decision = 0;
testLookaheadTrees(lg, g, "a.b", "s", decision, new String[] { "(e:1 a . b <EOF>)", "(e:2 a . b <EOF>)" });
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestLookaheadTrees method testLookaheadTrees.
public void testLookaheadTrees(LexerGrammar lg, Grammar g, String input, String startRuleName, int decision, String[] expectedTrees) {
int startRuleIndex = g.getRule(startRuleName).index;
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
parser.setProfile(true);
ParseTree t = parser.parse(startRuleIndex);
DecisionInfo decisionInfo = parser.getParseInfo().getDecisionInfo()[decision];
LookaheadEventInfo lookaheadEventInfo = decisionInfo.SLL_MaxLookEvent;
List<ParserRuleContext> lookaheadParseTrees = GrammarParserInterpreter.getLookaheadParseTrees(g, parser, tokens, startRuleIndex, lookaheadEventInfo.decision, lookaheadEventInfo.startIndex, lookaheadEventInfo.stopIndex);
assertEquals(expectedTrees.length, lookaheadParseTrees.size());
for (int i = 0; i < lookaheadParseTrees.size(); i++) {
ParserRuleContext lt = lookaheadParseTrees.get(i);
assertEquals(expectedTrees[i], Trees.toStringTree(lt, nodeTextProvider));
}
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestLookaheadTrees method testAlts2.
@Test
public void testAlts2() throws Exception {
LexerGrammar lg = new LexerGrammar(lexerText);
Grammar g = new Grammar("parser grammar T;\n" + "s : e? SEMI EOF ;\n" + "e : ID\n" + " | e BANG" + " ;\n", lg);
String startRuleName = "s";
// (...)* in e.
int decision = 1;
testLookaheadTrees(lg, g, "a;", startRuleName, decision, new String[] { // Decision for alt 1 is error as no ! char, but alt 2 (exit) is good.
"(e:2 (e:1 a) <error ;>)", // root s:1 is included to show ';' node
"(s:1 (e:1 a) ; <EOF>)" });
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestParseTreeMatcher method testPatternMatchesStartRule2.
@Test
public void testPatternMatchesStartRule2() throws Exception {
String grammar = "grammar X2;\n" + "s : ID '=' expr ';' | expr ';' ;\n" + "expr : ID | INT ;\n" + "ID : [a-z]+ ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\r\\n\\t]+ -> skip ;\n";
boolean ok = rawGenerateAndBuildRecognizer("X2.g4", grammar, "X2Parser", "X2Lexer", false);
assertTrue(ok);
ParseTreePatternMatcher m = getPatternMatcher("X2");
boolean failed = false;
try {
m.compile("<ID> <ID> ;", m.getParser().getRuleIndex("s"));
} catch (NoViableAltException e) {
failed = true;
}
assertTrue(failed);
}
Aggregations