Search in sources :

Example 46 with Parser

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

the class TestGrammarParserInterpreter method testAltsWithLabels.

@Test
public void testAltsWithLabels() throws Exception {
    LexerGrammar lg = new LexerGrammar(lexerText);
    Grammar g = new Grammar("parser grammar T;\n" + "s : ID  # foo\n" + "  | INT # bar\n" + "  ;\n", lg);
    // it won't show the labels here because my simple node text provider above just shows the alternative
    testInterp(lg, g, "s", "a", "(s:1 a)");
    testInterp(lg, g, "s", "3", "(s:2 3)");
}
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 47 with Parser

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

the class TestGrammarParserInterpreter method testAltsAsSet.

@Test
public void testAltsAsSet() throws Exception {
    LexerGrammar lg = new LexerGrammar(lexerText);
    Grammar g = new Grammar("parser grammar T;\n" + "s : ID\n" + "  | INT\n" + "  ;\n", lg);
    testInterp(lg, g, "s", "a", "(s:1 a)");
    testInterp(lg, g, "s", "3", "(s:1 3)");
}
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 48 with Parser

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

the class TestParserInterpreter method testCall2.

@Test
public void testCall2() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : t C ;\n" + "t : u ;\n" + "u : A{;} | B ;\n", lg);
    testInterp(lg, g, "s", "ac", "(s (t (u a)) c)");
    testInterp(lg, g, "s", "bc", "(s (t (u b)) c)");
}
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 49 with Parser

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

the class TestParserInterpreter method testEOFInChild.

@Test
public void testEOFInChild() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : x ;\n" + "x : A EOF ;", lg);
    ParseTree t = testInterp(lg, g, "s", "a", "(s (x a <EOF>))");
    assertEquals("0..1", t.getSourceInterval().toString());
    assertEquals("0..1", t.getChild(0).getSourceInterval().toString());
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 50 with Parser

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

the class TestParserInterpreter method testEmptyStartRule.

@Test
public void testEmptyStartRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s :  ;", lg);
    testInterp(lg, g, "s", "", "s");
    testInterp(lg, g, "s", "a", "s");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

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