Search in sources :

Example 6 with ExpressionContext

use of org.beetl.core.parser.BeetlParser.ExpressionContext 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 7 with ExpressionContext

use of org.beetl.core.parser.BeetlParser.ExpressionContext 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 8 with ExpressionContext

use of org.beetl.core.parser.BeetlParser.ExpressionContext 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;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) 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) ExpressionListContext(org.beetl.core.parser.BeetlParser.ExpressionListContext) TagStatement(org.beetl.core.statement.TagStatement) 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) Literal(org.beetl.core.statement.Literal) VarDefineNode(org.beetl.core.statement.VarDefineNode) TagVarBindingStatement(org.beetl.core.statement.TagVarBindingStatement)

Example 9 with ExpressionContext

use of org.beetl.core.parser.BeetlParser.ExpressionContext in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseAssingInExp.

/**
 *  赋值变量
 * @param agc
 * @return
 */
protected VarRefAssignExpress parseAssingInExp(AssignGeneralInExpContext agc) {
    VarRefAssignExpress vas = null;
    ExpressionContext expCtx = agc.generalAssignExp().expression();
    Expression exp = parseExpress(expCtx);
    VarRefContext varRefCtx = agc.generalAssignExp().varRef();
    VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
    vas = new VarRefAssignExpress(exp, ref);
    if (ref.attributes.length == 0) {
        // 变量定义:
        Token token = varRefCtx.Identifier().getSymbol();
        if (pbCtx.hasDefined(token.getText()) != null) {
            registerVar(vas);
            return vas;
        } else {
            BeetlException ex = new BeetlException(BeetlException.VAR_NOT_DEFINED);
            ex.pushToken(this.getBTToken(token));
            throw ex;
        }
    }
    return vas;
}
Also used : VarRef(org.beetl.core.statement.VarRef) BeetlException(org.beetl.core.exception.BeetlException) 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) VarRefContext(org.beetl.core.parser.BeetlParser.VarRefContext) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken) VarRefAssignExpress(org.beetl.core.statement.VarRefAssignExpress)

Example 10 with ExpressionContext

use of org.beetl.core.parser.BeetlParser.ExpressionContext 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)10 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)10 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)10 AndExpression (org.beetl.core.statement.AndExpression)10 ArthExpression (org.beetl.core.statement.ArthExpression)10 CompareExpression (org.beetl.core.statement.CompareExpression)10 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)10 Expression (org.beetl.core.statement.Expression)10 FormatExpression (org.beetl.core.statement.FormatExpression)10 FunctionExpression (org.beetl.core.statement.FunctionExpression)10 IncDecExpression (org.beetl.core.statement.IncDecExpression)10 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)10 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)10 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)10 NegExpression (org.beetl.core.statement.NegExpression)10 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)10 OrExpression (org.beetl.core.statement.OrExpression)10 StatementExpression (org.beetl.core.statement.StatementExpression)10 TernaryExpression (org.beetl.core.statement.TernaryExpression)10 BlockStatement (org.beetl.core.statement.BlockStatement)7