Search in sources :

Example 76 with ErrorQueue

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

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";
    mkdir(tmpdir);
    writeFile(tmpdir, "L.g4", gstr);
    gstr = "parser grammar G1;\n" + "s: a | b;\n" + "a: T1;\n" + "b: T2;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "G1.g4", gstr);
    gstr = "parser grammar G2;\n" + "import G1;\n" + "a: T3;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "G2.g4", gstr);
    String G3str = "grammar G3;\n" + "import G2;\n" + "b: T4;\n";
    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 : Grammar(org.antlr.v4.tool.Grammar) Test(org.junit.Test)

Example 77 with ErrorQueue

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

the class TestCompositeGrammars method testDelegatesSeeSameTokenType2.

@Test
public void testDelegatesSeeSameTokenType2() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = // A, B, C token type order
    "parser grammar S;\n" + "tokens { A, B, C }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String slave2 = "parser grammar T;\n" + // reverse order
    "tokens { C, B, A }\n" + "y : A {System.out.println(\"T.y\");} ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave2);
    String master = "grammar M;\n" + "import S,T;\n" + // matches AA, which should be "aa"
    "s : x y ;\n" + // another order: B, A, C
    "B : 'b' ;\n" + "A : 'a' ;\n" + "C : 'c' ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    String expectedTokenIDToTypeMap = "{EOF=-1, B=1, A=2, C=3, WS=4}";
    String expectedStringLiteralToTypeMap = "{'a'=2, 'b'=1, 'c'=3}";
    String expectedTypeToTokenList = "[B, A, C, WS]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, sort(g.stringLiteralToTypeMap).toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    String found = execParser("M.g4", master, "MParser", "MLexer", "s", "aa", debug);
    assertEquals("S.x\n" + "T.y\n", found);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Test(org.junit.Test)

Example 78 with ErrorQueue

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

the class TestCompositeGrammars method testImportedTokenVocabIgnoredWithWarning.

@Test
public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar S;\n" + "options {tokenVocab=whatever;}\n" + "tokens { A }\n" + "x : A {System.out.println(\"S.x\");} ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    Object expectedArg = "S";
    ErrorType expectedMsgID = ErrorType.OPTIONS_IN_DELEGATE;
    GrammarSemanticsMessage expectedMessage = new GrammarSemanticsMessage(expectedMsgID, g.fileName, null, expectedArg);
    checkGrammarSemanticsWarning(equeue, expectedMessage);
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    assertEquals("unexpected warnings: " + equeue, 1, equeue.warnings.size());
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) Grammar(org.antlr.v4.tool.Grammar) GrammarSemanticsMessage(org.antlr.v4.tool.GrammarSemanticsMessage) Test(org.junit.Test)

Example 79 with ErrorQueue

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

the class TestCompositeGrammars method testImportSelfLoop.

// Test for https://github.com/antlr/antlr4/issues/1317
@Test
public void testImportSelfLoop() throws Exception {
    RuntimeTestUtils.mkdir(getTempDirPath());
    String master = "grammar M;\n" + "import M;\n" + "s : 'a' ;\n";
    writeFile(getTempDirPath(), "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.g4", false, "-lib", getTempDirPath());
    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 80 with ErrorQueue

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

the class TestCompositeGrammars method testDelegatesSeeSameTokenType.

@Test
public void testDelegatesSeeSameTokenType() throws Exception {
    String slaveS = "parser grammar S;\n" + "tokens { A, B, C }\n" + "x : A ;\n";
    String slaveT = "parser grammar T;\n" + "tokens { C, B, A } // reverse order\n" + "y : A ;\n";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "S.g4", slaveS);
    writeFile(getTempDirPath(), "T.g4", slaveT);
    String master = "// The lexer will create rules to match letters a, b, c.\n" + "// The associated token types A, B, C must have the same value\n" + "// and all import'd parsers.  Since ANTLR regenerates all imports\n" + "// for use with the delegator M, it can generate the same token type\n" + "// mapping in each parser:\n" + "// public static final int C=6;\n" + "// public static final int EOF=-1;\n" + "// public static final int B=5;\n" + "// public static final int WS=7;\n" + "// public static final int A=4;\n" + "grammar M;\n" + "import S,T;\n" + "s : x y ; // matches AA, which should be 'aa'\n" + "B : 'b' ; // another order: B, A, C\n" + "A : 'a' ;\n" + "C : 'c' ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(getTempDirPath(), "M.g4", master);
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(getTempDirPath() + "/M.g4", master, equeue);
    String expectedTokenIDToTypeMap = "{EOF=-1, B=1, A=2, C=3, WS=4}";
    String expectedStringLiteralToTypeMap = "{'a'=2, 'b'=1, 'c'=3}";
    String expectedTypeToTokenList = "[B, A, C, WS]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, sort(g.stringLiteralToTypeMap).toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
}
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)

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