Search in sources :

Example 1 with BaseRuntimeTest.antlrOnString

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

the class TestCompositeGrammars method testHeadersPropogatedCorrectlyToImportedGrammars.

@Test
public void testHeadersPropogatedCorrectlyToImportedGrammars() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.print(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "@header{package mypackage;}\n" + "s : a ;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", master, false);
    // should be ok
    int expecting = 0;
    assertEquals(expecting, equeue.errors.size());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 2 with BaseRuntimeTest.antlrOnString

use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString 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";
    BaseRuntimeTest.mkdir(tmpdir);
    String subdir = tmpdir + "/sub";
    BaseRuntimeTest.mkdir(subdir);
    writeFile(subdir, "S.g4", slave);
    String parser = "parser grammar MParser;\n" + "import S;\n" + "options {tokenVocab=MLexer;}\n" + "s : a ;\n";
    writeFile(tmpdir, "MParser.g4", parser);
    String lexer = "lexer grammar MLexer;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "MLexer.g4", lexer);
    String outdir = tmpdir + "/out";
    BaseRuntimeTest.mkdir(outdir);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "MLexer.g4", false, "-o", outdir);
    assertEquals(0, equeue.size());
    equeue = BaseRuntimeTest.antlrOnString(tmpdir, "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 3 with BaseRuntimeTest.antlrOnString

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

the class TestCompositeGrammars method testImportFileNotSearchedForInOutputDir.

@Test
public void testImportFileNotSearchedForInOutputDir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    String outdir = tmpdir + "/out";
    BaseRuntimeTest.mkdir(outdir);
    writeFile(outdir, "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);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir);
    assertEquals(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, equeue.errors.get(0).getErrorType());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 4 with BaseRuntimeTest.antlrOnString

use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString 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";
    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);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "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 5 with BaseRuntimeTest.antlrOnString

use of org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString 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)

Aggregations

ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)10 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)8 Test (org.junit.Test)8 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)1 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)1 STGroupString (org.stringtemplate.v4.STGroupString)1