use of org.beetl.core.parser.BeetlParser.StatementContext 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.parser.BeetlParser.StatementContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseWhile.
protected WhileStatement parseWhile(WhileStContext wc) {
pbCtx.enterBlock();
// break,continue语句到此为止
pbCtx.current.canStopContinueBreakFlag = true;
ExpressionContext condtionCtx = wc.parExpression().expression();
StatementContext bodyCtx = wc.statement();
Expression condtion = this.parseExpress(condtionCtx);
Statement body = this.parseStatment(bodyCtx);
WhileStatement whileStat = new WhileStatement(condtion, body, this.getBTToken(wc.getStart()));
pbCtx.exitBlock();
return whileStat;
}
use of org.beetl.core.parser.BeetlParser.StatementContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseSwitch.
protected SwitchStatement parseSwitch(SiwchStContext sctx) {
// this.pbCtx.enterBlock();
// this.pbCtx.current.canStopContinueBreakFlag = true;
ExpressionContext ect = sctx.parExpression().expression();
Expression exp = this.parseExpress(ect);
List<SwitchBlockStatementGroupContext> list = sctx.switchBlock().switchBlockStatementGroup();
LinkedHashMap<Expression, BlockStatement> condtionsStatementsMap = new LinkedHashMap<Expression, BlockStatement>();
List<Expression> conditionList = new ArrayList<Expression>();
BlockStatement defaultBlock = null;
for (SwitchBlockStatementGroupContext group : list) {
List<SwitchLabelContext> labels = group.switchLabel();
List<StatementContext> stats = group.statement();
BlockStatement block = stats != null ? this.parseBlock(stats, group) : null;
for (SwitchLabelContext label : labels) {
Expression caseExp = this.parseExpress(label.expression());
if (caseExp == null) {
// default
defaultBlock = block;
break;
} else {
conditionList.add(caseExp);
condtionsStatementsMap.put(caseExp, block);
}
}
}
SwitchStatement switchStat = new SwitchStatement(exp, condtionsStatementsMap, defaultBlock, this.getBTToken(sctx.getStart()));
return switchStat;
}
use of org.beetl.core.parser.BeetlParser.StatementContext in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseSelect.
protected SelectStatement parseSelect(SelectStContext selectCtx) {
// this.pbCtx.enterBlock();
// this.pbCtx.current.canStopContinueBreakFlag = true;
G_switchStatmentContext ctx = selectCtx.g_switchStatment();
ExpressionContext exp = ctx.expression();
Expression base = exp != null ? this.parseExpress(exp) : null;
List<G_caseStatmentContext> caseCtxList = ctx.g_caseStatment();
List<Expression> condtionList = new LinkedList<Expression>();
List<BlockStatement> blockList = new LinkedList<BlockStatement>();
for (G_caseStatmentContext caseCtx : caseCtxList) {
List<ExpressionContext> expCtxList = caseCtx.expression();
List<StatementContext> statCtxList = caseCtx.statement();
BlockStatement block = this.parseBlock(statCtxList, caseCtx);
for (ExpressionContext expCtx : expCtxList) {
Expression condition = this.parseExpress(expCtx);
// select case 的条件是||的关系,只要任何一个条件满足,都可以执行block
condtionList.add(condition);
blockList.add(block);
}
}
BlockStatement defaultStatement = null;
G_defaultStatmentContext defaultCtx = ctx.g_defaultStatment();
if (defaultCtx != null) {
List<StatementContext> defaultCtxList = ctx.g_defaultStatment().statement();
defaultStatement = this.parseBlock(defaultCtxList, ctx);
}
SelectStatement select = new SelectStatement(base, condtionList.toArray(new Expression[0]), blockList.toArray(new BlockStatement[0]), defaultStatement, this.getBTToken(selectCtx.Select().getSymbol()));
return select;
}
use of org.beetl.core.parser.BeetlParser.StatementContext 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