use of org.stringtemplate.v4.StringRenderer 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;
}
use of org.stringtemplate.v4.StringRenderer in project antlr4 by antlr.
the class BaseRuntimeTest method testParser.
public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
mkdir(delegate.getTmpDir());
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.getTmpDir(), 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());
if (delegate instanceof SpecialRuntimeTestAssert) {
((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
} else {
assertEquals(descriptor.getErrors(), delegate.getParseErrors());
assertEquals(descriptor.getOutput(), found);
}
}
use of org.stringtemplate.v4.StringRenderer in project antlr4 by antlr.
the class BaseRuntimeTest method testLexer.
public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
mkdir(delegate.getTmpDir());
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.getTmpDir(), 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());
if (delegate instanceof SpecialRuntimeTestAssert) {
((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
} else {
assertEquals(descriptor.getOutput(), found);
assertEquals(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
assertEquals(descriptor.getErrors(), delegate.getParseErrors());
}
}
use of org.stringtemplate.v4.StringRenderer 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;
}
use of org.stringtemplate.v4.StringRenderer 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;
}
Aggregations