Search in sources :

Example 36 with Grammar

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 .>)" });
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 37 with Grammar

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>)" });
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 38 with Grammar

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));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) LookaheadEventInfo(org.antlr.v4.runtime.atn.LookaheadEventInfo)

Example 39 with Grammar

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>)" });
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 40 with Grammar

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);
}
Also used : ParseTreePatternMatcher(org.antlr.v4.runtime.tree.pattern.ParseTreePatternMatcher) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Test(org.junit.Test)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15