private ParserItemExpression BuildExpressionPath(string expr)
        {
            ParserItemExpression top = null;

            char[] tokens = new char[] { '.', '[' };
            //First one must be a top level item name
            int index = expr.IndexOfAny(tokens);

            string subexpr;

            if (index > 0)
            {
                top     = new ParserItemTopExpression(expr.Substring(0, index));
                subexpr = expr.Substring(index); //leave the dot or indexor in place
            }
            else if (index == 0)
            {
                top     = new ParserItemTopExpression(CurrentDataContextName);
                subexpr = expr;
            }
            else
            {
                top     = new ParserItemTopExpression(expr);
                subexpr = null;
            }

            if (!string.IsNullOrEmpty(subexpr))
            {
                this.BuildInnerExpressionPath(top, subexpr, expr);
            }

            return(top);
        }
        private void BuildInnerExpressionPath(ParserItemExpression item, string expr, string fullExpression)
        {
            if (string.IsNullOrEmpty(expr))
            {
                return;
            }

            if (expr.StartsWith("."))
            {
                expr = expr.Substring(1);//remove the first .

                int    end = expr.IndexOfAny(new char[] { '.', '[' });
                string prop;
                if (end > 0)
                {
                    prop = expr.Substring(0, end);
                    expr = expr.Substring(end);
                }
                else
                {
                    prop = expr;
                    expr = null;
                }
                item.AppendExpression(new ParserItemPropertyExpression(prop));

                //continue on
                BuildInnerExpressionPath(item, expr, fullExpression);
            }
            else if (expr.StartsWith("["))
            {
                int end = expr.IndexOf(']');
                if (end < 0)
                {
                    throw new ArgumentOutOfRangeException("]", "No closing brace found for expression '" + fullExpression + "'");
                }
                string content = expr.Substring(1, end - 1);
                expr = expr.Substring(end + 1);
                if (content.StartsWith("\"") || content.StartsWith("'"))
                {
                    content = content.Substring(1, content.Length - 2);
                    ParserItemKeyedExpression keyed = new ParserItemKeyedExpression(content);
                    item.AppendExpression(keyed);
                    BuildInnerExpressionPath(item, expr, fullExpression);
                }
                else
                {
                    int index;
                    if (!int.TryParse(content, out index))
                    {
                        throw new ArgumentException("index", "Could not convert the expression '" + content + " into an integer for the binding path '" + fullExpression + "'");
                    }
                    ParserItemIndexorExpression idexor = new ParserItemIndexorExpression(index);
                    item.AppendExpression(idexor);
                    BuildInnerExpressionPath(item, expr, fullExpression);
                }
            }
        }
Exemplo n.º 3
0
 public void AppendExpression(ParserItemExpression expr)
 {
     if (null == this.Next)
     {
         this.Next = expr;
     }
     else
     {
         this.Next.AppendExpression(expr);
     }
 }