~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// <file>
2
 
//     <copyright see="prj:///doc/copyright.txt"/>
3
 
//     <license see="prj:///doc/license.txt"/>
4
 
//     <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
5
 
//     <version>$Revision: 4741 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Linq;
10
 
using System.Collections;
11
 
using System.Collections.Generic;
12
 
using System.Diagnostics;
13
 
using System.Globalization;
14
 
using System.Text;
15
 
 
16
 
using ICSharpCode.OldNRefactory.Ast;
17
 
using ICSharpCode.OldNRefactory.Parser;
18
 
using ICSharpCode.OldNRefactory.Parser.CSharp;
19
 
using ICSharpCode.OldNRefactory.Visitors;
20
 
 
21
 
namespace ICSharpCode.OldNRefactory.PrettyPrinter
22
 
{
23
 
        public sealed class CSharpOutputVisitor : NodeTrackingAstVisitor, IOutputAstVisitor
24
 
        {
25
 
                Errors                errors             = new Errors();
26
 
                CSharpOutputFormatter outputFormatter;
27
 
                PrettyPrintOptions    prettyPrintOptions = new PrettyPrintOptions();
28
 
                
29
 
                public string Text {
30
 
                        get {
31
 
                                return outputFormatter.Text;
32
 
                        }
33
 
                }
34
 
                
35
 
                public Errors Errors {
36
 
                        get {
37
 
                                return errors;
38
 
                        }
39
 
                }
40
 
                
41
 
                AbstractPrettyPrintOptions IOutputAstVisitor.Options {
42
 
                        get { return prettyPrintOptions; }
43
 
                }
44
 
                
45
 
                public PrettyPrintOptions Options {
46
 
                        get { return prettyPrintOptions; }
47
 
                        set {
48
 
                                if (value == null)
49
 
                                        throw new ArgumentNullException();
50
 
                                prettyPrintOptions = value;
51
 
                        }
52
 
                }
53
 
                
54
 
                public IOutputFormatter OutputFormatter {
55
 
                        get {
56
 
                                return outputFormatter;
57
 
                        }
58
 
                }
59
 
                
60
 
                public CSharpOutputVisitor()
61
 
                {
62
 
                        outputFormatter = new CSharpOutputFormatter(prettyPrintOptions);
63
 
                }
64
 
                
65
 
                public event Action<INode> BeforeNodeVisit;
66
 
                public event Action<INode> AfterNodeVisit;
67
 
                
68
 
                protected override void BeginVisit(INode node)
69
 
                {
70
 
                        if (BeforeNodeVisit != null) {
71
 
                                BeforeNodeVisit(node);
72
 
                        }
73
 
                        base.BeginVisit(node);
74
 
                }
75
 
                
76
 
                protected override void EndVisit(INode node)
77
 
                {
78
 
                        base.EndVisit(node);
79
 
                        if (AfterNodeVisit != null) {
80
 
                                AfterNodeVisit(node);
81
 
                        }
82
 
                }
83
 
                
84
 
                void Error(INode node, string message)
85
 
                {
86
 
                        outputFormatter.PrintText(" // ERROR: " + message + Environment.NewLine);
87
 
                        errors.Error(node.StartLocation.Line, node.StartLocation.Column, message);
88
 
                }
89
 
                
90
 
                void NotSupported(INode node)
91
 
                {
92
 
                        Error(node, "Not supported in C#: " + node.GetType().Name);
93
 
                }
94
 
                
95
 
                #region ICSharpCode.OldNRefactory.Parser.IASTVisitor interface implementation
96
 
                public override object TrackedVisitCompilationUnit(CompilationUnit compilationUnit, object data)
97
 
                {
98
 
                        compilationUnit.AcceptChildren(this, data);
99
 
                        outputFormatter.EndFile();
100
 
                        return null;
101
 
                }
102
 
                
103
 
                /// <summary>
104
 
                /// Converts type name to primitive type name. Returns typeString if typeString is not
105
 
                /// a primitive type.
106
 
                /// </summary>
107
 
                static string ConvertTypeString(string typeString)
108
 
                {
109
 
                        string primitiveType;
110
 
                        if (TypeReference.PrimitiveTypesCSharpReverse.TryGetValue(typeString, out primitiveType))
111
 
                                return primitiveType;
112
 
                        else
113
 
                                return typeString;
114
 
                }
115
 
                
116
 
                void PrintTemplates(List<TemplateDefinition> templates)
117
 
                {
118
 
                        if (templates.Count == 0) return;
119
 
                        outputFormatter.PrintToken(Tokens.LessThan);
120
 
                        for (int i = 0; i < templates.Count; i++) {
121
 
                                if (i > 0) PrintFormattedComma();
122
 
                                outputFormatter.PrintIdentifier(templates[i].Name);
123
 
                        }
124
 
                        outputFormatter.PrintToken(Tokens.GreaterThan);
125
 
                }
126
 
                
127
 
                public override object TrackedVisitTypeReference(TypeReference typeReference, object data)
128
 
                {
129
 
                        if (typeReference == TypeReference.ClassConstraint) {
130
 
                                outputFormatter.PrintToken(Tokens.Class);
131
 
                        } else if (typeReference == TypeReference.StructConstraint) {
132
 
                                outputFormatter.PrintToken(Tokens.Struct);
133
 
                        } else if (typeReference == TypeReference.NewConstraint) {
134
 
                                outputFormatter.PrintToken(Tokens.New);
135
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
136
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
137
 
                        } else {
138
 
                                PrintTypeReferenceWithoutArray(typeReference);
139
 
                                if (typeReference.IsArrayType) {
140
 
                                        PrintArrayRank(typeReference.RankSpecifier, 0);
141
 
                                }
142
 
                        }
143
 
                        return null;
144
 
                }
145
 
                
146
 
                void PrintArrayRank(int[] rankSpecifier, int startRankIndex)
147
 
                {
148
 
                        for (int i = startRankIndex; i < rankSpecifier.Length; ++i) {
149
 
                                outputFormatter.PrintToken(Tokens.OpenSquareBracket);
150
 
                                bool outputSpace = this.prettyPrintOptions.SpacesWithinBrackets && rankSpecifier[i] > 0;
151
 
                                if (outputSpace) {
152
 
                                        outputFormatter.Space();
153
 
                                }
154
 
                                for (int j = 0; j < rankSpecifier[i]; ++j) {
155
 
                                        outputFormatter.PrintToken(Tokens.Comma);
156
 
                                }
157
 
                                if (outputSpace) {
158
 
                                        outputFormatter.Space();
159
 
                                }
160
 
                                outputFormatter.PrintToken(Tokens.CloseSquareBracket);
161
 
                        }
162
 
                }
163
 
                
164
 
                void PrintTypeReferenceWithoutArray(TypeReference typeReference)
165
 
                {
166
 
                        if (typeReference.IsGlobal) {
167
 
                                outputFormatter.PrintText("global::");
168
 
                        }
169
 
                        bool printGenerics = true;
170
 
                        if (typeReference.IsKeyword) {
171
 
                                if (typeReference.Type == "System.Nullable"
172
 
                                    && typeReference.GenericTypes != null
173
 
                                    && typeReference.GenericTypes.Count == 1)
174
 
                                {
175
 
                                        TrackVisit(typeReference.GenericTypes[0], null);
176
 
                                        outputFormatter.PrintText("?");
177
 
                                        printGenerics = false;
178
 
                                } else {
179
 
                                        outputFormatter.PrintText(ConvertTypeString(typeReference.Type));
180
 
                                }
181
 
                        } else {
182
 
                                outputFormatter.PrintIdentifier(typeReference.Type);
183
 
                        }
184
 
                        if (printGenerics && typeReference.GenericTypes != null && typeReference.GenericTypes.Count > 0) {
185
 
                                outputFormatter.PrintToken(Tokens.LessThan);
186
 
                                AppendCommaSeparatedList(typeReference.GenericTypes);
187
 
                                outputFormatter.PrintToken(Tokens.GreaterThan);
188
 
                        }
189
 
                        for (int i = 0; i < typeReference.PointerNestingLevel; ++i) {
190
 
                                outputFormatter.PrintToken(Tokens.Times);
191
 
                        }
192
 
                }
193
 
                
194
 
                public override object TrackedVisitInnerClassTypeReference(InnerClassTypeReference innerClassTypeReference, object data)
195
 
                {
196
 
                        TrackVisit(innerClassTypeReference.BaseType, data);
197
 
                        outputFormatter.PrintToken(Tokens.Dot);
198
 
                        return VisitTypeReference((TypeReference)innerClassTypeReference, data);
199
 
                }
200
 
                
201
 
                #region Global scope
202
 
                void VisitAttributes(ICollection attributes, object data)
203
 
                {
204
 
                        if (attributes == null || attributes.Count <= 0) {
205
 
                                return;
206
 
                        }
207
 
                        foreach (AttributeSection section in attributes) {
208
 
                                TrackVisit(section, data);
209
 
                        }
210
 
                        bool formatSection = true;
211
 
                        if (data is bool)
212
 
                                formatSection = (bool)data;
213
 
                        if (!formatSection)
214
 
                                outputFormatter.Space ();
215
 
                }
216
 
                
217
 
                void PrintFormattedComma()
218
 
                {
219
 
                        if (this.prettyPrintOptions.SpacesBeforeComma) {
220
 
                                outputFormatter.Space();
221
 
                        }
222
 
                        outputFormatter.PrintToken(Tokens.Comma);
223
 
                        if (this.prettyPrintOptions.SpacesAfterComma) {
224
 
                                outputFormatter.Space();
225
 
                        }
226
 
                }
227
 
                void PrintFormattedCommaAndNewLine()
228
 
                {
229
 
                        if (this.prettyPrintOptions.SpacesBeforeComma) {
230
 
                                outputFormatter.Space();
231
 
                        }
232
 
                        outputFormatter.PrintToken(Tokens.Comma);
233
 
                        outputFormatter.NewLine();
234
 
                        outputFormatter.Indent();
235
 
                }
236
 
                
237
 
                public override object TrackedVisitAttributeSection(AttributeSection attributeSection, object data)
238
 
                {
239
 
                        bool formatSection = true;
240
 
                        if (data is bool)
241
 
                                formatSection = (bool)data;
242
 
                        if (formatSection)
243
 
                                outputFormatter.Indent();
244
 
                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
245
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
246
 
                                outputFormatter.Space();
247
 
                        }
248
 
                        if (!string.IsNullOrEmpty(attributeSection.AttributeTarget)) {
249
 
                                outputFormatter.PrintText(attributeSection.AttributeTarget);
250
 
                                outputFormatter.PrintToken(Tokens.Colon);
251
 
                                outputFormatter.Space();
252
 
                        }
253
 
                        Debug.Assert(attributeSection.Attributes != null);
254
 
                        for (int j = 0; j < attributeSection.Attributes.Count; ++j) {
255
 
                                TrackVisit((INode)attributeSection.Attributes[j], data);
256
 
                                if (j + 1 < attributeSection.Attributes.Count) {
257
 
                                        PrintFormattedComma();
258
 
                                }
259
 
                        }
260
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
261
 
                                outputFormatter.Space();
262
 
                        }
263
 
                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
264
 
                        if (formatSection) {
265
 
                                outputFormatter.NewLine();
266
 
                        }
267
 
                        return null;
268
 
                }
269
 
                
270
 
                public override object TrackedVisitAttribute (ICSharpCode.OldNRefactory.Ast.Attribute attribute, object data)
271
 
                {
272
 
                        outputFormatter.PrintIdentifier (attribute.Name);
273
 
                        if (attribute.IsEmptyCall)
274
 
                                return null;
275
 
                        outputFormatter.PrintToken (Tokens.OpenParenthesis);
276
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
277
 
                                outputFormatter.Space ();
278
 
                        }
279
 
                        this.AppendCommaSeparatedList (attribute.PositionalArguments);
280
 
                        
281
 
                        if (attribute.NamedArguments != null && attribute.NamedArguments.Count > 0) {
282
 
                                if (attribute.PositionalArguments.Count > 0) {
283
 
                                        PrintFormattedComma ();
284
 
                                }
285
 
                                for (int i = 0; i < attribute.NamedArguments.Count; ++i) {
286
 
                                        TrackVisit ((INode)attribute.NamedArguments[i], data);
287
 
                                        if (i + 1 < attribute.NamedArguments.Count) {
288
 
                                                PrintFormattedComma ();
289
 
                                        }
290
 
                                }
291
 
                        }
292
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
293
 
                                outputFormatter.Space ();
294
 
                        }
295
 
                        outputFormatter.PrintToken (Tokens.CloseParenthesis);
296
 
                        return null;
297
 
                }
298
 
                
299
 
                public override object TrackedVisitNamedArgumentExpression(NamedArgumentExpression namedArgumentExpression, object data)
300
 
                {
301
 
                        outputFormatter.PrintIdentifier(namedArgumentExpression.Name);
302
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
303
 
                                outputFormatter.Space();
304
 
                        }
305
 
                        outputFormatter.PrintToken(Tokens.Assign);
306
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
307
 
                                outputFormatter.Space();
308
 
                        }
309
 
                        TrackVisit(namedArgumentExpression.Expression, data);
310
 
                        return null;
311
 
                }
312
 
                
313
 
                public override object TrackedVisitUsing(Using @using, object data)
314
 
                {
315
 
                        outputFormatter.Indent();
316
 
                        outputFormatter.PrintToken(Tokens.Using);
317
 
                        outputFormatter.Space();
318
 
                        
319
 
                        outputFormatter.PrintIdentifier(@using.Name);
320
 
                        
321
 
                        if (@using.IsAlias) {
322
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
323
 
                                        outputFormatter.Space();
324
 
                                }
325
 
                                outputFormatter.PrintToken(Tokens.Assign);
326
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
327
 
                                        outputFormatter.Space();
328
 
                                }
329
 
                                TrackVisit(@using.Alias, data);
330
 
                        }
331
 
                        
332
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
333
 
                        outputFormatter.NewLine();
334
 
                        return null;
335
 
                }
336
 
                
337
 
                public override object TrackedVisitUsingDeclaration(UsingDeclaration usingDeclaration, object data)
338
 
                {
339
 
                        foreach (Using u in usingDeclaration.Usings) {
340
 
                                TrackVisit(u, data);
341
 
                        }
342
 
                        return null;
343
 
                }
344
 
                
345
 
                public override object TrackedVisitExternAliasDirective(ExternAliasDirective externAliasDirective, object data)
346
 
                {
347
 
                        outputFormatter.Indent();
348
 
                        outputFormatter.PrintText("extern alias ");
349
 
                        outputFormatter.PrintIdentifier(externAliasDirective.Name);
350
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
351
 
                        outputFormatter.NewLine();
352
 
                        return null;
353
 
                }
354
 
                
355
 
                public override object TrackedVisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration, object data)
356
 
                {
357
 
                        outputFormatter.Indent();
358
 
                        outputFormatter.PrintToken(Tokens.Namespace);
359
 
                        outputFormatter.Space();
360
 
                        outputFormatter.PrintIdentifier(namespaceDeclaration.Name);
361
 
                        
362
 
                        outputFormatter.BeginBrace (this.prettyPrintOptions.NamespaceBraceStyle, this.prettyPrintOptions.IndentNamespaceBody);
363
 
                        
364
 
                        namespaceDeclaration.AcceptChildren(this, data);
365
 
                        
366
 
                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentNamespaceBody);
367
 
                        
368
 
                        return null;
369
 
                }
370
 
                
371
 
                
372
 
                void OutputEnumMembers(TypeDeclaration typeDeclaration, object data)
373
 
                {
374
 
                        for (int i = 0; i < typeDeclaration.Children.Count; i++) {
375
 
                                FieldDeclaration fieldDeclaration = (FieldDeclaration)typeDeclaration.Children[i];
376
 
                                BeginVisit(fieldDeclaration);
377
 
                                VariableDeclaration f = (VariableDeclaration)fieldDeclaration.Fields[0];
378
 
                                VisitAttributes(fieldDeclaration.Attributes, data);
379
 
                                outputFormatter.Indent();
380
 
                                outputFormatter.PrintIdentifier(f.Name);
381
 
                                if (f.Initializer != null && !f.Initializer.IsNull) {
382
 
                                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
383
 
                                                outputFormatter.Space();
384
 
                                        }
385
 
                                        outputFormatter.PrintToken(Tokens.Assign);
386
 
                                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
387
 
                                                outputFormatter.Space();
388
 
                                        }
389
 
                                        TrackVisit(f.Initializer, data);
390
 
                                }
391
 
                                if (i < typeDeclaration.Children.Count - 1) {
392
 
                                        outputFormatter.PrintToken(Tokens.Comma);
393
 
                                }
394
 
                                outputFormatter.NewLine();
395
 
                                EndVisit(fieldDeclaration);
396
 
                        }
397
 
                }
398
 
                
399
 
                TypeDeclaration currentType = null;
400
 
                
401
 
                public override object TrackedVisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
402
 
                {
403
 
                        VisitAttributes(typeDeclaration.Attributes, data);
404
 
                        outputFormatter.Indent();
405
 
                        OutputModifier(typeDeclaration.Modifier);
406
 
                        switch (typeDeclaration.Type) {
407
 
                                case ClassType.Enum:
408
 
                                        outputFormatter.PrintToken(Tokens.Enum);
409
 
                                        break;
410
 
                                case ClassType.Interface:
411
 
                                        outputFormatter.PrintToken(Tokens.Interface);
412
 
                                        break;
413
 
                                case ClassType.Struct:
414
 
                                        outputFormatter.PrintToken(Tokens.Struct);
415
 
                                        break;
416
 
                                default:
417
 
                                        outputFormatter.PrintToken(Tokens.Class);
418
 
                                        break;
419
 
                        }
420
 
                        outputFormatter.Space();
421
 
                        outputFormatter.PrintIdentifier(typeDeclaration.Name);
422
 
                        
423
 
                        PrintTemplates(typeDeclaration.Templates);
424
 
                        
425
 
                        if (typeDeclaration.BaseTypes != null && typeDeclaration.BaseTypes.Count > 0) {
426
 
                                outputFormatter.Space();
427
 
                                outputFormatter.PrintToken(Tokens.Colon);
428
 
                                outputFormatter.Space();
429
 
                                for (int i = 0; i < typeDeclaration.BaseTypes.Count; ++i) {
430
 
                                        if (i > 0) {
431
 
                                                PrintFormattedComma();
432
 
                                        }
433
 
                                        TrackVisit(typeDeclaration.BaseTypes[i], data);
434
 
                                }
435
 
                        }
436
 
                        
437
 
                        foreach (TemplateDefinition templateDefinition in typeDeclaration.Templates) {
438
 
                                TrackVisit(templateDefinition, data);
439
 
                        }
440
 
                        
441
 
                        switch (typeDeclaration.Type) {
442
 
                                case ClassType.Enum:
443
 
                                        outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle, this.prettyPrintOptions.IndentEnumBody);
444
 
                                        break;
445
 
                                case ClassType.Interface:
446
 
                                        outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle, this.prettyPrintOptions.IndentInterfaceBody);
447
 
                                        break;
448
 
                                case ClassType.Struct:
449
 
                                        outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle, this.prettyPrintOptions.IndentStructBody);
450
 
                                        break;
451
 
                                default:
452
 
                                        outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle, this.prettyPrintOptions.IndentClassBody);
453
 
                                        break;
454
 
                        }
455
 
                        
456
 
                        TypeDeclaration oldType = currentType;
457
 
                        currentType = typeDeclaration;
458
 
                        if (typeDeclaration.Type == ClassType.Enum) {
459
 
                                OutputEnumMembers(typeDeclaration, data);
460
 
                        } else {
461
 
                                typeDeclaration.AcceptChildren(this, data);
462
 
                        }
463
 
                        currentType = oldType;
464
 
                        switch (typeDeclaration.Type) {
465
 
                                case ClassType.Enum:
466
 
                                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentEnumBody);
467
 
                                        break;
468
 
                                case ClassType.Interface:
469
 
                                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentInterfaceBody);
470
 
                                        break;
471
 
                                case ClassType.Struct:
472
 
                                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentStructBody);
473
 
                                        break;
474
 
                                default:
475
 
                                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentCaseBody);
476
 
                                        break;
477
 
                        }
478
 
                        
479
 
                        return null;
480
 
                }
481
 
                
482
 
                public override object TrackedVisitTemplateDefinition(TemplateDefinition templateDefinition, object data)
483
 
                {
484
 
                        if (templateDefinition.Bases.Count == 0)
485
 
                                return null;
486
 
                        
487
 
                        outputFormatter.Space();
488
 
                        outputFormatter.PrintText("where");
489
 
                        outputFormatter.Space();
490
 
                        outputFormatter.PrintIdentifier(templateDefinition.Name);
491
 
                        outputFormatter.Space();
492
 
                        outputFormatter.PrintToken(Tokens.Colon);
493
 
                        outputFormatter.Space();
494
 
                        
495
 
                        for (int i = 0; i < templateDefinition.Bases.Count; ++i) {
496
 
                                TrackVisit(templateDefinition.Bases[i], data);
497
 
                                if (i + 1 < templateDefinition.Bases.Count) {
498
 
                                        PrintFormattedComma();
499
 
                                }
500
 
                        }
501
 
                        return null;
502
 
                }
503
 
                
504
 
                public override object TrackedVisitDelegateDeclaration(DelegateDeclaration delegateDeclaration, object data)
505
 
                {
506
 
                        VisitAttributes(delegateDeclaration.Attributes, data);
507
 
                        outputFormatter.Indent();
508
 
                        OutputModifier(delegateDeclaration.Modifier);
509
 
                        outputFormatter.PrintToken(Tokens.Delegate);
510
 
                        outputFormatter.Space();
511
 
                        TrackVisit(delegateDeclaration.ReturnType, data);
512
 
                        outputFormatter.Space();
513
 
                        outputFormatter.PrintIdentifier(delegateDeclaration.Name);
514
 
                        PrintTemplates(delegateDeclaration.Templates);
515
 
                        if (prettyPrintOptions.BeforeDelegateDeclarationParentheses) {
516
 
                                outputFormatter.Space();
517
 
                        }
518
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
519
 
                        bool withinParentheses = this.prettyPrintOptions.WithinMethodDeclarationParentheses && delegateDeclaration.Parameters.Any ();
520
 
                        if (withinParentheses) {
521
 
                                outputFormatter.Space();
522
 
                        }
523
 
                        AppendCommaSeparatedList(delegateDeclaration.Parameters);
524
 
                        if (withinParentheses) {
525
 
                                outputFormatter.Space();
526
 
                        }
527
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
528
 
                        foreach (TemplateDefinition templateDefinition in delegateDeclaration.Templates) {
529
 
                                TrackVisit(templateDefinition, data);
530
 
                        }
531
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
532
 
                        outputFormatter.NewLine();
533
 
                        return null;
534
 
                }
535
 
                
536
 
                public override object TrackedVisitOptionDeclaration(OptionDeclaration optionDeclaration, object data)
537
 
                {
538
 
                        if ((optionDeclaration.OptionType == OptionType.Explicit || optionDeclaration.OptionType == OptionType.Strict)
539
 
                            && optionDeclaration.OptionValue == true)
540
 
                        {
541
 
                                // Explicit On/Strict On is what C# does, do not report an error
542
 
                        } else {
543
 
                                NotSupported(optionDeclaration);
544
 
                        }
545
 
                        return null;
546
 
                }
547
 
                #endregion
548
 
                
549
 
                #region Type level
550
 
                public override object TrackedVisitFieldDeclaration(FieldDeclaration fieldDeclaration, object data)
551
 
                {
552
 
                        if (!fieldDeclaration.TypeReference.IsNull) {
553
 
                                VisitAttributes(fieldDeclaration.Attributes, data);
554
 
                                outputFormatter.Indent();
555
 
                                OutputModifier(fieldDeclaration.Modifier);
556
 
                                TrackVisit(fieldDeclaration.TypeReference, data);
557
 
                                outputFormatter.Space();
558
 
                                AppendCommaSeparatedList(fieldDeclaration.Fields);
559
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
560
 
                                outputFormatter.NewLine();
561
 
                        } else {
562
 
                                for (int i = 0; i < fieldDeclaration.Fields.Count; i++) {
563
 
                                        VisitAttributes(fieldDeclaration.Attributes, data);
564
 
                                        outputFormatter.Indent();
565
 
                                        OutputModifier(fieldDeclaration.Modifier);
566
 
                                        TrackVisit(fieldDeclaration.GetTypeForField(i), data);
567
 
                                        outputFormatter.Space();
568
 
                                        TrackVisit(fieldDeclaration.Fields[i], data);
569
 
                                        outputFormatter.PrintToken(Tokens.Semicolon);
570
 
                                        outputFormatter.NewLine();
571
 
                                }
572
 
                        }
573
 
                        return null;
574
 
                }
575
 
                
576
 
                public override object TrackedVisitVariableDeclaration(VariableDeclaration variableDeclaration, object data)
577
 
                {
578
 
                        outputFormatter.PrintIdentifier(variableDeclaration.Name);
579
 
                        if (!variableDeclaration.FixedArrayInitialization.IsNull) {
580
 
                                outputFormatter.PrintToken(Tokens.OpenSquareBracket);
581
 
                                TrackVisit(variableDeclaration.FixedArrayInitialization, data);
582
 
                                outputFormatter.PrintToken(Tokens.CloseSquareBracket);
583
 
                        }
584
 
                        if (!variableDeclaration.Initializer.IsNull) {
585
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
586
 
                                        outputFormatter.Space();
587
 
                                }
588
 
                                outputFormatter.PrintToken(Tokens.Assign);
589
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
590
 
                                        outputFormatter.Space();
591
 
                                }
592
 
                                TrackVisit(variableDeclaration.Initializer, data);
593
 
                        }
594
 
                        return null;
595
 
                }
596
 
                
597
 
                public override object TrackedVisitPropertyDeclaration(PropertyDeclaration propertyDeclaration, object data)
598
 
                {
599
 
                        VisitAttributes(propertyDeclaration.Attributes, data);
600
 
                        outputFormatter.Indent();
601
 
                        propertyDeclaration.Modifier &= ~Modifiers.ReadOnly;
602
 
                        OutputModifier(propertyDeclaration.Modifier);
603
 
                        TrackVisit(propertyDeclaration.TypeReference, data);
604
 
                        outputFormatter.Space();
605
 
                        if (propertyDeclaration.InterfaceImplementations.Count > 0) {
606
 
                                TrackVisit(propertyDeclaration.InterfaceImplementations[0].InterfaceType, data);
607
 
                                outputFormatter.PrintToken(Tokens.Dot);
608
 
                        }
609
 
                        outputFormatter.PrintIdentifier(propertyDeclaration.Name);
610
 
                        
611
 
                        OutputGetAndSetRegion(propertyDeclaration.GetRegion, propertyDeclaration.SetRegion);
612
 
                        
613
 
                        return null;
614
 
                }
615
 
                
616
 
                void OutputGetAndSetRegion(PropertyGetRegion getRegion, PropertySetRegion setRegion)
617
 
                {
618
 
                        BraceStyle braceStyle = this.prettyPrintOptions.PropertyBraceStyle;
619
 
                        
620
 
                        if (getRegion.Block.IsNull && setRegion.Block.IsNull && getRegion.Attributes.Count == 0 && setRegion.Attributes.Count == 0
621
 
                            && (braceStyle == BraceStyle.EndOfLine || braceStyle == BraceStyle.EndOfLineWithoutSpace))
622
 
                        {
623
 
                                if (braceStyle == BraceStyle.EndOfLine)
624
 
                                        outputFormatter.Space();
625
 
                                outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
626
 
                                // automatic property / abstract property:
627
 
                                // output in a single line: "string Text { get; set; }"
628
 
                                if (!getRegion.IsNull) {
629
 
                                        outputFormatter.Space();
630
 
                                        OutputModifier(getRegion.Modifier);
631
 
                                        outputFormatter.PrintText("get;");
632
 
                                }
633
 
                                if (!setRegion.IsNull) {
634
 
                                        outputFormatter.Space();
635
 
                                        OutputModifier(setRegion.Modifier);
636
 
                                        outputFormatter.PrintText("set;");
637
 
                                }
638
 
                                outputFormatter.Space();
639
 
                                outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
640
 
                                outputFormatter.NewLine();
641
 
                        } else {
642
 
                                outputFormatter.BeginBrace(braceStyle, this.prettyPrintOptions.IndentPropertyBody);
643
 
                                TrackVisit(getRegion, null);
644
 
                                TrackVisit(setRegion, null);
645
 
                                outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
646
 
                        }
647
 
                }
648
 
                
649
 
                public override object TrackedVisitPropertyGetRegion(PropertyGetRegion propertyGetRegion, object data)
650
 
                {
651
 
                        this.VisitAttributes(propertyGetRegion.Attributes, data);
652
 
                        outputFormatter.Indent();
653
 
                        OutputModifier(propertyGetRegion.Modifier);
654
 
                        outputFormatter.PrintText("get");
655
 
                        if (prettyPrintOptions.AllowPropertyGetBlockInline) {
656
 
                                OutputBlockAllowInline(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle);
657
 
                        } else {
658
 
                                OutputBlock(propertyGetRegion.Block, prettyPrintOptions.PropertyGetBraceStyle);
659
 
                        }
660
 
                        return null;
661
 
                }
662
 
                
663
 
                public override object TrackedVisitPropertySetRegion(PropertySetRegion propertySetRegion, object data)
664
 
                {
665
 
                        this.VisitAttributes(propertySetRegion.Attributes, data);
666
 
                        outputFormatter.Indent();
667
 
                        OutputModifier(propertySetRegion.Modifier);
668
 
                        outputFormatter.PrintText("set");
669
 
                        if (prettyPrintOptions.AllowPropertySetBlockInline) {
670
 
                                OutputBlockAllowInline(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle);
671
 
                        } else {
672
 
                                OutputBlock(propertySetRegion.Block, prettyPrintOptions.PropertySetBraceStyle);
673
 
                        }
674
 
                        return null;
675
 
                }
676
 
                
677
 
                public override object TrackedVisitEventDeclaration(EventDeclaration eventDeclaration, object data)
678
 
                {
679
 
                        VisitAttributes(eventDeclaration.Attributes, data);
680
 
                        outputFormatter.Indent();
681
 
                        OutputModifier(eventDeclaration.Modifier);
682
 
                        outputFormatter.PrintToken(Tokens.Event);
683
 
                        outputFormatter.Space();
684
 
                        TrackVisit(eventDeclaration.TypeReference, data);
685
 
                        outputFormatter.Space();
686
 
                        
687
 
                        if (eventDeclaration.InterfaceImplementations.Count > 0) {
688
 
                                TrackVisit(eventDeclaration.InterfaceImplementations[0].InterfaceType, data);
689
 
                                outputFormatter.PrintToken(Tokens.Dot);
690
 
                        }
691
 
                        
692
 
                        outputFormatter.PrintIdentifier(eventDeclaration.Name);
693
 
                        
694
 
                        if (!eventDeclaration.Initializer.IsNull) {
695
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
696
 
                                        outputFormatter.Space();
697
 
                                }
698
 
                                outputFormatter.PrintToken(Tokens.Assign);
699
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
700
 
                                        outputFormatter.Space();
701
 
                                }
702
 
                                TrackVisit(eventDeclaration.Initializer, data);
703
 
                        }
704
 
                        
705
 
                        if (eventDeclaration.AddRegion.IsNull && eventDeclaration.RemoveRegion.IsNull) {
706
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
707
 
                                outputFormatter.NewLine();
708
 
                        } else {
709
 
                                outputFormatter.BeginBrace(this.prettyPrintOptions.EventBraceStyle, this.prettyPrintOptions.IndentEventBody);
710
 
                                TrackVisit(eventDeclaration.AddRegion, data);
711
 
                                TrackVisit(eventDeclaration.RemoveRegion, data);
712
 
                                outputFormatter.EndBrace(this.prettyPrintOptions.IndentEventBody);
713
 
                        }
714
 
                        return null;
715
 
                }
716
 
                
717
 
                public override object TrackedVisitEventAddRegion(EventAddRegion eventAddRegion, object data)
718
 
                {
719
 
                        VisitAttributes(eventAddRegion.Attributes, data);
720
 
                        outputFormatter.Indent();
721
 
                        outputFormatter.PrintText("add");
722
 
                        if (prettyPrintOptions.AllowEventAddBlockInline) {
723
 
                                OutputBlockAllowInline(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle);
724
 
                        } else {
725
 
                                OutputBlock(eventAddRegion.Block, prettyPrintOptions.EventAddBraceStyle);
726
 
                        }
727
 
                        return null;
728
 
                }
729
 
                
730
 
                public override object TrackedVisitEventRemoveRegion(EventRemoveRegion eventRemoveRegion, object data)
731
 
                {
732
 
                        VisitAttributes(eventRemoveRegion.Attributes, data);
733
 
                        outputFormatter.Indent();
734
 
                        outputFormatter.PrintText("remove");
735
 
                        if (prettyPrintOptions.AllowEventRemoveBlockInline) {
736
 
                                OutputBlockAllowInline(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle);
737
 
                        } else {
738
 
                                OutputBlock(eventRemoveRegion.Block, prettyPrintOptions.EventRemoveBraceStyle);
739
 
                        }
740
 
                        return null;
741
 
                }
742
 
                
743
 
                public override object TrackedVisitEventRaiseRegion(EventRaiseRegion eventRaiseRegion, object data)
744
 
                {
745
 
                        // VB.NET only
746
 
                        NotSupported(eventRaiseRegion);
747
 
                        return null;
748
 
                }
749
 
                
750
 
                public override object TrackedVisitParameterDeclarationExpression(ParameterDeclarationExpression parameterDeclarationExpression, object data)
751
 
                {
752
 
                        VisitAttributes(parameterDeclarationExpression.Attributes, false);
753
 
                        if (!parameterDeclarationExpression.DefaultValue.IsNull) {
754
 
                                outputFormatter.PrintText("[System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute(");
755
 
                                TrackVisit(parameterDeclarationExpression.DefaultValue, data);
756
 
                                outputFormatter.PrintText(")] ");
757
 
                        }
758
 
                        OutputModifier(parameterDeclarationExpression.ParamModifier, parameterDeclarationExpression);
759
 
                        if (!parameterDeclarationExpression.TypeReference.IsNull) {
760
 
                                TrackVisit(parameterDeclarationExpression.TypeReference, data);
761
 
                                outputFormatter.Space();
762
 
                        }
763
 
                        outputFormatter.PrintIdentifier(parameterDeclarationExpression.ParameterName);
764
 
                        return null;
765
 
                }
766
 
                
767
 
                public override object TrackedVisitMethodDeclaration(MethodDeclaration methodDeclaration, object data)
768
 
                {
769
 
                        VisitAttributes(methodDeclaration.Attributes, data);
770
 
                        outputFormatter.Indent();
771
 
                        OutputModifier(methodDeclaration.Modifier);
772
 
                        TrackVisit(methodDeclaration.TypeReference, data);
773
 
                        outputFormatter.Space();
774
 
                        if (methodDeclaration.InterfaceImplementations.Count > 0) {
775
 
                                TrackVisit(methodDeclaration.InterfaceImplementations[0].InterfaceType, data);
776
 
                                outputFormatter.PrintToken(Tokens.Dot);
777
 
                        }
778
 
                        if (methodDeclaration.HandlesClause.Count > 0) {
779
 
                                Error(methodDeclaration, "Handles clauses are not supported in C#");
780
 
                        }
781
 
                        outputFormatter.PrintIdentifier(methodDeclaration.Name);
782
 
                        
783
 
                        PrintMethodDeclaration(methodDeclaration);
784
 
                        return null;
785
 
                }
786
 
                
787
 
                void PrintMethodDeclaration(MethodDeclaration methodDeclaration)
788
 
                {
789
 
                        PrintTemplates(methodDeclaration.Templates);
790
 
                        if (prettyPrintOptions.BeforeMethodDeclarationParentheses) {
791
 
                                outputFormatter.Space();
792
 
                        }
793
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
794
 
                        bool withinParentheses = this.prettyPrintOptions.WithinMethodDeclarationParentheses && methodDeclaration.Parameters.Any ();
795
 
                        if (withinParentheses) {
796
 
                                outputFormatter.Space();
797
 
                        }
798
 
                        if (methodDeclaration.IsExtensionMethod) {
799
 
                                outputFormatter.PrintToken(Tokens.This);
800
 
                                outputFormatter.Space();
801
 
                        }
802
 
                        AppendCommaSeparatedList(methodDeclaration.Parameters);
803
 
                        if (withinParentheses) {
804
 
                                outputFormatter.Space();
805
 
                        }
806
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
807
 
                        foreach (TemplateDefinition templateDefinition in methodDeclaration.Templates) {
808
 
                                TrackVisit(templateDefinition, null);
809
 
                        }
810
 
                        OutputBlock(methodDeclaration.Body, this.prettyPrintOptions.MethodBraceStyle);
811
 
                }
812
 
                
813
 
                public override object TrackedVisitOperatorDeclaration(OperatorDeclaration operatorDeclaration, object data)
814
 
                {
815
 
                        VisitAttributes(operatorDeclaration.Attributes, data);
816
 
                        outputFormatter.Indent();
817
 
                        OutputModifier(operatorDeclaration.Modifier);
818
 
                        
819
 
                        if (operatorDeclaration.IsConversionOperator) {
820
 
                                if (operatorDeclaration.ConversionType == ConversionType.Implicit) {
821
 
                                        outputFormatter.PrintToken(Tokens.Implicit);
822
 
                                } else {
823
 
                                        outputFormatter.PrintToken(Tokens.Explicit);
824
 
                                }
825
 
                        } else {
826
 
                                TrackVisit(operatorDeclaration.TypeReference, data);
827
 
                        }
828
 
                        outputFormatter.Space();
829
 
                        outputFormatter.PrintToken(Tokens.Operator);
830
 
                        outputFormatter.Space();
831
 
                        
832
 
                        if (operatorDeclaration.IsConversionOperator) {
833
 
                                TrackVisit(operatorDeclaration.TypeReference, data);
834
 
                        } else {
835
 
                                switch (operatorDeclaration.OverloadableOperator) {
836
 
                                        case OverloadableOperatorType.Add:
837
 
                                        case OverloadableOperatorType.UnaryPlus:
838
 
                                                outputFormatter.PrintToken(Tokens.Plus);
839
 
                                                break;
840
 
                                        case OverloadableOperatorType.BitNot:
841
 
                                                outputFormatter.PrintToken(Tokens.BitwiseComplement);
842
 
                                                break;
843
 
                                        case OverloadableOperatorType.BitwiseAnd:
844
 
                                                outputFormatter.PrintToken(Tokens.BitwiseAnd);
845
 
                                                break;
846
 
                                        case OverloadableOperatorType.BitwiseOr:
847
 
                                                outputFormatter.PrintToken(Tokens.BitwiseOr);
848
 
                                                break;
849
 
                                        case OverloadableOperatorType.Concat:
850
 
                                                outputFormatter.PrintToken(Tokens.Plus);
851
 
                                                break;
852
 
                                        case OverloadableOperatorType.Decrement:
853
 
                                                outputFormatter.PrintToken(Tokens.Decrement);
854
 
                                                break;
855
 
                                        case OverloadableOperatorType.Divide:
856
 
                                        case OverloadableOperatorType.DivideInteger:
857
 
                                                outputFormatter.PrintToken(Tokens.Div);
858
 
                                                break;
859
 
                                        case OverloadableOperatorType.Equality:
860
 
                                                outputFormatter.PrintToken(Tokens.Equal);
861
 
                                                break;
862
 
                                        case OverloadableOperatorType.ExclusiveOr:
863
 
                                                outputFormatter.PrintToken(Tokens.Xor);
864
 
                                                break;
865
 
                                        case OverloadableOperatorType.GreaterThan:
866
 
                                                outputFormatter.PrintToken(Tokens.GreaterThan);
867
 
                                                break;
868
 
                                        case OverloadableOperatorType.GreaterThanOrEqual:
869
 
                                                outputFormatter.PrintToken(Tokens.GreaterEqual);
870
 
                                                break;
871
 
                                        case OverloadableOperatorType.Increment:
872
 
                                                outputFormatter.PrintToken(Tokens.Increment);
873
 
                                                break;
874
 
                                        case OverloadableOperatorType.InEquality:
875
 
                                                outputFormatter.PrintToken(Tokens.NotEqual);
876
 
                                                break;
877
 
                                        case OverloadableOperatorType.IsTrue:
878
 
                                                outputFormatter.PrintToken(Tokens.True);
879
 
                                                break;
880
 
                                        case OverloadableOperatorType.IsFalse:
881
 
                                                outputFormatter.PrintToken(Tokens.False);
882
 
                                                break;
883
 
                                        case OverloadableOperatorType.LessThan:
884
 
                                                outputFormatter.PrintToken(Tokens.LessThan);
885
 
                                                break;
886
 
                                        case OverloadableOperatorType.LessThanOrEqual:
887
 
                                                outputFormatter.PrintToken(Tokens.LessEqual);
888
 
                                                break;
889
 
                                        case OverloadableOperatorType.Like:
890
 
                                                outputFormatter.PrintText("Like");
891
 
                                                break;
892
 
                                        case OverloadableOperatorType.Modulus:
893
 
                                                outputFormatter.PrintToken(Tokens.Mod);
894
 
                                                break;
895
 
                                        case OverloadableOperatorType.Multiply:
896
 
                                                outputFormatter.PrintToken(Tokens.Times);
897
 
                                                break;
898
 
                                        case OverloadableOperatorType.Not:
899
 
                                                outputFormatter.PrintToken(Tokens.Not);
900
 
                                                break;
901
 
                                        case OverloadableOperatorType.Power:
902
 
                                                outputFormatter.PrintText("Power");
903
 
                                                break;
904
 
                                        case OverloadableOperatorType.ShiftLeft:
905
 
                                                outputFormatter.PrintToken(Tokens.ShiftLeft);
906
 
                                                break;
907
 
                                        case OverloadableOperatorType.ShiftRight:
908
 
                                                outputFormatter.PrintToken(Tokens.GreaterThan);
909
 
                                                outputFormatter.PrintToken(Tokens.GreaterThan);
910
 
                                                break;
911
 
                                        case OverloadableOperatorType.UnaryMinus:
912
 
                                        case OverloadableOperatorType.Subtract:
913
 
                                                outputFormatter.PrintToken(Tokens.Minus);
914
 
                                                break;
915
 
                                        default:
916
 
                                                Error(operatorDeclaration, operatorDeclaration.OverloadableOperator.ToString() + " is not supported as overloadable operator");
917
 
                                                break;
918
 
                                }
919
 
                        }
920
 
                        
921
 
                        PrintMethodDeclaration(operatorDeclaration);
922
 
                        return null;
923
 
                }
924
 
                
925
 
                public override object TrackedVisitInterfaceImplementation(InterfaceImplementation interfaceImplementation, object data)
926
 
                {
927
 
                        throw new InvalidOperationException();
928
 
                }
929
 
                
930
 
                public override object TrackedVisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
931
 
                {
932
 
                        VisitAttributes(constructorDeclaration.Attributes, data);
933
 
                        outputFormatter.Indent();
934
 
                        OutputModifier(constructorDeclaration.Modifier);
935
 
                        if (currentType != null) {
936
 
                                outputFormatter.PrintIdentifier(currentType.Name);
937
 
                        } else {
938
 
                                outputFormatter.PrintIdentifier(constructorDeclaration.Name);
939
 
                        }
940
 
                        if (prettyPrintOptions.BeforeConstructorDeclarationParentheses) {
941
 
                                outputFormatter.Space();
942
 
                        }
943
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
944
 
                        bool withinParentheses = this.prettyPrintOptions.WithinMethodDeclarationParentheses && constructorDeclaration.Parameters.Any ();
945
 
                        if (withinParentheses) {
946
 
                                outputFormatter.Space();
947
 
                        }
948
 
                        AppendCommaSeparatedList(constructorDeclaration.Parameters);
949
 
                        if (withinParentheses) {
950
 
                                outputFormatter.Space();
951
 
                        }
952
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
953
 
                        TrackVisit(constructorDeclaration.ConstructorInitializer, data);
954
 
                        OutputBlock(constructorDeclaration.Body, this.prettyPrintOptions.ConstructorBraceStyle);
955
 
                        return null;
956
 
                }
957
 
                
958
 
                public override object TrackedVisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
959
 
                {
960
 
                        if (constructorInitializer.IsNull) {
961
 
                                return null;
962
 
                        }
963
 
                        outputFormatter.Space();
964
 
                        outputFormatter.PrintToken(Tokens.Colon);
965
 
                        outputFormatter.Space();
966
 
                        if (constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.Base) {
967
 
                                outputFormatter.PrintToken(Tokens.Base);
968
 
                        } else {
969
 
                                outputFormatter.PrintToken(Tokens.This);
970
 
                        }
971
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
972
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
973
 
                                outputFormatter.Space();
974
 
                        }
975
 
                        AppendCommaSeparatedList(constructorInitializer.Arguments);
976
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
977
 
                                outputFormatter.Space();
978
 
                        }
979
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
980
 
                        return null;
981
 
                }
982
 
                
983
 
                public override object TrackedVisitIndexerDeclaration(IndexerDeclaration indexerDeclaration, object data)
984
 
                {
985
 
                        VisitAttributes(indexerDeclaration.Attributes, data);
986
 
                        outputFormatter.Indent();
987
 
                        OutputModifier(indexerDeclaration.Modifier);
988
 
                        TrackVisit(indexerDeclaration.TypeReference, data);
989
 
                        outputFormatter.Space();
990
 
                        if (indexerDeclaration.InterfaceImplementations.Count > 0) {
991
 
                                TrackVisit(indexerDeclaration.InterfaceImplementations[0].InterfaceType, data);
992
 
                                outputFormatter.PrintToken(Tokens.Dot);
993
 
                        }
994
 
                        outputFormatter.PrintToken(Tokens.This);
995
 
                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
996
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
997
 
                                outputFormatter.Space();
998
 
                        }
999
 
                        AppendCommaSeparatedList(indexerDeclaration.Parameters);
1000
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
1001
 
                                outputFormatter.Space();
1002
 
                        }
1003
 
                        
1004
 
                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
1005
 
                        
1006
 
                        outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentPropertyBody);
1007
 
                        
1008
 
                        TrackVisit(indexerDeclaration.GetRegion, data);
1009
 
                        TrackVisit(indexerDeclaration.SetRegion, data);
1010
 
                        
1011
 
                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
1012
 
                        return null;
1013
 
                }
1014
 
                
1015
 
                public override object TrackedVisitDestructorDeclaration(DestructorDeclaration destructorDeclaration, object data)
1016
 
                {
1017
 
                        VisitAttributes(destructorDeclaration.Attributes, data);
1018
 
                        outputFormatter.Indent();
1019
 
                        OutputModifier(destructorDeclaration.Modifier);
1020
 
                        outputFormatter.PrintToken(Tokens.BitwiseComplement);
1021
 
                        if (currentType != null)
1022
 
                                outputFormatter.PrintIdentifier(currentType.Name);
1023
 
                        else
1024
 
                                outputFormatter.PrintIdentifier(destructorDeclaration.Name);
1025
 
                        if (prettyPrintOptions.BeforeConstructorDeclarationParentheses) {
1026
 
                                outputFormatter.Space();
1027
 
                        }
1028
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1029
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1030
 
                        
1031
 
                        OutputBlock(destructorDeclaration.Body, this.prettyPrintOptions.DestructorBraceStyle);
1032
 
                        return null;
1033
 
                }
1034
 
                
1035
 
                public override object TrackedVisitDeclareDeclaration(DeclareDeclaration declareDeclaration, object data)
1036
 
                {
1037
 
                        NotSupported(declareDeclaration);
1038
 
                        return null;
1039
 
                }
1040
 
                #endregion
1041
 
                
1042
 
                #region Statements
1043
 
                
1044
 
                void OutputBlock(BlockStatement blockStatement, BraceStyle braceStyle)
1045
 
                {
1046
 
                        OutputBlock(blockStatement, braceStyle, true);
1047
 
                }
1048
 
                void OutputBlock(BlockStatement blockStatement, BraceStyle braceStyle, bool emitEndingNewLine)
1049
 
                {
1050
 
                        BeginVisit(blockStatement);
1051
 
                        if (blockStatement.IsNull) {
1052
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1053
 
                                outputFormatter.NewLine();
1054
 
                        } else {
1055
 
                                outputFormatter.BeginBrace(braceStyle, this.prettyPrintOptions.IndentBlocks);
1056
 
                                foreach (Statement stmt in blockStatement.Children) {
1057
 
                                        outputFormatter.Indent();
1058
 
                                        if (stmt is BlockStatement) {
1059
 
                                                TrackVisit(stmt, BraceStyle.EndOfLine);
1060
 
                                        } else {
1061
 
                                                TrackVisit(stmt, null);
1062
 
                                        }
1063
 
                                        if (!outputFormatter.LastCharacterIsNewLine)
1064
 
                                                outputFormatter.NewLine();
1065
 
                                }
1066
 
                                outputFormatter.EndBrace (this.prettyPrintOptions.IndentBlocks, emitEndingNewLine);
1067
 
                        }
1068
 
                        EndVisit(blockStatement);
1069
 
                }
1070
 
                
1071
 
                void OutputBlockAllowInline(BlockStatement blockStatement, BraceStyle braceStyle)
1072
 
                {
1073
 
                        OutputBlockAllowInline(blockStatement, braceStyle, true);
1074
 
                }
1075
 
                
1076
 
                void OutputBlockAllowInline(BlockStatement blockStatement, BraceStyle braceStyle, bool useNewLine)
1077
 
                {
1078
 
                        if (!blockStatement.IsNull
1079
 
                            && (
1080
 
                                blockStatement.Children.Count == 0
1081
 
                                || blockStatement.Children.Count == 1
1082
 
                                && (blockStatement.Children[0] is ExpressionStatement
1083
 
                                    || blockStatement.Children[0] is ReturnStatement
1084
 
                                   )))
1085
 
                        {
1086
 
                                outputFormatter.Space();
1087
 
                                outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
1088
 
                                outputFormatter.Space();
1089
 
                                if (blockStatement.Children.Count != 0) {
1090
 
                                        bool doIndent  = outputFormatter.DoIndent;
1091
 
                                        bool doNewLine = outputFormatter.DoNewLine;
1092
 
                                        outputFormatter.DoIndent  = false;
1093
 
                                        outputFormatter.DoNewLine = false;
1094
 
                                        
1095
 
                                        TrackVisit(blockStatement.Children[0], null);
1096
 
                                        
1097
 
                                        outputFormatter.DoIndent  = doIndent;
1098
 
                                        outputFormatter.DoNewLine = doNewLine;
1099
 
                                        
1100
 
                                        outputFormatter.Space();
1101
 
                                }
1102
 
                                outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
1103
 
                                if (useNewLine) {
1104
 
                                        outputFormatter.NewLine();
1105
 
                                }
1106
 
                        } else {
1107
 
                                OutputBlock(blockStatement, braceStyle, useNewLine);
1108
 
                        }
1109
 
                }
1110
 
                
1111
 
                public override object TrackedVisitBlockStatement(BlockStatement blockStatement, object data)
1112
 
                {
1113
 
                        if (outputFormatter.TextLength == 0) {
1114
 
                                // we are outputting only a code block:
1115
 
                                // do not output braces, just the block's contents
1116
 
                                foreach (Statement stmt in blockStatement.Children) {
1117
 
                                        outputFormatter.Indent();
1118
 
                                        TrackVisit(stmt, null);
1119
 
                                        if (!outputFormatter.LastCharacterIsNewLine)
1120
 
                                                outputFormatter.NewLine();
1121
 
                                }
1122
 
                                return null;
1123
 
                        }
1124
 
                        
1125
 
                        if (data is BraceStyle)
1126
 
                                OutputBlock(blockStatement, (BraceStyle)data);
1127
 
                        else
1128
 
                                OutputBlock(blockStatement, BraceStyle.NextLine);
1129
 
                        return null;
1130
 
                }
1131
 
                
1132
 
                public override object TrackedVisitAddHandlerStatement(AddHandlerStatement addHandlerStatement, object data)
1133
 
                {
1134
 
                        TrackVisit(addHandlerStatement.EventExpression, data);
1135
 
                        outputFormatter.Space();
1136
 
                        outputFormatter.PrintToken(Tokens.PlusAssign);
1137
 
                        outputFormatter.Space();
1138
 
                        TrackVisit(addHandlerStatement.HandlerExpression, data);
1139
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1140
 
                        return null;
1141
 
                }
1142
 
                
1143
 
                public override object TrackedVisitRemoveHandlerStatement(RemoveHandlerStatement removeHandlerStatement, object data)
1144
 
                {
1145
 
                        TrackVisit(removeHandlerStatement.EventExpression, data);
1146
 
                        outputFormatter.Space();
1147
 
                        outputFormatter.PrintToken(Tokens.MinusAssign);
1148
 
                        outputFormatter.Space();
1149
 
                        TrackVisit(removeHandlerStatement.HandlerExpression, data);
1150
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1151
 
                        return null;
1152
 
                }
1153
 
                
1154
 
                public override object TrackedVisitRaiseEventStatement(RaiseEventStatement raiseEventStatement, object data)
1155
 
                {
1156
 
                        outputFormatter.PrintToken(Tokens.If);
1157
 
                        outputFormatter.Space();
1158
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1159
 
                        outputFormatter.PrintIdentifier(raiseEventStatement.EventName);
1160
 
                        outputFormatter.Space();
1161
 
                        outputFormatter.PrintToken(Tokens.NotEqual);
1162
 
                        outputFormatter.Space();
1163
 
                        outputFormatter.PrintToken(Tokens.Null);
1164
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1165
 
                        
1166
 
                        outputFormatter.BeginBrace(BraceStyle.EndOfLine, this.prettyPrintOptions.IndentBlocks);
1167
 
                        
1168
 
                        outputFormatter.Indent();
1169
 
                        outputFormatter.PrintIdentifier(raiseEventStatement.EventName);
1170
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1171
 
                        this.AppendCommaSeparatedList(raiseEventStatement.Arguments);
1172
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1173
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1174
 
                        
1175
 
                        outputFormatter.NewLine();
1176
 
                        outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks);
1177
 
                        
1178
 
                        return null;
1179
 
                }
1180
 
                
1181
 
                public override object TrackedVisitEraseStatement(EraseStatement eraseStatement, object data)
1182
 
                {
1183
 
                        for (int i = 0; i < eraseStatement.Expressions.Count; i++) {
1184
 
                                if (i > 0) {
1185
 
                                        outputFormatter.NewLine();
1186
 
                                        outputFormatter.Indent();
1187
 
                                }
1188
 
                                TrackVisit(eraseStatement.Expressions[i], data);
1189
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
1190
 
                                        outputFormatter.Space();
1191
 
                                }
1192
 
                                outputFormatter.PrintToken(Tokens.Assign);
1193
 
                                if (this.prettyPrintOptions.AroundAssignmentParentheses) {
1194
 
                                        outputFormatter.Space();
1195
 
                                }
1196
 
                                outputFormatter.PrintToken(Tokens.Null);
1197
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1198
 
                        }
1199
 
                        return null;
1200
 
                }
1201
 
                
1202
 
                public override object TrackedVisitErrorStatement(ErrorStatement errorStatement, object data)
1203
 
                {
1204
 
                        NotSupported(errorStatement);
1205
 
                        return null;
1206
 
                }
1207
 
                
1208
 
                public override object TrackedVisitOnErrorStatement(OnErrorStatement onErrorStatement, object data)
1209
 
                {
1210
 
                        NotSupported(onErrorStatement);
1211
 
                        return null;
1212
 
                }
1213
 
                
1214
 
                public override object TrackedVisitReDimStatement(ReDimStatement reDimStatement, object data)
1215
 
                {
1216
 
                        if (!reDimStatement.IsPreserve) {
1217
 
                                NotSupported(reDimStatement);
1218
 
                                return null;
1219
 
                        }
1220
 
                        foreach (InvocationExpression ie in reDimStatement.ReDimClauses) {
1221
 
                                outputFormatter.PrintText("Array.Resize(ref ");
1222
 
                                ie.TargetObject.AcceptVisitor(this, data);
1223
 
                                outputFormatter.PrintText(", ");
1224
 
                                for (int i = 0; i < ie.Arguments.Count; i++) {
1225
 
                                        if (i > 0) outputFormatter.PrintText(", ");
1226
 
                                        Expression.AddInteger(ie.Arguments[i], 1).AcceptVisitor(this, data);
1227
 
                                }
1228
 
                                outputFormatter.PrintText(")");
1229
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1230
 
                        }
1231
 
                        return null;
1232
 
                }
1233
 
                
1234
 
                public override object TrackedVisitExpressionStatement(ExpressionStatement expressionStatement, object data)
1235
 
                {
1236
 
                        TrackVisit(expressionStatement.Expression, data);
1237
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1238
 
                        return null;
1239
 
                }
1240
 
                
1241
 
                public override object TrackedVisitLocalVariableDeclaration(LocalVariableDeclaration localVariableDeclaration, object data)
1242
 
                {
1243
 
                        TypeReference type = localVariableDeclaration.GetTypeForVariable(0);
1244
 
                        for (int i = 1; i < localVariableDeclaration.Variables.Count; ++i) {
1245
 
                                if (localVariableDeclaration.GetTypeForVariable(i) != type)
1246
 
                                        return TrackedVisitLocalVariableDeclarationSeparateTypes(localVariableDeclaration, data);
1247
 
                        }
1248
 
                        // all variables have the same type
1249
 
                        OutputModifier(localVariableDeclaration.Modifier);
1250
 
                        TrackVisit(type ?? new TypeReference("System.Object", true), data);
1251
 
                        outputFormatter.Space();
1252
 
                        AppendCommaSeparatedList(localVariableDeclaration.Variables);
1253
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1254
 
                        return null;
1255
 
                }
1256
 
                
1257
 
                object TrackedVisitLocalVariableDeclarationSeparateTypes(LocalVariableDeclaration localVariableDeclaration, object data)
1258
 
                {
1259
 
                        for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) {
1260
 
                                VariableDeclaration v = (VariableDeclaration)localVariableDeclaration.Variables[i];
1261
 
                                if (i > 0) {
1262
 
                                        outputFormatter.NewLine();
1263
 
                                        outputFormatter.Indent();
1264
 
                                }
1265
 
                                OutputModifier(localVariableDeclaration.Modifier);
1266
 
                                TrackVisit(localVariableDeclaration.GetTypeForVariable(i) ?? new TypeReference("System.Object", true), data);
1267
 
                                outputFormatter.Space();
1268
 
                                TrackVisit(v, data);
1269
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1270
 
                        }
1271
 
                        return null;
1272
 
                }
1273
 
                
1274
 
                public override object TrackedVisitEmptyStatement(EmptyStatement emptyStatement, object data)
1275
 
                {
1276
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1277
 
                        return null;
1278
 
                }
1279
 
                
1280
 
                public override object TrackedVisitYieldStatement(YieldStatement yieldStatement, object data)
1281
 
                {
1282
 
                        Debug.Assert(yieldStatement != null);
1283
 
                        Debug.Assert(yieldStatement.Statement != null);
1284
 
                        outputFormatter.PrintText("yield");
1285
 
                        outputFormatter.Space();
1286
 
                        TrackVisit(yieldStatement.Statement, data);
1287
 
                        return null;
1288
 
                }
1289
 
                
1290
 
                public override object TrackedVisitReturnStatement(ReturnStatement returnStatement, object data)
1291
 
                {
1292
 
                        outputFormatter.PrintToken(Tokens.Return);
1293
 
                        if (!returnStatement.Expression.IsNull) {
1294
 
                                outputFormatter.Space();
1295
 
                                TrackVisit(returnStatement.Expression, data);
1296
 
                        }
1297
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1298
 
                        return null;
1299
 
                }
1300
 
                
1301
 
                public override object TrackedVisitIfElseStatement(IfElseStatement ifElseStatement, object data)
1302
 
                {
1303
 
                        outputFormatter.PrintToken(Tokens.If);
1304
 
                        if (this.prettyPrintOptions.IfParentheses) {
1305
 
                                outputFormatter.Space();
1306
 
                        }
1307
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1308
 
                        if (this.prettyPrintOptions.WithinIfParentheses) {
1309
 
                                outputFormatter.Space();
1310
 
                        }
1311
 
                        TrackVisit(ifElseStatement.Condition, data);
1312
 
                        if (this.prettyPrintOptions.WithinIfParentheses) {
1313
 
                                outputFormatter.Space();
1314
 
                        }
1315
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1316
 
                        
1317
 
                        PrintIfSection(ifElseStatement.TrueStatement);
1318
 
                        bool wasBlock = false;
1319
 
                        if (ifElseStatement.TrueStatement != null && ifElseStatement.TrueStatement.Count > 0)
1320
 
                                wasBlock = ifElseStatement.TrueStatement.Last () is BlockStatement;
1321
 
                        
1322
 
                        foreach (ElseIfSection elseIfSection in ifElseStatement.ElseIfSections) {
1323
 
                                TrackVisit(elseIfSection, data);
1324
 
                                wasBlock = elseIfSection.EmbeddedStatement is BlockStatement;
1325
 
                        }
1326
 
                        
1327
 
                        if (ifElseStatement.HasElseStatements) {
1328
 
                                if (prettyPrintOptions.PlaceElseOnNewLine || (prettyPrintOptions.PlaceNonBlockElseOnNewLine && !wasBlock)) {
1329
 
                                        outputFormatter.NewLine();
1330
 
                                        outputFormatter.Indent();
1331
 
                                } else {
1332
 
                                        outputFormatter.Space();
1333
 
                                }
1334
 
                                outputFormatter.PrintToken(Tokens.Else);
1335
 
                                PrintIfSection(ifElseStatement.FalseStatement);
1336
 
                        }
1337
 
                        
1338
 
                        return null;
1339
 
                }
1340
 
                
1341
 
                void PrintIfSection(List<Statement> statements)
1342
 
                {
1343
 
                        if (statements.Count == 1 && (statements[0] is BlockStatement)) {
1344
 
                                OutputBlock((BlockStatement)statements[0],
1345
 
                                            prettyPrintOptions.StatementBraceStyle,
1346
 
                                            prettyPrintOptions.PlaceElseOnNewLine && prettyPrintOptions.PlaceElseIfOnNewLine);
1347
 
                                return;
1348
 
                        }
1349
 
                        /*                      if (statements.Count != 1 || !(statements[0] is BlockStatement)) {
1350
 
                                outputFormatter.Space();
1351
 
                        }*/
1352
 
                        if (statements.Count != 1) {
1353
 
                                outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
1354
 
                        } else {
1355
 
                                outputFormatter.NewLine ();
1356
 
                                outputFormatter.IndentationLevel++;
1357
 
                                outputFormatter.Indent ();
1358
 
                        }
1359
 
                        
1360
 
                        foreach (Statement stmt in statements) {
1361
 
                                
1362
 
                                TrackVisit(stmt, prettyPrintOptions.StatementBraceStyle);
1363
 
                        }
1364
 
                        
1365
 
                        if (statements.Count == 1) {
1366
 
                                outputFormatter.IndentationLevel--;
1367
 
                        } else {
1368
 
                                outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
1369
 
                        }
1370
 
                        /*                      if (statements.Count != 1 || !(statements[0] is BlockStatement)) {
1371
 
                                outputFormatter.Space();
1372
 
                        }*/
1373
 
                }
1374
 
                
1375
 
                public override object TrackedVisitElseIfSection(ElseIfSection elseIfSection, object data)
1376
 
                {
1377
 
                        if (prettyPrintOptions.PlaceElseIfOnNewLine) {
1378
 
                                outputFormatter.NewLine();
1379
 
                                outputFormatter.Indent();
1380
 
                        } else {
1381
 
                                outputFormatter.Space();
1382
 
                        }
1383
 
                        outputFormatter.PrintToken(Tokens.Else);
1384
 
                        outputFormatter.Space();
1385
 
                        outputFormatter.PrintToken(Tokens.If);
1386
 
                        if (prettyPrintOptions.IfParentheses) {
1387
 
                                outputFormatter.Space();
1388
 
                        }
1389
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1390
 
                        if (this.prettyPrintOptions.WithinIfParentheses) {
1391
 
                                outputFormatter.Space();
1392
 
                        }
1393
 
                        TrackVisit(elseIfSection.Condition, data);
1394
 
                        if (this.prettyPrintOptions.WithinIfParentheses) {
1395
 
                                outputFormatter.Space();
1396
 
                        }
1397
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1398
 
                        
1399
 
                        WriteEmbeddedStatement(elseIfSection.EmbeddedStatement, prettyPrintOptions.IfElseBraceForcement, prettyPrintOptions.StatementBraceStyle, false);
1400
 
                        
1401
 
                        return null;
1402
 
                }
1403
 
                
1404
 
                public override object TrackedVisitForStatement(ForStatement forStatement, object data)
1405
 
                {
1406
 
                        outputFormatter.PrintToken(Tokens.For);
1407
 
                        if (this.prettyPrintOptions.ForParentheses) {
1408
 
                                outputFormatter.Space();
1409
 
                        }
1410
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1411
 
                        if (this.prettyPrintOptions.WithinForParentheses) {
1412
 
                                outputFormatter.Space();
1413
 
                        }
1414
 
                        outputFormatter.DoIndent = false;
1415
 
                        outputFormatter.DoNewLine = false;
1416
 
                        outputFormatter.EmitSemicolon = false;
1417
 
                        for (int i = 0; i < forStatement.Initializers.Count; ++i) {
1418
 
                                INode node = (INode)forStatement.Initializers[i];
1419
 
                                TrackVisit(node, data);
1420
 
                                if (i + 1 < forStatement.Initializers.Count) {
1421
 
                                        outputFormatter.PrintToken(Tokens.Comma);
1422
 
                                }
1423
 
                        }
1424
 
                        outputFormatter.EmitSemicolon = true;
1425
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1426
 
                        outputFormatter.EmitSemicolon = false;
1427
 
                        if (!forStatement.Condition.IsNull) {
1428
 
                                if (this.prettyPrintOptions.SpacesAfterSemicolon) {
1429
 
                                        outputFormatter.Space();
1430
 
                                }
1431
 
                                TrackVisit(forStatement.Condition, data);
1432
 
                        }
1433
 
                        outputFormatter.EmitSemicolon = true;
1434
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1435
 
                        outputFormatter.EmitSemicolon = false;
1436
 
                        if (forStatement.Iterator != null && forStatement.Iterator.Count > 0) {
1437
 
                                if (this.prettyPrintOptions.SpacesAfterSemicolon) {
1438
 
                                        outputFormatter.Space();
1439
 
                                }
1440
 
                                
1441
 
                                for (int i = 0; i < forStatement.Iterator.Count; ++i) {
1442
 
                                        INode node = (INode)forStatement.Iterator[i];
1443
 
                                        TrackVisit(node, data);
1444
 
                                        if (i + 1 < forStatement.Iterator.Count) {
1445
 
                                                outputFormatter.PrintToken(Tokens.Comma);
1446
 
                                        }
1447
 
                                }
1448
 
                        }
1449
 
                        if (this.prettyPrintOptions.WithinForParentheses) {
1450
 
                                outputFormatter.Space();
1451
 
                        }
1452
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1453
 
                        outputFormatter.EmitSemicolon = true;
1454
 
                        outputFormatter.DoNewLine     = true;
1455
 
                        outputFormatter.DoIndent      = true;
1456
 
                        
1457
 
                        WriteEmbeddedStatement(forStatement.EmbeddedStatement, prettyPrintOptions.ForBraceForcement, prettyPrintOptions.StatementBraceStyle, true);
1458
 
                        
1459
 
                        return null;
1460
 
                }
1461
 
                
1462
 
                void WriteEmbeddedStatement (Statement statement)
1463
 
                {
1464
 
                        WriteEmbeddedStatement (statement, true);
1465
 
                }
1466
 
                
1467
 
                void WriteEmbeddedStatement (Statement statement, bool emitEndingNewLine)
1468
 
                {
1469
 
                        if (statement is BlockStatement) {
1470
 
                                OutputBlock((BlockStatement)statement, prettyPrintOptions.StatementBraceStyle, emitEndingNewLine);
1471
 
                        } else {
1472
 
                                ++outputFormatter.IndentationLevel;
1473
 
                                outputFormatter.NewLine();
1474
 
                                outputFormatter.Indent ();
1475
 
                                TrackVisit(statement, null);
1476
 
                                --outputFormatter.IndentationLevel;
1477
 
                        }
1478
 
                }
1479
 
                
1480
 
                void WriteEmbeddedStatement (Statement statement, BraceForcement forcement, BraceStyle braceStyle, bool emitEndingNewLine)
1481
 
                {
1482
 
                        if (statement is BlockStatement) {
1483
 
                                BlockStatement block = (BlockStatement)statement;
1484
 
                                switch (forcement) {
1485
 
                                case BraceForcement.RemoveBraces:
1486
 
                                        if (block.Children.Count == 1) {
1487
 
                                                ++outputFormatter.IndentationLevel;
1488
 
                                                outputFormatter.NewLine();
1489
 
                                                outputFormatter.Indent ();
1490
 
                                                TrackVisit(block.Children[0], null);
1491
 
                                                --outputFormatter.IndentationLevel;
1492
 
                                        } else  {
1493
 
                                                goto default;
1494
 
                                        }
1495
 
                                        break;
1496
 
                                case BraceForcement.RemoveBracesForSingleLine:
1497
 
                                        goto case BraceForcement.RemoveBraces;
1498
 
                                default:
1499
 
                                        OutputBlock((BlockStatement)statement, prettyPrintOptions.StatementBraceStyle, emitEndingNewLine);
1500
 
                                        break;
1501
 
                                }
1502
 
                        } else {
1503
 
                                switch (forcement) {
1504
 
                                case BraceForcement.AddBraces:
1505
 
                                        BlockStatement blockStatement = new BlockStatement ();
1506
 
                                        blockStatement.AddChild (statement);
1507
 
                                        OutputBlock(blockStatement, braceStyle, true);
1508
 
                                        break;
1509
 
                                default:
1510
 
                                        WriteEmbeddedStatement (statement, emitEndingNewLine);
1511
 
                                        break;
1512
 
                                }
1513
 
                        }
1514
 
                }
1515
 
                
1516
 
                public override object TrackedVisitLabelStatement(LabelStatement labelStatement, object data)
1517
 
                {
1518
 
                        outputFormatter.PrintIdentifier(labelStatement.Label);
1519
 
                        outputFormatter.PrintToken(Tokens.Colon);
1520
 
                        return null;
1521
 
                }
1522
 
                
1523
 
                public override object TrackedVisitGotoStatement(GotoStatement gotoStatement, object data)
1524
 
                {
1525
 
                        outputFormatter.PrintToken(Tokens.Goto);
1526
 
                        outputFormatter.Space();
1527
 
                        outputFormatter.PrintIdentifier(gotoStatement.Label);
1528
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1529
 
                        return null;
1530
 
                }
1531
 
                
1532
 
                public override object TrackedVisitSwitchStatement(SwitchStatement switchStatement, object data)
1533
 
                {
1534
 
                        outputFormatter.PrintToken(Tokens.Switch);
1535
 
                        if (this.prettyPrintOptions.SwitchParentheses) {
1536
 
                                outputFormatter.Space();
1537
 
                        }
1538
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1539
 
                        if (this.prettyPrintOptions.WithinSwitchParentheses) {
1540
 
                                outputFormatter.Space();
1541
 
                        }
1542
 
                        TrackVisit(switchStatement.SwitchExpression, data);
1543
 
                        if (this.prettyPrintOptions.WithinSwitchParentheses) {
1544
 
                                outputFormatter.Space();
1545
 
                        }
1546
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1547
 
                        
1548
 
                        outputFormatter.BeginBrace(prettyPrintOptions.StatementBraceStyle, prettyPrintOptions.IndentSwitchBody);
1549
 
                        foreach (SwitchSection section in switchStatement.SwitchSections) {
1550
 
                                TrackVisit(section, data);
1551
 
                        }
1552
 
                        outputFormatter.EndBrace (prettyPrintOptions.IndentSwitchBody);
1553
 
                        return null;
1554
 
                }
1555
 
                
1556
 
                public override object TrackedVisitSwitchSection(SwitchSection switchSection, object data)
1557
 
                {
1558
 
                        foreach (CaseLabel label in switchSection.SwitchLabels) {
1559
 
                                TrackVisit(label, data);
1560
 
                        }
1561
 
                        int standardIndentLevel = outputFormatter.IndentationLevel;
1562
 
                        if (prettyPrintOptions.IndentCaseBody)
1563
 
                                ++outputFormatter.IndentationLevel;
1564
 
                        for (int i = 0; i < switchSection.Children.Count; i++) {
1565
 
                                Statement stmt = switchSection.Children[i] as Statement;
1566
 
                                int oldIndent = outputFormatter.IndentationLevel;
1567
 
                                if (i == switchSection.Children.Count - 1) {
1568
 
                                        if (prettyPrintOptions.IndentBreakStatements)
1569
 
                                                outputFormatter.IndentationLevel = standardIndentLevel + 1;
1570
 
                                        else
1571
 
                                                outputFormatter.IndentationLevel = standardIndentLevel;
1572
 
                                }
1573
 
                                outputFormatter.Indent();
1574
 
                                TrackVisit(stmt, data);
1575
 
                                outputFormatter.NewLine();
1576
 
                                outputFormatter.IndentationLevel = oldIndent;
1577
 
                        }
1578
 
                        
1579
 
                        if (prettyPrintOptions.IndentCaseBody)
1580
 
                                --outputFormatter.IndentationLevel;
1581
 
                        return null;
1582
 
                }
1583
 
                
1584
 
                public override object TrackedVisitCaseLabel(CaseLabel caseLabel, object data)
1585
 
                {
1586
 
                        outputFormatter.Indent();
1587
 
                        if (caseLabel.IsDefault) {
1588
 
                                outputFormatter.PrintToken(Tokens.Default);
1589
 
                        } else {
1590
 
                                outputFormatter.PrintToken(Tokens.Case);
1591
 
                                outputFormatter.Space();
1592
 
                                if (caseLabel.BinaryOperatorType != BinaryOperatorType.None) {
1593
 
                                        Error(caseLabel, String.Format("Case labels with binary operators are unsupported : {0}", caseLabel.BinaryOperatorType));
1594
 
                                }
1595
 
                                TrackVisit(caseLabel.Label, data);
1596
 
                        }
1597
 
                        outputFormatter.PrintToken(Tokens.Colon);
1598
 
                        if (!caseLabel.ToExpression.IsNull) {
1599
 
                                PrimitiveExpression pl = caseLabel.Label as PrimitiveExpression;
1600
 
                                PrimitiveExpression pt = caseLabel.ToExpression as PrimitiveExpression;
1601
 
                                if (pl != null && pt != null && pl.Value is int && pt.Value is int) {
1602
 
                                        int plv = (int)pl.Value;
1603
 
                                        int prv = (int)pt.Value;
1604
 
                                        if (plv < prv && plv + 12 > prv) {
1605
 
                                                for (int i = plv + 1; i <= prv; i++) {
1606
 
                                                        outputFormatter.NewLine();
1607
 
                                                        outputFormatter.Indent();
1608
 
                                                        outputFormatter.PrintToken(Tokens.Case);
1609
 
                                                        outputFormatter.Space();
1610
 
                                                        outputFormatter.PrintText(i.ToString(NumberFormatInfo.InvariantInfo));
1611
 
                                                        outputFormatter.PrintToken(Tokens.Colon);
1612
 
                                                }
1613
 
                                        } else {
1614
 
                                                outputFormatter.PrintText(" // TODO: to ");
1615
 
                                                TrackVisit(caseLabel.ToExpression, data);
1616
 
                                        }
1617
 
                                } else {
1618
 
                                        outputFormatter.PrintText(" // TODO: to ");
1619
 
                                        TrackVisit(caseLabel.ToExpression, data);
1620
 
                                }
1621
 
                        }
1622
 
                        outputFormatter.NewLine();
1623
 
                        return null;
1624
 
                }
1625
 
                
1626
 
                public override object TrackedVisitBreakStatement(BreakStatement breakStatement, object data)
1627
 
                {
1628
 
                        outputFormatter.PrintToken(Tokens.Break);
1629
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1630
 
                        return null;
1631
 
                }
1632
 
                
1633
 
                public override object TrackedVisitStopStatement(StopStatement stopStatement, object data)
1634
 
                {
1635
 
                        outputFormatter.PrintText("System.Diagnostics.Debugger.Break()");
1636
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1637
 
                        return null;
1638
 
                }
1639
 
                
1640
 
                public override object TrackedVisitResumeStatement(ResumeStatement resumeStatement, object data)
1641
 
                {
1642
 
                        NotSupported(resumeStatement);
1643
 
                        return null;
1644
 
                }
1645
 
                
1646
 
                public override object TrackedVisitEndStatement(EndStatement endStatement, object data)
1647
 
                {
1648
 
                        outputFormatter.PrintText("System.Environment.Exit(0)");
1649
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1650
 
                        return null;
1651
 
                }
1652
 
                
1653
 
                public override object TrackedVisitContinueStatement(ContinueStatement continueStatement, object data)
1654
 
                {
1655
 
                        outputFormatter.PrintToken(Tokens.Continue);
1656
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1657
 
                        return null;
1658
 
                }
1659
 
                
1660
 
                public override object TrackedVisitGotoCaseStatement(GotoCaseStatement gotoCaseStatement, object data)
1661
 
                {
1662
 
                        outputFormatter.PrintToken(Tokens.Goto);
1663
 
                        outputFormatter.Space();
1664
 
                        if (gotoCaseStatement.IsDefaultCase) {
1665
 
                                outputFormatter.PrintToken(Tokens.Default);
1666
 
                        } else {
1667
 
                                outputFormatter.PrintToken(Tokens.Case);
1668
 
                                outputFormatter.Space();
1669
 
                                TrackVisit(gotoCaseStatement.Expression, data);
1670
 
                        }
1671
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1672
 
                        return null;
1673
 
                }
1674
 
                
1675
 
                void PrintLoopCheck(DoLoopStatement doLoopStatement)
1676
 
                {
1677
 
                        outputFormatter.PrintToken(Tokens.While);
1678
 
                        if (this.prettyPrintOptions.WhileParentheses) {
1679
 
                                outputFormatter.Space();
1680
 
                        }
1681
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1682
 
                        if (this.prettyPrintOptions.WithinWhileParentheses) {
1683
 
                                outputFormatter.Space();
1684
 
                        }
1685
 
                        
1686
 
                        if (doLoopStatement.ConditionType == ConditionType.Until) {
1687
 
                                outputFormatter.PrintToken(Tokens.Not);
1688
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
1689
 
                        }
1690
 
                        
1691
 
                        if (doLoopStatement.Condition.IsNull) {
1692
 
                                outputFormatter.PrintToken(Tokens.True);
1693
 
                        } else {
1694
 
                                TrackVisit(doLoopStatement.Condition, null);
1695
 
                        }
1696
 
                        
1697
 
                        if (doLoopStatement.ConditionType == ConditionType.Until) {
1698
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
1699
 
                        }
1700
 
                        if (this.prettyPrintOptions.WithinWhileParentheses) {
1701
 
                                outputFormatter.Space();
1702
 
                        }
1703
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1704
 
                }
1705
 
                
1706
 
                public override object TrackedVisitDoLoopStatement(DoLoopStatement doLoopStatement, object data)
1707
 
                {
1708
 
                        if (doLoopStatement.ConditionPosition == ConditionPosition.None) {
1709
 
                                Error(doLoopStatement, String.Format("Unknown condition position for loop : {0}.", doLoopStatement));
1710
 
                        }
1711
 
                        
1712
 
                        if (doLoopStatement.ConditionPosition == ConditionPosition.Start) {
1713
 
                                PrintLoopCheck(doLoopStatement);
1714
 
                        } else {
1715
 
                                outputFormatter.PrintToken(Tokens.Do);
1716
 
                        }
1717
 
                        
1718
 
                        WriteEmbeddedStatement(doLoopStatement.EmbeddedStatement, prettyPrintOptions.WhileBraceForcement, prettyPrintOptions.StatementBraceStyle, prettyPrintOptions.PlaceWhileOnNewLine);
1719
 
                        
1720
 
                        if (doLoopStatement.ConditionPosition == ConditionPosition.End) {
1721
 
                                if (prettyPrintOptions.PlaceWhileOnNewLine) {
1722
 
                                        outputFormatter.Indent();
1723
 
                                } else {
1724
 
                                        outputFormatter.Space();
1725
 
                                }
1726
 
                                PrintLoopCheck(doLoopStatement);
1727
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1728
 
                                outputFormatter.NewLine();
1729
 
                        }
1730
 
                        
1731
 
                        return null;
1732
 
                }
1733
 
                
1734
 
                public override object TrackedVisitForeachStatement(ForeachStatement foreachStatement, object data)
1735
 
                {
1736
 
                        outputFormatter.PrintToken(Tokens.Foreach);
1737
 
                        if (this.prettyPrintOptions.ForeachParentheses) {
1738
 
                                outputFormatter.Space();
1739
 
                        }
1740
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1741
 
                        if (this.prettyPrintOptions.WithinForEachParentheses) {
1742
 
                                outputFormatter.Space();
1743
 
                        }
1744
 
                        TrackVisit(foreachStatement.TypeReference, data);
1745
 
                        outputFormatter.Space();
1746
 
                        outputFormatter.PrintIdentifier(foreachStatement.VariableName);
1747
 
                        outputFormatter.Space();
1748
 
                        outputFormatter.PrintToken(Tokens.In);
1749
 
                        outputFormatter.Space();
1750
 
                        TrackVisit(foreachStatement.Expression, data);
1751
 
                        if (this.prettyPrintOptions.WithinForEachParentheses) {
1752
 
                                outputFormatter.Space();
1753
 
                        }
1754
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1755
 
                        
1756
 
                        WriteEmbeddedStatement(foreachStatement.EmbeddedStatement, prettyPrintOptions.ForEachBraceForcement, prettyPrintOptions.StatementBraceStyle, true);
1757
 
                        
1758
 
                        return null;
1759
 
                }
1760
 
                
1761
 
                public override object TrackedVisitLockStatement(LockStatement lockStatement, object data)
1762
 
                {
1763
 
                        outputFormatter.PrintToken(Tokens.Lock);
1764
 
                        if (this.prettyPrintOptions.LockParentheses) {
1765
 
                                outputFormatter.Space();
1766
 
                        }
1767
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1768
 
                        if (this.prettyPrintOptions.WithinLockParentheses) {
1769
 
                                outputFormatter.Space();
1770
 
                        }
1771
 
                        TrackVisit(lockStatement.LockExpression, data);
1772
 
                        if (this.prettyPrintOptions.WithinLockParentheses) {
1773
 
                                outputFormatter.Space();
1774
 
                        }
1775
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1776
 
                        
1777
 
                        WriteEmbeddedStatement(lockStatement.EmbeddedStatement);
1778
 
                        
1779
 
                        return null;
1780
 
                }
1781
 
                
1782
 
                public override object TrackedVisitUsingStatement(UsingStatement usingStatement, object data)
1783
 
                {
1784
 
                        outputFormatter.PrintToken(Tokens.Using);
1785
 
                        if (this.prettyPrintOptions.UsingParentheses) {
1786
 
                                outputFormatter.Space();
1787
 
                        }
1788
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1789
 
                        if (prettyPrintOptions.WithinUsingParentheses) {
1790
 
                                outputFormatter.Space();
1791
 
                        }
1792
 
                        PrintStatementInline(usingStatement.ResourceAcquisition, data);
1793
 
                        if (prettyPrintOptions.WithinUsingParentheses) {
1794
 
                                outputFormatter.Space();
1795
 
                        }
1796
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1797
 
                        
1798
 
                        WriteEmbeddedStatement(usingStatement.EmbeddedStatement, prettyPrintOptions.UsingBraceForcement, prettyPrintOptions.StatementBraceStyle, true);
1799
 
                        
1800
 
                        return null;
1801
 
                }
1802
 
                
1803
 
                void PrintStatementInline(Statement statement, object data)
1804
 
                {
1805
 
                        outputFormatter.DoIndent = false;
1806
 
                        outputFormatter.DoNewLine = false;
1807
 
                        outputFormatter.EmitSemicolon = false;
1808
 
                        TrackVisit(statement, data);
1809
 
                        outputFormatter.DoIndent = true;
1810
 
                        outputFormatter.DoNewLine = true;
1811
 
                        outputFormatter.EmitSemicolon = true;
1812
 
                }
1813
 
                
1814
 
                public override object TrackedVisitWithStatement(WithStatement withStatement, object data)
1815
 
                {
1816
 
                        NotSupported(withStatement);
1817
 
                        return null;
1818
 
                }
1819
 
                
1820
 
                public override object TrackedVisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
1821
 
                {
1822
 
                        outputFormatter.PrintToken(Tokens.Try);
1823
 
                        
1824
 
                        WriteEmbeddedStatement (tryCatchStatement.StatementBlock, prettyPrintOptions.PlaceCatchOnNewLine);
1825
 
                        for (int i = 0 ; i < tryCatchStatement.CatchClauses.Count; i++) {
1826
 
                                TrackVisit(tryCatchStatement.CatchClauses[i], i == tryCatchStatement.CatchClauses.Count - 1);
1827
 
                        }
1828
 
                        
1829
 
                        if (!tryCatchStatement.FinallyBlock.IsNull) {
1830
 
                                if (prettyPrintOptions.PlaceFinallyOnNewLine) {
1831
 
                                        //                              if (!prettyPrintOptions.PlaceCatchOnNewLine)
1832
 
                                        //                                      outputFormatter.NewLine ();
1833
 
                                        outputFormatter.Indent();
1834
 
                                } else {
1835
 
                                        outputFormatter.Space();
1836
 
                                }
1837
 
                                outputFormatter.PrintToken(Tokens.Finally);
1838
 
                                WriteEmbeddedStatement(tryCatchStatement.FinallyBlock);
1839
 
                        }
1840
 
                        
1841
 
                        return null;
1842
 
                }
1843
 
                
1844
 
                public override object TrackedVisitCatchClause(CatchClause catchClause, object data)
1845
 
                {
1846
 
                        if (prettyPrintOptions.PlaceCatchOnNewLine) {
1847
 
                                outputFormatter.Indent();
1848
 
                        } else {
1849
 
                                outputFormatter.Space();
1850
 
                        }
1851
 
                        outputFormatter.PrintToken(Tokens.Catch);
1852
 
                        
1853
 
                        if (!catchClause.TypeReference.IsNull) {
1854
 
                                if (this.prettyPrintOptions.CatchParentheses) {
1855
 
                                        outputFormatter.Space();
1856
 
                                }
1857
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
1858
 
                                if (this.prettyPrintOptions.WithinCatchParentheses) {
1859
 
                                        outputFormatter.Space();
1860
 
                                }
1861
 
                                outputFormatter.PrintIdentifier(catchClause.TypeReference.Type);
1862
 
                                if (catchClause.VariableName.Length > 0) {
1863
 
                                        outputFormatter.Space();
1864
 
                                        outputFormatter.PrintIdentifier(catchClause.VariableName);
1865
 
                                }
1866
 
                                if (this.prettyPrintOptions.WithinCatchParentheses) {
1867
 
                                        outputFormatter.Space();
1868
 
                                }
1869
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
1870
 
                        }
1871
 
                        WriteEmbeddedStatement(catchClause.StatementBlock, ((bool)data) ? prettyPrintOptions.PlaceFinallyOnNewLine : prettyPrintOptions.PlaceCatchOnNewLine);
1872
 
                        return null;
1873
 
                }
1874
 
                
1875
 
                public override object TrackedVisitThrowStatement(ThrowStatement throwStatement, object data)
1876
 
                {
1877
 
                        outputFormatter.PrintToken(Tokens.Throw);
1878
 
                        if (!throwStatement.Expression.IsNull) {
1879
 
                                outputFormatter.Space();
1880
 
                                TrackVisit(throwStatement.Expression, data);
1881
 
                        }
1882
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1883
 
                        return null;
1884
 
                }
1885
 
                
1886
 
                public override object TrackedVisitFixedStatement(FixedStatement fixedStatement, object data)
1887
 
                {
1888
 
                        outputFormatter.PrintToken(Tokens.Fixed);
1889
 
                        if (this.prettyPrintOptions.FixedParentheses) {
1890
 
                                outputFormatter.Space();
1891
 
                        }
1892
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1893
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
1894
 
                                outputFormatter.Space();
1895
 
                        }
1896
 
                        PrintStatementInline(fixedStatement.PointerDeclaration, data);
1897
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
1898
 
                                outputFormatter.Space();
1899
 
                        }
1900
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1901
 
                        
1902
 
                        WriteEmbeddedStatement(fixedStatement.EmbeddedStatement, prettyPrintOptions.FixedBraceForcement, prettyPrintOptions.StatementBraceStyle, true);
1903
 
                        return null;
1904
 
                }
1905
 
                
1906
 
                public override object TrackedVisitUnsafeStatement(UnsafeStatement unsafeStatement, object data)
1907
 
                {
1908
 
                        outputFormatter.PrintToken(Tokens.Unsafe);
1909
 
                        WriteEmbeddedStatement(unsafeStatement.Block);
1910
 
                        return null;
1911
 
                }
1912
 
                
1913
 
                public override object TrackedVisitCheckedStatement(CheckedStatement checkedStatement, object data)
1914
 
                {
1915
 
                        outputFormatter.PrintToken(Tokens.Checked);
1916
 
                        WriteEmbeddedStatement(checkedStatement.Block);
1917
 
                        return null;
1918
 
                }
1919
 
                
1920
 
                public override object TrackedVisitUncheckedStatement(UncheckedStatement uncheckedStatement, object data)
1921
 
                {
1922
 
                        outputFormatter.PrintToken(Tokens.Unchecked);
1923
 
                        WriteEmbeddedStatement(uncheckedStatement.Block);
1924
 
                        return null;
1925
 
                }
1926
 
                
1927
 
                public override object TrackedVisitExitStatement(ExitStatement exitStatement, object data)
1928
 
                {
1929
 
                        if (exitStatement.ExitType == ExitType.Function || exitStatement.ExitType == ExitType.Sub || exitStatement.ExitType == ExitType.Property) {
1930
 
                                outputFormatter.PrintToken(Tokens.Return);
1931
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1932
 
                        } else {
1933
 
                                outputFormatter.PrintToken(Tokens.Break);
1934
 
                                outputFormatter.PrintToken(Tokens.Semicolon);
1935
 
                                outputFormatter.PrintText(" // TODO: might not be correct. Was : Exit " + exitStatement.ExitType);
1936
 
                        }
1937
 
                        outputFormatter.NewLine();
1938
 
                        return null;
1939
 
                }
1940
 
                
1941
 
                public override object TrackedVisitForNextStatement(ForNextStatement forNextStatement, object data)
1942
 
                {
1943
 
                        outputFormatter.PrintToken(Tokens.For);
1944
 
                        outputFormatter.Space();
1945
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
1946
 
                        if (this.prettyPrintOptions.WithinForParentheses) {
1947
 
                                outputFormatter.Space();
1948
 
                        }
1949
 
                        if (forNextStatement.LoopVariableExpression.IsNull) {
1950
 
                                if (!forNextStatement.TypeReference.IsNull) {
1951
 
                                        TrackVisit(forNextStatement.TypeReference, data);
1952
 
                                        outputFormatter.Space();
1953
 
                                }
1954
 
                                outputFormatter.PrintIdentifier(forNextStatement.VariableName);
1955
 
                        } else {
1956
 
                                TrackVisit(forNextStatement.LoopVariableExpression, data);
1957
 
                        }
1958
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
1959
 
                                outputFormatter.Space();
1960
 
                        }
1961
 
                        outputFormatter.PrintToken(Tokens.Assign);
1962
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
1963
 
                                outputFormatter.Space();
1964
 
                        }
1965
 
                        TrackVisit(forNextStatement.Start, data);
1966
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1967
 
                        outputFormatter.Space();
1968
 
                        if (forNextStatement.LoopVariableExpression.IsNull)
1969
 
                                outputFormatter.PrintIdentifier(forNextStatement.VariableName);
1970
 
                        else
1971
 
                                TrackVisit(forNextStatement.LoopVariableExpression, data);
1972
 
                        outputFormatter.Space();
1973
 
                        PrimitiveExpression pe = forNextStatement.Step as PrimitiveExpression;
1974
 
                        if ((pe == null || !(pe.Value is int) || ((int)pe.Value) >= 0)
1975
 
                            && !(forNextStatement.Step is UnaryOperatorExpression))
1976
 
                                outputFormatter.PrintToken(Tokens.LessEqual);
1977
 
                        else
1978
 
                                outputFormatter.PrintToken(Tokens.GreaterEqual);
1979
 
                        outputFormatter.Space();
1980
 
                        TrackVisit(forNextStatement.End, data);
1981
 
                        outputFormatter.PrintToken(Tokens.Semicolon);
1982
 
                        outputFormatter.Space();
1983
 
                        if (forNextStatement.LoopVariableExpression.IsNull)
1984
 
                                outputFormatter.PrintIdentifier(forNextStatement.VariableName);
1985
 
                        else
1986
 
                                TrackVisit(forNextStatement.LoopVariableExpression, data);
1987
 
                        if (forNextStatement.Step.IsNull) {
1988
 
                                outputFormatter.PrintToken(Tokens.Increment);
1989
 
                        } else {
1990
 
                                outputFormatter.Space();
1991
 
                                outputFormatter.PrintToken(Tokens.PlusAssign);
1992
 
                                outputFormatter.Space();
1993
 
                                TrackVisit(forNextStatement.Step, data);
1994
 
                        }
1995
 
                        if (this.prettyPrintOptions.WithinForParentheses) {
1996
 
                                outputFormatter.Space();
1997
 
                        }
1998
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
1999
 
                        
2000
 
                        WriteEmbeddedStatement(forNextStatement.EmbeddedStatement, prettyPrintOptions.ForBraceForcement, prettyPrintOptions.StatementBraceStyle, true);
2001
 
                        return null;
2002
 
                }
2003
 
                #endregion
2004
 
                
2005
 
                #region Expressions
2006
 
                public override object TrackedVisitClassReferenceExpression(ClassReferenceExpression classReferenceExpression, object data)
2007
 
                {
2008
 
                        NotSupported(classReferenceExpression);
2009
 
                        return null;
2010
 
                }
2011
 
                
2012
 
                static string ConvertCharLiteral(char ch)
2013
 
                {
2014
 
                        if (ch == '\'') return "\\'";
2015
 
                        return ConvertChar(ch);
2016
 
                }
2017
 
                
2018
 
                static string ConvertChar(char ch)
2019
 
                {
2020
 
                        switch (ch) {
2021
 
                                case '\\':
2022
 
                                        return "\\\\";
2023
 
                                case '\0':
2024
 
                                        return "\\0";
2025
 
                                case '\a':
2026
 
                                        return "\\a";
2027
 
                                case '\b':
2028
 
                                        return "\\b";
2029
 
                                case '\f':
2030
 
                                        return "\\f";
2031
 
                                case '\n':
2032
 
                                        return "\\n";
2033
 
                                case '\r':
2034
 
                                        return "\\r";
2035
 
                                case '\t':
2036
 
                                        return "\\t";
2037
 
                                case '\v':
2038
 
                                        return "\\v";
2039
 
                                default:
2040
 
                                        if (char.IsControl(ch)) {
2041
 
                                                return "\\u" + ((int)ch).ToString("x4");
2042
 
                                        } else {
2043
 
                                                return ch.ToString();
2044
 
                                        }
2045
 
                        }
2046
 
                }
2047
 
                
2048
 
                static string ConvertString(string str)
2049
 
                {
2050
 
                        StringBuilder sb = new StringBuilder();
2051
 
                        foreach (char ch in str) {
2052
 
                                if (ch == '"')
2053
 
                                        sb.Append("\\\"");
2054
 
                                else
2055
 
                                        sb.Append(ConvertChar(ch));
2056
 
                        }
2057
 
                        return sb.ToString();
2058
 
                }
2059
 
                
2060
 
                public override object TrackedVisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
2061
 
                {
2062
 
                        outputFormatter.PrintText(ToCSharpString(primitiveExpression));
2063
 
                        return null;
2064
 
                }
2065
 
                
2066
 
                internal static string ToCSharpString(PrimitiveExpression primitiveExpression)
2067
 
                {
2068
 
                        if (primitiveExpression.Value == null) 
2069
 
                                return "null";
2070
 
                        if (primitiveExpression.HasStringValue)
2071
 
                                return primitiveExpression.StringValue;
2072
 
                        object val = primitiveExpression.Value;
2073
 
                        
2074
 
                        if (val is bool) {
2075
 
                                if ((bool)val) {
2076
 
                                        return "true";
2077
 
                                } else {
2078
 
                                        return "false";
2079
 
                                }
2080
 
                        }
2081
 
                        
2082
 
                        if (val is string) {
2083
 
                                if (primitiveExpression.LiteralFormat == LiteralFormat.VerbatimStringLiteral)
2084
 
                                        return "@\"" + val.ToString() + "\"";
2085
 
                                return "\"" + ConvertString(val.ToString()) + "\"";
2086
 
                        }
2087
 
                        
2088
 
                        if (val is char) {
2089
 
                                return "'" + ConvertCharLiteral((char)val) + "'";
2090
 
                        }
2091
 
                        
2092
 
                        if (val is decimal) {
2093
 
                                return ((decimal)val).ToString(NumberFormatInfo.InvariantInfo) + "m";
2094
 
                        }
2095
 
                        
2096
 
                        if (val is float) {
2097
 
                                return ((float)val).ToString(NumberFormatInfo.InvariantInfo) + "f";
2098
 
                        }
2099
 
                        
2100
 
                        if (val is double) {
2101
 
                                string text = ((double)val).ToString(NumberFormatInfo.InvariantInfo);
2102
 
                                if (text.IndexOf('.') < 0 && text.IndexOf('E') < 0)
2103
 
                                        return text + ".0";
2104
 
                                else
2105
 
                                        return text;
2106
 
                        }
2107
 
                        
2108
 
                        if (val is IFormattable) {
2109
 
                                StringBuilder b = new StringBuilder();
2110
 
                                if (primitiveExpression.LiteralFormat == LiteralFormat.HexadecimalNumber) {
2111
 
                                        b.Append("0x");
2112
 
                                        b.Append(((IFormattable)val).ToString("x", NumberFormatInfo.InvariantInfo));
2113
 
                                } else {
2114
 
                                        b.Append(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo));
2115
 
                                }
2116
 
                                if (val is uint || val is ulong) {
2117
 
                                        b.Append("u");
2118
 
                                }
2119
 
                                if (val is long || val is ulong) {
2120
 
                                        b.Append("L");
2121
 
                                }
2122
 
                                return b.ToString();
2123
 
                        } else {
2124
 
                                return val.ToString();
2125
 
                        }
2126
 
                }
2127
 
                
2128
 
                static bool IsNullLiteralExpression(Expression expr)
2129
 
                {
2130
 
                        PrimitiveExpression pe = expr as PrimitiveExpression;
2131
 
                        if (pe == null) return false;
2132
 
                        return pe.Value == null;
2133
 
                }
2134
 
                
2135
 
                public override object TrackedVisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
2136
 
                {
2137
 
                        // VB-operators that require special representation:
2138
 
                        switch (binaryOperatorExpression.Op) {
2139
 
                                case BinaryOperatorType.ReferenceEquality:
2140
 
                                case BinaryOperatorType.ReferenceInequality:
2141
 
                                        if (IsNullLiteralExpression(binaryOperatorExpression.Left) || IsNullLiteralExpression(binaryOperatorExpression.Right)) {
2142
 
                                                // prefer a == null to object.ReferenceEquals(a, null)
2143
 
                                                break;
2144
 
                                        }
2145
 
                                        
2146
 
                                        if (binaryOperatorExpression.Op == BinaryOperatorType.ReferenceInequality)
2147
 
                                                outputFormatter.PrintToken(Tokens.Not);
2148
 
                                        outputFormatter.PrintText("object.ReferenceEquals");
2149
 
                                        if (prettyPrintOptions.BeforeMethodCallParentheses) {
2150
 
                                                outputFormatter.Space();
2151
 
                                        }
2152
 
                                        
2153
 
                                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2154
 
                                        TrackVisit(binaryOperatorExpression.Left, data);
2155
 
                                        PrintFormattedComma();
2156
 
                                        TrackVisit(binaryOperatorExpression.Right, data);
2157
 
                                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2158
 
                                        return null;
2159
 
                                case BinaryOperatorType.Power:
2160
 
                                        outputFormatter.PrintText("Math.Pow");
2161
 
                                        if (prettyPrintOptions.BeforeMethodCallParentheses) {
2162
 
                                                outputFormatter.Space();
2163
 
                                        }
2164
 
                                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2165
 
                                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2166
 
                                                outputFormatter.Space();
2167
 
                                        }
2168
 
                                        TrackVisit(binaryOperatorExpression.Left, data);
2169
 
                                        PrintFormattedComma();
2170
 
                                        TrackVisit(binaryOperatorExpression.Right, data);
2171
 
                                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2172
 
                                                outputFormatter.Space();
2173
 
                                        }
2174
 
                                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2175
 
                                        return null;
2176
 
                                case BinaryOperatorType.DictionaryAccess:
2177
 
                                        TrackVisit(binaryOperatorExpression.Left, data);
2178
 
                                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
2179
 
                                        TrackVisit(binaryOperatorExpression.Right, data);
2180
 
                                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
2181
 
                                        return null;
2182
 
                        }
2183
 
                        TrackVisit(binaryOperatorExpression.Left, data);
2184
 
                        switch (binaryOperatorExpression.Op) {
2185
 
                                case BinaryOperatorType.Add:
2186
 
                                case BinaryOperatorType.Concat: // translate Concatenation to +
2187
 
                                        if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
2188
 
                                                outputFormatter.Space();
2189
 
                                        }
2190
 
                                        outputFormatter.PrintToken(Tokens.Plus);
2191
 
                                        if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
2192
 
                                                outputFormatter.Space();
2193
 
                                        }
2194
 
                                        break;
2195
 
                                        
2196
 
                                case BinaryOperatorType.Subtract:
2197
 
                                        if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
2198
 
                                                outputFormatter.Space();
2199
 
                                        }
2200
 
                                        outputFormatter.PrintToken(Tokens.Minus);
2201
 
                                        if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
2202
 
                                                outputFormatter.Space();
2203
 
                                        }
2204
 
                                        break;
2205
 
                                        
2206
 
                                case BinaryOperatorType.Multiply:
2207
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2208
 
                                                outputFormatter.Space();
2209
 
                                        }
2210
 
                                        outputFormatter.PrintToken(Tokens.Times);
2211
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2212
 
                                                outputFormatter.Space();
2213
 
                                        }
2214
 
                                        break;
2215
 
                                        
2216
 
                                case BinaryOperatorType.Divide:
2217
 
                                case BinaryOperatorType.DivideInteger:
2218
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2219
 
                                                outputFormatter.Space();
2220
 
                                        }
2221
 
                                        outputFormatter.PrintToken(Tokens.Div);
2222
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2223
 
                                                outputFormatter.Space();
2224
 
                                        }
2225
 
                                        break;
2226
 
                                        
2227
 
                                case BinaryOperatorType.Modulus:
2228
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2229
 
                                                outputFormatter.Space();
2230
 
                                        }
2231
 
                                        outputFormatter.PrintToken(Tokens.Mod);
2232
 
                                        if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
2233
 
                                                outputFormatter.Space();
2234
 
                                        }
2235
 
                                        break;
2236
 
                                        
2237
 
                                case BinaryOperatorType.ShiftLeft:
2238
 
                                        if (prettyPrintOptions.AroundShiftOperatorParentheses) {
2239
 
                                                outputFormatter.Space();
2240
 
                                        }
2241
 
                                        outputFormatter.PrintToken(Tokens.ShiftLeft);
2242
 
                                        if (prettyPrintOptions.AroundShiftOperatorParentheses) {
2243
 
                                                outputFormatter.Space();
2244
 
                                        }
2245
 
                                        break;
2246
 
                                        
2247
 
                                case BinaryOperatorType.ShiftRight:
2248
 
                                        if (prettyPrintOptions.AroundShiftOperatorParentheses) {
2249
 
                                                outputFormatter.Space();
2250
 
                                        }
2251
 
                                        outputFormatter.PrintToken(Tokens.GreaterThan);
2252
 
                                        outputFormatter.PrintToken(Tokens.GreaterThan);
2253
 
                                        if (prettyPrintOptions.AroundShiftOperatorParentheses) {
2254
 
                                                outputFormatter.Space();
2255
 
                                        }
2256
 
                                        break;
2257
 
                                        
2258
 
                                case BinaryOperatorType.BitwiseAnd:
2259
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2260
 
                                                outputFormatter.Space();
2261
 
                                        }
2262
 
                                        outputFormatter.PrintToken(Tokens.BitwiseAnd);
2263
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2264
 
                                                outputFormatter.Space();
2265
 
                                        }
2266
 
                                        break;
2267
 
                                case BinaryOperatorType.BitwiseOr:
2268
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2269
 
                                                outputFormatter.Space();
2270
 
                                        }
2271
 
                                        outputFormatter.PrintToken(Tokens.BitwiseOr);
2272
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2273
 
                                                outputFormatter.Space();
2274
 
                                        }
2275
 
                                        break;
2276
 
                                case BinaryOperatorType.ExclusiveOr:
2277
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2278
 
                                                outputFormatter.Space();
2279
 
                                        }
2280
 
                                        outputFormatter.PrintToken(Tokens.Xor);
2281
 
                                        if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
2282
 
                                                outputFormatter.Space();
2283
 
                                        }
2284
 
                                        break;
2285
 
                                        
2286
 
                                case BinaryOperatorType.LogicalAnd:
2287
 
                                        if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
2288
 
                                                outputFormatter.Space();
2289
 
                                        }
2290
 
                                        outputFormatter.PrintToken(Tokens.LogicalAnd);
2291
 
                                        if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
2292
 
                                                outputFormatter.Space();
2293
 
                                        }
2294
 
                                        break;
2295
 
                                case BinaryOperatorType.LogicalOr:
2296
 
                                        if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
2297
 
                                                outputFormatter.Space();
2298
 
                                        }
2299
 
                                        outputFormatter.PrintToken(Tokens.LogicalOr);
2300
 
                                        if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
2301
 
                                                outputFormatter.Space();
2302
 
                                        }
2303
 
                                        break;
2304
 
                                        
2305
 
                                case BinaryOperatorType.Equality:
2306
 
                                case BinaryOperatorType.ReferenceEquality:
2307
 
                                        if (prettyPrintOptions.AroundEqualityOperatorParentheses) {
2308
 
                                                outputFormatter.Space();
2309
 
                                        }
2310
 
                                        outputFormatter.PrintToken(Tokens.Equal);
2311
 
                                        if (prettyPrintOptions.AroundEqualityOperatorParentheses) {
2312
 
                                                outputFormatter.Space();
2313
 
                                        }
2314
 
                                        break;
2315
 
                                case BinaryOperatorType.GreaterThan:
2316
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2317
 
                                                outputFormatter.Space();
2318
 
                                        }
2319
 
                                        outputFormatter.PrintToken(Tokens.GreaterThan);
2320
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2321
 
                                                outputFormatter.Space();
2322
 
                                        }
2323
 
                                        break;
2324
 
                                case BinaryOperatorType.GreaterThanOrEqual:
2325
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2326
 
                                                outputFormatter.Space();
2327
 
                                        }
2328
 
                                        outputFormatter.PrintToken(Tokens.GreaterEqual);
2329
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2330
 
                                                outputFormatter.Space();
2331
 
                                        }
2332
 
                                        break;
2333
 
                                case BinaryOperatorType.InEquality:
2334
 
                                case BinaryOperatorType.ReferenceInequality:
2335
 
                                        if (prettyPrintOptions.AroundEqualityOperatorParentheses) {
2336
 
                                                outputFormatter.Space();
2337
 
                                        }
2338
 
                                        outputFormatter.PrintToken(Tokens.NotEqual);
2339
 
                                        if (prettyPrintOptions.AroundEqualityOperatorParentheses) {
2340
 
                                                outputFormatter.Space();
2341
 
                                        }
2342
 
                                        break;
2343
 
                                case BinaryOperatorType.LessThan:
2344
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2345
 
                                                outputFormatter.Space();
2346
 
                                        }
2347
 
                                        outputFormatter.PrintToken(Tokens.LessThan);
2348
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2349
 
                                                outputFormatter.Space();
2350
 
                                        }
2351
 
                                        break;
2352
 
                                case BinaryOperatorType.LessThanOrEqual:
2353
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2354
 
                                                outputFormatter.Space();
2355
 
                                        }
2356
 
                                        outputFormatter.PrintToken(Tokens.LessEqual);
2357
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2358
 
                                                outputFormatter.Space();
2359
 
                                        }
2360
 
                                        break;
2361
 
                                case BinaryOperatorType.NullCoalescing:
2362
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2363
 
                                                outputFormatter.Space();
2364
 
                                        }
2365
 
                                        outputFormatter.PrintToken(Tokens.DoubleQuestion);
2366
 
                                        if (prettyPrintOptions.AroundRelationalOperatorParentheses) {
2367
 
                                                outputFormatter.Space();
2368
 
                                        }
2369
 
                                        break;
2370
 
                                        
2371
 
                                default:
2372
 
                                        Error(binaryOperatorExpression, String.Format("Unknown binary operator {0}", binaryOperatorExpression.Op));
2373
 
                                        return null;
2374
 
                        }
2375
 
                        TrackVisit(binaryOperatorExpression.Right, data);
2376
 
                        return null;
2377
 
                }
2378
 
                
2379
 
                public override object TrackedVisitParenthesizedExpression(ParenthesizedExpression parenthesizedExpression, object data)
2380
 
                {
2381
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2382
 
                        if (this.prettyPrintOptions.WithinParentheses) {
2383
 
                                outputFormatter.Space();
2384
 
                        }
2385
 
                        TrackVisit(parenthesizedExpression.Expression, data);
2386
 
                        if (this.prettyPrintOptions.WithinParentheses) {
2387
 
                                outputFormatter.Space();
2388
 
                        }
2389
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2390
 
                        return null;
2391
 
                }
2392
 
                
2393
 
                public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data)
2394
 
                {
2395
 
                        TrackVisit(invocationExpression.TargetObject, data);
2396
 
                        
2397
 
                        if (prettyPrintOptions.BeforeMethodCallParentheses) {
2398
 
                                outputFormatter.Space();
2399
 
                        }
2400
 
                        
2401
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2402
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2403
 
                                outputFormatter.Space();
2404
 
                        }
2405
 
                        AppendCommaSeparatedList(invocationExpression.Arguments);
2406
 
                        if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2407
 
                                outputFormatter.Space();
2408
 
                        }
2409
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2410
 
                        return null;
2411
 
                }
2412
 
                
2413
 
                public override object TrackedVisitIdentifierExpression(IdentifierExpression identifierExpression, object data)
2414
 
                {
2415
 
                        outputFormatter.PrintIdentifier(identifierExpression.Identifier);
2416
 
                        PrintTypeArgumentList(identifierExpression.TypeArguments);
2417
 
                        return null;
2418
 
                }
2419
 
                
2420
 
                void PrintTypeArgumentList(List<TypeReference> typeArguments)
2421
 
                {
2422
 
                        if (typeArguments != null && typeArguments.Count > 0) {
2423
 
                                outputFormatter.PrintToken(Tokens.LessThan);
2424
 
                                AppendCommaSeparatedList(typeArguments);
2425
 
                                outputFormatter.PrintToken(Tokens.GreaterThan);
2426
 
                        }
2427
 
                }
2428
 
                
2429
 
                public override object TrackedVisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression, object data)
2430
 
                {
2431
 
                        TrackVisit(typeReferenceExpression.TypeReference, data);
2432
 
                        return null;
2433
 
                }
2434
 
                
2435
 
                public override object TrackedVisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data)
2436
 
                {
2437
 
                        switch (unaryOperatorExpression.Op) {
2438
 
                                case UnaryOperatorType.BitNot:
2439
 
                                        outputFormatter.PrintToken(Tokens.BitwiseComplement);
2440
 
                                        break;
2441
 
                                case UnaryOperatorType.Decrement:
2442
 
                                        outputFormatter.PrintToken(Tokens.Decrement);
2443
 
                                        break;
2444
 
                                case UnaryOperatorType.Increment:
2445
 
                                        outputFormatter.PrintToken(Tokens.Increment);
2446
 
                                        break;
2447
 
                                case UnaryOperatorType.Minus:
2448
 
                                        outputFormatter.PrintToken(Tokens.Minus);
2449
 
                                        break;
2450
 
                                case UnaryOperatorType.Not:
2451
 
                                        outputFormatter.PrintToken(Tokens.Not);
2452
 
                                        break;
2453
 
                                case UnaryOperatorType.Plus:
2454
 
                                        outputFormatter.PrintToken(Tokens.Plus);
2455
 
                                        break;
2456
 
                                case UnaryOperatorType.PostDecrement:
2457
 
                                        TrackVisit(unaryOperatorExpression.Expression, data);
2458
 
                                        outputFormatter.PrintToken(Tokens.Decrement);
2459
 
                                        return null;
2460
 
                                case UnaryOperatorType.PostIncrement:
2461
 
                                        TrackVisit(unaryOperatorExpression.Expression, data);
2462
 
                                        outputFormatter.PrintToken(Tokens.Increment);
2463
 
                                        return null;
2464
 
                                case UnaryOperatorType.Dereference:
2465
 
                                        outputFormatter.PrintToken(Tokens.Times);
2466
 
                                        break;
2467
 
                                case UnaryOperatorType.AddressOf:
2468
 
                                        outputFormatter.PrintToken(Tokens.BitwiseAnd);
2469
 
                                        break;
2470
 
                                default:
2471
 
                                        Error(unaryOperatorExpression, String.Format("Unknown unary operator {0}", unaryOperatorExpression.Op));
2472
 
                                        return null;
2473
 
                        }
2474
 
                        TrackVisit(unaryOperatorExpression.Expression, data);
2475
 
                        return null;
2476
 
                }
2477
 
                
2478
 
                public override object TrackedVisitAssignmentExpression(AssignmentExpression assignmentExpression, object data)
2479
 
                {
2480
 
                        TrackVisit(assignmentExpression.Left, data);
2481
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
2482
 
                                outputFormatter.Space();
2483
 
                        }
2484
 
                        switch (assignmentExpression.Op) {
2485
 
                                case AssignmentOperatorType.Assign:
2486
 
                                        outputFormatter.PrintToken(Tokens.Assign);
2487
 
                                        break;
2488
 
                                case AssignmentOperatorType.Add:
2489
 
                                case AssignmentOperatorType.ConcatString:
2490
 
                                        outputFormatter.PrintToken(Tokens.PlusAssign);
2491
 
                                        break;
2492
 
                                case AssignmentOperatorType.Subtract:
2493
 
                                        outputFormatter.PrintToken(Tokens.MinusAssign);
2494
 
                                        break;
2495
 
                                case AssignmentOperatorType.Multiply:
2496
 
                                        outputFormatter.PrintToken(Tokens.TimesAssign);
2497
 
                                        break;
2498
 
                                case AssignmentOperatorType.Divide:
2499
 
                                case AssignmentOperatorType.DivideInteger:
2500
 
                                        outputFormatter.PrintToken(Tokens.DivAssign);
2501
 
                                        break;
2502
 
                                case AssignmentOperatorType.ShiftLeft:
2503
 
                                        outputFormatter.PrintToken(Tokens.ShiftLeftAssign);
2504
 
                                        break;
2505
 
                                case AssignmentOperatorType.ShiftRight:
2506
 
                                        outputFormatter.PrintToken(Tokens.GreaterThan);
2507
 
                                        outputFormatter.PrintToken(Tokens.GreaterEqual);
2508
 
                                        break;
2509
 
                                case AssignmentOperatorType.ExclusiveOr:
2510
 
                                        outputFormatter.PrintToken(Tokens.XorAssign);
2511
 
                                        break;
2512
 
                                case AssignmentOperatorType.Modulus:
2513
 
                                        outputFormatter.PrintToken(Tokens.ModAssign);
2514
 
                                        break;
2515
 
                                case AssignmentOperatorType.BitwiseAnd:
2516
 
                                        outputFormatter.PrintToken(Tokens.BitwiseAndAssign);
2517
 
                                        break;
2518
 
                                case AssignmentOperatorType.BitwiseOr:
2519
 
                                        outputFormatter.PrintToken(Tokens.BitwiseOrAssign);
2520
 
                                        break;
2521
 
                                case AssignmentOperatorType.Power:
2522
 
                                        outputFormatter.PrintToken(Tokens.Assign);
2523
 
                                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
2524
 
                                                outputFormatter.Space();
2525
 
                                        }
2526
 
                                        VisitBinaryOperatorExpression(new BinaryOperatorExpression(assignmentExpression.Left,
2527
 
                                                                                                   BinaryOperatorType.Power,
2528
 
                                                                                                   assignmentExpression.Right), data);
2529
 
                                        return null;
2530
 
                                default:
2531
 
                                        Error(assignmentExpression, String.Format("Unknown assignment operator {0}", assignmentExpression.Op));
2532
 
                                        return null;
2533
 
                        }
2534
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
2535
 
                                outputFormatter.Space();
2536
 
                        }
2537
 
                        TrackVisit(assignmentExpression.Right, data);
2538
 
                        return null;
2539
 
                }
2540
 
                
2541
 
                public override object TrackedVisitSizeOfExpression(SizeOfExpression sizeOfExpression, object data)
2542
 
                {
2543
 
                        outputFormatter.PrintToken(Tokens.Sizeof);
2544
 
                        if (prettyPrintOptions.SizeOfParentheses) {
2545
 
                                outputFormatter.Space();
2546
 
                        }
2547
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2548
 
                        if (this.prettyPrintOptions.WithinSizeOfParentheses) {
2549
 
                                outputFormatter.Space();
2550
 
                        }
2551
 
                        TrackVisit(sizeOfExpression.TypeReference, data);
2552
 
                        if (this.prettyPrintOptions.WithinSizeOfParentheses) {
2553
 
                                outputFormatter.Space();
2554
 
                        }
2555
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2556
 
                        return null;
2557
 
                }
2558
 
                
2559
 
                public override object TrackedVisitTypeOfExpression(TypeOfExpression typeOfExpression, object data)
2560
 
                {
2561
 
                        outputFormatter.PrintToken(Tokens.Typeof);
2562
 
                        if (prettyPrintOptions.TypeOfParentheses) {
2563
 
                                outputFormatter.Space();
2564
 
                        }
2565
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2566
 
                        if (this.prettyPrintOptions.WithinTypeOfParentheses) {
2567
 
                                outputFormatter.Space();
2568
 
                        }
2569
 
                        TrackVisit(typeOfExpression.TypeReference, data);
2570
 
                        if (this.prettyPrintOptions.WithinTypeOfParentheses) {
2571
 
                                outputFormatter.Space();
2572
 
                        }
2573
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2574
 
                        return null;
2575
 
                }
2576
 
                
2577
 
                public override object TrackedVisitDefaultValueExpression(DefaultValueExpression defaultValueExpression, object data)
2578
 
                {
2579
 
                        outputFormatter.PrintToken(Tokens.Default);
2580
 
                        if (prettyPrintOptions.TypeOfParentheses) {
2581
 
                                outputFormatter.Space();
2582
 
                        }
2583
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2584
 
                        if (this.prettyPrintOptions.WithinTypeOfParentheses) {
2585
 
                                outputFormatter.Space();
2586
 
                        }
2587
 
                        TrackVisit(defaultValueExpression.TypeReference, data);
2588
 
                        if (this.prettyPrintOptions.WithinTypeOfParentheses) {
2589
 
                                outputFormatter.Space();
2590
 
                        }
2591
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2592
 
                        return null;
2593
 
                }
2594
 
                
2595
 
                public override object TrackedVisitTypeOfIsExpression(TypeOfIsExpression typeOfIsExpression, object data)
2596
 
                {
2597
 
                        TrackVisit(typeOfIsExpression.Expression, data);
2598
 
                        outputFormatter.Space();
2599
 
                        outputFormatter.PrintToken(Tokens.Is);
2600
 
                        outputFormatter.Space();
2601
 
                        TrackVisit(typeOfIsExpression.TypeReference, data);
2602
 
                        return null;
2603
 
                }
2604
 
                
2605
 
                public override object TrackedVisitAddressOfExpression(AddressOfExpression addressOfExpression, object data)
2606
 
                {
2607
 
                        // C# 2.0 can reference methods directly
2608
 
                        return TrackVisit(addressOfExpression.Expression, data);
2609
 
                }
2610
 
                
2611
 
                public override object TrackedVisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data)
2612
 
                {
2613
 
                        outputFormatter.PrintToken(Tokens.Delegate);
2614
 
                        
2615
 
                        if (anonymousMethodExpression.Parameters.Count > 0 || anonymousMethodExpression.HasParameterList) {
2616
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2617
 
                                bool withinParentheses = this.prettyPrintOptions.WithinMethodDeclarationParentheses && anonymousMethodExpression.Parameters.Any ();
2618
 
                                if (withinParentheses) {
2619
 
                                        outputFormatter.Space();
2620
 
                                }
2621
 
                                AppendCommaSeparatedList(anonymousMethodExpression.Parameters);
2622
 
                                if (withinParentheses) {
2623
 
                                        outputFormatter.Space();
2624
 
                                }
2625
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2626
 
                        }
2627
 
                        OutputBlockAllowInline(anonymousMethodExpression.Body, this.prettyPrintOptions.AnonymousMethodBraceStyle, false);
2628
 
                        return null;
2629
 
                }
2630
 
                
2631
 
                public override object TrackedVisitLambdaExpression(LambdaExpression lambdaExpression, object data)
2632
 
                {
2633
 
                        if (lambdaExpression.Parameters.Count == 1 && lambdaExpression.Parameters[0].TypeReference.IsNull) {
2634
 
                                // short syntax
2635
 
                                outputFormatter.PrintIdentifier(lambdaExpression.Parameters[0].ParameterName);
2636
 
                        } else {
2637
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2638
 
                                if (this.prettyPrintOptions.WithinParentheses) {
2639
 
                                        outputFormatter.Space();
2640
 
                                }
2641
 
                                AppendCommaSeparatedList(lambdaExpression.Parameters);
2642
 
                                if (this.prettyPrintOptions.WithinParentheses) {
2643
 
                                        outputFormatter.Space();
2644
 
                                }
2645
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2646
 
                        }
2647
 
                        outputFormatter.Space();
2648
 
                        outputFormatter.PrintToken(Tokens.LambdaArrow);
2649
 
                        if (!lambdaExpression.ExpressionBody.IsNull) {
2650
 
                                outputFormatter.Space();
2651
 
                                TrackVisit(lambdaExpression.ExpressionBody, null);
2652
 
                        }
2653
 
                        if (!lambdaExpression.StatementBody.IsNull) {
2654
 
                                OutputBlockAllowInline(lambdaExpression.StatementBody, this.prettyPrintOptions.MethodBraceStyle, false);
2655
 
                        }
2656
 
                        return null;
2657
 
                }
2658
 
                
2659
 
                public override object TrackedVisitCheckedExpression(CheckedExpression checkedExpression, object data)
2660
 
                {
2661
 
                        outputFormatter.PrintToken(Tokens.Checked);
2662
 
                        if (prettyPrintOptions.CheckedParentheses) {
2663
 
                                outputFormatter.Space();
2664
 
                        }
2665
 
                        
2666
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2667
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
2668
 
                                outputFormatter.Space();
2669
 
                        }
2670
 
                        TrackVisit(checkedExpression.Expression, data);
2671
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
2672
 
                                outputFormatter.Space();
2673
 
                        }
2674
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2675
 
                        return null;
2676
 
                }
2677
 
                
2678
 
                public override object TrackedVisitUncheckedExpression(UncheckedExpression uncheckedExpression, object data)
2679
 
                {
2680
 
                        outputFormatter.PrintToken(Tokens.Unchecked);
2681
 
                        if (prettyPrintOptions.UncheckedParentheses) {
2682
 
                                outputFormatter.Space();
2683
 
                        }
2684
 
                        outputFormatter.PrintToken(Tokens.OpenParenthesis);
2685
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
2686
 
                                outputFormatter.Space();
2687
 
                        }
2688
 
                        TrackVisit(uncheckedExpression.Expression, data);
2689
 
                        if (this.prettyPrintOptions.WithinCheckedExpressionParantheses) {
2690
 
                                outputFormatter.Space();
2691
 
                        }
2692
 
                        outputFormatter.PrintToken(Tokens.CloseParenthesis);
2693
 
                        return null;
2694
 
                }
2695
 
                
2696
 
                public override object TrackedVisitPointerReferenceExpression(PointerReferenceExpression pointerReferenceExpression, object data)
2697
 
                {
2698
 
                        Expression target = pointerReferenceExpression.TargetObject;
2699
 
                        
2700
 
                        if (target is BinaryOperatorExpression || target is CastExpression) {
2701
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2702
 
                        }
2703
 
                        TrackVisit(target, data);
2704
 
                        if (target is BinaryOperatorExpression || target is CastExpression) {
2705
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2706
 
                        }
2707
 
                        outputFormatter.PrintToken(Tokens.Pointer);
2708
 
                        outputFormatter.PrintIdentifier(pointerReferenceExpression.MemberName);
2709
 
                        PrintTypeArgumentList(pointerReferenceExpression.TypeArguments);
2710
 
                        
2711
 
                        return null;
2712
 
                }
2713
 
                
2714
 
                public override object TrackedVisitCastExpression(CastExpression castExpression, object data)
2715
 
                {
2716
 
                        if (castExpression.CastType == CastType.TryCast) {
2717
 
                                TrackVisit(castExpression.Expression, data);
2718
 
                                outputFormatter.Space();
2719
 
                                outputFormatter.PrintToken(Tokens.As);
2720
 
                                outputFormatter.Space();
2721
 
                                TrackVisit(castExpression.CastTo, data);
2722
 
                        } else {
2723
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2724
 
                                if (prettyPrintOptions.WithinCastParentheses) {
2725
 
                                        outputFormatter.Space();
2726
 
                                }
2727
 
                                TrackVisit(castExpression.CastTo, data);
2728
 
                                if (prettyPrintOptions.WithinCastParentheses) {
2729
 
                                        outputFormatter.Space();
2730
 
                                }
2731
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2732
 
                                if (this.prettyPrintOptions.SpacesAfterTypecast) {
2733
 
                                        outputFormatter.Space();
2734
 
                                }
2735
 
                                TrackVisit(castExpression.Expression, data);
2736
 
                        }
2737
 
                        return null;
2738
 
                }
2739
 
                
2740
 
                public override object TrackedVisitStackAllocExpression(StackAllocExpression stackAllocExpression, object data)
2741
 
                {
2742
 
                        outputFormatter.PrintToken(Tokens.Stackalloc);
2743
 
                        outputFormatter.Space();
2744
 
                        TrackVisit(stackAllocExpression.TypeReference, data);
2745
 
                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
2746
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
2747
 
                                outputFormatter.Space();
2748
 
                        }
2749
 
                        TrackVisit(stackAllocExpression.Expression, data);
2750
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
2751
 
                                outputFormatter.Space();
2752
 
                        }
2753
 
                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
2754
 
                        return null;
2755
 
                }
2756
 
                
2757
 
                public override object TrackedVisitIndexerExpression(IndexerExpression indexerExpression, object data)
2758
 
                {
2759
 
                        TrackVisit(indexerExpression.TargetObject, data);
2760
 
                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
2761
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
2762
 
                                outputFormatter.Space();
2763
 
                        }
2764
 
                        AppendCommaSeparatedList(indexerExpression.Indexes);
2765
 
                        if (this.prettyPrintOptions.SpacesWithinBrackets) {
2766
 
                                outputFormatter.Space();
2767
 
                        }
2768
 
                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
2769
 
                        return null;
2770
 
                }
2771
 
                
2772
 
                public override object TrackedVisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression, object data)
2773
 
                {
2774
 
                        outputFormatter.PrintToken(Tokens.This);
2775
 
                        return null;
2776
 
                }
2777
 
                
2778
 
                public override object TrackedVisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression, object data) {
2779
 
                        outputFormatter.PrintToken(Tokens.Base);
2780
 
                        return null;
2781
 
                }
2782
 
                
2783
 
                public override object TrackedVisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
2784
 
                {
2785
 
                        outputFormatter.PrintToken(Tokens.New);
2786
 
                        if (!objectCreateExpression.CreateType.IsNull) {
2787
 
                                outputFormatter.Space();
2788
 
                                TrackVisit(objectCreateExpression.CreateType, data);
2789
 
                        }
2790
 
                        if (objectCreateExpression.Parameters.Count > 0 || objectCreateExpression.ObjectInitializer.IsNull) {
2791
 
                                if (prettyPrintOptions.NewParentheses) {
2792
 
                                        outputFormatter.Space();
2793
 
                                }
2794
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2795
 
                                if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2796
 
                                        outputFormatter.Space();
2797
 
                                }
2798
 
                                AppendCommaSeparatedList(objectCreateExpression.Parameters);
2799
 
                                if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2800
 
                                        outputFormatter.Space();
2801
 
                                }
2802
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2803
 
                        }
2804
 
                        if (!objectCreateExpression.ObjectInitializer.IsNull) {
2805
 
                                outputFormatter.Space();
2806
 
                                TrackVisit(objectCreateExpression.ObjectInitializer, data);
2807
 
                        }
2808
 
                        return null;
2809
 
                }
2810
 
                
2811
 
                public override object TrackedVisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data)
2812
 
                {
2813
 
                        outputFormatter.PrintToken(Tokens.New);
2814
 
                        if (arrayCreateExpression.IsImplicitlyTyped) {
2815
 
                                outputFormatter.PrintToken(Tokens.OpenSquareBracket);
2816
 
                                outputFormatter.PrintToken(Tokens.CloseSquareBracket);
2817
 
                        } else {
2818
 
                                outputFormatter.Space();
2819
 
                                PrintTypeReferenceWithoutArray(arrayCreateExpression.CreateType);
2820
 
                                
2821
 
                                if (arrayCreateExpression.Arguments.Count > 0) {
2822
 
                                        outputFormatter.PrintToken(Tokens.OpenSquareBracket);
2823
 
                                        bool outputSpace = this.prettyPrintOptions.SpacesWithinBrackets && arrayCreateExpression.Arguments.Count > 0;
2824
 
                                        if (outputSpace) {
2825
 
                                                outputFormatter.Space();
2826
 
                                        }
2827
 
                                        for (int i = 0; i < arrayCreateExpression.Arguments.Count; ++i) {
2828
 
                                                if (i > 0) PrintFormattedComma();
2829
 
                                                TrackVisit(arrayCreateExpression.Arguments[i], data);
2830
 
                                        }
2831
 
                                        if (outputSpace) {
2832
 
                                                outputFormatter.Space();
2833
 
                                        }
2834
 
                                        outputFormatter.PrintToken(Tokens.CloseSquareBracket);
2835
 
                                        PrintArrayRank(arrayCreateExpression.CreateType.RankSpecifier, 1);
2836
 
                                } else {
2837
 
                                        PrintArrayRank(arrayCreateExpression.CreateType.RankSpecifier, 0);
2838
 
                                }
2839
 
                        }
2840
 
                        
2841
 
                        if (!arrayCreateExpression.ArrayInitializer.IsNull) {
2842
 
                                outputFormatter.Space();
2843
 
                                TrackVisit(arrayCreateExpression.ArrayInitializer, data);
2844
 
                        }
2845
 
                        return null;
2846
 
                }
2847
 
                
2848
 
                public override object TrackedVisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data)
2849
 
                {
2850
 
                        Expression target = memberReferenceExpression.TargetObject;
2851
 
                        
2852
 
                        if (target is BinaryOperatorExpression || target is CastExpression) {
2853
 
                                outputFormatter.PrintToken(Tokens.OpenParenthesis);
2854
 
                                if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2855
 
                                        outputFormatter.Space();
2856
 
                                }
2857
 
                        }
2858
 
                        TrackVisit(target, data);
2859
 
                        if (target is BinaryOperatorExpression || target is CastExpression) {
2860
 
                                if (this.prettyPrintOptions.WithinMethodCallParentheses) {
2861
 
                                        outputFormatter.Space();
2862
 
                                }
2863
 
                                outputFormatter.PrintToken(Tokens.CloseParenthesis);
2864
 
                        }
2865
 
                        outputFormatter.PrintToken(Tokens.Dot);
2866
 
                        outputFormatter.PrintIdentifier(memberReferenceExpression.MemberName);
2867
 
                        PrintTypeArgumentList(memberReferenceExpression.TypeArguments);
2868
 
                        return null;
2869
 
                }
2870
 
                
2871
 
                public override object TrackedVisitDirectionExpression(DirectionExpression directionExpression, object data)
2872
 
                {
2873
 
                        switch (directionExpression.FieldDirection) {
2874
 
                                case FieldDirection.Out:
2875
 
                                        outputFormatter.PrintToken(Tokens.Out);
2876
 
                                        outputFormatter.Space();
2877
 
                                        break;
2878
 
                                case FieldDirection.Ref:
2879
 
                                        outputFormatter.PrintToken(Tokens.Ref);
2880
 
                                        outputFormatter.Space();
2881
 
                                        break;
2882
 
                        }
2883
 
                        TrackVisit(directionExpression.Expression, data);
2884
 
                        return null;
2885
 
                }
2886
 
                
2887
 
                public override object TrackedVisitCollectionInitializerExpression(CollectionInitializerExpression arrayInitializerExpression, object data)
2888
 
                {
2889
 
                        outputFormatter.PrintToken (Tokens.OpenCurlyBrace);
2890
 
                        if (arrayInitializerExpression.CreateExpressions.Count == 1) {
2891
 
                                outputFormatter.Space ();
2892
 
                        } else {
2893
 
                                if (this.prettyPrintOptions.PlaceArrayInitializersOnNewLine == ArrayInitializerPlacement.AlwaysNewLine) {
2894
 
                                        outputFormatter.IndentationLevel++;
2895
 
                                        outputFormatter.NewLine ();
2896
 
                                        outputFormatter.Indent ();
2897
 
                                } else {
2898
 
                                        outputFormatter.Space ();
2899
 
                                }
2900
 
                        }
2901
 
                        this.AppendCommaSeparatedList (arrayInitializerExpression.CreateExpressions, this.prettyPrintOptions.PlaceArrayInitializersOnNewLine == ArrayInitializerPlacement.AlwaysNewLine);
2902
 
                        if (arrayInitializerExpression.CreateExpressions.Count == 1) {
2903
 
                                outputFormatter.Space ();
2904
 
                        } else {
2905
 
                                if (this.prettyPrintOptions.PlaceArrayInitializersOnNewLine == ArrayInitializerPlacement.AlwaysNewLine) {
2906
 
                                        outputFormatter.IndentationLevel--;
2907
 
                                        outputFormatter.NewLine();
2908
 
                                        outputFormatter.Indent();
2909
 
                                } else {
2910
 
                                        outputFormatter.Space();
2911
 
                                }
2912
 
                        }
2913
 
                        outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
2914
 
                        return null;
2915
 
                }
2916
 
                
2917
 
                public override object TrackedVisitConditionalExpression(ConditionalExpression conditionalExpression, object data)
2918
 
                {
2919
 
                        TrackVisit(conditionalExpression.Condition, data);
2920
 
                        if (this.prettyPrintOptions.ConditionalOperatorBeforeConditionSpace) {
2921
 
                                outputFormatter.Space();
2922
 
                        }
2923
 
                        outputFormatter.PrintToken(Tokens.Question);
2924
 
                        if (this.prettyPrintOptions.ConditionalOperatorAfterConditionSpace) {
2925
 
                                outputFormatter.Space();
2926
 
                        }
2927
 
                        TrackVisit(conditionalExpression.TrueExpression, data);
2928
 
                        if (this.prettyPrintOptions.ConditionalOperatorBeforeSeparatorSpace) {
2929
 
                                outputFormatter.Space();
2930
 
                        }
2931
 
                        outputFormatter.PrintToken(Tokens.Colon);
2932
 
                        if (this.prettyPrintOptions.ConditionalOperatorAfterSeparatorSpace) {
2933
 
                                outputFormatter.Space();
2934
 
                        }
2935
 
                        TrackVisit(conditionalExpression.FalseExpression, data);
2936
 
                        return null;
2937
 
                }
2938
 
                
2939
 
                #endregion
2940
 
                #endregion
2941
 
                
2942
 
                void OutputModifier(ParameterModifiers modifier, INode node)
2943
 
                {
2944
 
                        if ((modifier & ParameterModifiers.Ref) == ParameterModifiers.Ref) {
2945
 
                                outputFormatter.PrintToken(Tokens.Ref);
2946
 
                                outputFormatter.Space();
2947
 
                        } else if ((modifier & ParameterModifiers.Out) == ParameterModifiers.Out) {
2948
 
                                outputFormatter.PrintToken(Tokens.Out);
2949
 
                                outputFormatter.Space();
2950
 
                        }
2951
 
                        if ((modifier & ParameterModifiers.Params) == ParameterModifiers.Params) {
2952
 
                                outputFormatter.PrintToken(Tokens.Params);
2953
 
                                outputFormatter.Space();
2954
 
                        }
2955
 
                        if ((modifier & ParameterModifiers.Optional) == ParameterModifiers.Optional) {
2956
 
                                Error(node, String.Format("Optional parameters aren't supported in C#"));
2957
 
                        }
2958
 
                }
2959
 
                
2960
 
                void OutputModifier(Modifiers modifier)
2961
 
                {
2962
 
                        ArrayList tokenList = new ArrayList();
2963
 
                        if ((modifier & Modifiers.Unsafe) != 0) {
2964
 
                                tokenList.Add(Tokens.Unsafe);
2965
 
                        }
2966
 
                        if ((modifier & Modifiers.Public) != 0) {
2967
 
                                tokenList.Add(Tokens.Public);
2968
 
                        }
2969
 
                        if ((modifier & Modifiers.Private) != 0) {
2970
 
                                tokenList.Add(Tokens.Private);
2971
 
                        }
2972
 
                        if ((modifier & Modifiers.Protected) != 0) {
2973
 
                                tokenList.Add(Tokens.Protected);
2974
 
                        }
2975
 
                        if ((modifier & Modifiers.Static) != 0) {
2976
 
                                tokenList.Add(Tokens.Static);
2977
 
                        }
2978
 
                        if ((modifier & Modifiers.Internal) != 0) {
2979
 
                                tokenList.Add(Tokens.Internal);
2980
 
                        }
2981
 
                        if ((modifier & Modifiers.Override) != 0) {
2982
 
                                tokenList.Add(Tokens.Override);
2983
 
                        }
2984
 
                        if ((modifier & Modifiers.Abstract) != 0) {
2985
 
                                tokenList.Add(Tokens.Abstract);
2986
 
                        }
2987
 
                        if ((modifier & Modifiers.Virtual) != 0) {
2988
 
                                tokenList.Add(Tokens.Virtual);
2989
 
                        }
2990
 
                        if ((modifier & Modifiers.New) != 0) {
2991
 
                                tokenList.Add(Tokens.New);
2992
 
                        }
2993
 
                        if ((modifier & Modifiers.Sealed) != 0) {
2994
 
                                tokenList.Add(Tokens.Sealed);
2995
 
                        }
2996
 
                        if ((modifier & Modifiers.Extern) != 0) {
2997
 
                                tokenList.Add(Tokens.Extern);
2998
 
                        }
2999
 
                        if ((modifier & Modifiers.Const) != 0) {
3000
 
                                tokenList.Add(Tokens.Const);
3001
 
                        }
3002
 
                        if ((modifier & Modifiers.ReadOnly) != 0) {
3003
 
                                tokenList.Add(Tokens.Readonly);
3004
 
                        }
3005
 
                        if ((modifier & Modifiers.Volatile) != 0) {
3006
 
                                tokenList.Add(Tokens.Volatile);
3007
 
                        }
3008
 
                        if ((modifier & Modifiers.Fixed) != 0) {
3009
 
                                tokenList.Add(Tokens.Fixed);
3010
 
                        }
3011
 
                        outputFormatter.PrintTokenList(tokenList);
3012
 
                        
3013
 
                        if ((modifier & Modifiers.Partial) != 0) {
3014
 
                                outputFormatter.PrintText("partial ");
3015
 
                        }
3016
 
                }
3017
 
                
3018
 
                object TrackVisit(INode node, object data)
3019
 
                {
3020
 
                        return node.AcceptVisitor(this, data);
3021
 
                }
3022
 
                
3023
 
                /// <summary>
3024
 
                /// Resets the output formatter, sets Text to string.Empty.
3025
 
                /// </summary>
3026
 
                public void Reset ()
3027
 
                {
3028
 
                        outputFormatter.Reset ();
3029
 
                }
3030
 
                
3031
 
                public void AppendCommaSeparatedList<T>(ICollection<T> list) where T : class, INode
3032
 
                {
3033
 
                        AppendCommaSeparatedList(list, false);
3034
 
                }
3035
 
                
3036
 
                public void AppendCommaSeparatedList<T>(ICollection<T> list, bool alwaysBreakLine) where T : class, INode
3037
 
                {
3038
 
                        if (list != null) {
3039
 
                                int i = 0;
3040
 
                                foreach (T node in list) {
3041
 
                                        node.AcceptVisitor(this, null);
3042
 
                                        if (i + 1 < list.Count) {
3043
 
                                                if (alwaysBreakLine || (i + 1) % 10 == 0) {
3044
 
                                                        PrintFormattedCommaAndNewLine();
3045
 
                                                } else {
3046
 
                                                        PrintFormattedComma();
3047
 
                                                }
3048
 
                                        }
3049
 
                                        i++;
3050
 
                                }
3051
 
                        }
3052
 
                }
3053
 
                
3054
 
                public override object TrackedVisitQueryExpression(QueryExpression queryExpression, object data)
3055
 
                {
3056
 
                        if (queryExpression.IsQueryContinuation) {
3057
 
                                queryExpression.FromClause.InExpression.AcceptVisitor(this, data);
3058
 
                        }
3059
 
                        outputFormatter.IndentationLevel++;
3060
 
                        if (queryExpression.IsQueryContinuation) {
3061
 
                                outputFormatter.Space();
3062
 
                                outputFormatter.PrintToken(Tokens.Into);
3063
 
                                outputFormatter.Space();
3064
 
                                outputFormatter.PrintIdentifier(queryExpression.FromClause.Identifier);
3065
 
                        } else {
3066
 
                                queryExpression.FromClause.AcceptVisitor(this, data);
3067
 
                        }
3068
 
                        queryExpression.MiddleClauses.ForEach(PrintClause);
3069
 
                        PrintClause(queryExpression.SelectOrGroupClause);
3070
 
                        outputFormatter.IndentationLevel--;
3071
 
                        return null;
3072
 
                }
3073
 
                
3074
 
                void PrintClause(QueryExpressionClause clause)
3075
 
                {
3076
 
                        if (!clause.IsNull) {
3077
 
                                outputFormatter.NewLine();
3078
 
                                outputFormatter.Indent();
3079
 
                                clause.AcceptVisitor(this, null);
3080
 
                        }
3081
 
                }
3082
 
                
3083
 
                public override object TrackedVisitQueryExpressionFromClause(QueryExpressionFromClause fromClause, object data)
3084
 
                {
3085
 
                        outputFormatter.PrintToken(Tokens.From);
3086
 
                        outputFormatter.Space();
3087
 
                        VisitQueryExpressionFromOrJoinClause(fromClause, data);
3088
 
                        return null;
3089
 
                }
3090
 
                
3091
 
                public override object TrackedVisitQueryExpressionJoinClause(QueryExpressionJoinClause joinClause, object data)
3092
 
                {
3093
 
                        outputFormatter.PrintToken(Tokens.Join);
3094
 
                        outputFormatter.Space();
3095
 
                        VisitQueryExpressionFromOrJoinClause(joinClause, data);
3096
 
                        outputFormatter.Space();
3097
 
                        outputFormatter.PrintToken(Tokens.On);
3098
 
                        outputFormatter.Space();
3099
 
                        joinClause.OnExpression.AcceptVisitor(this, data);
3100
 
                        outputFormatter.Space();
3101
 
                        outputFormatter.PrintToken(Tokens.Equals);
3102
 
                        outputFormatter.Space();
3103
 
                        joinClause.EqualsExpression.AcceptVisitor(this, data);
3104
 
                        if (!string.IsNullOrEmpty(joinClause.IntoIdentifier)) {
3105
 
                                outputFormatter.Space();
3106
 
                                outputFormatter.PrintToken(Tokens.Into);
3107
 
                                outputFormatter.Space();
3108
 
                                outputFormatter.PrintIdentifier(joinClause.IntoIdentifier);
3109
 
                        }
3110
 
                        return null;
3111
 
                }
3112
 
                
3113
 
                void VisitQueryExpressionFromOrJoinClause(QueryExpressionFromOrJoinClause clause, object data)
3114
 
                {
3115
 
                        outputFormatter.PrintIdentifier(clause.Identifier);
3116
 
                        outputFormatter.Space();
3117
 
                        outputFormatter.PrintToken(Tokens.In);
3118
 
                        outputFormatter.Space();
3119
 
                        clause.InExpression.AcceptVisitor(this, data);
3120
 
                }
3121
 
                
3122
 
                public override object TrackedVisitQueryExpressionLetClause(QueryExpressionLetClause letClause, object data)
3123
 
                {
3124
 
                        outputFormatter.PrintToken(Tokens.Let);
3125
 
                        outputFormatter.Space();
3126
 
                        outputFormatter.PrintIdentifier(letClause.Identifier);
3127
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
3128
 
                                outputFormatter.Space();
3129
 
                        }
3130
 
                        outputFormatter.PrintToken(Tokens.Assign);
3131
 
                        if (this.prettyPrintOptions.AroundAssignmentParentheses) {
3132
 
                                outputFormatter.Space();
3133
 
                        }
3134
 
                        var result = letClause.Expression.AcceptVisitor(this, data);
3135
 
                        outputFormatter.Space();
3136
 
                        return result;
3137
 
                }
3138
 
                
3139
 
                public override object TrackedVisitQueryExpressionGroupClause(QueryExpressionGroupClause groupClause, object data)
3140
 
                {
3141
 
                        outputFormatter.PrintToken(Tokens.Group);
3142
 
                        outputFormatter.Space();
3143
 
                        groupClause.Projection.AcceptVisitor(this, data);
3144
 
                        outputFormatter.Space();
3145
 
                        outputFormatter.PrintToken(Tokens.By);
3146
 
                        outputFormatter.Space();
3147
 
                        return groupClause.GroupBy.AcceptVisitor(this, data);
3148
 
                }
3149
 
                
3150
 
                public override object TrackedVisitQueryExpressionOrderClause(QueryExpressionOrderClause queryExpressionOrderClause, object data)
3151
 
                {
3152
 
                        outputFormatter.PrintToken(Tokens.Orderby);
3153
 
                        outputFormatter.Space();
3154
 
                        AppendCommaSeparatedList(queryExpressionOrderClause.Orderings);
3155
 
                        return null;
3156
 
                }
3157
 
                
3158
 
                public override object TrackedVisitQueryExpressionOrdering(QueryExpressionOrdering ordering, object data)
3159
 
                {
3160
 
                        ordering.Criteria.AcceptVisitor(this, data);
3161
 
                        if (ordering.Direction == QueryExpressionOrderingDirection.Ascending) {
3162
 
                                outputFormatter.Space();
3163
 
                                outputFormatter.PrintToken(Tokens.Ascending);
3164
 
                        } else if (ordering.Direction == QueryExpressionOrderingDirection.Descending) {
3165
 
                                outputFormatter.Space();
3166
 
                                outputFormatter.PrintToken(Tokens.Descending);
3167
 
                        }
3168
 
                        return null;
3169
 
                }
3170
 
                
3171
 
                public override object TrackedVisitQueryExpressionSelectClause(QueryExpressionSelectClause selectClause, object data)
3172
 
                {
3173
 
                        outputFormatter.PrintToken(Tokens.Select);
3174
 
                        outputFormatter.Space();
3175
 
                        return selectClause.Projection.AcceptVisitor(this, data);
3176
 
                }
3177
 
                
3178
 
                public override object TrackedVisitQueryExpressionWhereClause(QueryExpressionWhereClause whereClause, object data)
3179
 
                {
3180
 
                        outputFormatter.PrintToken(Tokens.Where);
3181
 
                        outputFormatter.Space();
3182
 
                        return whereClause.Condition.AcceptVisitor(this, data);
3183
 
                }
3184
 
        }
3185
 
}