Search in sources :

Example 1 with StatementContext

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;
    }
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarAssignStatementSeq(org.beetl.core.statement.VarAssignStatementSeq) BeetlException(org.beetl.core.exception.BeetlException) GrammarToken(org.beetl.core.statement.GrammarToken) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) ForUpdateContext(org.beetl.core.parser.BeetlParser.ForUpdateContext) ForInitContext(org.beetl.core.parser.BeetlParser.ForInitContext) VarDeclareListContext(org.beetl.core.parser.BeetlParser.VarDeclareListContext) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext) GeneralForControlContext(org.beetl.core.parser.BeetlParser.GeneralForControlContext) GeneralForControlContext(org.beetl.core.parser.BeetlParser.GeneralForControlContext) ForControlContext(org.beetl.core.parser.BeetlParser.ForControlContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) VarDefineNode(org.beetl.core.statement.VarDefineNode) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) ForStatement(org.beetl.core.statement.ForStatement) ForInControlContext(org.beetl.core.parser.BeetlParser.ForInControlContext) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 2 with StatementContext

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;
}
Also used : StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) WhileStatement(org.beetl.core.statement.WhileStatement) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext)

Example 3 with StatementContext

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;
}
Also used : ArrayList(java.util.ArrayList) BlockStatement(org.beetl.core.statement.BlockStatement) SwitchBlockStatementGroupContext(org.beetl.core.parser.BeetlParser.SwitchBlockStatementGroupContext) LinkedHashMap(java.util.LinkedHashMap) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext) SwitchStatement(org.beetl.core.statement.SwitchStatement) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) SwitchLabelContext(org.beetl.core.parser.BeetlParser.SwitchLabelContext)

Example 4 with StatementContext

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;
}
Also used : BlockStatement(org.beetl.core.statement.BlockStatement) LinkedList(java.util.LinkedList) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext) SelectStatement(org.beetl.core.statement.SelectStatement) G_switchStatmentContext(org.beetl.core.parser.BeetlParser.G_switchStatmentContext) G_defaultStatmentContext(org.beetl.core.parser.BeetlParser.G_defaultStatmentContext) G_caseStatmentContext(org.beetl.core.parser.BeetlParser.G_caseStatmentContext) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression)

Example 5 with StatementContext

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()));
}
Also used : IfStatement(org.beetl.core.statement.IfStatement) StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) ContinueStatement(org.beetl.core.statement.ContinueStatement) DirectiveStatement(org.beetl.core.statement.DirectiveStatement) WhileStatement(org.beetl.core.statement.WhileStatement) AjaxStatement(org.beetl.core.statement.AjaxStatement) BreakStatement(org.beetl.core.statement.BreakStatement) ReturnStatement(org.beetl.core.statement.ReturnStatement) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement) EndStatement(org.beetl.core.statement.EndStatement) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) GeneralForStatement(org.beetl.core.statement.GeneralForStatement) Statement(org.beetl.core.statement.Statement) BlockStatement(org.beetl.core.statement.BlockStatement) IfStatement(org.beetl.core.statement.IfStatement) ForStatement(org.beetl.core.statement.ForStatement) SwitchStatement(org.beetl.core.statement.SwitchStatement) TagStatement(org.beetl.core.statement.TagStatement) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) SelectStatement(org.beetl.core.statement.SelectStatement) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) StatementContext(org.beetl.core.parser.BeetlParser.StatementContext)

Aggregations

ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)5 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)5 StatementContext (org.beetl.core.parser.BeetlParser.StatementContext)5 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)5 AndExpression (org.beetl.core.statement.AndExpression)5 ArthExpression (org.beetl.core.statement.ArthExpression)5 BlockStatement (org.beetl.core.statement.BlockStatement)5 CompareExpression (org.beetl.core.statement.CompareExpression)5 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)5 Expression (org.beetl.core.statement.Expression)5 FormatExpression (org.beetl.core.statement.FormatExpression)5 FunctionExpression (org.beetl.core.statement.FunctionExpression)5 IncDecExpression (org.beetl.core.statement.IncDecExpression)5 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)5 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)5 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)5 NegExpression (org.beetl.core.statement.NegExpression)5 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)5 OrExpression (org.beetl.core.statement.OrExpression)5 StatementExpression (org.beetl.core.statement.StatementExpression)5