diff --git a/.github/workflows/pack.yaml b/.github/workflows/pack.yaml index 0d2b372..5334dac 100644 --- a/.github/workflows/pack.yaml +++ b/.github/workflows/pack.yaml @@ -57,12 +57,14 @@ jobs: sed -i "s/.*<\/Version>/${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Testing/Silverfly.Testing.csproj sed -i "s/.*<\/Version>/${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Generator/Silverfly.Generator.csproj sed -i "s/.*<\/Version>/${RELEASE_VERSION}<\/Version>/" Source/Silverfly.Repl/Silverfly.Repl.csproj + sed -i "s/.*<\/Version>/${RELEASE_VERSION}<\/Version>/" Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj - name: Build and package run: | dotnet build -c Release -o ./nuget Source/Silverfly/Silverfly.csproj 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 -o ./nuget Source/Silverfly.Generator/Silverfly.Generator.csproj - name: Publish to NuGet @@ -92,6 +94,6 @@ jobs: message: "Update version to ${{ env.RELEASE_VERSION }}" push: true new_branch: update-version - add: '["Source/Silverfly/Silverfly.csproj", "Source/Silverfly.Testing/Silverfly.Testing.csproj", "Source/Silverfly.Generator/Silverfly.Generator.csproj", "Source/Silverfly.Repl/Silverfly.Repl.csproj"]' + add: '["Source/Silverfly/Silverfly.csproj", "Source/Silverfly.Testing/Silverfly.Testing.csproj", "Source/Silverfly.Generator/Silverfly.Generator.csproj", "Source/Silverfly.Repl/Silverfly.Repl.csproj","Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj"]' diff --git a/Source/Benchmarks/Benchmarks.csproj b/Source/Benchmarks/Benchmarks.csproj index baab7fb..ef376bf 100644 --- a/Source/Benchmarks/Benchmarks.csproj +++ b/Source/Benchmarks/Benchmarks.csproj @@ -7,13 +7,13 @@ enable - - + + - - - + + + diff --git a/Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj b/Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj index 5cb7df5..81b3975 100644 --- a/Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj +++ b/Source/Samples/Sample.FuncLanguage/Sample.FuncLanguage.csproj @@ -1,7 +1,7 @@  - 1.0.70 + 1.0.71 Exe preview true diff --git a/Source/Samples/Sample.JSON/JsonGrammar.cs b/Source/Samples/Sample.JSON/JsonGrammar.cs index 57b9796..d99634d 100644 --- a/Source/Samples/Sample.JSON/JsonGrammar.cs +++ b/Source/Samples/Sample.JSON/JsonGrammar.cs @@ -1,8 +1,7 @@ using Sample.JSON.Nodes; using Sample.JSON.Parselets; -using Silverfly; -namespace Sample.JSON; +namespace Silverfly.Sample.JSON; public class JsonGrammar : Parser { diff --git a/Source/Samples/Sample.JSON/ReplInstance.cs b/Source/Samples/Sample.JSON/ReplInstance.cs index 9915aeb..c92de36 100644 --- a/Source/Samples/Sample.JSON/ReplInstance.cs +++ b/Source/Samples/Sample.JSON/ReplInstance.cs @@ -1,4 +1,5 @@ using Silverfly.Repl; +using Silverfly.Sample.JSON; namespace Sample.JSON; diff --git a/Source/Samples/Sample.JSON/Sample.JSON.csproj b/Source/Samples/Sample.JSON/Sample.JSON.csproj index 0d134e9..35ad25a 100644 --- a/Source/Samples/Sample.JSON/Sample.JSON.csproj +++ b/Source/Samples/Sample.JSON/Sample.JSON.csproj @@ -1,7 +1,7 @@  - 1.0.70 + 1.0.71 Exe net8.0 true diff --git a/Source/Samples/Sample.Rockstar/Sample.Rockstar.csproj b/Source/Samples/Sample.Rockstar/Sample.Rockstar.csproj index 1507a92..13f8002 100644 --- a/Source/Samples/Sample.Rockstar/Sample.Rockstar.csproj +++ b/Source/Samples/Sample.Rockstar/Sample.Rockstar.csproj @@ -1,7 +1,7 @@  - 1.0.70 + 1.0.71 Exe preview true diff --git a/Source/Silverfly.Generator/Silverfly.Generator.csproj b/Source/Silverfly.Generator/Silverfly.Generator.csproj index 82eada7..30a7539 100644 --- a/Source/Silverfly.Generator/Silverfly.Generator.csproj +++ b/Source/Silverfly.Generator/Silverfly.Generator.csproj @@ -1,7 +1,7 @@  - 1.0.70 + 1.0.71 net8.0 enable enable @@ -21,16 +21,16 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/Source/Silverfly.Repl/Silverfly.Repl.csproj b/Source/Silverfly.Repl/Silverfly.Repl.csproj index fc7c2f9..35575b2 100644 --- a/Source/Silverfly.Repl/Silverfly.Repl.csproj +++ b/Source/Silverfly.Repl/Silverfly.Repl.csproj @@ -1,7 +1,7 @@  - 1.0.70 + 1.0.71 preview true Silverfly.Repl diff --git a/Source/Silverfly.Testing/Silverfly.Testing.csproj b/Source/Silverfly.Testing/Silverfly.Testing.csproj index 562fd89..57a3c5f 100644 --- a/Source/Silverfly.Testing/Silverfly.Testing.csproj +++ b/Source/Silverfly.Testing/Silverfly.Testing.csproj @@ -1,7 +1,7 @@ - 1.0.70 + 1.0.71 preview true Silverfly.Testing @@ -9,15 +9,15 @@ https://github.com/furesoft/Silverfly A little framework to build parsers parser, pratt - net8.0 + net8.0 enable $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - - - + + + diff --git a/Source/Silverfly.TreeVisualizer/Program.cs b/Source/Silverfly.TreeVisualizer/Program.cs new file mode 100644 index 0000000..c2cb810 --- /dev/null +++ b/Source/Silverfly.TreeVisualizer/Program.cs @@ -0,0 +1,13 @@ +using Spectre.Console; +using Spectre.Console.Cli; + +namespace Silverfly.TreeVisualizer; + +public class Program +{ + public static int Main(string[] args) + { + var app = new CommandApp(); + return app.Run(args); + } +} diff --git a/Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj b/Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj new file mode 100644 index 0000000..1bff176 --- /dev/null +++ b/Source/Silverfly.TreeVisualizer/Silverfly.TreeVisualizer.csproj @@ -0,0 +1,30 @@ + + + + Exe + net8.0 + enable + enable + true + silver-tree + + 1.0.70 + preview + true + Silverfly.TreeVisualizer + furesoft + https://github.com/furesoft/Silverfly + A tool to view Silverfly trees + parser, pratt, silverfly, ast + + + + + + + + + + + + diff --git a/Source/Silverfly.TreeVisualizer/TreeCommand.cs b/Source/Silverfly.TreeVisualizer/TreeCommand.cs new file mode 100644 index 0000000..3cdec32 --- /dev/null +++ b/Source/Silverfly.TreeVisualizer/TreeCommand.cs @@ -0,0 +1,108 @@ +using System.Collections; +using System.ComponentModel; +using System.Reflection; +using Silverfly.Nodes; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace Silverfly.TreeVisualizer; + +internal sealed class TreeCommand : Command +{ + public sealed class Settings : CommandSettings + { + [Description("Path to the assembly containing the parsers")] + [CommandArgument(0, "")] + 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")] + 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 parsed = parserInstance.Parse(settings.Source); + + parserInstance.PrintMessages(); + + var root = new Tree(parsed.Tree.GetType().Name); + + BuildTree(root, parsed.Tree); + + AnsiConsole.Write(root); + + return 0; + } + + private string[] ignorePropNames = ["Tag", "Range", "Parent"]; + + private void BuildTree(IHasTreeNodes node, AstNode parsedTree) + { + if (parsedTree is BlockNode block) + { + foreach (var child in block.Children) + { + BuildTreeChild(node, child); + } + } + else + { + BuildTreeChild(node, parsedTree); + } + } + + private void BuildTreeChild(IHasTreeNodes node, object child) + { + var childType = child.GetType(); + var childNode = node.AddNode(childType.Name); + + foreach (var property in childType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) + { + if (ignorePropNames.Contains(property.Name)) + { + continue; + } + + var propValue = property.GetValue(child); + + if (propValue is LiteralNode literal) + { + childNode.AddNode($"{property.Name}={literal.Value}"); + continue; + } + if (propValue is NameNode nameNode) + { + childNode.AddNode($"{property.Name}={nameNode.Token}"); + continue; + } + + if (propValue is Token token) + { + childNode.AddNode($"{property.Name}={token}"); + continue; + } + else if (propValue is IEnumerable enumerable) + { + var itemNode = childNode.AddNode($"{property.Name}"); + foreach (var item in enumerable) + { + BuildTreeChild(itemNode, item); + } + } + + if (propValue is AstNode childBlock) + { + BuildTreeChild(childNode, childBlock); + } + } + } +} diff --git a/Source/Silverfly.sln b/Source/Silverfly.sln index addedbf..d6dbe13 100644 --- a/Source/Silverfly.sln +++ b/Source/Silverfly.sln @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Rockstar", "Samples\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Brainfuck", "Samples\Sample.Brainfuck\Sample.Brainfuck.csproj", "{C43A7724-4D0D-4600-9778-E479F8BD946B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Silverfly.TreeVisualizer", "Silverfly.TreeVisualizer\Silverfly.TreeVisualizer.csproj", "{A0D943E1-2579-4C51-84C6-375374FA0104}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -71,6 +73,10 @@ Global {C43A7724-4D0D-4600-9778-E479F8BD946B}.Debug|Any CPU.Build.0 = Debug|Any CPU {C43A7724-4D0D-4600-9778-E479F8BD946B}.Release|Any CPU.ActiveCfg = Release|Any CPU {C43A7724-4D0D-4600-9778-E479F8BD946B}.Release|Any CPU.Build.0 = Release|Any CPU + {A0D943E1-2579-4C51-84C6-375374FA0104}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0D943E1-2579-4C51-84C6-375374FA0104}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0D943E1-2579-4C51-84C6-375374FA0104}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0D943E1-2579-4C51-84C6-375374FA0104}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Source/Silverfly/Parselets/EnumParselet.cs b/Source/Silverfly/Parselets/EnumParselet.cs index 09a8763..336922e 100644 --- a/Source/Silverfly/Parselets/EnumParselet.cs +++ b/Source/Silverfly/Parselets/EnumParselet.cs @@ -3,6 +3,10 @@ namespace Silverfly.Parselets; +/// +/// Produces a +/// +/// public class EnumParselet : IPrefixParselet where TRegister : struct { diff --git a/Source/Silverfly/Silverfly.csproj b/Source/Silverfly/Silverfly.csproj index 10d6ecb..c26d81d 100644 --- a/Source/Silverfly/Silverfly.csproj +++ b/Source/Silverfly/Silverfly.csproj @@ -1,7 +1,7 @@ - 1.0.70 + 1.0.71 preview true Silverfly diff --git a/Source/TestProject/TestProject.csproj b/Source/TestProject/TestProject.csproj index abedc3c..a5a8c9d 100644 --- a/Source/TestProject/TestProject.csproj +++ b/Source/TestProject/TestProject.csproj @@ -6,9 +6,9 @@ - - - + + +