-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ExpressionPrettyPrinter.cs
439 lines (403 loc) · 14.3 KB
/
ExpressionPrettyPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace MongoDB.Driver.Linq
{
/// <summary>
/// A class that pretty prints an Expression.
/// </summary>
public class ExpressionPrettyPrinter : ExpressionVisitor
{
// private fields
private StringBuilder _sb;
private string _indentation = "";
// constructors
/// <summary>
/// Initializes a new instance of the ExpressionPrettyPrinter class.
/// </summary>
public ExpressionPrettyPrinter()
{
_sb = new StringBuilder();
_indentation = "";
}
// public methods
/// <summary>
/// Pretty prints an Expression.
/// </summary>
/// <param name="node">The Expression to pretty print.</param>
/// <returns>A string containing the pretty printed Expression.</returns>
public static string PrettyPrint(Expression node)
{
var prettyPrinter = new ExpressionPrettyPrinter();
prettyPrinter.Visit(node);
return prettyPrinter.ToString();
}
/// <summary>
/// Returns the pretty printed string representation of the Expression.
/// </summary>
/// <returns>The pretty printed string representation of the Expression.</returns>
public override string ToString()
{
return _sb.ToString();
}
// protected methods
/// <summary>
/// Visits a BinaryExpression.
/// </summary>
/// <param name="node">The BinaryExpression.</param>
/// <returns>The BinaryExpression.</returns>
protected override Expression VisitBinary(BinaryExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Method={0}", node.Method == null ? "null" : node.Method.Name);
WriteLine("Left:");
VisitIndented(node.Left);
WriteLine("Right:");
VisitIndented(node.Right);
}
return node;
}
/// <summary>
/// Visits a ConditionalExpression.
/// </summary>
/// <param name="node">The ConditionalExpression.</param>
/// <returns>The ConditionalExpression.</returns>
protected override Expression VisitConditional(ConditionalExpression node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a ConstantExpression.
/// </summary>
/// <param name="node">The ConstantExpression.</param>
/// <returns>The ConstantExpression.</returns>
protected override Expression VisitConstant(ConstantExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Value={0}", node.Value);
}
return node;
}
/// <summary>
/// Visits an ElementInit node.
/// </summary>
/// <param name="node">The ElementInit node.</param>
/// <returns>The ElementInit node.</returns>
protected override ElementInit VisitElementInit(ElementInit node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits an ElementInit list.
/// </summary>
/// <param name="nodes">The ElementInit list.</param>
/// <returns>The ElementInit list.</returns>
protected override IEnumerable<ElementInit> VisitElementInitList(
ReadOnlyCollection<ElementInit> nodes)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits an InvocationExpression.
/// </summary>
/// <param name="node">The InvocationExpression.</param>
/// <returns>The InvocationExpression.</returns>
protected override Expression VisitInvocation(InvocationExpression node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a LambdaExpression.
/// </summary>
/// <param name="node">The LambdaExpression.</param>
/// <returns>The LambdaExpression.</returns>
protected override Expression VisitLambda(LambdaExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Parameters:");
foreach (var parameter in node.Parameters)
{
VisitIndented(parameter);
}
WriteLine("Body:");
VisitIndented(node.Body);
}
return node;
}
/// <summary>
/// Visits a ListInitExpression.
/// </summary>
/// <param name="node">The ListInitExpression.</param>
/// <returns>The ListInitExpression.</returns>
protected override Expression VisitListInit(ListInitExpression node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberExpression.
/// </summary>
/// <param name="node">The MemberExpression.</param>
/// <returns>The MemberExpression.</returns>
protected override Expression VisitMember(MemberExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Expression:");
VisitIndented(node.Expression);
WriteLine("Member={0} {1}", node.Member.MemberType, node.Member.Name);
}
return node;
}
/// <summary>
/// Visits a MemberAssignment.
/// </summary>
/// <param name="node">The MemberAssignment.</param>
/// <returns>The MemberAssignment.</returns>
protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberBinding.
/// </summary>
/// <param name="node">The MemberBinding.</param>
/// <returns>The MemberBinding (possibly modified).</returns>
protected override MemberBinding VisitMemberBinding(MemberBinding node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberBinding list.
/// </summary>
/// <param name="nodes">The MemberBinding list.</param>
/// <returns>The MemberBinding list.</returns>
protected override IEnumerable<MemberBinding> VisitMemberBindingList(ReadOnlyCollection<MemberBinding> nodes)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberInitExpression.
/// </summary>
/// <param name="node">The MemberInitExpression.</param>
/// <returns>The MemberInitExpression.</returns>
protected override Expression VisitMemberInit(MemberInitExpression node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberListBinding.
/// </summary>
/// <param name="node">The MemberListBinding.</param>
/// <returns>The MemberListBinding.</returns>
protected override MemberListBinding VisitMemberListBinding(MemberListBinding node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MemberMemberBinding.
/// </summary>
/// <param name="node">The MemberMemberBinding.</param>
/// <returns>The MemberMemberBinding.</returns>
protected override MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a MethodCallExpression.
/// </summary>
/// <param name="node">The MethodCallExpression.</param>
/// <returns>The MethodCallExpression.</returns>
protected override Expression VisitMethodCall(MethodCallExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Method={0}", node.Method.Name);
if (node.Object == null)
{
WriteLine("Object=null");
}
else
{
WriteLine("Object:");
VisitIndented(node.Object);
}
WriteLine("Arguments:");
using (new Indentation(this))
{
foreach (var arg in node.Arguments)
{
Visit(arg);
}
}
}
return node;
}
/// <summary>
/// Visits a NewExpression.
/// </summary>
/// <param name="node">The NewExpression.</param>
/// <returns>The NewExpression.</returns>
protected override NewExpression VisitNew(NewExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Arguments:");
foreach (var arg in node.Arguments)
{
VisitIndented(arg);
}
}
return node;
}
/// <summary>
/// Visits a NewArrayExpression.
/// </summary>
/// <param name="node">The NewArrayExpression.</param>
/// <returns>The NewArrayExpression.</returns>
protected override Expression VisitNewArray(NewArrayExpression node)
{
throw new NotImplementedException();
}
/// <summary>
/// Visits a ParameterExpression.
/// </summary>
/// <param name="node">The ParameterExpression.</param>
/// <returns>The ParameterExpression.</returns>
protected override Expression VisitParameter(ParameterExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Name={0}", node.Name);
WriteLine("Type={0}", FriendlyClassName(node.Type));
}
return node;
}
/// <summary>
/// Visits a TypeBinaryExpression.
/// </summary>
/// <param name="node">The TypeBinaryExpression.</param>
/// <returns>The TypeBinaryExpression.</returns>
protected override Expression VisitTypeBinary(TypeBinaryExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("TypeOperand={0}", FriendlyClassName(node.TypeOperand));
WriteLine("Expression:");
VisitIndented(node.Expression);
}
return node;
}
/// <summary>
/// Visits a UnaryExpression.
/// </summary>
/// <param name="node">The UnaryExpression.</param>
/// <returns>The UnaryExpression.</returns>
protected override Expression VisitUnary(UnaryExpression node)
{
WriteHeader(node);
using (new Indentation(this))
{
WriteLine("Method={0}", node.Method == null ? "null" : node.Method.Name);
WriteLine("Operand:");
VisitIndented(node.Operand);
}
return node;
}
// private methods
private string FriendlyClassName(Type type)
{
var typeInfo = type.GetTypeInfo();
if (!typeInfo.IsGenericType)
{
return typeInfo.Name;
}
var sb = new StringBuilder();
sb.AppendFormat("{0}<", Regex.Replace(typeInfo.Name, @"\`\d+$", ""));
foreach (var typeParameter in typeInfo.GetGenericArguments())
{
sb.AppendFormat("{0}, ", FriendlyClassName(typeParameter));
}
sb.Remove(sb.Length - 2, 2);
sb.Append(">");
return sb.ToString();
}
private string PublicClassName(Type type)
{
var typeInfo = type.GetTypeInfo();
while (!typeInfo.IsPublic)
{
type = typeInfo.BaseType;
}
return FriendlyClassName(type);
}
private void VisitIndented(Expression node)
{
using (new Indentation(this))
{
Visit(node);
}
}
private void WriteHeader(Expression node)
{
WriteLine("{0}:{1} Type={2}", PublicClassName(node.GetType()), node.NodeType, FriendlyClassName(node.Type));
}
private void WriteLine(string line)
{
_sb.Append(_indentation);
_sb.Append(line);
_sb.AppendLine();
}
private void WriteLine(string format, params object[] args)
{
_sb.Append(_indentation);
_sb.AppendFormat(format, args);
_sb.AppendLine();
}
// nested classes
internal class Indentation : IDisposable
{
// private fields
private ExpressionPrettyPrinter _prettyPrinter;
// constructors
public Indentation(ExpressionPrettyPrinter prettyPrinter)
{
_prettyPrinter = prettyPrinter;
prettyPrinter._indentation += "| ";
}
// public methods
public void Dispose()
{
_prettyPrinter._indentation = _prettyPrinter._indentation.Remove(_prettyPrinter._indentation.Length - 2);
}
}
}
}