Search in sources :

Example 1 with Macro

use of org.codehaus.groovy.macro.runtime.Macro in project groovy by apache.

the class MacroGroovyMethods method macro.

@Macro
public static Expression macro(MacroContext macroContext, PropertyExpression phaseExpression, ConstantExpression asIsConstantExpression, ClosureExpression closureExpression) {
    if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
        macroContext.getSourceUnit().addError(new SyntaxException("Macro closure arguments are not allowed" + '\n', closureExpression));
        return macroContext.getCall();
    }
    final String source;
    try {
        source = ClosureUtils.convertClosureToSource(macroContext.getSourceUnit().getSource(), closureExpression);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
    Boolean asIs = (Boolean) asIsConstantExpression.getValue();
    return callX(propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), "macro", args(phaseExpression != null ? phaseExpression : constX(null), asIsConstantExpression, constX(source), buildSubstitutions(macroContext.getSourceUnit(), closureExpression), classX(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false))));
}
Also used : SyntaxException(org.codehaus.groovy.syntax.SyntaxException) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MacroBuilder(org.codehaus.groovy.macro.runtime.MacroBuilder) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) Macro(org.codehaus.groovy.macro.runtime.Macro)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1 Macro (org.codehaus.groovy.macro.runtime.Macro)1 MacroBuilder (org.codehaus.groovy.macro.runtime.MacroBuilder)1 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)1