Search in sources :

Example 21 with STGroupFile

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

the class BaseRuntimeTest method testLexer.

public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
    RuntimeTestUtils.mkdir(delegate.getTempParserDirPath());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTempParserDirPath(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.registerRenderer(String.class, new StringRenderer());
    g.importTemplates(targetTemplates);
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execLexer(grammarName + ".g4", grammar, grammarName, descriptor.getInput(), descriptor.showDFA());
    assertCorrectOutput(descriptor, delegate, found);
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 22 with STGroupFile

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

the class BaseRuntimeTest method testParser.

public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
    RuntimeTestUtils.mkdir(delegate.getTempParserDirPath());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTempParserDirPath(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.importTemplates(targetTemplates);
    g.registerRenderer(String.class, new StringRenderer());
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execParser(grammarName + ".g4", grammar, grammarName + "Parser", grammarName + "Lexer", grammarName + "Listener", grammarName + "Visitor", descriptor.getStartRule(), descriptor.getInput(), descriptor.showDiagnosticErrors());
    assertCorrectOutput(descriptor, delegate, found);
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 23 with STGroupFile

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

the class ErrorManager method setFormat.

/**
 * The format gets reset either from the Tool if the user supplied a command line option to that effect
 *  Otherwise we just use the default "antlr".
 */
public void setFormat(String formatName) {
    this.formatName = formatName;
    String fileName = FORMATS_DIR + formatName + STGroup.GROUP_FILE_EXTENSION;
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL url = cl.getResource(fileName);
    if (url == null) {
        cl = ErrorManager.class.getClassLoader();
        url = cl.getResource(fileName);
    }
    if (url == null && formatName.equals("antlr")) {
        rawError("ANTLR installation corrupted; cannot find ANTLR messages format file " + fileName);
        panic();
    } else if (url == null) {
        rawError("no such message format file " + fileName + " retrying with default ANTLR format");
        // recurse on this rule, trying the default message format
        setFormat("antlr");
        return;
    }
    format = new STGroupFile(url, "UTF-8", '<', '>');
    format.load();
    if (!initSTListener.errors.isEmpty()) {
        rawError("ANTLR installation corrupted; can't load messages format file:\n" + initSTListener.toString());
        panic();
    }
    boolean formatOK = verifyFormat();
    if (!formatOK && formatName.equals("antlr")) {
        rawError("ANTLR installation corrupted; ANTLR messages format file " + formatName + ".stg incomplete");
        panic();
    } else if (!formatOK) {
        // recurse on this rule, trying the default message format
        setFormat("antlr");
    }
}
Also used : STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL)

Example 24 with STGroupFile

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

the class BuildDependencyGenerator method loadDependencyTemplates.

public void loadDependencyTemplates() {
    if (templates != null)
        return;
    String fileName = "org/antlr/v4/tool/templates/depend.stg";
    templates = new STGroupFile(fileName, "UTF-8");
}
Also used : STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 25 with STGroupFile

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

the class Target method loadTemplates.

protected STGroup loadTemplates() {
    String groupFileName = CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION;
    STGroup result = null;
    try {
        result = new STGroupFile(groupFileName);
    } catch (IllegalArgumentException iae) {
        gen.tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, iae, language);
    }
    if (result == null)
        return null;
    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) STGroupFile(org.stringtemplate.v4.STGroupFile) STErrorListener(org.stringtemplate.v4.STErrorListener) NumberRenderer(org.stringtemplate.v4.NumberRenderer)

Aggregations

STGroupFile (org.stringtemplate.v4.STGroupFile)26 STGroup (org.stringtemplate.v4.STGroup)12 StringRenderer (org.stringtemplate.v4.StringRenderer)7 URL (java.net.URL)6 Before (org.junit.Before)6 ST (org.stringtemplate.v4.ST)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 NumberRenderer (org.stringtemplate.v4.NumberRenderer)3 STErrorListener (org.stringtemplate.v4.STErrorListener)3 STMessage (org.stringtemplate.v4.misc.STMessage)3 IOException (java.io.IOException)2 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)2 Pair (org.antlr.v4.runtime.misc.Pair)2 BodyExtractorFilter (se.inera.intyg.webcert.web.integration.integrationtest.BodyExtractorFilter)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 MultiMap (org.antlr.v4.runtime.misc.MultiMap)1 NotNull (org.antlr.v4.runtime.misc.NotNull)1 Primitive (org.eclipse.collections.codegenerator.model.Primitive)1