Search in sources :

Example 11 with Expression

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

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

the class AntlrProgramBuilder method parseSafeOutput.

protected Expression parseSafeOutput(Safe_outputContext soctx) {
    Expression safeExp = null;
    List list = soctx.children;
    if (list.size() == 1) {
        safeExp = null;
    } else {
        // just xxx!exp
        Safe_allow_expContext allowExp = (Safe_allow_expContext) list.get(1);
        if (allowExp.literal() != null) {
            safeExp = this.parseLiteralExpress(allowExp.literal());
        } else if (allowExp.nativeCall() != null) {
            safeExp = this.parseNativeCallExpression(allowExp.nativeCall());
        } else if (allowExp.functionCall() != null) {
            safeExp = this.parseFunExp(allowExp.functionCall());
        } else if (allowExp.expression() != null) {
            safeExp = this.parseExpress(allowExp.expression());
        } else if (allowExp.varRef() != null) {
            safeExp = this.parseVarRefExpression(allowExp.varRef());
        }
    }
    return safeExp;
}
Also used : Safe_allow_expContext(org.beetl.core.parser.BeetlParser.Safe_allow_expContext) 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) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 13 with Expression

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

the class AntlrProgramBuilder method parseMuldivmodExpression.

protected ArthExpression parseMuldivmodExpression(MuldivmodExpContext ctx) {
    Expression a = this.parseExpress(ctx.expression(0));
    Expression b = this.parseExpress(ctx.expression(1));
    TerminalNode tn = (TerminalNode) ctx.children.get(1);
    short mode = 0;
    if (ctx.MUlTIP() != null) {
        mode = ArthExpression.MUL;
    } else if (ctx.DIV() != null) {
        mode = ArthExpression.DIV;
    } else if (ctx.MOD() != null) {
        mode = ArthExpression.MOD;
    }
    return new ArthExpression(a, b, mode, this.getBTToken(tn.getSymbol()));
}
Also used : ArthExpression(org.beetl.core.statement.ArthExpression) 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) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 14 with Expression

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

the class VarAttributeNodeListener method onEvent.

@Override
public Object onEvent(Event e) {
    Stack stack = (Stack) e.getEventTaget();
    Object o = stack.peek();
    if (o instanceof VarRef) {
        VarRef ref = (VarRef) o;
        VarAttribute[] attrs = ref.attributes;
        for (int i = 0; i < attrs.length; i++) {
            GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
            VarAttribute attr = attrs[i];
            if (attr.getClass() == VarAttribute.class) {
                Type type = attr.type;
                String name = attr.token != null ? attr.token.text : null;
                // 换成速度较快的属性访问类
                try {
                    AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
                    attr.aa = aa;
                } catch (BeetlException ex) {
                    ex.pushToken(attr.token);
                    throw ex;
                }
            } else if (attr.getClass() == VarSquareAttribute.class) {
                Type type = attr.type;
                Class c = type.cls;
                if (Map.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getMapAA());
                } else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getListAA());
                } else if (c.isArray()) {
                    attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
                } else {
                    Expression exp = ((VarSquareAttribute) attr).exp;
                    if (exp instanceof Literal) {
                        Literal literal = (Literal) exp;
                        if (literal.obj instanceof String) {
                            try {
                                String attributeName = (String) literal.obj;
                                AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
                                ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
                            } catch (BeetlException ex) {
                                ex.pushToken(attr.token);
                                throw ex;
                            }
                        }
                    }
                }
            } else if (attr.getClass() == VarVirtualAttribute.class) {
                // 对虚拟属性~size做优化
                if (attr.token.text.equals("size")) {
                    // 优化
                    Class c = attr.type.cls;
                    if (Map.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (Collection.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (c.isArray()) {
                        ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
                    }
                }
            }
        }
    }
    return null;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) Set(java.util.Set) GroupTemplate(org.beetl.core.GroupTemplate) AttributeAccess(org.beetl.core.om.AttributeAccess) Stack(java.util.Stack) Type(org.beetl.core.statement.Type) Expression(org.beetl.core.statement.Expression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 15 with Expression

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

the class AntlrProgramBuilder method parseVarAttribute.

protected VarAttribute[] parseVarAttribute(List<VarAttributeContext> list) {
    List<VarAttribute> listVarAttr = new ArrayList<VarAttribute>();
    for (VarAttributeContext vac : list) {
        if (vac instanceof VarAttributeGeneralContext) {
            VarAttributeGeneralContext zf = (VarAttributeGeneralContext) vac;
            VarAttribute attr = new VarAttribute(this.getBTToken(zf.Identifier().getSymbol()));
            listVarAttr.add(attr);
            attr.setAA(ObjectAA.defaultObjectAA());
        } else if (vac instanceof VarAttributeArrayOrMapContext) {
            VarAttributeArrayOrMapContext zf = (VarAttributeArrayOrMapContext) vac;
            Expression exp = this.parseExpress(zf.expression());
            VarSquareAttribute attr = new VarSquareAttribute(exp, this.getBTToken("[]", exp.token.line));
            attr.setAA(ObjectAA.defaultObjectAA());
            listVarAttr.add(attr);
        } else if (vac instanceof VarAttributeVirtualContext) {
            VarAttributeVirtualContext zf = (VarAttributeVirtualContext) vac;
            VarVirtualAttribute attr = new VarVirtualAttribute(this.getBTToken(zf.Identifier().getSymbol()));
            listVarAttr.add(attr);
        }
    }
    return listVarAttr.toArray(new VarAttribute[0]);
}
Also used : VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) VarAttributeGeneralContext(org.beetl.core.parser.BeetlParser.VarAttributeGeneralContext) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) 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) VarAttribute(org.beetl.core.statement.VarAttribute) ArrayList(java.util.ArrayList) VarAttributeVirtualContext(org.beetl.core.parser.BeetlParser.VarAttributeVirtualContext) VarAttributeArrayOrMapContext(org.beetl.core.parser.BeetlParser.VarAttributeArrayOrMapContext)

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