ExpressionPrinter.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. throw new NotImplementedException ();
  79. }
  80. static string OperatorToString (BinaryExpression binary)
  81. {
  82. switch (binary.NodeType) {
  83. case ExpressionType.Add:
  84. case ExpressionType.AddChecked:
  85. return "+";
  86. case ExpressionType.AndAlso:
  87. return "&&";
  88. case ExpressionType.Coalesce:
  89. return "??";
  90. case ExpressionType.Divide:
  91. return "/";
  92. case ExpressionType.Equal:
  93. return "==";
  94. case ExpressionType.ExclusiveOr:
  95. return "^";
  96. case ExpressionType.GreaterThan:
  97. return ">";
  98. case ExpressionType.GreaterThanOrEqual:
  99. return ">=";
  100. case ExpressionType.LeftShift:
  101. return "<<";
  102. case ExpressionType.LessThan:
  103. return "<";
  104. case ExpressionType.LessThanOrEqual:
  105. return "<=";
  106. case ExpressionType.Modulo:
  107. return "%";
  108. case ExpressionType.Multiply:
  109. case ExpressionType.MultiplyChecked:
  110. return "*";
  111. case ExpressionType.NotEqual:
  112. return "!=";
  113. case ExpressionType.OrElse:
  114. return "||";
  115. case ExpressionType.Power:
  116. return "^";
  117. case ExpressionType.RightShift:
  118. return ">>";
  119. case ExpressionType.Subtract:
  120. case ExpressionType.SubtractChecked:
  121. return "-";
  122. case ExpressionType.And:
  123. return IsBoolean (binary) ? "And" : "&";
  124. case ExpressionType.Or:
  125. return IsBoolean (binary) ? "Or" : "|";
  126. default:
  127. return null;
  128. }
  129. }
  130. static bool IsBoolean (Expression expression)
  131. {
  132. return expression.Type == typeof (bool) || expression.Type == typeof (bool?);
  133. }
  134. protected override void VisitBinary (BinaryExpression binary)
  135. {
  136. throw new NotImplementedException ();
  137. }
  138. protected override void VisitTypeIs (TypeBinaryExpression type)
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. protected override void VisitConstant (ConstantExpression constant)
  143. {
  144. var value = constant.Value;
  145. if (value == null) {
  146. Print ("null");
  147. } else if (value is string) {
  148. Print ("\"");
  149. Print (value);
  150. Print ("\"");
  151. } else if (!HasStringRepresentation (value)) {
  152. Print ("value(");
  153. Print (value);
  154. Print (")");
  155. } else
  156. Print (value);
  157. }
  158. static bool HasStringRepresentation (object obj)
  159. {
  160. return obj.ToString () != obj.GetType ().ToString ();
  161. }
  162. protected override void VisitConditional (ConditionalExpression conditional)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. protected override void VisitParameter (ParameterExpression parameter)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. protected override void VisitMemberAccess (MemberExpression member)
  171. {
  172. throw new NotImplementedException ();
  173. }
  174. protected override void VisitMethodCall (MethodCallExpression methodCall)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. protected override void VisitExpressionList (ReadOnlyCollection<Expression> list)
  179. {
  180. throw new NotImplementedException ();
  181. }
  182. protected override void VisitMemberAssignment (MemberAssignment assignment)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. protected override void VisitMemberMemberBinding (MemberMemberBinding binding)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. protected override void VisitMemberListBinding (MemberListBinding binding)
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. protected override void VisitList<T> (ReadOnlyCollection<T> list, Action<T> visitor)
  195. {
  196. for (int i = 0; i < list.Count; i++) {
  197. if (i > 0)
  198. Print (", ");
  199. visitor (list [i]);
  200. }
  201. }
  202. protected override void VisitLambda (LambdaExpression lambda)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. protected override void VisitNew (NewExpression nex)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. protected override void VisitMemberInit (MemberInitExpression init)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. protected override void VisitListInit (ListInitExpression init)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. protected override void VisitNewArray (NewArrayExpression newArray)
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. protected override void VisitInvocation (InvocationExpression invocation)
  223. {
  224. throw new NotImplementedException ();
  225. }
  226. }
  227. }