ExpressionPrinter.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. Print ("(");
  152. Visit (binary.Left);
  153. Print (" {0} ", OperatorToString (binary));
  154. Visit (binary.Right);
  155. Print (")");
  156. }
  157. protected override void VisitTypeIs (TypeBinaryExpression type)
  158. {
  159. switch (type.NodeType) {
  160. case ExpressionType.TypeIs:
  161. Print ("(");
  162. Visit (type.Expression);
  163. Print (" Is {0})", type.TypeOperand.Name);
  164. return;
  165. }
  166. throw new NotImplementedException ();
  167. }
  168. protected override void VisitConstant (ConstantExpression constant)
  169. {
  170. var value = constant.Value;
  171. if (value == null) {
  172. Print ("null");
  173. } else if (value is string) {
  174. Print ("\"");
  175. Print (value);
  176. Print ("\"");
  177. } else if (!HasStringRepresentation (value)) {
  178. Print ("value(");
  179. Print (value);
  180. Print (")");
  181. } else
  182. Print (value);
  183. }
  184. static bool HasStringRepresentation (object obj)
  185. {
  186. return obj.ToString () != obj.GetType ().ToString ();
  187. }
  188. protected override void VisitConditional (ConditionalExpression conditional)
  189. {
  190. Print ("IIF(");
  191. Visit (conditional.Test);
  192. Print (", ");
  193. Visit (conditional.IfTrue);
  194. Print (", ");
  195. Visit (conditional.IfFalse);
  196. Print (")");
  197. }
  198. protected override void VisitParameter (ParameterExpression parameter)
  199. {
  200. Print (parameter.Name ?? "<param>");
  201. }
  202. protected override void VisitMemberAccess (MemberExpression member)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. protected override void VisitMethodCall (MethodCallExpression methodCall)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. protected override void VisitExpressionList (ReadOnlyCollection<Expression> list)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. protected override void VisitMemberAssignment (MemberAssignment assignment)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. protected override void VisitMemberMemberBinding (MemberMemberBinding binding)
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. protected override void VisitMemberListBinding (MemberListBinding binding)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. protected override void VisitList<T> (ReadOnlyCollection<T> list, Action<T> visitor)
  227. {
  228. for (int i = 0; i < list.Count; i++) {
  229. if (i > 0)
  230. Print (", ");
  231. visitor (list [i]);
  232. }
  233. }
  234. protected override void VisitLambda (LambdaExpression lambda)
  235. {
  236. throw new NotImplementedException ();
  237. }
  238. protected override void VisitNew (NewExpression nex)
  239. {
  240. throw new NotImplementedException ();
  241. }
  242. protected override void VisitMemberInit (MemberInitExpression init)
  243. {
  244. throw new NotImplementedException ();
  245. }
  246. protected override void VisitListInit (ListInitExpression init)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. protected override void VisitNewArray (NewArrayExpression newArray)
  251. {
  252. throw new NotImplementedException ();
  253. }
  254. protected override void VisitInvocation (InvocationExpression invocation)
  255. {
  256. throw new NotImplementedException ();
  257. }
  258. }
  259. }