package com.github.chrisbrenton.grappa.formal;

import com.github.fge.grappa.parsers.BaseParser;
import com.github.fge.grappa.rules.Rule;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.writer.OutputStreamCodeWriter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public final class CodeModelTest
{
    private CodeModelTest()
    {
        throw new Error("no instantiation is permitted");
    }

    public static void main(final String... args)
        throws JClassAlreadyExistsException, IOException
    {
        final JCodeModel model = new JCodeModel();

        final JClass e = model.ref(BaseParser.class).narrow(Object.class);

        final JDefinedClass c = model._class("foo.bar.Baz");
        c._extends(e);

        final JMethod method = c.method(JMod.PUBLIC, Rule.class, "someRule");

        method.body()._return(JExpr._null());

        final CodeWriter cw = new OutputStreamCodeWriter(System.out,
            StandardCharsets.UTF_8.displayName());

        model.build(cw);
    }
}

@@ -164,24 +164,57 @@
 CodeWriter;%0A
+import com.sun.codemodel.JBlock;%0A
 import com.s
@@ -1142,42 +1142,116 @@
     
-method.body()._return(JExpr._null(
+final JBlock body = method.body();%0A        body._return(JExpr.invoke(%22foo%22).arg(%22x%22).arg(JExpr.invoke(%22bar%22)
 ));%0A

make the example a little more worthwhile