Skip to content

Commit

Permalink
Wrap Result in AstGenWrapper (#13)
Browse files Browse the repository at this point in the history
Wrapper contains the file name and AST root.
  • Loading branch information
DavidBakerEffendi authored Dec 5, 2023
1 parent 0815c50 commit 3aad4b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
17 changes: 17 additions & 0 deletions DotNetAstGen/AstGenWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DotNetAstGen;

public class AstGenWrapper
{
public AstGenWrapper(string fileName, SyntaxTree tree)
{
AstRoot = tree.GetCompilationUnitRoot();
FileName = fileName;
}

public CompilationUnitSyntax AstRoot { get; set; }
public string FileName { get; set; }
}
19 changes: 10 additions & 9 deletions DotNetAstGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,25 @@ private static void _AstForFile(FileSystemInfo rootInputPath, FileSystemInfo roo
var programText = streamReader.ReadToEnd();
var tree = CSharpSyntaxTree.ParseText(programText);
_logger?.LogDebug("Successfully parsed: {filePath}", fullPath);
var root = tree.GetCompilationUnitRoot();
var jsonString = JsonConvert.SerializeObject(root, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver =
new SyntaxNodePropertiesResolver() // Comment this to see the unfiltered parser output
});
var astGenResult = new AstGenWrapper(fullPath, tree);
var jsonString = JsonConvert.SerializeObject(astGenResult, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver =
new SyntaxNodePropertiesResolver() // Comment this to see the unfiltered parser output
});
var outputName = Path.Combine(filePath.DirectoryName ?? "./",
$"{Path.GetFileNameWithoutExtension(fullPath)}.json")
.Replace(rootInputPath.FullName, rootOutputPath.FullName);

// Create dirs if they do not exist
var outputParentDir = Path.GetDirectoryName(outputName);
if (outputParentDir != null)
{
Directory.CreateDirectory(outputParentDir);
}

File.WriteAllText(outputName, jsonString);
_logger?.LogInformation("Successfully wrote AST to '{astJsonPath}'", outputName);
}
Expand Down
4 changes: 2 additions & 2 deletions DotNetAstGen/SyntaxNodePropertiesResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace DotNetAstGen
internal class SyntaxNodePropertiesResolver : DefaultContractResolver
{
private static readonly ILogger? Logger = Program.LoggerFactory?.CreateLogger("SyntaxNodePropertiesResolver");

private readonly HashSet<string> _propsToAllow = new(new[]
{
"Value", "Usings", "Name", "Identifier", "Left", "Right", "Members", "ConstraintClauses",
"Alias", "NamespaceOrType", "Arguments", "Expression", "Declaration", "ElementType", "Initializer", "Else",
"Condition", "Statement", "Statements", "Variables", "WhenNotNull", "AllowsAnyExpression", "Expressions",
"Modifiers", "ReturnType", "IsUnboundGenericName", "Default", "IsConst", "Parameters", "Types",
"ExplicitInterfaceSpecifier", "MetaData", "Kind"
"ExplicitInterfaceSpecifier", "MetaData", "Kind", "AstRoot", "FileName"
});

private readonly List<string> _regexToAllow = new(new[]
Expand Down

0 comments on commit 3aad4b8

Please sign in to comment.