GDShrapt

所属分类:工具库
开发工具:C#
文件大小:0KB
下载次数:0
上传日期:2022-12-02 14:33:09
上 传 者sh-1993
说明:  GDShrapt是GDScript编程语言的面向对象单程解析器。,
(GDShrapt is object-oriented one-pass parser of GDScript programming language.,)

文件列表:
.DS_Store (6148, 2022-12-02)
LICENSE (1071, 2022-12-02)
src/ (0, 2022-12-02)
src/GDShrapt.Converter.Tests/ (0, 2022-12-02)
src/GDShrapt.Converter.Tests/ConversionTests.cs (1281, 2022-12-02)
src/GDShrapt.Converter.Tests/GDShrapt.Converter.Tests.csproj (673, 2022-12-02)
src/GDShrapt.Converter/ (0, 2022-12-02)
src/GDShrapt.Converter/CSharpGeneratingVisitor.cs (8640, 2022-12-02)
src/GDShrapt.Converter/ConversionHelper.cs (2984, 2022-12-02)
src/GDShrapt.Converter/ConversionSettings.cs (242, 2022-12-02)
src/GDShrapt.Converter/GDShrapt.Converter.csproj (381, 2022-12-02)
src/GDShrapt.Converter/Program.cs (3326, 2022-12-02)
src/GDShrapt.Converter/SystemExtensionMethods.cs (275, 2022-12-02)
src/GDShrapt.Reader.Tests/ (0, 2022-12-02)
src/GDShrapt.Reader.Tests/AssertHelper.cs (1519, 2022-12-02)
src/GDShrapt.Reader.Tests/BigScriptTests.cs (1477, 2022-12-02)
src/GDShrapt.Reader.Tests/BuildingTests.cs (5260, 2022-12-02)
src/GDShrapt.Reader.Tests/GDShrapt.Reader.Tests.csproj (1045, 2022-12-02)
src/GDShrapt.Reader.Tests/ParsingTests.cs (68901, 2022-12-02)
src/GDShrapt.Reader.Tests/Scripts/ (0, 2022-12-02)
src/GDShrapt.Reader.Tests/Scripts/Sample.gd (3743, 2022-12-02)
src/GDShrapt.Reader.Tests/Scripts/Sample2.gd (4746, 2022-12-02)
src/GDShrapt.Reader.Tests/Scripts/Sample3.gd (1501, 2022-12-02)
src/GDShrapt.Reader.Tests/SyntaxTests.cs (12678, 2022-12-02)
src/GDShrapt.Reader.Tests/TestingExtensionMethods.cs (313, 2022-12-02)
src/GDShrapt.Reader/ (0, 2022-12-02)
src/GDShrapt.Reader/Atributes/ (0, 2022-12-02)
src/GDShrapt.Reader/Atributes/GDClassNameAtribute.cs (4978, 2022-12-02)
src/GDShrapt.Reader/Atributes/GDExtendsAtribute.cs (3924, 2022-12-02)
src/GDShrapt.Reader/Atributes/GDToolAtribute.cs (2046, 2022-12-02)
src/GDShrapt.Reader/Basics/ (0, 2022-12-02)
src/GDShrapt.Reader/Basics/GDAssociationOrderType.cs (156, 2022-12-02)
src/GDShrapt.Reader/Basics/GDCharSequence.cs (2683, 2022-12-02)
src/GDShrapt.Reader/Basics/GDDataToken.cs (610, 2022-12-02)
... ...

# GDShrapt GDShrapt is object-oriented one-pass parser of GDScript. Now the main goal is production-ready parser, lexical analyzer. The project written in C# and free to use. GDScript is the main language of [Godot Engine](https://github.com/godotengine/godot) **The project is created on personal initiative and on enthusiasm.** ## GDShrapt.Reader GDShrapt.Reader allows to build a lexical tree or generate a new code from scratch. ### How to install Currently the latest **3.1.2-alpha version** from [Nuget](https://www.nuget.org/packages/GDShrapt.Reader). Installation from Nuget console: ``` Install-Package GDShrapt.Reader -Version 3.1.2-alpha ``` ## Capabilities, plan and what can be parsed | Capability | Is completed | | ------------- | ------------- | | One pass parsing | YES | | Lexical tree structure | YES | | Class declaration parsing | YES | | Methods declaration parsing | YES | | Enums parsing | YES | | Atributes declaration parsing | YES | | Variables declaration parsing | YES | | Export block parsing | YES | | Arrays parsing | YES | | Dictionary parsing | YES | | 'Match' statement parsing | YES | | 'For' statement parsing | YES | | 'While' statement parsing | YES | | 'If-Elif-Else' statement parsing | YES | | 'Yield' statement parsing | YES | | Ternar 'If' expression parsing | YES | | Signals parsing | YES | | Strings parsing | YES | | Numbers parsing | YES | | Methods calls parsing | YES | | Single operators parsing | YES | | Dual operators parsing | YES | | Static types parsing | YES | | Expressions priority sorting | YES | | Basic tokenization managment | YES | | Save formatting while parsing | YES | | Save comments while parsing | YES | | Moving from tokens to parent | YES | | NodePath and GetNode short syntax parsing | YES | | Inner class parsing | YES | | Tree walking and node visiting | YES | | Syntax cloning | YES | | Syntax factory | YES | | Asserts handling | IN PLAN | | RPC annotations parsing | IN PLAN | | Syntax errors managment and properly handling | IN PLAN | | Code formatting | IN PLAN | | Custom formatter | IN PLAN | | Tree diff tool | IN PLAN | ## Version history #### 3.1.2-alpha Small improvements of the declaration search in the node's parents tree. #### 3.1.1-alpha Fixed parsing of the dual expression splitting. Fixed some parsing errors. Added Global next and previous tokens search. #### 3.1.0-alpha Improved parsing NodePath. Fixed issues with 'elif' branches and some expressions sort order. Improved inner classes parsing. Improved node walking and visiting. Fixed small parsing errors. #### 3.0.0-alpha Many small fixes with the if-elif-else branches parsing. Improved tree managment. Fixed 'newline' character parsing in multiline expressions and initializers. Fixed Yield parsing. Most of internal methods are public now. 'Form' of every node is now accessible from users code. Implemented two styles of codegeneration. Implemented many additional properties for tokens and nodes (like a 'StartLine', 'Length' and so on). Implemented method for parsing unspecified content. #### 2.1.0-alpha Implemented 'clone' methods for every token type. Now you can clone the lexical tree with all it's structure. Changed the project type to Net.Standart 2.0 (Because this version is supported by Godot). Small fix with the 'ToString' method of GDIndexerExpression. #### 2.0.0-alpha The project is now in Net.Standart 2.1 and was totally reworked with tokenization layer. The parser now performs token extraction and lexical tree construction at the same time. No style data loss. Possibility to manage every token in code. Implemented specific node parsing like NodePath, short form of 'get_node'. Properly handling of comments and spaces. #### 1.0.0-prealpha .NET 5.0 version Implemented all basic nodes and a lexical tree building with a style data loss. Has limitations in specific situations. ## Reading samples ### Parse class GDScript input: ```gdscript tool class_name HTerrainDataSaver extends ResourceFormatSaver const HTerrainData = preload("./ hterrain_data.gd") func get_recognized_extensions(res): if res != null and res is HTerrainData: return PoolStringArray([HTerrainData.META_EXTENSION]) return PoolStringArray() func recognize(res): return res is HTerrainData func save(path, resource, flags): resource.save_data(path.get_base_dir()) ``` Parser usage: ```csharp // Initialize a reader instance var reader = new GDScriptReader(); // Parse the raw code var @class = reader.ParseFileContent(code); // returns instance of type GDClassDeclaration // Get 'extends' atribute information Console.WriteLine(@class.Extends.Type.Sequence); // outputs base class name "ResourceFormatSaver" // Get 'class_name' atribute information Console.WriteLine(@class.ClassName.Type.Sequence); // outputs current class name "HTerrainDataSaver" // Check 'tool' atribute Console.WriteLine(@class.IsTool); // outputs true // Enumerates all class variables foreach(GDVariableDeclaration variable in @class.Variables) { Console.WriteLine(method.Identifier.Sequence); // outputs variables's name } // Enumerates all class methods foreach(GDMethodDeclaration method in @class.Methods) { Console.WriteLine(method.Identifier.Sequence); // outputs method's name // Enumerates all method statements foreach(GDStatement st in method.Statements) { // ... your code } } ``` ### Get comments from GDScript code ```csharp var @class = reader.ParseFileContent(code); // Add 'using System.Linq;' var comments = @class.AllTokens .OfType() .Select(x => x.ToString()) // Convert token to string .ToArray(); ``` ## Tree building samples or GDScript runtime generation GDShrapt supports many styles to simplify a code generation process. Just use the GD static class to create a token or a node. ### Short style ```csharp // Build a custom class. Safe code generation. Dont control a code style var declaration = GD.Declaration.Class( GD.List.Atributes( GD.Atribute.Tool(), GD.Atribute.ClassName("Generated"), GD.Atribute.Extends("Node2D")), GD.Declaration.Const("my_constant", GD.Expression.String("Hello World")), GD.Declaration.OnreadyVariable("parameter", GD.Expression.True()), GD.Declaration.Method("_start", GD.Expression.Call(GD.Expression.Identifier("print"), GD.Expression.String("Hello world")) ) ); declaration.UpdateIntendation(); // Auto update tabs (recursively) var code = declaration.ToString(); // Get the string representation ``` The result is code like: ```gdscript tool class_name Generated extends Node2D const my_constant = "Hello World" onready var parameter = true func _start(): print("Hello world") ``` ### Methods chain style ```csharp // Build a custom class. Full tokens control, but unsafe for exceptions var declaration = GD.Declaration.Class() .AddAtributes(x => x .AddToolAtribute() .AddNewLine() .AddClassNameAtribute("Generated") .AddNewLine() .AddExtendsAtribute("Node2D")) .AddNewLine() .AddNewLine() .AddMembers(x => x .AddVariable("a") .AddNewLine() .AddConst("message", GD.Expression.String("Hello")) .AddNewLine() .AddNewLine() .AddMethod(x => x .AddFuncKeyword() .AddSpace() .Add("_start") .AddOpenBracket() .AddCloseBracket() .AddStatements(x => x .AddNewLine() .AddNewLine() .AddIntendation() .AddCall(GD.Expression.Identifier("print"), GD.Expression.String("Hello world")) .AddNewLine() .AddNewLine() .AddIntendation() .AddPass()))); declaration.UpdateIntendation(); // Auto update tabs (recursively) var code = declaration.ToString(); // Get the string representation ``` ### Tokens list style ```csharp // Build a custom class. Full tokens control but unsafe for types var declaration = GD.Declaration.Class( GD.List.Atributes( GD.Atribute.Tool(), GD.Syntax.NewLine, GD.Atribute.ClassName("Generated"), GD.Syntax.NewLine, GD.Atribute.Extends("Node2D")), GD.Syntax.NewLine, GD.Syntax.NewLine, GD.Declaration.Variable( GD.Keyword.Const, GD.Syntax.OneSpace, GD.Syntax.Identifier("my_constant"), GD.Syntax.OneSpace, GD.Syntax.Assign, GD.Syntax.OneSpace, GD.Syntax.String("Hello World")), GD.Syntax.NewLine, GD.Syntax.NewLine, GD.Declaration.Variable( GD.Keyword.Onready, GD.Syntax.OneSpace, GD.Keyword.Var, GD.Syntax.OneSpace, GD.Syntax.Identifier("parameter"), GD.Syntax.OneSpace, GD.Syntax.Assign, GD.Syntax.OneSpace, GD.Expression.True()), GD.Syntax.NewLine, GD.Syntax.NewLine, GD.Declaration.Method( GD.Keyword.Func, GD.Syntax.OneSpace, GD.Syntax.Identifier("_start"), GD.Syntax.OpenBracket, GD.Syntax.CloseBracket, GD.Syntax.Colon, GD.Syntax.NewLine, GD.Syntax.Intendation(1), GD.Expression.Call( GD.Expression.Identifier("print"), GD.Syntax.OpenBracket, GD.List.Expressions(GD.Expression.String("Hello world")), GD.Syntax.CloseBracket))); var code = declaration.ToString(); // Get the string representation ``` ### Custom style initialization ```csharp // The sample of a For statement initizalization with predefined style. It is how GD.Statement.For method works. // You must know the 'form' to use this format. // For example a code line like "[1] = GD.Syntax.Space()" will insert space token BEFORE the first static point in the nodes form. // In the code below the first point of the For statement is the iterators variable name. public static GDForStatement For(GDIdentifier variable, GDExpression collection, GDExpression body) => new GDForStatement() { ForKeyword = new GDForKeyword(), [1] = GD.Syntax.Space(), Variable = variable, [2] = GD.Syntax.Space(), InKeyword = new GDInKeyword(), [3] = GD.Syntax.Space(), Collection = collection, Colon = new GDColon(), [5] = GD.Syntax.Space(), Expression = body }; ``` You may use a combination of the styles. ### Calculating properties ```csharp GDSyntaxToken token = null; // any token token.StartLine // calculate the start line of the token in the code token.EndLine // calculate the end line of the token in the code token.Length // calculate the length of the token token.StartColumn // calculate the start column in the line token.EndColumn // calculate the end column in the line token.NewLinesCount // calculate new line characters in the token. token.ClassMember // find the nearest class member from parents token.MainClassDeclaration // find the main class contains the token token.Parents // enumerate all parents of the token ``` For more samples see the [tests](src/GDShrapt.Reader.Tests/ParsingTests.cs).

近期下载者

相关文件


收藏者