Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 78e7fc4

Browse files
implement InsertMissingTokensDecorator
1 parent f100821 commit 78e7fc4

20 files changed

Lines changed: 635 additions & 216 deletions

src/Libraries/ICSharpCode.Decompiler/Ast/TextOutputFormatter.cs renamed to src/Libraries/ICSharpCode.Decompiler/Ast/TextTokenWriter.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace ICSharpCode.Decompiler.Ast
2929
{
30-
public class TextTokenWriter : ITokenWriter
30+
public class TextTokenWriter : TokenWriter
3131
{
3232
readonly ITextOutput output;
3333
readonly Stack<AstNode> nodeStack = new Stack<AstNode>();
@@ -47,7 +47,7 @@ public TextTokenWriter(ITextOutput output)
4747
this.output = output;
4848
}
4949

50-
public void WriteIdentifier(Identifier identifier)
50+
public override void WriteIdentifier(Identifier identifier)
5151
{
5252
var definition = GetCurrentDefinition();
5353
if (definition != null) {
@@ -160,12 +160,12 @@ object GetCurrentDefinition()
160160
return null;
161161
}
162162

163-
public void WriteKeyword(Role role, string keyword)
163+
public override void WriteKeyword(Role role, string keyword)
164164
{
165165
output.Write(keyword);
166166
}
167167

168-
public void WriteToken(Role role, string token)
168+
public override void WriteToken(Role role, string token)
169169
{
170170
// Attach member reference to token only if there's no identifier in the current node.
171171
MemberReference memberRef = GetCurrentMemberReference();
@@ -176,7 +176,7 @@ public void WriteToken(Role role, string token)
176176
output.Write(token);
177177
}
178178

179-
public void Space()
179+
public override void Space()
180180
{
181181
output.Write(' ');
182182
}
@@ -203,17 +203,17 @@ public void CloseBrace(BraceStyle style)
203203
braceLevelWithinType--;
204204
}
205205

206-
public void Indent()
206+
public override void Indent()
207207
{
208208
output.Indent();
209209
}
210210

211-
public void Unindent()
211+
public override void Unindent()
212212
{
213213
output.Unindent();
214214
}
215215

216-
public void NewLine()
216+
public override void NewLine()
217217
{
218218
if (lastUsingDeclaration) {
219219
output.MarkFoldEnd();
@@ -223,7 +223,7 @@ public void NewLine()
223223
output.WriteLine();
224224
}
225225

226-
public void WriteComment(CommentType commentType, string content)
226+
public override void WriteComment(CommentType commentType, string content)
227227
{
228228
switch (commentType) {
229229
case CommentType.SingleLine:
@@ -255,7 +255,7 @@ public void WriteComment(CommentType commentType, string content)
255255
}
256256
}
257257

258-
public void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
258+
public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
259259
{
260260
// pre-processor directive must start on its own line
261261
output.Write('#');
@@ -267,15 +267,20 @@ public void WritePreProcessorDirective(PreProcessorDirectiveType type, string ar
267267
output.WriteLine();
268268
}
269269

270-
public void WritePrimitiveValue(object value)
270+
public override void WritePrimitiveValue(object value, string literalValue = null)
271+
{
272+
273+
}
274+
275+
public override void WritePrimitiveType(string type)
271276
{
272277

273278
}
274279

275280
Stack<TextLocation> startLocations = new Stack<TextLocation>();
276281
Stack<MethodDebugSymbols> symbolsStack = new Stack<MethodDebugSymbols>();
277282

278-
public void StartNode(AstNode node)
283+
public override void StartNode(AstNode node)
279284
{
280285
if (nodeStack.Count == 0) {
281286
if (IsUsingDeclaration(node)) {
@@ -303,7 +308,7 @@ private bool IsUsingDeclaration(AstNode node)
303308
return node is UsingDeclaration || node is UsingAliasDeclaration;
304309
}
305310

306-
public void EndNode(AstNode node)
311+
public override void EndNode(AstNode node)
307312
{
308313
if (nodeStack.Pop() != node)
309314
throw new InvalidOperationException();

src/Libraries/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<Compile Include="Ast\DecompilerContext.cs" />
6161
<Compile Include="Ast\NameVariables.cs" />
6262
<Compile Include="Ast\NRefactoryExtensions.cs" />
63-
<Compile Include="Ast\TextOutputFormatter.cs" />
63+
<Compile Include="Ast\TextTokenWriter.cs" />
6464
<Compile Include="Ast\Transforms\AddCheckedBlocks.cs" />
6565
<Compile Include="Ast\Transforms\CombineQueryExpressions.cs" />
6666
<Compile Include="Ast\Transforms\ContextTrackingVisitor.cs" />

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,29 @@ public void AddChild<T> (T child, Role<T> role) where T : AstNode
406406
if (child == null || child.IsNull)
407407
return;
408408
ThrowIfFrozen();
409+
if (child == this)
410+
throw new ArgumentException ("Cannot add a node to itself as a child.", "child");
409411
if (child.parent != null)
410412
throw new ArgumentException ("Node is already used in another tree.", "child");
411413
if (child.IsFrozen)
412414
throw new ArgumentException ("Cannot add a frozen node.", "child");
413415
AddChildUnsafe (child, role);
414416
}
415417

418+
public void AddChildWithExistingRole<T> (T child) where T : AstNode
419+
{
420+
if (child == null || child.IsNull)
421+
return;
422+
ThrowIfFrozen();
423+
if (child == this)
424+
throw new ArgumentException ("Cannot add a node to itself as a child.", "child");
425+
if (child.parent != null)
426+
throw new ArgumentException ("Node is already used in another tree.", "child");
427+
if (child.IsFrozen)
428+
throw new ArgumentException ("Cannot add a frozen node.", "child");
429+
AddChildUnsafe (child, child.Role);
430+
}
431+
416432
/// <summary>
417433
/// Adds a child without performing any safety checks.
418434
/// </summary>

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/CSharpModifierToken.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,50 @@ public static int GetModifierLength(Modifiers modifier)
169169
throw new NotSupportedException("Invalid value for Modifiers");
170170
}
171171
}
172+
173+
public static Modifiers GetModifierValue(string modifier)
174+
{
175+
switch (modifier) {
176+
case "private":
177+
return Modifiers.Private;
178+
case "internal":
179+
return Modifiers.Internal;
180+
case "protected":
181+
return Modifiers.Protected;
182+
case "public":
183+
return Modifiers.Public;
184+
case "abstract":
185+
return Modifiers.Abstract;
186+
case "virtual":
187+
return Modifiers.Virtual;
188+
case "sealed":
189+
return Modifiers.Sealed;
190+
case "static":
191+
return Modifiers.Static;
192+
case "override":
193+
return Modifiers.Override;
194+
case "readonly":
195+
return Modifiers.Readonly;
196+
case "const":
197+
return Modifiers.Const;
198+
case "new":
199+
return Modifiers.New;
200+
case "partial":
201+
return Modifiers.Partial;
202+
case "extern":
203+
return Modifiers.Extern;
204+
case "volatile":
205+
return Modifiers.Volatile;
206+
case "unsafe":
207+
return Modifiers.Unsafe;
208+
case "async":
209+
return Modifiers.Async;
210+
case "any":
211+
// even though it's used for pattern matching only, 'any' needs to be in this list to be usable in the AST
212+
return Modifiers.Any;
213+
default:
214+
throw new NotSupportedException("Invalid value for Modifiers");
215+
}
216+
}
172217
}
173218
}

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/NullReferenceExpression.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// NullReferenceExpression.cs
3-
//
3+
//
44
// Author:
55
// Mike Krüger <mkrueger@novell.com>
66
//
@@ -38,6 +38,12 @@ public override TextLocation StartLocation {
3838
}
3939
}
4040

41+
internal void SetStartLocation(TextLocation value)
42+
{
43+
ThrowIfFrozen();
44+
this.location = value;
45+
}
46+
4147
public override TextLocation EndLocation {
4248
get {
4349
return new TextLocation (location.Line, location.Column + "null".Length);
@@ -57,7 +63,7 @@ public override void AcceptVisitor (IAstVisitor visitor)
5763
{
5864
visitor.VisitNullReferenceExpression (this);
5965
}
60-
66+
6167
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
6268
{
6369
return visitor.VisitNullReferenceExpression (this);

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/PrimitiveExpression.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public override TextLocation StartLocation {
4242
}
4343
}
4444

45+
internal void SetStartLocation(TextLocation value)
46+
{
47+
ThrowIfFrozen();
48+
this.startLocation = value;
49+
}
50+
4551
string literalValue;
4652
TextLocation? endLocation;
4753
public override TextLocation EndLocation {

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Identifier.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public override TextLocation StartLocation {
8383
}
8484
}
8585

86+
internal void SetStartLocation(TextLocation value)
87+
{
88+
ThrowIfFrozen();
89+
this.startLocation = value;
90+
}
91+
8692
const uint verbatimBit = 1u << AstNodeFlagsUsedBits;
8793

8894
public bool IsVerbatim {

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/PrimitiveType.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public override TextLocation StartLocation {
7171
return location;
7272
}
7373
}
74+
75+
internal void SetStartLocation(TextLocation value)
76+
{
77+
ThrowIfFrozen();
78+
this.location = value;
79+
}
80+
7481
public override TextLocation EndLocation {
7582
get {
7683
return new TextLocation (location.Line, location.Column + keyword.Length);

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
<Compile Include="Formatter\Indent.cs" />
208208
<Compile Include="OutputVisitor\CodeDomConvertVisitor.cs" />
209209
<Compile Include="OutputVisitor\CSharpAmbience.cs" />
210+
<Compile Include="OutputVisitor\InsertMissingTokensDecorator.cs" />
210211
<Compile Include="OutputVisitor\InsertParenthesesVisitor.cs" />
211212
<Compile Include="OutputVisitor\InsertRequiredSpacesDecorator.cs" />
212213
<Compile Include="OutputVisitor\CSharpOutputVisitor.cs" />

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public string ConvertEntity(IEntity entity)
4141
return writer.ToString();
4242
}
4343

44-
public void ConvertEntity(IEntity entity, ITokenWriter writer, CSharpFormattingOptions formattingPolicy)
44+
public void ConvertEntity(IEntity entity, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
4545
{
4646
if (entity == null)
4747
throw new ArgumentNullException("entity");
@@ -160,7 +160,7 @@ TypeSystemAstBuilder CreateAstBuilder()
160160
return astBuilder;
161161
}
162162

163-
void WriteTypeDeclarationName(ITypeDefinition typeDef, ITokenWriter writer, CSharpFormattingOptions formattingPolicy)
163+
void WriteTypeDeclarationName(ITypeDefinition typeDef, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
164164
{
165165
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
166166
EntityDeclaration node = astBuilder.ConvertEntity(typeDef);
@@ -178,7 +178,7 @@ void WriteTypeDeclarationName(ITypeDefinition typeDef, ITokenWriter writer, CSha
178178
}
179179
}
180180

181-
void WriteMemberDeclarationName(IMember member, ITokenWriter writer, CSharpFormattingOptions formattingPolicy)
181+
void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
182182
{
183183
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
184184
EntityDeclaration node = astBuilder.ConvertEntity(member);
@@ -235,7 +235,7 @@ void WriteMemberDeclarationName(IMember member, ITokenWriter writer, CSharpForma
235235
}
236236
}
237237

238-
void PrintModifiers(Modifiers modifiers, ITokenWriter writer)
238+
void PrintModifiers(Modifiers modifiers, TokenWriter writer)
239239
{
240240
foreach (var m in CSharpModifierToken.AllModifiers) {
241241
if ((modifiers & m) == m) {
@@ -245,7 +245,7 @@ void PrintModifiers(Modifiers modifiers, ITokenWriter writer)
245245
}
246246
}
247247

248-
void WriteQualifiedName(string name, ITokenWriter writer, CSharpFormattingOptions formattingPolicy)
248+
void WriteQualifiedName(string name, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
249249
{
250250
var node = AstType.Create(name);
251251
var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy);
@@ -270,7 +270,7 @@ public string ConvertType(IType type)
270270
return astType.ToString();
271271
}
272272

273-
public void ConvertType(IType type, ITokenWriter writer, CSharpFormattingOptions formattingPolicy)
273+
public void ConvertType(IType type, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
274274
{
275275
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
276276
AstType astType = astBuilder.ConvertType(type);

0 commit comments

Comments
 (0)