DynamicQueryable.cs 84 KB

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