Search in sources :

Example 26 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class Python2Target method loadTemplates.

@Override
protected STGroup loadTemplates() {
    STGroup result = super.loadTemplates();
    result.registerRenderer(String.class, new PythonStringRenderer(), true);
    return result;
}
Also used : STGroup(org.stringtemplate.v4.STGroup)

Example 27 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class CppTarget method loadTemplates.

@Override
protected STGroup loadTemplates() {
    STGroup result = super.loadTemplates();
    result.registerRenderer(Integer.class, new NumberRenderer());
    result.registerRenderer(String.class, new StringRenderer());
    result.setListener(new STErrorListener() {

        @Override
        public void compileTimeError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void runTimeError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void IOError(STMessage msg) {
            reportError(msg);
        }

        @Override
        public void internalError(STMessage msg) {
            reportError(msg);
        }

        private void reportError(STMessage msg) {
            getCodeGenerator().tool.errMgr.toolError(ErrorType.STRING_TEMPLATE_WARNING, msg.cause, msg.toString());
        }
    });
    return result;
}
Also used : STMessage(org.stringtemplate.v4.misc.STMessage) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STErrorListener(org.stringtemplate.v4.STErrorListener) NumberRenderer(org.stringtemplate.v4.NumberRenderer)

Example 28 with STGroup

use of org.stringtemplate.v4.STGroup 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)

Aggregations

STGroup (org.stringtemplate.v4.STGroup)26 ST (org.stringtemplate.v4.ST)14 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 Grammar (org.antlr.v4.tool.Grammar)6 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 STGroupFile (org.stringtemplate.v4.STGroupFile)5 StringRenderer (org.stringtemplate.v4.StringRenderer)5 NumberRenderer (org.stringtemplate.v4.NumberRenderer)3 STErrorListener (org.stringtemplate.v4.STErrorListener)3 STMessage (org.stringtemplate.v4.misc.STMessage)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 URL (java.net.URL)2