Search in sources :

Example 6 with AutoIndentWriter

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

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)

Example 7 with AutoIndentWriter

use of org.stringtemplate.v4.AutoIndentWriter in project bndtools by bndtools.

the class StringTemplateEngine method extractAttrs.

private void extractAttrs(ST st, final Map<String, String> attrs) throws Exception {
    Interpreter interpreter = new Interpreter(st.groupThatCreatedThisInstance, Locale.getDefault(), true) {

        @Override
        public Object getAttribute(InstanceScope scope, String name) {
            attrs.put(name, null);
            return "X";
        }
    };
    StringWriter writer = new StringWriter();
    interpreter.exec(new AutoIndentWriter(writer), new InstanceScope(null, st));
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Interpreter(org.stringtemplate.v4.Interpreter) StringWriter(java.io.StringWriter) InstanceScope(org.stringtemplate.v4.InstanceScope)

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