Search in sources :

Example 16 with SegmentCode

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

the class JetTemplateCodeVisitor method visitElseif_directive.

@Override
public Code visitElseif_directive(Elseif_directiveContext ctx) {
    BlockCode code = scopeCode.createBlockCode(16);
    SegmentCode expr_code = (SegmentCode) ctx.expression().accept(this);
    code.addLine("else 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("}");
    return code;
}
Also used : BlockCode(jetbrick.template.parser.code.BlockCode) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 17 with SegmentCode

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

the class JetTemplateCodeVisitor method visitBreak_directive.

@Override
public Code visitBreak_directive(Break_directiveContext ctx) {
    assert_inside_of_for_directive(ctx, "#break");
    ExpressionContext expression = ctx.expression();
    String source;
    if (expression != null) {
        SegmentCode c = (SegmentCode) expression.accept(this);
        source = get_if_expression_source(c);
    } else {
        source = "true";
    }
    source = "if (" + source + ") break; // line: " + ctx.getStart().getLine();
    return scopeCode.createLineCode(source);
}
Also used : ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 18 with SegmentCode

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

the class JetTemplateCodeVisitor method visitValue.

@Override
public Code visitValue(ValueContext ctx) {
    Code code = ctx.expression().accept(this);
    String source = code.toString();
    // 如果返回值是 void,那么不需要 print 语句。
    if (code instanceof SegmentCode) {
        Class<?> klass = ((SegmentCode) code).getKlass();
        if (Void.TYPE.equals(klass)) {
            return scopeCode.createLineCode(source + "; // line: " + ctx.getStart().getLine());
        }
    }
    Token token = ((TerminalNode) ctx.getChild(0)).getSymbol();
    if (token.getType() == JetTemplateParser.VALUE_ESCAPED_OPEN) {
        source = "JetUtils.asEscapeHtml(" + source + ")";
    }
    if ("null".equals(source)) {
        // 防止编译出错 (也可以生成一个空行)
        source = "(Object)null";
    }
    return scopeCode.createLineCode("$out.print(" + source + "); // line: " + ctx.getStart().getLine());
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) Token(org.antlr.v4.runtime.Token) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) 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 19 with SegmentCode

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

the class JetTemplateCodeVisitor method visitType.

@Override
public Code visitType(TypeContext ctx) {
    StringBuilder name = new StringBuilder();
    for (TerminalNode node : ctx.IDENTIFIER()) {
        if (name.length() > 0) {
            name.append('.');
        }
        name.append(node.getText());
    }
    // 查找 klass
    Class<?> klass = resolver.resolveClass(name.toString());
    if (klass == null) {
        StringBuilder sb = new StringBuilder(128);
        sb.append("java.lang.ClassNotFoundException: ").append(name);
        sb.append("\n advise: Please define package in 'import.packages' or use full qualified class name.");
        throw reportError(sb.toString(), ctx);
    }
    if (securityManager != null) {
        securityManager.checkMemberAccess(klass);
    }
    // 查找泛型类型 typeArgs
    TypedKlass[] typeArgs = TypedKlass.EMPTY_TYPE_ARGS;
    Type_argumentsContext type_arguments = ctx.type_arguments();
    if (type_arguments != null) {
        SegmentListCode c = (SegmentListCode) type_arguments.accept(this);
        typeArgs = new TypedKlass[c.size()];
        for (int i = 0; i < typeArgs.length; i++) {
            typeArgs[i] = c.getChild(i).getTypedKlass();
        }
    }
    // 如果是数组类型,则把 klass 转成数组
    String array_suffix = "";
    List<Type_array_suffixContext> type_array_suffix = ctx.type_array_suffix();
    for (Type_array_suffixContext c : type_array_suffix) {
        Code code = c.accept(this);
        array_suffix = array_suffix + code.toString();
    }
    if (array_suffix.length() > 0) {
        // 转换成 Array Class, 重新 resolve
        String klassName = name.toString() + array_suffix;
        klass = resolver.resolveClass(klassName);
        if (klass == null) {
            throw reportError("java.lang.ClassNotFoundException: " + klassName, ctx);
        }
    }
    // 返回带有的泛型信息的 Class
    TypedKlass typedKlass = TypedKlass.create(klass, typeArgs);
    return new SegmentCode(typedKlass, typedKlass.toString(), ctx);
}
Also used : 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) Type_argumentsContext(jetbrick.template.parser.grammer.JetTemplateParser.Type_argumentsContext) TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) Type_array_suffixContext(jetbrick.template.parser.grammer.JetTemplateParser.Type_array_suffixContext)

Example 20 with SegmentCode

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

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