Search in sources :

Example 6 with STGroupFile

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

the class LeftRecursiveRuleAnalyzer method loadPrecRuleTemplates.

public void loadPrecRuleTemplates() {
    String templateGroupFile = "org/antlr/v4/tool/templates/LeftRecursiveRules.stg";
    recRuleTemplates = new STGroupFile(templateGroupFile);
    if (!recRuleTemplates.isDefined("recRule")) {
        tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
    }
    // use codegen to get correct language templates; that's it though
    CodeGenerator gen = new CodeGenerator(tool, null, language);
    STGroup templates = gen.getTemplates();
    if (templates == null) {
        // this class will still operate using Java templates
        templates = new CodeGenerator(tool, null, "Java").getTemplates();
        assert templates != null;
    }
    codegenTemplates = templates;
}
Also used : STGroup(org.stringtemplate.v4.STGroup) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 7 with STGroupFile

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

the class Target method loadTemplates.

@NotNull
protected STGroup loadTemplates() {
    STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT + "/" + getLanguage() + "/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION);
    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) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 8 with STGroupFile

use of org.stringtemplate.v4.STGroupFile in project killbill by killbill.

the class TestStringTemplateInheritance method testCheckQueries.

@Test(groups = "fast")
public void testCheckQueries() throws Exception {
    final STGroup kombucha = new STGroupFile(this.getClass().getResource("/org/killbill/billing/util/dao/Kombucha.sql.stg"), "UTF-8", '<', '>');
    // Verify non inherited template
    Assert.assertEquals(kombucha.getInstanceOf("isItTimeForKombucha").render(), "select count(*) = 0 as is_time from kombucha;");
    // Verify inherited templates
    assertPattern(kombucha.getInstanceOf("getById").render(), "select\r?\n" + "  t.record_id\r?\n" + ", t.id\r?\n" + ", t.tea\r?\n" + ", t.mushroom\r?\n" + ", t.sugar\r?\n" + ", t.account_record_id\r?\n" + ", t.tenant_record_id\r?\n" + "from kombucha t\r?\n" + "where t.id = :id\r?\n" + "and t.tenant_record_id = :tenantRecordId\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("getByRecordId").render(), "select\r?\n" + "  t.record_id\r?\n" + ", t.id\r?\n" + ", t.tea\r?\n" + ", t.mushroom\r?\n" + ", t.sugar\r?\n" + ", t.account_record_id\r?\n" + ", t.tenant_record_id\r?\n" + "from kombucha t\r?\n" + "where t.record_id = :recordId\r?\n" + "and t.tenant_record_id = :tenantRecordId\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("getRecordId").render(), "select\r?\n" + "  t.record_id\r?\n" + "from kombucha t\r?\n" + "where t.id = :id\r?\n" + "and t.tenant_record_id = :tenantRecordId\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("getHistoryRecordId").render(), "select\r?\n" + "  max\\(t.record_id\\)\r?\n" + "from kombucha_history t\r?\n" + "where t.target_record_id = :targetRecordId\r?\n" + "and t.tenant_record_id = :tenantRecordId\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("getAll").render(), "select\r?\n" + "  t.record_id\r?\n" + ", t.id\r?\n" + ", t.tea\r?\n" + ", t.mushroom\r?\n" + ", t.sugar\r?\n" + ", t.account_record_id\r?\n" + ", t.tenant_record_id\r?\n" + "from kombucha t\r?\n" + "where t.tenant_record_id = :tenantRecordId\r?\n" + "order by t.record_id ASC\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("get").add("orderBy", "record_id").add("offset", "3").add("rowCount", "12").add("ordering", "ASC").render(), "select\r?\n" + "  t.record_id\r?\n" + ", t.id\r?\n" + ", t.tea\r?\n" + ", t.mushroom\r?\n" + ", t.sugar\r?\n" + ", t.account_record_id\r?\n" + ", t.tenant_record_id\r?\n" + "from kombucha t\r?\n" + "join \\(\r?\n" + "  select record_id\r?\n" + "  from kombucha\r?\n" + "  where tenant_record_id = :tenantRecordId\r?\n" + "  order by record_id ASC\r?\n" + "  limit :rowCount offset :offset\r?\n" + "\\) optimization on optimization.record_id = t.record_id\r?\n" + "order by t.record_id ASC\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("test").render(), "select\r?\n" + "  t.record_id\r?\n" + ", t.id\r?\n" + ", t.tea\r?\n" + ", t.mushroom\r?\n" + ", t.sugar\r?\n" + ", t.account_record_id\r?\n" + ", t.tenant_record_id\r?\n" + "from kombucha t\r?\n" + "where t.tenant_record_id = :tenantRecordId\r?\n" + "limit 1\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("addHistoriesFromTransaction").render(), "insert into kombucha_history \\(\r?\n" + "  id\r?\n" + ", target_record_id\r?\n" + ", change_type\r?\n" + ", tea\r?\n" + ", mushroom\r?\n" + ", sugar\r?\n" + ", account_record_id\r?\n" + ", tenant_record_id\r?\n" + "\\)\r?\n" + "values \\(\r?\n" + "  :id\r?\n" + ", :targetRecordId\r?\n" + ", :changeType\r?\n" + ", :tea\r?\n" + ", :mushroom\r?\n" + ", :sugar\r?\n" + ", :accountRecordId\r?\n" + ", :tenantRecordId\r?\n" + "\\)\r?\n" + ";");
    assertPattern(kombucha.getInstanceOf("insertAuditsFromTransaction").render(), "insert into audit_log \\(\r?\n" + "id\r?\n" + ", table_name\r?\n" + ", target_record_id\r?\n" + ", change_type\r?\n" + ", created_by\r?\n" + ", reason_code\r?\n" + ", comments\r?\n" + ", user_token\r?\n" + ", created_date\r?\n" + ", account_record_id\r?\n" + ", tenant_record_id\r?\n" + "\\)\r?\n" + "values \\(\r?\n" + "  :id\r?\n" + ", :tableName\r?\n" + ", :targetRecordId\r?\n" + ", :changeType\r?\n" + ", :createdBy\r?\n" + ", :reasonCode\r?\n" + ", :comments\r?\n" + ", :userToken\r?\n" + ", :createdDate\r?\n" + ", :accountRecordId\r?\n" + ", :tenantRecordId\r?\n" + "\\)\r?\n" + ";");
}
Also used : STGroup(org.stringtemplate.v4.STGroup) STGroupFile(org.stringtemplate.v4.STGroupFile) Test(org.testng.annotations.Test)

Example 9 with STGroupFile

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

the class LeftRecursiveRuleAnalyzer method loadPrecRuleTemplates.

public void loadPrecRuleTemplates() {
    String templateGroupFile = "org/antlr/v4/tool/templates/LeftRecursiveRules.stg";
    recRuleTemplates = new STGroupFile(templateGroupFile);
    if (!recRuleTemplates.isDefined("recRule")) {
        tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
    }
    // use codegen to get correct language templates; that's it though
    CodeGenerator gen = new CodeGenerator(tool, null, language);
    codegenTemplates = gen.getTemplates();
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) STGroupFile(org.stringtemplate.v4.STGroupFile)

Example 10 with STGroupFile

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

the class CSharpTarget method loadTemplates.

@Override
protected STGroup loadTemplates() {
    // override the superclass behavior to put all C# templates in the same folder
    STGroup result = new STGroupFile(CodeGenerator.TEMPLATE_ROOT + "/CSharp/" + getLanguage() + STGroup.GROUP_FILE_EXTENSION);
    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