Search in sources :

Example 6 with VarRef

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

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

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

Aggregations

Expression (org.beetl.core.statement.Expression)8 VarRef (org.beetl.core.statement.VarRef)8 AndExpression (org.beetl.core.statement.AndExpression)7 ArthExpression (org.beetl.core.statement.ArthExpression)7 CompareExpression (org.beetl.core.statement.CompareExpression)7 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)7 FormatExpression (org.beetl.core.statement.FormatExpression)7 FunctionExpression (org.beetl.core.statement.FunctionExpression)7 IncDecExpression (org.beetl.core.statement.IncDecExpression)7 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)7 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)7 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)7 NegExpression (org.beetl.core.statement.NegExpression)7 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)7 OrExpression (org.beetl.core.statement.OrExpression)7 StatementExpression (org.beetl.core.statement.StatementExpression)7 TernaryExpression (org.beetl.core.statement.TernaryExpression)7 BeetlException (org.beetl.core.exception.BeetlException)5 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)4 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)4