Search in sources :

Example 41 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class TestCompositeGrammars method testImportFileLocationInSubdir.

@Test
public void testImportFileLocationInSubdir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    String subdir = getTempDirPath() + PATH_SEP + "sub";
    RuntimeTestUtils.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(getTempDirPath(), "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-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 42 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class TestCompositeGrammars method testImportChannelsIntoLexerGrammar.

@Test
public void testImportChannelsIntoLexerGrammar() throws Exception {
    RuntimeTestUtils.mkdir(getTempDirPath());
    String master = "lexer grammar M;\n" + "import S;\n" + "channels {CH_A, CH_B}\n" + "A : 'a' -> channel(CH_A);\n" + "B : 'b' -> channel(CH_B);\n";
    writeFile(getTempDirPath(), "M.g4", master);
    String slave = "lexer grammar S;\n" + "C : 'c';\n";
    writeFile(getTempDirPath(), "S.g4", slave);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
    assertEquals(0, equeue.errors.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 43 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue in project antlr4 by antlr.

the class TestCompositeGrammars method testTokensFileInOutputDirAndImportFileInSubdir.

@Test
public void testTokensFileInOutputDirAndImportFileInSubdir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    String subdir = getTempDirPath() + "/sub";
    RuntimeTestUtils.mkdir(subdir);
    writeFile(subdir, "S.g4", slave);
    String parser = "parser grammar MParser;\n" + "import S;\n" + "options {tokenVocab=MLexer;}\n" + "s : a ;\n";
    writeFile(getTempDirPath(), "MParser.g4", parser);
    String lexer = "lexer grammar MLexer;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(getTempDirPath(), "MLexer.g4", lexer);
    String outdir = getTempDirPath() + "/out";
    RuntimeTestUtils.mkdir(outdir);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "MLexer.g4", false, "-o", outdir);
    assertEquals(0, equeue.size());
    equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "MParser.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 44 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue 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";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "L.g4", gstr);
    gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "G1.g4", gstr);
    gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "G2.g4", gstr);
    String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "G3.g4", G3str);
    Grammar g = new Grammar(getTempDirPath() + "/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 45 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue 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";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n";
    writeFile(getTempDirPath(), "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
    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) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Aggregations

ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)45 Grammar (org.antlr.v4.tool.Grammar)34 Test (org.junit.Test)34 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)27 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)23 LexerGrammar (org.antlr.v4.tool.LexerGrammar)23 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)17 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)12 STGroupString (org.stringtemplate.v4.STGroupString)12 ArrayList (java.util.ArrayList)10 ST (org.stringtemplate.v4.ST)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 ATN (org.antlr.v4.runtime.atn.ATN)8 DecisionState (org.antlr.v4.runtime.atn.DecisionState)8 STGroup (org.stringtemplate.v4.STGroup)7 HashSet (java.util.HashSet)6