Search in sources :

Example 1 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class BaseGoTest 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());
    }
}
Also used : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Example 2 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class BaseJavaTest 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();
        AnalysisPipeline anal = new AnalysisPipeline(g);
        anal.process();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser(false);
        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());
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) STGroup(org.stringtemplate.v4.STGroup) STGroupString(org.stringtemplate.v4.STGroupString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString)

Example 3 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class GoTarget method loadTemplates.

@Override
protected STGroup loadTemplates() {
    STGroup result = super.loadTemplates();
    result.registerRenderer(String.class, new JavaStringRenderer(), true);
    return result;
}
Also used : STGroup(org.stringtemplate.v4.STGroup)

Example 4 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class JavaScriptTarget method loadTemplates.

@Override
protected STGroup loadTemplates() {
    STGroup result = super.loadTemplates();
    result.registerRenderer(String.class, new JavaStringRenderer(), true);
    return result;
}
Also used : STGroup(org.stringtemplate.v4.STGroup)

Example 5 with STGroup

use of org.stringtemplate.v4.STGroup in project antlr4 by antlr.

the class JavaTarget method loadTemplates.

@Override
protected STGroup loadTemplates() {
    STGroup result = targetTemplates.get();
    if (result == null) {
        result = super.loadTemplates();
        result.registerRenderer(String.class, new JavaStringRenderer(), true);
        targetTemplates.set(result);
    }
    return result;
}
Also used : STGroup(org.stringtemplate.v4.STGroup)

Aggregations

STGroup (org.stringtemplate.v4.STGroup)26 ST (org.stringtemplate.v4.ST)14 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 Grammar (org.antlr.v4.tool.Grammar)6 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 STGroupFile (org.stringtemplate.v4.STGroupFile)5 StringRenderer (org.stringtemplate.v4.StringRenderer)5 NumberRenderer (org.stringtemplate.v4.NumberRenderer)3 STErrorListener (org.stringtemplate.v4.STErrorListener)3 STMessage (org.stringtemplate.v4.misc.STMessage)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 URL (java.net.URL)2