Search in sources :

Example 1 with BlockStatement

use of org.beetl.core.statement.BlockStatement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseBlock.

protected BlockStatement parseBlock(List list, ParserRuleContext ctx) {
    pbCtx.enterBlock();
    ASTNode[] statements = new ASTNode[list.size()];
    List<Statement> nodes = new ArrayList<Statement>();
    boolean hasGoto = false;
    for (int i = 0; i < statements.length; i++) {
        nodes.add(parseStatment((ParserRuleContext) list.get(i)));
    }
    BlockStatement block = new BlockStatement(nodes.toArray(new Statement[0]), this.getBTToken(ctx.getStart()));
    this.checkGoto(block);
    pbCtx.exitBlock();
    return block;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) 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) StaticTextByteASTNode(org.beetl.core.statement.StaticTextByteASTNode) ASTNode(org.beetl.core.statement.ASTNode) StaticTextASTNode(org.beetl.core.statement.StaticTextASTNode) ArrayList(java.util.ArrayList) BlockStatement(org.beetl.core.statement.BlockStatement)

Example 2 with BlockStatement

use of org.beetl.core.statement.BlockStatement 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;
}
Also used : BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) VarDefineNode(org.beetl.core.statement.VarDefineNode) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) BlockStatement(org.beetl.core.statement.BlockStatement) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 3 with BlockStatement

use of org.beetl.core.statement.BlockStatement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseAssign.

/**
 * 定义变量
 * @param amc
 * @return
 */
protected VarAssignStatement parseAssign(AssignMentContext amc) {
    VarAssignStatement vas = null;
    if (amc instanceof AssignGeneralInStContext) {
        AssignGeneralInStContext agc = (AssignGeneralInStContext) amc;
        ExpressionContext expCtx = agc.generalAssignExp().expression();
        Expression exp = parseExpress(expCtx);
        VarRefContext varRefCtx = agc.generalAssignExp().varRef();
        if (varRefCtx.children.size() == 1) {
            // var a=1;
            Token token = varRefCtx.Identifier().getSymbol();
            vas = new VarAssignStatement(exp, getBTToken(token));
        } else {
            // var a.b=1 since 2.7.0
            VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
            vas = new VarRefAssignStatement(exp, ref);
        }
        return vas;
    } else if (amc instanceof AssignIdContext) {
        AssignIdContext idCtx = (AssignIdContext) amc;
        vas = new VarAssignStatement(Literal.NULLLiteral, getBTToken(idCtx.Identifier().getSymbol()));
        return vas;
    } else if (amc instanceof AssignTemplateVarContext) {
        AssignTemplateVarContext templateVarCtx = (AssignTemplateVarContext) amc;
        BlockContext blockCtx = templateVarCtx.block();
        BlockStatement block = this.parseBlock(blockCtx.statement(), blockCtx);
        ContentBodyExpression bodyExp = new ContentBodyExpression(block, getBTToken(templateVarCtx.Identifier().getSymbol()));
        vas = new VarAssignStatement(bodyExp, getBTToken(templateVarCtx.Identifier().getSymbol()));
    } else {
        throw new RuntimeException("不支持 在 " + amc.start.getLine());
    }
    return vas;
}
Also used : VarRef(org.beetl.core.statement.VarRef) AssignGeneralInStContext(org.beetl.core.parser.BeetlParser.AssignGeneralInStContext) VarAssignStatement(org.beetl.core.statement.VarAssignStatement) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) VarRefAssignStatement(org.beetl.core.statement.VarRefAssignStatement) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) BlockStatement(org.beetl.core.statement.BlockStatement) AssignIdContext(org.beetl.core.parser.BeetlParser.AssignIdContext) 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) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) VarRefContext(org.beetl.core.parser.BeetlParser.VarRefContext) AssignTemplateVarContext(org.beetl.core.parser.BeetlParser.AssignTemplateVarContext)

Example 4 with BlockStatement

use of org.beetl.core.statement.BlockStatement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseAjax.

protected AjaxStatement parseAjax(AjaxStContext ajaxCtx) {
    GrammarToken token = null;
    String flag = "render";
    List<TerminalNode> nodes = ajaxCtx.Identifier();
    if (nodes.size() == 1) {
        token = this.getBTToken(nodes.get(0).getSymbol());
    } else {
        token = this.getBTToken(nodes.get(1).getSymbol());
        flag = nodes.get(0).getSymbol().getText();
        if (!(flag.equals("render") || flag.equals("norender"))) {
            BeetlException be = new BeetlException(BeetlException.AJAX_PROPERTY_ERROR, "expect render or norender ,but " + flag);
            be.pushToken(token);
            throw be;
        }
    }
    BlockContext blockCtx = ajaxCtx.block();
    BlockStatement block = (BlockStatement) this.parseBlock(blockCtx.statement(), blockCtx);
    AjaxStatement ajaxStat = new AjaxStatement(block, token, flag.equals("render"));
    if (this.data.ajaxs == null) {
        this.data.ajaxs = new HashMap<String, AjaxStatement>();
    }
    String anchor = ajaxStat.token.text;
    if (this.data.ajaxs.containsKey(anchor)) {
        GrammarToken lastToken = this.data.ajaxs.get(anchor).token;
        BeetlException ex = new BeetlException(BeetlException.AJAX_ALREADY_DEFINED, "已经在第" + lastToken.line + "行定义");
        ex.pushToken(token);
        throw ex;
    }
    this.data.ajaxs.put(anchor, ajaxStat);
    return ajaxStat;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) BlockStatement(org.beetl.core.statement.BlockStatement) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) GrammarToken(org.beetl.core.statement.GrammarToken) AjaxStatement(org.beetl.core.statement.AjaxStatement)

Example 5 with BlockStatement

use of org.beetl.core.statement.BlockStatement 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)

Aggregations

BlockStatement (org.beetl.core.statement.BlockStatement)6 BlockContext (org.beetl.core.parser.BeetlParser.BlockContext)3 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)3 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)3 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)3 AndExpression (org.beetl.core.statement.AndExpression)3 ArthExpression (org.beetl.core.statement.ArthExpression)3 CompareExpression (org.beetl.core.statement.CompareExpression)3 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)3 Expression (org.beetl.core.statement.Expression)3 FormatExpression (org.beetl.core.statement.FormatExpression)3 FunctionExpression (org.beetl.core.statement.FunctionExpression)3 GrammarToken (org.beetl.core.statement.GrammarToken)3 IncDecExpression (org.beetl.core.statement.IncDecExpression)3 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)3 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)3 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)3 NegExpression (org.beetl.core.statement.NegExpression)3 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)3 OrExpression (org.beetl.core.statement.OrExpression)3