Search in sources :

Example 16 with Expression

use of org.beetl.core.statement.Expression 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 17 with Expression

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

the class AntlrProgramBuilder method parseTextOutputSt.

protected Statement parseTextOutputSt(TextOutputStContext ctx) {
    TextStatmentContext tsc = ctx.textStatment();
    FormatExpression format = null;
    boolean isSafe = false;
    if (tsc.NOT() != null) {
        isSafe = true;
    }
    TextVarContext tvc = tsc.textVar();
    if (tvc.COMMA() != null) {
        String formatName = null;
        String pattern = null;
        String tokenName = null;
        int line = 0;
        TextformatContext tfc = tvc.textformat();
        TerminalNode node = tfc.StringLiteral();
        if (node != null) {
            tokenName = pattern = getStringValue(node.getText());
            line = node.getSymbol().getLine();
        }
        FunctionNsContext fnsc = tfc.functionNs();
        if (fnsc != null) {
            List<TerminalNode> listId = fnsc.Identifier();
            formatName = this.getID(listId);
            tokenName = formatName;
            line = listId.get(0).getSymbol().getLine();
        }
        format = new FormatExpression(formatName, pattern, org.beetl.core.statement.GrammarToken.createToken(tokenName, line));
    }
    Expression exp = this.parseExpress(tvc.expression());
    if (isSafe) {
        SafePlaceholderST placeholder = new SafePlaceholderST(exp, format, null);
        return placeholder;
    } else {
        PlaceholderST placeholder = new PlaceholderST(exp, format, null);
        return placeholder;
    }
}
Also used : SafePlaceholderST(org.beetl.core.statement.SafePlaceholderST) PlaceholderST(org.beetl.core.statement.PlaceholderST) SafePlaceholderST(org.beetl.core.statement.SafePlaceholderST) TextformatContext(org.beetl.core.parser.BeetlParser.TextformatContext) 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) TextVarContext(org.beetl.core.parser.BeetlParser.TextVarContext) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) FunctionNsContext(org.beetl.core.parser.BeetlParser.FunctionNsContext) FormatExpression(org.beetl.core.statement.FormatExpression) ConstantsTextStatmentContext(org.beetl.core.parser.BeetlParser.ConstantsTextStatmentContext) TextStatmentContext(org.beetl.core.parser.BeetlParser.TextStatmentContext)

Example 18 with Expression

use of org.beetl.core.statement.Expression 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 19 with Expression

use of org.beetl.core.statement.Expression 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 20 with Expression

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

the class AntlrProgramBuilder method parseNotExpression.

protected NotBooleanExpression parseNotExpression(NotExpContext ctx) {
    Expression exp = this.parseExpress(ctx.expression());
    NotBooleanExpression notExp = new NotBooleanExpression(exp, this.getBTToken(ctx.NOT().getSymbol()));
    return notExp;
}
Also used : NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) 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)

Aggregations

Expression (org.beetl.core.statement.Expression)24 AndExpression (org.beetl.core.statement.AndExpression)23 ArthExpression (org.beetl.core.statement.ArthExpression)23 CompareExpression (org.beetl.core.statement.CompareExpression)23 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)23 FormatExpression (org.beetl.core.statement.FormatExpression)23 FunctionExpression (org.beetl.core.statement.FunctionExpression)23 IncDecExpression (org.beetl.core.statement.IncDecExpression)23 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)23 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)23 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)23 NegExpression (org.beetl.core.statement.NegExpression)23 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)23 OrExpression (org.beetl.core.statement.OrExpression)23 StatementExpression (org.beetl.core.statement.StatementExpression)23 TernaryExpression (org.beetl.core.statement.TernaryExpression)23 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)10 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)10 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)10 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)7