Search in sources :

Example 1 with STViz

use of org.stringtemplate.v4.gui.STViz 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 2 with STViz

use of org.stringtemplate.v4.gui.STViz in project antlr4 by tunnelvisionlabs.

the class TestGenerator method generateTestFile.

protected void generateTestFile(STGroup index, STGroup targetGroup, String testdir, Collection<String> testTemplates) {
    ErrorBuffer errors = new ErrorBuffer();
    targetGroup.setListener(errors);
    File targetFolder = getOutputDir(testdir);
    String testName = testdir.substring(testdir.lastIndexOf('/') + 1);
    File targetFile = new File(targetFolder, "Test" + testName + ".java");
    // System.out.println("Generating file "+targetFile.getAbsolutePath());
    List<ST> templates = new ArrayList<ST>();
    for (String template : testTemplates) {
        STGroup testGroup = new STGroupFile(testdir + "/" + template + STGroup.GROUP_FILE_EXTENSION);
        importLanguageTemplates(testGroup, targetGroup);
        ST testType = testGroup.getInstanceOf("TestType");
        if (testType == null) {
            warn(String.format("Unable to generate tests for %s: no TestType specified.", template));
            continue;
        }
        ST testMethodTemplate = targetGroup.getInstanceOf(testType.render() + "TestMethod");
        if (testMethodTemplate == null) {
            warn(String.format("Unable to generate tests for %s: TestType '%s' is not supported by the current runtime.", template, testType.render()));
            continue;
        }
        testMethodTemplate.add(testMethodTemplate.impl.formalArguments.keySet().iterator().next(), testGroup);
        templates.add(testMethodTemplate);
    }
    ST testFileTemplate = targetGroup.getInstanceOf("TestFile");
    testFileTemplate.addAggr("file.{Options,name,tests}", index.rawGetDictionary("Options"), testName, templates);
    if (visualize) {
        STViz viz = testFileTemplate.inspect();
        try {
            viz.waitForClose();
        } catch (InterruptedException ex) {
        }
    }
    try {
        String output = testFileTemplate.render();
        if (errors.errors.size() > 0) {
            System.err.println("errors in " + targetGroup.getName() + ": " + errors);
        }
        writeFile(targetFile, output);
    } catch (IOException ex) {
        error(String.format("Failed to write output file: %s", targetFile), ex);
    }
}
Also used : ST(org.stringtemplate.v4.ST) ErrorBuffer(org.stringtemplate.v4.misc.ErrorBuffer) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList) IOException(java.io.IOException) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File) STGroupFile(org.stringtemplate.v4.STGroupFile) STViz(org.stringtemplate.v4.gui.STViz)

Example 3 with STViz

use of org.stringtemplate.v4.gui.STViz in project antlr4 by tunnelvisionlabs.

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)

Aggregations

ArrayList (java.util.ArrayList)3 ST (org.stringtemplate.v4.ST)3 StringWriter (java.io.StringWriter)2 ATNFactory (org.antlr.v4.automata.ATNFactory)2 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)2 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)2 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)2 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)2 Grammar (org.antlr.v4.tool.Grammar)2 LexerGrammar (org.antlr.v4.tool.LexerGrammar)2 AutoIndentWriter (org.stringtemplate.v4.AutoIndentWriter)2 InstanceScope (org.stringtemplate.v4.InstanceScope)2 File (java.io.File)1 IOException (java.io.IOException)1 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)1 STGroup (org.stringtemplate.v4.STGroup)1 STGroupFile (org.stringtemplate.v4.STGroupFile)1 STViz (org.stringtemplate.v4.gui.STViz)1 ErrorBuffer (org.stringtemplate.v4.misc.ErrorBuffer)1