ExpressionPrinter.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // ExpressionPrinter.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2008 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.Text;
  32. namespace System.Linq.Expressions {
  33. class ExpressionPrinter : ExpressionVisitor {
  34. StringBuilder builder;
  35. ExpressionPrinter (StringBuilder builder)
  36. {
  37. this.builder = builder;
  38. }
  39. ExpressionPrinter () : this (new StringBuilder ())
  40. {
  41. }
  42. public static string ToString (Expression expression)
  43. {
  44. var printer = new ExpressionPrinter ();
  45. printer.Visit (expression);
  46. return printer.builder.ToString ();
  47. }
  48. public static string ToString (ElementInit init)
  49. {
  50. var printer = new ExpressionPrinter ();
  51. printer.VisitElementInitializer (init);
  52. return printer.builder.ToString ();
  53. }
  54. public static string ToString (MemberBinding binding)
  55. {
  56. var printer = new ExpressionPrinter ();
  57. printer.VisitBinding (binding);
  58. return printer.builder.ToString ();
  59. }
  60. void Print (string str)
  61. {
  62. builder.Append (str);
  63. }
  64. void Print (object obj)
  65. {
  66. builder.Append (obj);
  67. }
  68. void Print (string str, params object [] objs)
  69. {
  70. builder.AppendFormat (str, objs);
  71. }
  72. protected override void VisitElementInitializer (ElementInit initializer)
  73. {
  74. Print (initializer.AddMethod);
  75. Print ("(");
  76. VisitExpressionList (initializer.Arguments);
  77. Print (")");
  78. }
  79. protected override void VisitUnary (UnaryExpression unary)
  80. {
  81. switch (unary.NodeType) {
  82. case ExpressionType.ArrayLength:
  83. Print ("{0}(", unary.NodeType);
  84. Visit (unary.Operand);
  85. Print (")");
  86. return;
  87. case ExpressionType.Negate:
  88. Print ("-");
  89. Visit (unary.Operand);
  90. return;
  91. case ExpressionType.Not:
  92. Print ("Not(");
  93. Visit (unary.Operand);
  94. Print (")");
  95. return;
  96. case ExpressionType.Quote:
  97. Visit (unary.Operand);
  98. return;
  99. case ExpressionType.TypeAs:
  100. Print ("(");
  101. Visit (unary.Operand);
  102. Print (" As {0})", unary.Type.Name);
  103. return;
  104. case ExpressionType.UnaryPlus:
  105. Print ("+");
  106. Visit (unary.Operand);
  107. return;
  108. }
  109. throw new NotImplementedException ();
  110. }
  111. static string OperatorToString (BinaryExpression binary)
  112. {
  113. switch (binary.NodeType) {
  114. case ExpressionType.Add:
  115. case ExpressionType.AddChecked:
  116. return "+";
  117. case ExpressionType.AndAlso:
  118. return "&&";
  119. case ExpressionType.Coalesce:
  120. return "??";
  121. case ExpressionType.Divide:
  122. return "/";
  123. case ExpressionType.Equal:
  124. return "=";
  125. case ExpressionType.ExclusiveOr:
  126. return "^";
  127. case ExpressionType.GreaterThan:
  128. return ">";
  129. case ExpressionType.GreaterThanOrEqual:
  130. return ">=";
  131. case ExpressionType.LeftShift:
  132. return "<<";
  133. case ExpressionType.LessThan:
  134. return "<";
  135. case ExpressionType.LessThanOrEqual:
  136. return "<=";
  137. case ExpressionType.Modulo:
  138. return "%";
  139. case ExpressionType.Multiply:
  140. case ExpressionType.MultiplyChecked:
  141. return "*";
  142. case ExpressionType.NotEqual:
  143. return "!=";
  144. case ExpressionType.OrElse:
  145. return "||";
  146. case ExpressionType.Power:
  147. return "^";
  148. case ExpressionType.RightShift:
  149. return ">>";
  150. case ExpressionType.Subtract:
  151. case ExpressionType.SubtractChecked:
  152. return "-";
  153. case ExpressionType.And:
  154. return IsBoolean (binary) ? "And" : "&";
  155. case ExpressionType.Or:
  156. return IsBoolean (binary) ? "Or" : "|";
  157. default:
  158. return null;
  159. }
  160. }
  161. static bool IsBoolean (Expression expression)
  162. {
  163. return expression.Type == typeof (bool) || expression.Type == typeof (bool?);
  164. }
  165. protected override void VisitBinary (BinaryExpression binary)
  166. {
  167. if (binary.NodeType == ExpressionType.ArrayIndex) {
  168. Visit (binary.Left);
  169. Print ("[");
  170. Visit (binary.Right);
  171. Print ("]");
  172. return;
  173. }
  174. Print ("(");
  175. Visit (binary.Left);
  176. Print (" {0} ", OperatorToString (binary));
  177. Visit (binary.Right);
  178. Print (")");
  179. }
  180. protected override void VisitTypeIs (TypeBinaryExpression type)
  181. {
  182. switch (type.NodeType) {
  183. case ExpressionType.TypeIs:
  184. Print ("(");
  185. Visit (type.Expression);
  186. Print (" Is {0})", type.TypeOperand.Name);
  187. return;
  188. }
  189. throw new NotImplementedException ();
  190. }
  191. protected override void VisitConstant (ConstantExpression constant)
  192. {
  193. var value = constant.Value;
  194. if (value == null) {
  195. Print ("null");
  196. } else if (value is string) {
  197. Print ("\"");
  198. Print (value);
  199. Print ("\"");
  200. } else if (!HasStringRepresentation (value)) {
  201. Print ("value(");
  202. Print (value);
  203. Print (")");
  204. } else
  205. Print (value);
  206. }
  207. static bool HasStringRepresentation (object obj)
  208. {
  209. return obj.ToString () != obj.GetType ().ToString ();
  210. }
  211. protected override void VisitConditional (ConditionalExpression conditional)
  212. {
  213. Print ("IIF(");
  214. Visit (conditional.Test);
  215. Print (", ");
  216. Visit (conditional.IfTrue);
  217. Print (", ");
  218. Visit (conditional.IfFalse);
  219. Print (")");
  220. }
  221. protected override void VisitParameter (ParameterExpression parameter)
  222. {
  223. Print (parameter.Name ?? "<param>");
  224. }
  225. protected override void VisitMemberAccess (MemberExpression access)
  226. {
  227. if (access.Expression == null)
  228. Print (access.Member.DeclaringType.Name);
  229. else
  230. Visit (access.Expression);
  231. Print (".{0}", access.Member.Name);
  232. }
  233. protected override void VisitMethodCall (MethodCallExpression call)
  234. {
  235. if (call.Object != null) {
  236. Visit (call.Object);
  237. Print (".");
  238. }
  239. Print (call.Method.Name);
  240. Print ("(");
  241. VisitExpressionList (call.Arguments);
  242. Print (")");
  243. }
  244. protected override void VisitMemberAssignment (MemberAssignment assignment)
  245. {
  246. Print ("{0} = ", assignment.Member.Name);
  247. Visit (assignment.Expression);
  248. }
  249. protected override void VisitMemberMemberBinding (MemberMemberBinding binding)
  250. {
  251. throw new NotImplementedException ();
  252. }
  253. protected override void VisitMemberListBinding (MemberListBinding binding)
  254. {
  255. Print (binding.Member.Name);
  256. Print (" = {");
  257. // replace when the patch to the visitor is in
  258. // VisitElementInitializerList (binding.Initializers);
  259. VisitList (binding.Initializers, VisitElementInitializer);
  260. Print ("}");
  261. }
  262. protected override void VisitList<T> (ReadOnlyCollection<T> list, Action<T> visitor)
  263. {
  264. for (int i = 0; i < list.Count; i++) {
  265. if (i > 0)
  266. Print (", ");
  267. visitor (list [i]);
  268. }
  269. }
  270. protected override void VisitLambda (LambdaExpression lambda)
  271. {
  272. if (lambda.Parameters.Count != 1) {
  273. Print ("(");
  274. // replace when the patch to the visitor is in
  275. // VisitExpressionList (lambda.Parameters);
  276. VisitList (lambda.Parameters, Visit);
  277. Print (")");
  278. } else
  279. Visit (lambda.Parameters [0]);
  280. Print (" => ");
  281. Visit (lambda.Body);
  282. }
  283. protected override void VisitNew (NewExpression nex)
  284. {
  285. Print ("new {0}(", nex.Type.Name);
  286. VisitExpressionList (nex.Arguments);
  287. Print (")");
  288. }
  289. protected override void VisitMemberInit (MemberInitExpression init)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. protected override void VisitListInit (ListInitExpression init)
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. protected override void VisitNewArray (NewArrayExpression newArray)
  298. {
  299. Print ("new ");
  300. switch (newArray.NodeType) {
  301. case ExpressionType.NewArrayBounds:
  302. Print (newArray.Type);
  303. Print ("(");
  304. VisitExpressionList (newArray.Expressions);
  305. Print (")");
  306. return;
  307. case ExpressionType.NewArrayInit:
  308. Print ("[] {");
  309. VisitExpressionList (newArray.Expressions);
  310. Print ("}");
  311. return;
  312. }
  313. throw new NotSupportedException ();
  314. }
  315. protected override void VisitInvocation (InvocationExpression invocation)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. }
  320. }