ExpressionPrinter.cs 7.4 KB

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