Search in sources :

Example 46 with ErrorQueue

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

the class BaseCSharpTest method rawGenerateRecognizer.

/** Return true if all is well */
protected boolean rawGenerateRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = antlrOnString(getTmpDir(), "CSharp", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".cs");
    }
    if (parserName != null) {
        files.add(parserName + ".cs");
        Set<String> optionsSet = new HashSet<String>(Arrays.asList(extraOptions));
        String grammarName = grammarFileName.substring(0, grammarFileName.lastIndexOf('.'));
        if (!optionsSet.contains("-no-listener")) {
            files.add(grammarName + "Listener.cs");
            files.add(grammarName + "BaseListener.cs");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarName + "Visitor.cs");
            files.add(grammarName + "BaseVisitor.cs");
        }
    }
    addSourceFiles(files.toArray(new String[files.size()]));
    return true;
}
Also used : ArrayList(java.util.ArrayList) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) HashSet(java.util.HashSet)

Example 47 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";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slaveS);
    writeFile(tmpdir, "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(tmpdir, "M.g4", master);
    ErrorQueue equeue = new ErrorQueue();
    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());
}
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 48 with ErrorQueue

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

the class TestCompositeGrammars method testSyntaxErrorsInImportsNotThrownOut.

@Test
public void testSyntaxErrorsInImportsNotThrownOut() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar S;\n" + "options {toke\n";
    BaseRuntimeTest.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);
    assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
}
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 49 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 {
    BaseRuntimeTest.mkdir(tmpdir);
    String master = "grammar M;\n" + "import M;\n" + "s : 'a' ;\n";
    writeFile(tmpdir, "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-lib", tmpdir);
    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 50 with ErrorQueue

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

the class BaseBrowserTest 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();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser();
        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 : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Aggregations

ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)40 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)21 Grammar (org.antlr.v4.tool.Grammar)21 LexerGrammar (org.antlr.v4.tool.LexerGrammar)17 Test (org.junit.Test)16 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15 GrammarSemanticsMessage (org.antlr.v4.tool.GrammarSemanticsMessage)14 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)11 STGroupString (org.stringtemplate.v4.STGroupString)11 ArrayList (java.util.ArrayList)9 ST (org.stringtemplate.v4.ST)8 HashSet (java.util.HashSet)7 ATNFactory (org.antlr.v4.automata.ATNFactory)7 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)7 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)7 ATN (org.antlr.v4.runtime.atn.ATN)6 DecisionState (org.antlr.v4.runtime.atn.DecisionState)6 STGroup (org.stringtemplate.v4.STGroup)6