DynamicQueryable.cs 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. using System.Threading;
  7. using System.Linq;
  8. using System;
  9. namespace Ant.ORM
  10. {
  11. public static class DynamicQueryable
  12. {
  13. public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values)
  14. {
  15. return (IQueryable<T>)Where((IQueryable)source, predicate, values);
  16. }
  17. public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
  18. {
  19. if (source == null) throw new ArgumentNullException("source");
  20. if (predicate == null) throw new ArgumentNullException("predicate");
  21. LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
  22. return source.Provider.CreateQuery(
  23. Expression.Call(
  24. typeof(Queryable), "Where",
  25. new Type[] { source.ElementType },
  26. source.Expression, Expression.Quote(lambda)));
  27. }
  28. public static IQueryable Select(this IQueryable source, string selector, params object[] values)
  29. {
  30. if (source == null) throw new ArgumentNullException("source");
  31. if (selector == null) throw new ArgumentNullException("selector");
  32. LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
  33. return source.Provider.CreateQuery(
  34. Expression.Call(
  35. typeof(Queryable), "Select",
  36. new Type[] { source.ElementType, lambda.Body.Type },
  37. source.Expression, Expression.Quote(lambda)));
  38. }
  39. public static IQueryable<T> DynamicOrderBy<T>(this IQueryable<T> source, string ordering, params object[] values)
  40. {
  41. return (IQueryable<T>)DynamicOrderBy((IQueryable)source, ordering, values);
  42. }
  43. public static IQueryable DynamicOrderBy(this IQueryable source, string ordering, params object[] values)
  44. {
  45. if (source == null) throw new ArgumentNullException("source");
  46. if (ordering == null) throw new ArgumentNullException("ordering");
  47. ParameterExpression[] parameters = new ParameterExpression[] {
  48. Expression.Parameter(source.ElementType, "") };
  49. ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
  50. IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
  51. Expression queryExpr = source.Expression;
  52. string methodAsc = "OrderBy";
  53. string methodDesc = "OrderByDescending";
  54. foreach (DynamicOrdering o in orderings)
  55. {
  56. queryExpr = Expression.Call(
  57. typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
  58. new Type[] { source.ElementType, o.Selector.Type },
  59. queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
  60. methodAsc = "ThenBy";
  61. methodDesc = "ThenByDescending";
  62. }
  63. return source.Provider.CreateQuery(queryExpr);
  64. }
  65. public static IQueryable Take(this IQueryable source, int count)
  66. {
  67. if (source == null) throw new ArgumentNullException("source");
  68. return source.Provider.CreateQuery(
  69. Expression.Call(
  70. typeof(Queryable), "Take",
  71. new Type[] { source.ElementType },
  72. source.Expression, Expression.Constant(count)));
  73. }
  74. public static IQueryable Skip(this IQueryable source, int count)
  75. {
  76. if (source == null) throw new ArgumentNullException("source");
  77. return source.Provider.CreateQuery(
  78. Expression.Call(
  79. typeof(Queryable), "Skip",
  80. new Type[] { source.ElementType },
  81. source.Expression, Expression.Constant(count)));
  82. }
  83. public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values)
  84. {
  85. if (source == null) throw new ArgumentNullException("source");
  86. if (keySelector == null) throw new ArgumentNullException("keySelector");
  87. if (elementSelector == null) throw new ArgumentNullException("elementSelector");
  88. LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values);
  89. LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values);
  90. return source.Provider.CreateQuery(
  91. Expression.Call(
  92. typeof(Queryable), "GroupBy",
  93. new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
  94. source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));
  95. }
  96. public static bool Any(this IQueryable source)
  97. {
  98. if (source == null) throw new ArgumentNullException("source");
  99. return (bool)source.Provider.Execute(
  100. Expression.Call(
  101. typeof(Queryable), "Any",
  102. new Type[] { source.ElementType }, source.Expression));
  103. }
  104. public static int Count(this IQueryable source)
  105. {
  106. if (source == null) throw new ArgumentNullException("source");
  107. return (int)source.Provider.Execute(
  108. Expression.Call(
  109. typeof(Queryable), "Count",
  110. new Type[] { source.ElementType }, source.Expression));
  111. }
  112. }
  113. public abstract class DynamicClass
  114. {
  115. public override string ToString()
  116. {
  117. PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
  118. StringBuilder sb = new StringBuilder();
  119. sb.Append("{");
  120. for (int i = 0; i < props.Length; i++)
  121. {
  122. if (i > 0) sb.Append(", ");
  123. sb.Append(props[i].Name);
  124. sb.Append("=");
  125. sb.Append(props[i].GetValue(this, null));
  126. }
  127. sb.Append("}");
  128. return sb.ToString();
  129. }
  130. }
  131. public class DynamicProperty
  132. {
  133. string name;
  134. Type type;
  135. public DynamicProperty(string name, Type type)
  136. {
  137. if (name == null) throw new ArgumentNullException("name");
  138. if (type == null) throw new ArgumentNullException("type");
  139. this.name = name;
  140. this.type = type;
  141. }
  142. public string Name
  143. {
  144. get { return name; }
  145. }
  146. public Type Type
  147. {
  148. get { return type; }
  149. }
  150. }
  151. public static class DynamicExpression
  152. {
  153. public static Expression Parse(Type resultType, string expression, params object[] values)
  154. {
  155. ExpressionParser parser = new ExpressionParser(null, expression, values);
  156. return parser.Parse(resultType);
  157. }
  158. public static LambdaExpression ParseLambda(Type itType, Type resultType, string expression, params object[] values)
  159. {
  160. return ParseLambda(new ParameterExpression[] { Expression.Parameter(itType, "") }, resultType, expression, values);
  161. }
  162. public static LambdaExpression ParseLambda(ParameterExpression[] parameters, Type resultType, string expression, params object[] values)
  163. {
  164. ExpressionParser parser = new ExpressionParser(parameters, expression, values);
  165. return Expression.Lambda(parser.Parse(resultType), parameters);
  166. }
  167. public static Expression<Func<T, S>> ParseLambda<T, S>(string expression, params object[] values)
  168. {
  169. return (Expression<Func<T, S>>)ParseLambda(typeof(T), typeof(S), expression, values);
  170. }
  171. public static Type CreateClass(params DynamicProperty[] properties)
  172. {
  173. return ClassFactory.Instance.GetDynamicClass(properties);
  174. }
  175. public static Type CreateClass(IEnumerable<DynamicProperty> properties)
  176. {
  177. return ClassFactory.Instance.GetDynamicClass(properties);
  178. }
  179. }
  180. internal class DynamicOrdering
  181. {
  182. public Expression Selector;
  183. public bool Ascending;
  184. }
  185. internal class Signature : IEquatable<Signature>
  186. {
  187. public DynamicProperty[] properties;
  188. public int hashCode;
  189. public Signature(IEnumerable<DynamicProperty> properties)
  190. {
  191. this.properties = properties.ToArray();
  192. hashCode = 0;
  193. foreach (DynamicProperty p in properties)
  194. {
  195. hashCode ^= p.Name.GetHashCode() ^ p.Type.GetHashCode();
  196. }
  197. }
  198. public override int GetHashCode()
  199. {
  200. return hashCode;
  201. }
  202. public override bool Equals(object obj)
  203. {
  204. return obj is Signature ? Equals((Signature)obj) : false;
  205. }
  206. public bool Equals(Signature other)
  207. {
  208. if (properties.Length != other.properties.Length) return false;
  209. for (int i = 0; i < properties.Length; i++)
  210. {
  211. if (properties[i].Name != other.properties[i].Name ||
  212. properties[i].Type != other.properties[i].Type) return false;
  213. }
  214. return true;
  215. }
  216. }
  217. internal class ClassFactory
  218. {
  219. public static readonly ClassFactory Instance = new ClassFactory();
  220. static ClassFactory() { } // Trigger lazy initialization of static fields
  221. ModuleBuilder module;
  222. Dictionary<Signature, Type> classes;
  223. int classCount;
  224. ReaderWriterLock rwLock;
  225. private ClassFactory()
  226. {
  227. AssemblyName name = new AssemblyName("DynamicClasses");
  228. AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
  229. #if ENABLE_LINQ_PARTIAL_TRUST
  230. new ReflectionPermission(PermissionState.Unrestricted).Assert();
  231. #endif
  232. try
  233. {
  234. module = assembly.DefineDynamicModule("Module");
  235. }
  236. finally
  237. {
  238. #if ENABLE_LINQ_PARTIAL_TRUST
  239. PermissionSet.RevertAssert();
  240. #endif
  241. }
  242. classes = new Dictionary<Signature, Type>();
  243. rwLock = new ReaderWriterLock();
  244. }
  245. public Type GetDynamicClass(IEnumerable<DynamicProperty> properties)
  246. {
  247. rwLock.AcquireReaderLock(Timeout.Infinite);
  248. try
  249. {
  250. Signature signature = new Signature(properties);
  251. Type type;
  252. if (!classes.TryGetValue(signature, out type))
  253. {
  254. type = CreateDynamicClass(signature.properties);
  255. classes.Add(signature, type);
  256. }
  257. return type;
  258. }
  259. finally
  260. {
  261. rwLock.ReleaseReaderLock();
  262. }
  263. }
  264. Type CreateDynamicClass(DynamicProperty[] properties)
  265. {
  266. LockCookie cookie = rwLock.UpgradeToWriterLock(Timeout.Infinite);
  267. try
  268. {
  269. string typeName = "DynamicClass" + (classCount + 1);
  270. #if ENABLE_LINQ_PARTIAL_TRUST
  271. new ReflectionPermission(PermissionState.Unrestricted).Assert();
  272. #endif
  273. try
  274. {
  275. TypeBuilder tb = this.module.DefineType(typeName, TypeAttributes.Class |
  276. TypeAttributes.Public, typeof(DynamicClass));
  277. FieldInfo[] fields = GenerateProperties(tb, properties);
  278. GenerateEquals(tb, fields);
  279. GenerateGetHashCode(tb, fields);
  280. Type result = tb.CreateType();
  281. classCount++;
  282. return result;
  283. }
  284. finally
  285. {
  286. #if ENABLE_LINQ_PARTIAL_TRUST
  287. PermissionSet.RevertAssert();
  288. #endif
  289. }
  290. }
  291. finally
  292. {
  293. rwLock.DowngradeFromWriterLock(ref cookie);
  294. }
  295. }
  296. FieldInfo[] GenerateProperties(TypeBuilder tb, DynamicProperty[] properties)
  297. {
  298. FieldInfo[] fields = new FieldBuilder[properties.Length];
  299. for (int i = 0; i < properties.Length; i++)
  300. {
  301. DynamicProperty dp = properties[i];
  302. FieldBuilder fb = tb.DefineField("_" + dp.Name, dp.Type, FieldAttributes.Private);
  303. PropertyBuilder pb = tb.DefineProperty(dp.Name, PropertyAttributes.HasDefault, dp.Type, null);
  304. MethodBuilder mbGet = tb.DefineMethod("get_" + dp.Name,
  305. MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
  306. dp.Type, Type.EmptyTypes);
  307. ILGenerator genGet = mbGet.GetILGenerator();
  308. genGet.Emit(OpCodes.Ldarg_0);
  309. genGet.Emit(OpCodes.Ldfld, fb);
  310. genGet.Emit(OpCodes.Ret);
  311. MethodBuilder mbSet = tb.DefineMethod("set_" + dp.Name,
  312. MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
  313. null, new Type[] { dp.Type });
  314. ILGenerator genSet = mbSet.GetILGenerator();
  315. genSet.Emit(OpCodes.Ldarg_0);
  316. genSet.Emit(OpCodes.Ldarg_1);
  317. genSet.Emit(OpCodes.Stfld, fb);
  318. genSet.Emit(OpCodes.Ret);
  319. pb.SetGetMethod(mbGet);
  320. pb.SetSetMethod(mbSet);
  321. fields[i] = fb;
  322. }
  323. return fields;
  324. }
  325. void GenerateEquals(TypeBuilder tb, FieldInfo[] fields)
  326. {
  327. MethodBuilder mb = tb.DefineMethod("Equals",
  328. MethodAttributes.Public | MethodAttributes.ReuseSlot |
  329. MethodAttributes.Virtual | MethodAttributes.HideBySig,
  330. typeof(bool), new Type[] { typeof(object) });
  331. ILGenerator gen = mb.GetILGenerator();
  332. LocalBuilder other = gen.DeclareLocal(tb);
  333. Label next = gen.DefineLabel();
  334. gen.Emit(OpCodes.Ldarg_1);
  335. gen.Emit(OpCodes.Isinst, tb);
  336. gen.Emit(OpCodes.Stloc, other);
  337. gen.Emit(OpCodes.Ldloc, other);
  338. gen.Emit(OpCodes.Brtrue_S, next);
  339. gen.Emit(OpCodes.Ldc_I4_0);
  340. gen.Emit(OpCodes.Ret);
  341. gen.MarkLabel(next);
  342. foreach (FieldInfo field in fields)
  343. {
  344. Type ft = field.FieldType;
  345. Type ct = typeof(EqualityComparer<>).MakeGenericType(ft);
  346. next = gen.DefineLabel();
  347. gen.EmitCall(OpCodes.Call, ct.GetMethod("get_Default"), null);
  348. gen.Emit(OpCodes.Ldarg_0);
  349. gen.Emit(OpCodes.Ldfld, field);
  350. gen.Emit(OpCodes.Ldloc, other);
  351. gen.Emit(OpCodes.Ldfld, field);
  352. gen.EmitCall(OpCodes.Callvirt, ct.GetMethod("Equals", new Type[] { ft, ft }), null);
  353. gen.Emit(OpCodes.Brtrue_S, next);
  354. gen.Emit(OpCodes.Ldc_I4_0);
  355. gen.Emit(OpCodes.Ret);
  356. gen.MarkLabel(next);
  357. }
  358. gen.Emit(OpCodes.Ldc_I4_1);
  359. gen.Emit(OpCodes.Ret);
  360. }
  361. void GenerateGetHashCode(TypeBuilder tb, FieldInfo[] fields)
  362. {
  363. MethodBuilder mb = tb.DefineMethod("GetHashCode",
  364. MethodAttributes.Public | MethodAttributes.ReuseSlot |
  365. MethodAttributes.Virtual | MethodAttributes.HideBySig,
  366. typeof(int), Type.EmptyTypes);
  367. ILGenerator gen = mb.GetILGenerator();
  368. gen.Emit(OpCodes.Ldc_I4_0);
  369. foreach (FieldInfo field in fields)
  370. {
  371. Type ft = field.FieldType;
  372. Type ct = typeof(EqualityComparer<>).MakeGenericType(ft);
  373. gen.EmitCall(OpCodes.Call, ct.GetMethod("get_Default"), null);
  374. gen.Emit(OpCodes.Ldarg_0);
  375. gen.Emit(OpCodes.Ldfld, field);
  376. gen.EmitCall(OpCodes.Callvirt, ct.GetMethod("GetHashCode", new Type[] { ft }), null);
  377. gen.Emit(OpCodes.Xor);
  378. }
  379. gen.Emit(OpCodes.Ret);
  380. }
  381. }
  382. public sealed class ParseException : Exception
  383. {
  384. int position;
  385. public ParseException(string message, int position)
  386. : base(message)
  387. {
  388. this.position = position;
  389. }
  390. public int Position
  391. {
  392. get { return position; }
  393. }
  394. public override string ToString()
  395. {
  396. return string.Format(Res.ParseExceptionFormat, Message, position);
  397. }
  398. }
  399. internal class ExpressionParser
  400. {
  401. struct Token
  402. {
  403. public TokenId id;
  404. public string text;
  405. public int pos;
  406. }
  407. enum TokenId
  408. {
  409. Unknown,
  410. End,
  411. Identifier,
  412. StringLiteral,
  413. IntegerLiteral,
  414. RealLiteral,
  415. Exclamation,
  416. Percent,
  417. Amphersand,
  418. OpenParen,
  419. CloseParen,
  420. Asterisk,
  421. Plus,
  422. Comma,
  423. Minus,
  424. Dot,
  425. Slash,
  426. Colon,
  427. LessThan,
  428. Equal,
  429. GreaterThan,
  430. Question,
  431. OpenBracket,
  432. CloseBracket,
  433. Bar,
  434. ExclamationEqual,
  435. DoubleAmphersand,
  436. LessThanEqual,
  437. LessGreater,
  438. DoubleEqual,
  439. GreaterThanEqual,
  440. DoubleBar
  441. }
  442. interface ILogicalSignatures
  443. {
  444. void F(bool x, bool y);
  445. void F(bool? x, bool? y);
  446. }
  447. interface IArithmeticSignatures
  448. {
  449. void F(int x, int y);
  450. void F(uint x, uint y);
  451. void F(long x, long y);
  452. void F(ulong x, ulong y);
  453. void F(float x, float y);
  454. void F(double x, double y);
  455. void F(decimal x, decimal y);
  456. void F(int? x, int? y);
  457. void F(uint? x, uint? y);
  458. void F(long? x, long? y);
  459. void F(ulong? x, ulong? y);
  460. void F(float? x, float? y);
  461. void F(double? x, double? y);
  462. void F(decimal? x, decimal? y);
  463. }
  464. interface IRelationalSignatures : IArithmeticSignatures
  465. {
  466. void F(string x, string y);
  467. void F(char x, char y);
  468. void F(DateTime x, DateTime y);
  469. void F(TimeSpan x, TimeSpan y);
  470. void F(char? x, char? y);
  471. void F(DateTime? x, DateTime? y);
  472. void F(TimeSpan? x, TimeSpan? y);
  473. }
  474. interface IEqualitySignatures : IRelationalSignatures
  475. {
  476. void F(bool x, bool y);
  477. void F(bool? x, bool? y);
  478. }
  479. interface IAddSignatures : IArithmeticSignatures
  480. {
  481. void F(DateTime x, TimeSpan y);
  482. void F(TimeSpan x, TimeSpan y);
  483. void F(DateTime? x, TimeSpan? y);
  484. void F(TimeSpan? x, TimeSpan? y);
  485. }
  486. interface ISubtractSignatures : IAddSignatures
  487. {
  488. void F(DateTime x, DateTime y);
  489. void F(DateTime? x, DateTime? y);
  490. }
  491. interface INegationSignatures
  492. {
  493. void F(int x);
  494. void F(long x);
  495. void F(float x);
  496. void F(double x);
  497. void F(decimal x);
  498. void F(int? x);
  499. void F(long? x);
  500. void F(float? x);
  501. void F(double? x);
  502. void F(decimal? x);
  503. }
  504. interface INotSignatures
  505. {
  506. void F(bool x);
  507. void F(bool? x);
  508. }
  509. interface IEnumerableSignatures
  510. {
  511. void Where(bool predicate);
  512. void Any();
  513. void Any(bool predicate);
  514. void All(bool predicate);
  515. void Count();
  516. void Count(bool predicate);
  517. void Min(object selector);
  518. void Max(object selector);
  519. void Sum(int selector);
  520. void Sum(int? selector);
  521. void Sum(long selector);
  522. void Sum(long? selector);
  523. void Sum(float selector);
  524. void Sum(float? selector);
  525. void Sum(double selector);
  526. void Sum(double? selector);
  527. void Sum(decimal selector);
  528. void Sum(decimal? selector);
  529. void Average(int selector);
  530. void Average(int? selector);
  531. void Average(long selector);
  532. void Average(long? selector);
  533. void Average(float selector);
  534. void Average(float? selector);
  535. void Average(double selector);
  536. void Average(double? selector);
  537. void Average(decimal selector);
  538. void Average(decimal? selector);
  539. }
  540. static readonly Type[] predefinedTypes = {
  541. typeof(Object),
  542. typeof(Boolean),
  543. typeof(Char),
  544. typeof(String),
  545. typeof(SByte),
  546. typeof(Byte),
  547. typeof(Int16),
  548. typeof(UInt16),
  549. typeof(Int32),
  550. typeof(UInt32),
  551. typeof(Int64),
  552. typeof(UInt64),
  553. typeof(Single),
  554. typeof(Double),
  555. typeof(Decimal),
  556. typeof(DateTime),
  557. typeof(TimeSpan),
  558. typeof(Guid),
  559. typeof(Math),
  560. typeof(Convert)
  561. };
  562. static readonly Expression trueLiteral = Expression.Constant(true);
  563. static readonly Expression falseLiteral = Expression.Constant(false);
  564. static readonly Expression nullLiteral = Expression.Constant(null);
  565. static readonly string keywordIt = "it";
  566. static readonly string keywordIif = "iif";
  567. static readonly string keywordNew = "new";
  568. static Dictionary<string, object> keywords;
  569. Dictionary<string, object> symbols;
  570. IDictionary<string, object> externals;
  571. Dictionary<Expression, string> literals;
  572. ParameterExpression it;
  573. string text;
  574. int textPos;
  575. int textLen;
  576. char ch;
  577. Token token;
  578. public ExpressionParser(ParameterExpression[] parameters, string expression, object[] values)
  579. {
  580. if (expression == null) throw new ArgumentNullException("expression");
  581. if (keywords == null) keywords = CreateKeywords();
  582. symbols = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  583. literals = new Dictionary<Expression, string>();
  584. if (parameters != null) ProcessParameters(parameters);
  585. if (values != null) ProcessValues(values);
  586. text = expression;
  587. textLen = text.Length;
  588. SetTextPos(0);
  589. NextToken();
  590. }
  591. void ProcessParameters(ParameterExpression[] parameters)
  592. {
  593. foreach (ParameterExpression pe in parameters)
  594. if (!String.IsNullOrEmpty(pe.Name))
  595. AddSymbol(pe.Name, pe);
  596. if (parameters.Length == 1 && String.IsNullOrEmpty(parameters[0].Name))
  597. it = parameters[0];
  598. }
  599. void ProcessValues(object[] values)
  600. {
  601. for (int i = 0; i < values.Length; i++)
  602. {
  603. object value = values[i];
  604. if (i == values.Length - 1 && value is IDictionary<string, object>)
  605. {
  606. externals = (IDictionary<string, object>)value;
  607. }
  608. else
  609. {
  610. AddSymbol("@" + i.ToString(System.Globalization.CultureInfo.InvariantCulture), value);
  611. }
  612. }
  613. }
  614. void AddSymbol(string name, object value)
  615. {
  616. if (symbols.ContainsKey(name))
  617. throw ParseError(Res.DuplicateIdentifier, name);
  618. symbols.Add(name, value);
  619. }
  620. public Expression Parse(Type resultType)
  621. {
  622. int exprPos = token.pos;
  623. Expression expr = ParseExpression();
  624. if (resultType != null)
  625. if ((expr = PromoteExpression(expr, resultType, true)) == null)
  626. throw ParseError(exprPos, Res.ExpressionTypeMismatch, GetTypeName(resultType));
  627. ValidateToken(TokenId.End, Res.SyntaxError);
  628. return expr;
  629. }
  630. #pragma warning disable 0219
  631. public IEnumerable<DynamicOrdering> ParseOrdering()
  632. {
  633. List<DynamicOrdering> orderings = new List<DynamicOrdering>();
  634. while (true)
  635. {
  636. Expression expr = ParseExpression();
  637. bool ascending = true;
  638. if (TokenIdentifierIs("asc") || TokenIdentifierIs("ascending"))
  639. {
  640. NextToken();
  641. }
  642. else if (TokenIdentifierIs("desc") || TokenIdentifierIs("descending"))
  643. {
  644. NextToken();
  645. ascending = false;
  646. }
  647. orderings.Add(new DynamicOrdering { Selector = expr, Ascending = ascending });
  648. if (token.id != TokenId.Comma) break;
  649. NextToken();
  650. }
  651. ValidateToken(TokenId.End, Res.SyntaxError);
  652. return orderings;
  653. }
  654. #pragma warning restore 0219
  655. // ?: operator
  656. Expression ParseExpression()
  657. {
  658. int errorPos = token.pos;
  659. Expression expr = ParseLogicalOr();
  660. if (token.id == TokenId.Question)
  661. {
  662. NextToken();
  663. Expression expr1 = ParseExpression();
  664. ValidateToken(TokenId.Colon, Res.ColonExpected);
  665. NextToken();
  666. Expression expr2 = ParseExpression();
  667. expr = GenerateConditional(expr, expr1, expr2, errorPos);
  668. }
  669. return expr;
  670. }
  671. // ||, or operator
  672. Expression ParseLogicalOr()
  673. {
  674. Expression left = ParseLogicalAnd();
  675. while (token.id == TokenId.DoubleBar || TokenIdentifierIs("or"))
  676. {
  677. Token op = token;
  678. NextToken();
  679. Expression right = ParseLogicalAnd();
  680. CheckAndPromoteOperands(typeof(ILogicalSignatures), op.text, ref left, ref right, op.pos);
  681. left = Expression.OrElse(left, right);
  682. }
  683. return left;
  684. }
  685. // &&, and operator
  686. Expression ParseLogicalAnd()
  687. {
  688. Expression left = ParseComparison();
  689. while (token.id == TokenId.DoubleAmphersand || TokenIdentifierIs("and"))
  690. {
  691. Token op = token;
  692. NextToken();
  693. Expression right = ParseComparison();
  694. CheckAndPromoteOperands(typeof(ILogicalSignatures), op.text, ref left, ref right, op.pos);
  695. left = Expression.AndAlso(left, right);
  696. }
  697. return left;
  698. }
  699. // =, ==, !=, <>, >, >=, <, <= operators
  700. Expression ParseComparison()
  701. {
  702. Expression left = ParseAdditive();
  703. while (token.id == TokenId.Equal || token.id == TokenId.DoubleEqual ||
  704. token.id == TokenId.ExclamationEqual || token.id == TokenId.LessGreater ||
  705. token.id == TokenId.GreaterThan || token.id == TokenId.GreaterThanEqual ||
  706. token.id == TokenId.LessThan || token.id == TokenId.LessThanEqual)
  707. {
  708. Token op = token;
  709. NextToken();
  710. Expression right = ParseAdditive();
  711. bool isEquality = op.id == TokenId.Equal || op.id == TokenId.DoubleEqual ||
  712. op.id == TokenId.ExclamationEqual || op.id == TokenId.LessGreater;
  713. if (isEquality && !left.Type.IsValueType && !right.Type.IsValueType)
  714. {
  715. if (left.Type != right.Type)
  716. {
  717. if (left.Type.IsAssignableFrom(right.Type))
  718. {
  719. right = Expression.Convert(right, left.Type);
  720. }
  721. else if (right.Type.IsAssignableFrom(left.Type))
  722. {
  723. left = Expression.Convert(left, right.Type);
  724. }
  725. else
  726. {
  727. throw IncompatibleOperandsError(op.text, left, right, op.pos);
  728. }
  729. }
  730. }
  731. else if (IsEnumType(left.Type) || IsEnumType(right.Type))
  732. {
  733. if (left.Type != right.Type)
  734. {
  735. Expression e;
  736. if ((e = PromoteExpression(right, left.Type, true)) != null)
  737. {
  738. right = e;
  739. }
  740. else if ((e = PromoteExpression(left, right.Type, true)) != null)
  741. {
  742. left = e;
  743. }
  744. else
  745. {
  746. throw IncompatibleOperandsError(op.text, left, right, op.pos);
  747. }
  748. }
  749. }
  750. else
  751. {
  752. CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures),
  753. op.text, ref left, ref right, op.pos);
  754. }
  755. switch (op.id)
  756. {
  757. case TokenId.Equal:
  758. case TokenId.DoubleEqual:
  759. left = GenerateEqual(left, right);
  760. break;
  761. case TokenId.ExclamationEqual:
  762. case TokenId.LessGreater:
  763. left = GenerateNotEqual(left, right);
  764. break;
  765. case TokenId.GreaterThan:
  766. left = GenerateGreaterThan(left, right);
  767. break;
  768. case TokenId.GreaterThanEqual:
  769. left = GenerateGreaterThanEqual(left, right);
  770. break;
  771. case TokenId.LessThan:
  772. left = GenerateLessThan(left, right);
  773. break;
  774. case TokenId.LessThanEqual:
  775. left = GenerateLessThanEqual(left, right);
  776. break;
  777. }
  778. }
  779. return left;
  780. }
  781. // +, -, & operators
  782. Expression ParseAdditive()
  783. {
  784. Expression left = ParseMultiplicative();
  785. while (token.id == TokenId.Plus || token.id == TokenId.Minus ||
  786. token.id == TokenId.Amphersand)
  787. {
  788. Token op = token;
  789. NextToken();
  790. Expression right = ParseMultiplicative();
  791. switch (op.id)
  792. {
  793. case TokenId.Plus:
  794. if (left.Type == typeof(string) || right.Type == typeof(string))
  795. goto case TokenId.Amphersand;
  796. CheckAndPromoteOperands(typeof(IAddSignatures), op.text, ref left, ref right, op.pos);
  797. left = GenerateAdd(left, right);
  798. break;
  799. case TokenId.Minus:
  800. CheckAndPromoteOperands(typeof(ISubtractSignatures), op.text, ref left, ref right, op.pos);
  801. left = GenerateSubtract(left, right);
  802. break;
  803. case TokenId.Amphersand:
  804. left = GenerateStringConcat(left, right);
  805. break;
  806. }
  807. }
  808. return left;
  809. }
  810. // *, /, %, mod operators
  811. Expression ParseMultiplicative()
  812. {
  813. Expression left = ParseUnary();
  814. while (token.id == TokenId.Asterisk || token.id == TokenId.Slash ||
  815. token.id == TokenId.Percent || TokenIdentifierIs("mod"))
  816. {
  817. Token op = token;
  818. NextToken();
  819. Expression right = ParseUnary();
  820. CheckAndPromoteOperands(typeof(IArithmeticSignatures), op.text, ref left, ref right, op.pos);
  821. switch (op.id)
  822. {
  823. case TokenId.Asterisk:
  824. left = Expression.Multiply(left, right);
  825. break;
  826. case TokenId.Slash:
  827. left = Expression.Divide(left, right);
  828. break;
  829. case TokenId.Percent:
  830. case TokenId.Identifier:
  831. left = Expression.Modulo(left, right);
  832. break;
  833. }
  834. }
  835. return left;
  836. }
  837. // -, !, not unary operators
  838. Expression ParseUnary()
  839. {
  840. if (token.id == TokenId.Minus || token.id == TokenId.Exclamation ||
  841. TokenIdentifierIs("not"))
  842. {
  843. Token op = token;
  844. NextToken();
  845. if (op.id == TokenId.Minus && (token.id == TokenId.IntegerLiteral ||
  846. token.id == TokenId.RealLiteral))
  847. {
  848. token.text = "-" + token.text;
  849. token.pos = op.pos;
  850. return ParsePrimary();
  851. }
  852. Expression expr = ParseUnary();
  853. if (op.id == TokenId.Minus)
  854. {
  855. CheckAndPromoteOperand(typeof(INegationSignatures), op.text, ref expr, op.pos);
  856. expr = Expression.Negate(expr);
  857. }
  858. else
  859. {
  860. CheckAndPromoteOperand(typeof(INotSignatures), op.text, ref expr, op.pos);
  861. expr = Expression.Not(expr);
  862. }
  863. return expr;
  864. }
  865. return ParsePrimary();
  866. }
  867. Expression ParsePrimary()
  868. {
  869. Expression expr = ParsePrimaryStart();
  870. while (true)
  871. {
  872. if (token.id == TokenId.Dot)
  873. {
  874. NextToken();
  875. expr = ParseMemberAccess(null, expr);
  876. }
  877. else if (token.id == TokenId.OpenBracket)
  878. {
  879. expr = ParseElementAccess(expr);
  880. }
  881. else
  882. {
  883. break;
  884. }
  885. }
  886. return expr;
  887. }
  888. Expression ParsePrimaryStart()
  889. {
  890. switch (token.id)
  891. {
  892. case TokenId.Identifier:
  893. return ParseIdentifier();
  894. case TokenId.StringLiteral:
  895. return ParseStringLiteral();
  896. case TokenId.IntegerLiteral:
  897. return ParseIntegerLiteral();
  898. case TokenId.RealLiteral:
  899. return ParseRealLiteral();
  900. case TokenId.OpenParen:
  901. return ParseParenExpression();
  902. default:
  903. throw ParseError(Res.ExpressionExpected);
  904. }
  905. }
  906. Expression ParseStringLiteral()
  907. {
  908. ValidateToken(TokenId.StringLiteral);
  909. char quote = token.text[0];
  910. string s = token.text.Substring(1, token.text.Length - 2);
  911. int start = 0;
  912. while (true)
  913. {
  914. int i = s.IndexOf(quote, start);
  915. if (i < 0) break;
  916. s = s.Remove(i, 1);
  917. start = i + 1;
  918. }
  919. if (quote == '\'')
  920. {
  921. if (s.Length != 1)
  922. throw ParseError(Res.InvalidCharacterLiteral);
  923. NextToken();
  924. return CreateLiteral(s[0], s);
  925. }
  926. NextToken();
  927. return CreateLiteral(s, s);
  928. }
  929. Expression ParseIntegerLiteral()
  930. {
  931. ValidateToken(TokenId.IntegerLiteral);
  932. string text = token.text;
  933. if (text[0] != '-')
  934. {
  935. ulong value;
  936. if (!UInt64.TryParse(text, out value))
  937. throw ParseError(Res.InvalidIntegerLiteral, text);
  938. NextToken();
  939. if (value <= (ulong)Int32.MaxValue) return CreateLiteral((int)value, text);
  940. if (value <= (ulong)UInt32.MaxValue) return CreateLiteral((uint)value, text);
  941. if (value <= (ulong)Int64.MaxValue) return CreateLiteral((long)value, text);
  942. return CreateLiteral(value, text);
  943. }
  944. else
  945. {
  946. long value;
  947. if (!Int64.TryParse(text, out value))
  948. throw ParseError(Res.InvalidIntegerLiteral, text);
  949. NextToken();
  950. if (value >= Int32.MinValue && value <= Int32.MaxValue)
  951. return CreateLiteral((int)value, text);
  952. return CreateLiteral(value, text);
  953. }
  954. }
  955. Expression ParseRealLiteral()
  956. {
  957. ValidateToken(TokenId.RealLiteral);
  958. string text = token.text;
  959. object value = null;
  960. char last = text[text.Length - 1];
  961. if (last == 'F' || last == 'f')
  962. {
  963. float f;
  964. if (Single.TryParse(text.Substring(0, text.Length - 1), out f)) value = f;
  965. }
  966. else
  967. {
  968. double d;
  969. if (Double.TryParse(text, out d)) value = d;
  970. }
  971. if (value == null) throw ParseError(Res.InvalidRealLiteral, text);
  972. NextToken();
  973. return CreateLiteral(value, text);
  974. }
  975. Expression CreateLiteral(object value, string text)
  976. {
  977. ConstantExpression expr = Expression.Constant(value);
  978. literals.Add(expr, text);
  979. return expr;
  980. }
  981. Expression ParseParenExpression()
  982. {
  983. ValidateToken(TokenId.OpenParen, Res.OpenParenExpected);
  984. NextToken();
  985. Expression e = ParseExpression();
  986. ValidateToken(TokenId.CloseParen, Res.CloseParenOrOperatorExpected);
  987. NextToken();
  988. return e;
  989. }
  990. Expression ParseIdentifier()
  991. {
  992. ValidateToken(TokenId.Identifier);
  993. object value;
  994. if (keywords.TryGetValue(token.text, out value))
  995. {
  996. if (value is Type) return ParseTypeAccess((Type)value);
  997. if (value == (object)keywordIt) return ParseIt();
  998. if (value == (object)keywordIif) return ParseIif();
  999. if (value == (object)keywordNew) return ParseNew();
  1000. NextToken();
  1001. return (Expression)value;
  1002. }
  1003. if (symbols.TryGetValue(token.text, out value) ||
  1004. externals != null && externals.TryGetValue(token.text, out value))
  1005. {
  1006. Expression expr = value as Expression;
  1007. if (expr == null)
  1008. {
  1009. expr = Expression.Constant(value);
  1010. }
  1011. else
  1012. {
  1013. LambdaExpression lambda = expr as LambdaExpression;
  1014. if (lambda != null) return ParseLambdaInvocation(lambda);
  1015. }
  1016. NextToken();
  1017. return expr;
  1018. }
  1019. if (it != null) return ParseMemberAccess(null, it);
  1020. throw ParseError(Res.UnknownIdentifier, token.text);
  1021. }
  1022. Expression ParseIt()
  1023. {
  1024. if (it == null)
  1025. throw ParseError(Res.NoItInScope);
  1026. NextToken();
  1027. return it;
  1028. }
  1029. Expression ParseIif()
  1030. {
  1031. int errorPos = token.pos;
  1032. NextToken();
  1033. Expression[] args = ParseArgumentList();
  1034. if (args.Length != 3)
  1035. throw ParseError(errorPos, Res.IifRequiresThreeArgs);
  1036. return GenerateConditional(args[0], args[1], args[2], errorPos);
  1037. }
  1038. Expression GenerateConditional(Expression test, Expression expr1, Expression expr2, int errorPos)
  1039. {
  1040. if (test.Type != typeof(bool))
  1041. throw ParseError(errorPos, Res.FirstExprMustBeBool);
  1042. if (expr1.Type != expr2.Type)
  1043. {
  1044. Expression expr1as2 = expr2 != nullLiteral ? PromoteExpression(expr1, expr2.Type, true) : null;
  1045. Expression expr2as1 = expr1 != nullLiteral ? PromoteExpression(expr2, expr1.Type, true) : null;
  1046. if (expr1as2 != null && expr2as1 == null)
  1047. {
  1048. expr1 = expr1as2;
  1049. }
  1050. else if (expr2as1 != null && expr1as2 == null)
  1051. {
  1052. expr2 = expr2as1;
  1053. }
  1054. else
  1055. {
  1056. string type1 = expr1 != nullLiteral ? expr1.Type.Name : "null";
  1057. string type2 = expr2 != nullLiteral ? expr2.Type.Name : "null";
  1058. if (expr1as2 != null && expr2as1 != null)
  1059. throw ParseError(errorPos, Res.BothTypesConvertToOther, type1, type2);
  1060. throw ParseError(errorPos, Res.NeitherTypeConvertsToOther, type1, type2);
  1061. }
  1062. }
  1063. return Expression.Condition(test, expr1, expr2);
  1064. }
  1065. Expression ParseNew()
  1066. {
  1067. NextToken();
  1068. ValidateToken(TokenId.OpenParen, Res.OpenParenExpected);
  1069. NextToken();
  1070. List<DynamicProperty> properties = new List<DynamicProperty>();
  1071. List<Expression> expressions = new List<Expression>();
  1072. while (true)
  1073. {
  1074. int exprPos = token.pos;
  1075. Expression expr = ParseExpression();
  1076. string propName;
  1077. if (TokenIdentifierIs("as"))
  1078. {
  1079. NextToken();
  1080. propName = GetIdentifier();
  1081. NextToken();
  1082. }
  1083. else
  1084. {
  1085. MemberExpression me = expr as MemberExpression;
  1086. if (me == null) throw ParseError(exprPos, Res.MissingAsClause);
  1087. propName = me.Member.Name;
  1088. }
  1089. expressions.Add(expr);
  1090. properties.Add(new DynamicProperty(propName, expr.Type));
  1091. if (token.id != TokenId.Comma) break;
  1092. NextToken();
  1093. }
  1094. ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected);
  1095. NextToken();
  1096. Type type = DynamicExpression.CreateClass(properties);
  1097. MemberBinding[] bindings = new MemberBinding[properties.Count];
  1098. for (int i = 0; i < bindings.Length; i++)
  1099. bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);
  1100. return Expression.MemberInit(Expression.New(type), bindings);
  1101. }
  1102. Expression ParseLambdaInvocation(LambdaExpression lambda)
  1103. {
  1104. int errorPos = token.pos;
  1105. NextToken();
  1106. Expression[] args = ParseArgumentList();
  1107. MethodBase method;
  1108. if (FindMethod(lambda.Type, "Invoke", false, args, out method) != 1)
  1109. throw ParseError(errorPos, Res.ArgsIncompatibleWithLambda);
  1110. return Expression.Invoke(lambda, args);
  1111. }
  1112. Expression ParseTypeAccess(Type type)
  1113. {
  1114. int errorPos = token.pos;
  1115. NextToken();
  1116. if (token.id == TokenId.Question)
  1117. {
  1118. if (!type.IsValueType || IsNullableType(type))
  1119. throw ParseError(errorPos, Res.TypeHasNoNullableForm, GetTypeName(type));
  1120. type = typeof(Nullable<>).MakeGenericType(type);
  1121. NextToken();
  1122. }
  1123. if (token.id == TokenId.OpenParen)
  1124. {
  1125. Expression[] args = ParseArgumentList();
  1126. MethodBase method;
  1127. switch (FindBestMethod(type.GetConstructors(), args, out method))
  1128. {
  1129. case 0:
  1130. if (args.Length == 1)
  1131. return GenerateConversion(args[0], type, errorPos);
  1132. throw ParseError(errorPos, Res.NoMatchingConstructor, GetTypeName(type));
  1133. case 1:
  1134. return Expression.New((ConstructorInfo)method, args);
  1135. default:
  1136. throw ParseError(errorPos, Res.AmbiguousConstructorInvocation, GetTypeName(type));
  1137. }
  1138. }
  1139. ValidateToken(TokenId.Dot, Res.DotOrOpenParenExpected);
  1140. NextToken();
  1141. return ParseMemberAccess(type, null);
  1142. }
  1143. Expression GenerateConversion(Expression expr, Type type, int errorPos)
  1144. {
  1145. Type exprType = expr.Type;
  1146. if (exprType == type) return expr;
  1147. if (exprType.IsValueType && type.IsValueType)
  1148. {
  1149. if ((IsNullableType(exprType) || IsNullableType(type)) &&
  1150. GetNonNullableType(exprType) == GetNonNullableType(type))
  1151. return Expression.Convert(expr, type);
  1152. if ((IsNumericType(exprType) || IsEnumType(exprType)) &&
  1153. (IsNumericType(type)) || IsEnumType(type))
  1154. return Expression.ConvertChecked(expr, type);
  1155. }
  1156. if (exprType.IsAssignableFrom(type) || type.IsAssignableFrom(exprType) ||
  1157. exprType.IsInterface || type.IsInterface)
  1158. return Expression.Convert(expr, type);
  1159. throw ParseError(errorPos, Res.CannotConvertValue,
  1160. GetTypeName(exprType), GetTypeName(type));
  1161. }
  1162. Expression ParseMemberAccess(Type type, Expression instance)
  1163. {
  1164. if (instance != null) type = instance.Type;
  1165. int errorPos = token.pos;
  1166. string id = GetIdentifier();
  1167. NextToken();
  1168. if (token.id == TokenId.OpenParen)
  1169. {
  1170. if (instance != null && type != typeof(string))
  1171. {
  1172. Type enumerableType = FindGenericType(typeof(IEnumerable<>), type);
  1173. if (enumerableType != null)
  1174. {
  1175. Type elementType = enumerableType.GetGenericArguments()[0];
  1176. return ParseAggregate(instance, elementType, id, errorPos);
  1177. }
  1178. }
  1179. Expression[] args = ParseArgumentList();
  1180. MethodBase mb;
  1181. switch (FindMethod(type, id, instance == null, args, out mb))
  1182. {
  1183. case 0:
  1184. throw ParseError(errorPos, Res.NoApplicableMethod,
  1185. id, GetTypeName(type));
  1186. case 1:
  1187. MethodInfo method = (MethodInfo)mb;
  1188. if (!IsPredefinedType(method.DeclaringType))
  1189. throw ParseError(errorPos, Res.MethodsAreInaccessible, GetTypeName(method.DeclaringType));
  1190. if (method.ReturnType == typeof(void))
  1191. throw ParseError(errorPos, Res.MethodIsVoid,
  1192. id, GetTypeName(method.DeclaringType));
  1193. return Expression.Call(instance, (MethodInfo)method, args);
  1194. default:
  1195. throw ParseError(errorPos, Res.AmbiguousMethodInvocation,
  1196. id, GetTypeName(type));
  1197. }
  1198. }
  1199. else
  1200. {
  1201. MemberInfo member = FindPropertyOrField(type, id, instance == null);
  1202. if (member == null)
  1203. throw ParseError(errorPos, Res.UnknownPropertyOrField,
  1204. id, GetTypeName(type));
  1205. return member is PropertyInfo ?
  1206. Expression.Property(instance, (PropertyInfo)member) :
  1207. Expression.Field(instance, (FieldInfo)member);
  1208. }
  1209. }
  1210. static Type FindGenericType(Type generic, Type type)
  1211. {
  1212. while (type != null && type != typeof(object))
  1213. {
  1214. if (type.IsGenericType && type.GetGenericTypeDefinition() == generic) return type;
  1215. if (generic.IsInterface)
  1216. {
  1217. foreach (Type intfType in type.GetInterfaces())
  1218. {
  1219. Type found = FindGenericType(generic, intfType);
  1220. if (found != null) return found;
  1221. }
  1222. }
  1223. type = type.BaseType;
  1224. }
  1225. return null;
  1226. }
  1227. Expression ParseAggregate(Expression instance, Type elementType, string methodName, int errorPos)
  1228. {
  1229. ParameterExpression outerIt = it;
  1230. ParameterExpression innerIt = Expression.Parameter(elementType, "");
  1231. it = innerIt;
  1232. Expression[] args = ParseArgumentList();
  1233. it = outerIt;
  1234. MethodBase signature;
  1235. if (FindMethod(typeof(IEnumerableSignatures), methodName, false, args, out signature) != 1)
  1236. throw ParseError(errorPos, Res.NoApplicableAggregate, methodName);
  1237. Type[] typeArgs;
  1238. if (signature.Name == "Min" || signature.Name == "Max")
  1239. {
  1240. typeArgs = new Type[] { elementType, args[0].Type };
  1241. }
  1242. else
  1243. {
  1244. typeArgs = new Type[] { elementType };
  1245. }
  1246. if (args.Length == 0)
  1247. {
  1248. args = new Expression[] { instance };
  1249. }
  1250. else
  1251. {
  1252. args = new Expression[] { instance, Expression.Lambda(args[0], innerIt) };
  1253. }
  1254. return Expression.Call(typeof(Enumerable), signature.Name, typeArgs, args);
  1255. }
  1256. Expression[] ParseArgumentList()
  1257. {
  1258. ValidateToken(TokenId.OpenParen, Res.OpenParenExpected);
  1259. NextToken();
  1260. Expression[] args = token.id != TokenId.CloseParen ? ParseArguments() : new Expression[0];
  1261. ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected);
  1262. NextToken();
  1263. return args;
  1264. }
  1265. Expression[] ParseArguments()
  1266. {
  1267. List<Expression> argList = new List<Expression>();
  1268. while (true)
  1269. {
  1270. argList.Add(ParseExpression());
  1271. if (token.id != TokenId.Comma) break;
  1272. NextToken();
  1273. }
  1274. return argList.ToArray();
  1275. }
  1276. Expression ParseElementAccess(Expression expr)
  1277. {
  1278. int errorPos = token.pos;
  1279. ValidateToken(TokenId.OpenBracket, Res.OpenParenExpected);
  1280. NextToken();
  1281. Expression[] args = ParseArguments();
  1282. ValidateToken(TokenId.CloseBracket, Res.CloseBracketOrCommaExpected);
  1283. NextToken();
  1284. if (expr.Type.IsArray)
  1285. {
  1286. if (expr.Type.GetArrayRank() != 1 || args.Length != 1)
  1287. throw ParseError(errorPos, Res.CannotIndexMultiDimArray);
  1288. Expression index = PromoteExpression(args[0], typeof(int), true);
  1289. if (index == null)
  1290. throw ParseError(errorPos, Res.InvalidIndex);
  1291. return Expression.ArrayIndex(expr, index);
  1292. }
  1293. else
  1294. {
  1295. MethodBase mb;
  1296. switch (FindIndexer(expr.Type, args, out mb))
  1297. {
  1298. case 0:
  1299. throw ParseError(errorPos, Res.NoApplicableIndexer,
  1300. GetTypeName(expr.Type));
  1301. case 1:
  1302. return Expression.Call(expr, (MethodInfo)mb, args);
  1303. default:
  1304. throw ParseError(errorPos, Res.AmbiguousIndexerInvocation,
  1305. GetTypeName(expr.Type));
  1306. }
  1307. }
  1308. }
  1309. static bool IsPredefinedType(Type type)
  1310. {
  1311. foreach (Type t in predefinedTypes) if (t == type) return true;
  1312. return false;
  1313. }
  1314. static bool IsNullableType(Type type)
  1315. {
  1316. return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
  1317. }
  1318. static Type GetNonNullableType(Type type)
  1319. {
  1320. return IsNullableType(type) ? type.GetGenericArguments()[0] : type;
  1321. }
  1322. static string GetTypeName(Type type)
  1323. {
  1324. Type baseType = GetNonNullableType(type);
  1325. string s = baseType.Name;
  1326. if (type != baseType) s += '?';
  1327. return s;
  1328. }
  1329. static bool IsNumericType(Type type)
  1330. {
  1331. return GetNumericTypeKind(type) != 0;
  1332. }
  1333. static bool IsSignedIntegralType(Type type)
  1334. {
  1335. return GetNumericTypeKind(type) == 2;
  1336. }
  1337. static bool IsUnsignedIntegralType(Type type)
  1338. {
  1339. return GetNumericTypeKind(type) == 3;
  1340. }
  1341. static int GetNumericTypeKind(Type type)
  1342. {
  1343. type = GetNonNullableType(type);
  1344. if (type.IsEnum) return 0;
  1345. switch (Type.GetTypeCode(type))
  1346. {
  1347. case TypeCode.Char:
  1348. case TypeCode.Single:
  1349. case TypeCode.Double:
  1350. case TypeCode.Decimal:
  1351. return 1;
  1352. case TypeCode.SByte:
  1353. case TypeCode.Int16:
  1354. case TypeCode.Int32:
  1355. case TypeCode.Int64:
  1356. return 2;
  1357. case TypeCode.Byte:
  1358. case TypeCode.UInt16:
  1359. case TypeCode.UInt32:
  1360. case TypeCode.UInt64:
  1361. return 3;
  1362. default:
  1363. return 0;
  1364. }
  1365. }
  1366. static bool IsEnumType(Type type)
  1367. {
  1368. return GetNonNullableType(type).IsEnum;
  1369. }
  1370. void CheckAndPromoteOperand(Type signatures, string opName, ref Expression expr, int errorPos)
  1371. {
  1372. Expression[] args = new Expression[] { expr };
  1373. MethodBase method;
  1374. if (FindMethod(signatures, "F", false, args, out method) != 1)
  1375. throw ParseError(errorPos, Res.IncompatibleOperand,
  1376. opName, GetTypeName(args[0].Type));
  1377. expr = args[0];
  1378. }
  1379. void CheckAndPromoteOperands(Type signatures, string opName, ref Expression left, ref Expression right, int errorPos)
  1380. {
  1381. Expression[] args = new Expression[] { left, right };
  1382. MethodBase method;
  1383. if (FindMethod(signatures, "F", false, args, out method) != 1)
  1384. throw IncompatibleOperandsError(opName, left, right, errorPos);
  1385. left = args[0];
  1386. right = args[1];
  1387. }
  1388. Exception IncompatibleOperandsError(string opName, Expression left, Expression right, int pos)
  1389. {
  1390. return ParseError(pos, Res.IncompatibleOperands,
  1391. opName, GetTypeName(left.Type), GetTypeName(right.Type));
  1392. }
  1393. MemberInfo FindPropertyOrField(Type type, string memberName, bool staticAccess)
  1394. {
  1395. BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly |
  1396. (staticAccess ? BindingFlags.Static : BindingFlags.Instance);
  1397. foreach (Type t in SelfAndBaseTypes(type))
  1398. {
  1399. MemberInfo[] members = t.FindMembers(MemberTypes.Property | MemberTypes.Field,
  1400. flags, Type.FilterNameIgnoreCase, memberName);
  1401. if (members.Length != 0) return members[0];
  1402. }
  1403. return null;
  1404. }
  1405. int FindMethod(Type type, string methodName, bool staticAccess, Expression[] args, out MethodBase method)
  1406. {
  1407. BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly |
  1408. (staticAccess ? BindingFlags.Static : BindingFlags.Instance);
  1409. foreach (Type t in SelfAndBaseTypes(type))
  1410. {
  1411. MemberInfo[] members = t.FindMembers(MemberTypes.Method,
  1412. flags, Type.FilterNameIgnoreCase, methodName);
  1413. int count = FindBestMethod(members.Cast<MethodBase>(), args, out method);
  1414. if (count != 0) return count;
  1415. }
  1416. method = null;
  1417. return 0;
  1418. }
  1419. int FindIndexer(Type type, Expression[] args, out MethodBase method)
  1420. {
  1421. foreach (Type t in SelfAndBaseTypes(type))
  1422. {
  1423. MemberInfo[] members = t.GetDefaultMembers();
  1424. if (members.Length != 0)
  1425. {
  1426. IEnumerable<MethodBase> methods = members.
  1427. OfType<PropertyInfo>().
  1428. Select(p => (MethodBase)p.GetGetMethod()).
  1429. Where(m => m != null);
  1430. int count = FindBestMethod(methods, args, out method);
  1431. if (count != 0) return count;
  1432. }
  1433. }
  1434. method = null;
  1435. return 0;
  1436. }
  1437. static IEnumerable<Type> SelfAndBaseTypes(Type type)
  1438. {
  1439. if (type.IsInterface)
  1440. {
  1441. List<Type> types = new List<Type>();
  1442. AddInterface(types, type);
  1443. return types;
  1444. }
  1445. return SelfAndBaseClasses(type);
  1446. }
  1447. static IEnumerable<Type> SelfAndBaseClasses(Type type)
  1448. {
  1449. while (type != null)
  1450. {
  1451. yield return type;
  1452. type = type.BaseType;
  1453. }
  1454. }
  1455. static void AddInterface(List<Type> types, Type type)
  1456. {
  1457. if (!types.Contains(type))
  1458. {
  1459. types.Add(type);
  1460. foreach (Type t in type.GetInterfaces()) AddInterface(types, t);
  1461. }
  1462. }
  1463. class MethodData
  1464. {
  1465. public MethodBase MethodBase;
  1466. public ParameterInfo[] Parameters;
  1467. public Expression[] Args;
  1468. }
  1469. int FindBestMethod(IEnumerable<MethodBase> methods, Expression[] args, out MethodBase method)
  1470. {
  1471. MethodData[] applicable = methods.
  1472. Select(m => new MethodData { MethodBase = m, Parameters = m.GetParameters() }).
  1473. Where(m => IsApplicable(m, args)).
  1474. ToArray();
  1475. if (applicable.Length > 1)
  1476. {
  1477. applicable = applicable.
  1478. Where(m => applicable.All(n => m == n || IsBetterThan(args, m, n))).
  1479. ToArray();
  1480. }
  1481. if (applicable.Length == 1)
  1482. {
  1483. MethodData md = applicable[0];
  1484. for (int i = 0; i < args.Length; i++) args[i] = md.Args[i];
  1485. method = md.MethodBase;
  1486. }
  1487. else
  1488. {
  1489. method = null;
  1490. }
  1491. return applicable.Length;
  1492. }
  1493. bool IsApplicable(MethodData method, Expression[] args)
  1494. {
  1495. if (method.Parameters.Length != args.Length) return false;
  1496. Expression[] promotedArgs = new Expression[args.Length];
  1497. for (int i = 0; i < args.Length; i++)
  1498. {
  1499. ParameterInfo pi = method.Parameters[i];
  1500. if (pi.IsOut) return false;
  1501. Expression promoted = PromoteExpression(args[i], pi.ParameterType, false);
  1502. if (promoted == null) return false;
  1503. promotedArgs[i] = promoted;
  1504. }
  1505. method.Args = promotedArgs;
  1506. return true;
  1507. }
  1508. Expression PromoteExpression(Expression expr, Type type, bool exact)
  1509. {
  1510. if (expr.Type == type) return expr;
  1511. if (expr is ConstantExpression)
  1512. {
  1513. ConstantExpression ce = (ConstantExpression)expr;
  1514. if (ce == nullLiteral)
  1515. {
  1516. if (!type.IsValueType || IsNullableType(type))
  1517. return Expression.Constant(null, type);
  1518. }
  1519. else
  1520. {
  1521. string text;
  1522. if (literals.TryGetValue(ce, out text))
  1523. {
  1524. Type target = GetNonNullableType(type);
  1525. Object value = null;
  1526. switch (Type.GetTypeCode(ce.Type))
  1527. {
  1528. case TypeCode.Int32:
  1529. case TypeCode.UInt32:
  1530. case TypeCode.Int64:
  1531. case TypeCode.UInt64:
  1532. value = ParseNumber(text, target);
  1533. break;
  1534. case TypeCode.Double:
  1535. if (target == typeof(decimal)) value = ParseNumber(text, target);
  1536. break;
  1537. case TypeCode.String:
  1538. value = ParseEnum(text, target);
  1539. break;
  1540. }
  1541. if (value != null)
  1542. return Expression.Constant(value, type);
  1543. }
  1544. }
  1545. }
  1546. if (IsCompatibleWith(expr.Type, type))
  1547. {
  1548. if (type.IsValueType || exact) return Expression.Convert(expr, type);
  1549. return expr;
  1550. }
  1551. return null;
  1552. }
  1553. static object ParseNumber(string text, Type type)
  1554. {
  1555. switch (Type.GetTypeCode(GetNonNullableType(type)))
  1556. {
  1557. case TypeCode.SByte:
  1558. sbyte sb;
  1559. if (sbyte.TryParse(text, out sb)) return sb;
  1560. break;
  1561. case TypeCode.Byte:
  1562. byte b;
  1563. if (byte.TryParse(text, out b)) return b;
  1564. break;
  1565. case TypeCode.Int16:
  1566. short s;
  1567. if (short.TryParse(text, out s)) return s;
  1568. break;
  1569. case TypeCode.UInt16:
  1570. ushort us;
  1571. if (ushort.TryParse(text, out us)) return us;
  1572. break;
  1573. case TypeCode.Int32:
  1574. int i;
  1575. if (int.TryParse(text, out i)) return i;
  1576. break;
  1577. case TypeCode.UInt32:
  1578. uint ui;
  1579. if (uint.TryParse(text, out ui)) return ui;
  1580. break;
  1581. case TypeCode.Int64:
  1582. long l;
  1583. if (long.TryParse(text, out l)) return l;
  1584. break;
  1585. case TypeCode.UInt64:
  1586. ulong ul;
  1587. if (ulong.TryParse(text, out ul)) return ul;
  1588. break;
  1589. case TypeCode.Single:
  1590. float f;
  1591. if (float.TryParse(text, out f)) return f;
  1592. break;
  1593. case TypeCode.Double:
  1594. double d;
  1595. if (double.TryParse(text, out d)) return d;
  1596. break;
  1597. case TypeCode.Decimal:
  1598. decimal e;
  1599. if (decimal.TryParse(text, out e)) return e;
  1600. break;
  1601. }
  1602. return null;
  1603. }
  1604. static object ParseEnum(string name, Type type)
  1605. {
  1606. if (type.IsEnum)
  1607. {
  1608. MemberInfo[] memberInfos = type.FindMembers(MemberTypes.Field,
  1609. BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static,
  1610. Type.FilterNameIgnoreCase, name);
  1611. if (memberInfos.Length != 0) return ((FieldInfo)memberInfos[0]).GetValue(null);
  1612. }
  1613. return null;
  1614. }
  1615. static bool IsCompatibleWith(Type source, Type target)
  1616. {
  1617. if (source == target) return true;
  1618. if (!target.IsValueType) return target.IsAssignableFrom(source);
  1619. Type st = GetNonNullableType(source);
  1620. Type tt = GetNonNullableType(target);
  1621. if (st != source && tt == target) return false;
  1622. TypeCode sc = st.IsEnum ? TypeCode.Object : Type.GetTypeCode(st);
  1623. TypeCode tc = tt.IsEnum ? TypeCode.Object : Type.GetTypeCode(tt);
  1624. switch (sc)
  1625. {
  1626. case TypeCode.SByte:
  1627. switch (tc)
  1628. {
  1629. case TypeCode.SByte:
  1630. case TypeCode.Int16:
  1631. case TypeCode.Int32:
  1632. case TypeCode.Int64:
  1633. case TypeCode.Single:
  1634. case TypeCode.Double:
  1635. case TypeCode.Decimal:
  1636. return true;
  1637. }
  1638. break;
  1639. case TypeCode.Byte:
  1640. switch (tc)
  1641. {
  1642. case TypeCode.Byte:
  1643. case TypeCode.Int16:
  1644. case TypeCode.UInt16:
  1645. case TypeCode.Int32:
  1646. case TypeCode.UInt32:
  1647. case TypeCode.Int64:
  1648. case TypeCode.UInt64:
  1649. case TypeCode.Single:
  1650. case TypeCode.Double:
  1651. case TypeCode.Decimal:
  1652. return true;
  1653. }
  1654. break;
  1655. case TypeCode.Int16:
  1656. switch (tc)
  1657. {
  1658. case TypeCode.Int16:
  1659. case TypeCode.Int32:
  1660. case TypeCode.Int64:
  1661. case TypeCode.Single:
  1662. case TypeCode.Double:
  1663. case TypeCode.Decimal:
  1664. return true;
  1665. }
  1666. break;
  1667. case TypeCode.UInt16:
  1668. switch (tc)
  1669. {
  1670. case TypeCode.UInt16:
  1671. case TypeCode.Int32:
  1672. case TypeCode.UInt32:
  1673. case TypeCode.Int64:
  1674. case TypeCode.UInt64:
  1675. case TypeCode.Single:
  1676. case TypeCode.Double:
  1677. case TypeCode.Decimal:
  1678. return true;
  1679. }
  1680. break;
  1681. case TypeCode.Int32:
  1682. switch (tc)
  1683. {
  1684. case TypeCode.Int32:
  1685. case TypeCode.Int64:
  1686. case TypeCode.Single:
  1687. case TypeCode.Double:
  1688. case TypeCode.Decimal:
  1689. return true;
  1690. }
  1691. break;
  1692. case TypeCode.UInt32:
  1693. switch (tc)
  1694. {
  1695. case TypeCode.UInt32:
  1696. case TypeCode.Int64:
  1697. case TypeCode.UInt64:
  1698. case TypeCode.Single:
  1699. case TypeCode.Double:
  1700. case TypeCode.Decimal:
  1701. return true;
  1702. }
  1703. break;
  1704. case TypeCode.Int64:
  1705. switch (tc)
  1706. {
  1707. case TypeCode.Int64:
  1708. case TypeCode.Single:
  1709. case TypeCode.Double:
  1710. case TypeCode.Decimal:
  1711. return true;
  1712. }
  1713. break;
  1714. case TypeCode.UInt64:
  1715. switch (tc)
  1716. {
  1717. case TypeCode.UInt64:
  1718. case TypeCode.Single:
  1719. case TypeCode.Double:
  1720. case TypeCode.Decimal:
  1721. return true;
  1722. }
  1723. break;
  1724. case TypeCode.Single:
  1725. switch (tc)
  1726. {
  1727. case TypeCode.Single:
  1728. case TypeCode.Double:
  1729. return true;
  1730. }
  1731. break;
  1732. default:
  1733. if (st == tt) return true;
  1734. break;
  1735. }
  1736. return false;
  1737. }
  1738. static bool IsBetterThan(Expression[] args, MethodData m1, MethodData m2)
  1739. {
  1740. bool better = false;
  1741. for (int i = 0; i < args.Length; i++)
  1742. {
  1743. int c = CompareConversions(args[i].Type,
  1744. m1.Parameters[i].ParameterType,
  1745. m2.Parameters[i].ParameterType);
  1746. if (c < 0) return false;
  1747. if (c > 0) better = true;
  1748. }
  1749. return better;
  1750. }
  1751. // Return 1 if s -> t1 is a better conversion than s -> t2
  1752. // Return -1 if s -> t2 is a better conversion than s -> t1
  1753. // Return 0 if neither conversion is better
  1754. static int CompareConversions(Type s, Type t1, Type t2)
  1755. {
  1756. if (t1 == t2) return 0;
  1757. if (s == t1) return 1;
  1758. if (s == t2) return -1;
  1759. bool t1t2 = IsCompatibleWith(t1, t2);
  1760. bool t2t1 = IsCompatibleWith(t2, t1);
  1761. if (t1t2 && !t2t1) return 1;
  1762. if (t2t1 && !t1t2) return -1;
  1763. if (IsSignedIntegralType(t1) && IsUnsignedIntegralType(t2)) return 1;
  1764. if (IsSignedIntegralType(t2) && IsUnsignedIntegralType(t1)) return -1;
  1765. return 0;
  1766. }
  1767. Expression GenerateEqual(Expression left, Expression right)
  1768. {
  1769. return Expression.Equal(left, right);
  1770. }
  1771. Expression GenerateNotEqual(Expression left, Expression right)
  1772. {
  1773. return Expression.NotEqual(left, right);
  1774. }
  1775. Expression GenerateGreaterThan(Expression left, Expression right)
  1776. {
  1777. if (left.Type == typeof(string))
  1778. {
  1779. return Expression.GreaterThan(
  1780. GenerateStaticMethodCall("Compare", left, right),
  1781. Expression.Constant(0)
  1782. );
  1783. }
  1784. return Expression.GreaterThan(left, right);
  1785. }
  1786. Expression GenerateGreaterThanEqual(Expression left, Expression right)
  1787. {
  1788. if (left.Type == typeof(string))
  1789. {
  1790. return Expression.GreaterThanOrEqual(
  1791. GenerateStaticMethodCall("Compare", left, right),
  1792. Expression.Constant(0)
  1793. );
  1794. }
  1795. return Expression.GreaterThanOrEqual(left, right);
  1796. }
  1797. Expression GenerateLessThan(Expression left, Expression right)
  1798. {
  1799. if (left.Type == typeof(string))
  1800. {
  1801. return Expression.LessThan(
  1802. GenerateStaticMethodCall("Compare", left, right),
  1803. Expression.Constant(0)
  1804. );
  1805. }
  1806. return Expression.LessThan(left, right);
  1807. }
  1808. Expression GenerateLessThanEqual(Expression left, Expression right)
  1809. {
  1810. if (left.Type == typeof(string))
  1811. {
  1812. return Expression.LessThanOrEqual(
  1813. GenerateStaticMethodCall("Compare", left, right),
  1814. Expression.Constant(0)
  1815. );
  1816. }
  1817. return Expression.LessThanOrEqual(left, right);
  1818. }
  1819. Expression GenerateAdd(Expression left, Expression right)
  1820. {
  1821. if (left.Type == typeof(string) && right.Type == typeof(string))
  1822. {
  1823. return GenerateStaticMethodCall("Concat", left, right);
  1824. }
  1825. return Expression.Add(left, right);
  1826. }
  1827. Expression GenerateSubtract(Expression left, Expression right)
  1828. {
  1829. return Expression.Subtract(left, right);
  1830. }
  1831. Expression GenerateStringConcat(Expression left, Expression right)
  1832. {
  1833. return Expression.Call(
  1834. null,
  1835. typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) }),
  1836. new[] { left, right });
  1837. }
  1838. MethodInfo GetStaticMethod(string methodName, Expression left, Expression right)
  1839. {
  1840. return left.Type.GetMethod(methodName, new[] { left.Type, right.Type });
  1841. }
  1842. Expression GenerateStaticMethodCall(string methodName, Expression left, Expression right)
  1843. {
  1844. return Expression.Call(null, GetStaticMethod(methodName, left, right), new[] { left, right });
  1845. }
  1846. void SetTextPos(int pos)
  1847. {
  1848. textPos = pos;
  1849. ch = textPos < textLen ? text[textPos] : '\0';
  1850. }
  1851. void NextChar()
  1852. {
  1853. if (textPos < textLen) textPos++;
  1854. ch = textPos < textLen ? text[textPos] : '\0';
  1855. }
  1856. void NextToken()
  1857. {
  1858. while (Char.IsWhiteSpace(ch)) NextChar();
  1859. TokenId t;
  1860. int tokenPos = textPos;
  1861. switch (ch)
  1862. {
  1863. case '!':
  1864. NextChar();
  1865. if (ch == '=')
  1866. {
  1867. NextChar();
  1868. t = TokenId.ExclamationEqual;
  1869. }
  1870. else
  1871. {
  1872. t = TokenId.Exclamation;
  1873. }
  1874. break;
  1875. case '%':
  1876. NextChar();
  1877. t = TokenId.Percent;
  1878. break;
  1879. case '&':
  1880. NextChar();
  1881. if (ch == '&')
  1882. {
  1883. NextChar();
  1884. t = TokenId.DoubleAmphersand;
  1885. }
  1886. else
  1887. {
  1888. t = TokenId.Amphersand;
  1889. }
  1890. break;
  1891. case '(':
  1892. NextChar();
  1893. t = TokenId.OpenParen;
  1894. break;
  1895. case ')':
  1896. NextChar();
  1897. t = TokenId.CloseParen;
  1898. break;
  1899. case '*':
  1900. NextChar();
  1901. t = TokenId.Asterisk;
  1902. break;
  1903. case '+':
  1904. NextChar();
  1905. t = TokenId.Plus;
  1906. break;
  1907. case ',':
  1908. NextChar();
  1909. t = TokenId.Comma;
  1910. break;
  1911. case '-':
  1912. NextChar();
  1913. t = TokenId.Minus;
  1914. break;
  1915. case '.':
  1916. NextChar();
  1917. t = TokenId.Dot;
  1918. break;
  1919. case '/':
  1920. NextChar();
  1921. t = TokenId.Slash;
  1922. break;
  1923. case ':':
  1924. NextChar();
  1925. t = TokenId.Colon;
  1926. break;
  1927. case '<':
  1928. NextChar();
  1929. if (ch == '=')
  1930. {
  1931. NextChar();
  1932. t = TokenId.LessThanEqual;
  1933. }
  1934. else if (ch == '>')
  1935. {
  1936. NextChar();
  1937. t = TokenId.LessGreater;
  1938. }
  1939. else
  1940. {
  1941. t = TokenId.LessThan;
  1942. }
  1943. break;
  1944. case '=':
  1945. NextChar();
  1946. if (ch == '=')
  1947. {
  1948. NextChar();
  1949. t = TokenId.DoubleEqual;
  1950. }
  1951. else
  1952. {
  1953. t = TokenId.Equal;
  1954. }
  1955. break;
  1956. case '>':
  1957. NextChar();
  1958. if (ch == '=')
  1959. {
  1960. NextChar();
  1961. t = TokenId.GreaterThanEqual;
  1962. }
  1963. else
  1964. {
  1965. t = TokenId.GreaterThan;
  1966. }
  1967. break;
  1968. case '?':
  1969. NextChar();
  1970. t = TokenId.Question;
  1971. break;
  1972. case '[':
  1973. NextChar();
  1974. t = TokenId.OpenBracket;
  1975. break;
  1976. case ']':
  1977. NextChar();
  1978. t = TokenId.CloseBracket;
  1979. break;
  1980. case '|':
  1981. NextChar();
  1982. if (ch == '|')
  1983. {
  1984. NextChar();
  1985. t = TokenId.DoubleBar;
  1986. }
  1987. else
  1988. {
  1989. t = TokenId.Bar;
  1990. }
  1991. break;
  1992. case '"':
  1993. case '\'':
  1994. char quote = ch;
  1995. do
  1996. {
  1997. NextChar();
  1998. while (textPos < textLen && ch != quote) NextChar();
  1999. if (textPos == textLen)
  2000. throw ParseError(textPos, Res.UnterminatedStringLiteral);
  2001. NextChar();
  2002. } while (ch == quote);
  2003. t = TokenId.StringLiteral;
  2004. break;
  2005. default:
  2006. if (Char.IsLetter(ch) || ch == '@' || ch == '_')
  2007. {
  2008. do
  2009. {
  2010. NextChar();
  2011. } while (Char.IsLetterOrDigit(ch) || ch == '_');
  2012. t = TokenId.Identifier;
  2013. break;
  2014. }
  2015. if (Char.IsDigit(ch))
  2016. {
  2017. t = TokenId.IntegerLiteral;
  2018. do
  2019. {
  2020. NextChar();
  2021. } while (Char.IsDigit(ch));
  2022. if (ch == '.')
  2023. {
  2024. t = TokenId.RealLiteral;
  2025. NextChar();
  2026. ValidateDigit();
  2027. do
  2028. {
  2029. NextChar();
  2030. } while (Char.IsDigit(ch));
  2031. }
  2032. if (ch == 'E' || ch == 'e')
  2033. {
  2034. t = TokenId.RealLiteral;
  2035. NextChar();
  2036. if (ch == '+' || ch == '-') NextChar();
  2037. ValidateDigit();
  2038. do
  2039. {
  2040. NextChar();
  2041. } while (Char.IsDigit(ch));
  2042. }
  2043. if (ch == 'F' || ch == 'f') NextChar();
  2044. break;
  2045. }
  2046. if (textPos == textLen)
  2047. {
  2048. t = TokenId.End;
  2049. break;
  2050. }
  2051. throw ParseError(textPos, Res.InvalidCharacter, ch);
  2052. }
  2053. token.id = t;
  2054. token.text = text.Substring(tokenPos, textPos - tokenPos);
  2055. token.pos = tokenPos;
  2056. }
  2057. bool TokenIdentifierIs(string id)
  2058. {
  2059. return token.id == TokenId.Identifier && String.Equals(id, token.text, StringComparison.OrdinalIgnoreCase);
  2060. }
  2061. string GetIdentifier()
  2062. {
  2063. ValidateToken(TokenId.Identifier, Res.IdentifierExpected);
  2064. string id = token.text;
  2065. if (id.Length > 1 && id[0] == '@') id = id.Substring(1);
  2066. return id;
  2067. }
  2068. void ValidateDigit()
  2069. {
  2070. if (!Char.IsDigit(ch)) throw ParseError(textPos, Res.DigitExpected);
  2071. }
  2072. void ValidateToken(TokenId t, string errorMessage)
  2073. {
  2074. if (token.id != t) throw ParseError(errorMessage);
  2075. }
  2076. void ValidateToken(TokenId t)
  2077. {
  2078. if (token.id != t) throw ParseError(Res.SyntaxError);
  2079. }
  2080. Exception ParseError(string format, params object[] args)
  2081. {
  2082. return ParseError(token.pos, format, args);
  2083. }
  2084. Exception ParseError(int pos, string format, params object[] args)
  2085. {
  2086. return new ParseException(string.Format(System.Globalization.CultureInfo.CurrentCulture, format, args), pos);
  2087. }
  2088. static Dictionary<string, object> CreateKeywords()
  2089. {
  2090. Dictionary<string, object> d = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  2091. d.Add("true", trueLiteral);
  2092. d.Add("false", falseLiteral);
  2093. d.Add("null", nullLiteral);
  2094. d.Add(keywordIt, keywordIt);
  2095. d.Add(keywordIif, keywordIif);
  2096. d.Add(keywordNew, keywordNew);
  2097. foreach (Type type in predefinedTypes) d.Add(type.Name, type);
  2098. return d;
  2099. }
  2100. }
  2101. static class Res
  2102. {
  2103. public const string DuplicateIdentifier = "The identifier '{0}' was defined more than once";
  2104. public const string ExpressionTypeMismatch = "Expression of type '{0}' expected";
  2105. public const string ExpressionExpected = "Expression expected";
  2106. public const string InvalidCharacterLiteral = "Character literal must contain exactly one character";
  2107. public const string InvalidIntegerLiteral = "Invalid integer literal '{0}'";
  2108. public const string InvalidRealLiteral = "Invalid real literal '{0}'";
  2109. public const string UnknownIdentifier = "Unknown identifier '{0}'";
  2110. public const string NoItInScope = "No 'it' is in scope";
  2111. public const string IifRequiresThreeArgs = "The 'iif' function requires three arguments";
  2112. public const string FirstExprMustBeBool = "The first expression must be of type 'Boolean'";
  2113. public const string BothTypesConvertToOther = "Both of the types '{0}' and '{1}' convert to the other";
  2114. public const string NeitherTypeConvertsToOther = "Neither of the types '{0}' and '{1}' converts to the other";
  2115. public const string MissingAsClause = "Expression is missing an 'as' clause";
  2116. public const string ArgsIncompatibleWithLambda = "Argument list incompatible with lambda expression";
  2117. public const string TypeHasNoNullableForm = "Type '{0}' has no nullable form";
  2118. public const string NoMatchingConstructor = "No matching constructor in type '{0}'";
  2119. public const string AmbiguousConstructorInvocation = "Ambiguous invocation of '{0}' constructor";
  2120. public const string CannotConvertValue = "A value of type '{0}' cannot be converted to type '{1}'";
  2121. public const string NoApplicableMethod = "No applicable method '{0}' exists in type '{1}'";
  2122. public const string MethodsAreInaccessible = "Methods on type '{0}' are not accessible";
  2123. public const string MethodIsVoid = "Method '{0}' in type '{1}' does not return a value";
  2124. public const string AmbiguousMethodInvocation = "Ambiguous invocation of method '{0}' in type '{1}'";
  2125. public const string UnknownPropertyOrField = "No property or field '{0}' exists in type '{1}'";
  2126. public const string NoApplicableAggregate = "No applicable aggregate method '{0}' exists";
  2127. public const string CannotIndexMultiDimArray = "Indexing of multi-dimensional arrays is not supported";
  2128. public const string InvalidIndex = "Array index must be an integer expression";
  2129. public const string NoApplicableIndexer = "No applicable indexer exists in type '{0}'";
  2130. public const string AmbiguousIndexerInvocation = "Ambiguous invocation of indexer in type '{0}'";
  2131. public const string IncompatibleOperand = "Operator '{0}' incompatible with operand type '{1}'";
  2132. public const string IncompatibleOperands = "Operator '{0}' incompatible with operand types '{1}' and '{2}'";
  2133. public const string UnterminatedStringLiteral = "Unterminated string literal";
  2134. public const string InvalidCharacter = "Syntax error '{0}'";
  2135. public const string DigitExpected = "Digit expected";
  2136. public const string SyntaxError = "Syntax error";
  2137. public const string TokenExpected = "{0} expected";
  2138. public const string ParseExceptionFormat = "{0} (at index {1})";
  2139. public const string ColonExpected = "':' expected";
  2140. public const string OpenParenExpected = "'(' expected";
  2141. public const string CloseParenOrOperatorExpected = "')' or operator expected";
  2142. public const string CloseParenOrCommaExpected = "')' or ',' expected";
  2143. public const string DotOrOpenParenExpected = "'.' or '(' expected";
  2144. public const string OpenBracketExpected = "'[' expected";
  2145. public const string CloseBracketOrCommaExpected = "']' or ',' expected";
  2146. public const string IdentifierExpected = "Identifier expected";
  2147. }
  2148. }
  2149. /* Test Code
  2150. namespace Dynamic
  2151. {
  2152. class Program
  2153. {
  2154. static void Main(string[] args)
  2155. {
  2156. // For this sample to work, you need an active database server or SqlExpress.
  2157. // Here is a connection to the Data sample project that ships with Microsoft Visual Studio 2008.
  2158. string dbPath = Path.GetFullPath(Path.Combine(Application.StartupPath, @"..\..\..\..\Data\NORTHWND.MDF"));
  2159. string sqlServerInstance = @".\SQLEXPRESS";
  2160. string connString = "AttachDBFileName='" + dbPath + "';Server='" + sqlServerInstance + "';user instance=true;Integrated Security=SSPI;Connection Timeout=60";
  2161. // Here is an alternate connect string that you can modify for your own purposes.
  2162. // string connString = "server=test;database=northwind;user id=test;password=test";
  2163. Northwind db = new Northwind(connString);
  2164. db.Log = Console.Out;
  2165. var query =
  2166. db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
  2167. OrderBy("CompanyName").
  2168. Select("New(CompanyName as Name, Phone)");
  2169. Console.WriteLine(query);
  2170. Console.ReadLine();
  2171. }
  2172. }
  2173. }
  2174. }*/