Search in sources :

Example 6 with Expression_listContext

use of jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitExpr_function_call.

@Override
public Code visitExpr_function_call(Expr_function_callContext ctx) {
    // 处理参数
    Expression_listContext expression_list = ctx.expression_list();
    SegmentListCode segmentListCode = (expression_list == null) ? SegmentListCode.EMPTY : (SegmentListCode) expression_list.accept(this);
    Class<?>[] parameterTypes = segmentListCode.getParameterTypes();
    // 查找方法
    String name = ctx.IDENTIFIER().getText();
    // 优先查找 macro
    MacroCode macroCode = null;
    if (macroMap != null) {
        macroCode = macroMap.get(name);
        if (macroCode != null) {
            // macro 参数匹配
            SegmentListCode defineListCode = macroCode.getDefineListCode();
            int size = (defineListCode == null) ? 0 : defineListCode.size();
            if (parameterTypes.length != size) {
                throw reportError("Arguments mismatch for #macro " + getMethodSignature(name, parameterTypes) + ".", ctx.IDENTIFIER());
            }
            for (int i = 0; i < parameterTypes.length; i++) {
                if (!ClassUtils.isAssignable(parameterTypes[i], defineListCode.getChild(i).getKlass())) {
                    throw reportError("Arguments mismatch for #macro " + getMethodSignature(name, parameterTypes) + ".", ctx.IDENTIFIER());
                }
            }
            // 生成 macro 调用 code
            StringBuilder sb = new StringBuilder(64);
            sb.append("$macro_").append(name);
            sb.append("($ctx");
            if (segmentListCode.size() > 0) {
                sb.append(',').append(segmentListCode.toString());
            }
            sb.append(')');
            return new SegmentCode(TypedKlass.VOID, sb.toString(), ctx);
        }
    }
    // 查找扩展方法
    boolean advanced = false;
    Method method = resolver.resolveFunction(name, parameterTypes);
    if (method == null) {
        method = resolver.resolveFunction_advanced(name, parameterTypes);
        advanced = true;
    }
    if (method == null) {
        throw reportError("Undefined function or arguments mismatch: " + getMethodSignature(name, parameterTypes) + ".", ctx.IDENTIFIER());
    }
    if (securityManager != null) {
        securityManager.checkMemberAccess(method);
    }
    // 生成code
    StringBuilder sb = new StringBuilder(64);
    sb.append(ClassUtils.getShortClassName(method.getDeclaringClass()));
    sb.append('.');
    sb.append(name);
    sb.append('(');
    if (advanced) {
        sb.append("$ctx");
    }
    if (segmentListCode.size() > 0) {
        if (advanced)
            sb.append(',');
        sb.append(segmentListCode.toString());
    }
    sb.append(')');
    TypedKlass typedKlass = TypedKlassUtils.getMethodReturnTypedKlass(method);
    return new SegmentCode(typedKlass, sb.toString(), ctx);
}
Also used : TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) Expression_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext) Method(java.lang.reflect.Method) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) MacroCode(jetbrick.template.parser.code.MacroCode)

Example 7 with Expression_listContext

use of jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitExpr_array_list.

@Override
public Code visitExpr_array_list(Expr_array_listContext ctx) {
    String source = "Collections.EMPTY_LIST";
    Expression_listContext expression_list = ctx.expression_list();
    if (expression_list != null) {
        Code code = expression_list.accept(this);
        source = "Arrays.asList(" + code.toString() + ")";
    }
    return new SegmentCode(List.class, source, ctx);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) Expression_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext) ScopeCode(jetbrick.template.parser.code.ScopeCode) BlockCode(jetbrick.template.parser.code.BlockCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) TemplateClassCode(jetbrick.template.parser.code.TemplateClassCode) TextCode(jetbrick.template.parser.code.TextCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) MacroCode(jetbrick.template.parser.code.MacroCode) Code(jetbrick.template.parser.code.Code) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) TagCode(jetbrick.template.parser.code.TagCode)

Aggregations

SegmentListCode (jetbrick.template.parser.code.SegmentListCode)7 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)7 SegmentCode (jetbrick.template.parser.code.SegmentCode)6 Method (java.lang.reflect.Method)4 TypedKlass (jetbrick.template.parser.support.TypedKlass)3 MacroCode (jetbrick.template.parser.code.MacroCode)2 TagCode (jetbrick.template.parser.code.TagCode)2 BlockCode (jetbrick.template.parser.code.BlockCode)1 Code (jetbrick.template.parser.code.Code)1 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)1 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)1 ScopeCode (jetbrick.template.parser.code.ScopeCode)1 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)1 TextCode (jetbrick.template.parser.code.TextCode)1 Expr_constantContext (jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext)1 Expr_identifierContext (jetbrick.template.parser.grammer.JetTemplateParser.Expr_identifierContext)1 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)1