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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.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
 
ļ»æ// 
2
 
// CSharpParser.cs
3
 
//
4
 
// Author:
5
 
//       Mike KrĆ¼ger <mkrueger@novell.com>
6
 
// 
7
 
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
using System;
27
 
using System.Linq;
28
 
using System.Collections.Generic;
29
 
using System.IO;
30
 
using System.Text;
31
 
using ICSharpCode.NRefactory.Editor;
32
 
using Mono.CSharp;
33
 
using ICSharpCode.NRefactory.TypeSystem;
34
 
 
35
 
namespace ICSharpCode.NRefactory.CSharp
36
 
{
37
 
        public class CSharpParser
38
 
        {
39
 
                class ConversionVisitor : StructuralVisitor
40
 
                {
41
 
                        CompilationUnit unit = new CompilationUnit ();
42
 
                        internal bool convertTypeSystemMode;
43
 
                        
44
 
                        public CompilationUnit Unit {
45
 
                                get {
46
 
                                        return unit;
47
 
                                }
48
 
                                set {
49
 
                                        unit = value;
50
 
                                }
51
 
                        }
52
 
                        
53
 
                        public LocationsBag LocationsBag {
54
 
                                get;
55
 
                                private set;
56
 
                        }
57
 
                        
58
 
                        public ConversionVisitor (bool convertTypeSystemMode, LocationsBag locationsBag)
59
 
                        {
60
 
                                this.convertTypeSystemMode = convertTypeSystemMode;
61
 
                                this.LocationsBag = locationsBag;
62
 
                        }
63
 
                        
64
 
                        public static TextLocation Convert (Mono.CSharp.Location loc)
65
 
                        {
66
 
                                return new TextLocation (loc.Row, loc.Column);
67
 
                        }
68
 
                        
69
 
                        public override void Visit(ModuleContainer mc)
70
 
                        {
71
 
                                bool first = true;
72
 
                                foreach (var container in mc.Containers) {
73
 
                                        var nspace = container as NamespaceContainer;
74
 
                                        if (nspace == null) {
75
 
                                                container.Accept(this);
76
 
                                                continue;
77
 
                                        }
78
 
                                        NamespaceDeclaration nDecl = null;
79
 
                                        var loc = LocationsBag.GetLocations(nspace);
80
 
                                        
81
 
                                        if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
82
 
                                                nDecl = new NamespaceDeclaration ();
83
 
                                                if (loc != null) {
84
 
                                                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
85
 
                                                }
86
 
                                                ConvertNamespaceName(nspace.RealMemberName, nDecl);
87
 
                                                if (loc != null && loc.Count > 1) {
88
 
                                                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
89
 
                                                }
90
 
                                                AddToNamespace(nDecl);
91
 
                                                namespaceStack.Push(nDecl);
92
 
                                        }
93
 
                                        
94
 
                                        if (nspace.Usings != null) {
95
 
                                                foreach (var us in nspace.Usings) {
96
 
                                                        us.Accept(this);
97
 
                                                }
98
 
                                        }
99
 
                                        
100
 
                                        if (first) {
101
 
                                                first = false;
102
 
                                                AddAttributeSection(Unit, mc);
103
 
                                        }
104
 
                                        
105
 
                                        if (nspace.Containers != null) {
106
 
                                                foreach (var subContainer in nspace.Containers) {
107
 
                                                        subContainer.Accept(this);
108
 
                                                }
109
 
                                        }
110
 
                                        if (nDecl != null) {
111
 
                                                AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
112
 
                                                if (loc != null && loc.Count > 2)
113
 
                                                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
114
 
                                                if (loc != null && loc.Count > 3)
115
 
                                                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [3])), Roles.Semicolon);
116
 
                                                
117
 
                                                namespaceStack.Pop ();
118
 
                                        } else {
119
 
                                                AddAttributeSection (unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
120
 
                                        }
121
 
                                }
122
 
                                AddAttributeSection (unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
123
 
                        }
124
 
                        
125
 
                        #region Global
126
 
                        Stack<NamespaceDeclaration> namespaceStack = new Stack<NamespaceDeclaration> ();
127
 
                        
128
 
                        void AddTypeArguments (ATypeNameExpression texpr, AstType result)
129
 
                        {
130
 
                                if (texpr.TypeArguments == null || texpr.TypeArguments.Args == null)
131
 
                                        return;
132
 
                                var loc = LocationsBag.GetLocations (texpr.TypeArguments);
133
 
                                if (loc != null && loc.Count >= 2)
134
 
                                        result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 2])), Roles.LChevron);
135
 
                                int i = 0;
136
 
                                foreach (var arg in texpr.TypeArguments.Args) {
137
 
                                        result.AddChild (ConvertToType (arg), Roles.TypeArgument);
138
 
                                        if (loc != null && i < loc.Count - 2)
139
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [i++])), Roles.Comma);
140
 
                                }
141
 
                                if (loc != null && loc.Count >= 2)
142
 
                                        result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 1])), Roles.RChevron);
143
 
                        }
144
 
                        
145
 
                        AstType ConvertToType (TypeParameter spec)
146
 
                        {
147
 
                                AstType result;
148
 
                                result = new SimpleType () { IdentifierToken = Identifier.Create (spec.Name, Convert (spec.Location)) };
149
 
                                return result;
150
 
                        }
151
 
                        
152
 
                        AstType ConvertToType (MemberName memberName)
153
 
                        {
154
 
                                AstType result;
155
 
                                if (memberName.Left != null) {
156
 
                                        result = new MemberType ();
157
 
                                        result.AddChild (ConvertToType (memberName.Left), MemberType.TargetRole);
158
 
                                        var loc = LocationsBag.GetLocations (memberName.Left);
159
 
                                        if (loc != null)
160
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Dot);
161
 
                                        result.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
162
 
                                } else {
163
 
                                        result = new SimpleType () { IdentifierToken = Identifier.Create (memberName.Name, Convert (memberName.Location)) };
164
 
                                }
165
 
                                if (memberName.TypeParameters != null) {
166
 
                                        var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
167
 
                                        if (chevronLocs != null)
168
 
                                                result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
169
 
                                        for (int i = 0; i < memberName.TypeParameters.Count; i++) {
170
 
                                                var param = memberName.TypeParameters [i];
171
 
                                                result.AddChild (new SimpleType (Identifier.Create (param.Name, Convert (param.Location))), Roles.TypeArgument);
172
 
                                                if (chevronLocs != null && i < chevronLocs.Count - 2)
173
 
                                                        result.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
174
 
                                        }
175
 
                                        if (chevronLocs != null)
176
 
                                                result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
177
 
                                }
178
 
                                return result;
179
 
                        }
180
 
                                
181
 
                        AstType ConvertToType (Mono.CSharp.Expression typeName)
182
 
                        {
183
 
                                if (typeName == null) // may happen in typeof(Generic<,,,,>)
184
 
                                        return new SimpleType ();
185
 
                                
186
 
                                if (typeName is TypeExpression) {
187
 
                                        var typeExpr = (Mono.CSharp.TypeExpression)typeName;
188
 
                                        return new PrimitiveType (typeExpr.GetSignatureForError (), Convert (typeExpr.Location));
189
 
                                }
190
 
                                
191
 
                                if (typeName is Mono.CSharp.QualifiedAliasMember) {
192
 
                                        var qam = (Mono.CSharp.QualifiedAliasMember)typeName;
193
 
                                        var memberType = new MemberType (); 
194
 
                                        memberType.Target = new SimpleType (qam.alias, Convert (qam.Location));
195
 
                                        memberType.IsDoubleColon = true;
196
 
                                        memberType.MemberName = qam.Name;
197
 
                                        return memberType;
198
 
                                }
199
 
                                
200
 
                                if (typeName is MemberAccess) {
201
 
                                        MemberAccess ma = (MemberAccess)typeName;
202
 
                                        
203
 
                                        var memberType = new MemberType ();
204
 
                                        memberType.AddChild (ConvertToType (ma.LeftExpression), MemberType.TargetRole);
205
 
                                        memberType.AddChild (new CSharpTokenNode (Convert (ma.DotLocation)), Roles.Dot);
206
 
                                        
207
 
                                        memberType.MemberNameToken = Identifier.Create (ma.Name, Convert (ma.Location));
208
 
                                        
209
 
                                        AddTypeArguments (ma, memberType);
210
 
                                        return memberType;
211
 
                                }
212
 
                                
213
 
                                if (typeName is SimpleName) {
214
 
                                        var sn = (SimpleName)typeName;
215
 
                                        var result = new SimpleType (sn.Name, Convert (sn.Location));
216
 
                                        AddTypeArguments (sn, result);
217
 
                                        return result;
218
 
                                }
219
 
                                
220
 
                                if (typeName is ComposedCast) {
221
 
                                        var cc = (ComposedCast)typeName;
222
 
                                        var baseType = ConvertToType (cc.Left);
223
 
                                        var result = new ComposedType () { BaseType = baseType };
224
 
                                        var ccSpec = cc.Spec;
225
 
                                        while (ccSpec != null) {
226
 
                                                if (ccSpec.IsNullable) {
227
 
                                                        result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), ComposedType.NullableRole);
228
 
                                                } else if (ccSpec.IsPointer) {
229
 
                                                        result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), ComposedType.PointerRole);
230
 
                                                } else {
231
 
                                                        var location = LocationsBag.GetLocations (ccSpec);
232
 
                                                        var spec = new ArraySpecifier () { Dimensions = ccSpec.Dimension };
233
 
                                                        spec.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), Roles.LBracket);
234
 
                                                        if (location != null)
235
 
                                                                spec.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RBracket);
236
 
                                                        
237
 
                                                        result.ArraySpecifiers.Add (spec);
238
 
                                                }
239
 
                                                ccSpec = ccSpec.Next;
240
 
                                        }
241
 
                                        return result;
242
 
                                }
243
 
                                
244
 
                                if (typeName is SpecialContraintExpr) {
245
 
                                        var sce = (SpecialContraintExpr)typeName;
246
 
                                        switch (sce.Constraint) {
247
 
                                        case SpecialConstraint.Class:
248
 
                                                return new PrimitiveType ("class", Convert (sce.Location));
249
 
                                        case SpecialConstraint.Struct:
250
 
                                                return new PrimitiveType ("struct", Convert (sce.Location));
251
 
                                        case SpecialConstraint.Constructor:
252
 
                                                return new PrimitiveType ("new", Convert (sce.Location));
253
 
                                        }
254
 
                                }
255
 
                                return new SimpleType ("unknown");
256
 
                        }
257
 
                        
258
 
                        IEnumerable<Attribute> GetAttributes (IEnumerable<Mono.CSharp.Attribute> optAttributes)
259
 
                        {
260
 
                                if (optAttributes == null)
261
 
                                        yield break;
262
 
                                foreach (var attr in optAttributes) {
263
 
                                        Attribute result = new Attribute ();
264
 
                                        result.Type = ConvertToType (attr.TypeNameExpression);
265
 
                                        var loc = LocationsBag.GetLocations (attr);
266
 
                                        result.HasArgumentList = loc != null;
267
 
                                        int pos = 0;
268
 
                                        if (loc != null)
269
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.LPar);
270
 
                                                
271
 
                                        if (attr.PositionalArguments != null) {
272
 
                                                foreach (var arg in attr.PositionalArguments) {
273
 
                                                        var na = arg as NamedArgument;
274
 
                                                        if (na != null) {
275
 
                                                                var newArg = new NamedArgumentExpression ();
276
 
                                                                newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
277
 
                                                                
278
 
                                                                var argLoc = LocationsBag.GetLocations (na);
279
 
                                                                if (argLoc != null)
280
 
                                                                        newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0])), Roles.Colon);
281
 
                                                                if (na.Expr != null)
282
 
                                                                        newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
283
 
                                                                result.AddChild (newArg, Roles.Argument);
284
 
                                                        } else {
285
 
                                                                if (arg.Expr != null)
286
 
                                                                        result.AddChild ((Expression)arg.Expr.Accept (this), Roles.Argument);
287
 
                                                        }
288
 
                                                        if (loc != null && pos + 1 < loc.Count)
289
 
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
290
 
                                                }
291
 
                                        }
292
 
                                        if (attr.NamedArguments != null) { 
293
 
                                                foreach (NamedArgument na in attr.NamedArguments) {
294
 
                                                        var newArg = new NamedExpression ();
295
 
                                                        newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
296
 
                                                        
297
 
                                                        var argLoc = LocationsBag.GetLocations (na);
298
 
                                                        if (argLoc != null)
299
 
                                                                newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0])), Roles.Assign);
300
 
                                                        if (na.Expr != null)
301
 
                                                                newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
302
 
                                                        result.AddChild (newArg, Roles.Argument);
303
 
                                                        if (loc != null && pos + 1 < loc.Count)
304
 
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
305
 
                                                }
306
 
                                        }
307
 
                                        if (loc != null && pos < loc.Count)
308
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.RPar);
309
 
                                        
310
 
                                        yield return result;
311
 
                                }
312
 
                        }
313
 
                        
314
 
                        AttributeSection ConvertAttributeSection (IEnumerable<Mono.CSharp.Attribute> optAttributes)
315
 
                        {
316
 
                                if (optAttributes == null)
317
 
                                        return null;
318
 
                                AttributeSection result = new AttributeSection ();
319
 
                                var loc = LocationsBag.GetLocations (optAttributes);
320
 
                                int pos = 0;
321
 
                                if (loc != null)
322
 
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.LBracket);
323
 
                                
324
 
                                var first = optAttributes.FirstOrDefault ();
325
 
                                string target = first != null ? first.ExplicitTarget : null;
326
 
                                
327
 
                                if (!string.IsNullOrEmpty (target)) {
328
 
                                        if (loc != null && pos < loc.Count - 1) {
329
 
                                                result.AddChild (Identifier.Create (target, Convert (loc [pos++])), Roles.Identifier);
330
 
                                        } else {
331
 
                                                result.AddChild (Identifier.Create (target), Roles.Identifier);
332
 
                                        }
333
 
                                        if (loc != null && pos < loc.Count)
334
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Colon);
335
 
                                }
336
 
                                
337
 
                                foreach (var attr in GetAttributes (optAttributes)) {
338
 
                                        result.AddChild (attr, Roles.Attribute);
339
 
                                }
340
 
                                // optional comma
341
 
                                if (loc != null && pos < loc.Count - 1 && !loc [pos].Equals (loc [pos + 1]))
342
 
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
343
 
                                if (loc != null && pos < loc.Count)
344
 
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.RBracket);
345
 
                                return result;
346
 
                        }
347
 
                        
348
 
                        public override void Visit(NamespaceContainer nspace)
349
 
                        {
350
 
                                NamespaceDeclaration nDecl = null;
351
 
                                var loc = LocationsBag.GetLocations(nspace);
352
 
                                
353
 
                                if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
354
 
                                        nDecl = new NamespaceDeclaration ();
355
 
                                        if (loc != null) {
356
 
                                                nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
357
 
                                        }
358
 
                                        ConvertNamespaceName(nspace.RealMemberName, nDecl);
359
 
                                        if (loc != null && loc.Count > 1) {
360
 
                                                nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
361
 
                                        }
362
 
                                        AddToNamespace(nDecl);
363
 
                                        namespaceStack.Push(nDecl);
364
 
                                }
365
 
                                
366
 
                                if (nspace.Usings != null) {
367
 
                                        foreach (var us in nspace.Usings) {
368
 
                                                us.Accept(this);
369
 
                                        }
370
 
                                }
371
 
                                
372
 
                                if (nspace.Containers != null) {
373
 
                                        foreach (var container in nspace.Containers) {
374
 
                                                container.Accept(this);
375
 
                                        }
376
 
                                }
377
 
                                
378
 
                                if (nDecl != null) {
379
 
                                        AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
380
 
                                        if (loc != null && loc.Count > 2)
381
 
                                                nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
382
 
                                        if (loc != null && loc.Count > 3)
383
 
                                                nDecl.AddChild (new CSharpTokenNode (Convert (loc [3])), Roles.Semicolon);
384
 
                                        
385
 
                                        namespaceStack.Pop ();
386
 
                                }
387
 
                        }
388
 
//                      public override void Visit (UsingsBag.Namespace nspace)
389
 
//                      {
390
 
//                              
391
 
//                              
392
 
//                              VisitNamespaceUsings (nspace);
393
 
//                              VisitNamespaceBody (nspace);
394
 
//                              
395
 
//                      }
396
 
//                      
397
 
                        void ConvertNamespaceName (MemberName memberName, NamespaceDeclaration namespaceDecl)
398
 
                        {
399
 
                                AstNode insertPos = null;
400
 
                                while (memberName != null) {
401
 
                                        Identifier newIdent = Identifier.Create (memberName.Name, Convert (memberName.Location));
402
 
                                        namespaceDecl.InsertChildBefore (insertPos, newIdent, Roles.Identifier);
403
 
                                        insertPos = newIdent;
404
 
                                        
405
 
                                        if (!memberName.DotLocation.IsNull) {
406
 
                                                var dotToken = new CSharpTokenNode (Convert (memberName.DotLocation));
407
 
                                                namespaceDecl.InsertChildBefore (insertPos, dotToken, Roles.Dot);
408
 
                                                insertPos = dotToken;
409
 
                                        }
410
 
                                        
411
 
                                        memberName = memberName.Left;
412
 
                                }
413
 
                        }
414
 
                        
415
 
                        public override void Visit (UsingNamespace un)
416
 
                        {
417
 
                                var ud = new UsingDeclaration ();
418
 
                                var loc = LocationsBag.GetLocations (un);
419
 
                                ud.AddChild (new CSharpTokenNode (Convert (un.Location)), UsingDeclaration.UsingKeywordRole);
420
 
                                if (un.NamespaceExpression != null)
421
 
                                        ud.AddChild (ConvertToType (un.NamespaceExpression), UsingDeclaration.ImportRole);
422
 
                                if (loc != null)
423
 
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Semicolon);
424
 
                                AddToNamespace (ud);
425
 
                        }
426
 
 
427
 
                        public override void Visit (UsingAliasNamespace uan)
428
 
                        {
429
 
                                var ud = new UsingAliasDeclaration ();
430
 
                                var loc = LocationsBag.GetLocations (uan);
431
 
                                
432
 
                                ud.AddChild (new CSharpTokenNode (Convert (uan.Location)), UsingAliasDeclaration.UsingKeywordRole);
433
 
                                ud.AddChild (Identifier.Create (uan.Alias.Value, Convert (uan.Alias.Location)), UsingAliasDeclaration.AliasRole);
434
 
                                if (loc != null)
435
 
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
436
 
                                if (uan.NamespaceExpression != null)
437
 
                                        ud.AddChild (ConvertToType (uan.NamespaceExpression), UsingAliasDeclaration.ImportRole);
438
 
                                if (loc != null && loc.Count > 1)
439
 
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon);
440
 
                                AddToNamespace (ud);
441
 
                        }
442
 
                
443
 
                        public override void Visit (UsingExternAlias uea)
444
 
                        {
445
 
                                var ud = new ExternAliasDeclaration ();
446
 
                                var loc = LocationsBag.GetLocations (uea);
447
 
                                ud.AddChild (new CSharpTokenNode (Convert (uea.Location)), Roles.ExternKeyword);
448
 
                                if (loc != null)
449
 
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.AliasKeyword);
450
 
                                ud.AddChild (Identifier.Create (uea.Alias.Value, Convert (uea.Alias.Location)), Roles.Identifier);
451
 
                                if (loc != null && loc.Count > 1)
452
 
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon);
453
 
                                AddToNamespace (ud);
454
 
                        }
455
 
 
456
 
                        AstType ConvertImport (MemberName memberName)
457
 
                        {
458
 
                                if (memberName.Left != null) {
459
 
                                        // left.name
460
 
                                        var t = new MemberType ();
461
 
//                                      t.IsDoubleColon = memberName.IsDoubleColon;
462
 
                                        t.AddChild (ConvertImport (memberName.Left), MemberType.TargetRole);
463
 
                                        
464
 
                                        if (!memberName.DotLocation.IsNull)
465
 
                                                t.AddChild (new CSharpTokenNode (Convert (memberName.DotLocation)), Roles.Dot);
466
 
                                        
467
 
                                        t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
468
 
                                        AddTypeArguments (t, memberName);
469
 
                                        return t;
470
 
                                } else {
471
 
                                        SimpleType t = new SimpleType ();
472
 
                                        t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
473
 
                                        AddTypeArguments (t, memberName);
474
 
                                        return t;
475
 
                                }
476
 
                        }
477
 
                        
478
 
                        public override void Visit (MemberCore member)
479
 
                        {
480
 
                                Console.WriteLine ("Unknown member:");
481
 
                                Console.WriteLine (member.GetType () + "-> Member {0}", member.GetSignatureForError ());
482
 
                        }
483
 
                        
484
 
                        Stack<TypeDeclaration> typeStack = new Stack<TypeDeclaration> ();
485
 
                        
486
 
                        public override void Visit(Class c)
487
 
                        {
488
 
                                var newType = new TypeDeclaration ();
489
 
                                newType.ClassType = ClassType.Class;
490
 
                                AddAttributeSection(newType, c);
491
 
                                
492
 
                                var location = LocationsBag.GetMemberLocation(c);
493
 
                                AddModifiers(newType, location);
494
 
                                int curLoc = 0;
495
 
                                if (location != null && location.Count > 0)
496
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.ClassKeyword);
497
 
                                
498
 
                                newType.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
499
 
                                AddTypeParameters (newType, c.MemberName);
500
 
                                
501
 
                                if (c.TypeBaseExpressions != null) {
502
 
                                        if (location != null && curLoc < location.Count)
503
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
504
 
                                        
505
 
                                        var commaLocations = LocationsBag.GetLocations (c.TypeBaseExpressions);
506
 
                                        int i = 0;
507
 
                                        foreach (var baseTypes in c.TypeBaseExpressions) {
508
 
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
509
 
                                                if (commaLocations != null && i < commaLocations.Count) {
510
 
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
511
 
                                                        i++;
512
 
                                                }
513
 
                                        }
514
 
                                }
515
 
                                
516
 
                                AddConstraints (newType, c.CurrentTypeParameters);
517
 
                                if (location != null && curLoc < location.Count)
518
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
519
 
                                typeStack.Push (newType);
520
 
                                base.Visit (c);
521
 
                                AddAttributeSection (newType, c.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
522
 
                                
523
 
                                if (location != null && curLoc < location.Count) {
524
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
525
 
                                        
526
 
                                        if (location != null && curLoc < location.Count)
527
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
528
 
                                        
529
 
                                } else {
530
 
                                        // parser error, set end node to max value.
531
 
                                        newType.AddChild (new ErrorNode (), Roles.Error);
532
 
                                }
533
 
                                typeStack.Pop ();
534
 
                                AddType (newType);
535
 
                        }
536
 
                        
537
 
                        public override void Visit(Struct s)
538
 
                        {
539
 
                                var newType = new TypeDeclaration();
540
 
                                newType.ClassType = ClassType.Struct;
541
 
                                AddAttributeSection(newType, s);
542
 
                                var location = LocationsBag.GetMemberLocation(s);
543
 
                                AddModifiers(newType, location);
544
 
                                int curLoc = 0;
545
 
                                if (location != null && location.Count > 0)
546
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.StructKeyword);
547
 
                                newType.AddChild (Identifier.Create (s.MemberName.Name, Convert (s.MemberName.Location)), Roles.Identifier);
548
 
                                AddTypeParameters (newType, s.MemberName);
549
 
                                
550
 
                                if (s.TypeBaseExpressions != null) {
551
 
                                        if (location != null && curLoc < location.Count)
552
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
553
 
                                        var commaLocations = LocationsBag.GetLocations (s.TypeBaseExpressions);
554
 
                                        int i = 0;
555
 
                                        foreach (var baseTypes in s.TypeBaseExpressions) {
556
 
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
557
 
                                                if (commaLocations != null && i < commaLocations.Count) {
558
 
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
559
 
                                                        i++;
560
 
                                                }
561
 
                                        }
562
 
                                }
563
 
                                
564
 
                                AddConstraints (newType, s.CurrentTypeParameters);
565
 
                                if (location != null && curLoc < location.Count)
566
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
567
 
                                typeStack.Push (newType);
568
 
                                base.Visit (s);
569
 
                                if (location != null && location.Count > 2) {
570
 
                                        if (location != null && curLoc < location.Count)
571
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
572
 
                                        if (location != null && curLoc < location.Count)
573
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
574
 
                                } else {
575
 
                                        // parser error, set end node to max value.
576
 
                                        newType.AddChild (new ErrorNode (), Roles.Error);
577
 
                                }
578
 
                                typeStack.Pop ();
579
 
                                AddType (newType);
580
 
                        }
581
 
                        
582
 
                        public override void Visit(Interface i)
583
 
                        {
584
 
                                var newType = new TypeDeclaration();
585
 
                                newType.ClassType = ClassType.Interface;
586
 
                                AddAttributeSection(newType, i);
587
 
                                var location = LocationsBag.GetMemberLocation(i);
588
 
                                AddModifiers(newType, location);
589
 
                                int curLoc = 0;
590
 
                                if (location != null && location.Count > 0)
591
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.InterfaceKeyword);
592
 
                                newType.AddChild (Identifier.Create (i.MemberName.Name, Convert (i.MemberName.Location)), Roles.Identifier);
593
 
                                AddTypeParameters (newType, i.MemberName);
594
 
                                
595
 
                                if (i.TypeBaseExpressions != null) {
596
 
                                        if (location != null && curLoc < location.Count)
597
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
598
 
                                        var commaLocations = LocationsBag.GetLocations (i.TypeBaseExpressions);
599
 
                                        int j = 0;
600
 
                                        foreach (var baseTypes in i.TypeBaseExpressions) {
601
 
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
602
 
                                                if (commaLocations != null && j < commaLocations.Count) {
603
 
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [j])), Roles.Comma);
604
 
                                                        j++;
605
 
                                                }
606
 
                                        }
607
 
                                }
608
 
                                
609
 
                                AddConstraints (newType, i.CurrentTypeParameters);
610
 
                                if (location != null && curLoc < location.Count)
611
 
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
612
 
                                typeStack.Push (newType);
613
 
                                base.Visit (i);
614
 
                                if (location != null && location.Count > 2) {
615
 
                                        if (location != null && curLoc < location.Count)
616
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
617
 
                                        if (location != null && curLoc < location.Count)
618
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
619
 
                                } else {
620
 
                                        // parser error, set end node to max value.
621
 
                                        newType.AddChild (new ErrorNode (), Roles.Error);
622
 
                                }
623
 
                                typeStack.Pop ();
624
 
                                AddType (newType);
625
 
                        }
626
 
                        
627
 
                        public override void Visit(Mono.CSharp.Delegate d)
628
 
                        {
629
 
                                DelegateDeclaration newDelegate = new DelegateDeclaration ();
630
 
                                var location = LocationsBag.GetMemberLocation(d);
631
 
                                AddAttributeSection(newDelegate, d);
632
 
                                AddModifiers(newDelegate, location);
633
 
                                if (location != null && location.Count > 0) {
634
 
                                        newDelegate.AddChild(new CSharpTokenNode (Convert(location [0])), Roles.DelegateKeyword);
635
 
                                }
636
 
                                if (d.ReturnType != null)
637
 
                                        newDelegate.AddChild (ConvertToType (d.ReturnType), Roles.Type);
638
 
                                newDelegate.AddChild (Identifier.Create (d.MemberName.Name, Convert (d.MemberName.Location)), Roles.Identifier);
639
 
                                AddTypeParameters (newDelegate, d.MemberName);
640
 
                                
641
 
                                if (location != null && location.Count > 1)
642
 
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
643
 
                                AddParameter (newDelegate, d.Parameters);
644
 
                                
645
 
                                if (location != null && location.Count > 2) {
646
 
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
647
 
                                }
648
 
                                AddConstraints (newDelegate, d.CurrentTypeParameters);
649
 
                                if (location != null && location.Count > 3) {
650
 
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.Semicolon);
651
 
                                }
652
 
                                AddType (newDelegate);
653
 
                        }
654
 
                        
655
 
                        void AddType (EntityDeclaration child)
656
 
                        {
657
 
                                if (typeStack.Count > 0) {
658
 
                                        typeStack.Peek ().AddChild (child, Roles.TypeMemberRole);
659
 
                                } else {
660
 
                                        AddToNamespace (child);
661
 
                                }
662
 
                        }
663
 
                        
664
 
                        void AddToNamespace (AstNode child)
665
 
                        {
666
 
                                if (namespaceStack.Count > 0) {
667
 
                                        namespaceStack.Peek ().AddChild (child, NamespaceDeclaration.MemberRole);
668
 
                                } else {
669
 
                                        unit.AddChild (child, CompilationUnit.MemberRole);
670
 
                                }
671
 
                        }
672
 
                        
673
 
                        public override void Visit(Mono.CSharp.Enum e)
674
 
                        {
675
 
                                var newType = new TypeDeclaration();
676
 
                                newType.ClassType = ClassType.Enum;
677
 
                                AddAttributeSection(newType, e);
678
 
                                var location = LocationsBag.GetMemberLocation(e);
679
 
                                
680
 
                                AddModifiers(newType, location);
681
 
                                int curLoc = 0;
682
 
                                if (location != null && location.Count > 0)
683
 
                                        newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.EnumKeyword);
684
 
                                newType.AddChild(Identifier.Create(e.MemberName.Name, Convert(e.MemberName.Location)), Roles.Identifier);
685
 
                                
686
 
                                if (e.BaseTypeExpression != null) {
687
 
                                        if (location != null && curLoc < location.Count)
688
 
                                                newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.Colon);
689
 
                                        newType.AddChild(ConvertToType(e.BaseTypeExpression), Roles.BaseType);
690
 
                                }
691
 
 
692
 
                                if (location != null && curLoc < location.Count)
693
 
                                        newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.LBrace);
694
 
                                typeStack.Push(newType);
695
 
                                
696
 
                                foreach (var m in e.Members) {
697
 
                                        EnumMember member = m as EnumMember;
698
 
                                        if (member == null) {
699
 
                                                Console.WriteLine("WARNING - ENUM MEMBER: " + m);
700
 
                                                continue;
701
 
                                        }
702
 
                                        Visit(member);
703
 
                                        if (location != null && curLoc < location.Count - 1) //last one is closing brace
704
 
                                                newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.Comma);
705
 
                                }
706
 
                                
707
 
                                if (location != null && location.Count > 2) {
708
 
                                        if (location != null && curLoc < location.Count)
709
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
710
 
                                        if (location != null && curLoc < location.Count)
711
 
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
712
 
                                } else {
713
 
                                        // parser error, set end node to max value.
714
 
                                        newType.AddChild (new ErrorNode (), Roles.Error);
715
 
                                }
716
 
                                typeStack.Pop ();
717
 
                                AddType (newType);
718
 
                        }
719
 
                        
720
 
                        public override void Visit (EnumMember em)
721
 
                        {
722
 
                                EnumMemberDeclaration newField = new EnumMemberDeclaration ();
723
 
                                AddAttributeSection (newField, em);
724
 
                                newField.AddChild (Identifier.Create (em.Name, Convert (em.Location)), Roles.Identifier);
725
 
                                if (em.Initializer != null) {
726
 
                                        newField.AddChild (new CSharpTokenNode (Convert (em.Initializer.Location)), Roles.Assign);
727
 
                                        newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole);
728
 
                                }
729
 
                                //Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation);
730
 
                                
731
 
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
732
 
                        }
733
 
                        #endregion
734
 
                        
735
 
                        #region Type members
736
 
                        
737
 
                        
738
 
                        public override void Visit (FixedField f)
739
 
                        {
740
 
                                var location = LocationsBag.GetMemberLocation (f);
741
 
                                int locationIdx = 0;
742
 
                                
743
 
                                var newField = new FixedFieldDeclaration ();
744
 
                                AddAttributeSection (newField, f);
745
 
                                AddModifiers (newField, location);
746
 
                                if (location != null && location.Count > 0)
747
 
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), FixedFieldDeclaration.FixedKeywordRole);
748
 
 
749
 
                                if (f.TypeExpression != null)
750
 
                                        newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
751
 
                                
752
 
                                var variable = new FixedVariableInitializer ();
753
 
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
754
 
                                if (f.Initializer != null && !f.Initializer.IsNull) {
755
 
                                        var bracketLocations = LocationsBag.GetLocations (f.Initializer);
756
 
                                        if (bracketLocations != null && bracketLocations.Count > 1)
757
 
                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.LBracket);
758
 
                                                
759
 
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
760
 
                                        if (bracketLocations != null && bracketLocations.Count > 1)
761
 
                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.RBracket);
762
 
                                }
763
 
                                newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
764
 
                                
765
 
                                if (f.Declarators != null) {
766
 
                                        foreach (var decl in f.Declarators) {
767
 
                                                var declLoc = LocationsBag.GetLocations (decl);
768
 
                                                if (declLoc != null)
769
 
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
770
 
                                                
771
 
                                                variable = new FixedVariableInitializer ();
772
 
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
773
 
                                                if (!decl.Initializer.IsNull) {
774
 
                                                        var bracketLocations = LocationsBag.GetLocations (f.Initializer);
775
 
                                                        if (bracketLocations != null && bracketLocations.Count > 1)
776
 
                                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.LBracket);
777
 
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
778
 
                                                        if (bracketLocations != null && bracketLocations.Count > 1)
779
 
                                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.RBracket);
780
 
                                                }
781
 
                                                newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
782
 
                                        }
783
 
                                }
784
 
                                if (location != null && location.Count > locationIdx)
785
 
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx])), Roles.Semicolon);
786
 
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
787
 
                                
788
 
                        }
789
 
 
790
 
                        public override void Visit(Field f)
791
 
                        {
792
 
                                var location = LocationsBag.GetMemberLocation(f);
793
 
                                
794
 
                                FieldDeclaration newField = new FieldDeclaration ();
795
 
                                AddAttributeSection (newField, f);
796
 
                                AddModifiers (newField, location);
797
 
                                newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
798
 
                                
799
 
                                VariableInitializer variable = new VariableInitializer ();
800
 
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
801
 
                                int locationIdx = 0;
802
 
                                if (f.Initializer != null) {
803
 
                                        if (location != null)
804
 
                                                variable.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), Roles.Assign);
805
 
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
806
 
                                }
807
 
                                newField.AddChild (variable, Roles.Variable);
808
 
                                if (f.Declarators != null) {
809
 
                                        foreach (var decl in f.Declarators) {
810
 
                                                var declLoc = LocationsBag.GetLocations (decl);
811
 
                                                if (declLoc != null)
812
 
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
813
 
                                                
814
 
                                                variable = new VariableInitializer ();
815
 
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
816
 
                                                if (decl.Initializer != null) {
817
 
                                                        if (declLoc != null)
818
 
                                                                variable.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
819
 
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
820
 
                                                }
821
 
                                                newField.AddChild (variable, Roles.Variable);
822
 
                                        }
823
 
                                }
824
 
                                if (location != null &&location.Count > locationIdx)
825
 
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), Roles.Semicolon);
826
 
 
827
 
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
828
 
                        }
829
 
                        
830
 
                        public override void Visit (Const f)
831
 
                        {
832
 
                                var location = LocationsBag.GetMemberLocation (f);
833
 
                                
834
 
                                FieldDeclaration newField = new FieldDeclaration ();
835
 
                                AddAttributeSection (newField, f);
836
 
                                AddModifiers (newField, location);
837
 
                                if (location != null)
838
 
                                        newField.AddChild (new CSharpModifierToken (Convert (location [0]), Modifiers.Const), EntityDeclaration.ModifierRole);
839
 
                                newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
840
 
                                
841
 
                                VariableInitializer variable = new VariableInitializer ();
842
 
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
843
 
                                
844
 
                                if (f.Initializer != null) {
845
 
                                        variable.AddChild (new CSharpTokenNode (Convert (f.Initializer.Location)), Roles.Assign);
846
 
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
847
 
                                }
848
 
                                newField.AddChild (variable, Roles.Variable);
849
 
                                if (f.Declarators != null) {
850
 
                                        foreach (var decl in f.Declarators) {
851
 
                                                var declLoc = LocationsBag.GetLocations (decl);
852
 
                                                if (declLoc != null)
853
 
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
854
 
                                                
855
 
                                                variable = new VariableInitializer ();
856
 
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
857
 
                                                if (decl.Initializer != null) {
858
 
                                                        variable.AddChild (new CSharpTokenNode (Convert (decl.Initializer.Location)), Roles.Assign);
859
 
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
860
 
                                                }
861
 
                                                newField.AddChild (variable, Roles.Variable);
862
 
                                        }
863
 
                                }
864
 
                                if (location != null)
865
 
                                        newField.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
866
 
                                
867
 
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
868
 
 
869
 
                                
870
 
                        }
871
 
                        
872
 
                        public override void Visit (Operator o)
873
 
                        {
874
 
                                OperatorDeclaration newOperator = new OperatorDeclaration ();
875
 
                                newOperator.OperatorType = (OperatorType)o.OperatorType;
876
 
                                
877
 
                                var location = LocationsBag.GetMemberLocation (o);
878
 
                                AddAttributeSection (newOperator, o);
879
 
                                AddModifiers (newOperator, location);
880
 
                                
881
 
                                
882
 
                                if (o.OperatorType == Operator.OpType.Implicit) {
883
 
                                        if (location != null && location.Count > 0) {
884
 
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.ImplicitRole);
885
 
                                                if (location.Count > 1)
886
 
                                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.OperatorKeywordRole);
887
 
                                        }
888
 
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
889
 
                                } else if (o.OperatorType == Operator.OpType.Explicit) {
890
 
                                        if (location != null && location.Count > 0) {
891
 
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.ExplicitRole);
892
 
                                                if (location.Count > 1)
893
 
                                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.OperatorKeywordRole);
894
 
                                        }
895
 
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
896
 
                                } else {
897
 
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
898
 
 
899
 
                                        if (location != null && location.Count > 0)
900
 
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.OperatorKeywordRole);
901
 
                                        
902
 
                                        if (location != null && location.Count > 1)
903
 
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.GetRole (newOperator.OperatorType));
904
 
                                }
905
 
                                if (location != null && location.Count > 2)
906
 
                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LPar);
907
 
                                AddParameter (newOperator, o.ParameterInfo);
908
 
                                if (location != null && location.Count > 3)
909
 
                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RPar);
910
 
                                
911
 
                                if (o.Block != null) {
912
 
                                        newOperator.AddChild ((BlockStatement)o.Block.Accept (this), Roles.Body);
913
 
                                } else {
914
 
                                        if (location != null && location.Count >= 5)
915
 
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [4])), Roles.Semicolon);
916
 
                                }
917
 
                                typeStack.Peek ().AddChild (newOperator, Roles.TypeMemberRole);
918
 
                        }
919
 
                        
920
 
                        public void AddAttributeSection (AstNode parent, Attributable a)
921
 
                        {
922
 
                                if (a == null || a.OptAttributes == null)
923
 
                                        return;
924
 
                                AddAttributeSection (parent, a.OptAttributes);
925
 
                        }
926
 
                        
927
 
                        public void AddAttributeSection (AstNode parent, Attributes attrs, Role role)
928
 
                        {
929
 
                                if (attrs == null)
930
 
                                        return;
931
 
                                foreach (var attr in attrs.Sections) {
932
 
                                        parent.AddChild (ConvertAttributeSection (attr), EntityDeclaration.AttributeRole);
933
 
                                }
934
 
                        }
935
 
 
936
 
                        public void AddAttributeSection (AstNode parent, Attributes attrs)
937
 
                        {
938
 
                                AddAttributeSection (parent, attrs, EntityDeclaration.AttributeRole);
939
 
                        }
940
 
                        
941
 
                        public override void Visit (Indexer indexer)
942
 
                        {
943
 
                                IndexerDeclaration newIndexer = new IndexerDeclaration ();
944
 
                                AddAttributeSection (newIndexer, indexer);
945
 
                                var location = LocationsBag.GetMemberLocation (indexer);
946
 
                                AddModifiers (newIndexer, location);
947
 
                                newIndexer.AddChild (ConvertToType (indexer.TypeExpression), Roles.Type);
948
 
                                AddExplicitInterface (newIndexer, indexer.MemberName);
949
 
                                var name = indexer.MemberName;
950
 
                                newIndexer.AddChild (Identifier.Create ("this", Convert (name.Location)), Roles.Identifier);
951
 
                                
952
 
                                if (location != null && location.Count > 0)
953
 
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBracket);
954
 
                                AddParameter (newIndexer, indexer.ParameterInfo);
955
 
                                if (location != null && location.Count > 1)
956
 
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBracket);
957
 
                                
958
 
                                if (location != null && location.Count > 2)
959
 
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LBrace);
960
 
                                if (indexer.Get != null) {
961
 
                                        Accessor getAccessor = new Accessor ();
962
 
                                        var getLocation = LocationsBag.GetMemberLocation (indexer.Get);
963
 
                                        AddAttributeSection (getAccessor, indexer.Get);
964
 
                                        AddModifiers (getAccessor, getLocation);
965
 
                                        if (getLocation != null)
966
 
                                                getAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Get.Location)), PropertyDeclaration.GetKeywordRole);
967
 
                                        if (indexer.Get.Block != null) {
968
 
                                                getAccessor.AddChild ((BlockStatement)indexer.Get.Block.Accept (this), Roles.Body);
969
 
                                        } else {
970
 
                                                if (getLocation != null && getLocation.Count > 0)
971
 
                                                        newIndexer.AddChild (new CSharpTokenNode (Convert (getLocation [0])), Roles.Semicolon);
972
 
                                        }
973
 
                                        newIndexer.AddChild (getAccessor, PropertyDeclaration.GetterRole);
974
 
                                }
975
 
                                
976
 
                                if (indexer.Set != null) {
977
 
                                        Accessor setAccessor = new Accessor ();
978
 
                                        var setLocation = LocationsBag.GetMemberLocation (indexer.Set);
979
 
                                        AddAttributeSection (setAccessor, indexer.Set);
980
 
                                        AddModifiers (setAccessor, setLocation);
981
 
                                        if (setLocation != null)
982
 
                                                setAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Set.Location)), PropertyDeclaration.SetKeywordRole);
983
 
                                        
984
 
                                        if (indexer.Set.Block != null) {
985
 
                                                setAccessor.AddChild ((BlockStatement)indexer.Set.Block.Accept (this), Roles.Body);
986
 
                                        } else {
987
 
                                                if (setLocation != null && setLocation.Count > 0)
988
 
                                                        newIndexer.AddChild (new CSharpTokenNode (Convert (setLocation [0])), Roles.Semicolon);
989
 
                                        }
990
 
                                        newIndexer.AddChild (setAccessor, PropertyDeclaration.SetterRole);
991
 
                                }
992
 
                                
993
 
                                if (location != null) {
994
 
                                        if (location.Count > 3)
995
 
                                                newIndexer.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RBrace);
996
 
                                } else {
997
 
                                        // parser error, set end node to max value.
998
 
                                        newIndexer.AddChild (new ErrorNode (), Roles.Error);
999
 
                                }
1000
 
                                typeStack.Peek ().AddChild (newIndexer, Roles.TypeMemberRole);
1001
 
                        }
1002
 
                        
1003
 
                        public override void Visit (Method m)
1004
 
                        {
1005
 
                                MethodDeclaration newMethod = new MethodDeclaration ();
1006
 
                                AddAttributeSection (newMethod, m);
1007
 
                                var location = LocationsBag.GetMemberLocation (m);
1008
 
                                AddModifiers (newMethod, location);
1009
 
                                newMethod.AddChild (ConvertToType (m.TypeExpression), Roles.Type);
1010
 
                                AddExplicitInterface (newMethod, m.MethodName);
1011
 
                                newMethod.AddChild (Identifier.Create (m.MethodName.Name, Convert (m.Location)), Roles.Identifier);
1012
 
                                
1013
 
                                AddTypeParameters (newMethod, m.MemberName);
1014
 
                                
1015
 
                                if (location != null && location.Count > 0)
1016
 
                                        newMethod.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1017
 
                                AddParameter (newMethod, m.ParameterInfo);
1018
 
                                
1019
 
                                if (location != null && location.Count > 1)
1020
 
                                        newMethod.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1021
 
                                
1022
 
                                AddConstraints (newMethod, m.CurrentTypeParameters);
1023
 
                                
1024
 
                                if (m.Block != null) {
1025
 
                                        var bodyBlock = (BlockStatement)m.Block.Accept (this);
1026
 
//                                      if (m.Block is ToplevelBlock) {
1027
 
//                                              newMethod.AddChild (bodyBlock.FirstChild.NextSibling, Roles.Body);
1028
 
//                                      } else {
1029
 
                                        newMethod.AddChild (bodyBlock, Roles.Body);
1030
 
//                                      }
1031
 
                                } else if (location != null) {
1032
 
                                        if (location.Count < 3) {
1033
 
                                                // parser error, set end node to max value.
1034
 
                                                newMethod.AddChild (new ErrorNode (), Roles.Error);
1035
 
                                        } else {
1036
 
                                                newMethod.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.Semicolon);
1037
 
                                        }
1038
 
                                }
1039
 
                                typeStack.Peek ().AddChild (newMethod, Roles.TypeMemberRole);
1040
 
                        }
1041
 
                        
1042
 
                        static Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> modifierTable = new Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> ();
1043
 
                        static string[] keywordTable;
1044
 
                        
1045
 
                        static ConversionVisitor ()
1046
 
                        {
1047
 
                                modifierTable [Mono.CSharp.Modifiers.NEW] = ICSharpCode.NRefactory.CSharp.Modifiers.New;
1048
 
                                modifierTable [Mono.CSharp.Modifiers.PUBLIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
1049
 
                                modifierTable [Mono.CSharp.Modifiers.PROTECTED] = ICSharpCode.NRefactory.CSharp.Modifiers.Protected;
1050
 
                                modifierTable [Mono.CSharp.Modifiers.PRIVATE] = ICSharpCode.NRefactory.CSharp.Modifiers.Private;
1051
 
                                modifierTable [Mono.CSharp.Modifiers.INTERNAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Internal;
1052
 
                                modifierTable [Mono.CSharp.Modifiers.ABSTRACT] = ICSharpCode.NRefactory.CSharp.Modifiers.Abstract;
1053
 
                                modifierTable [Mono.CSharp.Modifiers.VIRTUAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Virtual;
1054
 
                                modifierTable [Mono.CSharp.Modifiers.SEALED] = ICSharpCode.NRefactory.CSharp.Modifiers.Sealed;
1055
 
                                modifierTable [Mono.CSharp.Modifiers.STATIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Static;
1056
 
                                modifierTable [Mono.CSharp.Modifiers.OVERRIDE] = ICSharpCode.NRefactory.CSharp.Modifiers.Override;
1057
 
                                modifierTable [Mono.CSharp.Modifiers.READONLY] = ICSharpCode.NRefactory.CSharp.Modifiers.Readonly;
1058
 
                                modifierTable [Mono.CSharp.Modifiers.PARTIAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Partial;
1059
 
                                modifierTable [Mono.CSharp.Modifiers.EXTERN] = ICSharpCode.NRefactory.CSharp.Modifiers.Extern;
1060
 
                                modifierTable [Mono.CSharp.Modifiers.VOLATILE] = ICSharpCode.NRefactory.CSharp.Modifiers.Volatile;
1061
 
                                modifierTable [Mono.CSharp.Modifiers.UNSAFE] = ICSharpCode.NRefactory.CSharp.Modifiers.Unsafe;
1062
 
                                modifierTable [Mono.CSharp.Modifiers.ASYNC] = ICSharpCode.NRefactory.CSharp.Modifiers.Async;
1063
 
                                
1064
 
                                keywordTable = new string[255];
1065
 
                                for (int i = 0; i< keywordTable.Length; i++) 
1066
 
                                        keywordTable [i] = "unknown";
1067
 
                                
1068
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Other] = "void";
1069
 
                                keywordTable [(int)BuiltinTypeSpec.Type.String] = "string";
1070
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Int] = "int";
1071
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Object] = "object";
1072
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Float] = "float";
1073
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Double] = "double";
1074
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Long] = "long";
1075
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Byte] = "byte";
1076
 
                                keywordTable [(int)BuiltinTypeSpec.Type.UInt] = "uint";
1077
 
                                keywordTable [(int)BuiltinTypeSpec.Type.ULong] = "ulong";
1078
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Short] = "short";
1079
 
                                keywordTable [(int)BuiltinTypeSpec.Type.UShort] = "ushort";
1080
 
                                keywordTable [(int)BuiltinTypeSpec.Type.SByte] = "sbyte";
1081
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Decimal] = "decimal";
1082
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Char] = "char";
1083
 
                                keywordTable [(int)BuiltinTypeSpec.Type.Bool] = "bool";
1084
 
                        }
1085
 
                        
1086
 
                        void AddModifiers (EntityDeclaration parent, LocationsBag.MemberLocations location)
1087
 
                        {
1088
 
                                if (location == null || location.Modifiers == null)
1089
 
                                        return;
1090
 
                                foreach (var modifier in location.Modifiers) {
1091
 
                                        ICSharpCode.NRefactory.CSharp.Modifiers mod;
1092
 
                                        if (!modifierTable.TryGetValue (modifier.Item1, out mod)) {
1093
 
                                                Console.WriteLine ("modifier " + modifier.Item1 + " can't be converted,");
1094
 
                                        }
1095
 
                                        
1096
 
                                        parent.AddChild (new CSharpModifierToken (Convert (modifier.Item2), mod), EntityDeclaration.ModifierRole);
1097
 
                                }
1098
 
                        }
1099
 
                        
1100
 
                        public override void Visit (Property p)
1101
 
                        {
1102
 
                                PropertyDeclaration newProperty = new PropertyDeclaration ();
1103
 
                                AddAttributeSection (newProperty, p);
1104
 
                                var location = LocationsBag.GetMemberLocation (p);
1105
 
                                AddModifiers (newProperty, location);
1106
 
                                newProperty.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
1107
 
                                AddExplicitInterface (newProperty, p.MemberName);
1108
 
                                newProperty.AddChild (Identifier.Create (p.MemberName.Name, Convert (p.Location)), Roles.Identifier);
1109
 
                                
1110
 
                                if (location != null && location.Count > 0)
1111
 
                                        newProperty.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBrace);
1112
 
                                
1113
 
                                Accessor getAccessor = null;
1114
 
                                if (p.Get != null) {
1115
 
                                        getAccessor = new Accessor ();
1116
 
                                        AddAttributeSection (getAccessor, p.Get);
1117
 
                                        var getLocation = LocationsBag.GetMemberLocation (p.Get);
1118
 
                                        AddModifiers (getAccessor, getLocation);
1119
 
                                        getAccessor.AddChild (new CSharpTokenNode (Convert (p.Get.Location)), PropertyDeclaration.GetKeywordRole);
1120
 
                                        
1121
 
                                        if (p.Get.Block != null) {
1122
 
                                                getAccessor.AddChild ((BlockStatement)p.Get.Block.Accept (this), Roles.Body);
1123
 
                                        } else {
1124
 
                                                if (getLocation != null && getLocation.Count > 0)
1125
 
                                                        getAccessor.AddChild (new CSharpTokenNode (Convert (getLocation [0])), Roles.Semicolon);
1126
 
                                        }
1127
 
                                }
1128
 
                                
1129
 
                                Accessor setAccessor = null;
1130
 
                                if (p.Set != null) {
1131
 
                                        setAccessor = new Accessor ();
1132
 
                                        AddAttributeSection (setAccessor, p.Set);
1133
 
                                        var setLocation = LocationsBag.GetMemberLocation (p.Set);
1134
 
                                        AddModifiers (setAccessor, setLocation);
1135
 
                                        setAccessor.AddChild (new CSharpTokenNode (Convert (p.Set.Location)), PropertyDeclaration.SetKeywordRole);
1136
 
                                        
1137
 
                                        if (p.Set.Block != null) {
1138
 
                                                setAccessor.AddChild ((BlockStatement)p.Set.Block.Accept (this), Roles.Body);
1139
 
                                        } else {
1140
 
                                                if (setLocation != null && setLocation.Count > 0)
1141
 
                                                        setAccessor.AddChild (new CSharpTokenNode (Convert (setLocation [0])), Roles.Semicolon);
1142
 
                                        }
1143
 
                                }
1144
 
                                if (getAccessor != null && setAccessor != null) {
1145
 
                                        if (getAccessor.StartLocation < setAccessor.StartLocation) {
1146
 
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1147
 
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1148
 
                                        } else {
1149
 
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1150
 
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1151
 
                                        }
1152
 
                                } else {
1153
 
                                        if (getAccessor != null)
1154
 
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1155
 
                                        if (setAccessor != null)
1156
 
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1157
 
                                }
1158
 
                                
1159
 
                                if (location != null && location.Count > 1) {
1160
 
                                        newProperty.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBrace);
1161
 
                                } else {
1162
 
                                        // parser error, set end node to max value.
1163
 
                                        newProperty.AddChild (new ErrorNode (), Roles.Error);
1164
 
                                }
1165
 
                                
1166
 
                                typeStack.Peek ().AddChild (newProperty, Roles.TypeMemberRole);
1167
 
                        }
1168
 
                        
1169
 
                        public override void Visit (Constructor c)
1170
 
                        {
1171
 
                                ConstructorDeclaration newConstructor = new ConstructorDeclaration ();
1172
 
                                AddAttributeSection (newConstructor, c);
1173
 
                                var location = LocationsBag.GetMemberLocation (c);
1174
 
                                AddModifiers (newConstructor, location);
1175
 
                                newConstructor.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
1176
 
                                if (location != null && location.Count > 0)
1177
 
                                        newConstructor.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1178
 
                                
1179
 
                                AddParameter (newConstructor, c.ParameterInfo);
1180
 
                                if (location != null && location.Count > 1)
1181
 
                                        newConstructor.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1182
 
                                
1183
 
                                if (c.Initializer != null) {
1184
 
                                        var initializer = new ConstructorInitializer ();
1185
 
                                        initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This;
1186
 
                                        var initializerLocation = LocationsBag.GetLocations (c.Initializer);
1187
 
                                        
1188
 
                                        if (initializerLocation != null)
1189
 
                                                newConstructor.AddChild (new CSharpTokenNode (Convert (initializerLocation [0])), Roles.Colon);
1190
 
                                        
1191
 
                                        if (initializerLocation != null && initializerLocation.Count > 1) {
1192
 
                                                // this and base has the same length
1193
 
                                                initializer.AddChild (new CSharpTokenNode (Convert (c.Initializer.Location)), initializer.ConstructorInitializerType == ConstructorInitializerType.This ? ConstructorInitializer.ThisKeywordRole : ConstructorInitializer.BaseKeywordRole);
1194
 
                                                initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [1])), Roles.LPar);
1195
 
                                                AddArguments (initializer, LocationsBag.GetLocations (c.Initializer.Arguments), c.Initializer.Arguments);
1196
 
                                                initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [2])), Roles.RPar);
1197
 
                                                newConstructor.AddChild (initializer, ConstructorDeclaration.InitializerRole);
1198
 
                                        }
1199
 
                                }
1200
 
                                
1201
 
                                if (c.Block != null)
1202
 
                                        newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), Roles.Body);
1203
 
                                typeStack.Peek ().AddChild (newConstructor, Roles.TypeMemberRole);
1204
 
                        }
1205
 
                        
1206
 
                        public override void Visit (Destructor d)
1207
 
                        {
1208
 
                                DestructorDeclaration newDestructor = new DestructorDeclaration ();
1209
 
                                AddAttributeSection (newDestructor, d);
1210
 
                                var location = LocationsBag.GetMemberLocation (d);
1211
 
                                AddModifiers (newDestructor, location);
1212
 
                                if (location != null && location.Count > 0)
1213
 
                                        newDestructor.AddChild (new CSharpTokenNode (Convert (location [0])), DestructorDeclaration.TildeRole);
1214
 
                                newDestructor.AddChild (Identifier.Create (d.Identifier, Convert (d.MemberName.Location)), Roles.Identifier);
1215
 
                                
1216
 
                                if (location != null && location.Count > 1) {
1217
 
                                        newDestructor.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
1218
 
                                        
1219
 
                                        if (location.Count > 2)
1220
 
                                                newDestructor.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
1221
 
                                }
1222
 
                                
1223
 
                                if (d.Block != null)
1224
 
                                        newDestructor.AddChild ((BlockStatement)d.Block.Accept (this), Roles.Body);
1225
 
                                
1226
 
                                typeStack.Peek ().AddChild (newDestructor, Roles.TypeMemberRole);
1227
 
                        }
1228
 
                        
1229
 
                        public override void Visit (EventField e)
1230
 
                        {
1231
 
                                EventDeclaration newEvent = new EventDeclaration ();
1232
 
                                AddAttributeSection (newEvent, e);
1233
 
                                var location = LocationsBag.GetMemberLocation (e);
1234
 
                                AddModifiers (newEvent, location);
1235
 
                                
1236
 
                                if (location != null && location.Count > 0)
1237
 
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [0])), EventDeclaration.EventKeywordRole);
1238
 
                                newEvent.AddChild (ConvertToType (e.TypeExpression), Roles.Type);
1239
 
                                
1240
 
                                VariableInitializer variable = new VariableInitializer ();
1241
 
                                variable.AddChild (Identifier.Create (e.MemberName.Name, Convert (e.MemberName.Location)), Roles.Identifier);
1242
 
                                
1243
 
                                if (e.Initializer != null) {
1244
 
                                        if (location != null && location.Count > 0)
1245
 
                                                variable.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
1246
 
                                        variable.AddChild ((Expression)e.Initializer.Accept (this), Roles.Expression);
1247
 
                                }
1248
 
                                newEvent.AddChild (variable, Roles.Variable);
1249
 
                                if (e.Declarators != null) {
1250
 
                                        foreach (var decl in e.Declarators) {
1251
 
                                                var declLoc = LocationsBag.GetLocations (decl);
1252
 
                                                if (declLoc != null)
1253
 
                                                        newEvent.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
1254
 
                                                
1255
 
                                                variable = new VariableInitializer ();
1256
 
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
1257
 
 
1258
 
                                                if (decl.Initializer != null) {
1259
 
                                                        if (declLoc != null)
1260
 
                                                                variable.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
1261
 
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1262
 
                                                }
1263
 
                                                newEvent.AddChild (variable, Roles.Variable);
1264
 
                                        }
1265
 
                                }
1266
 
                                
1267
 
                                if (location != null && location.Count > 1)
1268
 
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1269
 
                                
1270
 
                                typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1271
 
                        }
1272
 
 
1273
 
                        void AddExplicitInterface (AstNode parent, MemberName memberName)
1274
 
                        {
1275
 
                                if (memberName == null || memberName.ExplicitInterface == null)
1276
 
                                        return;
1277
 
                                
1278
 
                                parent.AddChild (ConvertToType (memberName.ExplicitInterface), EntityDeclaration.PrivateImplementationTypeRole);
1279
 
                                var privateImplTypeLoc = LocationsBag.GetLocations (memberName.ExplicitInterface);
1280
 
                                if (privateImplTypeLoc != null)
1281
 
                                        parent.AddChild (new CSharpTokenNode (Convert (privateImplTypeLoc [0])), Roles.Dot);
1282
 
                        }
1283
 
                        
1284
 
                        public override void Visit (EventProperty ep)
1285
 
                        {
1286
 
                                CustomEventDeclaration newEvent = new CustomEventDeclaration ();
1287
 
                                AddAttributeSection (newEvent, ep);
1288
 
                                var location = LocationsBag.GetMemberLocation (ep);
1289
 
                                AddModifiers (newEvent, location);
1290
 
                                
1291
 
                                if (location != null && location.Count > 0)
1292
 
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [0])), CustomEventDeclaration.EventKeywordRole);
1293
 
                                newEvent.AddChild (ConvertToType (ep.TypeExpression), Roles.Type);
1294
 
                                
1295
 
                                AddExplicitInterface (newEvent, ep.MemberName);
1296
 
                                
1297
 
                                newEvent.AddChild (Identifier.Create (ep.MemberName.Name, Convert (ep.Location)), Roles.Identifier);
1298
 
 
1299
 
                                if (location != null && location.Count >= 2)
1300
 
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LBrace);
1301
 
                                
1302
 
                                if (ep.Add != null) {
1303
 
                                        Accessor addAccessor = new Accessor ();
1304
 
                                        AddAttributeSection (addAccessor, ep.Add);
1305
 
                                        var addLocation = LocationsBag.GetMemberLocation (ep.Add);
1306
 
                                        AddModifiers (addAccessor, addLocation);
1307
 
                                        addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location)), CustomEventDeclaration.AddKeywordRole);
1308
 
                                        if (ep.Add.Block != null)
1309
 
                                                addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), Roles.Body);
1310
 
                                        newEvent.AddChild (addAccessor, CustomEventDeclaration.AddAccessorRole);
1311
 
                                }
1312
 
                                
1313
 
                                if (ep.Remove != null) {
1314
 
                                        Accessor removeAccessor = new Accessor ();
1315
 
                                        AddAttributeSection (removeAccessor, ep.Remove);
1316
 
                                        var removeLocation = LocationsBag.GetMemberLocation (ep.Remove);
1317
 
                                        AddModifiers (removeAccessor, removeLocation);
1318
 
                                        removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location)), CustomEventDeclaration.RemoveKeywordRole);
1319
 
                                        
1320
 
                                        if (ep.Remove.Block != null)
1321
 
                                                removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), Roles.Body);
1322
 
                                        newEvent.AddChild (removeAccessor, CustomEventDeclaration.RemoveAccessorRole);
1323
 
                                }
1324
 
                                if (location != null && location.Count >= 3) {
1325
 
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RBrace);
1326
 
                                } else {
1327
 
                                        // parser error, set end node to max value.
1328
 
                                        newEvent.AddChild (new ErrorNode (), Roles.Error);
1329
 
                                }
1330
 
                                
1331
 
                                typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1332
 
                        }
1333
 
                        
1334
 
                        #endregion
1335
 
                        
1336
 
                        #region Statements
1337
 
                        public override object Visit (Mono.CSharp.Statement stmt)
1338
 
                        {
1339
 
                                Console.WriteLine ("unknown statement:" + stmt);
1340
 
                                return null;
1341
 
                        }
1342
 
                        
1343
 
                        public override object Visit (BlockVariableDeclaration blockVariableDeclaration)
1344
 
                        {
1345
 
                                var result = new VariableDeclarationStatement ();
1346
 
                                result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1347
 
                                
1348
 
                                var varInit = new VariableInitializer ();
1349
 
                                var location = LocationsBag.GetLocations (blockVariableDeclaration);
1350
 
                                varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1351
 
                                if (blockVariableDeclaration.Initializer != null) {
1352
 
                                        if (location != null && location.Count > 0)
1353
 
                                                varInit.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
1354
 
                                        varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1355
 
                                }
1356
 
                                
1357
 
                                result.AddChild (varInit, Roles.Variable);
1358
 
                                
1359
 
                                if (blockVariableDeclaration.Declarators != null) {
1360
 
                                        foreach (var decl in blockVariableDeclaration.Declarators) {
1361
 
                                                var loc = LocationsBag.GetLocations (decl);
1362
 
                                                var init = new VariableInitializer ();
1363
 
                                                if (loc != null && loc.Count > 0)
1364
 
                                                        result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1365
 
                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1366
 
                                                if (decl.Initializer != null) {
1367
 
                                                        if (loc != null && loc.Count > 1)
1368
 
                                                                init.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Assign);
1369
 
                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1370
 
                                                } else {
1371
 
                                                }
1372
 
                                                result.AddChild (init, Roles.Variable);
1373
 
                                        }
1374
 
                                }
1375
 
                                if (location != null && (blockVariableDeclaration.Initializer == null || location.Count > 1))
1376
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.Semicolon);
1377
 
                                return result;
1378
 
                        }
1379
 
                        
1380
 
                        public override object Visit (BlockConstantDeclaration blockVariableDeclaration)
1381
 
                        {
1382
 
                                var result = new VariableDeclarationStatement ();
1383
 
                                
1384
 
                                var location = LocationsBag.GetLocations (blockVariableDeclaration);
1385
 
                                if (location != null && location.Count > 0)
1386
 
                                        result.AddChild (new CSharpModifierToken (Convert (location [0]), ICSharpCode.NRefactory.CSharp.Modifiers.Const), VariableDeclarationStatement.ModifierRole);
1387
 
                                
1388
 
                                result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1389
 
                                
1390
 
                                var varInit = new VariableInitializer ();
1391
 
                                varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1392
 
                                if (blockVariableDeclaration.Initializer != null) {
1393
 
                                        if (location != null && location.Count > 1)
1394
 
                                                varInit.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Assign);
1395
 
                                        varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1396
 
                                }
1397
 
                                
1398
 
                                result.AddChild (varInit, Roles.Variable);
1399
 
                                
1400
 
                                if (blockVariableDeclaration.Declarators != null) {
1401
 
                                        foreach (var decl in blockVariableDeclaration.Declarators) {
1402
 
                                                var loc = LocationsBag.GetLocations (decl);
1403
 
                                                var init = new VariableInitializer ();
1404
 
                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1405
 
                                                if (decl.Initializer != null) {
1406
 
                                                        if (loc != null)
1407
 
                                                                init.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
1408
 
                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1409
 
                                                        if (loc != null && loc.Count > 1)
1410
 
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Comma);
1411
 
                                                } else {
1412
 
                                                        if (loc != null && loc.Count > 0)
1413
 
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1414
 
                                                }
1415
 
                                                result.AddChild (init, Roles.Variable);
1416
 
                                        }
1417
 
                                }
1418
 
                                if (location != null) {
1419
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.Semicolon);
1420
 
                                } else {
1421
 
                                        // parser error, set end node to max value.
1422
 
                                        result.AddChild (new ErrorNode (), Roles.Error);
1423
 
                                }
1424
 
                                return result;
1425
 
                        }
1426
 
                        
1427
 
                        public override object Visit (Mono.CSharp.EmptyStatement emptyStatement)
1428
 
                        {
1429
 
                                var result = new EmptyStatement ();
1430
 
                                result.Location = Convert (emptyStatement.loc);
1431
 
                                return result;
1432
 
                        }
1433
 
                        
1434
 
                        public override object Visit (Mono.CSharp.EmptyExpression emptyExpression)
1435
 
                        {
1436
 
                                return new ICSharpCode.NRefactory.CSharp.EmptyExpression (Convert (emptyExpression.Location));
1437
 
                        }
1438
 
                        
1439
 
                        public override object Visit (Mono.CSharp.ErrorExpression emptyExpression)
1440
 
                        {
1441
 
                                return new ICSharpCode.NRefactory.CSharp.ErrorExpression (Convert (emptyExpression.Location));
1442
 
                        }
1443
 
                        
1444
 
                        public override object Visit (EmptyExpressionStatement emptyExpressionStatement)
1445
 
                        {
1446
 
                                return new EmptyExpression (Convert (emptyExpressionStatement.Location));
1447
 
                        }
1448
 
                        
1449
 
                        public override object Visit (If ifStatement)
1450
 
                        {
1451
 
                                var result = new IfElseStatement ();
1452
 
                                
1453
 
                                var location = LocationsBag.GetLocations (ifStatement);
1454
 
                                
1455
 
                                result.AddChild (new CSharpTokenNode (Convert (ifStatement.loc)), IfElseStatement.IfKeywordRole);
1456
 
                                if (location != null)
1457
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1458
 
                                if (ifStatement.Expr != null)
1459
 
                                        result.AddChild ((Expression)ifStatement.Expr.Accept (this), Roles.Condition);
1460
 
                                if (location != null && location.Count > 1)
1461
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1462
 
                                
1463
 
                                if (ifStatement.TrueStatement != null)
1464
 
                                        result.AddChild ((Statement)ifStatement.TrueStatement.Accept (this), IfElseStatement.TrueRole);
1465
 
                                
1466
 
                                if (ifStatement.FalseStatement != null) {
1467
 
                                        if (location != null && location.Count > 2)
1468
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [2])), IfElseStatement.ElseKeywordRole);
1469
 
                                        result.AddChild ((Statement)ifStatement.FalseStatement.Accept (this), IfElseStatement.FalseRole);
1470
 
                                }
1471
 
                                
1472
 
                                return result;
1473
 
                        }
1474
 
                        
1475
 
                        public override object Visit (Do doStatement)
1476
 
                        {
1477
 
                                var result = new DoWhileStatement ();
1478
 
                                var location = LocationsBag.GetLocations (doStatement);
1479
 
                                result.AddChild (new CSharpTokenNode (Convert (doStatement.loc)), DoWhileStatement.DoKeywordRole);
1480
 
                                if (doStatement.EmbeddedStatement != null)
1481
 
                                        result.AddChild ((Statement)doStatement.EmbeddedStatement.Accept (this), Roles.EmbeddedStatement);
1482
 
                                if (location != null)
1483
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), DoWhileStatement.WhileKeywordRole);
1484
 
                                if (location != null && location.Count > 1)
1485
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
1486
 
                                if (doStatement.expr != null)
1487
 
                                        result.AddChild ((Expression)doStatement.expr.Accept (this), Roles.Condition);
1488
 
                                if (location != null && location.Count > 2) {
1489
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
1490
 
                                        if (location.Count > 3)
1491
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.Semicolon);
1492
 
                                }
1493
 
                                
1494
 
                                return result;
1495
 
                        }
1496
 
                        
1497
 
                        public override object Visit (While whileStatement)
1498
 
                        {
1499
 
                                var result = new WhileStatement ();
1500
 
                                var location = LocationsBag.GetLocations (whileStatement);
1501
 
                                result.AddChild (new CSharpTokenNode (Convert (whileStatement.loc)), WhileStatement.WhileKeywordRole);
1502
 
                                
1503
 
                                if (location != null)
1504
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1505
 
                                if (whileStatement.expr != null)
1506
 
                                        result.AddChild ((Expression)whileStatement.expr.Accept (this), Roles.Condition);
1507
 
                                if (location != null && location.Count > 1)
1508
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1509
 
                                if (whileStatement.Statement != null)
1510
 
                                        result.AddChild ((Statement)whileStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1511
 
                                return result;
1512
 
                        }
1513
 
                        
1514
 
                        void AddStatementOrList (ForStatement forStatement, Mono.CSharp.Statement init, Role<Statement> role)
1515
 
                        {
1516
 
                                if (init == null)
1517
 
                                        return;
1518
 
                                if (init is StatementList) {
1519
 
                                        foreach (var stmt in ((StatementList)init).Statements) {
1520
 
                                                forStatement.AddChild ((Statement)stmt.Accept (this), role);
1521
 
                                        }
1522
 
                                } else if (init is Mono.CSharp.EmptyStatement) {
1523
 
                                        
1524
 
                                } else {
1525
 
                                        forStatement.AddChild ((Statement)init.Accept (this), role);
1526
 
                                }
1527
 
                        }
1528
 
 
1529
 
                        public override object Visit (For forStatement)
1530
 
                        {
1531
 
                                var result = new ForStatement ();
1532
 
                                
1533
 
                                var location = LocationsBag.GetLocations (forStatement);
1534
 
                                
1535
 
                                result.AddChild (new CSharpTokenNode (Convert (forStatement.loc)), ForStatement.ForKeywordRole);
1536
 
                                if (location != null)
1537
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1538
 
                                
1539
 
                                AddStatementOrList (result, forStatement.Initializer, ForStatement.InitializerRole);
1540
 
                                
1541
 
                                if (location != null && location.Count > 1)
1542
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1543
 
                                if (forStatement.Condition != null)
1544
 
                                        result.AddChild ((Expression)forStatement.Condition.Accept (this), Roles.Condition);
1545
 
                                if (location != null && location.Count >= 3)
1546
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.Semicolon);
1547
 
                                
1548
 
                                AddStatementOrList (result, forStatement.Iterator, ForStatement.IteratorRole);
1549
 
                                
1550
 
                                if (location != null && location.Count >= 4)
1551
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RPar);
1552
 
                                
1553
 
                                if (forStatement.Statement != null)
1554
 
                                        result.AddChild ((Statement)forStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1555
 
                                
1556
 
                                return result;
1557
 
                        }
1558
 
                        
1559
 
                        public override object Visit (StatementExpression statementExpression)
1560
 
                        {
1561
 
                                var result = new ExpressionStatement ();
1562
 
                                var expr = statementExpression.Expr.Accept (this) as Expression;
1563
 
                                if (expr != null)
1564
 
                                        result.AddChild ((Expression)expr, Roles.Expression);
1565
 
                                var location = LocationsBag.GetLocations (statementExpression);
1566
 
                                if (location != null)
1567
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1568
 
                                return result;
1569
 
                        }
1570
 
                        
1571
 
                        public override object Visit (StatementErrorExpression statementErrorExpression)
1572
 
                        {
1573
 
                                var result = new ExpressionStatement ();
1574
 
                                var expr = statementErrorExpression.Expr.Accept (this) as Expression;
1575
 
                                if (expr != null)
1576
 
                                        result.AddChild ((Expression)expr, Roles.Expression);
1577
 
                                return result;
1578
 
                        }
1579
 
                        
1580
 
                        public override object Visit (InvalidStatementExpression statementExpression)
1581
 
                        {
1582
 
                                var result = new ExpressionStatement ();
1583
 
                                if (statementExpression.Expression == null)
1584
 
                                        return result;
1585
 
                                var expr = statementExpression.Expression.Accept (this) as Expression;
1586
 
                                if (expr != null)
1587
 
                                        result.AddChild (expr, Roles.Expression);
1588
 
                                var location = LocationsBag.GetLocations (statementExpression);
1589
 
                                if (location != null)
1590
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1591
 
                                return result;
1592
 
                        }
1593
 
                        
1594
 
                        public override object Visit (Return returnStatement)
1595
 
                        {
1596
 
                                var result = new ReturnStatement ();
1597
 
                                
1598
 
                                result.AddChild (new CSharpTokenNode (Convert (returnStatement.loc)), ReturnStatement.ReturnKeywordRole);
1599
 
                                if (returnStatement.Expr != null)
1600
 
                                        result.AddChild ((Expression)returnStatement.Expr.Accept (this), Roles.Expression);
1601
 
                                
1602
 
                                var location = LocationsBag.GetLocations (returnStatement);
1603
 
                                if (location != null)
1604
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1605
 
                                
1606
 
                                return result;
1607
 
                        }
1608
 
                        
1609
 
                        public override object Visit (Goto gotoStatement)
1610
 
                        {
1611
 
                                var result = new GotoStatement ();
1612
 
                                var location = LocationsBag.GetLocations (gotoStatement);
1613
 
                                result.AddChild (new CSharpTokenNode (Convert (gotoStatement.loc)), GotoStatement.GotoKeywordRole);
1614
 
                                var loc = location != null ? Convert (location [0]) : TextLocation.Empty;
1615
 
                                result.AddChild (Identifier.Create (gotoStatement.Target, loc), Roles.Identifier);
1616
 
                                if (location != null && location.Count > 1)
1617
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1618
 
                                
1619
 
                                return result;
1620
 
                        }
1621
 
                        
1622
 
                        public override object Visit (LabeledStatement labeledStatement)
1623
 
                        {
1624
 
                                var result = new LabelStatement ();
1625
 
                                result.AddChild (Identifier.Create (labeledStatement.Name, Convert (labeledStatement.loc)), Roles.Identifier);
1626
 
                                var location = LocationsBag.GetLocations (labeledStatement);
1627
 
                                if (location != null)
1628
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Colon);
1629
 
                                return result;
1630
 
                        }
1631
 
                        
1632
 
                        public override object Visit (GotoDefault gotoDefault)
1633
 
                        {
1634
 
                                var result = new GotoDefaultStatement ();
1635
 
                                result.AddChild (new CSharpTokenNode (Convert (gotoDefault.loc)), GotoDefaultStatement.GotoKeywordRole);
1636
 
                                var location = LocationsBag.GetLocations (gotoDefault);
1637
 
                                if (location != null) {
1638
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), GotoDefaultStatement.DefaultKeywordRole);
1639
 
                                        if (location.Count > 1)
1640
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1641
 
                                }
1642
 
                                
1643
 
                                return result;
1644
 
                        }
1645
 
                        
1646
 
                        public override object Visit (GotoCase gotoCase)
1647
 
                        {
1648
 
                                var result = new GotoCaseStatement ();
1649
 
                                result.AddChild (new CSharpTokenNode (Convert (gotoCase.loc)), GotoCaseStatement.GotoKeywordRole);
1650
 
                                
1651
 
                                var location = LocationsBag.GetLocations (gotoCase);
1652
 
                                if (location != null)
1653
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), GotoCaseStatement.CaseKeywordRole);
1654
 
                                if (gotoCase.Expr != null)
1655
 
                                        result.AddChild ((Expression)gotoCase.Expr.Accept (this), Roles.Expression);
1656
 
                                if (location != null && location.Count > 1)
1657
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1658
 
                                return result;
1659
 
                        }
1660
 
                        
1661
 
                        public override object Visit (Throw throwStatement)
1662
 
                        {
1663
 
                                var result = new ThrowStatement ();
1664
 
                                var location = LocationsBag.GetLocations (throwStatement);
1665
 
                                
1666
 
                                result.AddChild (new CSharpTokenNode (Convert (throwStatement.loc)), ThrowStatement.ThrowKeywordRole);
1667
 
                                if (throwStatement.Expr != null)
1668
 
                                        result.AddChild ((Expression)throwStatement.Expr.Accept (this), Roles.Expression);
1669
 
                                if (location != null)
1670
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1671
 
                                return result;
1672
 
                        }
1673
 
                        
1674
 
                        public override object Visit (Break breakStatement)
1675
 
                        {
1676
 
                                var result = new BreakStatement ();
1677
 
                                var location = LocationsBag.GetLocations (breakStatement);
1678
 
                                
1679
 
                                result.AddChild (new CSharpTokenNode (Convert (breakStatement.loc)), BreakStatement.BreakKeywordRole);
1680
 
                                if (location != null)
1681
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1682
 
                                return result;
1683
 
                        }
1684
 
                        
1685
 
                        public override object Visit (Continue continueStatement)
1686
 
                        {
1687
 
                                var result = new ContinueStatement ();
1688
 
                                var location = LocationsBag.GetLocations (continueStatement);
1689
 
                                result.AddChild (new CSharpTokenNode (Convert (continueStatement.loc)), ContinueStatement.ContinueKeywordRole);
1690
 
                                if (location != null)
1691
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1692
 
                                return result;
1693
 
                        }
1694
 
                        
1695
 
                        public static bool IsLower (Location left, Location right)
1696
 
                        {
1697
 
                                return left.Row < right.Row || left.Row == right.Row && left.Column < right.Column;
1698
 
                        }
1699
 
                        
1700
 
                        public UsingStatement CreateUsingStatement (Block blockStatement)
1701
 
                        {
1702
 
                                var usingResult = new UsingStatement ();
1703
 
                                Mono.CSharp.Statement cur = blockStatement.Statements [0];
1704
 
                                if (cur is Using) {
1705
 
                                        Using u = (Using)cur;
1706
 
                                        usingResult.AddChild (new CSharpTokenNode (Convert (u.loc)), UsingStatement.UsingKeywordRole);
1707
 
                                        usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation)), Roles.LPar);
1708
 
                                        if (u.Variables != null) {
1709
 
                                                var initializer = new VariableInitializer () {
1710
 
                                                        NameToken = Identifier.Create (u.Variables.Variable.Name, Convert (u.Variables.Variable.Location)),
1711
 
                                                };
1712
 
                                                
1713
 
                                                var loc = LocationsBag.GetLocations (u.Variables);
1714
 
                                                if (loc != null)
1715
 
                                                        initializer.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
1716
 
                                                if (u.Variables.Initializer != null)
1717
 
                                                        initializer.Initializer = u.Variables.Initializer.Accept (this) as Expression;
1718
 
                                                
1719
 
                                                
1720
 
                                                var varDec = new VariableDeclarationStatement () {
1721
 
                                                        Type = ConvertToType (u.Variables.TypeExpression),
1722
 
                                                        Variables = { initializer }
1723
 
                                                };
1724
 
                                                
1725
 
                                                if (u.Variables.Declarators != null) {
1726
 
                                                        foreach (var decl in u.Variables.Declarators) {
1727
 
                                                                var declLoc = LocationsBag.GetLocations (decl);
1728
 
                                                                var init = new VariableInitializer ();
1729
 
                                                                if (declLoc != null && declLoc.Count > 0)
1730
 
                                                                        varDec.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
1731
 
                                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1732
 
                                                                if (decl.Initializer != null) {
1733
 
                                                                        if (declLoc != null && declLoc.Count > 1)
1734
 
                                                                                init.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
1735
 
                                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1736
 
                                                                }
1737
 
                                                                varDec.AddChild (init, Roles.Variable);
1738
 
                                                        }
1739
 
                                                }
1740
 
                                                usingResult.AddChild (varDec, UsingStatement.ResourceAcquisitionRole);
1741
 
                                        }
1742
 
                                        cur = u.Statement;
1743
 
                                        usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation)), Roles.RPar);
1744
 
                                        if (cur != null)
1745
 
                                                usingResult.AddChild ((Statement)cur.Accept (this), Roles.EmbeddedStatement);
1746
 
                                }
1747
 
                                return usingResult;
1748
 
                        }
1749
 
                        
1750
 
                        void AddBlockChildren (BlockStatement result, Block blockStatement, ref int curLocal)
1751
 
                        {
1752
 
                                if (convertTypeSystemMode) {
1753
 
                                        return;
1754
 
                                }
1755
 
                                foreach (Mono.CSharp.Statement stmt in blockStatement.Statements) {
1756
 
                                        if (stmt == null)
1757
 
                                                continue;
1758
 
                                        /*                                      if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) {
1759
 
                                                result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), Roles.Statement);
1760
 
                                                curLocal++;
1761
 
                                        }*/
1762
 
                                        if (stmt is Block && !(stmt is ToplevelBlock || stmt is ExplicitBlock)) {
1763
 
                                                AddBlockChildren (result, (Block)stmt, ref curLocal);
1764
 
                                        } else {
1765
 
                                                result.AddChild ((Statement)stmt.Accept (this), BlockStatement.StatementRole);
1766
 
                                        }
1767
 
                                }
1768
 
                        }
1769
 
                        
1770
 
                        public override object Visit (Block blockStatement)
1771
 
                        {
1772
 
                                if (blockStatement.IsCompilerGenerated && blockStatement.Statements.Any ()) {
1773
 
                                        if (blockStatement.Statements.First () is Using)
1774
 
                                                return CreateUsingStatement (blockStatement);
1775
 
                                        return blockStatement.Statements.Last ().Accept (this);
1776
 
                                }
1777
 
                                var result = new BlockStatement ();
1778
 
                                result.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation)), Roles.LBrace);
1779
 
                                int curLocal = 0;
1780
 
                                AddBlockChildren (result, blockStatement, ref curLocal);
1781
 
                                
1782
 
                                result.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation)), Roles.RBrace);
1783
 
                                return result;
1784
 
                        }
1785
 
 
1786
 
                        public override object Visit (Switch switchStatement)
1787
 
                        {
1788
 
                                var result = new SwitchStatement ();
1789
 
                                
1790
 
                                var location = LocationsBag.GetLocations (switchStatement);
1791
 
                                result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc)), SwitchStatement.SwitchKeywordRole);
1792
 
                                if (location != null)
1793
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1794
 
                                if (switchStatement.Expr != null)
1795
 
                                        result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression);
1796
 
                                if (location != null && location.Count > 1)
1797
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1798
 
                                if (location != null && location.Count > 2)
1799
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LBrace);
1800
 
                                if (switchStatement.Sections != null) {
1801
 
                                        foreach (var section in switchStatement.Sections) {
1802
 
                                                var newSection = new SwitchSection ();
1803
 
                                                if (section.Labels != null) {
1804
 
                                                        foreach (var caseLabel in section.Labels) {
1805
 
                                                                var newLabel = new CaseLabel ();
1806
 
                                                                if (caseLabel.Label != null) {
1807
 
                                                                        newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location)), CaseLabel.CaseKeywordRole);
1808
 
                                                                        if (caseLabel.Label != null)
1809
 
                                                                                newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), Roles.Expression);
1810
 
                                                                        var colonLocation = LocationsBag.GetLocations (caseLabel);
1811
 
                                                                        if (colonLocation != null)
1812
 
                                                                                newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0])), Roles.Colon);
1813
 
                                                                } else {
1814
 
                                                                        newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location)), CaseLabel.DefaultKeywordRole);
1815
 
                                                                        newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length)), Roles.Colon);
1816
 
                                                                }
1817
 
                                                                newSection.AddChild (newLabel, SwitchSection.CaseLabelRole);
1818
 
                                                        }
1819
 
                                                }
1820
 
                                                
1821
 
                                                var blockStatement = section.Block;
1822
 
                                                var bodyBlock = new BlockStatement ();
1823
 
                                                int curLocal = 0;
1824
 
                                                AddBlockChildren (bodyBlock, blockStatement, ref curLocal);
1825
 
                                                foreach (var statement in bodyBlock.Statements) {
1826
 
                                                        statement.Remove ();
1827
 
                                                        newSection.AddChild (statement, Roles.EmbeddedStatement);
1828
 
                                                        
1829
 
                                                }
1830
 
                                                result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
1831
 
                                        }
1832
 
                                }
1833
 
                                
1834
 
                                if (location != null && location.Count > 3) {
1835
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RBrace);
1836
 
                                } else {
1837
 
                                        // parser error, set end node to max value.
1838
 
                                        result.AddChild (new ErrorNode (), Roles.Error);
1839
 
                                }
1840
 
                                
1841
 
                                return result;
1842
 
                        }
1843
 
                        
1844
 
                        public override object Visit (Lock lockStatement)
1845
 
                        {
1846
 
                                var result = new LockStatement ();
1847
 
                                var location = LocationsBag.GetLocations (lockStatement);
1848
 
                                result.AddChild (new CSharpTokenNode (Convert (lockStatement.loc)), LockStatement.LockKeywordRole);
1849
 
                                
1850
 
                                if (location != null)
1851
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1852
 
                                if (lockStatement.Expr != null)
1853
 
                                        result.AddChild ((Expression)lockStatement.Expr.Accept (this), Roles.Expression);
1854
 
                                
1855
 
                                if (location != null && location.Count > 1)
1856
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1857
 
                                if (lockStatement.Statement != null)
1858
 
                                        result.AddChild ((Statement)lockStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1859
 
                                
1860
 
                                return result;
1861
 
                        }
1862
 
                        
1863
 
                        public override object Visit (Unchecked uncheckedStatement)
1864
 
                        {
1865
 
                                var result = new UncheckedStatement ();
1866
 
                                result.AddChild (new CSharpTokenNode (Convert (uncheckedStatement.loc)), UncheckedStatement.UncheckedKeywordRole);
1867
 
                                if (uncheckedStatement.Block != null)
1868
 
                                        result.AddChild ((BlockStatement)uncheckedStatement.Block.Accept (this), Roles.Body);
1869
 
                                return result;
1870
 
                        }
1871
 
                        
1872
 
                        public override object Visit (Checked checkedStatement)
1873
 
                        {
1874
 
                                var result = new CheckedStatement ();
1875
 
                                result.AddChild (new CSharpTokenNode (Convert (checkedStatement.loc)), CheckedStatement.CheckedKeywordRole);
1876
 
                                if (checkedStatement.Block != null)
1877
 
                                        result.AddChild ((BlockStatement)checkedStatement.Block.Accept (this), Roles.Body);
1878
 
                                return result;
1879
 
                        }
1880
 
                        
1881
 
                        public override object Visit (Unsafe unsafeStatement)
1882
 
                        {
1883
 
                                var result = new UnsafeStatement ();
1884
 
                                result.AddChild (new CSharpTokenNode (Convert (unsafeStatement.loc)), UnsafeStatement.UnsafeKeywordRole);
1885
 
                                if (unsafeStatement.Block != null)
1886
 
                                        result.AddChild ((BlockStatement)unsafeStatement.Block.Accept (this), Roles.Body);
1887
 
                                return result;
1888
 
                        }
1889
 
                        
1890
 
                        public override object Visit (Fixed fixedStatement)
1891
 
                        {
1892
 
                                var result = new FixedStatement ();
1893
 
                                var location = LocationsBag.GetLocations (fixedStatement);
1894
 
                                
1895
 
                                result.AddChild (new CSharpTokenNode (Convert (fixedStatement.loc)), FixedStatement.FixedKeywordRole);
1896
 
                                if (location != null)
1897
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1898
 
                                
1899
 
                                if (fixedStatement.Variables != null) {
1900
 
                                        var blockVariableDeclaration = fixedStatement.Variables;
1901
 
                                        result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1902
 
                                        var varInit = new VariableInitializer ();
1903
 
                                        var initLocation = LocationsBag.GetLocations (blockVariableDeclaration);
1904
 
                                        varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1905
 
                                        if (blockVariableDeclaration.Initializer != null) {
1906
 
                                                if (initLocation != null)
1907
 
                                                        varInit.AddChild (new CSharpTokenNode (Convert (initLocation [0])), Roles.Assign);
1908
 
                                                varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1909
 
                                        }
1910
 
                                        
1911
 
                                        result.AddChild (varInit, Roles.Variable);
1912
 
                                        
1913
 
                                        if (blockVariableDeclaration.Declarators != null) {
1914
 
                                                foreach (var decl in blockVariableDeclaration.Declarators) {
1915
 
                                                        var loc = LocationsBag.GetLocations (decl);
1916
 
                                                        var init = new VariableInitializer ();
1917
 
                                                        if (loc != null && loc.Count > 0)
1918
 
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1919
 
                                                        init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1920
 
                                                        if (decl.Initializer != null) {
1921
 
                                                                if (loc != null && loc.Count > 1)
1922
 
                                                                        init.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Assign);
1923
 
                                                                init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1924
 
                                                        } else {
1925
 
                                                        }
1926
 
                                                        result.AddChild (init, Roles.Variable);
1927
 
                                                }
1928
 
                                        }
1929
 
                                }
1930
 
                                
1931
 
                                if (location != null && location.Count > 1)
1932
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1933
 
                                if (fixedStatement.Statement != null)
1934
 
                                        result.AddChild ((Statement)fixedStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1935
 
                                return result;
1936
 
                        }
1937
 
                        
1938
 
                        public override object Visit (TryFinally tryFinallyStatement)
1939
 
                        {
1940
 
                                TryCatchStatement result;
1941
 
                                var location = LocationsBag.GetLocations (tryFinallyStatement);
1942
 
                                
1943
 
                                if (tryFinallyStatement.Stmt is TryCatch) {
1944
 
                                        result = (TryCatchStatement)tryFinallyStatement.Stmt.Accept (this);
1945
 
                                } else {
1946
 
                                        result = new TryCatchStatement ();
1947
 
                                        result.AddChild (new CSharpTokenNode (Convert (tryFinallyStatement.loc)), TryCatchStatement.TryKeywordRole);
1948
 
                                        if (tryFinallyStatement.Stmt != null)
1949
 
                                                result.AddChild ((BlockStatement)tryFinallyStatement.Stmt.Accept (this), TryCatchStatement.TryBlockRole);
1950
 
                                }
1951
 
                                if (location != null)
1952
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), TryCatchStatement.FinallyKeywordRole);
1953
 
                                if (tryFinallyStatement.Fini != null)
1954
 
                                        result.AddChild ((BlockStatement)tryFinallyStatement.Fini.Accept (this), TryCatchStatement.FinallyBlockRole);
1955
 
                                
1956
 
                                return result;
1957
 
                        }
1958
 
                        
1959
 
                        CatchClause ConvertCatch (Catch ctch)
1960
 
                        {
1961
 
                                CatchClause result = new CatchClause ();
1962
 
                                var location = LocationsBag.GetLocations (ctch);
1963
 
                                result.AddChild (new CSharpTokenNode (Convert (ctch.loc)), CatchClause.CatchKeywordRole);
1964
 
                                if (ctch.TypeExpression != null) {
1965
 
                                        if (location != null)
1966
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1967
 
                                        
1968
 
                                        if (ctch.TypeExpression != null)
1969
 
                                                result.AddChild (ConvertToType (ctch.TypeExpression), Roles.Type);
1970
 
                                        if (ctch.Variable != null && !string.IsNullOrEmpty (ctch.Variable.Name))
1971
 
                                                result.AddChild (Identifier.Create (ctch.Variable.Name, Convert (ctch.Variable.Location)), Roles.Identifier);
1972
 
                                        
1973
 
                                        if (location != null && location.Count > 1)
1974
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1975
 
                                }
1976
 
 
1977
 
                                if (ctch.Block != null)
1978
 
                                        result.AddChild ((BlockStatement)ctch.Block.Accept (this), Roles.Body);
1979
 
                                
1980
 
                                return result;
1981
 
                        }
1982
 
                        
1983
 
                        public override object Visit (TryCatch tryCatchStatement)
1984
 
                        {
1985
 
                                var result = new TryCatchStatement ();
1986
 
                                result.AddChild (new CSharpTokenNode (Convert (tryCatchStatement.loc)), TryCatchStatement.TryKeywordRole);
1987
 
                                if (tryCatchStatement.Block != null)
1988
 
                                        result.AddChild ((BlockStatement)tryCatchStatement.Block.Accept (this), TryCatchStatement.TryBlockRole);
1989
 
                                if (tryCatchStatement.Clauses != null) {
1990
 
                                        foreach (var ctch in tryCatchStatement.Clauses) {
1991
 
                                                result.AddChild (ConvertCatch (ctch), TryCatchStatement.CatchClauseRole);
1992
 
                                        }
1993
 
                                }
1994
 
//                              if (tryCatchStatement.General != null)
1995
 
//                                      result.AddChild (ConvertCatch (tryCatchStatement.General), TryCatchStatement.CatchClauseRole);
1996
 
                                
1997
 
                                return result;
1998
 
                        }
1999
 
                        
2000
 
                        public override object Visit (Using usingStatement)
2001
 
                        {
2002
 
                                var result = new UsingStatement ();
2003
 
                                var location = LocationsBag.GetLocations (usingStatement);
2004
 
                                
2005
 
                                result.AddChild (new CSharpTokenNode (Convert (usingStatement.loc)), UsingStatement.UsingKeywordRole);
2006
 
                                if (location != null)
2007
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2008
 
                                if (usingStatement.Expr != null)
2009
 
                                        result.AddChild ((AstNode)usingStatement.Expr.Accept (this), UsingStatement.ResourceAcquisitionRole);
2010
 
                                
2011
 
                                if (location != null && location.Count > 1)
2012
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2013
 
                                
2014
 
                                if (usingStatement.Statement != null)
2015
 
                                        result.AddChild ((Statement)usingStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2016
 
                                return result;
2017
 
                        }
2018
 
                        
2019
 
                        public override object Visit (Foreach foreachStatement)
2020
 
                        {
2021
 
                                var result = new ForeachStatement ();
2022
 
                                
2023
 
                                var location = LocationsBag.GetLocations (foreachStatement);
2024
 
                                
2025
 
                                result.AddChild (new CSharpTokenNode (Convert (foreachStatement.loc)), ForeachStatement.ForeachKeywordRole);
2026
 
                                if (location != null)
2027
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2028
 
                                
2029
 
                                if (foreachStatement.TypeExpression != null)
2030
 
                                        result.AddChild (ConvertToType (foreachStatement.TypeExpression), Roles.Type);
2031
 
                                
2032
 
                                if (foreachStatement.Variable != null)
2033
 
                                        result.AddChild (Identifier.Create (foreachStatement.Variable.Name, Convert (foreachStatement.Variable.Location)), Roles.Identifier);
2034
 
                                
2035
 
                                if (location != null && location.Count > 1)
2036
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), ForeachStatement.InKeywordRole);
2037
 
                                
2038
 
                                if (foreachStatement.Expr != null)
2039
 
                                        result.AddChild ((Expression)foreachStatement.Expr.Accept (this), Roles.Expression);
2040
 
                                
2041
 
                                if (location != null && location.Count > 2)
2042
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
2043
 
                                
2044
 
                                if (foreachStatement.Statement != null)
2045
 
                                        result.AddChild ((Statement)foreachStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2046
 
                                
2047
 
                                return result;
2048
 
                        }
2049
 
                        
2050
 
                        public override object Visit (Yield yieldStatement)
2051
 
                        {
2052
 
                                var result = new YieldReturnStatement ();
2053
 
                                var location = LocationsBag.GetLocations (yieldStatement);
2054
 
                                
2055
 
                                result.AddChild (new CSharpTokenNode (Convert (yieldStatement.loc)), YieldReturnStatement.YieldKeywordRole);
2056
 
                                if (location != null)
2057
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), YieldReturnStatement.ReturnKeywordRole);
2058
 
                                if (yieldStatement.Expr != null)
2059
 
                                        result.AddChild ((Expression)yieldStatement.Expr.Accept (this), Roles.Expression);
2060
 
                                if (location != null && location.Count > 1)
2061
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
2062
 
                                
2063
 
                                return result;
2064
 
                        }
2065
 
                        
2066
 
                        public override object Visit (YieldBreak yieldBreakStatement)
2067
 
                        {
2068
 
                                var result = new YieldBreakStatement ();
2069
 
                                var location = LocationsBag.GetLocations (yieldBreakStatement);
2070
 
                                result.AddChild (new CSharpTokenNode (Convert (yieldBreakStatement.loc)), YieldBreakStatement.YieldKeywordRole);
2071
 
                                if (location != null) {
2072
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), YieldBreakStatement.BreakKeywordRole);
2073
 
                                        if (location.Count > 1)
2074
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
2075
 
                                }
2076
 
                                return result;
2077
 
                        }
2078
 
                        #endregion
2079
 
                        
2080
 
                        #region Expression
2081
 
                        public override object Visit (Mono.CSharp.Expression expression)
2082
 
                        {
2083
 
                                Console.WriteLine ("Visit unknown expression:" + expression);
2084
 
                                System.Console.WriteLine (Environment.StackTrace);
2085
 
                                return null;
2086
 
                        }
2087
 
                        
2088
 
                        public override object Visit (Mono.CSharp.DefaultParameterValueExpression defaultParameterValueExpression)
2089
 
                        {
2090
 
                                return defaultParameterValueExpression.Child.Accept (this);
2091
 
                        }
2092
 
 
2093
 
                        public override object Visit (TypeExpression typeExpression)
2094
 
                        {
2095
 
                                return new TypeReferenceExpression (new PrimitiveType (keywordTable [(int)typeExpression.Type.BuiltinType], Convert (typeExpression.Location)));
2096
 
                        }
2097
 
 
2098
 
                        public override object Visit (LocalVariableReference localVariableReference)
2099
 
                        {
2100
 
                                return Identifier.Create (localVariableReference.Name, Convert (localVariableReference.Location));
2101
 
                        }
2102
 
 
2103
 
                        public override object Visit (MemberAccess memberAccess)
2104
 
                        {
2105
 
                                Expression result;
2106
 
                                if (memberAccess.LeftExpression is Indirection) {
2107
 
                                        var ind = memberAccess.LeftExpression as Indirection;
2108
 
                                        result = new PointerReferenceExpression ();
2109
 
                                        result.AddChild ((Expression)ind.Expr.Accept (this), Roles.TargetExpression);
2110
 
                                        result.AddChild (new CSharpTokenNode (Convert (ind.Location)), PointerReferenceExpression.ArrowRole);
2111
 
                                } else {
2112
 
                                        result = new MemberReferenceExpression ();
2113
 
                                        if (memberAccess.LeftExpression != null) {
2114
 
                                                var leftExpr = memberAccess.LeftExpression.Accept (this);
2115
 
                                                result.AddChild ((Expression)leftExpr, Roles.TargetExpression);
2116
 
                                        }
2117
 
                                        if (!memberAccess.DotLocation.IsNull) {
2118
 
                                                result.AddChild (new CSharpTokenNode (Convert (memberAccess.DotLocation)), Roles.Dot);
2119
 
                                        }
2120
 
                                }
2121
 
                                                
2122
 
                                result.AddChild (Identifier.Create (memberAccess.Name, Convert (memberAccess.Location)), Roles.Identifier);
2123
 
                                
2124
 
                                AddTypeArguments (result, memberAccess);
2125
 
                                return result;
2126
 
                        }
2127
 
                        
2128
 
                        public override object Visit (QualifiedAliasMember qualifiedAliasMember)
2129
 
                        {
2130
 
                                var result = new MemberType ();
2131
 
                                result.Target = new SimpleType (qualifiedAliasMember.alias, Convert (qualifiedAliasMember.Location));
2132
 
                                result.IsDoubleColon = true;
2133
 
                                var location = LocationsBag.GetLocations (qualifiedAliasMember);
2134
 
                                result.AddChild (Identifier.Create (qualifiedAliasMember.Name, location != null ? Convert (location [0]) : TextLocation.Empty), Roles.Identifier);
2135
 
                                return  new TypeReferenceExpression () { Type = result };
2136
 
                        }
2137
 
                        
2138
 
                        public override object Visit (Constant constant)
2139
 
                        {
2140
 
                                if (constant.GetValue () == null) 
2141
 
                                        return new NullReferenceExpression (Convert (constant.Location));
2142
 
                                string literalValue;
2143
 
                                if (constant is ILiteralConstant) {
2144
 
                                        literalValue = new string (((ILiteralConstant)constant).ParsedValue);
2145
 
                                } else {
2146
 
                                        literalValue = constant.GetValueAsLiteral ();
2147
 
                                }
2148
 
                                object val = constant.GetValue ();
2149
 
                                if (val is bool)
2150
 
                                        literalValue = (bool)val ? "true" : "false";
2151
 
                                var result = new PrimitiveExpression (val, Convert (constant.Location), literalValue);
2152
 
                                return result;
2153
 
                        }
2154
 
 
2155
 
                        public override object Visit (SimpleName simpleName)
2156
 
                        {
2157
 
                                var result = new IdentifierExpression ();
2158
 
                                result.AddChild (Identifier.Create (simpleName.Name, Convert (simpleName.Location)), Roles.Identifier);
2159
 
                                AddTypeArguments (result, simpleName);
2160
 
                                return result;
2161
 
                        }
2162
 
                        
2163
 
                        public override object Visit (BooleanExpression booleanExpression)
2164
 
                        {
2165
 
                                return booleanExpression.Expr.Accept (this);
2166
 
                        }
2167
 
                        
2168
 
                        public override object Visit (Mono.CSharp.ParenthesizedExpression parenthesizedExpression)
2169
 
                        {
2170
 
                                var result = new ParenthesizedExpression ();
2171
 
                                var location = LocationsBag.GetLocations (parenthesizedExpression);
2172
 
                                if (location != null)
2173
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2174
 
                                if (parenthesizedExpression.Expr != null)
2175
 
                                        result.AddChild ((Expression)parenthesizedExpression.Expr.Accept (this), Roles.Expression);
2176
 
                                if (location != null && location.Count > 1)
2177
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2178
 
                                return result;
2179
 
                        }
2180
 
                        
2181
 
                        public override object Visit (Unary unaryExpression)
2182
 
                        {
2183
 
                                var result = new UnaryOperatorExpression ();
2184
 
                                switch (unaryExpression.Oper) {
2185
 
                                case Unary.Operator.UnaryPlus:
2186
 
                                        result.Operator = UnaryOperatorType.Plus;
2187
 
                                        break;
2188
 
                                case Unary.Operator.UnaryNegation:
2189
 
                                        result.Operator = UnaryOperatorType.Minus;
2190
 
                                        break;
2191
 
                                case Unary.Operator.LogicalNot:
2192
 
                                        result.Operator = UnaryOperatorType.Not;
2193
 
                                        break;
2194
 
                                case Unary.Operator.OnesComplement:
2195
 
                                        result.Operator = UnaryOperatorType.BitNot;
2196
 
                                        break;
2197
 
                                case Unary.Operator.AddressOf:
2198
 
                                        result.Operator = UnaryOperatorType.AddressOf;
2199
 
                                        break;
2200
 
                                }
2201
 
                                result.AddChild (new CSharpTokenNode (Convert (unaryExpression.Location)), UnaryOperatorExpression.GetOperatorRole (result.Operator));
2202
 
                                if (unaryExpression.Expr != null)
2203
 
                                        result.AddChild ((Expression)unaryExpression.Expr.Accept (this), Roles.Expression);
2204
 
                                return result;
2205
 
                        }
2206
 
                        
2207
 
                        public override object Visit (UnaryMutator unaryMutatorExpression)
2208
 
                        {
2209
 
                                var result = new UnaryOperatorExpression ();
2210
 
                                if (unaryMutatorExpression.Expr == null)
2211
 
                                        return result;
2212
 
                                var expression = (Expression)unaryMutatorExpression.Expr.Accept (this);
2213
 
                                switch (unaryMutatorExpression.UnaryMutatorMode) {
2214
 
                                case UnaryMutator.Mode.PostDecrement:
2215
 
                                        result.Operator = UnaryOperatorType.PostDecrement;
2216
 
                                        result.AddChild (expression, Roles.Expression);
2217
 
                                        result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.DecrementRole);
2218
 
                                        break;
2219
 
                                case UnaryMutator.Mode.PostIncrement:
2220
 
                                        result.Operator = UnaryOperatorType.PostIncrement;
2221
 
                                        result.AddChild (expression, Roles.Expression);
2222
 
                                        result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.IncrementRole);
2223
 
                                        break;
2224
 
                                                
2225
 
                                case UnaryMutator.Mode.PreIncrement:
2226
 
                                        result.Operator = UnaryOperatorType.Increment;
2227
 
                                        result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.IncrementRole);
2228
 
                                        result.AddChild (expression, Roles.Expression);
2229
 
                                        break;
2230
 
                                case UnaryMutator.Mode.PreDecrement:
2231
 
                                        result.Operator = UnaryOperatorType.Decrement;
2232
 
                                        result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.DecrementRole);
2233
 
                                        result.AddChild (expression, Roles.Expression);
2234
 
                                        break;
2235
 
                                }
2236
 
                                
2237
 
                                return result;
2238
 
                        }
2239
 
                        
2240
 
                        public override object Visit (Indirection indirectionExpression)
2241
 
                        {
2242
 
                                var result = new UnaryOperatorExpression ();
2243
 
                                result.Operator = UnaryOperatorType.Dereference;
2244
 
                                result.AddChild (new CSharpTokenNode (Convert (indirectionExpression.Location)), UnaryOperatorExpression.DereferenceRole);
2245
 
                                if (indirectionExpression.Expr != null)
2246
 
                                        result.AddChild ((Expression)indirectionExpression.Expr.Accept (this), Roles.Expression);
2247
 
                                return result;
2248
 
                        }
2249
 
                        
2250
 
                        public override object Visit (Is isExpression)
2251
 
                        {
2252
 
                                var result = new IsExpression ();
2253
 
                                if (isExpression.Expr != null)
2254
 
                                        result.AddChild ((Expression)isExpression.Expr.Accept (this), Roles.Expression);
2255
 
                                result.AddChild (new CSharpTokenNode (Convert (isExpression.Location)), IsExpression.IsKeywordRole);
2256
 
                                
2257
 
                                if (isExpression.ProbeType != null)
2258
 
                                        result.AddChild (ConvertToType (isExpression.ProbeType), Roles.Type);
2259
 
                                return result;
2260
 
                        }
2261
 
                        
2262
 
                        public override object Visit (As asExpression)
2263
 
                        {
2264
 
                                var result = new AsExpression ();
2265
 
                                if (asExpression.Expr != null)
2266
 
                                        result.AddChild ((Expression)asExpression.Expr.Accept (this), Roles.Expression);
2267
 
                                result.AddChild (new CSharpTokenNode (Convert (asExpression.Location)), AsExpression.AsKeywordRole);
2268
 
                                if (asExpression.ProbeType != null)
2269
 
                                        result.AddChild (ConvertToType (asExpression.ProbeType), Roles.Type);
2270
 
                                return result;
2271
 
                        }
2272
 
                        
2273
 
                        public override object Visit (Cast castExpression)
2274
 
                        {
2275
 
                                var result = new CastExpression ();
2276
 
                                var location = LocationsBag.GetLocations (castExpression);
2277
 
                                
2278
 
                                result.AddChild (new CSharpTokenNode (Convert (castExpression.Location)), Roles.LPar);
2279
 
                                if (castExpression.TargetType != null)
2280
 
                                        result.AddChild (ConvertToType (castExpression.TargetType), Roles.Type);
2281
 
                                if (location != null)
2282
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RPar);
2283
 
                                if (castExpression.Expr != null)
2284
 
                                        result.AddChild ((Expression)castExpression.Expr.Accept (this), Roles.Expression);
2285
 
                                return result;
2286
 
                        }
2287
 
                        
2288
 
                        public override object Visit (ComposedCast composedCast)
2289
 
                        {
2290
 
                                var result = new ComposedType ();
2291
 
                                result.AddChild (ConvertToType (composedCast.Left), Roles.Type);
2292
 
                                
2293
 
                                var spec = composedCast.Spec;
2294
 
                                while (spec != null) {
2295
 
                                        if (spec.IsNullable) {
2296
 
                                                result.AddChild (new CSharpTokenNode (Convert (spec.Location)), ComposedType.NullableRole);
2297
 
                                        } else if (spec.IsPointer) {
2298
 
                                                result.AddChild (new CSharpTokenNode (Convert (spec.Location)), ComposedType.PointerRole);
2299
 
                                        } else {
2300
 
                                                var aSpec = new ArraySpecifier ();
2301
 
                                                aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location)), Roles.LBracket);
2302
 
                                                var location = LocationsBag.GetLocations (spec);
2303
 
                                                if (location != null)
2304
 
                                                        aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location)), Roles.RBracket);
2305
 
                                                result.AddChild (aSpec, ComposedType.ArraySpecifierRole);
2306
 
                                        }
2307
 
                                        spec = spec.Next;
2308
 
                                }
2309
 
                                
2310
 
                                return result;
2311
 
                        }
2312
 
                        
2313
 
                        public override object Visit (Mono.CSharp.DefaultValueExpression defaultValueExpression)
2314
 
                        {
2315
 
                                var result = new DefaultValueExpression ();
2316
 
                                result.AddChild (new CSharpTokenNode (Convert (defaultValueExpression.Location)), DefaultValueExpression.DefaultKeywordRole);
2317
 
                                var location = LocationsBag.GetLocations (defaultValueExpression);
2318
 
                                if (location != null)
2319
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2320
 
                                result.AddChild (ConvertToType (defaultValueExpression.Expr), Roles.Type);
2321
 
                                if (location != null && location.Count > 1)
2322
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2323
 
                                return result;
2324
 
                        }
2325
 
                        
2326
 
                        public override object Visit (Binary binaryExpression)
2327
 
                        {
2328
 
                                var result = new BinaryOperatorExpression ();
2329
 
                                switch (binaryExpression.Oper) {
2330
 
                                case Binary.Operator.Multiply:
2331
 
                                        result.Operator = BinaryOperatorType.Multiply;
2332
 
                                        break;
2333
 
                                case Binary.Operator.Division:
2334
 
                                        result.Operator = BinaryOperatorType.Divide;
2335
 
                                        break;
2336
 
                                case Binary.Operator.Modulus:
2337
 
                                        result.Operator = BinaryOperatorType.Modulus;
2338
 
                                        break;
2339
 
                                case Binary.Operator.Addition:
2340
 
                                        result.Operator = BinaryOperatorType.Add;
2341
 
                                        break;
2342
 
                                case Binary.Operator.Subtraction:
2343
 
                                        result.Operator = BinaryOperatorType.Subtract;
2344
 
                                        break;
2345
 
                                case Binary.Operator.LeftShift:
2346
 
                                        result.Operator = BinaryOperatorType.ShiftLeft;
2347
 
                                        break;
2348
 
                                case Binary.Operator.RightShift:
2349
 
                                        result.Operator = BinaryOperatorType.ShiftRight;
2350
 
                                        break;
2351
 
                                case Binary.Operator.LessThan:
2352
 
                                        result.Operator = BinaryOperatorType.LessThan;
2353
 
                                        break;
2354
 
                                case Binary.Operator.GreaterThan:
2355
 
                                        result.Operator = BinaryOperatorType.GreaterThan;
2356
 
                                        break;
2357
 
                                case Binary.Operator.LessThanOrEqual:
2358
 
                                        result.Operator = BinaryOperatorType.LessThanOrEqual;
2359
 
                                        break;
2360
 
                                case Binary.Operator.GreaterThanOrEqual:
2361
 
                                        result.Operator = BinaryOperatorType.GreaterThanOrEqual;
2362
 
                                        break;
2363
 
                                case Binary.Operator.Equality:
2364
 
                                        result.Operator = BinaryOperatorType.Equality;
2365
 
                                        break;
2366
 
                                case Binary.Operator.Inequality:
2367
 
                                        result.Operator = BinaryOperatorType.InEquality;
2368
 
                                        break;
2369
 
                                case Binary.Operator.BitwiseAnd:
2370
 
                                        result.Operator = BinaryOperatorType.BitwiseAnd;
2371
 
                                        break;
2372
 
                                case Binary.Operator.ExclusiveOr:
2373
 
                                        result.Operator = BinaryOperatorType.ExclusiveOr;
2374
 
                                        break;
2375
 
                                case Binary.Operator.BitwiseOr:
2376
 
                                        result.Operator = BinaryOperatorType.BitwiseOr;
2377
 
                                        break;
2378
 
                                case Binary.Operator.LogicalAnd:
2379
 
                                        result.Operator = BinaryOperatorType.ConditionalAnd;
2380
 
                                        break;
2381
 
                                case Binary.Operator.LogicalOr:
2382
 
                                        result.Operator = BinaryOperatorType.ConditionalOr;
2383
 
                                        break;
2384
 
                                }
2385
 
                                
2386
 
                                if (binaryExpression.Left != null)
2387
 
                                        result.AddChild ((Expression)binaryExpression.Left.Accept (this), BinaryOperatorExpression.LeftRole);
2388
 
                                result.AddChild (new CSharpTokenNode (Convert (binaryExpression.Location)), BinaryOperatorExpression.GetOperatorRole (result.Operator));
2389
 
                                if (binaryExpression.Right != null)
2390
 
                                        result.AddChild ((Expression)binaryExpression.Right.Accept (this), BinaryOperatorExpression.RightRole);
2391
 
                                return result;
2392
 
                        }
2393
 
                        
2394
 
                        public override object Visit (Mono.CSharp.Nullable.NullCoalescingOperator nullCoalescingOperator)
2395
 
                        {
2396
 
                                var result = new BinaryOperatorExpression ();
2397
 
                                result.Operator = BinaryOperatorType.NullCoalescing;
2398
 
                                if (nullCoalescingOperator.LeftExpression != null)
2399
 
                                        result.AddChild ((Expression)nullCoalescingOperator.LeftExpression.Accept (this), BinaryOperatorExpression.LeftRole);
2400
 
                                result.AddChild (new CSharpTokenNode (Convert (nullCoalescingOperator.Location)), BinaryOperatorExpression.NullCoalescingRole);
2401
 
                                if (nullCoalescingOperator.RightExpression != null)
2402
 
                                        result.AddChild ((Expression)nullCoalescingOperator.RightExpression.Accept (this), BinaryOperatorExpression.RightRole);
2403
 
                                return result;
2404
 
                        }
2405
 
                        
2406
 
                        public override object Visit (Conditional conditionalExpression)
2407
 
                        {
2408
 
                                var result = new ConditionalExpression ();
2409
 
                                
2410
 
                                if (conditionalExpression.Expr != null)
2411
 
                                        result.AddChild ((Expression)conditionalExpression.Expr.Accept (this), Roles.Condition);
2412
 
                                var location = LocationsBag.GetLocations (conditionalExpression);
2413
 
                                
2414
 
                                result.AddChild (new CSharpTokenNode (Convert (conditionalExpression.Location)), ConditionalExpression.QuestionMarkRole);
2415
 
                                if (conditionalExpression.TrueExpr != null)
2416
 
                                        result.AddChild ((Expression)conditionalExpression.TrueExpr.Accept (this), ConditionalExpression.TrueRole);
2417
 
                                if (location != null)
2418
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), ConditionalExpression.ColonRole);
2419
 
                                if (conditionalExpression.FalseExpr != null)
2420
 
                                        result.AddChild ((Expression)conditionalExpression.FalseExpr.Accept (this), ConditionalExpression.FalseRole);
2421
 
                                return result;
2422
 
                        }
2423
 
                        
2424
 
                        void AddParameter (AstNode parent, Mono.CSharp.AParametersCollection parameters)
2425
 
                        {
2426
 
                                if (parameters == null)
2427
 
                                        return;
2428
 
                                var paramLocation = LocationsBag.GetLocations (parameters);
2429
 
                                
2430
 
                                for (int i = 0; i < parameters.Count; i++) {
2431
 
                                        var p = (Parameter)parameters.FixedParameters [i];
2432
 
                                        if (p == null)
2433
 
                                                continue;
2434
 
                                        var location = LocationsBag.GetLocations (p);
2435
 
                                        ParameterDeclaration parameterDeclarationExpression = new ParameterDeclaration ();
2436
 
                                        AddAttributeSection (parameterDeclarationExpression, p);
2437
 
                                        switch (p.ModFlags) {
2438
 
                                        case Parameter.Modifier.OUT:
2439
 
                                                parameterDeclarationExpression.ParameterModifier = ParameterModifier.Out;
2440
 
                                                if (location != null)
2441
 
                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.OutModifierRole);
2442
 
                                                break;
2443
 
                                        case Parameter.Modifier.REF:
2444
 
                                                parameterDeclarationExpression.ParameterModifier = ParameterModifier.Ref;
2445
 
                                                if (location != null)
2446
 
                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.RefModifierRole);
2447
 
                                                break;
2448
 
                                        case Parameter.Modifier.PARAMS:
2449
 
                                                parameterDeclarationExpression.ParameterModifier = ParameterModifier.Params;
2450
 
                                                if (location != null)
2451
 
                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.ParamsModifierRole);
2452
 
                                                break;
2453
 
                                        default:
2454
 
                                                if (p.HasExtensionMethodModifier) {
2455
 
                                                        parameterDeclarationExpression.ParameterModifier = ParameterModifier.This;
2456
 
                                                        if (location != null) {
2457
 
                                                                parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.ThisModifierRole);
2458
 
                                                        }
2459
 
                                                }
2460
 
                                                break;
2461
 
                                        }
2462
 
                                        if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
2463
 
                                                parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
2464
 
                                        if (p.Name != null)
2465
 
                                                parameterDeclarationExpression.AddChild (Identifier.Create (p.Name, Convert (p.Location)), Roles.Identifier);
2466
 
                                        if (p.HasDefaultValue) {
2467
 
                                                if (location != null && location.Count > 1)
2468
 
                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Assign);
2469
 
                                                parameterDeclarationExpression.AddChild ((Expression)p.DefaultValue.Accept (this), Roles.Expression);
2470
 
                                        }
2471
 
                                        parent.AddChild (parameterDeclarationExpression, Roles.Parameter);
2472
 
                                        if (paramLocation != null && i < paramLocation.Count) {
2473
 
                                                parent.AddChild (new CSharpTokenNode (Convert (paramLocation [i])), Roles.Comma);
2474
 
                                        }
2475
 
                                }
2476
 
                        }
2477
 
                        
2478
 
                        void AddTypeParameters (AstNode parent, MemberName memberName)
2479
 
                        {
2480
 
                                if (memberName == null || memberName.TypeParameters == null)
2481
 
                                        return;
2482
 
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2483
 
                                if (chevronLocs != null)
2484
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2485
 
                                for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2486
 
                                        if (chevronLocs != null && i > 0 && i - 1 < chevronLocs.Count)
2487
 
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i - 1])), Roles.Comma);
2488
 
                                        var arg = memberName.TypeParameters [i];
2489
 
                                        if (arg == null)
2490
 
                                                continue;
2491
 
                                        TypeParameterDeclaration tp = new TypeParameterDeclaration ();
2492
 
                                        
2493
 
                                        List<Location> varianceLocation;
2494
 
                                        switch (arg.Variance) {
2495
 
                                        case Variance.Contravariant:
2496
 
                                                tp.Variance = VarianceModifier.Contravariant;
2497
 
                                                varianceLocation = LocationsBag.GetLocations (arg);
2498
 
                                                if (varianceLocation != null)
2499
 
                                                        tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0])), TypeParameterDeclaration.InVarianceKeywordRole);
2500
 
                                                break;
2501
 
                                        case Variance.Covariant:
2502
 
                                                tp.Variance = VarianceModifier.Covariant;
2503
 
                                                varianceLocation = LocationsBag.GetLocations (arg);
2504
 
                                                if (varianceLocation != null)
2505
 
                                                        tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0])), TypeParameterDeclaration.OutVarianceKeywordRole);
2506
 
                                                break;
2507
 
                                        default:
2508
 
                                                tp.Variance = VarianceModifier.Invariant;
2509
 
                                                break;
2510
 
                                                
2511
 
                                        }
2512
 
                                        
2513
 
                                        AddAttributeSection (tp, arg.OptAttributes);
2514
 
 
2515
 
                                        switch (arg.Variance) {
2516
 
                                        case Variance.Covariant:
2517
 
                                                tp.Variance = VarianceModifier.Covariant;
2518
 
                                                break;
2519
 
                                        case Variance.Contravariant:
2520
 
                                                tp.Variance = VarianceModifier.Contravariant;
2521
 
                                                break;
2522
 
                                        }
2523
 
                                        tp.AddChild (Identifier.Create (arg.Name, Convert (arg.Location)), Roles.Identifier);
2524
 
                                        parent.AddChild (tp, Roles.TypeParameter);
2525
 
                                }
2526
 
                                if (chevronLocs != null)
2527
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2528
 
                        }
2529
 
                        
2530
 
                        void AddTypeArguments (AstNode parent, MemberName memberName)
2531
 
                        {
2532
 
                                if (memberName == null || memberName.TypeParameters == null)
2533
 
                                        return;
2534
 
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2535
 
                                if (chevronLocs != null)
2536
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2537
 
                                
2538
 
                                for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2539
 
                                        var arg = memberName.TypeParameters [i];
2540
 
                                        if (arg == null)
2541
 
                                                continue;
2542
 
                                        parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2543
 
                                        if (chevronLocs != null && i < chevronLocs.Count - 2)
2544
 
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
2545
 
                                }
2546
 
                                
2547
 
                                if (chevronLocs != null)
2548
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2549
 
                        }
2550
 
                        
2551
 
                        void AddTypeArguments (AstNode parent, ATypeNameExpression memberName)
2552
 
                        {
2553
 
                                if (memberName == null || !memberName.HasTypeArguments)
2554
 
                                        return;
2555
 
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeArguments);
2556
 
                                if (chevronLocs != null)
2557
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2558
 
                                
2559
 
                                for (int i = 0; i < memberName.TypeArguments.Count; i++) {
2560
 
                                        var arg = memberName.TypeArguments.Args [i];
2561
 
                                        if (arg == null)
2562
 
                                                continue;
2563
 
                                        parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2564
 
                                        if (chevronLocs != null && i < chevronLocs.Count - 2)
2565
 
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
2566
 
                                }
2567
 
                                
2568
 
                                if (chevronLocs != null)
2569
 
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2570
 
                        }
2571
 
                        
2572
 
                        void AddConstraints(AstNode parent, TypeParameters d)
2573
 
                        {
2574
 
                                if (d == null)
2575
 
                                        return;
2576
 
                                for (int i = d.Count - 1; i >= 0; i--) {
2577
 
                                        var typeParameter = d [i];
2578
 
                                        if (typeParameter == null)
2579
 
                                                continue;
2580
 
                                        var c = typeParameter.Constraints;
2581
 
                                        if (c == null)
2582
 
                                                continue;
2583
 
                                        var location = LocationsBag.GetLocations (c);
2584
 
                                        var constraint = new Constraint ();
2585
 
                                        constraint.AddChild (new CSharpTokenNode (Convert (c.Location)), Roles.WhereKeyword);
2586
 
                                        constraint.AddChild (new SimpleType (Identifier.Create (c.TypeParameter.Value, Convert (c.TypeParameter.Location))), Roles.ConstraintTypeParameter);
2587
 
                                        if (location != null)
2588
 
                                                constraint.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Colon);
2589
 
                                        var commaLocs = LocationsBag.GetLocations (c.ConstraintExpressions);
2590
 
                                        int curComma = 0;
2591
 
                                        if (c.ConstraintExpressions != null) {
2592
 
                                                foreach (var expr in c.ConstraintExpressions) {
2593
 
                                                        constraint.AddChild (ConvertToType (expr), Roles.BaseType);
2594
 
                                                        if (commaLocs != null && curComma < commaLocs.Count)
2595
 
                                                                constraint.AddChild (new CSharpTokenNode (Convert (commaLocs [curComma++])), Roles.Comma);
2596
 
                                                }
2597
 
                                        }
2598
 
                                        
2599
 
                                        parent.AddChild (constraint, Roles.Constraint);
2600
 
                                }
2601
 
                        }
2602
 
                        
2603
 
                        Expression ConvertArgument (Argument arg)
2604
 
                        {
2605
 
                                if (arg is NamedArgument) {
2606
 
                                        var na = (NamedArgument)arg;
2607
 
                                        NamedArgumentExpression newArg = new NamedArgumentExpression ();
2608
 
                                        newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
2609
 
                                        
2610
 
                                        var loc = LocationsBag.GetLocations (na);
2611
 
                                        if (loc != null)
2612
 
                                                newArg.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Colon);
2613
 
                                        
2614
 
                                        if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2615
 
                                                DirectionExpression direction = new DirectionExpression ();
2616
 
                                                direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2617
 
                                                var argLocation = LocationsBag.GetLocations (arg);
2618
 
                                                if (argLocation != null)
2619
 
                                                        direction.AddChild (new CSharpTokenNode (Convert (argLocation [0])), arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole);
2620
 
                                                direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2621
 
                                                newArg.AddChild (direction, Roles.Expression);
2622
 
                                        } else {
2623
 
                                                newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
2624
 
                                        }
2625
 
                                        return newArg;
2626
 
                                }
2627
 
                                
2628
 
                                if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2629
 
                                        DirectionExpression direction = new DirectionExpression ();
2630
 
                                        direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2631
 
                                        var argLocation = LocationsBag.GetLocations (arg);
2632
 
                                        if (argLocation != null)
2633
 
                                                direction.AddChild (new CSharpTokenNode (Convert (argLocation [0])), arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole);
2634
 
                                        direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2635
 
                                        return direction;
2636
 
                                }
2637
 
                                
2638
 
                                return (Expression)arg.Expr.Accept (this);
2639
 
                        }
2640
 
                        
2641
 
                        void AddArguments (AstNode parent, object location, Mono.CSharp.Arguments args)
2642
 
                        {
2643
 
                                if (args == null)
2644
 
                                        return;
2645
 
                                
2646
 
                                var commaLocations = LocationsBag.GetLocations (args);
2647
 
                                for (int i = 0; i < args.Count; i++) {
2648
 
                                        parent.AddChild (ConvertArgument (args [i]), Roles.Argument);
2649
 
                                        if (commaLocations != null && i < commaLocations.Count) {
2650
 
                                                parent.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2651
 
                                        }
2652
 
                                }
2653
 
                                if (commaLocations != null && commaLocations.Count > args.Count)
2654
 
                                        parent.AddChild (new CSharpTokenNode (Convert (commaLocations [args.Count])), Roles.Comma);
2655
 
                        }
2656
 
                        
2657
 
                        public override object Visit (Invocation invocationExpression)
2658
 
                        {
2659
 
                                var result = new InvocationExpression ();
2660
 
                                var location = LocationsBag.GetLocations (invocationExpression);
2661
 
                                if (invocationExpression.Exp != null)
2662
 
                                        result.AddChild ((Expression)invocationExpression.Exp.Accept (this), Roles.TargetExpression);
2663
 
                                if (location != null)
2664
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2665
 
                                AddArguments (result, location, invocationExpression.Arguments);
2666
 
                                
2667
 
                                if (location != null && location.Count > 1)
2668
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2669
 
                                return result;
2670
 
                        }
2671
 
                        
2672
 
                        public override object Visit (New newExpression)
2673
 
                        {
2674
 
                                var result = new ObjectCreateExpression ();
2675
 
                                var location = LocationsBag.GetLocations (newExpression);
2676
 
                                result.AddChild (new CSharpTokenNode (Convert (newExpression.Location)), ObjectCreateExpression.NewKeywordRole);
2677
 
                                
2678
 
                                if (newExpression.TypeRequested != null)
2679
 
                                        result.AddChild (ConvertToType (newExpression.TypeRequested), Roles.Type);
2680
 
                                if (location != null)
2681
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2682
 
                                AddArguments (result, location, newExpression.Arguments);
2683
 
                                
2684
 
                                if (location != null && location.Count > 1)
2685
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2686
 
                                
2687
 
                                return result;
2688
 
                        }
2689
 
                        
2690
 
                        public override object Visit (NewAnonymousType newAnonymousType)
2691
 
                        {
2692
 
                                var result = new AnonymousTypeCreateExpression ();
2693
 
                                var location = LocationsBag.GetLocations (newAnonymousType);
2694
 
                                result.AddChild (new CSharpTokenNode (Convert (newAnonymousType.Location)), ObjectCreateExpression.NewKeywordRole);
2695
 
                                if (location != null)
2696
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBrace);
2697
 
                                if (newAnonymousType.Parameters != null) {
2698
 
                                        foreach (var par in newAnonymousType.Parameters) {
2699
 
                                                if (par == null)
2700
 
                                                        continue;
2701
 
                                                var parLocation = LocationsBag.GetLocations (par);
2702
 
        
2703
 
                                                if (parLocation == null) {
2704
 
                                                        if (par.Expr != null)
2705
 
                                                                result.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2706
 
                                                } else {
2707
 
                                                        var namedExpression = new NamedExpression ();
2708
 
                                                        namedExpression.AddChild (Identifier.Create (par.Name, Convert (par.Location)), Roles.Identifier);
2709
 
                                                        namedExpression.AddChild (new CSharpTokenNode (Convert (parLocation [0])), Roles.Assign);
2710
 
                                                        if (par.Expr != null)
2711
 
                                                                namedExpression.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2712
 
                                                        result.AddChild (namedExpression, Roles.Expression);
2713
 
                                                }
2714
 
                                        }
2715
 
                                }
2716
 
                                if (location != null && location.Count > 1)
2717
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBrace);
2718
 
                                return result;
2719
 
                        }
2720
 
 
2721
 
            ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
2722
 
            {
2723
 
                if (minit == null)
2724
 
                    return null;
2725
 
                var init = new ArrayInitializerExpression();
2726
 
                AddConvertCollectionOrObjectInitializers(init, minit);
2727
 
                return init;
2728
 
            }
2729
 
 
2730
 
            void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
2731
 
                        {
2732
 
                                var initLoc = LocationsBag.GetLocations(minit);
2733
 
                                var commaLoc = LocationsBag.GetLocations(minit.Initializers);
2734
 
                                int curComma = 0;
2735
 
                                if (initLoc != null)
2736
 
                                        init.AddChild(new CSharpTokenNode(Convert(initLoc [0])), Roles.LBrace);
2737
 
                                foreach (var expr in minit.Initializers) {
2738
 
                                        var collectionInit = expr as CollectionElementInitializer;
2739
 
                                        if (collectionInit != null) {
2740
 
                                                AstNode parent;
2741
 
                                                // For ease of use purposes in the resolver the ast representation 
2742
 
                                                // of { a, b, c }  is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
2743
 
                                                // can be identified by expr.IsSingleElement.
2744
 
                                                if (!collectionInit.IsSingle) {
2745
 
                                                        parent = new ArrayInitializerExpression();
2746
 
                                                        parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location)), Roles.LBrace); 
2747
 
                                                } else {
2748
 
                                                        parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
2749
 
                                                }
2750
 
                                                for (int i = 0; i < collectionInit.Arguments.Count; i++) {
2751
 
                                                        var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
2752
 
                                                        if (arg == null)
2753
 
                                                                continue;
2754
 
                                                        parent.AddChild(
2755
 
                                                                (ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept(this),
2756
 
                                                                Roles.Expression
2757
 
                                                        );
2758
 
                                                }
2759
 
 
2760
 
                                                if (!collectionInit.IsSingle) {
2761
 
                                                        var braceLocs = LocationsBag.GetLocations(expr);
2762
 
                                                        if (braceLocs != null)
2763
 
                                                                parent.AddChild(new CSharpTokenNode(Convert(braceLocs [0])), Roles.RBrace);
2764
 
                                                }
2765
 
                                                init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
2766
 
                                        } else {
2767
 
                                                var eleInit = expr as ElementInitializer;
2768
 
                                                if (eleInit != null) {
2769
 
                                                        var nexpr = new NamedExpression();
2770
 
                                                        nexpr.AddChild(
2771
 
                                                                Identifier.Create(eleInit.Name, Convert(eleInit.Location)),
2772
 
                                                                Roles.Identifier
2773
 
                                                        );
2774
 
                                                        var assignLoc = LocationsBag.GetLocations(eleInit);
2775
 
                                                        if (assignLoc != null)
2776
 
                                                                nexpr.AddChild(new CSharpTokenNode(Convert(assignLoc [0])), Roles.Assign);
2777
 
                                                        if (eleInit.Source != null) {
2778
 
                                                                if (eleInit.Source is CollectionOrObjectInitializers) {
2779
 
                                                                        var arrInit = new ArrayInitializerExpression();
2780
 
                                                                        AddConvertCollectionOrObjectInitializers(
2781
 
                                                                                arrInit,
2782
 
                                                                                eleInit.Source as CollectionOrObjectInitializers
2783
 
                                                                        );
2784
 
                                                                        nexpr.AddChild(arrInit, Roles.Expression);
2785
 
                                                                } else {
2786
 
                                                                        nexpr.AddChild((Expression)eleInit.Source.Accept(this), Roles.Expression);
2787
 
                                                                }
2788
 
                                                        }
2789
 
 
2790
 
                                                        init.AddChild(nexpr, Roles.Expression);
2791
 
                                                }
2792
 
                                        }
2793
 
                                        if (commaLoc != null && curComma < commaLoc.Count)
2794
 
                                                init.AddChild(new CSharpTokenNode(Convert(commaLoc [curComma++])), Roles.Comma);
2795
 
 
2796
 
                                }
2797
 
                                if (initLoc != null) {
2798
 
                                        if (initLoc.Count == 3) // optional comma
2799
 
                                                init.AddChild(new CSharpTokenNode(Convert(initLoc [1])), Roles.Comma);
2800
 
                                        init.AddChild(new CSharpTokenNode(Convert(initLoc [initLoc.Count - 1])), Roles.RBrace);
2801
 
                                }
2802
 
                        }
2803
 
 
2804
 
                        
2805
 
                        public override object Visit (NewInitialize newInitializeExpression)
2806
 
                        {
2807
 
                                var result = new ObjectCreateExpression ();
2808
 
                                result.AddChild (new CSharpTokenNode (Convert (newInitializeExpression.Location)), ObjectCreateExpression.NewKeywordRole);
2809
 
                                
2810
 
                                if (newInitializeExpression.TypeRequested != null)
2811
 
                                        result.AddChild (ConvertToType (newInitializeExpression.TypeRequested), Roles.Type);
2812
 
                                
2813
 
                                var location = LocationsBag.GetLocations (newInitializeExpression);
2814
 
                                if (location != null)
2815
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2816
 
                                AddArguments (result, location, newInitializeExpression.Arguments);
2817
 
                                if (location != null && location.Count > 1)
2818
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2819
 
                                
2820
 
                                var init = ConvertCollectionOrObjectInitializers (newInitializeExpression.Initializers);
2821
 
                                if (init != null)
2822
 
                                        result.AddChild (init, ObjectCreateExpression.InitializerRole);
2823
 
                                
2824
 
                                return result;
2825
 
                        }
2826
 
                        
2827
 
                        public override object Visit (ArrayCreation arrayCreationExpression)
2828
 
                        {
2829
 
                                var result = new ArrayCreateExpression ();
2830
 
                                
2831
 
                                var location = LocationsBag.GetLocations (arrayCreationExpression);
2832
 
                                result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location)), ArrayCreateExpression.NewKeywordRole);
2833
 
                                if (arrayCreationExpression.TypeExpression != null)
2834
 
                                        result.AddChild (ConvertToType (arrayCreationExpression.TypeExpression), Roles.Type);
2835
 
                                        
2836
 
                                var next = arrayCreationExpression.Rank;
2837
 
                                if (arrayCreationExpression.Arguments != null) {
2838
 
                                        // skip first array rank.
2839
 
                                        next = next.Next;
2840
 
                                        
2841
 
                                        if (location != null)
2842
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBracket);
2843
 
                                        
2844
 
                                        var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments);
2845
 
                                        for (int i = 0; i < arrayCreationExpression.Arguments.Count; i++) {
2846
 
                                                result.AddChild ((Expression)arrayCreationExpression.Arguments [i].Accept (this), Roles.Argument);
2847
 
                                                if (commaLocations != null && i < commaLocations.Count)
2848
 
                                                        result.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2849
 
                                        }
2850
 
                                        if (location != null && location.Count > 1)
2851
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBracket);
2852
 
                                        
2853
 
                                }
2854
 
                                
2855
 
                                while (next != null) {
2856
 
                                        ArraySpecifier spec = new ArraySpecifier (next.Dimension);
2857
 
                                        var loc = LocationsBag.GetLocations (next);
2858
 
                                        spec.AddChild (new CSharpTokenNode (Convert (next.Location)), Roles.LBracket);
2859
 
                                        result.AddChild (spec, ArrayCreateExpression.AdditionalArraySpecifierRole);
2860
 
                                        if (loc != null)
2861
 
                                                result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.RBracket);
2862
 
                                        next = next.Next;
2863
 
                                }
2864
 
                                
2865
 
                                if (arrayCreationExpression.Initializers != null) {
2866
 
                                        var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers);
2867
 
                                        ArrayInitializerExpression initializer = new ArrayInitializerExpression ();
2868
 
                                        
2869
 
                                        initializer.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location)), Roles.LBrace);
2870
 
                                        var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements);
2871
 
                                        for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) {
2872
 
                                                var init = arrayCreationExpression.Initializers [i];
2873
 
                                                if (init == null)
2874
 
                                                        continue;
2875
 
                                                initializer.AddChild ((Expression)init.Accept (this), Roles.Expression);
2876
 
                                                if (commaLocations != null && i < commaLocations.Count) {
2877
 
                                                        initializer.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2878
 
                                                }
2879
 
                                        }
2880
 
                                        
2881
 
                                        if (initLocation != null)
2882
 
                                                initializer.AddChild (new CSharpTokenNode (Convert (initLocation [initLocation.Count - 1])), Roles.RBrace);
2883
 
                                        result.AddChild (initializer, ArrayCreateExpression.InitializerRole);
2884
 
                                }
2885
 
                                
2886
 
                                return result;
2887
 
                        }
2888
 
                        
2889
 
                        public override object Visit (This thisExpression)
2890
 
                        {
2891
 
                                var result = new ThisReferenceExpression ();
2892
 
                                result.Location = Convert (thisExpression.Location);
2893
 
                                return result;
2894
 
                        }
2895
 
                        
2896
 
                        public override object Visit (ArglistAccess argListAccessExpression)
2897
 
                        {
2898
 
                                var result = new UndocumentedExpression () {
2899
 
                                        UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess
2900
 
                                };
2901
 
                                result.AddChild (new CSharpTokenNode (Convert (argListAccessExpression.Location)), UndocumentedExpression.ArglistKeywordRole);
2902
 
                                return result;
2903
 
                        }
2904
 
                        
2905
 
                        #region Undocumented expressions
2906
 
                        public override object Visit (Arglist argListExpression)
2907
 
                        {
2908
 
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.ArgList };
2909
 
                                result.AddChild (new CSharpTokenNode (Convert (argListExpression.Location)), UndocumentedExpression.ArglistKeywordRole);
2910
 
                                var location = LocationsBag.GetLocations (argListExpression);
2911
 
                                if (location != null)
2912
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2913
 
                                
2914
 
                                AddArguments (result, location, argListExpression.Arguments);
2915
 
                                
2916
 
                                if (location != null && location.Count > 1)
2917
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2918
 
                                return result;
2919
 
                        }
2920
 
                        
2921
 
                        public override object Visit (MakeRefExpr makeRefExpr)
2922
 
                        {
2923
 
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.MakeRef };
2924
 
                                result.AddChild (new CSharpTokenNode (Convert (makeRefExpr.Location)), UndocumentedExpression.MakerefKeywordRole);
2925
 
                                var location = LocationsBag.GetLocations (makeRefExpr);
2926
 
                                if (location != null)
2927
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2928
 
                                if (makeRefExpr.Expr != null)
2929
 
                                        result.AddChild ((Expression)makeRefExpr.Expr.Accept (this), Roles.Argument);
2930
 
                                if (location != null && location.Count > 1)
2931
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2932
 
                                return result;
2933
 
                        }
2934
 
                        
2935
 
                        public override object Visit (RefTypeExpr refTypeExpr)
2936
 
                        {
2937
 
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefType };
2938
 
                                result.AddChild (new CSharpTokenNode (Convert (refTypeExpr.Location)), UndocumentedExpression.ReftypeKeywordRole);
2939
 
                                var location = LocationsBag.GetLocations (refTypeExpr);
2940
 
                                if (location != null)
2941
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2942
 
                                
2943
 
                                if (refTypeExpr.Expr != null)
2944
 
                                        result.AddChild ((Expression)refTypeExpr.Expr.Accept (this), Roles.Argument);
2945
 
                                
2946
 
                                if (location != null && location.Count > 1)
2947
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2948
 
                                return result;
2949
 
                        }
2950
 
                        
2951
 
                        public override object Visit (RefValueExpr refValueExpr)
2952
 
                        {
2953
 
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefValue };
2954
 
                                result.AddChild (new CSharpTokenNode (Convert (refValueExpr.Location)), UndocumentedExpression.RefvalueKeywordRole);
2955
 
                                var location = LocationsBag.GetLocations (refValueExpr);
2956
 
                                if (location != null)
2957
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2958
 
                                
2959
 
                                
2960
 
                                if (refValueExpr.Expr != null)
2961
 
                                        result.AddChild ((Expression)refValueExpr.Expr.Accept (this), Roles.Argument);
2962
 
 
2963
 
                                if (refValueExpr.FullNamedExpression != null)
2964
 
                                        result.AddChild ((Expression)refValueExpr.FullNamedExpression.Accept (this), Roles.Argument);
2965
 
                                
2966
 
                                if (location != null && location.Count > 1)
2967
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2968
 
                                return result;
2969
 
                        }
2970
 
                        #endregion
2971
 
                        
2972
 
                        public override object Visit (TypeOf typeOfExpression)
2973
 
                        {
2974
 
                                var result = new TypeOfExpression ();
2975
 
                                var location = LocationsBag.GetLocations (typeOfExpression);
2976
 
                                result.AddChild (new CSharpTokenNode (Convert (typeOfExpression.Location)), TypeOfExpression.TypeofKeywordRole);
2977
 
                                if (location != null)
2978
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2979
 
                                if (typeOfExpression.TypeExpression != null)
2980
 
                                        result.AddChild (ConvertToType (typeOfExpression.TypeExpression), Roles.Type);
2981
 
                                if (location != null && location.Count > 1)
2982
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2983
 
                                return result;
2984
 
                        }
2985
 
                        
2986
 
                        public override object Visit (SizeOf sizeOfExpression)
2987
 
                        {
2988
 
                                var result = new SizeOfExpression ();
2989
 
                                var location = LocationsBag.GetLocations (sizeOfExpression);
2990
 
                                result.AddChild (new CSharpTokenNode (Convert (sizeOfExpression.Location)), SizeOfExpression.SizeofKeywordRole);
2991
 
                                if (location != null)
2992
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2993
 
                                if (sizeOfExpression.TypeExpression != null)
2994
 
                                        result.AddChild (ConvertToType (sizeOfExpression.TypeExpression), Roles.Type);
2995
 
                                if (location != null && location.Count > 1)
2996
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2997
 
                                return result;
2998
 
                        }
2999
 
                        
3000
 
                        public override object Visit (CheckedExpr checkedExpression)
3001
 
                        {
3002
 
                                var result = new CheckedExpression ();
3003
 
                                var location = LocationsBag.GetLocations (checkedExpression);
3004
 
                                result.AddChild (new CSharpTokenNode (Convert (checkedExpression.Location)), CheckedExpression.CheckedKeywordRole);
3005
 
                                if (location != null)
3006
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
3007
 
                                if (checkedExpression.Expr != null)
3008
 
                                        result.AddChild ((Expression)checkedExpression.Expr.Accept (this), Roles.Expression);
3009
 
                                if (location != null && location.Count > 1)
3010
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
3011
 
                                return result;
3012
 
                        }
3013
 
                        
3014
 
                        public override object Visit (UnCheckedExpr uncheckedExpression)
3015
 
                        {
3016
 
                                var result = new UncheckedExpression ();
3017
 
                                var location = LocationsBag.GetLocations (uncheckedExpression);
3018
 
                                result.AddChild (new CSharpTokenNode (Convert (uncheckedExpression.Location)), UncheckedExpression.UncheckedKeywordRole);
3019
 
                                if (location != null)
3020
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
3021
 
                                if (uncheckedExpression.Expr != null)
3022
 
                                        result.AddChild ((Expression)uncheckedExpression.Expr.Accept (this), Roles.Expression);
3023
 
                                if (location != null && location.Count > 1)
3024
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
3025
 
                                return result;
3026
 
                        }
3027
 
                        
3028
 
                        public override object Visit (ElementAccess elementAccessExpression)
3029
 
                        {
3030
 
                                IndexerExpression result = new IndexerExpression ();
3031
 
                                var location = LocationsBag.GetLocations (elementAccessExpression);
3032
 
                                
3033
 
                                if (elementAccessExpression.Expr != null)
3034
 
                                        result.AddChild ((Expression)elementAccessExpression.Expr.Accept (this), Roles.TargetExpression);
3035
 
                                result.AddChild (new CSharpTokenNode (Convert (elementAccessExpression.Location)), Roles.LBracket);
3036
 
                                AddArguments (result, location, elementAccessExpression.Arguments);
3037
 
                                if (location != null)
3038
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RBracket);
3039
 
                                return result;
3040
 
                        }
3041
 
                        
3042
 
                        public override object Visit (BaseThis baseAccessExpression)
3043
 
                        {
3044
 
                                var result = new BaseReferenceExpression ();
3045
 
                                result.Location = Convert (baseAccessExpression.Location);
3046
 
                                return result;
3047
 
                        }
3048
 
                        
3049
 
                        public override object Visit (StackAlloc stackAllocExpression)
3050
 
                        {
3051
 
                                var result = new StackAllocExpression ();
3052
 
                                
3053
 
                                var location = LocationsBag.GetLocations (stackAllocExpression);
3054
 
                                if (location != null)
3055
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), StackAllocExpression.StackallocKeywordRole);
3056
 
                                if (stackAllocExpression.TypeExpression != null)
3057
 
                                        result.AddChild (ConvertToType (stackAllocExpression.TypeExpression), Roles.Type);
3058
 
                                if (location != null && location.Count > 1)
3059
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LBracket);
3060
 
                                if (stackAllocExpression.CountExpression != null)
3061
 
                                        result.AddChild ((Expression)stackAllocExpression.CountExpression.Accept (this), Roles.Expression);
3062
 
                                if (location != null && location.Count > 2)
3063
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RBracket);
3064
 
                                return result;
3065
 
                        }
3066
 
                        
3067
 
                        public override object Visit (SimpleAssign simpleAssign)
3068
 
                        {
3069
 
                                var result = new AssignmentExpression ();
3070
 
                                
3071
 
                                result.Operator = AssignmentOperatorType.Assign;
3072
 
                                if (simpleAssign.Target != null)
3073
 
                                        result.AddChild ((Expression)simpleAssign.Target.Accept (this), AssignmentExpression.LeftRole);
3074
 
                                result.AddChild (new CSharpTokenNode (Convert (simpleAssign.Location)), AssignmentExpression.AssignRole);
3075
 
                                if (simpleAssign.Source != null) {
3076
 
                                        result.AddChild ((Expression)simpleAssign.Source.Accept (this), AssignmentExpression.RightRole);
3077
 
                                }
3078
 
                                return result;
3079
 
                        }
3080
 
                        
3081
 
                        public override object Visit (CompoundAssign compoundAssign)
3082
 
                        {
3083
 
                                var result = new AssignmentExpression ();
3084
 
                                switch (compoundAssign.Op) {
3085
 
                                case Binary.Operator.Multiply:
3086
 
                                        result.Operator = AssignmentOperatorType.Multiply;
3087
 
                                        break;
3088
 
                                case Binary.Operator.Division:
3089
 
                                        result.Operator = AssignmentOperatorType.Divide;
3090
 
                                        break;
3091
 
                                case Binary.Operator.Modulus:
3092
 
                                        result.Operator = AssignmentOperatorType.Modulus;
3093
 
                                        break;
3094
 
                                case Binary.Operator.Addition:
3095
 
                                        result.Operator = AssignmentOperatorType.Add;
3096
 
                                        break;
3097
 
                                case Binary.Operator.Subtraction:
3098
 
                                        result.Operator = AssignmentOperatorType.Subtract;
3099
 
                                        break;
3100
 
                                case Binary.Operator.LeftShift:
3101
 
                                        result.Operator = AssignmentOperatorType.ShiftLeft;
3102
 
                                        break;
3103
 
                                case Binary.Operator.RightShift:
3104
 
                                        result.Operator = AssignmentOperatorType.ShiftRight;
3105
 
                                        break;
3106
 
                                case Binary.Operator.BitwiseAnd:
3107
 
                                        result.Operator = AssignmentOperatorType.BitwiseAnd;
3108
 
                                        break;
3109
 
                                case Binary.Operator.BitwiseOr:
3110
 
                                        result.Operator = AssignmentOperatorType.BitwiseOr;
3111
 
                                        break;
3112
 
                                case Binary.Operator.ExclusiveOr:
3113
 
                                        result.Operator = AssignmentOperatorType.ExclusiveOr;
3114
 
                                        break;
3115
 
                                }
3116
 
                                
3117
 
                                if (compoundAssign.Target != null)
3118
 
                                        result.AddChild ((Expression)compoundAssign.Target.Accept (this), AssignmentExpression.LeftRole);
3119
 
                                result.AddChild (new CSharpTokenNode (Convert (compoundAssign.Location)), AssignmentExpression.GetOperatorRole (result.Operator));
3120
 
                                if (compoundAssign.Source != null)
3121
 
                                        result.AddChild ((Expression)compoundAssign.Source.Accept (this), AssignmentExpression.RightRole);
3122
 
                                return result;
3123
 
                        }
3124
 
                        
3125
 
                        public override object Visit (Mono.CSharp.AnonymousMethodExpression anonymousMethodExpression)
3126
 
                        {
3127
 
                                var result = new AnonymousMethodExpression ();
3128
 
                                var location = LocationsBag.GetLocations (anonymousMethodExpression);
3129
 
                                int l = 0;
3130
 
                                if (anonymousMethodExpression.IsAsync) {
3131
 
                                        result.IsAsync = true;
3132
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++])), AnonymousMethodExpression.AsyncModifierRole);
3133
 
                                }
3134
 
                                if (location != null) {
3135
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++])), AnonymousMethodExpression.DelegateKeywordRole);
3136
 
                                        
3137
 
                                        if (location.Count > l) {
3138
 
                                                result.HasParameterList = true;
3139
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.LPar);
3140
 
                                                AddParameter (result, anonymousMethodExpression.Parameters);
3141
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.RPar);
3142
 
                                        }
3143
 
                                }
3144
 
                                if (anonymousMethodExpression.Block != null)
3145
 
                                        result.AddChild ((BlockStatement)anonymousMethodExpression.Block.Accept (this), Roles.Body);
3146
 
                                return result;
3147
 
                        }
3148
 
 
3149
 
                        public override object Visit (Mono.CSharp.LambdaExpression lambdaExpression)
3150
 
                        {
3151
 
                                var result = new LambdaExpression ();
3152
 
                                var location = LocationsBag.GetLocations (lambdaExpression);
3153
 
                                int l = 0;
3154
 
                                if (lambdaExpression.IsAsync) {
3155
 
                                        result.IsAsync = true;
3156
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.AsyncModifierRole);
3157
 
                                }
3158
 
                                
3159
 
                                if (location == null || location.Count == l + 1) {
3160
 
                                        AddParameter (result, lambdaExpression.Parameters);
3161
 
                                        if (location != null)
3162
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.ArrowRole);
3163
 
                                } else {
3164
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.LPar);
3165
 
                                        AddParameter (result, lambdaExpression.Parameters);
3166
 
                                        if (location != null) {
3167
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.RPar);
3168
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.ArrowRole);
3169
 
                                        }
3170
 
                                }
3171
 
                                if (lambdaExpression.Block != null) {
3172
 
                                        if (lambdaExpression.Block.IsCompilerGenerated) {
3173
 
                                                ContextualReturn generatedReturn = (ContextualReturn)lambdaExpression.Block.Statements [0];
3174
 
                                                result.AddChild ((AstNode)generatedReturn.Expr.Accept (this), LambdaExpression.BodyRole);
3175
 
                                        } else {
3176
 
                                                result.AddChild ((AstNode)lambdaExpression.Block.Accept (this), LambdaExpression.BodyRole);
3177
 
                                        }
3178
 
                                }
3179
 
                                
3180
 
                                return result;
3181
 
                        }
3182
 
                        
3183
 
                        public override object Visit (ConstInitializer constInitializer)
3184
 
                        {
3185
 
                                return constInitializer.Expr.Accept (this);
3186
 
                        }
3187
 
                        
3188
 
                        public override object Visit (ArrayInitializer arrayInitializer)
3189
 
                        {
3190
 
                                var result = new ArrayInitializerExpression ();
3191
 
                                var location = LocationsBag.GetLocations (arrayInitializer);
3192
 
                                result.AddChild (new CSharpTokenNode (Convert (arrayInitializer.Location)), Roles.LBrace);
3193
 
                                var commaLocations = LocationsBag.GetLocations (arrayInitializer.Elements);
3194
 
                                for (int i = 0; i < arrayInitializer.Count; i++) {
3195
 
                                        var init = arrayInitializer [i];
3196
 
                                        if (init == null)
3197
 
                                                continue;
3198
 
                                        result.AddChild ((Expression)init.Accept (this), Roles.Expression);
3199
 
                                        if (commaLocations != null && i < commaLocations.Count)
3200
 
                                                result.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
3201
 
                                }
3202
 
                                
3203
 
                                if (location != null) {
3204
 
                                        if (location.Count == 2) // optional comma
3205
 
                                                result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Comma);
3206
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.RBrace);
3207
 
                                }
3208
 
                                return result;
3209
 
                        }
3210
 
                        
3211
 
                        #endregion
3212
 
                        
3213
 
                        #region LINQ expressions
3214
 
                        public override object Visit (Mono.CSharp.Linq.QueryExpression queryExpression)
3215
 
                        {
3216
 
                                var result = new QueryExpression ();
3217
 
                                
3218
 
                                var currentClause = queryExpression.next;
3219
 
                                
3220
 
                                while (currentClause != null) {
3221
 
                                        QueryClause clause = (QueryClause)currentClause.Accept (this);
3222
 
                                        if (clause is QueryContinuationClause) {
3223
 
                                                // insert preceding query at beginning of QueryContinuationClause
3224
 
                                                clause.InsertChildAfter (null, result, QueryContinuationClause.PrecedingQueryRole);
3225
 
                                                // create a new QueryExpression for the remaining query
3226
 
                                                result = new QueryExpression ();
3227
 
                                        }
3228
 
                                        result.AddChild (clause, QueryExpression.ClauseRole);
3229
 
                                        currentClause = currentClause.next;
3230
 
                                }
3231
 
                                
3232
 
                                return result;
3233
 
                        }
3234
 
                        
3235
 
                        public override object Visit (Mono.CSharp.Linq.QueryStartClause queryStart)
3236
 
                        {
3237
 
                                if (queryStart.Expr == null) {
3238
 
                                        var intoClause = new QueryContinuationClause ();
3239
 
                                        intoClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryContinuationClause.IntoKeywordRole);
3240
 
                                        intoClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3241
 
                                        return intoClause;
3242
 
                                }
3243
 
                                
3244
 
                                var fromClause = new QueryFromClause ();
3245
 
 
3246
 
                                fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryFromClause.FromKeywordRole);
3247
 
                                
3248
 
                                if (queryStart.IdentifierType != null)
3249
 
                                        fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3250
 
                                
3251
 
                                fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3252
 
                                
3253
 
                                var location = LocationsBag.GetLocations (queryStart);
3254
 
                                if (location != null)
3255
 
                                        fromClause.AddChild (new CSharpTokenNode (Convert (location [0])), QueryFromClause.InKeywordRole);
3256
 
 
3257
 
                                if (queryStart.Expr != null)
3258
 
                                        fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3259
 
                                return fromClause;
3260
 
                        }
3261
 
                        
3262
 
                        public override object Visit (Mono.CSharp.Linq.SelectMany queryStart)
3263
 
                        {
3264
 
                                var fromClause = new QueryFromClause ();
3265
 
 
3266
 
                                fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryFromClause.FromKeywordRole);
3267
 
                                
3268
 
                                if (queryStart.IdentifierType != null)
3269
 
                                        fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3270
 
                                
3271
 
                                fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3272
 
                                
3273
 
                                var location = LocationsBag.GetLocations (queryStart);
3274
 
                                if (location != null)
3275
 
                                        fromClause.AddChild (new CSharpTokenNode (Convert (location [0])), QueryFromClause.InKeywordRole);
3276
 
 
3277
 
                                if (queryStart.Expr != null)
3278
 
                                        fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3279
 
                                return fromClause;
3280
 
                        }
3281
 
                        
3282
 
                        public override object Visit (Mono.CSharp.Linq.Select sel)
3283
 
                        {
3284
 
                                var result = new QuerySelectClause ();
3285
 
                                result.AddChild (new CSharpTokenNode (Convert (sel.Location)), QuerySelectClause.SelectKeywordRole);
3286
 
                                if (sel.Expr != null)
3287
 
                                        result.AddChild ((Expression)sel.Expr.Accept (this), Roles.Expression);
3288
 
                                return result;
3289
 
                        }
3290
 
                        
3291
 
                        public override object Visit (Mono.CSharp.Linq.GroupBy groupBy)
3292
 
                        {
3293
 
                                var result = new QueryGroupClause ();
3294
 
                                var location = LocationsBag.GetLocations (groupBy);
3295
 
                                result.AddChild (new CSharpTokenNode (Convert (groupBy.Location)), QueryGroupClause.GroupKeywordRole);
3296
 
                                if (groupBy.ElementSelector != null)
3297
 
                                        result.AddChild ((Expression)groupBy.ElementSelector.Accept (this), QueryGroupClause.ProjectionRole);
3298
 
                                if (location != null)
3299
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryGroupClause.ByKeywordRole);
3300
 
                                if (groupBy.Expr != null)
3301
 
                                        result.AddChild ((Expression)groupBy.Expr.Accept (this), QueryGroupClause.KeyRole);
3302
 
                                return result;
3303
 
                        }
3304
 
                        
3305
 
                        public override object Visit (Mono.CSharp.Linq.Let l)
3306
 
                        {
3307
 
                                var result = new QueryLetClause ();
3308
 
                                var location = LocationsBag.GetLocations (l);
3309
 
                                
3310
 
                                result.AddChild (new CSharpTokenNode (Convert (l.Location)), QueryLetClause.LetKeywordRole);
3311
 
                                result.AddChild (Identifier.Create (l.IntoVariable.Name, Convert (l.IntoVariable.Location)), Roles.Identifier);
3312
 
                                if (location != null)
3313
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
3314
 
                                if (l.Expr != null)
3315
 
                                        result.AddChild ((Expression)l.Expr.Accept (this), Roles.Expression);
3316
 
                                return result;
3317
 
                        }
3318
 
                        
3319
 
                        public override object Visit (Mono.CSharp.Linq.Where w)
3320
 
                        {
3321
 
                                var result = new QueryWhereClause ();
3322
 
                                var location = LocationsBag.GetLocations (w);
3323
 
                                if (location != null)
3324
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryWhereClause.WhereKeywordRole);
3325
 
                                if (w.Expr != null)
3326
 
                                        result.AddChild ((Expression)w.Expr.Accept (this), Roles.Condition);
3327
 
                                return result;
3328
 
                        }
3329
 
                        
3330
 
                        public override object Visit (Mono.CSharp.Linq.Join join)
3331
 
                        {
3332
 
                                var result = new QueryJoinClause ();
3333
 
                                var location = LocationsBag.GetLocations (join);
3334
 
                                result.AddChild (new CSharpTokenNode (Convert (join.Location)), QueryJoinClause.JoinKeywordRole);
3335
 
                                result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3336
 
                                
3337
 
                                if (location != null)
3338
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryJoinClause.InKeywordRole);
3339
 
 
3340
 
                                if (join.Expr != null)
3341
 
                                        result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3342
 
                                
3343
 
                                if (location != null && location.Count > 1)
3344
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), QueryJoinClause.OnKeywordRole);
3345
 
                                
3346
 
                                var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3347
 
                                if (outer != null)
3348
 
                                        result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3349
 
                                
3350
 
                                if (location != null && location.Count > 2)
3351
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), QueryJoinClause.EqualsKeywordRole);
3352
 
                                
3353
 
                                var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3354
 
                                if (inner != null)
3355
 
                                        result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3356
 
                                
3357
 
                                return result;
3358
 
                        }
3359
 
                        
3360
 
                        public override object Visit (Mono.CSharp.Linq.GroupJoin join)
3361
 
                        {
3362
 
                                var result = new QueryJoinClause ();
3363
 
                                var location = LocationsBag.GetLocations (join);
3364
 
                                result.AddChild (new CSharpTokenNode (Convert (join.Location)), QueryJoinClause.JoinKeywordRole);
3365
 
                                
3366
 
                                // mcs seems to have swapped IntoVariable with JoinVariable, so we'll swap it back here
3367
 
                                result.AddChild (Identifier.Create (join.IntoVariable.Name, Convert (join.IntoVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3368
 
                                
3369
 
                                if (location != null)
3370
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryJoinClause.InKeywordRole);
3371
 
                                
3372
 
                                if (join.Expr != null)
3373
 
                                        result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3374
 
 
3375
 
                                if (location != null && location.Count > 1)
3376
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [1])), QueryJoinClause.OnKeywordRole);
3377
 
 
3378
 
                                var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3379
 
                                if (outer != null)
3380
 
                                        result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3381
 
                                
3382
 
 
3383
 
                                if (location != null && location.Count > 2)
3384
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [2])), QueryJoinClause.EqualsKeywordRole);
3385
 
                                var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3386
 
                                if (inner != null)
3387
 
                                        result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3388
 
                                
3389
 
                                if (location != null && location.Count > 3)
3390
 
                                        result.AddChild (new CSharpTokenNode (Convert (location [3])), QueryJoinClause.IntoKeywordRole);
3391
 
                                
3392
 
                                result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.IntoIdentifierRole);
3393
 
                                return result;
3394
 
                        }
3395
 
                        
3396
 
                        public override object Visit (Mono.CSharp.Linq.OrderByAscending orderByAscending)
3397
 
                        {
3398
 
                                var result = new QueryOrderClause ();
3399
 
                                
3400
 
                                var ordering = new QueryOrdering ();
3401
 
                                if (orderByAscending.Expr != null)
3402
 
                                        ordering.AddChild ((Expression)orderByAscending.Expr.Accept (this), Roles.Expression);
3403
 
                                var location = LocationsBag.GetLocations (orderByAscending);
3404
 
                                if (location != null) {
3405
 
                                        ordering.Direction = QueryOrderingDirection.Ascending;
3406
 
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.AscendingKeywordRole);
3407
 
                                }
3408
 
                                result.AddChild (ordering, QueryOrderClause.OrderingRole);
3409
 
                                return result;
3410
 
                        }
3411
 
                        
3412
 
                        public override object Visit (Mono.CSharp.Linq.OrderByDescending orderByDescending)
3413
 
                        {
3414
 
                                var result = new QueryOrderClause ();
3415
 
                                
3416
 
                                var ordering = new QueryOrdering ();
3417
 
                                if (orderByDescending.Expr != null)
3418
 
                                        ordering.AddChild ((Expression)orderByDescending.Expr.Accept (this), Roles.Expression);
3419
 
                                var location = LocationsBag.GetLocations (orderByDescending);
3420
 
                                if (location != null) {
3421
 
                                        ordering.Direction = QueryOrderingDirection.Descending;
3422
 
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.DescendingKeywordRole);
3423
 
                                }
3424
 
                                result.AddChild (ordering, QueryOrderClause.OrderingRole);
3425
 
                                return result;
3426
 
                        }
3427
 
                        
3428
 
                        public override object Visit (Mono.CSharp.Linq.ThenByAscending thenByAscending)
3429
 
                        {
3430
 
                                var result = new QueryOrderClause ();
3431
 
                                
3432
 
                                var ordering = new QueryOrdering ();
3433
 
                                if (thenByAscending.Expr != null)
3434
 
                                        ordering.AddChild ((Expression)thenByAscending.Expr.Accept (this), Roles.Expression);
3435
 
                                var location = LocationsBag.GetLocations (thenByAscending);
3436
 
                                if (location != null) {
3437
 
                                        ordering.Direction = QueryOrderingDirection.Ascending;
3438
 
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.AscendingKeywordRole);
3439
 
                                }
3440
 
                                result.AddChild (ordering, QueryOrderClause.OrderingRole);
3441
 
                                return result;
3442
 
                        }
3443
 
                        
3444
 
                        public override object Visit (Mono.CSharp.Linq.ThenByDescending thenByDescending)
3445
 
                        {
3446
 
                                var result = new QueryOrderClause ();
3447
 
                                
3448
 
                                var ordering = new QueryOrdering ();
3449
 
                                if (thenByDescending.Expr != null)
3450
 
                                        ordering.AddChild ((Expression)thenByDescending.Expr.Accept (this), Roles.Expression);
3451
 
                                var location = LocationsBag.GetLocations (thenByDescending);
3452
 
                                if (location != null) {
3453
 
                                        ordering.Direction = QueryOrderingDirection.Descending;
3454
 
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.DescendingKeywordRole);
3455
 
                                }
3456
 
                                result.AddChild (ordering, QueryOrderClause.OrderingRole);
3457
 
                                return result;
3458
 
                        }
3459
 
                        
3460
 
                        public override object Visit (Await awaitExpr)
3461
 
                        {
3462
 
                                var result = new UnaryOperatorExpression ();
3463
 
                                result.Operator = UnaryOperatorType.Await;
3464
 
                                result.AddChild (new CSharpTokenNode (Convert (awaitExpr.Location)), UnaryOperatorExpression.AwaitRole);
3465
 
                                if (awaitExpr.Expression != null)
3466
 
                                        result.AddChild ((Expression)awaitExpr.Expression.Accept (this), Roles.Expression);
3467
 
                                return result;
3468
 
                        }
3469
 
                        #endregion
3470
 
                }
3471
 
                
3472
 
                public CSharpParser ()
3473
 
                {
3474
 
                        CompilerSettings = new CompilerSettings ();
3475
 
                        CompilerSettings.Unsafe = true;
3476
 
                }
3477
 
                
3478
 
                public CSharpParser (CompilerSettings args)
3479
 
                {
3480
 
                        CompilerSettings = args;
3481
 
                }
3482
 
                
3483
 
                static AstNode GetOuterLeft (AstNode node)
3484
 
                {
3485
 
                        var outerLeft = node;
3486
 
                        while (outerLeft.FirstChild != null)
3487
 
                                outerLeft = outerLeft.FirstChild;
3488
 
                        return outerLeft;
3489
 
                }
3490
 
                
3491
 
                static AstNode NextLeaf (AstNode node)
3492
 
                {
3493
 
                        if (node == null)
3494
 
                                return null;
3495
 
                        var next = node.NextSibling;
3496
 
                        if (next == null)
3497
 
                                return node.Parent;
3498
 
                        return GetOuterLeft (next);
3499
 
                }
3500
 
                
3501
 
                static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
3502
 
                {
3503
 
                        var leaf = GetOuterLeft (conversionVisitor.Unit);
3504
 
                        for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) {
3505
 
                                var special = top.SpecialsBag.Specials [i];
3506
 
                                AstNode newLeaf = null;
3507
 
                                var comment = special as SpecialsBag.Comment;
3508
 
                                if (comment != null) {
3509
 
                                        // HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer
3510
 
                                        bool isMultilineDocumentationComment = (
3511
 
                                                comment.CommentType == SpecialsBag.CommentType.Multi
3512
 
                                                && comment.Content.StartsWith("*", StringComparison.Ordinal)
3513
 
                                                && !comment.Content.StartsWith("**", StringComparison.Ordinal)
3514
 
                                        );
3515
 
                                        if (conversionVisitor.convertTypeSystemMode && !(comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment))
3516
 
                                                continue;
3517
 
                                        var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType;
3518
 
                                        var start = new TextLocation (comment.Line, comment.Col);
3519
 
                                        var end = new TextLocation (comment.EndLine, comment.EndCol);
3520
 
                                        newLeaf = new Comment (type, start, end) {
3521
 
                                                        StartsLine = comment.StartsLine,
3522
 
                                                        Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
3523
 
                                        };
3524
 
                                } else {
3525
 
                                        var directive = special as SpecialsBag.PreProcessorDirective;
3526
 
                                        if (directive != null) {
3527
 
                                                newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
3528
 
                                                        Argument = directive.Arg,
3529
 
                                                        Take = directive.Take
3530
 
                                                };
3531
 
                                        }
3532
 
                                }
3533
 
                                if (newLeaf == null)
3534
 
                                        continue;
3535
 
                                
3536
 
                                while (true) {
3537
 
                                        var nextLeaf = NextLeaf (leaf);
3538
 
                                        // insert comment at begin
3539
 
                                        if (newLeaf.StartLocation < leaf.StartLocation) {
3540
 
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
3541
 
                                                while (node.Parent != null && node.FirstChild == leaf) {
3542
 
                                                        leaf = node;
3543
 
                                                        node = node.Parent;
3544
 
                                                }
3545
 
                                                if (newLeaf is Comment) {
3546
 
                                                        node.InsertChildBefore (leaf, (Comment)newLeaf, Roles.Comment);
3547
 
                                                } else {
3548
 
                                                        node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3549
 
                                                }
3550
 
                                                leaf = newLeaf;
3551
 
                                                break;
3552
 
                                        }
3553
 
                                        
3554
 
                                        // insert comment at the end
3555
 
                                        if (nextLeaf == null) {
3556
 
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
3557
 
                                                if (newLeaf is Comment) {
3558
 
                                                        node.AddChild ((Comment)newLeaf, Roles.Comment);
3559
 
                                                } else {
3560
 
                                                        node.AddChild ((PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3561
 
                                                }
3562
 
                                                leaf = newLeaf;
3563
 
                                                break;
3564
 
                                        }
3565
 
                                        
3566
 
                                        // comment is between 2 nodes
3567
 
                                        if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) {
3568
 
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
3569
 
                                                if (newLeaf is Comment) {
3570
 
                                                        node.InsertChildAfter (leaf, (Comment)newLeaf, Roles.Comment);
3571
 
                                                } else {
3572
 
                                                        node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3573
 
                                                }
3574
 
                                                leaf = newLeaf;
3575
 
                                                break;
3576
 
                                        }
3577
 
                                        leaf = nextLeaf;
3578
 
                                }
3579
 
                        }
3580
 
                }
3581
 
                
3582
 
                public class ErrorReportPrinter : ReportPrinter
3583
 
                {
3584
 
                        readonly string fileName;
3585
 
                        public readonly List<Error> Errors = new List<Error> ();
3586
 
                        
3587
 
                        public ErrorReportPrinter (string fileName)
3588
 
                        {
3589
 
                                this.fileName = fileName;
3590
 
                        }
3591
 
                        
3592
 
                        public override void Print (AbstractMessage msg, bool showFullPath)
3593
 
                        {
3594
 
                                base.Print (msg, showFullPath);
3595
 
                                var newError = new Error (msg.IsWarning ? ErrorType.Warning : ErrorType.Error, msg.Text, new DomRegion (fileName, msg.Location.Row, msg.Location.Column));
3596
 
                                Errors.Add (newError);
3597
 
                        }
3598
 
                }
3599
 
                ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (null);
3600
 
                
3601
 
                public ErrorReportPrinter ErrorPrinter {
3602
 
                        get {
3603
 
                                return errorReportPrinter;
3604
 
                        }
3605
 
                }
3606
 
                
3607
 
                public bool HasErrors {
3608
 
                        get {
3609
 
                                return errorReportPrinter.ErrorsCount > 0;
3610
 
                        }
3611
 
                }
3612
 
                
3613
 
                public bool HasWarnings {
3614
 
                        get {
3615
 
                                return errorReportPrinter.WarningsCount > 0;
3616
 
                        }
3617
 
                }
3618
 
                
3619
 
                public CompilationUnit Parse (ITextSource textSource, string fileName, int lineModifier = 0)
3620
 
                {
3621
 
                        return Parse (textSource.CreateReader (), fileName, lineModifier);
3622
 
                }
3623
 
                
3624
 
                public CompilationUnit Parse (TextReader reader, string fileName, int lineModifier = 0)
3625
 
                {
3626
 
                        // TODO: can we optimize this to avoid the text->stream->text roundtrip?
3627
 
                        using (MemoryStream stream = new MemoryStream ()) {
3628
 
                                StreamWriter w = new StreamWriter (stream, Encoding.UTF8);
3629
 
                                char[] buffer = new char[2048];
3630
 
                                int read;
3631
 
                                while ((read = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
3632
 
                                        w.Write (buffer, 0, read);
3633
 
                                w.Flush (); // we can't close the StreamWriter because that would also close the MemoryStream
3634
 
                                stream.Position = 0;
3635
 
                                
3636
 
                                return Parse (stream, fileName, lineModifier);
3637
 
                        }
3638
 
                }
3639
 
 
3640
 
                public CompilationUnit Parse(CompilerCompilationUnit top, string fileName, int lineModifier = 0)
3641
 
                {
3642
 
                        if (top == null) {
3643
 
                                return null;
3644
 
                        }
3645
 
                        CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag);
3646
 
                        top.ModuleCompiled.Accept(conversionVisitor);
3647
 
                        InsertComments(top, conversionVisitor);
3648
 
                        if (CompilationUnitCallback != null) {
3649
 
                                CompilationUnitCallback(top);
3650
 
                        }
3651
 
                        if (top.LastYYValue is Mono.CSharp.Expression) {
3652
 
                                conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode;
3653
 
                        }
3654
 
                        conversionVisitor.Unit.FileName = fileName;
3655
 
                        return conversionVisitor.Unit;
3656
 
                }
3657
 
                
3658
 
                public CompilerSettings CompilerSettings {
3659
 
                        get;
3660
 
                        internal set;
3661
 
                }
3662
 
                
3663
 
                public Action<CompilerCompilationUnit> CompilationUnitCallback {
3664
 
                        get;
3665
 
                        set;
3666
 
                }
3667
 
                
3668
 
                public bool GenerateTypeSystemMode {
3669
 
                        get;
3670
 
                        set;
3671
 
                }
3672
 
                
3673
 
                public CompilationUnit Parse (string program, string fileName)
3674
 
                {
3675
 
                        return Parse (new StringReader (program), fileName);
3676
 
                }
3677
 
                
3678
 
                internal static object parseLock = new object ();
3679
 
                
3680
 
                public CompilationUnit Parse(Stream stream, string fileName, int lineModifier = 0)
3681
 
                {
3682
 
                        lock (parseLock) {
3683
 
                                errorReportPrinter = new ErrorReportPrinter ("");
3684
 
                                var ctx = new CompilerContext (CompilerSettings, errorReportPrinter);
3685
 
                                ctx.Settings.TabSize = 1;
3686
 
                                var reader = new SeekableStreamReader (stream, Encoding.UTF8);
3687
 
                                var file = new SourceFile (fileName, fileName, 0);
3688
 
                                Location.Initialize (new List<SourceFile> (new [] { file }));
3689
 
                                var module = new ModuleContainer (ctx);
3690
 
                                var parser = Driver.Parse (reader, file, module, lineModifier);
3691
 
                                
3692
 
                                var top = new CompilerCompilationUnit () { 
3693
 
                                        ModuleCompiled = module,
3694
 
                                        LocationsBag = parser.LocationsBag,
3695
 
                                        SpecialsBag = parser.Lexer.sbag
3696
 
                                };
3697
 
                                var unit = Parse (top, fileName, lineModifier);         
3698
 
                                unit.Errors.AddRange (errorReportPrinter.Errors);
3699
 
                                CompilerCallableEntryPoint.Reset ();
3700
 
                                return unit;
3701
 
                        }
3702
 
                }
3703
 
 
3704
 
                public IEnumerable<EntityDeclaration> ParseTypeMembers (TextReader reader, int lineModifier = 0)
3705
 
                {
3706
 
                        string code = "unsafe partial class MyClass { " + Environment.NewLine + reader.ReadToEnd () + "}";
3707
 
                        var cu = Parse (new StringReader (code), "parsed.cs", lineModifier - 1);
3708
 
                        if (cu == null)
3709
 
                                return Enumerable.Empty<EntityDeclaration> ();
3710
 
                        var td = cu.Children.FirstOrDefault () as TypeDeclaration;
3711
 
                        if (td != null)
3712
 
                                return td.Members;
3713
 
                        return Enumerable.Empty<EntityDeclaration> ();
3714
 
                }
3715
 
                
3716
 
                public IEnumerable<Statement> ParseStatements (TextReader reader, int lineModifier = 0)
3717
 
                {
3718
 
                        string code = "void M() { " + Environment.NewLine + reader.ReadToEnd () + "}";
3719
 
                        var members = ParseTypeMembers (new StringReader (code), lineModifier - 1);
3720
 
                        var method = members.FirstOrDefault () as MethodDeclaration;
3721
 
                        if (method != null && method.Body != null)
3722
 
                                return method.Body.Statements;
3723
 
                        return Enumerable.Empty<Statement> ();
3724
 
                }
3725
 
                
3726
 
                public AstType ParseTypeReference (TextReader reader)
3727
 
                {
3728
 
                        string code = reader.ReadToEnd () + " a;";
3729
 
                        var members = ParseTypeMembers (new StringReader (code));
3730
 
                        var field = members.FirstOrDefault () as FieldDeclaration;
3731
 
                        if (field != null)
3732
 
                                return field.ReturnType;
3733
 
                        return AstType.Null;
3734
 
                }
3735
 
                
3736
 
                public AstNode ParseExpression (TextReader reader)
3737
 
                {
3738
 
                        var es = ParseStatements (new StringReader ("tmp = " + Environment.NewLine + reader.ReadToEnd () + ";"), -1).FirstOrDefault () as ExpressionStatement;
3739
 
                        if (es != null) {
3740
 
                                AssignmentExpression ae = es.Expression as AssignmentExpression;
3741
 
                                if (ae != null)
3742
 
                                        return ae.Right;
3743
 
                        }
3744
 
                        return null;
3745
 
                }
3746
 
                
3747
 
                /// <summary>
3748
 
                /// Parses a file snippet; guessing what the code snippet represents (compilation unit, type members, block, type reference, expression).
3749
 
                /// </summary>
3750
 
                public AstNode ParseSnippet (TextReader reader)
3751
 
                {
3752
 
                        // TODO: add support for parsing a part of a file
3753
 
                        throw new NotImplementedException ();
3754
 
                }
3755
 
                
3756
 
                public DocumentationReference ParseDocumentationReference (string cref)
3757
 
                {
3758
 
                        if (cref == null)
3759
 
                                throw new ArgumentNullException ("cref");
3760
 
                        cref = cref.Replace ('{', '<').Replace ('}', '>');
3761
 
                        // TODO: add support for parsing cref attributes
3762
 
                        // (documentation_parsing production, see DocumentationBuilder.HandleXrefCommon)
3763
 
                        throw new NotImplementedException ();
3764
 
                }
3765
 
        }
3766
 
}