Search in sources :

Example 91 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by tunnelvisionlabs.

the class DefaultToolListener method error.

@Override
public void error(ANTLRMessage msg) {
    ST msgST = tool.errMgr.getMessageTemplate(msg);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    System.err.println(outputMsg);
}
Also used : ST(org.stringtemplate.v4.ST)

Example 92 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by tunnelvisionlabs.

the class TestRuleVersioning method testPropertyEvaluation.

@Test
public void testPropertyEvaluation() throws Exception {
    Properties properties = getProperties();
    assertNotNull(properties);
    STGroup group = createGroup(properties);
    assertNotNull(group);
    Set<String> templateNames = group.getTemplateNames();
    String templateName0 = "/PropertyEval0-sample-template";
    assertTrue(templateNames.contains(templateName0));
    ST st = group.getInstanceOf(templateName0);
    assertNotNull(st);
    String result = st.render();
    assertEquals("foo", result);
    String templateName1 = "/PropertyEval1-sample-template";
    assertTrue(templateNames.contains(templateName1));
    ST st1 = group.getInstanceOf(templateName1);
    assertNotNull(st1);
    String result1 = st1.render();
    assertEquals("foo", result1);
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval0", "sample"));
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval1", "sample"));
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval2", "sample"));
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval3", "sample"));
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval4", "sample"));
    assertEquals("foo", resolveProperty(properties, group, "PropertyEval5", "sample"));
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) Properties(java.util.Properties) Test(org.junit.Test)

Example 93 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by tunnelvisionlabs.

the class Antlr4ErrorLog method warning.

/**
 * {@inheritDoc}
 * <p>
 * This implementation passes the message to the Maven log.
 * </p>
 * @param message
 */
@Override
public void warning(ANTLRMessage message) {
    ST msgST = tool.errMgr.getMessageTemplate(message);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    log.warn(outputMsg);
    if (message.fileName != null) {
        String text = message.getMessageTemplate(false).render();
        buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_WARNING, message.getCause());
    }
}
Also used : ST(org.stringtemplate.v4.ST) File(java.io.File)

Example 94 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by tunnelvisionlabs.

the class Antlr4ErrorLog method error.

/**
 * {@inheritDoc}
 * <p>
 * This implementation passes the message to the Maven log.
 * </p>
 * @param message The message to send to Maven.
 */
@Override
public void error(ANTLRMessage message) {
    ST msgST = tool.errMgr.getMessageTemplate(message);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    log.error(outputMsg);
    if (message.fileName != null) {
        String text = message.getMessageTemplate(false).render();
        buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_ERROR, message.getCause());
    }
}
Also used : ST(org.stringtemplate.v4.ST) File(java.io.File)

Example 95 with ST

use of edu.princeton.cs.algs4.ST 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)

Aggregations

ST (org.stringtemplate.v4.ST)197 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)37 STGroup (org.stringtemplate.v4.STGroup)24 File (java.io.File)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)12 STGroupFile (org.stringtemplate.v4.STGroupFile)12 Path (java.nio.file.Path)10 Test (org.junit.Test)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 Grammar (org.antlr.v4.tool.Grammar)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)9 STGroupString (org.stringtemplate.v4.STGroupString)9 LinkedHashMap (java.util.LinkedHashMap)7 URL (java.net.URL)6 Map (java.util.Map)6