Skip to content

Commit

Permalink
feat(tree-visualizer): Add Automatic Assembly And Parser Recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 12, 2024
1 parent d6bb5de commit 9f781e2
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 63 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/pack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.x'

- uses: actions/cache@v4
name: Setup Cache
id: cache-setup
with:
path: |
~/.nuget/packages
~/.dotnet
key: ${{ runner.os }}-${{ github.ref_name }}
restore-keys: |
${{ runner.os }}-${{ github.ref_name }}

- name: Install dependencies
run: dotnet restore Source/Silverfly/Silverfly.csproj
Expand All @@ -65,6 +54,7 @@ jobs:
dotnet build -c Release -o ./nuget Source/Silverfly.Testing/Silverfly.Testing.csproj
dotnet build -c Release -o ./nuget Source/Silverfly.Repl/Silverfly.Repl.csproj
dotnet build -c Release -o ./nuget Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj
dotnet build -c Release -o ./nuget Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj
dotnet build -o ./nuget Source/Silverfly.Generator/Silverfly.Generator.csproj
- name: Publish to NuGet
Expand Down
30 changes: 20 additions & 10 deletions Source/Samples/Sample.FuncLanguage/EvaluationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using Silverfly.Generator;
using Silverfly.Nodes;
using Silverfly.Nodes.Operators;
using Silverfly.Text;
Expand All @@ -9,9 +8,27 @@

namespace Silverfly.Sample.Func;

[Visitor]
public partial class EvaluationVisitor : TaggedNodeVisitor<Value, Scope>
public class EvaluationVisitor : TaggedNodeVisitor<Value, Scope>
{
public EvaluationVisitor()
{
For<TupleBindingNode>(Visit);
For<EnumNode>(Visit);
For<ImportNode>(Visit);
For<BinaryOperatorNode>(Visit);
For<PrefixOperatorNode>(Visit);
For<GroupNode>(Visit);
For<ModuleNode>(Visit);
For<BlockNode>(Visit);
For<VariableBindingNode>(Visit);
For<IfNode>(Visit);
For<LambdaNode>(Visit);
For<NameNode>(Visit);
For<CallNode>(Visit);
For<LiteralNode>(Visit);
For<TupleNode>(Visit);
}

Value Visit(TupleBindingNode node, Scope scope)
{
var value = Visit(node.Value, scope);
Expand Down Expand Up @@ -167,7 +184,6 @@ Value Visit(BlockNode block, Scope scope)

//@enter(on_enter)
//@leave(on_leave)
[VisitorIgnore]
private void CallAnnotationRef(string annotationName, AnnotatedNode node, Scope scope, params Value[] args)
{
foreach (var annotation in node.Annotations)
Expand Down Expand Up @@ -248,7 +264,6 @@ Value Visit(CallNode call, Scope scope)
};
}

[VisitorIgnore]
void AddAnnotations(AstNode node, Value value, Scope scope)
{
if (node is AnnotatedNode an)
Expand All @@ -266,7 +281,6 @@ void AddAnnotations(AstNode node, Value value, Scope scope)
}
}

[VisitorIgnore]
Value VisitOtherFunction(CallNode call, Scope scope)
{
var args = call.Arguments.Select(_ => Visit(_, scope)).ToArray();
Expand All @@ -280,7 +294,6 @@ Value VisitOtherFunction(CallNode call, Scope scope)
return UnitValue.Shared;
}

[VisitorIgnore]
private Value VisitLambdaFunction(LambdaNode funcGroup, CallNode call, Scope scope)
{
var args = call.Arguments.Select(_ => Visit(_, scope)).ToArray();
Expand All @@ -294,7 +307,6 @@ private Value VisitLambdaFunction(LambdaNode funcGroup, CallNode call, Scope sco
return UnitValue.Shared;
}

[VisitorIgnore]
private Value VisitNamedFunction(NameNode func, CallNode call, Scope scope)
{
var args = call.Arguments.Select(arg => Visit(arg, scope)).ToArray();
Expand All @@ -310,7 +322,6 @@ private Value VisitNamedFunction(NameNode func, CallNode call, Scope scope)
return UnitValue.Shared;
}

[VisitorIgnore]
protected override Value VisitUnknown(AstNode node, Scope tag)
{
if (node is InvalidNode invalid)
Expand All @@ -321,7 +332,6 @@ protected override Value VisitUnknown(AstNode node, Scope tag)
return UnitValue.Shared;
}

[VisitorIgnore]
private Value CallFunction(ImmutableList<NameNode> parameters, Value[] args, AstNode definition)
{
var subScope = Scope.Root.NewSubScope();
Expand Down
19 changes: 0 additions & 19 deletions Source/Samples/Sample.FuncLanguage/Parselets/GeneratedParselet.cs

This file was deleted.

19 changes: 16 additions & 3 deletions Source/Samples/Sample.FuncLanguage/RewriterVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
using System.Collections.Immutable;
using Silverfly.Generator;
using Silverfly.Nodes;
using Silverfly.Nodes.Operators;
using Silverfly.Sample.Func.Nodes;

namespace Silverfly.Sample.Func;

[Visitor]
public partial class RewriterVisitor : Rewriter
public class RewriterVisitor : Rewriter
{
public RewriterVisitor()
{
For<TupleBindingNode>(Rewrite);
For<BinaryOperatorNode>(Rewrite);
For<PrefixOperatorNode>(Rewrite);
For<GroupNode>(Rewrite);
For<BlockNode>(Rewrite);
For<VariableBindingNode>(Rewrite);
For<LambdaNode>(Rewrite);
For<NameNode>(Rewrite);
For<CallNode>(Rewrite);
For<LiteralNode>(Rewrite);
For<TupleNode>(Rewrite);
}

protected override AstNode VisitUnknown(AstNode node) => node;

private AstNode Rewrite(TupleBindingNode node)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
2 changes: 1 addition & 1 deletion Source/Samples/Sample.JSON/Sample.JSON.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
2 changes: 1 addition & 1 deletion Source/Samples/Sample.Rockstar/Sample.Rockstar.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
1 change: 0 additions & 1 deletion Source/Silverfly.Generator/ParseletSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDecla

var attributeName = attributeSymbol.ContainingType.ToDisplayString();

// Check the full name of the [Report] attribute.
if (attributeName == $"{Namespace}.{ParseletAttributeName}")
return (classDeclarationSyntax, true);
}
Expand Down
3 changes: 1 addition & 2 deletions Source/Silverfly.Generator/Silverfly.Generator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -30,7 +30,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Source/Silverfly.Repl/Silverfly.Repl.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Silverfly.Repl</Title>
Expand Down
2 changes: 1 addition & 1 deletion Source/Silverfly.Testing/Silverfly.Testing.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Silverfly.Testing</Title>
Expand Down
3 changes: 1 addition & 2 deletions Source/Silverfly.TreeVisualizer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Cli;

namespace Silverfly.TreeVisualizer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackAsTool>true</PackAsTool>
<ToolCommandName>silver-tree</ToolCommandName>

<Version>1.0.70</Version>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Silverfly.TreeVisualizer</Title>
Expand Down
59 changes: 51 additions & 8 deletions Source/Silverfly.TreeVisualizer/TreeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ internal sealed class TreeCommand : Command<TreeCommand.Settings>
{
public sealed class Settings : CommandSettings
{
[Description("Path to the assembly containing the parsers")]
[CommandArgument(0, "<assembly>")]
public string? AssemblyPath { get; set; }

[CommandOption("-p|--parser")]
[Description("Specify the parser to use")]
public string? Parser { get; set; }

[Description("The source to parse")]
[CommandOption("-s|--source")]
[CommandArgument(0, "<source>")]
public string Source { get; set; }
}

public override int Execute(CommandContext context, Settings settings)
{
var parserAssembly = Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, settings.AssemblyPath));
var parserType = parserAssembly.GetType(settings.Parser);
var parserInstance = (Parser)Activator.CreateInstance(parserType);
var parserInstance = FindParser(settings);

var parsed = parserInstance.Parse(settings.Source);

Expand All @@ -43,6 +37,55 @@ public override int Execute(CommandContext context, Settings settings)
return 0;
}

private Parser FindParser(Settings settings)
{
foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*.dll"))
{
try
{
var assembly = Assembly.LoadFrom(file);

if (assembly == typeof(Parser).Assembly || assembly.FullName!.StartsWith("Microsoft"))
{
continue;
}

var types = assembly.GetTypes();

if (settings.Parser == null)
{
foreach (var type in types)
{
if (type.IsSubclassOf(typeof(Parser)))
{
return (Parser)Activator.CreateInstance(type)!;
}
}

continue;
}

var parserType = assembly.GetTypes().ToList()
.FirstOrDefault(t => t.IsSubclassOf(typeof(Parser)) && t.FullName == settings.Parser);

if (parserType == null)
{
continue;
}

return (Parser)Activator.CreateInstance(parserType)!;
}
catch
{
}
}

AnsiConsole.WriteLine("Could not find parser");
Environment.Exit(1);

return null;
}

private string[] ignorePropNames = ["Tag", "Range", "Parent"];

private void BuildTree(IHasTreeNodes node, AstNode parsedTree)
Expand Down
2 changes: 1 addition & 1 deletion Source/Silverfly/Silverfly.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.71</Version>
<Version>1.0.72</Version>
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>Silverfly</Title>
Expand Down

0 comments on commit 9f781e2

Please sign in to comment.