forked from xamarin/NRefactory
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSharpParser.cs
More file actions
4154 lines (3656 loc) · 176 KB
/
Copy pathCSharpParser.cs
File metadata and controls
4154 lines (3656 loc) · 176 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// CSharpParser.cs
//
// Author:
// Mike Krüger <mkrueger@novell.com>
//
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.NRefactory.MonoCSharp;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp
{
public class CSharpParser
{
CompilerSettings compilerSettings;
class ConversionVisitor : StructuralVisitor
{
SyntaxTree unit = new SyntaxTree();
internal bool convertTypeSystemMode;
public SyntaxTree Unit {
get {
return unit;
}
set {
unit = value;
}
}
public LocationsBag LocationsBag {
get;
private set;
}
public ConversionVisitor(bool convertTypeSystemMode, LocationsBag locationsBag)
{
this.convertTypeSystemMode = convertTypeSystemMode;
this.LocationsBag = locationsBag;
}
public static TextLocation Convert(Location loc)
{
return new TextLocation(loc.Row, loc.Column);
}
public override void Visit(ModuleContainer mc)
{
bool first = true;
foreach (var container in mc.Containers) {
var nspace = container as NamespaceContainer;
if (nspace == null) {
container.Accept(this);
continue;
}
NamespaceDeclaration nDecl = null;
var loc = LocationsBag.GetLocations(nspace);
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
nDecl = new NamespaceDeclaration();
if (loc != null) {
nDecl.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
}
nDecl.AddChild(ConvertNamespaceName(nspace.RealMemberName), NamespaceDeclaration.NamespaceNameRole);
if (loc != null && loc.Count > 1) {
nDecl.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.LBrace), Roles.LBrace);
}
AddToNamespace(nDecl);
namespaceStack.Push(nDecl);
}
if (nspace.Usings != null) {
foreach (var us in nspace.Usings) {
us.Accept(this);
}
}
if (first) {
first = false;
if (mc.OptAttributes != null) {
foreach (var attr in mc.OptAttributes.Sections) {
var section = ConvertAttributeSection(attr);
if (section != null)
unit.AddChild(section, SyntaxTree.MemberRole);
}
}
}
if (nspace.Containers != null) {
foreach (var subContainer in nspace.Containers) {
subContainer.Accept(this);
}
}
if (nDecl != null) {
AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
if (loc != null && loc.Count > 2)
nDecl.AddChild(new CSharpTokenNode(Convert(loc [2]), Roles.RBrace), Roles.RBrace);
if (loc != null && loc.Count > 3)
nDecl.AddChild(new CSharpTokenNode(Convert(loc [3]), Roles.Semicolon), Roles.Semicolon);
namespaceStack.Pop();
} else {
AddAttributeSection(unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
}
}
AddAttributeSection(unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
}
#region Global
readonly Stack<NamespaceDeclaration> namespaceStack = new Stack<NamespaceDeclaration>();
void AddTypeArguments(ATypeNameExpression texpr, AstType result)
{
var unbound = texpr.TypeArguments as UnboundTypeArguments;
if (unbound != null) {
TextLocation ll = Convert (texpr.Location);
result.AddChild(new CSharpTokenNode(ll, Roles.LChevron), Roles.LChevron);
ll = new TextLocation (ll.Line, ll.Column + 1);
for (int j = 0; j < unbound.Count; j++) {
result.AddChild (new SimpleType (), Roles.TypeArgument);
result.AddChild(new CSharpTokenNode(ll, Roles.LChevron), Roles.Comma);
ll = new TextLocation (ll.Line, ll.Column + 1);
}
result.AddChild(new CSharpTokenNode(ll, Roles.RChevron), Roles.RChevron);
/*
var loc2 = LocationsBag.GetLocations(texpr.TypeArguments);
if (loc2 == null)
return;
int j = 0;
if (j < loc2.Count)
result.AddChild(new CSharpTokenNode(Convert(loc2 [j++]), Roles.LChevron), Roles.LChevron);
while (j < loc2.Count - 1) {
result.AddChild (new SimpleType (), Roles.TypeArgument);
result.AddChild(new CSharpTokenNode(Convert(loc2 [j++]), Roles.LChevron), Roles.Comma);
}
if (j < loc2.Count) {
result.AddChild (new SimpleType (), Roles.TypeArgument);
result.AddChild(new CSharpTokenNode(Convert(loc2 [j++]), Roles.RChevron), Roles.RChevron);
}*/
return;
}
if (texpr.TypeArguments == null || texpr.TypeArguments.Args == null)
return;
var loc = LocationsBag.GetLocations(texpr.TypeArguments);
if (loc != null && loc.Count >= 2)
result.AddChild(new CSharpTokenNode(Convert(loc [loc.Count - 2]), Roles.LChevron), Roles.LChevron);
int i = 0;
foreach (var arg in texpr.TypeArguments.Args) {
result.AddChild(ConvertToType(arg), Roles.TypeArgument);
if (loc != null && i < loc.Count - 2)
result.AddChild(new CSharpTokenNode(Convert(loc [i++]), Roles.Comma), Roles.Comma);
}
if (loc != null && loc.Count >= 2)
result.AddChild(new CSharpTokenNode(Convert(loc [loc.Count - 1]), Roles.RChevron), Roles.RChevron);
}
static AstType ConvertToType(TypeParameter spec)
{
AstType result;
result = new SimpleType { IdentifierToken = Identifier.Create(spec.Name, Convert(spec.Location)) };
return result;
}
AstType ConvertToType(MemberName memberName)
{
AstType result;
if (memberName.Left != null) {
result = new MemberType();
result.AddChild(ConvertToType(memberName.Left), MemberType.TargetRole);
var loc = LocationsBag.GetLocations(memberName);
if (loc != null)
result.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Dot), Roles.Dot);
result.AddChild(Identifier.Create(memberName.Name, Convert(memberName.Location)), Roles.Identifier);
} else {
result = new SimpleType { IdentifierToken = Identifier.Create(memberName.Name, Convert(memberName.Location)) };
}
if (memberName.TypeParameters != null) {
var chevronLocs = LocationsBag.GetLocations(memberName.TypeParameters);
if (chevronLocs != null)
result.AddChild(new CSharpTokenNode(Convert(chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
var param = memberName.TypeParameters [i];
result.AddChild(new SimpleType(Identifier.Create(param.Name, Convert(param.Location))), Roles.TypeArgument);
if (chevronLocs != null && i < chevronLocs.Count - 2)
result.AddChild(new CSharpTokenNode(Convert(chevronLocs [i]), Roles.Comma), Roles.Comma);
}
if (chevronLocs != null)
result.AddChild(new CSharpTokenNode(Convert(chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
}
return result;
}
AstType ConvertToType(ICSharpCode.NRefactory.MonoCSharp.Expression typeName)
{
if (typeName == null) // may happen in typeof(Generic<,,,,>)
return new SimpleType();
var typeExpr = typeName as TypeExpression;
if (typeExpr != null) {
return new PrimitiveType(typeExpr.GetSignatureForError(), Convert(typeExpr.Location));
}
var qam = typeName as QualifiedAliasMember;
if (qam != null) {
var loc = LocationsBag.GetLocations(typeName);
var memberType = new MemberType();
memberType.Target = new SimpleType(qam.alias, Convert(qam.Location));
memberType.IsDoubleColon = true;
if (loc != null && loc.Count > 0)
memberType.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.DoubleColon), Roles.DoubleColon);
memberType.MemberNameToken = Identifier.Create(qam.Name, loc != null ? Convert(loc [1]) : TextLocation.Empty);
AddTypeArguments(qam, memberType);
return memberType;
}
var ma = typeName as MemberAccess;
if (ma != null) {
var memberType = new MemberType();
memberType.AddChild(ConvertToType(ma.LeftExpression), MemberType.TargetRole);
var loc = LocationsBag.GetLocations(ma);
if (loc != null)
memberType.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Dot), Roles.Dot);
memberType.MemberNameToken = Identifier.Create(ma.Name, Convert(ma.Location));
AddTypeArguments(ma, memberType);
return memberType;
}
var sn = typeName as SimpleName;
if (sn != null) {
var result = new SimpleType(sn.Name, Convert(sn.Location));
AddTypeArguments(sn, result);
return result;
}
var cc = typeName as ComposedCast;
if (cc != null) {
var baseType = ConvertToType(cc.Left);
var result = new ComposedType { BaseType = baseType };
var ccSpec = cc.Spec;
while (ccSpec != null) {
if (ccSpec.IsNullable) {
result.AddChild(new CSharpTokenNode(Convert(ccSpec.Location), ComposedType.NullableRole), ComposedType.NullableRole);
} else if (ccSpec.IsPointer) {
result.AddChild(new CSharpTokenNode(Convert(ccSpec.Location), ComposedType.PointerRole), ComposedType.PointerRole);
} else {
var location = LocationsBag.GetLocations(ccSpec);
var spec = new ArraySpecifier { Dimensions = ccSpec.Dimension };
spec.AddChild(new CSharpTokenNode(Convert(ccSpec.Location), Roles.LBracket), Roles.LBracket);
if (location != null)
spec.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.RBracket), Roles.RBracket);
result.ArraySpecifiers.Add(spec);
}
ccSpec = ccSpec.Next;
}
return result;
}
var sce = typeName as SpecialContraintExpr;
if (sce != null) {
switch (sce.Constraint) {
case SpecialConstraint.Class:
return new PrimitiveType("class", Convert(sce.Location));
case SpecialConstraint.Struct:
return new PrimitiveType("struct", Convert(sce.Location));
case SpecialConstraint.Constructor:
return new PrimitiveType("new", Convert(sce.Location));
}
}
return new SimpleType("unknown");
}
IEnumerable<Attribute> GetAttributes(IEnumerable<ICSharpCode.NRefactory.MonoCSharp.Attribute> optAttributes)
{
if (optAttributes == null)
yield break;
foreach (var attr in optAttributes) {
var result = new Attribute();
result.Type = ConvertToType(attr.TypeNameExpression);
var loc = LocationsBag.GetLocations(attr);
result.HasArgumentList = loc != null;
int pos = 0;
if (loc != null)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.LPar), Roles.LPar);
if (attr.PositionalArguments != null) {
foreach (var arg in attr.PositionalArguments) {
if (arg == null)
continue;
var na = arg as NamedArgument;
if (na != null) {
var newArg = new NamedArgumentExpression();
newArg.AddChild(Identifier.Create(na.Name, Convert(na.Location)), Roles.Identifier);
var argLoc = LocationsBag.GetLocations(na);
if (argLoc != null)
newArg.AddChild(new CSharpTokenNode(Convert(argLoc [0]), Roles.Colon), Roles.Colon);
if (na.Expr != null)
newArg.AddChild((Expression)na.Expr.Accept(this), Roles.Expression);
result.AddChild(newArg, Roles.Argument);
} else {
if (arg.Expr != null)
result.AddChild((Expression)arg.Expr.Accept(this), Roles.Argument);
}
if (loc != null && pos + 1 < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.Comma), Roles.Comma);
}
}
if (attr.NamedArguments != null) {
foreach (var arg in attr.NamedArguments) {
var na = (NamedArgument)arg;
var newArg = new NamedExpression();
newArg.AddChild(Identifier.Create(na.Name, Convert(na.Location)), Roles.Identifier);
var argLoc = LocationsBag.GetLocations(na);
if (argLoc != null)
newArg.AddChild(new CSharpTokenNode(Convert(argLoc [0]), Roles.Assign), Roles.Assign);
if (na.Expr != null)
newArg.AddChild((Expression)na.Expr.Accept(this), Roles.Expression);
result.AddChild(newArg, Roles.Argument);
if (loc != null && pos + 1 < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.Comma), Roles.Comma);
}
}
if (loc != null && pos < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.RPar), Roles.RPar);
yield return result;
}
}
AttributeSection ConvertAttributeSection(IEnumerable<ICSharpCode.NRefactory.MonoCSharp.Attribute> optAttributes)
{
if (optAttributes == null)
return null;
var result = new AttributeSection();
var loc = LocationsBag.GetLocations(optAttributes);
int pos = 0;
if (loc != null)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.LBracket), Roles.LBracket);
var first = optAttributes.FirstOrDefault();
string target = first != null ? first.ExplicitTarget : null;
if (!string.IsNullOrEmpty(target)) {
if (loc != null && pos < loc.Count - 1) {
result.AddChild(Identifier.Create(target, Convert(loc [pos++])), Roles.Identifier);
} else {
result.AddChild(Identifier.Create(target), Roles.Identifier);
}
if (loc != null && pos < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.Colon), Roles.Colon);
}
int attributeCount = 0;
foreach (var attr in GetAttributes (optAttributes)) {
result.AddChild(attr, Roles.Attribute);
if (loc != null && pos + 1 < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.Comma), Roles.Comma);
attributeCount++;
}
if (attributeCount == 0)
return null;
// Left and right bracket + commas between the attributes
int locCount = 2 + attributeCount - 1;
// optional comma
if (loc != null && pos < loc.Count - 1 && loc.Count == locCount + 1)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.Comma), Roles.Comma);
if (loc != null && pos < loc.Count)
result.AddChild(new CSharpTokenNode(Convert(loc [pos++]), Roles.RBracket), Roles.RBracket);
return result;
}
public override void Visit(NamespaceContainer ns)
{
NamespaceDeclaration nDecl = null;
var loc = LocationsBag.GetLocations(ns);
// <invalid> is caused by the parser - see Bug 12383 - [AST] Non existing namespaces generated
if (ns.NS != null && !string.IsNullOrEmpty(ns.NS.Name) && !ns.NS.Name.EndsWith("<invalid>", StringComparison.Ordinal)) {
nDecl = new NamespaceDeclaration();
if (loc != null) {
nDecl.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
}
nDecl.AddChild(ConvertNamespaceName(ns.RealMemberName), NamespaceDeclaration.NamespaceNameRole);
if (loc != null && loc.Count > 1) {
nDecl.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.LBrace), Roles.LBrace);
}
AddToNamespace(nDecl);
namespaceStack.Push(nDecl);
}
if (ns.Usings != null) {
foreach (var us in ns.Usings) {
us.Accept(this);
}
}
if (ns.Containers != null) {
foreach (var container in ns.Containers) {
container.Accept(this);
}
}
if (nDecl != null) {
AddAttributeSection(nDecl, ns.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
if (loc != null && loc.Count > 2)
nDecl.AddChild(new CSharpTokenNode(Convert(loc [2]), Roles.RBrace), Roles.RBrace);
if (loc != null && loc.Count > 3)
nDecl.AddChild(new CSharpTokenNode(Convert(loc [3]), Roles.Semicolon), Roles.Semicolon);
namespaceStack.Pop();
}
}
// public override void Visit (UsingsBag.Namespace nspace)
// {
//
//
// VisitNamespaceUsings (nspace);
// VisitNamespaceBody (nspace);
//
// }
//
AstType ConvertNamespaceName(MemberName memberName)
{
// HACK for a parser 'bug' - sometimes it generates "<invalid>" identifiers in namespace names (on certain bugs in the input file)
if (memberName.Name == "<invalid>")
return AstType.Null;
return ConvertToType(memberName);
}
public override void Visit(UsingNamespace un)
{
var ud = new UsingDeclaration();
var loc = LocationsBag.GetLocations(un);
ud.AddChild(new CSharpTokenNode(Convert(un.Location), UsingDeclaration.UsingKeywordRole), UsingDeclaration.UsingKeywordRole);
if (un.NamespaceExpression != null)
ud.AddChild(ConvertToType(un.NamespaceExpression), UsingDeclaration.ImportRole);
if (loc != null)
ud.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Semicolon), Roles.Semicolon);
AddToNamespace(ud);
}
public override void Visit(UsingClause un)
{
var ud = new UsingDeclaration();
var loc = LocationsBag.GetLocations(un);
ud.AddChild(new CSharpTokenNode(Convert(un.Location), UsingDeclaration.UsingKeywordRole), UsingDeclaration.UsingKeywordRole);
if (un.NamespaceExpression != null)
ud.AddChild(ConvertToType(un.NamespaceExpression), UsingDeclaration.ImportRole);
if (loc != null)
ud.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Semicolon), Roles.Semicolon);
AddToNamespace(ud);
}
public override void Visit(UsingAliasNamespace uan)
{
var ud = new UsingAliasDeclaration();
var loc = LocationsBag.GetLocations(uan);
ud.AddChild(new CSharpTokenNode(Convert(uan.Location), UsingAliasDeclaration.UsingKeywordRole), UsingAliasDeclaration.UsingKeywordRole);
ud.AddChild(Identifier.Create(uan.Alias.Value, Convert(uan.Alias.Location)), UsingAliasDeclaration.AliasRole);
if (loc != null)
ud.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Assign), Roles.Assign);
if (uan.NamespaceExpression != null)
ud.AddChild(ConvertToType(uan.NamespaceExpression), UsingAliasDeclaration.ImportRole);
if (loc != null && loc.Count > 1)
ud.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.Semicolon), Roles.Semicolon);
AddToNamespace(ud);
}
public override void Visit(UsingExternAlias uea)
{
var ud = new ExternAliasDeclaration();
var loc = LocationsBag.GetLocations(uea);
ud.AddChild(new CSharpTokenNode(Convert(uea.Location), Roles.ExternKeyword), Roles.ExternKeyword);
if (loc != null)
ud.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.AliasKeyword), Roles.AliasKeyword);
ud.AddChild(Identifier.Create(uea.Alias.Value, Convert(uea.Alias.Location)), Roles.Identifier);
if (loc != null && loc.Count > 1)
ud.AddChild(new CSharpTokenNode(Convert(loc [1]), Roles.Semicolon), Roles.Semicolon);
AddToNamespace(ud);
}
AstType ConvertImport(MemberName memberName)
{
if (memberName.Left != null) {
// left.name
var t = new MemberType();
// t.IsDoubleColon = memberName.IsDoubleColon;
t.AddChild(ConvertImport(memberName.Left), MemberType.TargetRole);
var loc = LocationsBag.GetLocations(memberName);
if (loc != null)
t.AddChild(new CSharpTokenNode(Convert(loc [0]), Roles.Dot), Roles.Dot);
t.AddChild(Identifier.Create(memberName.Name, Convert(memberName.Location)), Roles.Identifier);
AddTypeArguments(t, memberName);
return t;
} else {
var t = new SimpleType();
t.AddChild(Identifier.Create(memberName.Name, Convert(memberName.Location)), Roles.Identifier);
AddTypeArguments(t, memberName);
return t;
}
}
public override void Visit(MemberCore member)
{
Console.WriteLine("Unknown member:");
Console.WriteLine(member.GetType() + "-> Member {0}", member.GetSignatureForError());
}
readonly Stack<TypeDeclaration> typeStack = new Stack<TypeDeclaration>();
public override void Visit(Class c)
{
var newType = new TypeDeclaration();
newType.ClassType = ClassType.Class;
AddAttributeSection(newType, c);
var location = LocationsBag.GetMemberLocation(c);
AddModifiers(newType, location);
int curLoc = 0;
if (location != null && location.Count > 0)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.ClassKeyword), Roles.ClassKeyword);
newType.AddChild(Identifier.Create(c.MemberName.Name, Convert(c.MemberName.Location)), Roles.Identifier);
AddTypeParameters(newType, c.MemberName);
if (c.TypeBaseExpressions != null) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
var commaLocations = LocationsBag.GetLocations(c.TypeBaseExpressions);
int i = 0;
foreach (var baseTypes in c.TypeBaseExpressions) {
newType.AddChild(ConvertToType(baseTypes), Roles.BaseType);
if (commaLocations != null && i < commaLocations.Count) {
newType.AddChild(new CSharpTokenNode(Convert(commaLocations [i]), Roles.Comma), Roles.Comma);
i++;
}
}
}
AddConstraints(newType, c.CurrentTypeParameters);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
typeStack.Push(newType);
base.Visit(c);
AddAttributeSection(newType, c.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
if (location != null && curLoc < location.Count) {
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.RBrace), Roles.RBrace);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
} else {
// parser error, set end node to max value.
newType.AddChild(new ErrorNode(), Roles.Error);
}
typeStack.Pop();
AddType(newType);
}
public override void Visit(Struct s)
{
var newType = new TypeDeclaration();
newType.ClassType = ClassType.Struct;
AddAttributeSection(newType, s);
var location = LocationsBag.GetMemberLocation(s);
AddModifiers(newType, location);
int curLoc = 0;
if (location != null && location.Count > 0)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.StructKeyword), Roles.StructKeyword);
newType.AddChild(Identifier.Create(s.MemberName.Name, Convert(s.MemberName.Location)), Roles.Identifier);
AddTypeParameters(newType, s.MemberName);
if (s.TypeBaseExpressions != null) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
var commaLocations = LocationsBag.GetLocations(s.TypeBaseExpressions);
int i = 0;
foreach (var baseTypes in s.TypeBaseExpressions) {
newType.AddChild(ConvertToType(baseTypes), Roles.BaseType);
if (commaLocations != null && i < commaLocations.Count) {
newType.AddChild(new CSharpTokenNode(Convert(commaLocations [i]), Roles.Comma), Roles.Comma);
i++;
}
}
}
AddConstraints(newType, s.CurrentTypeParameters);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
typeStack.Push(newType);
base.Visit(s);
if (location != null && location.Count > 2) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.RBrace), Roles.RBrace);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
} else {
// parser error, set end node to max value.
newType.AddChild(new ErrorNode(), Roles.Error);
}
typeStack.Pop();
AddType(newType);
}
public override void Visit(Interface i)
{
var newType = new TypeDeclaration();
newType.ClassType = ClassType.Interface;
AddAttributeSection(newType, i);
var location = LocationsBag.GetMemberLocation(i);
AddModifiers(newType, location);
int curLoc = 0;
if (location != null && location.Count > 0)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.InterfaceKeyword), Roles.InterfaceKeyword);
newType.AddChild(Identifier.Create(i.MemberName.Name, Convert(i.MemberName.Location)), Roles.Identifier);
AddTypeParameters(newType, i.MemberName);
if (i.TypeBaseExpressions != null) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
var commaLocations = LocationsBag.GetLocations(i.TypeBaseExpressions);
int j = 0;
foreach (var baseTypes in i.TypeBaseExpressions) {
newType.AddChild(ConvertToType(baseTypes), Roles.BaseType);
if (commaLocations != null && j < commaLocations.Count) {
newType.AddChild(new CSharpTokenNode(Convert(commaLocations [j]), Roles.Comma), Roles.Comma);
j++;
}
}
}
AddConstraints(newType, i.CurrentTypeParameters);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
typeStack.Push(newType);
base.Visit(i);
if (location != null && location.Count > 2) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.RBrace), Roles.RBrace);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
} else {
// parser error, set end node to max value.
newType.AddChild(new ErrorNode(), Roles.Error);
}
typeStack.Pop();
AddType(newType);
}
public override void Visit(ICSharpCode.NRefactory.MonoCSharp.Delegate d)
{
var newDelegate = new DelegateDeclaration();
var location = LocationsBag.GetMemberLocation(d);
AddAttributeSection(newDelegate, d);
AddModifiers(newDelegate, location);
if (location != null && location.Count > 0) {
newDelegate.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.DelegateKeyword), Roles.DelegateKeyword);
}
if (d.ReturnType != null)
newDelegate.AddChild(ConvertToType(d.ReturnType), Roles.Type);
newDelegate.AddChild(Identifier.Create(d.MemberName.Name, Convert(d.MemberName.Location)), Roles.Identifier);
AddTypeParameters(newDelegate, d.MemberName);
if (location != null && location.Count > 1)
newDelegate.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.LPar), Roles.LPar);
AddParameter(newDelegate, d.Parameters);
if (location != null && location.Count > 2) {
newDelegate.AddChild(new CSharpTokenNode(Convert(location [2]), Roles.RPar), Roles.RPar);
}
AddConstraints(newDelegate, d.CurrentTypeParameters);
if (location != null && location.Count > 3) {
newDelegate.AddChild(new CSharpTokenNode(Convert(location [3]), Roles.Semicolon), Roles.Semicolon);
}
AddType(newDelegate);
}
void AddType(EntityDeclaration child)
{
if (typeStack.Count > 0) {
typeStack.Peek().AddChild(child, Roles.TypeMemberRole);
} else {
AddToNamespace(child);
}
}
void AddToNamespace(AstNode child)
{
if (namespaceStack.Count > 0) {
namespaceStack.Peek().AddChild(child, NamespaceDeclaration.MemberRole);
} else {
unit.AddChild(child, SyntaxTree.MemberRole);
}
}
public override void Visit(ICSharpCode.NRefactory.MonoCSharp.Enum e)
{
var newType = new TypeDeclaration();
newType.ClassType = ClassType.Enum;
AddAttributeSection(newType, e);
var location = LocationsBag.GetMemberLocation(e);
AddModifiers(newType, location);
int curLoc = 0;
if (location != null && location.Count > 0)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.EnumKeyword), Roles.EnumKeyword);
newType.AddChild(Identifier.Create(e.MemberName.Name, Convert(e.MemberName.Location)), Roles.Identifier);
if (e.BaseTypeExpression != null) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
newType.AddChild(ConvertToType(e.BaseTypeExpression), Roles.BaseType);
}
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
typeStack.Push(newType);
foreach (var m in e.Members) {
var member = m as EnumMember;
if (member == null) {
Console.WriteLine("WARNING - ENUM MEMBER: " + m);
continue;
}
Visit(member);
if (location != null && curLoc < location.Count - 1) //last one is closing brace
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Comma), Roles.Comma);
}
if (location != null && location.Count > 2) {
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.RBrace), Roles.RBrace);
if (location != null && curLoc < location.Count)
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
} else {
// parser error, set end node to max value.
newType.AddChild(new ErrorNode(), Roles.Error);
}
AddAttributeSection(newType, e.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
typeStack.Pop();
AddType(newType);
}
public override void Visit(EnumMember em)
{
var newField = new EnumMemberDeclaration();
AddAttributeSection(newField, em);
newField.AddChild(Identifier.Create(em.Name, Convert(em.Location)), Roles.Identifier);
if (em.Initializer != null) {
newField.AddChild(new CSharpTokenNode(Convert(em.Initializer.Location), Roles.Assign), Roles.Assign);
newField.AddChild((Expression)em.Initializer.Accept(this), EnumMemberDeclaration.InitializerRole);
}
//Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation);
typeStack.Peek().AddChild(newField, Roles.TypeMemberRole);
}
#endregion
#region Type members
public override void Visit(FixedField f)
{
var location = LocationsBag.GetMemberLocation(f);
int locationIdx = 0;
var newField = new FixedFieldDeclaration();
AddAttributeSection(newField, f);
AddModifiers(newField, location);
if (location != null && location.Count > 0)
newField.AddChild(new CSharpTokenNode(Convert(location [locationIdx++]), FixedFieldDeclaration.FixedKeywordRole), FixedFieldDeclaration.FixedKeywordRole);
if (f.TypeExpression != null)
newField.AddChild(ConvertToType(f.TypeExpression), Roles.Type);
var variable = new FixedVariableInitializer();
variable.AddChild(Identifier.Create(f.MemberName.Name, Convert(f.MemberName.Location)), Roles.Identifier);
if (f.Initializer != null && !f.Initializer.IsNull) {
variable.AddChild(new CSharpTokenNode(Convert(f.Initializer.Location), Roles.LBracket), Roles.LBracket);
variable.AddChild((Expression)f.Initializer.Accept(this), Roles.Expression);
var bracketLocations = LocationsBag.GetLocations(f.Initializer);
if (bracketLocations != null)
variable.AddChild(new CSharpTokenNode(Convert(bracketLocations [0]), Roles.RBracket), Roles.RBracket);
}
newField.AddChild(variable, FixedFieldDeclaration.VariableRole);
if (f.Declarators != null) {
foreach (var decl in f.Declarators) {
var declLoc = LocationsBag.GetLocations(decl);
if (declLoc != null)
newField.AddChild(new CSharpTokenNode(Convert(declLoc [0]), Roles.Comma), Roles.Comma);
variable = new FixedVariableInitializer();
variable.AddChild(Identifier.Create(decl.Name.Value, Convert(decl.Name.Location)), Roles.Identifier);
variable.AddChild(new CSharpTokenNode(Convert(decl.Initializer.Location), Roles.LBracket), Roles.LBracket);
variable.AddChild((Expression)decl.Initializer.Accept(this), Roles.Expression);
var bracketLocations = LocationsBag.GetLocations(decl.Initializer);
if (bracketLocations != null)
variable.AddChild(new CSharpTokenNode(Convert(bracketLocations [0]), Roles.RBracket), Roles.RBracket);
newField.AddChild(variable, FixedFieldDeclaration.VariableRole);
}
}
if (location != null && location.Count > locationIdx)
newField.AddChild(new CSharpTokenNode(Convert(location [locationIdx]), Roles.Semicolon), Roles.Semicolon);
typeStack.Peek().AddChild(newField, Roles.TypeMemberRole);
}
public override void Visit(Field f)
{
var location = LocationsBag.GetMemberLocation(f);
var newField = new FieldDeclaration();
AddAttributeSection(newField, f);
AddModifiers(newField, location);
newField.AddChild(ConvertToType(f.TypeExpression), Roles.Type);
var variable = new VariableInitializer();
variable.AddChild(Identifier.Create(f.MemberName.Name, Convert(f.MemberName.Location)), Roles.Identifier);
int locationIdx = 0;
if (f.Initializer != null) {
if (location != null)
variable.AddChild(new CSharpTokenNode(Convert(location [locationIdx++]), Roles.Assign), Roles.Assign);
variable.AddChild((Expression)f.Initializer.Accept(this), Roles.Expression);
}
newField.AddChild(variable, Roles.Variable);
if (f.Declarators != null) {
foreach (var decl in f.Declarators) {
var declLoc = LocationsBag.GetLocations(decl);
if (declLoc != null)
newField.AddChild(new CSharpTokenNode(Convert(declLoc [0]), Roles.Comma), Roles.Comma);
variable = new VariableInitializer();
variable.AddChild(Identifier.Create(decl.Name.Value, Convert(decl.Name.Location)), Roles.Identifier);
if (decl.Initializer != null) {
if (declLoc != null)
variable.AddChild(new CSharpTokenNode(Convert(declLoc [1]), Roles.Assign), Roles.Assign);
variable.AddChild((Expression)decl.Initializer.Accept(this), Roles.Expression);
}
newField.AddChild(variable, Roles.Variable);
}
}
if (location != null && location.Count > locationIdx)
newField.AddChild(new CSharpTokenNode(Convert(location [locationIdx++]), Roles.Semicolon), Roles.Semicolon);
typeStack.Peek().AddChild(newField, Roles.TypeMemberRole);
}
public override void Visit(Const c)
{
var location = LocationsBag.GetMemberLocation(c);
var newField = new FieldDeclaration();
AddAttributeSection(newField, c);
AddModifiers(newField, location);
if (location != null)
newField.AddChild(new CSharpModifierToken(Convert(location [0]), Modifiers.Const), EntityDeclaration.ModifierRole);
newField.AddChild(ConvertToType(c.TypeExpression), Roles.Type);
var variable = new VariableInitializer();
variable.AddChild(Identifier.Create(c.MemberName.Name, Convert(c.MemberName.Location)), Roles.Identifier);
if (c.Initializer != null) {
variable.AddChild(new CSharpTokenNode(Convert(c.Initializer.Location), Roles.Assign), Roles.Assign);
variable.AddChild((Expression)c.Initializer.Accept(this), Roles.Expression);
}
newField.AddChild(variable, Roles.Variable);
if (c.Declarators != null) {
foreach (var decl in c.Declarators) {
var declLoc = LocationsBag.GetLocations(decl);
if (declLoc != null)
newField.AddChild(new CSharpTokenNode(Convert(declLoc [0]), Roles.Comma), Roles.Comma);
variable = new VariableInitializer();
variable.AddChild(Identifier.Create(decl.Name.Value, Convert(decl.Name.Location)), Roles.Identifier);
if (decl.Initializer != null) {
variable.AddChild(new CSharpTokenNode(Convert(decl.Initializer.Location), Roles.Assign), Roles.Assign);
variable.AddChild((Expression)decl.Initializer.Accept(this), Roles.Expression);
}
newField.AddChild(variable, Roles.Variable);
}
}
if (location != null)
newField.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.Semicolon), Roles.Semicolon);
typeStack.Peek().AddChild(newField, Roles.TypeMemberRole);
}
public override void Visit(Operator o)
{
var newOperator = new OperatorDeclaration();
newOperator.OperatorType = (OperatorType)o.OperatorType;
var location = LocationsBag.GetMemberLocation(o);
AddAttributeSection(newOperator, o);
AddModifiers(newOperator, location);
if (o.OperatorType == Operator.OpType.Implicit) {
if (location != null && location.Count > 0) {
newOperator.AddChild(new CSharpTokenNode(Convert(location [0]), OperatorDeclaration.ImplicitRole), OperatorDeclaration.ImplicitRole);
if (location.Count > 1)
newOperator.AddChild(new CSharpTokenNode(Convert(location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
}
newOperator.AddChild(ConvertToType(o.TypeExpression), Roles.Type);
} else if (o.OperatorType == Operator.OpType.Explicit) {
if (location != null && location.Count > 0) {
newOperator.AddChild(new CSharpTokenNode(Convert(location [0]), OperatorDeclaration.ExplicitRole), OperatorDeclaration.ExplicitRole);
if (location.Count > 1)
newOperator.AddChild(new CSharpTokenNode(Convert(location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
}
newOperator.AddChild(ConvertToType(o.TypeExpression), Roles.Type);
} else {
newOperator.AddChild(ConvertToType(o.TypeExpression), Roles.Type);
if (location != null && location.Count > 0)
newOperator.AddChild(new CSharpTokenNode(Convert(location [0]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
if (location != null && location.Count > 1) {
var r = OperatorDeclaration.GetRole(newOperator.OperatorType);
newOperator.AddChild(new CSharpTokenNode(Convert(location [1]), r), r);
}
}
if (location != null && location.Count > 2)
newOperator.AddChild(new CSharpTokenNode(Convert(location [2]), Roles.LPar), Roles.LPar);
AddParameter(newOperator, o.ParameterInfo);
if (location != null && location.Count > 3)
newOperator.AddChild(new CSharpTokenNode(Convert(location [3]), Roles.RPar), Roles.RPar);
if (o.Block != null) {
var blockStatement = o.Block.Accept(this) as BlockStatement;
if (blockStatement != null)
newOperator.AddChild(blockStatement, Roles.Body);
} else {
if (location != null && location.Count >= 5)
newOperator.AddChild(new CSharpTokenNode(Convert(location [4]), Roles.Semicolon), Roles.Semicolon);
}
typeStack.Peek().AddChild(newOperator, Roles.TypeMemberRole);
}
public void AddAttributeSection(AstNode parent, Attributable a)
{
if (a == null || a.OptAttributes == null)
return;
AddAttributeSection(parent, a.OptAttributes);
}
public void AddAttributeSection(AstNode parent, Attributes attrs, Role<AttributeSection> role)
{
if (attrs == null)
return;
foreach (var attr in attrs.Sections) {
var section = ConvertAttributeSection(attr);
if (section == null)
continue;
parent.AddChild(section, role);
}
}
public void AddAttributeSection(AstNode parent, Attributes attrs)
{
AddAttributeSection(parent, attrs, EntityDeclaration.AttributeRole);