Search in sources :

Example 1 with InstanceScope

use of org.stringtemplate.v4.InstanceScope 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 2 with InstanceScope

use of org.stringtemplate.v4.InstanceScope 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

StringWriter (java.io.StringWriter)2 AutoIndentWriter (org.stringtemplate.v4.AutoIndentWriter)2 InstanceScope (org.stringtemplate.v4.InstanceScope)2 ArrayList (java.util.ArrayList)1 ATNFactory (org.antlr.v4.automata.ATNFactory)1 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)1 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)1 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)1 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)1 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)1 Grammar (org.antlr.v4.tool.Grammar)1 LexerGrammar (org.antlr.v4.tool.LexerGrammar)1 Interpreter (org.stringtemplate.v4.Interpreter)1 ST (org.stringtemplate.v4.ST)1