use of org.stringtemplate.v4.STGroup in project buck by facebook.
the class BuckPyFunction method toPythonFunction.
public String toPythonFunction(BuildRuleType type, Object dto) {
@Nullable TargetName defaultName = dto.getClass().getAnnotation(TargetName.class);
ImmutableList.Builder<StParamInfo> mandatory = ImmutableList.builder();
ImmutableList.Builder<StParamInfo> optional = ImmutableList.builder();
for (ParamInfo param : ImmutableSortedSet.copyOf(argMarshaller.getAllParamInfo(dto))) {
if (isSkippable(param)) {
continue;
}
if (param.isOptional()) {
optional.add(new StParamInfo(param));
} else {
mandatory.add(new StParamInfo(param));
}
}
optional.add(StParamInfo.ofOptionalValue("autodeps", "autodeps"));
optional.add(StParamInfo.ofOptionalValue("visibility", "visibility"));
STGroup group = buckPyFunctionTemplate.get();
ST st;
// See discussion in: https://github.com/antlr/stringtemplate4/issues/61
synchronized (group) {
st = group.getInstanceOf("buck_py_function");
}
st.add("name", type.getName());
// Mandatory params must come before optional ones.
st.add("params", ImmutableList.builder().addAll(mandatory.build()).addAll(optional.build()).build());
st.add("typePropName", TYPE_PROPERTY_NAME);
st.add("defaultName", defaultName == null ? null : defaultName.name());
StringWriter stringWriter = new StringWriter();
try {
st.write(new AutoIndentWriter(stringWriter, "\n"));
} catch (IOException e) {
throw new IllegalStateException("ST writer should not throw with StringWriter", e);
}
return stringWriter.toString();
}
use of org.stringtemplate.v4.STGroup 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.STGroup 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.STGroup in project antlr4 by antlr.
the class BaseCppTest method testActions.
public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
int lp = templates.indexOf('(');
String name = templates.substring(0, lp);
STGroup group = new STGroupString(templates);
ST st = group.getInstanceOf(name);
st.add(actionName, action);
String grammar = st.render();
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammar, equeue);
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
String output = outputFileST.render();
//System.out.println(output);
String b = "#" + actionName + "#";
int start = output.indexOf(b);
String e = "#end-" + actionName + "#";
int end = output.indexOf(e);
String snippet = output.substring(start + b.length(), end);
assertEquals(expected, snippet);
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
}
use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.
the class TestAttributeChecks method testActions.
public void testActions(String location, String[] pairs, String template) {
for (int i = 0; i < pairs.length; i += 2) {
String action = pairs[i];
String expected = pairs[i + 1];
STGroup g = new STGroup('<', '>');
// hush warnings
g.setListener(new ErrorBuffer());
ST st = new ST(g, template);
st.add(location, action);
String grammar = st.render();
testErrors(new String[] { grammar, expected }, false);
}
}
Aggregations