Search in sources :

Example 31 with ErrorQueue

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

the class BaseDartTest method rawGenerateAndBuildRecognizer.

/**
 * Return true if all is well
 */
protected boolean rawGenerateAndBuildRecognizer(String grammarFileName, String grammarStr, String parserName, String lexerName, boolean defaultListener, String... extraOptions) {
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Dart", grammarFileName, grammarStr, defaultListener, extraOptions);
    if (!equeue.errors.isEmpty()) {
        return false;
    }
    List<String> files = new ArrayList<String>();
    if (lexerName != null) {
        files.add(lexerName + ".dart");
    }
    if (parserName != null) {
        files.add(parserName + ".dart");
        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.dart");
            files.add(grammarName + "BaseListener.dart");
        }
        if (optionsSet.contains("-visitor")) {
            files.add(grammarName + "Visitor.dart");
            files.add(grammarName + "BaseVisitor.dart");
        }
    }
    String runtime = locateRuntime();
    writeFile(getTempDirPath(), "pubspec.yaml", "name: \"test\"\n" + "dependencies:\n" + "  antlr4:\n" + "    path: " + runtime + "\n" + "environment:\n" + "  sdk: \">=2.12.0 <3.0.0\"\n");
    final File dartToolDir = new File(getTempDirPath(), ".dart_tool");
    if (cacheDartPackages == null) {
        try {
            final Process process = Runtime.getRuntime().exec(new String[] { locateDart(), "pub", "get" }, null, getTempTestDir());
            StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
            stderrVacuum.start();
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    try {
                        process.destroy();
                    } catch (Exception e) {
                        e.printStackTrace(System.err);
                    }
                }
            }, 30_000);
            process.waitFor();
            timer.cancel();
            stderrVacuum.join();
            String stderrDuringPubGet = stderrVacuum.toString();
            if (!stderrDuringPubGet.isEmpty()) {
                System.out.println("Pub Get error: " + stderrVacuum.toString());
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return false;
        }
        cacheDartPackages = readFile(getTempDirPath(), ".packages");
        cacheDartPackageConfig = readFile(dartToolDir.getAbsolutePath(), "package_config.json");
    } else {
        writeFile(getTempDirPath(), ".packages", cacheDartPackages);
        // noinspection ResultOfMethodCallIgnored
        dartToolDir.mkdir();
        writeFile(dartToolDir.getAbsolutePath(), "package_config.json", cacheDartPackageConfig);
    }
    // allIsWell: no compile
    return true;
}
Also used : BaseRuntimeTest.readFile(org.antlr.v4.test.runtime.BaseRuntimeTest.readFile) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Example 32 with ErrorQueue

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

the class TestCodeGeneration method getEvalInfoForString.

public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammarString);
    List<String> evals = new ArrayList<String>();
    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();
        // STViz viz = outputFileST.inspect();
        // try {
        // viz.waitForClose();
        // }
        // catch (Exception e) {
        // e.printStackTrace();
        // }
        boolean debug = false;
        DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
        InstanceScope scope = new InstanceScope(null, outputFileST);
        StringWriter sw = new StringWriter();
        AutoIndentWriter out = new AutoIndentWriter(sw);
        interp.exec(out, scope);
        for (String e : interp.evals) {
            if (e.contains(pattern)) {
                evals.add(e);
            }
        }
    }
    if (equeue.size() > 0) {
        System.err.println(equeue.toString());
    }
    return evals;
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) StringWriter(java.io.StringWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) InstanceScope(org.stringtemplate.v4.InstanceScope) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue)

Example 33 with ErrorQueue

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

the class TestCompositeGrammars method testOutputDirShouldNotEffectImports.

@Test
public void testOutputDirShouldNotEffectImports() 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 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);
    String outdir = getTempDirPath() + "/out";
    RuntimeTestUtils.mkdir(outdir);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(getTempDirPath(), "Java", "M.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 34 with ErrorQueue

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

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";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : x ;\n" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(getTempDirPath(), "M.g4", master);
    Grammar g = new Grammar(getTempDirPath() + "/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) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) GrammarSemanticsMessage(org.antlr.v4.tool.GrammarSemanticsMessage) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 35 with ErrorQueue

use of org.antlr.v4.test.runtime.ErrorQueue 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";
    RuntimeTestUtils.mkdir(getTempDirPath());
    writeFile(getTempDirPath(), "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(getTempDirPath(), "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)

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