Search in sources :

Example 1 with ErrorBuffer

use of org.stringtemplate.v4.misc.ErrorBuffer in project bender by Nextdoor.

the class BenderConfig method swapEnvironmentVariables.

/**
 * Parses an input String and replaces instances of {@literal <XXX>}" with the value of the XXX OS
 * Environment Variable. This is used as a pre-parser for the Config files, allowing environment
 * variables to be swapped at run-time.
 *
 * @param raw A raw string (not necessarily valid configuration data)
 * @return A parsed string with OS variables swapped in
 * @throws ConfigurationException If any discovered {@literal <WRAPPED_VALUES>} are not found in
 *         System.getenv().
 */
public static String swapEnvironmentVariables(String raw) throws ConfigurationException {
    ErrorBuffer errors = new ErrorBuffer();
    ST template = new ST(raw);
    STGroup g = template.groupThatCreatedThisInstance;
    g.setListener(errors);
    Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
        template.add(envName, env.get(envName));
    }
    String parsed = template.render();
    if (errors.errors.size() > 0) {
        throw new ConfigurationException(errors.toString());
    }
    return parsed;
}
Also used : ST(org.stringtemplate.v4.ST) ErrorBuffer(org.stringtemplate.v4.misc.ErrorBuffer) STGroup(org.stringtemplate.v4.STGroup)

Example 2 with ErrorBuffer

use of org.stringtemplate.v4.misc.ErrorBuffer 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 ErrorBuffer

use of org.stringtemplate.v4.misc.ErrorBuffer in project bndtools by bndtools.

the class StringTemplateEngine method compile.

private ST compile(STGroup group, String name, Resource resource) throws Exception {
    ErrorBuffer errors = new ErrorBuffer();
    group.setListener(errors);
    ST st;
    try {
        loadRawTemplate(group, name, resource);
        st = group.getInstanceOf(name);
    } catch (Exception e) {
        // Wrap the ST exception, which gives us no detail in its message
        throw new IllegalArgumentException(String.format("Failed to compile template '%s': %s", name, errors.toString()));
    }
    if (st == null)
        throw new Exception("Template name not loaded: " + name);
    return st;
}
Also used : CompiledST(org.stringtemplate.v4.compiler.CompiledST) ST(org.stringtemplate.v4.ST) ErrorBuffer(org.stringtemplate.v4.misc.ErrorBuffer) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with ErrorBuffer

use of org.stringtemplate.v4.misc.ErrorBuffer in project antlr4 by antlr.

the class TestAttributeChecks method testActions.

public void testActions(String location, String[] pairs, String template) {
    for (int i = 0; i < pairs.length; i += 2) {
        String action = pairs[i];
        String expected = pairs[i + 1];
        STGroup g = new STGroup('<', '>');
        // hush warnings
        g.setListener(new ErrorBuffer());
        ST st = new ST(g, template);
        st.add(location, action);
        String grammar = st.render();
        testErrors(new String[] { grammar, expected }, false);
    }
}
Also used : ST(org.stringtemplate.v4.ST) ErrorBuffer(org.stringtemplate.v4.misc.ErrorBuffer) STGroup(org.stringtemplate.v4.STGroup)

Aggregations

ST (org.stringtemplate.v4.ST)4 ErrorBuffer (org.stringtemplate.v4.misc.ErrorBuffer)4 STGroup (org.stringtemplate.v4.STGroup)3 IOException (java.io.IOException)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 STGroupFile (org.stringtemplate.v4.STGroupFile)1 CompiledST (org.stringtemplate.v4.compiler.CompiledST)1 STViz (org.stringtemplate.v4.gui.STViz)1