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);
}
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());
}
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);
}
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);
}
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;
}
Aggregations