Search in sources :

Example 16 with STGroup

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

Example 17 with STGroup

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

the class BaseRuntimeTest method testParser.

public void testParser(RuntimeTestDescriptor descriptor) throws Exception {
    mkdir(delegate.getTmpDir());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTmpDir(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.importTemplates(targetTemplates);
    g.registerRenderer(String.class, new StringRenderer());
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execParser(grammarName + ".g4", grammar, grammarName + "Parser", grammarName + "Lexer", grammarName + "Listener", grammarName + "Visitor", descriptor.getStartRule(), descriptor.getInput(), descriptor.showDiagnosticErrors());
    if (delegate instanceof SpecialRuntimeTestAssert) {
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
    } else {
        assertEquals(descriptor.getErrors(), delegate.getParseErrors());
        assertEquals(descriptor.getOutput(), found);
    }
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 18 with STGroup

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

the class BaseRuntimeTest method testLexer.

public void testLexer(RuntimeTestDescriptor descriptor) throws Exception {
    mkdir(delegate.getTmpDir());
    Pair<String, String> pair = descriptor.getGrammar();
    ClassLoader cloader = getClass().getClassLoader();
    URL templates = cloader.getResource("org/antlr/v4/test/runtime/templates/" + descriptor.getTarget() + ".test.stg");
    STGroupFile targetTemplates = new STGroupFile(templates, "UTF-8", '<', '>');
    targetTemplates.registerRenderer(String.class, new StringRenderer());
    // write out any slave grammars
    List<Pair<String, String>> slaveGrammars = descriptor.getSlaveGrammars();
    if (slaveGrammars != null) {
        for (Pair<String, String> spair : slaveGrammars) {
            STGroup g = new STGroup('<', '>');
            g.registerRenderer(String.class, new StringRenderer());
            g.importTemplates(targetTemplates);
            ST grammarST = new ST(g, spair.b);
            writeFile(delegate.getTmpDir(), spair.a + ".g4", grammarST.render());
        }
    }
    String grammarName = pair.a;
    String grammar = pair.b;
    STGroup g = new STGroup('<', '>');
    g.registerRenderer(String.class, new StringRenderer());
    g.importTemplates(targetTemplates);
    ST grammarST = new ST(g, grammar);
    grammar = grammarST.render();
    String found = delegate.execLexer(grammarName + ".g4", grammar, grammarName, descriptor.getInput(), descriptor.showDFA());
    if (delegate instanceof SpecialRuntimeTestAssert) {
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getOutput(), found);
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
        ((SpecialRuntimeTestAssert) delegate).assertEqualStrings(descriptor.getErrors(), delegate.getParseErrors());
    } else {
        assertEquals(descriptor.getOutput(), found);
        assertEquals(descriptor.getANTLRToolErrors(), delegate.getANTLRToolErrors());
        assertEquals(descriptor.getErrors(), delegate.getParseErrors());
    }
}
Also used : ST(org.stringtemplate.v4.ST) STGroup(org.stringtemplate.v4.STGroup) StringRenderer(org.stringtemplate.v4.StringRenderer) STGroupFile(org.stringtemplate.v4.STGroupFile) URL(java.net.URL) Pair(org.antlr.v4.runtime.misc.Pair)

Example 19 with STGroup

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

the class BaseCppTest 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 20 with STGroup

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

the class TestAttributeChecks method testActions.

public void testActions(String location, String[] pairs, String template) {
    for (int i = 0; i < pairs.length; i += 2) {
        String action = pairs[i];
        String expected = pairs[i + 1];
        STGroup g = new STGroup('<', '>');
        // hush warnings
        g.setListener(new ErrorBuffer());
        ST st = new ST(g, template);
        st.add(location, action);
        String grammar = st.render();
        testErrors(new String[] { grammar, expected }, false);
    }
}
Also used : ST(org.stringtemplate.v4.ST) ErrorBuffer(org.stringtemplate.v4.misc.ErrorBuffer) 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