Permalink
Browse files

Cleanup and work on statements

  • Loading branch information...
Kathleen Dollard
Kathleen Dollard committed Jul 11, 2014
1 parent afd2ac5 commit 58f147fe9b9d3455c32b5a437a72f967df34cc09
Showing with 654 additions and 444 deletions.
  1. BIN Documentation/Structural Interrogation Walk-throughs.docx
  2. +6 −2 Readme.md
  3. +165 −37 RoslynDom/RDomFactory.cs
  4. +7 −1 RoslynDom/RoslynDom.csproj
  5. +42 −0 RoslynDom/StatementImplementations/RDomAssignmentStatement.cs
  6. +11 −25 RoslynDom/{Implementations/RDomStatement.cs → StatementImplementations/RDomBaseBlock.cs}
  7. +37 −0 RoslynDom/StatementImplementations/RDomBlockStatement.cs
  8. +53 −0 RoslynDom/StatementImplementations/RDomDeclarationStatement.cs
  9. +82 −0 RoslynDom/StatementImplementations/RDomIfStatement.cs
  10. +42 −0 RoslynDom/StatementImplementations/RDomReturnStatemen.cs
  11. +35 −0 RoslynDom/StatementImplementations/RDomStatement.cs
  12. +0 −1 RoslynDomCommon/BaseInterfaces/IStatement.cs
  13. +12 −10 RoslynDomCommon/Enums/StatementKind.cs
  14. +3 −3 RoslynDomCommon/Interfaces/IAttribute.cs
  15. 0 RoslynDomCommon/InterfacesForStatements/{IContinue.cs → IContinueStatement.cs}
  16. +4 −4 RoslynDomCommon/InterfacesForStatements/IDeclarationStatement.cs
  17. +9 −0 RoslynDomCommon/InterfacesForStatements/IEmptyStatement.cs
  18. +3 −4 RoslynDomCommon/RoslynDomCommon.csproj
  19. +0 −29 RoslynDomCommon/SameIntentInitial/SameIIntentClass.cs
  20. +0 −21 RoslynDomCommon/SameIntentInitial/SameIntentBaseType.cs
  21. +0 −113 RoslynDomCommon/SameIntentInitial/SameIntentCommon.cs
  22. +0 −24 RoslynDomCommon/SameIntentInitial/SameIntentFactory.cs
  23. +0 −29 RoslynDomCommon/SameIntentInitial/SameIntentHelpers.cs
  24. +0 −22 RoslynDomCommon/SameIntentInitial/SameIntentNamespace.cs
  25. +0 −22 RoslynDomCommon/SameIntentInitial/SameIntentNestedContainer.cs
  26. +0 −21 RoslynDomCommon/SameIntentInitial/SameIntentRoot.cs
  27. +0 −22 RoslynDomCommon/SameIntentInitial/SameIntentStemContainer.cs
  28. +73 −29 RoslynDomExampleTests/Interrogation.cs
  29. +4 −4 RoslynDomExampleTests/RoslynDomLoad.cs
  30. +3 −3 RoslynDomExampleTests/Scenario_PatternMatchingSelection.cs
  31. +7 −3 RoslynDomExampleTests/TestFile.cs
  32. +3 −3 RoslynDomExampleTests/WorkspaceTests.cs
  33. +2 −1 RoslynDomTests/AttributeTests.cs
  34. +14 −6 RoslynDomTests/CopyTests.cs
  35. +37 −5 RoslynDomTests/PlaySpace.cs
Binary file not shown.
View
@@ -1,11 +1,15 @@
-RoslynDOM
+RoslynDom
=========
-This provides programmer friendly view of your application via the Roslyn AST, currently in a very preliminary form
+This provides programmer friendly view of your application via the .NET Compiler Platform (code named Roslyn) from Microsoft. This is an early, experimental release.
Available on NuGet
---
[Install-Package RoslynDOM -Pre](https://www.nuget.org/packages/RoslynDOM/)
+More info
+---
+You can find more info on [my blog] (http://msmvps.com/blogs/kathleen/default.aspx)
+
Examples
---
```C#
View
@@ -87,6 +87,26 @@ private static IUsing MakeUsingDirective(UsingDirectiveSyntax x)
return (list.Count() > startCount);
}
+ private static bool DoStatement<T>(StatementSyntax val,
+ Func<T, IStatement> doAction, out IStatement retValue)
+ where T : class
+ {
+ var item = val as T;
+ retValue = item != null ? doAction(item) : null;
+ return (retValue != null);
+ }
+
+ private static bool DoStatements<T>(StatementSyntax val,
+ Action<T, List<IStatement>> doAction, List<IStatement> list)
+ where T : class
+ {
+ var item = val as T;
+ if (item == null) return false;
+ var startCount = list.Count();
+ doAction(item, list);
+ return (list.Count() > startCount);
+ }
+
/// <summary>
/// Creates namespace and class
/// </summary>
@@ -177,9 +197,109 @@ private static IParameter MakeParameter(ParameterSyntax rawParm)
private static IStatement MakeStatement(StatementSyntax rawStatement)
{
- var statements = ListUtilities.MakeList(rawStatement, x => GetStatements(x), x => MakeStatement(x));
- var publicAnnotations = GetPublicAnnotations(rawStatement).ToArray();
- return new RDomStatement(rawStatement, statements, publicAnnotations);
+ IStatement ret;
+ // The action happens in DoMember. I felt it read better with else than negation and it made copying new lines easier
+ if (DoStatement<BlockSyntax>(rawStatement, MakeBlockStatement, out ret)) { }
+ else if (DoStatement<IfStatementSyntax>(rawStatement, MakeIfStatement, out ret)) { }
+ return ret as IStatement;
+
+ //ForEach,
+ //For,
+ //Empty,
+ //Return,
+ //Declaration,
+ //Try,
+
+ ////While, variation of Do
+ ////Else, characteristic of If
+ ////Using, characteristic of Block
+ ////Catch, characteristic of Try
+ ////Switch, // can this be handled as a special case of if?
+ //Break, // probably have to support
+ //Continue, // probably have to support
+ //Throw, // probably have to support
+
+ ////Expression statements, // break this appart into two kinds
+ //Invocation,
+ //Assignment,
+
+ //Special // (platform or lanuguage specific)
+ ////Checked (block)
+ ////Lock,
+ ////Yield, Split into YieldBreak and YieldReturn (expression)
+
+ // Planning to avoid unless someone has a scenario
+ //Unsafe,
+ //Fixed,
+ //Goto,
+ //Labeled,
+ }
+
+ private static IBlockStatement MakeBlockStatement(BlockSyntax rawBlock)
+ {
+ var publicAnnotations = GetPublicAnnotations(rawBlock).ToArray();
+ var statements = ListUtilities.MakeList(rawBlock, x => x.Statements, x => MakeStatement(x));
+ return new RDomBlockStatement(rawBlock, statements, publicAnnotations);
+ }
+
+ private static IIfStatement MakeIfStatement(IfStatementSyntax rawIf)
+ {
+ var publicAnnotations = GetPublicAnnotations(rawIf).ToArray();
+ var hasBlock = false;
+ var statements = new List<IStatement>();
+ var block = rawIf.Statement as BlockSyntax;
+ if (block != null)
+ {
+ hasBlock = true;
+ statements.AddRange(ListUtilities.MakeList(rawIf, x => block.Statements, x => MakeStatement(x)));
+ }
+ else
+ { statements.Add(MakeStatement(rawIf.Statement)); }
+ IEnumerable<IIfStatement> elses = null;
+ if (rawIf.Else != null)
+ { elses = MakeElses(rawIf.Else.Statement); }
+ return new RDomIfStatement(rawIf, hasBlock, statements, elses, publicAnnotations);
+ }
+
+ private static IEnumerable<IIfStatement> MakeElses(StatementSyntax rawElseStatement)
+ {
+ var publicAnnotations = GetPublicAnnotations(rawElseStatement).ToArray();
+ var elses = new List<IIfStatement>();
+ var elseStatement = rawElseStatement as IfStatementSyntax;
+ if (elseStatement != null)
+ {
+ elses.Add(MakeIfStatement(elseStatement));
+ elses.AddRange(MakeElses(elseStatement.Else.Statement));
+ }
+ else
+ {
+ var statements = new List<IStatement>();
+ bool hasBlock = false;
+ var blockElseStatement = rawElseStatement as BlockSyntax;
+ if (blockElseStatement != null)
+ {
+ hasBlock = true;
+ statements.AddRange(ListUtilities.MakeList(rawElseStatement, x => blockElseStatement.Statements, x => MakeStatement(x)));
+ }
+ else
+ { statements.Add(MakeStatement(rawElseStatement)); }
+ elses.Add(new RDomIfStatement(null, hasBlock, statements, null, publicAnnotations));
+
+ }
+ return elses;
+ }
+
+ private static IEnumerable<IDeclarationStatement> MakeDeclarationStatement(LocalDeclarationStatementSyntax rawDeclaration)
+ {
+ var list = new List<IDeclarationStatement>();
+ var declaration = rawDeclaration.Declaration;
+ var variables = declaration.Variables.OfType<VariableDeclaratorSyntax>();
+ foreach (var variable in variables)
+ {
+ var publicAnnotations = GetPublicAnnotations(rawDeclaration).ToArray();
+ list.Add(new RDomDeclarationStatement(rawDeclaration, declaration, variable, publicAnnotations));
+ }
+ return list;
}
private static IEnumerable<StatementSyntax> GetStatements(MethodDeclarationSyntax rawMethod)
@@ -188,47 +308,55 @@ private static IEnumerable<StatementSyntax> GetStatements(MethodDeclarationSynta
return rawMethod.Body.Statements;
}
+
private static IEnumerable<StatementSyntax> GetStatements(StatementSyntax rawStatement)
{
var list = new List<StatementSyntax>();
-
- // These have nested block syntax
+ // This handles method and property
if (LoadStatementListFromList<BlockSyntax>(rawStatement, x => x.Statements, list)) { return list; }
- if (LoadStatementListFromList<UnsafeStatementSyntax>(rawStatement, x => x.Block.Statements, list)) { return list; }
- if (LoadStatementListFromList<CheckedStatementSyntax>(rawStatement, x => x.Block.Statements, list)) { return list; }
- if (LoadStatementListFromList<TryStatementSyntax>(rawStatement, x => x.Block.Statements, list)) { return list; }
-
- // These have a statement which often, but not always, holds a nested block
- if (LoadStatementListFromItem<DoStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<ForEachStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<ForStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<LockStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<WhileStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<UsingStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
- if (LoadStatementListFromItem<IfStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; } // if has a second block for else
-
- // These do not have nested blocksyntax or statements
- // BreakStatementSyntax
- // ContinueStatementSyntax
- // EmptyStatementSyntax
- // ExpressionStatementSyntax
- // LocalDeclarationStatement
- // ReturnStatementSyntax
- // ThrowStatementSyntax
- // YieldStatementSyntax
-
- // This is a very weird special case
- // SwitchStatementSyntax
-
- // These are items I currently do not plan to support - although need to confirm that switch doesn't use labeled statements
- // FixedStatementSyntax
- // GotoStatementSyntax
- // LabeledStatementSyntax
+
+ //// These have nested block syntax
+ //if (LoadStatementListFromList<BlockSyntax>(rawStatement, x => x.Statements, list)) { return list; }
+ //if (LoadStatementListFromList<CheckedStatementSyntax>(rawStatement, x => x.Block.Statements, list)) { return list; }
+
+ //// These have a statement which often, but not always, holds a nested block
+ //if (LoadStatementListFromItem<DoStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+ //if (LoadStatementListFromItem<ForEachStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+ //if (LoadStatementListFromItem<ForStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+ //if (LoadStatementListFromItem<LockStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+ //if (LoadStatementListFromItem<WhileStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+ //if (LoadStatementListFromItem<UsingStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; }
+
+ //// These have more than one statement block
+ //if (LoadStatementListFromItem<IfStatementSyntax>(rawStatement, x => x.Statement, list)) { return list; } // if has a second block for else
+ //if (LoadStatementListFromList<TryStatementSyntax>(rawStatement, x => x.Block.Statements, list)) { return list; }
+
+ //// These do not have nested blocksyntax or statements
+ //// BreakStatementSyntax
+ //// ContinueStatementSyntax
+ //// EmptyStatementSyntax
+ //// ExpressionStatementSyntax
+ //// LocalDeclarationStatement
+ //// ReturnStatementSyntax
+ //// ThrowStatementSyntax
+ //// YieldStatementSyntax
+
+ //// This is a very weird special case
+ //// SwitchStatementSyntax
+
+ //// These are items I currently do not plan to support - although need to confirm that switch doesn't use labeled statements
+ //// FixedStatementSyntax
+ //// GotoStatementSyntax
+ //// LabeledStatementSyntax
+ //// UnsafeStatementSyntax
return list;
}
- private static bool LoadStatementListFromList<T>(StatementSyntax rawStatement, Func<T, IEnumerable<StatementSyntax>> getList, List<StatementSyntax> list)
+ private static bool LoadStatementListFromList<T>(
+ StatementSyntax rawStatement,
+ Func<T, IEnumerable<StatementSyntax>> getList,
+ List<StatementSyntax> list)
where T : StatementSyntax
{
var typed = rawStatement as T;
@@ -256,7 +384,7 @@ private static IMember MakeProperty(PropertyDeclarationSyntax rawProperty)
return new RDomProperty(rawProperty, parms, getAccessor, setAccessor, publicAnnotations);
}
- private static IAccessor MakeAccessor(AccessorDeclarationSyntax rawAccessor)
+ private static IAccessor MakeAccessor(AccessorDeclarationSyntax rawAccessor)
{
if (rawAccessor == null) return null;
var statements = ListUtilities.MakeList(rawAccessor, x => GetStatements(rawAccessor.Body), x => MakeStatement(x));
@@ -72,7 +72,10 @@
<Compile Include="Implementations\RDomInterface.cs" />
<Compile Include="Implementations\RDomInvalidTypeMember.cs" />
<Compile Include="Implementations\RDomAccessor.cs" />
- <Compile Include="Implementations\RDomStatement.cs" />
+ <Compile Include="StatementImplementations\RDomAssignmentStatement.cs" />
+ <Compile Include="StatementImplementations\RDomReturnStatemen.cs" />
+ <Compile Include="StatementImplementations\RDomDeclarationStatement.cs" />
+ <Compile Include="StatementImplementations\RDomStatement.cs" />
<Compile Include="Implementations\RDomMethod.cs" />
<Compile Include="Implementations\RDomNamespace.cs" />
<Compile Include="Implementations\RDomParameter.cs" />
@@ -88,6 +91,9 @@
<Compile Include="RDomFactory.cs" />
<Compile Include="RoslynUtilities.cs" />
<Compile Include="RoslynDomUtilities.cs" />
+ <Compile Include="StatementImplementations\RDomBaseBlock.cs" />
+ <Compile Include="StatementImplementations\RDomIfStatement.cs" />
+ <Compile Include="StatementImplementations\RDomBlockStatement.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using RoslynDom.Common;
+
+namespace RoslynDom
+{
+ public class RDomAssignmentStatement : RDomStatement, IAssignmentStatement
+ {
+ private VariableDeclarationSyntax _variableSyntax;
+ private VariableDeclaratorSyntax _declaratorSyntax;
+
+
+ internal RDomAssignmentStatement(
+ ExpressionStatementSyntax rawExpression,
+ params PublicAnnotation[] publicAnnotations)
+ : base(rawExpression, StatementKind.Assignment, publicAnnotations)
+ {
+ Initialize();
+ }
+
+ internal RDomAssignmentStatement(RDomAssignmentStatement oldRDom)
+ : base(oldRDom)
+ { }
+
+ protected override void Initialize()
+ {
+ base.Initialize();
+ }
+
+ public override StatementSyntax BuildSyntax()
+ {
+ return null;
+ }
+
+ public string VarName { get; set; }
+ public IExpression Expression { get; set; }
+ }
+}
@@ -7,56 +7,42 @@
namespace RoslynDom
{
- public class RDomStatement : RDomBase<IStatement, StatementSyntax, ISymbol>, IStatement
+ public abstract class RDomBaseBlock : RDomStatement
{
private IList<IStatement> _statements = new List<IStatement>();
- internal RDomStatement(
+ internal RDomBaseBlock(
StatementSyntax rawItem,
IEnumerable<IStatement> statements,
+ StatementKind statementKind,
params PublicAnnotation[] publicAnnotations)
- : base(rawItem, publicAnnotations)
+ : base(rawItem, statementKind, publicAnnotations)
{
foreach (var statement in statements)
- { AddStatement(statement); }
- Initialize();
+ { AddOrMoveStatement(statement); }
}
- internal RDomStatement(RDomStatement oldRDom)
+ internal RDomBaseBlock(RDomBaseBlock oldRDom)
: base(oldRDom)
{
var newStatements = RoslynDomUtilities.CopyMembers(oldRDom._statements);
foreach (var statement in newStatements)
- { AddStatement(statement); }
-
- IsBlock = oldRDom.IsBlock;
- StatementKind = oldRDom.StatementKind;
- }
-
- protected override void Initialize()
- {
- base.Initialize();
- IsBlock = _statements.Count() > 1 || TypedSyntax is BlockSyntax;
+ { AddOrMoveStatement(statement); }
}
public override StatementSyntax BuildSyntax()
{
- return TypedSyntax;
+ return null;
}
-
+
public void RemoveStatement(IStatement statement)
{ _statements.Remove(statement); }
- public void AddStatement(IStatement statement)
+ public void AddOrMoveStatement(IStatement statement)
{ _statements.Add(statement); }
-
- public bool IsBlock { get; set; }
-
- public StatementKind StatementKind { get; set; }
-
public IEnumerable<IStatement> Statements
{ get { return _statements; } }
-
+
}
}
Oops, something went wrong.

0 comments on commit 58f147f

Please sign in to comment.