Beispiel #1
0
        IQueryable IQueryProvider.CreateQuery(Expression expression)
        {
            Type elementType = TypeCoercionUtility.GetElementType(expression.Type) ?? expression.Type;

            try
            {
                // TODO: replace with DynamicMethodGenerator.GetTypeFactory?
                return((IQueryable)Activator.CreateInstance(typeof(Query <>).MakeGenericType(elementType), new object[] { this, expression }));
            }
            catch (TargetInvocationException ex)
            {
                // unwrap inner exception
                throw ex.InnerException;
            }
        }
Beispiel #2
0
        private object ConsumeArray(IStream <Token <ModelTokenType> > tokens, Type arrayType)
        {
            Token <ModelTokenType> token = tokens.Pop();

            // verify correct starting state
            if (token.TokenType != ModelTokenType.ArrayBegin)
            {
                throw new TokenException <ModelTokenType>(
                          token,
                          String.Format(ModelAnalyzer.ErrorExpectedArray, token.TokenType));
            }

            Type itemType = TypeCoercionUtility.GetElementType(arrayType);

            // if itemType was specified by caller, then isn't just a hint
            bool isItemTypeHint = (itemType == null);

            IList array = new List <object>();

            while (!tokens.IsCompleted)
            {
                token = tokens.Peek();

                // consume the next item
                object item;
                switch (token.TokenType)
                {
                case ModelTokenType.ArrayBegin:
                {
                    // array item
                    item = this.ConsumeArray(tokens, isItemTypeHint ? null : itemType);
                    break;
                }

                case ModelTokenType.ArrayEnd:
                {
                    // end of the array loop
                    tokens.Pop();
                    return(this.Coercion.CoerceCollection(arrayType, itemType, array));
                }

                case ModelTokenType.ObjectBegin:
                {
                    // object item
                    item = this.ConsumeObject(tokens, isItemTypeHint ? null : itemType);
                    break;
                }

                case ModelTokenType.Primitive:
                {
                    // primitive item
                    if (!this.TryReadFilters(tokens, out item))
                    {
                        // TODO: evaluate how to ensure that filters didn't take anything
                        token = tokens.Pop();
                        item  = (token != null) ? token.Value : null;
                    }

                    if (!isItemTypeHint)
                    {
                        item = this.Coercion.CoerceType(itemType, item);
                    }
                    break;
                }

                default:
                {
                    // these are invalid here
                    tokens.Pop();
                    throw new TokenException <ModelTokenType>(
                              token,
                              String.Format(ModelAnalyzer.ErrorExpectedArrayItem, token.TokenType));
                }
                }

                // establish common type
                itemType = TypeCoercionUtility.FindCommonType(itemType, item);

                // add item to the array
                array.Add(item);
            }

            // end of input
            throw new TokenException <ModelTokenType>(
                      ModelGrammar.TokenNone,
                      ModelAnalyzer.ErrorUnterminatedArray);
        }