Search in sources :

Example 6 with BlockCode

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

the class JetTemplateCodeVisitor method visitPut_directive.

@Override
public Code visitPut_directive(Put_directiveContext ctx) {
    List<ExpressionContext> expression_list = ctx.expression();
    int size = expression_list.size();
    if (size == 0 || size % 2 == 1) {
        throw reportError("Mismatched arguments count for #put directive", ctx);
    }
    BlockCode code = scopeCode.createBlockCode(size / 2);
    for (int i = 0; i < size; i += 2) {
        SegmentCode name = (SegmentCode) expression_list.get(i).accept(this);
        SegmentCode value = (SegmentCode) expression_list.get(i + 1).accept(this);
        if (!String.class.equals(name.getKlass())) {
            throw reportError("The argument type can't cast to String.class for #put directive", name.getNode());
        }
        assert_not_void_expression(value);
        code.addLine(Code.CONTEXT_NAME + ".putAsParents(" + name.toString() + ", " + value.toString() + "); // line: " + ctx.getStart().getLine());
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 7 with BlockCode

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

the class JetTemplateCodeVisitor method visitIf_directive.

@Override
public Code visitIf_directive(If_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    SegmentCode expr_code = (SegmentCode) ctx.expression().accept(this);
    code.addLine("if (" + get_if_expression_source(expr_code) + ") { // line: " + ctx.getStart().getLine());
    scopeCode = scopeCode.push();
    code.addChild(ctx.block().accept(this));
    scopeCode = scopeCode.pop();
    code.addLine("}");
    // elseif ...
    List<Elseif_directiveContext> elseif_directive_list = ctx.elseif_directive();
    for (Elseif_directiveContext elseif_directive : elseif_directive_list) {
        code.addChild(elseif_directive.accept(this));
    }
    // else ...
    Else_directiveContext else_directive = ctx.else_directive();
    if (else_directive != null) {
        code.addChild(else_directive.accept(this));
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) Elseif_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Elseif_directiveContext) Else_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 8 with BlockCode

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

the class JetTemplateCodeVisitor method visitDefine_directive.

@Override
public Code visitDefine_directive(Define_directiveContext ctx) {
    SegmentListCode define_expression_list = (SegmentListCode) ctx.define_expression_list().accept(this);
    BlockCode code = scopeCode.createBlockCode(define_expression_list.size());
    for (SegmentCode node : define_expression_list.getChildren()) {
        DefineExpressionCode c = (DefineExpressionCode) node;
        String name = c.getName();
        if (!scopeCode.define(name, c.getTypedKlass())) {
            throw reportError("Duplicate local variable " + name, c.getNode());
        }
        String typeName = c.getTypedKlass().asBoxedTypedKlass().toString();
        code.addLine(typeName + " " + name + " = (" + typeName + ") " + Code.CONTEXT_NAME + ".get(\"" + name + "\"); // line: " + c.getNode().getStart().getLine());
    }
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) SegmentCode(jetbrick.template.parser.code.SegmentCode) DefineExpressionCode(jetbrick.template.parser.code.DefineExpressionCode) SegmentListCode(jetbrick.template.parser.code.SegmentListCode)

Example 9 with BlockCode

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

the class JetTemplateCodeVisitor method visitBlock.

@Override
public Code visitBlock(BlockContext ctx) {
    int size = ctx.getChildCount();
    BlockCode code = scopeCode.createBlockCode(size);
    if (size == 0)
        return code;
    for (int i = 0; i < size; i++) {
        ParseTree node = ctx.children.get(i);
        Code c = node.accept(this);
        if (node instanceof TextContext) {
            // 文本节点
            TextCode textCode = (TextCode) c;
            if (trimDirectiveLine || trimDirectiveComments) {
                ParseTree prev = (i > 0) ? ctx.children.get(i - 1) : null;
                ParseTree next = (i < size - 1) ? ctx.children.get(i + 1) : null;
                boolean trimLeft;
                boolean keepLeftNewLine = false;
                if (prev == null) {
                    trimLeft = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimLeft = prev instanceof DirectiveContext;
                    if (trimLeft) {
                        // inline directive, 对于一个内联的 #if, #for 指令,后面有要求保留一个 NewLine
                        // @see https://github.com/subchen/jetbrick-template/issues/25
                        ParserRuleContext directive = (ParserRuleContext) ((DirectiveContext) prev).getChild(0);
                        if (directive instanceof If_directiveContext || directive instanceof For_directiveContext) {
                            if (directive.getStart().getLine() == directive.getStop().getLine()) {
                                // 保留一个 NewLine
                                keepLeftNewLine = true;
                            }
                        }
                    }
                }
                boolean trimRight;
                if (next == null) {
                    trimRight = !(ctx.getParent() instanceof TemplateContext);
                } else {
                    trimRight = (next instanceof DirectiveContext);
                }
                // trim 指令两边的注释
                if (trimDirectiveComments) {
                    textCode.trimComments(trimLeft, trimRight, commentsPrefix, commentsSuffix);
                }
                // trim 指令两边的空白内容
                if (trimDirectiveLine) {
                    textCode.trimEmptyLine(trimLeft, trimRight, keepLeftNewLine);
                }
                // trim 掉 #tag 和 #macro 指令最后一个多余的 '\n'
                if (next == null) {
                    if (ctx.getParent() instanceof Tag_directiveContext || ctx.getParent() instanceof Macro_directiveContext) {
                        textCode.trimLastNewLine();
                    }
                }
            }
            if (!textCode.isEmpty()) {
                // 如果有相同内容的Text,则从缓存中读取
                TextCode old = textCache.get(textCode.getText());
                if (old == null) {
                    old = textCode;
                    textCache.put(textCode.getText(), textCode);
                    // add text into field
                    tcc.addField(textCode.getId(), textCode.getText());
                }
                code.addLine(old.toString());
            }
        } else {
            code.addChild(c);
        }
    }
    return code;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) For_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext) TemplateContext(jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext) 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) BlockCode(jetbrick.template.parser.code.BlockCode) If_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext) TextContext(jetbrick.template.parser.grammer.JetTemplateParser.TextContext) Macro_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext) DirectiveContext(jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext) TextCode(jetbrick.template.parser.code.TextCode) Tag_directiveContext(jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

BlockCode (jetbrick.template.parser.code.BlockCode)9 SegmentCode (jetbrick.template.parser.code.SegmentCode)8 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)4 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)4 Code (jetbrick.template.parser.code.Code)3 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)3 MacroCode (jetbrick.template.parser.code.MacroCode)3 ScopeCode (jetbrick.template.parser.code.ScopeCode)3 TagCode (jetbrick.template.parser.code.TagCode)3 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)3 TextCode (jetbrick.template.parser.code.TextCode)3 Else_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Else_directiveContext)2 If_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.If_directiveContext)2 DirectiveContext (jetbrick.template.parser.grammer.JetTemplateParser.DirectiveContext)1 Elseif_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Elseif_directiveContext)1 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)1 For_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.For_directiveContext)1 Macro_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Macro_directiveContext)1 Set_expressionContext (jetbrick.template.parser.grammer.JetTemplateParser.Set_expressionContext)1 Tag_directiveContext (jetbrick.template.parser.grammer.JetTemplateParser.Tag_directiveContext)1