use of org.beetl.core.statement.VarDefineNode in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseForSt.
protected Statement parseForSt(ForStContext ctx) {
pbCtx.enterBlock();
// break,continue语句到此为止
pbCtx.current.canStopContinueBreakFlag = true;
StatementContext forContext = ctx.statement(0);
StatementContext elseContext = null;
ForControlContext forTypeCtx = ctx.forControl();
if (forTypeCtx.forInControl() != null) {
// for(a in list)...
ForInControlContext forCtx = forTypeCtx.forInControl();
VarDefineNode forVar = new VarDefineNode(this.getBTToken(forCtx.Identifier().getSymbol()));
if (pbCtx.hasDefined(forVar.token.text) != null) {
GrammarToken token = pbCtx.hasDefined(forVar.token.text);
BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "已经在第" + token.line + "行定义");
ex.pushToken(forVar.token);
throw ex;
}
VarDefineNode loopStatusVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier().getSymbol().getText() + "LP", forCtx.Identifier().getSymbol().getLine(), 0));
if (pbCtx.hasDefined(loopStatusVar.token.text) != null) {
GrammarToken token = pbCtx.hasDefined(loopStatusVar.token.text);
BeetlException ex = new BeetlException(BeetlException.VAR_ALREADY_DEFINED, "For循环隐含变量,已经在第" + token.line + "行定义");
ex.pushToken(loopStatusVar.token);
throw ex;
}
pbCtx.addVarAndPostion(forVar);
pbCtx.addVarAndPostion(loopStatusVar);
// //beetl.2 兼容
// VarDefineNode indexVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
// .getSymbol().getText()
// + "_index", forCtx.Identifier().getSymbol().getLine(), 0));
//
// VarDefineNode sizeVar = new VarDefineNode(new org.beetl.core.statement.GrammarToken(forCtx.Identifier()
// .getSymbol().getText()
// + "_size", forCtx.Identifier().getSymbol().getLine(), 0));
//
// pbCtx.addVarAndPostion(indexVar);
//
// pbCtx.addVarAndPostion(sizeVar);
Expression exp = this.parseExpress(forCtx.expression());
Statement forPart = this.parseStatment(forContext);
// elsefor
Statement elseForPart = null;
if (ctx.Elsefor() != null) {
elseContext = ctx.statement(1);
elseForPart = this.parseStatment(elseContext);
}
boolean hasSafe = false;
if (exp instanceof VarRef) {
VarRef varRef = (VarRef) exp;
hasSafe = varRef.hasSafe;
}
if (pbCtx.isSafeOutput) {
hasSafe = true;
}
ForStatement forStatement = new ForStatement(forVar, exp, hasSafe, forPart, elseForPart, forVar.token);
this.checkGoto(forStatement);
pbCtx.exitBlock();
return forStatement;
} else {
GeneralForControlContext forCtx = forTypeCtx.generalForControl();
Expression[] initExp = null;
VarAssignStatementSeq varInitSeq = null;
Expression condtion = null;
Expression[] updateExp = null;
if (forCtx.forInit() != null) {
ForInitContext forInitCtx = forCtx.forInit();
if (forInitCtx.Var() == null) {
// for( a=1,b=3;
List<ExpressionContext> list = forInitCtx.expressionList().expression();
initExp = this.parseExpressionCtxList(list);
} else {
// for( var a=1,b=3;
VarDeclareListContext varDeclare = forInitCtx.varDeclareList();
varInitSeq = this.parseVarDeclareList(varDeclare);
}
}
if (forCtx.expression() != null) {
condtion = this.parseExpress(forCtx.expression());
}
if (forCtx.forUpdate() != null) {
ForUpdateContext updateCtx = forCtx.forUpdate();
List<ExpressionContext> list = updateCtx.expressionList().expression();
updateExp = this.parseExpressionCtxList(list);
}
Statement forPart = this.parseStatment(forContext);
// elsefor
Statement elseForPart = null;
if (ctx.Elsefor() != null) {
elseContext = ctx.statement(1);
elseForPart = this.parseStatment(elseContext);
}
String str = forTypeCtx.getText();
GeneralForStatement forStat = new GeneralForStatement(varInitSeq, initExp, condtion, updateExp, forPart, elseForPart, this.getBTToken(str, forTypeCtx.start.getLine()));
pbCtx.exitBlock();
return forStat;
}
}
use of org.beetl.core.statement.VarDefineNode in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseTryCatch.
protected TryCatchStatement parseTryCatch(TryStContext tryStCtx) {
BlockContext tryBlockCtx = tryStCtx.block(0);
BlockStatement tryPart = (BlockStatement) this.parseBlock(tryBlockCtx.statement(), tryBlockCtx);
BlockStatement catchPart = null;
VarDefineNode errorNode = null;
if (tryStCtx.Catch() != null) {
this.pbCtx.enterBlock();
if (tryStCtx.Identifier() != null) {
Token errorToken = tryStCtx.Identifier().getSymbol();
errorNode = new VarDefineNode(this.getBTToken(errorToken));
this.pbCtx.addVarAndPostion(errorNode);
}
BlockContext catchBlockCtx = tryStCtx.block(1);
catchPart = (BlockStatement) this.parseBlock(catchBlockCtx.statement(), catchBlockCtx);
this.pbCtx.exitBlock();
}
TryCatchStatement statement = new TryCatchStatement(tryPart, catchPart, errorNode, this.getBTToken(tryStCtx.Try().getSymbol()));
return statement;
}
use of org.beetl.core.statement.VarDefineNode 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;
}
}
Aggregations