Search in sources :

Example 1 with ExpressionContext

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

the class JetTemplateCodeVisitor method visitStop_directive.

@Override
public Code visitStop_directive(Stop_directiveContext ctx) {
    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 + ") return; // line: " + ctx.getStart().getLine();
    return scopeCode.createLineCode(source);
}
Also used : ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) SegmentCode(jetbrick.template.parser.code.SegmentCode)

Example 2 with ExpressionContext

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

the class JetTemplateCodeVisitor method visitInclude_directive.

@Override
public Code visitInclude_directive(Include_directiveContext ctx) {
    Expression_listContext expression_list = ctx.expression_list();
    SegmentListCode childrenCode = (SegmentListCode) expression_list.accept(this);
    if (childrenCode.size() > 2) {
        throw reportError("Arguments mismatch for #include directive.", ctx);
    }
    // argument 1: file
    SegmentCode fileCode = childrenCode.getChild(0);
    ExpressionContext fileExpression = expression_list.expression(0);
    if (!String.class.equals(fileCode.getKlass())) {
        throw reportError("Type mismatch: the first argument cannot convert from " + fileCode.getKlassName() + " to String", fileExpression);
    }
    // argument 2: parameters
    SegmentCode parametersCode = null;
    if (childrenCode.size() > 1) {
        parametersCode = childrenCode.getChild(1);
        if (!(Map.class.equals(parametersCode.getKlass()))) {
            throw reportError("Type mismatch: the second argument cannot convert from " + parametersCode.getKlassName() + " to Map", expression_list.expression(1));
        }
    }
    // 如果 file 是常量,那么进行 file.exists() 校验
    if (fileExpression instanceof Expr_constantContext) {
        String file = fileCode.toString();
        file = file.substring(1, file.length() - 1);
        file = StringEscapeUtils.unescapeJava(file);
        file = PathUtils.getAbsolutionName(resource.getName(), file);
        if (!engine.lookupResource(file)) {
            throw reportError("FileNotFoundException: " + file, fileExpression);
        }
    }
    // 生成代码
    StringBuilder source = new StringBuilder();
    source.append("JetUtils.asInclude($ctx, ");
    source.append(fileCode.toString());
    source.append(", (Map<String, Object>)");
    source.append((parametersCode != null) ? parametersCode.toString() : "null");
    source.append("); // line: ");
    source.append(ctx.getStart().getLine());
    return scopeCode.createLineCode(source.toString());
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) Expression_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext) Expr_constantContext(jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext) SegmentListCode(jetbrick.template.parser.code.SegmentListCode)

Example 3 with ExpressionContext

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

the class JetTemplateCodeVisitor method visitExpr_new_array.

@Override
public Code visitExpr_new_array(Expr_new_arrayContext ctx) {
    SegmentCode code = (SegmentCode) ctx.type().accept(this);
    if (code.getKlass().isArray()) {
        throw reportError("Cannot specify an array dimension after an empty dimension", ctx.type());
    }
    StringBuilder typeSource = new StringBuilder(code.toString());
    // 生成代码
    StringBuilder source = new StringBuilder(32);
    source.append("(new ").append(code.toString());
    for (ExpressionContext expression : ctx.expression()) {
        SegmentCode c = (SegmentCode) expression.accept(this);
        if (!ClassUtils.isAssignable(Integer.TYPE, c.getKlass())) {
            throw reportError("Type mismatch: cannot convert from " + c.getKlassName() + " to int.", expression);
        }
        source.append('[').append(c.toString()).append(']');
        typeSource.append("[]");
    }
    source.append(')');
    TypedKlass resultKlass = resolver.resolveTypedKlass(typeSource.toString());
    return new SegmentCode(resultKlass, source.toString(), ctx);
}
Also used : TypedKlass(jetbrick.template.parser.support.TypedKlass) SegmentCode(jetbrick.template.parser.code.SegmentCode) ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)

Example 4 with ExpressionContext

use of jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext 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 5 with ExpressionContext

use of jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext 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

ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)10 SegmentCode (jetbrick.template.parser.code.SegmentCode)9 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)3 ConstantContext (jetbrick.template.parser.grammer.JetTemplateParser.ConstantContext)2 BlockCode (jetbrick.template.parser.code.BlockCode)1 Expr_constantContext (jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext)1 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)1 TypedKlass (jetbrick.template.parser.support.TypedKlass)1