Search in sources :

Example 41 with SegmentCode

use of jetbrick.template.parser.code.SegmentCode 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)

Example 42 with SegmentCode

use of jetbrick.template.parser.code.SegmentCode in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitExpr_identifier.

@Override
public Code visitExpr_identifier(Expr_identifierContext ctx) {
    String name = assert_java_identifier(ctx.IDENTIFIER(), false);
    // 特殊处理 for 变量
    if ("for".equals(name)) {
        assert_inside_of_for_directive(ctx, "Local variable \"for\"");
        // 强制映射成 JetForStatus $for
        // 取出 forStatus 的实际变量名
        String forStatus = forStack.peek();
        return new SegmentCode(TypedKlass.JetForStatus, forStatus, ctx);
    }
    // 找到变量的类型
    TypedKlass resultKlass = scopeCode.resolve(name, false);
    if (resultKlass == null) {
        // 没有定义过,继续向上深度查找
        resultKlass = scopeCode.resolve(name, true);
        // 没有定义过,则查找全局定义
        if (resultKlass == null) {
            resultKlass = resolver.resolveVariable(name);
        }
        if (scopeCode.define(name, resultKlass, true)) {
            if (resultKlass == TypedKlass.Object) {
            //removed unsed warning in 1.2.0
            //log.warn("line " + ctx.getStart().getLine() + ": Implicit definition for context variable: " + resultKlass.toString() + " " + name);
            }
        }
    }
    return new SegmentCode(resultKlass, name, ctx);
}
Also used : TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Aggregations

SegmentCode (jetbrick.template.parser.code.SegmentCode)42 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)14 TypedKlass (jetbrick.template.parser.support.TypedKlass)12 BlockCode (jetbrick.template.parser.code.BlockCode)11 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)9 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)9 MacroCode (jetbrick.template.parser.code.MacroCode)8 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)7 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)7 Code (jetbrick.template.parser.code.Code)6 ScopeCode (jetbrick.template.parser.code.ScopeCode)6 TagCode (jetbrick.template.parser.code.TagCode)6 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)6 TextCode (jetbrick.template.parser.code.TextCode)6 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)6 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Field (java.lang.reflect.Field)2 ConstantContext (jetbrick.template.parser.grammer.JetTemplateParser.ConstantContext)2