Search in sources :

Example 46 with Grammar

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

the class TestCompositeGrammars method testNestedComposite.

@Test
public void testNestedComposite() throws Exception {
    // Wasn't compiling. http://www.antlr.org/jira/browse/ANTLR-438
    ErrorQueue equeue = new ErrorQueue();
    String gstr = "lexer grammar L;\n" + "T1: '1';\n" + "T2: '2';\n" + "T3: '3';\n" + "T4: '4';\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "L.g4", gstr);
    gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "G1.g4", gstr);
    gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "G2.g4", gstr);
    String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "G3.g4", G3str);
    Grammar g = new Grammar(tmpdir + "/G3.g4", G3str, equeue);
    String expectedTokenIDToTypeMap = "{EOF=-1, T4=1, T3=2}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[T4, T3]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    boolean ok = rawGenerateAndBuildRecognizer("G3.g4", G3str, "G3Parser", null);
    // should be ok
    boolean expecting = true;
    assertEquals(expecting, ok);
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 47 with Grammar

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

the class TestCompositeGrammars method testErrorInImportedGetsRightFilename.

@Test
public void testErrorInImportedGetsRightFilename() throws Exception {
    String slave = "parser grammar S;\n" + "a : 'a' | c;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n";
    writeFile(tmpdir, "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir);
    ANTLRMessage msg = equeue.errors.get(0);
    assertEquals(ErrorType.UNDEFINED_RULE_REF, msg.getErrorType());
    assertEquals("c", msg.getArgs()[0]);
    assertEquals(2, msg.line);
    assertEquals(10, msg.charPosition);
    assertEquals("S.g4", new File(msg.fileName).getName());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) File(java.io.File) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 48 with Grammar

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

the class TestCompositeGrammars method testOutputDirShouldNotEffectImports.

@Test
public void testOutputDirShouldNotEffectImports() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    String subdir = tmpdir + "/sub";
    BaseRuntimeTest.mkdir(subdir);
    writeFile(subdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    String outdir = tmpdir + "/out";
    BaseRuntimeTest.mkdir(outdir);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir, "-lib", subdir);
    assertEquals(0, equeue.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 49 with Grammar

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

the class TestGrammarParserInterpreter method testOneAlt.

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

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

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