use of org.stringtemplate.v4.AutoIndentWriter in project antlr4 by tunnelvisionlabs.
the class CodeGenerator method write.
public void write(ST code, String fileName) {
try {
@SuppressWarnings("unused") long start = System.currentTimeMillis();
Writer w = tool.getOutputFileWriter(g, fileName);
STWriter wr = new AutoIndentWriter(w);
wr.setLineWidth(lineWidth);
code.write(wr);
w.close();
@SuppressWarnings("unused") long stop = System.currentTimeMillis();
} catch (IOException ioe) {
tool.errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe, fileName);
}
}
use of org.stringtemplate.v4.AutoIndentWriter in project antlr4 by antlr.
the class CodeGenerator method write.
public void write(ST code, String fileName) {
try {
// long start = System.currentTimeMillis();
Writer w = tool.getOutputFileWriter(g, fileName);
STWriter wr = new AutoIndentWriter(w);
wr.setLineWidth(lineWidth);
code.write(wr);
w.close();
// long stop = System.currentTimeMillis();
} catch (IOException ioe) {
tool.errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe, fileName);
}
}
use of org.stringtemplate.v4.AutoIndentWriter in project antlr4 by antlr.
the class TestCodeGeneration method getEvalInfoForString.
public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammarString);
List<String> evals = new ArrayList<String>();
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();
// STViz viz = outputFileST.inspect();
// try {
// viz.waitForClose();
// }
// catch (Exception e) {
// e.printStackTrace();
// }
boolean debug = false;
DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
InstanceScope scope = new InstanceScope(null, outputFileST);
StringWriter sw = new StringWriter();
AutoIndentWriter out = new AutoIndentWriter(sw);
interp.exec(out, scope);
for (String e : interp.evals) {
if (e.contains(pattern)) {
evals.add(e);
}
}
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
return evals;
}
use of org.stringtemplate.v4.AutoIndentWriter in project buck by facebook.
the class IjProjectWriter method writeToFile.
@VisibleForTesting
protected void writeToFile(ST contents, Path path) throws IOException {
StringWriter stringWriter = new StringWriter();
AutoIndentWriter noIndentWriter = new AutoIndentWriter(stringWriter);
contents.write(noIndentWriter);
byte[] renderedContentsBytes = noIndentWriter.toString().getBytes(StandardCharsets.UTF_8);
if (projectFilesystem.exists(path)) {
Sha1HashCode fileSha1 = projectFilesystem.computeSha1(path);
Sha1HashCode contentsSha1 = Sha1HashCode.fromHashCode(Hashing.sha1().hashBytes(renderedContentsBytes));
if (fileSha1.equals(contentsSha1)) {
return;
}
}
boolean danglingTempFile = false;
Path tempFile = projectFilesystem.createTempFile(IDEA_CONFIG_DIR_PREFIX, path.getFileName().toString(), ".tmp");
try {
danglingTempFile = true;
try (OutputStream outputStream = projectFilesystem.newFileOutputStream(tempFile)) {
outputStream.write(contents.render().getBytes());
}
projectFilesystem.createParentDirs(path);
projectFilesystem.move(tempFile, path, StandardCopyOption.REPLACE_EXISTING);
danglingTempFile = false;
} finally {
if (danglingTempFile) {
projectFilesystem.deleteFileAtPath(tempFile);
}
}
}
use of org.stringtemplate.v4.AutoIndentWriter 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();
}
Aggregations