use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseTag.
protected TagStatement parseTag(FunctionTagCallContext fc) {
String id = this.getID(fc.functionNs().Identifier());
ExpressionListContext expListCtx = fc.expressionList();
List<ExpressionContext> list = null;
if (expListCtx != null) {
list = fc.expressionList().expression();
} else {
list = Collections.EMPTY_LIST;
}
Expression[] expList = this.parseExpressionCtxList(list);
if (id.equals("htmltagvar")) {
int line = fc.functionNs().getStart().getLine();
// 标签具有绑定变量功能
Literal l = (Literal) expList[2];
String varList = (String) l.obj;
String[] vars = varList.split(",");
// 定义的变量仅仅在标签体内可见
this.pbCtx.enterBlock();
VarDefineNode[] varDefine = new VarDefineNode[vars.length];
for (int i = 0; i < vars.length; i++) {
VarDefineNode varNode = new VarDefineNode(this.getBTToken(vars[i].trim(), line));
this.pbCtx.addVarAndPostion(varNode);
varDefine[i] = varNode;
}
BlockContext blockCtx = fc.block();
Statement block = parseBlock(blockCtx.statement(), blockCtx);
this.pbCtx.exitBlock();
TagFactory tf = this.gt.getTagFactory(id);
if (tf == null) {
BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
throw ex;
}
TagStatement tag = new TagVarBindingStatement(id, expList, block, varDefine, this.getBTToken(id, line));
return tag;
} else {
BlockContext blockCtx = fc.block();
Statement block = parseBlock(blockCtx.statement(), blockCtx);
TagFactory tf = this.gt.getTagFactory(id);
if (tf == null) {
BeetlException ex = new BeetlException(BeetlException.TAG_NOT_FOUND);
ex.pushToken(this.getBTToken(id, fc.functionNs().getStart().getLine()));
throw ex;
}
TagStatement tag = new TagStatement(id, expList, block, this.getBTToken(id, fc.functionNs().getStart().getLine()));
return tag;
}
}
use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method build.
/**
* 通过Antlr的ParseTree生成Beetl的ProgramMetaData
* @param tree
* @return
*/
public ProgramMetaData build(ParseTree tree) {
int size = tree.getChildCount() - 1;
List<Statement> ls = new ArrayList<Statement>(size);
for (int i = 0; i < size; i++) {
Statement st = parseStatment((ParserRuleContext) tree.getChild(i));
if (st != null) {
ls.add(st);
}
}
if (pbCtx.current.gotoValue == IGoto.RETURN || pbCtx.current.gotoValue == IGoto.BREAK) {
// 如果顶级scope也有return 和break,则检测
data.hasGoto = true;
}
pbCtx.anzlyszeGlobal();
pbCtx.anzlyszeLocal();
data.varIndexSize = pbCtx.varIndexSize;
data.tempVarStartIndex = pbCtx.globalIndexMap.size();
data.statements = ls.toArray(new Statement[0]);
data.globalIndexMap = pbCtx.globalIndexMap;
data.globalVarAttr = pbCtx.globaVarAttr;
data.setTemplateRootScopeIndexMap(pbCtx.rootIndexMap);
return data;
}
use of org.beetl.core.statement.Statement in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseIf.
protected IfStatement parseIf(IfStContext ctx) {
ParExpressionContext pe = ctx.parExpression();
ExpressionContext expCtx = pe.expression();
Expression exp = this.parseExpress(expCtx);
StatementContext ifStatCtx = ctx.statement(0);
Statement ifStat = this.parseStatment(ifStatCtx);
StatementContext elseStatCtx = ctx.statement(1);
Statement elseStat = null;
if (elseStatCtx != null) {
elseStat = this.parseStatment(elseStatCtx);
}
return new IfStatement(exp, ifStat, elseStat, this.getBTToken(ctx.If().getSymbol()));
}
Aggregations