/WCFWebApi/src/Microsoft.ApplicationServer.Http/Microsoft/ApplicationServer/Query/DynamicQueryable.cs
C# | 2292 lines | 2129 code | 130 blank | 33 comment | 470 complexity | f152c28c7f61f7927d0c126ac0352ad7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- // <copyright>
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
-
- namespace Microsoft.ApplicationServer.Query
- {
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using Microsoft.ApplicationServer.Http;
-
- internal static class DynamicQueryable
- {
- public static IQueryable Where(this IQueryable source, string predicate, QueryResolver queryResolver)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- if (predicate == null)
- throw new ArgumentNullException("predicate");
- LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, queryResolver);
- return source.Provider.CreateQuery(
- Expression.Call(
- typeof(Queryable), "Where",
- new Type[] { source.ElementType },
- source.Expression, Expression.Quote(lambda)));
- }
-
- public static IQueryable OrderBy(this IQueryable source, string ordering, QueryResolver queryResolver)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- if (ordering == null)
- throw new ArgumentNullException("ordering");
- ParameterExpression[] parameters = new ParameterExpression[] {
- Expression.Parameter(source.ElementType, "") };
- ExpressionParser parser = new ExpressionParser(parameters, ordering, queryResolver);
- IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
- Expression queryExpr = source.Expression;
- string methodAsc = "OrderBy";
- string methodDesc = "OrderByDescending";
- foreach (DynamicOrdering o in orderings)
- {
- queryExpr = Expression.Call(
- typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
- new Type[] { source.ElementType, o.Selector.Type },
- queryExpr, Expression.Quote(DynamicExpression.Lambda(o.Selector, parameters)));
- methodAsc = "ThenBy";
- methodDesc = "ThenByDescending";
- }
- return source.Provider.CreateQuery(queryExpr);
- }
-
- public static IQueryable Take(this IQueryable source, int count)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- return source.Provider.CreateQuery(
- Expression.Call(
- typeof(Queryable), "Take",
- new Type[] { source.ElementType },
- source.Expression, Expression.Constant(count)));
- }
-
- public static IQueryable Skip(this IQueryable source, int count)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- return source.Provider.CreateQuery(
- Expression.Call(
- typeof(Queryable), "Skip",
- new Type[] { source.ElementType },
- source.Expression, Expression.Constant(count)));
- }
- }
-
- internal static class DynamicExpression
- {
- static readonly Type[] funcTypes = new Type[] {
- typeof(Func<>),
- typeof(Func<,>),
- typeof(Func<,,>),
- typeof(Func<,,,>),
- typeof(Func<,,,,>)
- };
-
- public static LambdaExpression ParseLambda(Type itType, Type resultType, string expression, QueryResolver queryResolver)
- {
- return ParseLambda(new ParameterExpression[] { Expression.Parameter(itType, "") }, resultType, expression, queryResolver);
- }
-
- public static LambdaExpression ParseLambda(ParameterExpression[] parameters, Type resultType, string expression, QueryResolver queryResolver)
- {
- ExpressionParser parser = new ExpressionParser(parameters, expression, queryResolver);
- return Lambda(parser.Parse(resultType), parameters);
- }
-
- public static LambdaExpression Lambda(Expression body, params ParameterExpression[] parameters)
- {
- int paramCount = parameters == null ? 0 : parameters.Length;
- Type[] typeArgs = new Type[paramCount + 1];
- for (int i = 0; i < paramCount; i++)
- typeArgs[i] = parameters[i].Type;
- typeArgs[paramCount] = body.Type;
- return Expression.Lambda(GetFuncType(typeArgs), body, parameters);
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "Arguments are provided internally by the parser's ParserLambda methods.")]
- public static Type GetFuncType(params Type[] typeArgs)
- {
- if (typeArgs == null || typeArgs.Length < 1 || typeArgs.Length > 5)
- throw new ArgumentException();
- return funcTypes[typeArgs.Length - 1].MakeGenericType(typeArgs);
- }
- }
-
- internal class DynamicOrdering
- {
- public Expression Selector;
- public bool Ascending;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "Exception is intended to only be used by the dynamic parser.")]
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1064:ExceptionsShouldBePublic", Justification = "Exception is intended to only be used by the dynamic parser.")]
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable", Justification = "Exception is intended to only be used by the dynamic parser.")]
- internal class ParseException : Exception
- {
- public ParseException(string message, int position)
- : base(string.Format(CultureInfo.InvariantCulture, SR.ParseExceptionFormat(message, position)))
- {
- }
- }
-
- internal class ExpressionParser
- {
- struct Token
- {
- public TokenId id;
- public string text;
- public int pos;
- }
-
- enum TokenId
- {
- Unknown,
- End,
- Identifier,
- StringLiteral,
- IntegerLiteral,
- RealLiteral,
- Exclamation,
- Percent,
- Amphersand,
- OpenParen,
- CloseParen,
- Asterisk,
- Plus,
- Comma,
- Minus,
- Dot,
- Slash,
- Colon,
- LessThan,
- Equal,
- GreaterThan,
- Question,
- OpenBracket,
- CloseBracket,
- Bar,
- ExclamationEqual,
- DoubleAmphersand,
- LessThanEqual,
- LessGreater,
- DoubleEqual,
- GreaterThanEqual,
- DoubleBar
- }
-
- internal class MappedMemberInfo
- {
- public MappedMemberInfo(Type mappedType, string memberName, bool isStatic, bool isMethod)
- {
- MappedType = mappedType;
- MemberName = memberName;
- IsStatic = isStatic;
- IsMethod = isMethod;
- }
-
- public Type MappedType { get; private set; }
- public string MemberName { get; private set; }
- public bool IsStatic { get; private set; }
- public bool IsMethod { get; private set; }
- }
-
- interface ILogicalSignatures
- {
- void F(bool x, bool y);
- void F(bool? x, bool? y);
- }
-
- interface IArithmeticSignatures
- {
- void F(int x, int y);
- void F(uint x, uint y);
- void F(long x, long y);
- void F(ulong x, ulong y);
- void F(float x, float y);
- void F(double x, double y);
- void F(decimal x, decimal y);
- void F(int? x, int? y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(uint? x, uint? y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(long? x, long? y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(ulong? x, ulong? y);
- void F(float? x, float? y);
- void F(double? x, double? y);
- void F(decimal? x, decimal? y);
- }
-
- interface IRelationalSignatures : IArithmeticSignatures
- {
- void F(string x, string y);
- void F(char x, char y);
- void F(DateTime x, DateTime y);
- void F(TimeSpan x, TimeSpan y);
- void F(char? x, char? y);
- void F(DateTime? x, DateTime? y);
- void F(TimeSpan? x, TimeSpan? y);
- void F(DateTimeOffset x, DateTimeOffset y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(DateTimeOffset? x, DateTimeOffset? y);
- }
-
- interface IEqualitySignatures : IRelationalSignatures
- {
- void F(bool x, bool y);
- void F(bool? x, bool? y);
- void F(Guid x, Guid y);
- void F(Guid? x, Guid? y);
- }
-
- interface IAddSignatures : IArithmeticSignatures
- {
- void F(DateTime x, TimeSpan y);
- void F(TimeSpan x, TimeSpan y);
- void F(DateTime? x, TimeSpan? y);
- void F(TimeSpan? x, TimeSpan? y);
- void F(DateTimeOffset x, TimeSpan y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(DateTimeOffset? x, TimeSpan? y);
- }
-
- interface ISubtractSignatures : IAddSignatures
- {
- void F(DateTime x, DateTime y);
- void F(DateTime? x, DateTime? y);
- void F(DateTimeOffset x, DateTimeOffset y);
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA908:AvoidTypesThatRequireJitCompilationInPrecompiledAssemblies", Justification = "Legacy code.")]
- void F(DateTimeOffset? x, DateTimeOffset? y);
- }
-
- interface INegationSignatures
- {
- void F(int x);
- void F(long x);
- void F(float x);
- void F(double x);
- void F(decimal x);
- void F(int? x);
- void F(long? x);
- void F(float? x);
- void F(double? x);
- void F(decimal? x);
- }
-
- interface INotSignatures
- {
- void F(bool x);
- void F(bool? x);
- }
-
- interface IEnumerableSignatures
- {
- void Where(bool predicate);
- void Any();
- void Any(bool predicate);
- void All(bool predicate);
- void Count();
- void Count(bool predicate);
- void Min(object selector);
- void Max(object selector);
- void Sum(int selector);
- void Sum(int? selector);
- void Sum(long selector);
- void Sum(long? selector);
- void Sum(float selector);
- void Sum(float? selector);
- void Sum(double selector);
- void Sum(double? selector);
- void Sum(decimal selector);
- void Sum(decimal? selector);
- void Average(int selector);
- void Average(int? selector);
- void Average(long selector);
- void Average(long? selector);
- void Average(float selector);
- void Average(float? selector);
- void Average(double selector);
- void Average(double? selector);
- void Average(decimal selector);
- void Average(decimal? selector);
- }
-
- static readonly Type[] predefinedTypes = {
- typeof(Object),
- typeof(Boolean),
- typeof(Char),
- typeof(String),
- typeof(SByte),
- typeof(Byte),
- typeof(Int16),
- typeof(UInt16),
- typeof(Int32),
- typeof(UInt32),
- typeof(Int64),
- typeof(UInt64),
- typeof(Single),
- typeof(Double),
- typeof(Decimal),
- typeof(DateTime),
- typeof(DateTimeOffset),
- typeof(TimeSpan),
- typeof(Guid),
- typeof(Math),
- typeof(Convert),
- typeof(StringComparison),
- typeof(Uri)
- };
-
- static readonly Expression trueLiteral = Expression.Constant(true);
- static readonly Expression falseLiteral = Expression.Constant(false);
- static readonly Expression nullLiteral = Expression.Constant(null);
-
- const string keywordIt = "it";
- const string keywordIif = "iif";
-
- static Dictionary<string, object> keywords;
-
- Dictionary<string, object> symbols;
- Dictionary<Expression, string> literals;
- ParameterExpression it;
- string text;
- int textPos;
- int textLen;
- char ch;
- Token token;
- QueryResolver queryResolver;
-
- public ExpressionParser(ParameterExpression[] parameters, string expression, QueryResolver queryResolver)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- if (keywords == null)
- keywords = CreateKeywords();
- this.queryResolver = queryResolver;
- symbols = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
- literals = new Dictionary<Expression, string>();
- if (parameters != null)
- ProcessParameters(parameters);
- text = expression;
- textLen = text.Length;
- SetTextPos(0);
- NextToken();
- }
-
- void ProcessParameters(ParameterExpression[] parameters)
- {
- foreach (ParameterExpression pe in parameters)
- if (!String.IsNullOrEmpty(pe.Name))
- AddSymbol(pe.Name, pe);
- if (parameters.Length == 1 && String.IsNullOrEmpty(parameters[0].Name))
- it = parameters[0];
- }
-
- void AddSymbol(string name, object value)
- {
- if (symbols.ContainsKey(name))
- throw ParseError(SR.DuplicateIdentifier(name));
- symbols.Add(name, value);
- }
-
- public Expression Parse(Type resultType)
- {
- int exprPos = token.pos;
- Expression expr = ParseExpression();
- if (resultType != null)
- if ((expr = PromoteExpression(expr, resultType, true)) == null)
- throw ParseError(exprPos, SR.ExpressionTypeMismatch(GetTypeName(resultType)));
- ValidateToken(TokenId.End, SR.SyntaxError);
- return expr;
- }
-
- #pragma warning disable 0219
- public IEnumerable<DynamicOrdering> ParseOrdering()
- {
- List<DynamicOrdering> orderings = new List<DynamicOrdering>();
- while (true)
- {
- Expression expr = ParseExpression();
- bool ascending = true;
- if (TokenIdentifierIs("asc") || TokenIdentifierIs("ascending"))
- {
- NextToken();
- }
- else if (TokenIdentifierIs("desc") || TokenIdentifierIs("descending"))
- {
- NextToken();
- ascending = false;
- }
- orderings.Add(new DynamicOrdering
- {
- Selector = expr,
- Ascending = ascending
- });
- if (token.id != TokenId.Comma)
- break;
- NextToken();
- }
- ValidateToken(TokenId.End, SR.SyntaxError);
- return orderings;
- }
- #pragma warning restore 0219
-
- // ?: operator
- Expression ParseExpression()
- {
- int errorPos = token.pos;
- Expression expr = ParseLogicalOr();
- if (token.id == TokenId.Question)
- {
- NextToken();
- Expression expr1 = ParseExpression();
- ValidateToken(TokenId.Colon, SR.ColonExpected);
- NextToken();
- Expression expr2 = ParseExpression();
- expr = GenerateConditional(expr, expr1, expr2, errorPos);
- }
- return expr;
- }
-
- // ||, or operator
- Expression ParseLogicalOr()
- {
- Expression left = ParseLogicalAnd();
- while (token.id == TokenId.DoubleBar || TokenIdentifierIs("or"))
- {
- Token op = token;
- NextToken();
- Expression right = ParseLogicalAnd();
- CheckAndPromoteOperands(typeof(ILogicalSignatures), op.text, ref left, ref right, op.pos);
- left = Expression.OrElse(left, right);
- }
- return left;
- }
-
- // &&, and operator
- Expression ParseLogicalAnd()
- {
- Expression left = ParseComparison();
- while (token.id == TokenId.DoubleAmphersand || TokenIdentifierIs("and"))
- {
- Token op = token;
- NextToken();
- Expression right = ParseComparison();
- CheckAndPromoteOperands(typeof(ILogicalSignatures), op.text, ref left, ref right, op.pos);
- left = Expression.AndAlso(left, right);
- }
- return left;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Legacy code.")]
- // =, ==, !=, <>, >, >=, <, <= operators
- Expression ParseComparison()
- {
- Expression left = ParseAdditive();
- while (token.id == TokenId.Equal || token.id == TokenId.DoubleEqual ||
- token.id == TokenId.ExclamationEqual || token.id == TokenId.LessGreater ||
- token.id == TokenId.GreaterThan || token.id == TokenId.GreaterThanEqual ||
- token.id == TokenId.LessThan || token.id == TokenId.LessThanEqual)
- {
- Token op = token;
- NextToken();
- Expression right = ParseAdditive();
- bool isEquality = op.id == TokenId.Equal || op.id == TokenId.DoubleEqual ||
- op.id == TokenId.ExclamationEqual || op.id == TokenId.LessGreater;
- if (isEquality && !left.Type.IsValueType && !right.Type.IsValueType)
- {
- if (left.Type != right.Type)
- {
- if (left.Type.IsAssignableFrom(right.Type))
- {
- right = Expression.Convert(right, left.Type);
- }
- else if (right.Type.IsAssignableFrom(left.Type))
- {
- left = Expression.Convert(left, right.Type);
- }
- else
- {
- throw IncompatibleOperandsError(op.text, left, right, op.pos);
- }
- }
- }
- else if (IsEnumType(left.Type) || IsEnumType(right.Type))
- {
- // convert enum expressions to their underlying values for comparison
- left = ConvertEnumExpression(left, right);
- right = ConvertEnumExpression(right, left);
-
- CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures),
- op.text, ref left, ref right, op.pos);
- }
- else
- {
- CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures),
- op.text, ref left, ref right, op.pos);
- }
- switch (op.id)
- {
- case TokenId.Equal:
- case TokenId.DoubleEqual:
- left = GenerateEqual(left, right);
- break;
- case TokenId.ExclamationEqual:
- case TokenId.LessGreater:
- left = GenerateNotEqual(left, right);
- break;
- case TokenId.GreaterThan:
- left = GenerateGreaterThan(left, right);
- break;
- case TokenId.GreaterThanEqual:
- left = GenerateGreaterThanEqual(left, right);
- break;
- case TokenId.LessThan:
- left = GenerateLessThan(left, right);
- break;
- case TokenId.LessThanEqual:
- left = GenerateLessThanEqual(left, right);
- break;
- }
- }
- return left;
- }
-
- /// <summary>
- /// We perform comparisons against enums using the underlying type
- /// because a more complete set of comparisons can be performed.
- /// </summary>
- static Expression ConvertEnumExpression(Expression expr, Expression otherExpr)
- {
- if (!IsEnumType(expr.Type))
- {
- return expr;
- }
-
- Type underlyingType;
- if (IsNullableType(expr.Type) ||
- (otherExpr.NodeType == ExpressionType.Constant && ((ConstantExpression)otherExpr).Value == null))
- {
- // if the enum expression itself is nullable or is being compared against null
- // we use a nullable type
- underlyingType = typeof(Nullable<>).MakeGenericType(Enum.GetUnderlyingType(GetNonNullableType(expr.Type)));
- }
- else
- {
- underlyingType = Enum.GetUnderlyingType(expr.Type);
- }
-
- return Expression.Convert(expr, underlyingType);
- }
-
- // +, -, & operators
- Expression ParseAdditive()
- {
- Expression left = ParseMultiplicative();
- while (token.id == TokenId.Plus || token.id == TokenId.Minus ||
- token.id == TokenId.Amphersand)
- {
- Token op = token;
- NextToken();
- Expression right = ParseMultiplicative();
- switch (op.id)
- {
- case TokenId.Plus:
- if (left.Type == typeof(string) || right.Type == typeof(string))
- goto case TokenId.Amphersand;
- CheckAndPromoteOperands(typeof(IAddSignatures), op.text, ref left, ref right, op.pos);
- left = GenerateAdd(left, right);
- break;
- case TokenId.Minus:
- CheckAndPromoteOperands(typeof(ISubtractSignatures), op.text, ref left, ref right, op.pos);
- left = GenerateSubtract(left, right);
- break;
- case TokenId.Amphersand:
- left = GenerateStringConcat(left, right);
- break;
- }
- }
- return left;
- }
-
- // *, /, %, mod operators
- Expression ParseMultiplicative()
- {
- Expression left = ParseUnary();
- while (token.id == TokenId.Asterisk || token.id == TokenId.Slash ||
- token.id == TokenId.Percent || TokenIdentifierIs("mod"))
- {
- Token op = token;
- NextToken();
- Expression right = ParseUnary();
- CheckAndPromoteOperands(typeof(IArithmeticSignatures), op.text, ref left, ref right, op.pos);
- switch (op.id)
- {
- case TokenId.Asterisk:
- left = Expression.Multiply(left, right);
- break;
- case TokenId.Slash:
- left = Expression.Divide(left, right);
- break;
- case TokenId.Percent:
- case TokenId.Identifier:
- left = Expression.Modulo(left, right);
- break;
- }
- }
- return left;
- }
-
- // -, !, not unary operators
- Expression ParseUnary()
- {
- if (token.id == TokenId.Minus || token.id == TokenId.Exclamation ||
- TokenIdentifierIs("not"))
- {
- Token op = token;
- NextToken();
- if (op.id == TokenId.Minus && (token.id == TokenId.IntegerLiteral ||
- token.id == TokenId.RealLiteral))
- {
- token.text = "-" + token.text;
- token.pos = op.pos;
- return ParsePrimary();
- }
- Expression expr = ParseUnary();
- if (op.id == TokenId.Minus)
- {
- CheckAndPromoteOperand(typeof(INegationSignatures), op.text, ref expr, op.pos);
- expr = Expression.Negate(expr);
- }
- else
- {
- CheckAndPromoteOperand(typeof(INotSignatures), op.text, ref expr, op.pos);
- expr = Expression.Not(expr);
- }
- return expr;
- }
- return ParsePrimary();
- }
-
- Expression ParsePrimary()
- {
- Expression expr = ParsePrimaryStart();
- while (true)
- {
- if (token.id == TokenId.Dot)
- {
- NextToken();
- expr = ParseMemberAccess(null, expr);
- }
- else if (token.id == TokenId.OpenBracket)
- {
- expr = ParseElementAccess(expr);
- }
- else
- {
- break;
- }
- }
- return expr;
- }
-
- Expression ParsePrimaryStart()
- {
- switch (token.id)
- {
- case TokenId.Identifier:
- return ParseIdentifier();
- case TokenId.StringLiteral:
- return ParseStringLiteral();
- case TokenId.IntegerLiteral:
- return ParseIntegerLiteral();
- case TokenId.RealLiteral:
- return ParseRealLiteral();
- case TokenId.OpenParen:
- return ParseParenExpression();
- default:
- throw ParseError(SR.ExpressionExpected);
- }
- }
-
- Expression ParseStringLiteral()
- {
- ValidateToken(TokenId.StringLiteral);
- char quote = token.text[0];
- // Unwrap string (remove surrounding quotes) and unwrap backslashes.
- string s = token.text.Substring(1, token.text.Length - 2).Replace("\\\\", "\\");
-
- if (quote == '\'')
- {
- // Unwrap single quotes.
- s = s.Replace("\\\'", "\'");
- }
- else
- {
- // Unwrap double quotes.
- s = s.Replace("\\\"", "\"");
-
- // TODO : do we need this code anymore?
- }
-
- NextToken();
- return CreateLiteral(s, s);
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames", Justification = "Legacy code.")]
- Expression ParseIntegerLiteral()
- {
- ValidateToken(TokenId.IntegerLiteral);
- string text = token.text;
- if (text[0] != '-')
- {
- ulong value;
- if (!UInt64.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out value))
- throw ParseError(SR.InvalidIntegerLiteral(text));
- NextToken();
- if (value <= (ulong)Int32.MaxValue)
- return CreateLiteral((int)value, text);
- if (value <= (ulong)UInt32.MaxValue)
- return CreateLiteral((uint)value, text);
- if (value <= (ulong)Int64.MaxValue)
- return CreateLiteral((long)value, text);
- return CreateLiteral(value, text);
- }
- else
- {
- long value;
- if (!Int64.TryParse(text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out value))
- throw ParseError(SR.InvalidIntegerLiteral(text));
- NextToken();
- if (value >= Int32.MinValue && value <= Int32.MaxValue)
- return CreateLiteral((int)value, text);
- return CreateLiteral(value, text);
- }
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1500:VariableNamesShouldNotMatchFieldNames", Justification = "Legacy code.")]
- Expression ParseRealLiteral()
- {
- ValidateToken(TokenId.RealLiteral);
- string text = token.text;
- object value = null;
- char last = text[text.Length - 1];
- if (last == 'F' || last == 'f')
- {
- float f;
- if (Single.TryParse(text.Substring(0, text.Length - 1), NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out f))
- value = f;
- }
- else if (last == 'M' || last == 'm')
- {
- decimal m;
- if (Decimal.TryParse(text.Substring(0, text.Length - 1), NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out m))
- value = m;
- }
- else if (last == 'D' || last == 'd')
- {
- double d;
- if (Double.TryParse(text.Substring(0, text.Length - 1), NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d))
- value = d;
- }
- else
- {
- double d;
- if (Double.TryParse(text, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out d))
- value = d;
- }
- if (value == null)
- throw ParseError(SR.InvalidRealLiteral(text));
- NextToken();
- return CreateLiteral(value, text);
- }
-
- Expression CreateLiteral(object value, string valueAsString)
- {
- ConstantExpression expr = Expression.Constant(value);
- literals.Add(expr, valueAsString);
- return expr;
- }
-
- Expression ParseParenExpression()
- {
- ValidateToken(TokenId.OpenParen, SR.OpenParenExpected);
- NextToken();
- Expression e = ParseExpression();
- ValidateToken(TokenId.CloseParen, SR.CloseParenOrOperatorExpected);
- NextToken();
- return e;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Legacy code.")]
- Expression ParseIdentifier()
- {
- ValidateToken(TokenId.Identifier);
- object value;
- if (keywords.TryGetValue(token.text, out value))
- {
- if (value is Type)
- return ParseTypeAccess((Type)value);
- if (value == (object)keywordIt)
- return ParseIt();
- if (value == (object)keywordIif)
- return ParseIif();
- NextToken();
- return (Expression)value;
- }
-
- if (symbols.TryGetValue(token.text, out value))
- {
- Expression expr = value as Expression;
- if (expr == null)
- {
- expr = Expression.Constant(value);
- }
- NextToken();
- return expr;
- }
-
- // See if the token is a mapped function call
- MappedMemberInfo mappedFunction = MapFunction(token.text);
- if (mappedFunction != null)
- {
- return ParseMappedFunction(mappedFunction);
- }
-
- if (it != null)
- return ParseMemberAccess(null, it);
-
- throw ParseError(SR.UnknownIdentifier(token.text));
- }
-
- MappedMemberInfo MapFunction(string functionName)
- {
- MappedMemberInfo mappedMember = MapStringFunction(functionName);
- if (mappedMember != null)
- {
- return mappedMember;
- }
-
- mappedMember = MapDateFunction(functionName);
- if (mappedMember != null)
- {
- return mappedMember;
- }
-
- mappedMember = MapMathFunction(functionName);
- if (mappedMember != null)
- {
- return mappedMember;
- }
-
- return null;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Legacy code.")]
- MappedMemberInfo MapStringFunction(string functionName)
- {
- if (functionName == "startswith")
- {
- return new MappedMemberInfo(typeof(string), "StartsWith", false, true);
- }
- else if (functionName == "endswith")
- {
- return new MappedMemberInfo(typeof(string), "EndsWith", false, true);
- }
- else if (functionName == "length")
- {
- return new MappedMemberInfo(typeof(string), "Length", false, false);
- }
- else if (functionName == "toupper")
- {
- return new MappedMemberInfo(typeof(string), "ToUpper", false, true);
- }
- else if (functionName == "tolower")
- {
- return new MappedMemberInfo(typeof(string), "ToLower", false, true);
- }
- else if (functionName == "substringof")
- {
- return new MappedMemberInfo(typeof(string), "Contains", false, true);
- }
- else if (functionName == "indexof")
- {
- return new MappedMemberInfo(typeof(string), "IndexOf", false, true);
- }
- else if (functionName == "replace")
- {
- return new MappedMemberInfo(typeof(string), "Replace", false, true);
- }
- else if (functionName == "substring")
- {
- return new MappedMemberInfo(typeof(string), "Substring", false, true);
- }
- else if (functionName == "trim")
- {
- return new MappedMemberInfo(typeof(string), "Trim", false, true);
- }
- else if (functionName == "concat")
- {
- return new MappedMemberInfo(typeof(string), "Concat", true, true);
- }
-
- return null;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Legacy code.")]
- MappedMemberInfo MapDateFunction(string functionName)
- {
- // date functions
- if (functionName == "day")
- {
- return new MappedMemberInfo(typeof(DateTime), "Day", false, false);
- }
- else if (functionName == "month")
- {
- return new MappedMemberInfo(typeof(DateTime), "Month", false, false);
- }
- else if (functionName == "year")
- {
- return new MappedMemberInfo(typeof(DateTime), "Year", false, false);
- }
- else if (functionName == "hour")
- {
- return new MappedMemberInfo(typeof(DateTime), "Hour", false, false);
- }
- else if (functionName == "minute")
- {
- return new MappedMemberInfo(typeof(DateTime), "Minute", false, false);
- }
- else if (functionName == "second")
- {
- return new MappedMemberInfo(typeof(DateTime), "Second", false, false);
- }
-
- return null;
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Legacy code.")]
- MappedMemberInfo MapMathFunction(string functionName)
- {
- if (functionName == "round")
- {
- return new MappedMemberInfo(typeof(Math), "Round", true, true);
- }
- if (functionName == "floor")
- {
- return new MappedMemberInfo(typeof(Math), "Floor", true, true);
- }
- if (functionName == "ceiling")
- {
- return new MappedMemberInfo(typeof(Math), "Ceiling", true, true);
- }
-
- return null;
- }
-
- Expression ParseIt()
- {
- if (it == null)
- throw ParseError(SR.NoItInScope);
- NextToken();
- return it;
- }
-
- Expression ParseIif()
- {
- int errorPos = token.pos;
- NextToken();
- Expression[] args = ParseArgumentList();
- if (args.Length != 3)
- throw ParseError(errorPos, SR.IifRequiresThreeArgs);
- return GenerateConditional(args[0], args[1], args[2], errorPos);
- }
-
- Expression GenerateConditional(Expression test, Expression expr1, Expression expr2, int errorPos)
- {
- if (test.Type != typeof(bool))
- throw ParseError(errorPos, SR.FirstExprMustBeBool);
- if (expr1.Type != expr2.Type)
- {
- Expression expr1as2 = expr2 != nullLiteral ? PromoteExpression(expr1, expr2.Type, true) : null;
- Expression expr2as1 = expr1 != nullLiteral ? PromoteExpression(expr2, expr1.Type, true) : null;
- if (expr1as2 != null && expr2as1 == null)
- {
- expr1 = expr1as2;
- }
- else if (expr2as1 != null && expr1as2 == null)
- {
- expr2 = expr2as1;
- }
- else
- {
- string type1 = expr1 != nullLiteral ? expr1.Type.Name : "null";
- string type2 = expr2 != nullLiteral ? expr2.Type.Name : "null";
- if (expr1as2 != null && expr2as1 != null)
- throw ParseError(errorPos, SR.BothTypesConvertToOther(type1, type2));
- throw ParseError(errorPos, SR.NeitherTypeConvertsToOther(type1, type2));
- }
- }
- return Expression.Condition(test, expr1, expr2);
- }
-
- Expression ParseTypeAccess(Type type)
- {
- int errorPos = token.pos;
- NextToken();
- if (token.id == TokenId.Question)
- {
- if (!type.IsValueType || IsNullableType(type))
- throw ParseError(errorPos, SR.TypeHasNoNullableForm(GetTypeName(type)));
- type = typeof(Nullable<>).MakeGenericType(type);
- NextToken();
- }
- if (token.id == TokenId.OpenParen)
- {
- Expression[] args = ParseArgumentList();
- MethodBase method;
- switch (FindBestMethod(type.GetConstructors(), args, out method))
- {
- case 0:
- if (args.Length == 1)
- return GenerateConversion(args[0], type, errorPos);
- throw ParseError(errorPos, SR.NoMatchingConstructor(GetTypeName(type)));
- case 1:
- return Expression.New((ConstructorInfo)method, args);
- default:
- throw ParseError(errorPos, SR.AmbiguousConstructorInvocation(GetTypeName(type)));
- }
- }
- ValidateToken(TokenId.Dot, SR.DotOrOpenParenExpected);
- NextToken();
- return ParseMemberAccess(type, null);
- }
-
- static Expression GenerateConversion(Expression expr, Type type, int errorPos)
- {
- Type exprType = expr.Type;
- if (exprType == type)
- return expr;
- if (exprType.IsValueType && type.IsValueType)
- {
- if ((IsNullableType(exprType) || IsNullableType(type)) &&
- GetNonNullableType(exprType) == GetNonNullableType(type))
- return Expression.Convert(expr, type);
- if ((IsNumericType(exprType) || IsEnumType(exprType)) &&
- (IsNumericType(type) || IsEnumType(type)))
- return Expression.ConvertChecked(expr, type);
- }
- if (exprType.IsAssignableFrom(type) || type.IsAssignableFrom(exprType) ||
- exprType.IsInterface || type.IsInterface)
- return Expression.Convert(expr, type);
- throw ParseError(errorPos, SR.CannotConvertValue(
- GetTypeName(exprType), GetTypeName(type)));
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Legacy code.")]
- Expression ParseMappedFunction(MappedMemberInfo mappedMember)
- {
- Type type = mappedMember.MappedType;
- string mappedMemberName = mappedMember.MemberName;
-
- int errorPos = token.pos;
- Expression[] args = null;
- Expression paramArg = null;
- Expression instance = null;
-
- NextToken();
- if (token.id == TokenId.OpenParen)
- {
- args = ParseArgumentList();
-
- // TODO : verify the first argument is the param
- // Really what we'll want to do here is allow either to be the param
- paramArg = args[0];
- instance = paramArg;
-
- // static methods need to include the target
- if (!mappedMember.IsStatic)
- {
- args = args.Skip(1).ToArray();
- }
- else
- {
- instance = null;
- }
- }
-
- if (mappedMember.IsMethod)
- {
- // a mapped function
- MethodBase mb;
- switch (FindMethod(type, mappedMemberName, mappedMember.IsStatic, args, out mb))
- {
- case 0:
- throw ParseError(errorPos, SR.NoApplicableMethod(
- mappedMemberName, GetTypeName(type)));
- case 1:
- MethodInfo method = (MethodInfo)mb;
- if (!IsPredefinedType(method.DeclaringType))
- throw ParseError(errorPos, SR.MethodsAreInaccessible(GetTypeName(method.DeclaringType)));
- if (method.ReturnType == typeof(void))
- throw ParseError(errorPos, SR.MethodIsVoid(
- mappedMemberName, GetTypeName(method.DeclaringType)));
- return Expression.Call(instance, (MethodInfo)method, args);
- default:
- throw ParseError(errorPos, SR.AmbiguousMethodInvocation(
- mappedMemberName, GetTypeName(type)));
- }
- }
- else
- {
- // a mapped Property/Field
- MemberInfo member = FindPropertyOrField(type, mappedMemberName, mappedMember.IsStatic);
- if (member == null)
- {
- if (this.queryResolver != null)
- {
- MemberExpression mex = queryResolver.ResolveMember(type, mappedMemberName, instance);
- if (mex != null)
- {
- return mex;
- }
- }
- throw ParseError(errorPos, SR.UnknownPropertyOrField(
- mappedMemberName, GetTypeName(type)));
- }
-
- return member is PropertyInfo ?
- Expression.Property(instance, (PropertyInfo)member) :
- Expression.Field(instance, (FieldInfo)member);
- }
- }
-
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "Legacy code.")]
- Expression ParseMemberAccess(Type type, Expression instance)
- {
- if (instance != null)
- type = instance.Type;
- int errorPos = token.pos;
- string id = GetIdentifier();
- NextToken();
- if (token.id == TokenId.OpenParen)
- {
- if (instance != null && type != typeof(string))
- {
- Type enumerableType = FindGenericType(typeof(IEnumerable<>), type);
- if (enumerableType != null)
- {
- Type elementType = enumerableType.GetGenericArguments()[0];
- return ParseAggregate(instance, elementType, id, errorPos);
- }
- }
- Expression[] args = ParseArgumentList();
- MethodBase mb;
- switch (FindMethod(type, id, instance == null, args, out mb))
- {
- case 0:
- throw ParseError(errorPos, SR.NoApplicableMethod(
- id, GetTypeName(type)));
- case 1:
- MethodInfo method = (MethodInfo)mb;
- if (!IsPredefinedType(method.DeclaringType))
- throw ParseError(errorPos, SR.MethodsAreInaccessible(GetTypeName(method.DeclaringType)));
- if (method.ReturnType == typeof(void))
- throw ParseError(errorPos, SR.MethodIsVoid(
- id, GetTypeName(method.DeclaringType)));
- return Expression.Call(instance, (MethodInfo)method, args);
- default:
- throw ParseError(errorPos, SR.AmbiguousMethodInvocation(
- id, GetTypeName(type)));
- }
- }
- else
- {
- MemberInfo member = FindPropertyOrField(type, id, instance == null);
- if (member == null)
- {
- if (this.queryResolver != null)
- {
- MemberExpression mex = queryResolver.ResolveMember(type, id, instance);
- if (mex != null)
- {
- return mex;
- }
- }
- throw ParseError(errorPos, SR.UnknownPropertyOrField(
- id, GetTypeName(type)));
- }
- return member is PropertyInfo ?
- Expression.Property(instance, (PropertyInfo)member) :
- Expression.Field(instance, (FieldInfo)member);
- }
- }
-
- static Type FindGenericType(Type generic, Type type)
- {
- while (type != null && type != typeof(object))
- {
- if (type.IsGenericType && type.GetGenericTypeDefinition() == generic)
- return typ…
Large files files are truncated, but you can click here to view the full file