Skip to content

Commit

Permalink
Kind & Location Information (#9)
Browse files Browse the repository at this point in the history
* Made sure artifact is correctly versioned and this can be viewed by `--version`
* Extended property resolver to add meta data class containing type and location info on serialization
* Removed unused code
  • Loading branch information
DavidBakerEffendi authored Nov 29, 2023
1 parent 247e996 commit d718e88
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 237 deletions.
25 changes: 13 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build
- name: Get next release version (dry run)
id: taggerDryRun
uses: anothrNick/[email protected]
Expand All @@ -32,18 +28,23 @@ jobs:
DRY_RUN: true
- name: echo new tag
run: |
echo "The next tag version will be: ${{ steps.taggerDryRun.outputs.new_tag }}"
export NEXT_VERSION=${{ steps.taggerDryRun.outputs.new_tag }}
echo "The next tag version will be: $NEXT_VERSION"
- name: echo tag
run: |
echo "The current tag is: ${{ steps.taggerDryRun.outputs.tag }}"
echo "The current tag is: ${{ steps.taggerDryRun.outputs.tag }}"
- name: Install dependencies
run: dotnet restore
- name: Compile
run: ./build-with-version $NEXT_VERSION
- name: Build
run: |
dotnet publish -c Release -r linux-x64 -o ./release/linux DotNetAstGen/DotNetAstGen.csproj
dotnet publish -c Release -r linux-arm -o ./release/linux-arm DotNetAstGen/DotNetAstGen.csproj
dotnet publish -c Release -r osx-x64 -o ./release/macos DotNetAstGen/DotNetAstGen.csproj
dotnet publish -c Release -r osx-arm64 -o ./release/macos-arm DotNetAstGen/DotNetAstGen.csproj
dotnet publish -c Release -r win-x64 -o ./release/win DotNetAstGen/DotNetAstGen.csproj
dotnet publish -c Release -r win-arm -o ./release/win-arm DotNetAstGen/DotNetAstGen.csproj
./publish-release.sh linux x64
./publish-release.sh linux arm
./publish-release.sh osx x64
./publish-release.sh osx arm64
./publish-release.sh win x64
./publish-release.sh win arm
- name: Rename
run: |
mv ./release/linux/DotNetAstGen dotnetastgen-linux
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,5 @@ $RECYCLE.BIN/
*.lnk

.idea
**/*.json
**/*.json
.ast
2 changes: 2 additions & 0 deletions DotNetAstGen/DotNetAstGen.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version Condition=" '$(NEXT_VERSION)' == '' ">0.0.1-local</Version>
<Version Condition=" '$(NEXT_VERSION)' != '' ">$(NEXT_VERSION)</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
9 changes: 4 additions & 5 deletions DotNetAstGen/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics;
using System.Text;
using CommandLine;
using DotNetAstGen.Utils;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Expand All @@ -25,7 +24,7 @@ public static void Main(string[] args)
.AddConsole()
.AddDebug();
if (options.Verbose)
if (options.Debug)
{
builder.SetMinimumLevel(LogLevel.Debug);
}
Expand Down Expand Up @@ -85,7 +84,7 @@ private static void _AstForFile(FileSystemInfo rootInputPath, FileSystemInfo roo
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver =
new IgnorePropertiesResolver() // Comment this to see the unfiltered parser output
new SyntaxNodePropertiesResolver() // Comment this to see the unfiltered parser output
});
var outputName = Path.Combine(filePath.DirectoryName ?? "./",
$"{Path.GetFileNameWithoutExtension(fullPath)}.json")
Expand All @@ -104,8 +103,8 @@ private static void _AstForFile(FileSystemInfo rootInputPath, FileSystemInfo roo

internal class Options
{
[Option('v', "verbose", Required = false, HelpText = "Enable verbose output.")]
public bool Verbose { get; set; } = false;
[Option('d', "debug", Required = false, HelpText = "Enable verbose output.")]
public bool Debug { get; set; } = false;

[Option('i', "input", Required = true, HelpText = "Input file or directory.")]
public string InputFilePath { get; set; } = "";
Expand Down
76 changes: 76 additions & 0 deletions DotNetAstGen/SyntaxMetaDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Newtonsoft.Json.Serialization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Newtonsoft.Json;

namespace DotNetAstGen
{
public class SyntaxMetaDataProvider : IValueProvider
{
public object GetValue(object target)
{
return target.GetType().IsAssignableTo(typeof(SyntaxNode))
? GetNodeMetadata((SyntaxNode)target)
: new SyntaxMetaData();
}

private static SyntaxMetaData GetNodeMetadata(SyntaxNode node)
{
var span = node.SyntaxTree.GetLineSpan(node.Span);
return new SyntaxMetaData(
$"ast.{node.Kind()}",
span.StartLinePosition.Line,
span.EndLinePosition.Line,
span.StartLinePosition.Character,
span.EndLinePosition.Character
);
}

public void SetValue(object target, object value)
{
// ignore
}

public static JsonProperty CreateMetaDataProperty()
{
return new JsonProperty
{
PropertyName = "MetaData",
PropertyType = typeof(SyntaxMetaData),
DeclaringType = typeof(SyntaxNode),
ValueProvider = new SyntaxMetaDataProvider(),
AttributeProvider = null,
Readable = true,
Writable = false,
ShouldSerialize = _ => true
};
}
}

public class SyntaxMetaData
{
public SyntaxMetaData()
{
}

public SyntaxMetaData(string kind, int lineStart, int lineEnd, int columnStart, int columnEnd)
{
Kind = kind;
LineStart = lineStart;
LineEnd = lineEnd;
ColumnStart = columnStart;
ColumnEnd = columnEnd;
}

public string Kind { get; set; } = "ast.None";
public int LineStart { get; set; } = -1;
public int LineEnd { get; set; } = -1;
public int ColumnStart { get; set; } = -1;
public int ColumnEnd { get; set; } = -1;

public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace DotNetAstGen.Utils
namespace DotNetAstGen
{
internal class IgnorePropertiesResolver : DefaultContractResolver
internal class SyntaxNodePropertiesResolver : DefaultContractResolver
{
private static readonly ILogger? Logger = Program.LoggerFactory?.CreateLogger("IgnorePropertiesResolver");


private static readonly ILogger? Logger = Program.LoggerFactory?.CreateLogger("SyntaxNodePropertiesResolver");

private readonly HashSet<string> _propsToAllow = new(new[]
{
"Value", "Externs", "Usings", "Name", "Identifier", "Left", "Right", "Members", "ConstraintClauses",
"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", "Text", "Length", "Location"
"ExplicitInterfaceSpecifier", "MetaData", "Kind"
});

private readonly List<string> _regexToAllow = new(new[]
{
".*Token$", ".*Keyword$", ".*Lists?$", ".*Body$", "(Span)?Start"
".*Token$", ".*Keyword$", ".*Lists?$", ".*Body$", "(Line|Column)(Start|End)"
});

private readonly List<string> _regexToIgnore = new(new[]
{
".*(Semicolon|Brace|Bracket|EndOfFile|Paren|Dot)Token$"
".*(Semicolon|Brace|Bracket|EndOfFile|Paren|Dot)Token$", "AttributeLists",
"(Unsafe|Global|Static|Using)Keyword"
});

private bool MatchesAllow(string input)
Expand All @@ -40,6 +41,15 @@ private bool MatchesIgnore(string input)
return _regexToIgnore.Any(regex => Regex.IsMatch(input, regex));
}

protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
var isSyntaxNode = type.IsAssignableTo(typeof(SyntaxNode));
if (!isSyntaxNode) return properties;
properties.Add(SyntaxMetaDataProvider.CreateMetaDataProperty());
return properties;
}

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
Expand Down
111 changes: 0 additions & 111 deletions DotNetAstGen/Utils/MapSerializer.cs

This file was deleted.

Loading

0 comments on commit d718e88

Please sign in to comment.