Search in sources :

Example 51 with Grammar

use of org.antlr.v4.tool.Grammar 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 52 with Grammar

use of org.antlr.v4.tool.Grammar 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 53 with Grammar

use of org.antlr.v4.tool.Grammar 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 54 with Grammar

use of org.antlr.v4.tool.Grammar 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)

Example 55 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestParserInterpreter method testStarAorB.

@Test
public void testStarAorB() 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 : (A{;}|B)* C ;\n", lg);
    testInterp(lg, g, "s", "c", "(s c)");
    testInterp(lg, g, "s", "ac", "(s a c)");
    testInterp(lg, g, "s", "bc", "(s b c)");
    testInterp(lg, g, "s", "abaaabc", "(s a b a a a b c)");
    testInterp(lg, g, "s", "babac", "(s b a b a 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)

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