Search in sources :

Example 1 with VarSquareAttribute

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

the class AntlrProgramBuilder method parseVarRefInLeftExpression.

protected VarRef parseVarRefInLeftExpression(VarRefContext varRef) {
    Expression safeExp = null;
    Safe_outputContext soctx = varRef.safe_output();
    if (soctx != null) {
        throw new BeetlException(BeetlException.ERROR, "语法错,赋值表达式不能使用安全输出");
    }
    List<VarAttributeContext> list = varRef.varAttribute();
    VarAttribute[] vas = this.parseVarAttribute(list);
    // 变量属性,用来收集,暂时未用上
    if (vas.length > 0) {
        VarAttribute first = vas[0];
        if (!(first instanceof VarSquareAttribute || first instanceof VarVirtualAttribute)) {
            pbCtx.setVarAttr(varRef.Identifier().getText(), first.token.text);
        }
    }
    VarRef var = new VarRef(vas, false, null, this.getBTToken(varRef.getText(), varRef.Identifier().getSymbol().getLine()), this.getBTToken(varRef.Identifier().getSymbol()));
    pbCtx.setVarPosition(varRef.Identifier().getText(), var);
    return var;
}
Also used : VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) VarRef(org.beetl.core.statement.VarRef) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) Safe_outputContext(org.beetl.core.parser.BeetlParser.Safe_outputContext) 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)

Example 2 with VarSquareAttribute

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

the class AntlrProgramBuilder method parseVarRefExpression.

protected Expression parseVarRefExpression(VarRefContext varRef) {
    Expression safeExp = null;
    Safe_outputContext soctx = varRef.safe_output();
    boolean hasSafe = false;
    if (soctx != null) {
        safeExp = this.parseSafeOutput(soctx);
        hasSafe = true;
    }
    if (this.pbCtx.isSafeOutput) {
        hasSafe = true;
    }
    List<VarAttributeContext> list = varRef.varAttribute();
    VarAttribute[] vas = this.parseVarAttribute(list);
    if (vas.length > 0) {
        VarAttribute first = vas[0];
        if (!(first instanceof VarSquareAttribute || first instanceof VarVirtualAttribute)) {
            pbCtx.setVarAttr(varRef.Identifier().getText(), first.token.text);
        }
    }
    VarRef var = new VarRef(vas, hasSafe, safeExp, this.getBTToken(varRef.getText(), varRef.Identifier().getSymbol().getLine()), this.getBTToken(varRef.Identifier().getSymbol()));
    pbCtx.setVarPosition(varRef.Identifier().getText(), var);
    return var;
}
Also used : VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) VarRef(org.beetl.core.statement.VarRef) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) VarAttributeContext(org.beetl.core.parser.BeetlParser.VarAttributeContext) Safe_outputContext(org.beetl.core.parser.BeetlParser.Safe_outputContext) 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)

Example 3 with VarSquareAttribute

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

use of org.beetl.core.statement.VarSquareAttribute 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)4 VarAttribute (org.beetl.core.statement.VarAttribute)4 VarSquareAttribute (org.beetl.core.statement.VarSquareAttribute)4 VarVirtualAttribute (org.beetl.core.statement.VarVirtualAttribute)4 VarAttributeContext (org.beetl.core.parser.BeetlParser.VarAttributeContext)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 FormatExpression (org.beetl.core.statement.FormatExpression)3 FunctionExpression (org.beetl.core.statement.FunctionExpression)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 StatementExpression (org.beetl.core.statement.StatementExpression)3 TernaryExpression (org.beetl.core.statement.TernaryExpression)3