ExpressionPrinter.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. throw new NotImplementedException ();
  75. }
  76. protected override void VisitUnary (UnaryExpression unary)
  77. {
  78. switch (unary.NodeType) {
  79. case ExpressionType.ArrayLength:
  80. Print ("{0}(", unary.NodeType);
  81. Visit (unary.Operand);
  82. Print (")");
  83. return;
  84. case ExpressionType.Negate:
  85. Print ("-");
  86. Visit (unary.Operand);
  87. return;
  88. case ExpressionType.Not:
  89. Print ("Not(");
  90. Visit (unary.Operand);
  91. Print (")");
  92. return;
  93. case ExpressionType.Quote:
  94. Visit (unary.Operand);
  95. return;
  96. case ExpressionType.TypeAs:
  97. Print ("(");
  98. Visit (unary.Operand);
  99. Print (" As {0})", unary.Type.Name);
  100. return;
  101. case ExpressionType.UnaryPlus:
  102. Print ("+");
  103. Visit (unary.Operand);
  104. return;
  105. }
  106. throw new NotImplementedException ();
  107. }
  108. static string OperatorToString (BinaryExpression binary)
  109. {
  110. switch (binary.NodeType) {
  111. case ExpressionType.Add:
  112. case ExpressionType.AddChecked:
  113. return "+";
  114. case ExpressionType.AndAlso:
  115. return "&&";
  116. case ExpressionType.Coalesce:
  117. return "??";
  118. case ExpressionType.Divide:
  119. return "/";
  120. case ExpressionType.Equal:
  121. return "=";
  122. case ExpressionType.ExclusiveOr:
  123. return "^";
  124. case ExpressionType.GreaterThan:
  125. return ">";
  126. case ExpressionType.GreaterThanOrEqual:
  127. return ">=";
  128. case ExpressionType.LeftShift:
  129. return "<<";
  130. case ExpressionType.LessThan:
  131. return "<";
  132. case ExpressionType.LessThanOrEqual:
  133. return "<=";
  134. case ExpressionType.Modulo:
  135. return "%";
  136. case ExpressionType.Multiply:
  137. case ExpressionType.MultiplyChecked:
  138. return "*";
  139. case ExpressionType.NotEqual:
  140. return "!=";
  141. case ExpressionType.OrElse:
  142. return "||";
  143. case ExpressionType.Power:
  144. return "^";
  145. case ExpressionType.RightShift:
  146. return ">>";
  147. case ExpressionType.Subtract:
  148. case ExpressionType.SubtractChecked:
  149. return "-";
  150. case ExpressionType.And:
  151. return IsBoolean (binary) ? "And" : "&";
  152. case ExpressionType.Or:
  153. return IsBoolean (binary) ? "Or" : "|";
  154. default:
  155. return null;
  156. }
  157. }
  158. static bool IsBoolean (Expression expression)
  159. {
  160. return expression.Type == typeof (bool) || expression.Type == typeof (bool?);
  161. }
  162. protected override void VisitBinary (BinaryExpression binary)
  163. {
  164. if (binary.NodeType == ExpressionType.ArrayIndex) {
  165. Visit (binary.Left);
  166. Print ("[");
  167. Visit (binary.Right);
  168. Print ("]");
  169. return;
  170. }
  171. Print ("(");
  172. Visit (binary.Left);
  173. Print (" {0} ", OperatorToString (binary));
  174. Visit (binary.Right);
  175. Print (")");
  176. }
  177. protected override void VisitTypeIs (TypeBinaryExpression type)
  178. {
  179. switch (type.NodeType) {
  180. case ExpressionType.TypeIs:
  181. Print ("(");
  182. Visit (type.Expression);
  183. Print (" Is {0})", type.TypeOperand.Name);
  184. return;
  185. }
  186. throw new NotImplementedException ();
  187. }
  188. protected override void VisitConstant (ConstantExpression constant)
  189. {
  190. var value = constant.Value;
  191. if (value == null) {
  192. Print ("null");
  193. } else if (value is string) {
  194. Print ("\"");
  195. Print (value);
  196. Print ("\"");
  197. } else if (!HasStringRepresentation (value)) {
  198. Print ("value(");
  199. Print (value);
  200. Print (")");
  201. } else
  202. Print (value);
  203. }
  204. static bool HasStringRepresentation (object obj)
  205. {
  206. return obj.ToString () != obj.GetType ().ToString ();
  207. }
  208. protected override void VisitConditional (ConditionalExpression conditional)
  209. {
  210. Print ("IIF(");
  211. Visit (conditional.Test);
  212. Print (", ");
  213. Visit (conditional.IfTrue);
  214. Print (", ");
  215. Visit (conditional.IfFalse);
  216. Print (")");
  217. }
  218. protected override void VisitParameter (ParameterExpression parameter)
  219. {
  220. Print (parameter.Name ?? "<param>");
  221. }
  222. protected override void VisitMemberAccess (MemberExpression access)
  223. {
  224. if (access.Expression == null)
  225. Print (access.Member.DeclaringType.Name);
  226. else
  227. Visit (access.Expression);
  228. Print (".{0}", access.Member.Name);
  229. }
  230. protected override void VisitMethodCall (MethodCallExpression call)
  231. {
  232. if (call.Object != null) {
  233. Visit (call.Object);
  234. Print (".");
  235. }
  236. Print (call.Method.Name);
  237. Print ("(");
  238. VisitExpressionList (call.Arguments);
  239. Print (")");
  240. }
  241. protected override void VisitMemberAssignment (MemberAssignment assignment)
  242. {
  243. Print ("{0} = ", assignment.Member.Name);
  244. Visit (assignment.Expression);
  245. }
  246. protected override void VisitMemberMemberBinding (MemberMemberBinding binding)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. protected override void VisitMemberListBinding (MemberListBinding binding)
  251. {
  252. throw new NotImplementedException ();
  253. }
  254. protected override void VisitList<T> (ReadOnlyCollection<T> list, Action<T> visitor)
  255. {
  256. for (int i = 0; i < list.Count; i++) {
  257. if (i > 0)
  258. Print (", ");
  259. visitor (list [i]);
  260. }
  261. }
  262. protected override void VisitLambda (LambdaExpression lambda)
  263. {
  264. if (lambda.Parameters.Count != 1) {
  265. Print ("(");
  266. // replace when the patch to the visitor is in
  267. // VisitExpressionList (lambda.Parameters);
  268. VisitList (lambda.Parameters, Visit);
  269. Print (")");
  270. } else
  271. Visit (lambda.Parameters [0]);
  272. Print (" => ");
  273. Visit (lambda.Body);
  274. }
  275. protected override void VisitNew (NewExpression nex)
  276. {
  277. Print ("new {0}(", nex.Type.Name);
  278. VisitExpressionList (nex.Arguments);
  279. Print (")");
  280. }
  281. protected override void VisitMemberInit (MemberInitExpression init)
  282. {
  283. throw new NotImplementedException ();
  284. }
  285. protected override void VisitListInit (ListInitExpression init)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. protected override void VisitNewArray (NewArrayExpression newArray)
  290. {
  291. Print ("new ");
  292. switch (newArray.NodeType) {
  293. case ExpressionType.NewArrayBounds:
  294. Print (newArray.Type);
  295. Print ("(");
  296. VisitExpressionList (newArray.Expressions);
  297. Print (")");
  298. return;
  299. case ExpressionType.NewArrayInit:
  300. Print ("[] {");
  301. VisitExpressionList (newArray.Expressions);
  302. Print ("}");
  303. return;
  304. }
  305. throw new NotSupportedException ();
  306. }
  307. protected override void VisitInvocation (InvocationExpression invocation)
  308. {
  309. throw new NotImplementedException ();
  310. }
  311. }
  312. }