예제 #1
0
        public override Expression DeductInternal(object context)
        {
            var         map = (context as SearchParserContext).Properties;
            var         lhs = LeftChild.DeductInternal(context);
            var         rhs = RightChild.DeductInternal(context);
            PropertyMap prs = null;

            if (LeftChild.Value == null)
            {
                prs = map["Root"];
                var prop = prs.BestMatch(RightChild.Value.ToString());
                if (prop != null)
                {
                    DeductedType = prop.TargetType;
                    return(Expression.Parameter(prop.TargetType, "t"));
                }
            }
            else
            {
                prs = map[LeftChild.DeductedType.Name];
            }
            //存在对应的属性列表
            if (prs != null)
            {
                var prop = prs.BestMatch(RightChild.Value.ToString());
                if (prop != null)
                {
                    DeductedType = prop.TargetType;
                    return(Expression.Property(lhs, RightChild.Value.ToString()));
                }
            }
            //只有当前的域没有高级限定符时,才进行模糊查找
            if (LeftChild == null)
            {
                // 如果不存在,应从最近的有左子树的父结点开始,找他的最左子树的是PropertyAstNode结点,
                // 判断其DeductedType只否有属性列表
                var         pNode = this.Parent;
                AstNodeBase last  = this;
                while (pNode != null)
                {
                    //last是右子树说明pNode有已经推断的左孩子可能是Property属性
                    // 如果 是左子树,则其右子树尚未推断,不符合我们的语法要求,不去访问
                    if (last == pNode.RightChild)
                    {
                        var lch = pNode.LeftChild;

                        //一直判断其左孩子如果不为空(非叶子结点),并且其类型是PropertyAstNode
                        //那么尝试获取其属性列表,一旦不为空,匹配是否有合适的属性,若有返回

                        while (lch.LeftChild != null)
                        {
                            if (lch is PropertyAstNode)
                            {
                                prs = map[lch.DeductedType.Name];
                                if (prs != null)
                                {
                                    var prop = prs.BestMatch(RightChild.Value.ToString());
                                    if (prop != null)
                                    {
                                        DeductedType = prop.TargetType;
                                        return(Expression.Property(lhs, RightChild.Value.ToString()));
                                    }
                                    //未找到 可能是更高层级的域
                                }
                            }
                            lch = lch.LeftChild;
                        }
                    }

                    last  = pNode;
                    pNode = pNode.Parent;
                }
            }

            //遍历所有的结点均未找到合适的属性返回空
            Value = null;
            return(Expression.Constant(null));
        }