Search in sources :

Example 1 with Code

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

the class JetTemplateCodeVisitor method visitFor_directive.

@Override
public Code visitFor_directive(For_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    String id_for = getUid("for");
    scopeCode = scopeCode.push();
    // 注意:for循环变量的作用域要放在 for 内部, 防止出现变量重定义错误
    ForExpressionCode for_expr_code = (ForExpressionCode) ctx.for_expression().accept(this);
    // for block
    forStack.push(id_for);
    Code for_block_code = ctx.block().accept(this);
    forStack.pop();
    scopeCode = scopeCode.pop();
    // for-else
    Else_directiveContext else_directive = ctx.else_directive();
    Code for_else_block = (else_directive == null) ? null : else_directive.accept(this);
    // 生成代码
    String id_foritem = getUid("foritem");
    String typeName = for_expr_code.getKlassName();
    String itemName = for_expr_code.getName();
    code.addLine("Object " + id_foritem + " = context.get(\"" + itemName + "\"); // save it");
    code.addLine("JetForIterator " + id_for + " = new JetForIterator(" + for_expr_code.toString() + ");");
    code.addLine("while (" + id_for + ".hasNext()) { // line: " + ctx.getStart().getLine());
    // class item = (class) it.next() ...
    code.addLine("  " + typeName + " " + itemName + " = (" + typeName + ") " + id_for + ".next();");
    code.addLine("  context.put(\"" + itemName + "\", " + itemName + ");");
    code.addChild(for_block_code);
    code.addLine("}");
    code.addLine("context.put(\"" + itemName + "\", " + id_foritem + "); // reset it");
    // for else ...
    if (for_else_block != null) {
        code.addLine("if (" + id_for + ".empty()) { // line: " + ctx.getStart().getLine());
        code.addChild(for_else_block);
        code.addLine("}");
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) ForExpressionCode(jetbrick.template.parser.code.ForExpressionCode) Else_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext) 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 2 with Code

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

the class JetTemplateCodeVisitor method visitType_list.

@Override
public Code visitType_list(Type_listContext ctx) {
    List<Type_nameContext> type_name_list = ctx.type_name();
    SegmentListCode code = new SegmentListCode(type_name_list.size());
    for (Type_nameContext type_name : type_name_list) {
        Code c = type_name.accept(this);
        code.addChild((SegmentCode) c);
    }
    return code;
}
Also used : Type_nameContext(jetbrick.template.parser.grammer.JetTemplateParser.Type_nameContext) SegmentListCode(jetbrick.template.parser.code.SegmentListCode) 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 3 with Code

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

the class JetTemplateCodeVisitor method visitExpr_class_cast.

@Override
public Code visitExpr_class_cast(Expr_class_castContext ctx) {
    SegmentCode code = (SegmentCode) ctx.type().accept(this);
    Code expr_code = ctx.expression().accept(this);
    String source = "((" + code.toString() + ")" + expr_code.toString() + ")";
    return new SegmentCode(code.getTypedKlass(), source, ctx);
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) 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 4 with Code

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

the class JetTemplateCodeVisitor method visitExpr_hash_map.

@Override
public Code visitExpr_hash_map(Expr_hash_mapContext ctx) {
    String source = "Collections.EMPTY_MAP";
    Hash_map_entry_listContext hash_map_entry_list = ctx.hash_map_entry_list();
    if (hash_map_entry_list != null) {
        Code code = hash_map_entry_list.accept(this);
        source = "JetUtils.asMap(" + code.toString() + ")";
    }
    return new SegmentCode(Map.class, source, ctx);
}
Also used : Hash_map_entry_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Hash_map_entry_listContext) SegmentCode(jetbrick.template.parser.code.SegmentCode) 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 5 with Code

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

the class JetTemplateCodeVisitor method visitSet_directive.

@Override
public Code visitSet_directive(Set_directiveContext ctx) {
    List<Set_expressionContext> set_expression_list = ctx.set_expression();
    BlockCode code = scopeCode.createBlockCode(set_expression_list.size());
    for (Set_expressionContext node : set_expression_list) {
        Code c = node.accept(this);
        if (c != null) {
            code.addChild(c);
        }
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) Set_expressionContext(jetbrick.template.parser.grammer.JetTemplateParser.Set_expressionContext) 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

Code (jetbrick.template.parser.code.Code)10 BlockCode (jetbrick.template.parser.code.BlockCode)9 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)9 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)9 MacroCode (jetbrick.template.parser.code.MacroCode)9 ScopeCode (jetbrick.template.parser.code.ScopeCode)9 SegmentCode (jetbrick.template.parser.code.SegmentCode)9 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)9 TagCode (jetbrick.template.parser.code.TagCode)9 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)9 TextCode (jetbrick.template.parser.code.TextCode)9 TemplateContext (jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 DirectiveContext (jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext)1 Else_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext)1 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)1 For_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext)1 Hash_map_entry_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Hash_map_entry_listContext)1 If_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext)1 Macro_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext)1