Search in sources :

Example 36 with Parser

use of org.antlr.v4.runtime.Parser 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 Parser

use of org.antlr.v4.runtime.Parser 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 Parser

use of org.antlr.v4.runtime.Parser 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 Parser

use of org.antlr.v4.runtime.Parser 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 Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestParseTreeMatcher method checkPatternMatch.

public ParseTreeMatch checkPatternMatch(String grammar, String startRule, String input, String pattern, String grammarName, boolean invertMatch) throws Exception {
    String grammarFileName = grammarName + ".g4";
    String parserName = grammarName + "Parser";
    String lexerName = grammarName + "Lexer";
    boolean ok = rawGenerateAndBuildRecognizer(grammarFileName, grammar, parserName, lexerName, false);
    assertTrue(ok);
    ParseTree result = execParser(startRule, input, parserName, lexerName);
    ParseTreePattern p = getPattern(grammarName, pattern, startRule);
    ParseTreeMatch match = p.match(result);
    boolean matched = match.succeeded();
    if (invertMatch)
        assertFalse(matched);
    else
        assertTrue(matched);
    return match;
}
Also used : ParseTreePattern(org.antlr.v4.runtime.tree.pattern.ParseTreePattern) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeMatch(org.antlr.v4.runtime.tree.pattern.ParseTreeMatch)

Aggregations

Test (org.junit.Test)138 Grammar (org.antlr.v4.tool.Grammar)130 LexerGrammar (org.antlr.v4.tool.LexerGrammar)117 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)39 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)33 ParseTree (org.antlr.v4.runtime.tree.ParseTree)31 ATN (org.antlr.v4.runtime.atn.ATN)19 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)16 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)14 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)14 ArrayList (java.util.ArrayList)13 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)13 Parser (org.antlr.v4.runtime.Parser)10 RecognitionException (org.antlr.v4.runtime.RecognitionException)10 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)10 Lexer (org.antlr.v4.runtime.Lexer)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)8 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)8 Token (org.antlr.v4.runtime.Token)8