1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Linq; |
---|
22 | |
---|
23 | namespace ICSharpCode.NRefactory.CSharp |
---|
24 | { |
---|
25 | class InsertMissingTokensDecorator : DecoratingTokenWriter |
---|
26 | { |
---|
27 | readonly Stack<List<AstNode>> nodes = new Stack<List<AstNode>>(); |
---|
28 | List<AstNode> currentList; |
---|
29 | readonly ILocatable locationProvider; |
---|
30 | |
---|
31 | public InsertMissingTokensDecorator(TokenWriter writer, ILocatable locationProvider) |
---|
32 | : base(writer) |
---|
33 | { |
---|
34 | this.locationProvider = locationProvider; |
---|
35 | currentList = new List<AstNode>(); |
---|
36 | } |
---|
37 | |
---|
38 | public override void StartNode(AstNode node) |
---|
39 | { |
---|
40 | currentList.Add(node); |
---|
41 | nodes.Push(currentList); |
---|
42 | currentList = new List<AstNode>(); |
---|
43 | base.StartNode(node); |
---|
44 | } |
---|
45 | |
---|
46 | public override void EndNode(AstNode node) |
---|
47 | { |
---|
48 | System.Diagnostics.Debug.Assert(currentList != null); |
---|
49 | foreach (var removable in node.Children.Where(n => n is CSharpTokenNode)) { |
---|
50 | removable.Remove(); |
---|
51 | } |
---|
52 | foreach (var child in currentList) { |
---|
53 | System.Diagnostics.Debug.Assert(child.Parent == null || node == child.Parent); |
---|
54 | child.Remove(); |
---|
55 | node.AddChildWithExistingRole(child); |
---|
56 | } |
---|
57 | currentList = nodes.Pop(); |
---|
58 | base.EndNode(node); |
---|
59 | } |
---|
60 | |
---|
61 | public override void WriteToken(Role role, string token) |
---|
62 | { |
---|
63 | CSharpTokenNode t = new CSharpTokenNode(locationProvider.Location, (TokenRole)role); |
---|
64 | t.Role = role; |
---|
65 | EmptyStatement node = nodes.Peek().LastOrDefault() as EmptyStatement; |
---|
66 | if (node == null) |
---|
67 | currentList.Add(t); |
---|
68 | else { |
---|
69 | node.Location = locationProvider.Location; |
---|
70 | } |
---|
71 | base.WriteToken(role, token); |
---|
72 | } |
---|
73 | |
---|
74 | public override void WriteKeyword(Role role, string keyword) |
---|
75 | { |
---|
76 | TextLocation start = locationProvider.Location; |
---|
77 | CSharpTokenNode t = null; |
---|
78 | if (role is TokenRole) |
---|
79 | t = new CSharpTokenNode(start, (TokenRole)role); |
---|
80 | else if (role == EntityDeclaration.ModifierRole) |
---|
81 | t = new CSharpModifierToken(start, CSharpModifierToken.GetModifierValue(keyword)); |
---|
82 | else if (keyword == "this") { |
---|
83 | ThisReferenceExpression node = nodes.Peek().LastOrDefault() as ThisReferenceExpression; |
---|
84 | if (node != null) |
---|
85 | node.Location = start; |
---|
86 | } else if (keyword == "base") { |
---|
87 | BaseReferenceExpression node = nodes.Peek().LastOrDefault() as BaseReferenceExpression; |
---|
88 | if (node != null) |
---|
89 | node.Location = start; |
---|
90 | } |
---|
91 | if (t != null) currentList.Add(t); |
---|
92 | base.WriteKeyword(role, keyword); |
---|
93 | } |
---|
94 | |
---|
95 | public override void WriteIdentifier(Identifier identifier) |
---|
96 | { |
---|
97 | if (!identifier.IsNull) |
---|
98 | identifier.SetStartLocation(locationProvider.Location); |
---|
99 | currentList.Add(identifier); |
---|
100 | base.WriteIdentifier(identifier); |
---|
101 | } |
---|
102 | |
---|
103 | public override void WritePrimitiveValue(object value, string literalValue = null) |
---|
104 | { |
---|
105 | Expression node = nodes.Peek().LastOrDefault() as Expression; |
---|
106 | if (node is PrimitiveExpression) { |
---|
107 | ((PrimitiveExpression)node).SetStartLocation(locationProvider.Location); |
---|
108 | } |
---|
109 | if (node is NullReferenceExpression) { |
---|
110 | ((NullReferenceExpression)node).SetStartLocation(locationProvider.Location); |
---|
111 | } |
---|
112 | base.WritePrimitiveValue(value, literalValue); |
---|
113 | } |
---|
114 | |
---|
115 | public override void WritePrimitiveType(string type) |
---|
116 | { |
---|
117 | PrimitiveType node = nodes.Peek().LastOrDefault() as PrimitiveType; |
---|
118 | if (node != null) |
---|
119 | node.SetStartLocation(locationProvider.Location); |
---|
120 | base.WritePrimitiveType(type); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|