Search in sources :

Example 26 with ErrorQueue

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

the class TestCompositeGrammars method testCombinedImportsCombined.

@Test
public void testCombinedImportsCombined() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = // A, B, C token type order
    "grammar S;\n" + "tokens { A, B, C }\n" + "x : 'x' INT {System.out.println(\"S.x\");} ;\n" + "INT : '0'..'9'+ ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x INT ;\n";
    writeFile(tmpdir, "M.g4", master);
    @SuppressWarnings("unused") Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    String found = execParser("M.g4", master, "MParser", "MLexer", "s", "x 34 9", debug);
    assertEquals("S.x\n", found);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Test(org.junit.Test)

Example 27 with ErrorQueue

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

the class TestCompositeGrammars method test3LevelImport.

// Make sure that M can import S that imports T.
@Test
public void test3LevelImport() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar T;\n" + "a : T ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave);
    String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave2);
    String master = "grammar M;\n" + "import S;\n" + "a : M ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    // S and T aren't imported; overridden
    String expectedTokenIDToTypeMap = "{EOF=-1, M=1}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[M]";
    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("M.g4", master, "MParser", null);
    // should be ok
    boolean expecting = true;
    assertEquals(expecting, ok);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Test(org.junit.Test)

Example 28 with ErrorQueue

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

the class TestCompositeGrammars method testErrorInImportedGetsRightFilename.

@Test
public void testErrorInImportedGetsRightFilename() throws Exception {
    String slave = "parser grammar S;\n" + "a : 'a' | c;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n";
    writeFile(tmpdir, "M.g4", master);
    ErrorQueue equeue = antlr("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 : ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) File(java.io.File) Test(org.junit.Test)

Example 29 with ErrorQueue

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

the class TestCompositeGrammars method testRulesVisibleThroughMultilevelImport.

@Test
public void testRulesVisibleThroughMultilevelImport() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar T;\n" + "x : T ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave);
    String slave2 = // A, B, C token type order
    "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
    mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave2);
    String master = "grammar M;\n" + "import S;\n" + // x MUST BE VISIBLE TO M
    "a : M x ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    String expectedTokenIDToTypeMap = "{EOF=-1, M=1, T=2}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[M, T]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Test(org.junit.Test)

Example 30 with ErrorQueue

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

the class BaseJavaTest method testActions.

public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
    int lp = templates.indexOf('(');
    String name = templates.substring(0, lp);
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf(name);
    st.add(actionName, action);
    String grammar = st.render();
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammar, equeue);
    if (g.ast != null && !g.ast.hasErrors) {
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        ATNFactory factory = new ParserATNFactory(g);
        if (g.isLexer())
            factory = new LexerATNFactory((LexerGrammar) g);
        g.atn = factory.createATN();
        AnalysisPipeline anal = new AnalysisPipeline(g);
        anal.process();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser(false);
        String output = outputFileST.render();
        // System.out.println(output);
        String b = "#" + actionName + "#";
        int start = output.indexOf(b);
        String e = "#end-" + actionName + "#";
        int end = output.indexOf(e);
        String snippet = output.substring(start + b.length(), end);
        assertEquals(expected, snippet);
    }
    if (equeue.size() > 0) {
    // System.err.println(equeue.toString());
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) STGroup(org.stringtemplate.v4.STGroup) STGroupString(org.stringtemplate.v4.STGroupString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

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