From 8721e94a0e4d87d1acc218d928f7121d495626f4 Mon Sep 17 00:00:00 2001 From: Semenov Dmitry Date: Mon, 15 Jan 2024 01:47:57 +0400 Subject: [PATCH] Removed converter from this repo. Bumped version --- .DS_Store | Bin 6148 -> 0 bytes .../ConversionTests.cs | 53 ---- .../GDShrapt.Converter.Tests.csproj | 21 -- .../CSharpGeneratingVisitor.cs | 286 ------------------ src/GDShrapt.Converter/ConversionHelper.cs | 109 ------- src/GDShrapt.Converter/ConversionSettings.cs | 9 - .../GDShrapt.Converter.csproj | 16 - src/GDShrapt.Converter/Program.cs | 96 ------ .../SystemExtensionMethods.cs | 8 - src/GDShrapt.Reader/GDShrapt.Reader.csproj | 6 +- src/GDShrapt.sln | 16 +- 11 files changed, 5 insertions(+), 615 deletions(-) delete mode 100644 .DS_Store delete mode 100644 src/GDShrapt.Converter.Tests/ConversionTests.cs delete mode 100644 src/GDShrapt.Converter.Tests/GDShrapt.Converter.Tests.csproj delete mode 100644 src/GDShrapt.Converter/CSharpGeneratingVisitor.cs delete mode 100644 src/GDShrapt.Converter/ConversionHelper.cs delete mode 100644 src/GDShrapt.Converter/ConversionSettings.cs delete mode 100644 src/GDShrapt.Converter/GDShrapt.Converter.csproj delete mode 100644 src/GDShrapt.Converter/Program.cs delete mode 100644 src/GDShrapt.Converter/SystemExtensionMethods.cs diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 9a874b5768f336915163bb88cd434575b859f936..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425ml0g0s}V-^m;4I%_5-~tF3k&vj^b9A16778<}(6eNJu~Vz<8=6`~ zboab&MFtUB!i}=AFfm2m$tVxGT*u4pe81nUlA49C} z?O@64YO)2RT{MRe%{!}2F))pG(Sih~)xkgosK7*lF7m<7{{#Hn{6A@7N(HFEpDCdI z{ - - - net5.0 - - false - - - - - - - - - - - - - - - diff --git a/src/GDShrapt.Converter/CSharpGeneratingVisitor.cs b/src/GDShrapt.Converter/CSharpGeneratingVisitor.cs deleted file mode 100644 index 28786ce..0000000 --- a/src/GDShrapt.Converter/CSharpGeneratingVisitor.cs +++ /dev/null @@ -1,286 +0,0 @@ -using GDShrapt.Reader; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -namespace GDShrapt.Converter -{ - public class CSharpGeneratingVisitor //: INodeVisitor - { - private readonly ConversionSettings _settings; - - private NamespaceDeclarationSyntax _generatedNamespace; - - private int _classCounter = 0; - - private Stack _completionStack = new Stack(); - - - public CSharpGeneratingVisitor(ConversionSettings settings) - { - _settings = settings; - } - - public string BuildCSharpNormalisedCode() - { - while (_completionStack.Count > 0) - LeftNode(); - - if (_generatedNamespace == null) - return null; - - // Normalize and get code as string. - var code = _generatedNamespace - .NormalizeWhitespace() - .ToFullString(); - - return code; - } - - public void Visit(GDClassDeclaration d) - { - // Generate new namespace - var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(string.IsNullOrWhiteSpace(_settings.Namespace) ? "Generated" : _settings.Namespace ?? "Generated")).NormalizeWhitespace(); - - @namespace = @namespace.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System"))); - @namespace = @namespace.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System.Linq"))); - @namespace = @namespace.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("Godot"))); - - Push(@namespace, nameSpace => _generatedNamespace = (NamespaceDeclarationSyntax)nameSpace); - - var classDeclaration = GenerateClassShell(d.ClassName?.Identifier?.Sequence, d.Extends?.Type?.Sequence, d.IsTool); - - Push(classDeclaration, (NamespaceDeclarationSyntax nameSpace, ClassDeclarationSyntax @class ) => nameSpace.AddMembers(@class)); - } - - public void Visit(GDInnerClassDeclaration d) - { - //var classDeclaration = GenerateClassShell(d.Name?.Sequence, d.ExtendsClass?.Sequence, d.IsTool); - //Push(classDeclaration, (ClassDeclarationSyntax @parentClass, ClassDeclarationSyntax @class) => @parentClass.AddMembers(@class)); - } - - private ClassDeclarationSyntax GenerateClassShell(string name, string extendsClassName, bool isTool) - { - // Get class name - var className = name ?? (_settings.FileName != null ? Path.GetFileNameWithoutExtension(_settings.FileName) : null) ?? $"GeneratedClass{_classCounter++}"; - - if (_settings.ConvertGDScriptNamingStyleToSharp) - className = ConversionHelper.ConvertDeclarationNameToSharpNamingStyle(className); - - // Generate class declaration - var classDeclaration = SyntaxFactory.ClassDeclaration(className); - - classDeclaration = classDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); - - // Check tool atribute - if (isTool) - { - classDeclaration = classDeclaration.AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList( - new[] { SyntaxFactory.Attribute(SyntaxFactory.ParseName("Tool")) - }))); - } - - // Check base class - if (!string.IsNullOrWhiteSpace(extendsClassName)) - { - if (_settings.ConvertGDScriptNamingStyleToSharp) - extendsClassName = ConversionHelper.ConvertDeclarationNameToSharpNamingStyle(extendsClassName); - - classDeclaration = classDeclaration.AddBaseListTypes( - SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName(extendsClassName))); - } - - return classDeclaration; - } - - public void Visit(GDParameterDeclaration d) - { - - } - - public void Visit(GDVariableDeclaration d) - { - var field = - SyntaxFactory.FieldDeclaration( - SyntaxFactory.VariableDeclaration( - SyntaxFactory.ParseTypeName(ConversionHelper.ExtractType(_settings, d)), - SyntaxFactory.SeparatedList(new[] { SyntaxFactory.VariableDeclarator(ConversionHelper.GetName(_settings, d.Identifier)) }) - )) - .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); - - Push(field, (ClassDeclarationSyntax @class, FieldDeclarationSyntax field) => @class.AddMembers(field)); - - // Create an auto-property - /*var property = - SyntaxFactory.PropertyDeclaration( - SyntaxFactory.ParseTypeName(ConversionHelper.ExtractType(_settings, d)), - ConversionHelper.GetName(_settings, d.Identifier) - ) - .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) - .AddAccessorListAccessors( - SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)), - SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)) - ); - */ - - //_classDeclaration = _classDeclaration.AddMembers(property); - // _classDeclaration = _classDeclaration.AddMembers(field); - } - - public void Visit(GDMethodDeclaration d) - { - - } - - ////////////////////////////////////////////////////////////////////////////////////////////// - - public void Visit(GDToolAttribute a) - { - - } - - public void Visit(GDClassNameAttribute a) - { - - } - - public void Visit(GDExtendsAttribute a) - { - - } - - public void Visit(GDExpressionStatement s) - { - - } - - ////////////////////////////////////////////////////////////////////////////////////////////// - - public void Visit(GDIfStatement s) - { - - } - public void Visit(GDForStatement s) - { - - } - - public void Visit(GDMatchStatement s) - { - - } - - public void Visit(GDVariableDeclarationStatement s) - { - - } - - public void Visit(GDWhileStatement s) - { - - } - - public void Visit(GDYieldExpression s) - { - - } - - public void Visit(GDArrayInitializerExpression e) - { - - } - - public void Visit(GDBracketExpression e) - { - - } - - ///////////////////////////////////////////////////////////////////////////////////////////////// - public void Visit(GDCallExpression e) - { - - } - - public void Visit(GDDualOperatorExpression e) - { - - } - - public void Visit(GDIdentifierExpression e) - { - - } - - public void Visit(GDIndexerExpression e) - { - - } - - public void Visit(GDMemberOperatorExpression e) - { - - } - - public void Visit(GDNumberExpression e) - { - - } - - public void Visit(GDSingleOperatorExpression e) - { - - } - - public void Visit(GDStringExpression e) - { - - } - - public void Visit(GDReturnExpression e) - { - - } - - public void Visit(GDPassExpression e) - { - - } - - ///////////////////////////////////////////////////////////////////////////////////////////////// - - class StackNode - { - public SyntaxNode Node; - public Action Completion; - } - - void Update(Func update) - where T : SyntaxNode - { - var node = _completionStack.Where(x => x.Node is T).First(); - node.Node = update((T)node.Node); - } - - void Push(SyntaxNode node, Action completion) => _completionStack.Push(new StackNode() - { - Node = node, - Completion = completion - }); - - void Push(B node, Func update) where T : SyntaxNode where B : SyntaxNode => _completionStack.Push(new StackNode() - { - Node = node, - Completion = n => Update(x => update(x, (B)n)) - }); - - public void LeftNode() - { - var node = _completionStack.Pop(); - node.Completion(node.Node); - } - } -} \ No newline at end of file diff --git a/src/GDShrapt.Converter/ConversionHelper.cs b/src/GDShrapt.Converter/ConversionHelper.cs deleted file mode 100644 index 34a9998..0000000 --- a/src/GDShrapt.Converter/ConversionHelper.cs +++ /dev/null @@ -1,109 +0,0 @@ -using GDShrapt.Reader; -using Microsoft.CodeAnalysis; -using System.Globalization; -using System.Text; - -namespace GDShrapt.Converter -{ - public static class ConversionHelper - { - public static string ConvertDeclarationNameToSharpNamingStyle(string name) - { - if (name == null) - return null; - - var builder = new StringBuilder(); - - var needTitleCase = true; - - for (int i = 0; i < name.Length; i++) - { - if (name[i] == '_') - { - needTitleCase = true; - continue; - } - - if (needTitleCase) - builder.Append(CultureInfo.InvariantCulture.TextInfo.ToUpper(name[i])); - else - builder.Append(name[i]); - } - - return builder.ToString(); - } - - public static string ConvertVariableNameToSharpNamingStyle(string name) - { - if (name == null) - return null; - - var builder = new StringBuilder(); - - var needTitleCase = false; - - for (int i = 0; i < name.Length; i++) - { - if (name[i] == '_') - { - needTitleCase = true; - continue; - } - - if (needTitleCase) - builder.Append(CultureInfo.InvariantCulture.TextInfo.ToUpper(name[i])); - else - builder.Append(name[i]); - } - - return builder.ToString(); - } - - public static string ExtractType(ConversionSettings settings, GDVariableDeclaration d) - { - var typeName = d.Type.Sequence; - - if (string.IsNullOrWhiteSpace(typeName)) - { - if (d.Type.ExtractTypeFromInitializer) - { - typeName = GetTypeFromExpression(d.Initializer); - - if (settings.ConvertGDScriptNamingStyleToSharp) - typeName = ConvertDeclarationNameToSharpNamingStyle(typeName); - - return typeName; - } - else - { - return "dynamic"; - } - - } - else - { - if (settings.ConvertGDScriptNamingStyleToSharp) - typeName = ConvertDeclarationNameToSharpNamingStyle(typeName); - - return typeName; - } - } - - private static string GetTypeFromExpression(GDExpression e) - { - return "dynamic"; - // var - // e. - } - - public static string GetName(ConversionSettings settings, GDIdentifier identifier) - { - var name = identifier.Sequence; - - if (settings.ConvertGDScriptNamingStyleToSharp) - name = ConvertVariableNameToSharpNamingStyle(name); - - return name; - } - } -} \ No newline at end of file diff --git a/src/GDShrapt.Converter/ConversionSettings.cs b/src/GDShrapt.Converter/ConversionSettings.cs deleted file mode 100644 index b440d93..0000000 --- a/src/GDShrapt.Converter/ConversionSettings.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace GDShrapt.Converter -{ - public class ConversionSettings - { - public string Namespace { get; set; } - public string FileName { get; set; } - public bool ConvertGDScriptNamingStyleToSharp { get; set; } - } -} \ No newline at end of file diff --git a/src/GDShrapt.Converter/GDShrapt.Converter.csproj b/src/GDShrapt.Converter/GDShrapt.Converter.csproj deleted file mode 100644 index a10673b..0000000 --- a/src/GDShrapt.Converter/GDShrapt.Converter.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - Exe - net5.0 - - - - - - - - - - - diff --git a/src/GDShrapt.Converter/Program.cs b/src/GDShrapt.Converter/Program.cs deleted file mode 100644 index 370b3b9..0000000 --- a/src/GDShrapt.Converter/Program.cs +++ /dev/null @@ -1,96 +0,0 @@ -using GDShrapt.Reader; -using System; -using System.IO; -using System.Linq; - -namespace GDShrapt.Converter -{ - class Program - { - static void Main(string[] args) - { - RETURN: - if (args == null || args.Length < 1) - { - Console.WriteLine("GDScriptConverter parses and converts all godot scripts to their C# eqvivalent."); - Console.WriteLine("All code style will be rewritten to C# standarts (if possible)."); - Console.WriteLine(""); - Console.WriteLine("Usage:"); - Console.WriteLine("GDScriptConverter [empty destination directory]"); - return; - } - - var from = args[0]; - var to = args.ElementAtOrDefault(1) ?? string.Empty; - - if (from.IsNullOrEmpty() || to.IsNullOrEmpty() || !Directory.Exists(from)) - goto RETURN; - - var parser = new GDScriptReader(); - - if (File.Exists(from)) - { - ParseFile(parser, from, Path.Combine(to, Path.GetFileName(from))); - } - else - { - ProcessDirectory(from, to, (path, destination) => ParseFile(parser, path, destination)); - } - } - - static void ParseFile(GDScriptReader parser, string filePath, string destinationPath) - { - var extension = Path.GetExtension(filePath); - - if (extension.EndsWith(".gd", StringComparison.OrdinalIgnoreCase)) - { - Console.WriteLine($"Parsing script file: '{filePath}'"); - var declaration = parser.ParseFile(filePath); - - // Create GDScript tree walker - var visitor = new CSharpGeneratingVisitor(new ConversionSettings() - { - Namespace = "Generated", - FileName = filePath, - ConvertGDScriptNamingStyleToSharp = true - }); - - //var treeWalker = new GDTreeWalker(visitor); - //treeWalker.WalkInNode(declaration); - - // Generate C# code and save it in a file - var newPath = Path.ChangeExtension(destinationPath, ".cs"); - File.WriteAllText(newPath, visitor.BuildCSharpNormalisedCode()); - } - else - { - Console.WriteLine($"Simple copy file: '{filePath}'"); - File.Copy(filePath, destinationPath, true); - } - } - - static void ProcessDirectory(string path1, string path2, Action fileHandler) - { - Directory.CreateDirectory(path2); - Console.WriteLine($"Processing directory: '{path2}'"); - - DirectoryInfo dir = new DirectoryInfo(path1); - - if (!dir.Exists) - return; - - foreach (var subdir in dir.EnumerateDirectories()) - { - var tempPath = Path.Combine(path2, subdir.Name); - ProcessDirectory(subdir.FullName, tempPath, fileHandler); - } - - foreach (var file in dir.EnumerateFiles()) - { - var tempPath = Path.Combine(path2, file.Name); - fileHandler(file.FullName, tempPath); - } - } - } -} - diff --git a/src/GDShrapt.Converter/SystemExtensionMethods.cs b/src/GDShrapt.Converter/SystemExtensionMethods.cs deleted file mode 100644 index 41449fe..0000000 --- a/src/GDShrapt.Converter/SystemExtensionMethods.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace System -{ - internal static class SystemExtensionMethods - { - public static bool IsNullOrEmpty(this string self) => string.IsNullOrEmpty(self); - public static bool IsNullOrWhiteSpace(this string self) => string.IsNullOrWhiteSpace(self); - } -} diff --git a/src/GDShrapt.Reader/GDShrapt.Reader.csproj b/src/GDShrapt.Reader/GDShrapt.Reader.csproj index 3a49610..585b69d 100644 --- a/src/GDShrapt.Reader/GDShrapt.Reader.csproj +++ b/src/GDShrapt.Reader/GDShrapt.Reader.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - 4.1.0-alpha + 4.1.1-alpha elamaunt GDShrapt GDShrapt.Reader is .Net library and object-oriented one-pass parser of GDScript. It can build a lexical tree of GDScript code or generate a new code from scratch. @@ -16,8 +16,8 @@ Usage: Just create a GDScriptReader instance and call methods from it.https://github.com/elamaunt/GDShrapt git GDShrapt GDScript reader parser codegeneration Godot lexical analyzer - 4.1.0 - 4.1.0 + 4.1.1 + 4.1.1 true diff --git a/src/GDShrapt.sln b/src/GDShrapt.sln index 662191e..20843ef 100644 --- a/src/GDShrapt.sln +++ b/src/GDShrapt.sln @@ -1,26 +1,18 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30907.101 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34309.116 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GDShrapt.Converter", "GDShrapt.Converter\GDShrapt.Converter.csproj", "{0154101B-4D28-4763-A65A-D65124A2C07F}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GDShrapt.Reader.Tests", "GDShrapt.Reader.Tests\GDShrapt.Reader.Tests.csproj", "{2A9E4095-E028-474B-A4A6-9E928AE9ADFE}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GDShrapt.Reader", "GDShrapt.Reader\GDShrapt.Reader.csproj", "{EF8F42CC-A17C-4F08-800D-04BC5EAB59F9}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GDShrapt.Converter.Tests", "GDShrapt.Converter.Tests\GDShrapt.Converter.Tests.csproj", "{C4475DC1-EE4E-4808-864A-7923DF24AE36}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {0154101B-4D28-4763-A65A-D65124A2C07F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0154101B-4D28-4763-A65A-D65124A2C07F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0154101B-4D28-4763-A65A-D65124A2C07F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0154101B-4D28-4763-A65A-D65124A2C07F}.Release|Any CPU.Build.0 = Release|Any CPU {2A9E4095-E028-474B-A4A6-9E928AE9ADFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2A9E4095-E028-474B-A4A6-9E928AE9ADFE}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A9E4095-E028-474B-A4A6-9E928AE9ADFE}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -29,10 +21,6 @@ Global {EF8F42CC-A17C-4F08-800D-04BC5EAB59F9}.Debug|Any CPU.Build.0 = Debug|Any CPU {EF8F42CC-A17C-4F08-800D-04BC5EAB59F9}.Release|Any CPU.ActiveCfg = Release|Any CPU {EF8F42CC-A17C-4F08-800D-04BC5EAB59F9}.Release|Any CPU.Build.0 = Release|Any CPU - {C4475DC1-EE4E-4808-864A-7923DF24AE36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C4475DC1-EE4E-4808-864A-7923DF24AE36}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C4475DC1-EE4E-4808-864A-7923DF24AE36}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C4475DC1-EE4E-4808-864A-7923DF24AE36}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE