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