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