Search in sources :

Example 1 with AutoIndentWriter

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);
    }
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) IOException(java.io.IOException) STWriter(org.stringtemplate.v4.STWriter) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Writer(java.io.Writer) STWriter(org.stringtemplate.v4.STWriter)

Example 2 with AutoIndentWriter

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);
    }
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) IOException(java.io.IOException) STWriter(org.stringtemplate.v4.STWriter) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Writer(java.io.Writer) STWriter(org.stringtemplate.v4.STWriter)

Example 3 with AutoIndentWriter

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;
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) StringWriter(java.io.StringWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) InstanceScope(org.stringtemplate.v4.InstanceScope) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue)

Example 4 with AutoIndentWriter

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);
        }
    }
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Path(java.nio.file.Path) StringWriter(java.io.StringWriter) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) OutputStream(java.io.OutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with AutoIndentWriter

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();
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringWriter(java.io.StringWriter) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

AutoIndentWriter (org.stringtemplate.v4.AutoIndentWriter)7 StringWriter (java.io.StringWriter)5 IOException (java.io.IOException)3 InstanceScope (org.stringtemplate.v4.InstanceScope)3 ST (org.stringtemplate.v4.ST)3 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 ATNFactory (org.antlr.v4.automata.ATNFactory)2 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)2 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)2 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)2 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)2 Grammar (org.antlr.v4.tool.Grammar)2 LexerGrammar (org.antlr.v4.tool.LexerGrammar)2 STWriter (org.stringtemplate.v4.STWriter)2 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1