From ef4b2580ab8595953e2d3e0f8a437353b4c3d9eb Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:27:26 -0500 Subject: [PATCH 1/6] Remove extension methods --- .../GraphQL-Parser.approved.txt | 6 +--- .../Benchmarks/ParserBenchmark.cs | 2 +- .../Benchmarks/ParserBinaryBenchmark.cs | 2 +- src/GraphQLParser.Tests/Extensions.cs | 28 +++++++++++++++++++ src/GraphQLParser/ParserExtensions.cs | 17 ----------- 5 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/GraphQLParser.Tests/Extensions.cs diff --git a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt index 9625873c..659ea0d7 100644 --- a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt +++ b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt @@ -395,11 +395,7 @@ namespace GraphQLParser { public static GraphQLParser.AST.GraphQLDocument Parse(GraphQLParser.ROM source, GraphQLParser.ParserOptions options = default) { } } - public static class ParserExtensions - { - public static GraphQLParser.Token Lex(this string source, int start = 0) { } - public static GraphQLParser.AST.GraphQLDocument Parse(this string source, GraphQLParser.ParserOptions options = default) { } - } + public static class ParserExtensions { } public struct ParserOptions { public GraphQLParser.IgnoreOptions Ignore { get; set; } diff --git a/src/GraphQLParser.Benchmarks/Benchmarks/ParserBenchmark.cs b/src/GraphQLParser.Benchmarks/Benchmarks/ParserBenchmark.cs index 2a4723a5..13f3b837 100644 --- a/src/GraphQLParser.Benchmarks/Benchmarks/ParserBenchmark.cs +++ b/src/GraphQLParser.Benchmarks/Benchmarks/ParserBenchmark.cs @@ -56,7 +56,7 @@ orderby benchmark.Parameters["name"] public void Parse(string name, IgnoreOptions options) { var source = GetQueryByName(name); - source.Parse(new ParserOptions { Ignore = options }).Dispose(); + Parser.Parse(source, new ParserOptions { Ignore = options }).Dispose(); } public IEnumerable NamesAndOptions() diff --git a/src/GraphQLParser.Benchmarks/Benchmarks/ParserBinaryBenchmark.cs b/src/GraphQLParser.Benchmarks/Benchmarks/ParserBinaryBenchmark.cs index 4be96ffd..cea7b993 100644 --- a/src/GraphQLParser.Benchmarks/Benchmarks/ParserBinaryBenchmark.cs +++ b/src/GraphQLParser.Benchmarks/Benchmarks/ParserBinaryBenchmark.cs @@ -19,7 +19,7 @@ public void ParseBinaryFile() { try { - _binaryTest.Parse().Dispose(); + Parser.Parse(_binaryTest).Dispose(); } catch (GraphQLSyntaxErrorException) { diff --git a/src/GraphQLParser.Tests/Extensions.cs b/src/GraphQLParser.Tests/Extensions.cs new file mode 100644 index 00000000..9f2f4592 --- /dev/null +++ b/src/GraphQLParser.Tests/Extensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; +using GraphQLParser.AST; + +namespace GraphQLParser.Tests +{ + internal static class ParserTestExtensions + { + + /// + /// Generates token based on input text. + /// + /// Input data as a string. + /// The index in the source at which to start searching the token. + /// + public static Token Lex(this string source, int start = 0) => Lexer.Lex(source, start); + + /// + /// Generates AST based on input text. + /// + /// Input data as a string. + /// Parser options. + /// AST (Abstract Syntax Tree) for GraphQL document. + public static GraphQLDocument Parse(this string source, ParserOptions options = default) => Parser.Parse(source, options); + + } +} diff --git a/src/GraphQLParser/ParserExtensions.cs b/src/GraphQLParser/ParserExtensions.cs index 385cd49a..9efba11d 100644 --- a/src/GraphQLParser/ParserExtensions.cs +++ b/src/GraphQLParser/ParserExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Buffers; using System.Collections.Generic; -using GraphQLParser.AST; namespace GraphQLParser { @@ -10,22 +9,6 @@ namespace GraphQLParser /// public static class ParserExtensions { - /// - /// Generates token based on input text. - /// - /// Input data as a string. - /// The index in the source at which to start searching the token. - /// - public static Token Lex(this string source, int start = 0) => Lexer.Lex(source, start); - - /// - /// Generates AST based on input text. - /// - /// Input data as a string. - /// Parser options. - /// AST (Abstract Syntax Tree) for GraphQL document. - public static GraphQLDocument Parse(this string source, ParserOptions options = default) => Parser.Parse(source, options); - internal static (IMemoryOwner owner, ROM result) Concat(this List parts) { var newLine = Environment.NewLine.AsSpan(); From bc3914b86b7dbe3536a2076ca875e7bb0bceb979 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:28:39 -0500 Subject: [PATCH 2/6] Update --- src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt | 1 - src/GraphQLParser/ParserExtensions.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt index 659ea0d7..a9a3c2db 100644 --- a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt +++ b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt @@ -395,7 +395,6 @@ namespace GraphQLParser { public static GraphQLParser.AST.GraphQLDocument Parse(GraphQLParser.ROM source, GraphQLParser.ParserOptions options = default) { } } - public static class ParserExtensions { } public struct ParserOptions { public GraphQLParser.IgnoreOptions Ignore { get; set; } diff --git a/src/GraphQLParser/ParserExtensions.cs b/src/GraphQLParser/ParserExtensions.cs index 9efba11d..0dd90590 100644 --- a/src/GraphQLParser/ParserExtensions.cs +++ b/src/GraphQLParser/ParserExtensions.cs @@ -7,7 +7,7 @@ namespace GraphQLParser /// /// Extension methods for parsing GraphQL documents. /// - public static class ParserExtensions + internal static class ParserExtensions { internal static (IMemoryOwner owner, ROM result) Concat(this List parts) { From 11f4b56f189ffe1d9b458b90310a6d7b9e866bad Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:30:51 -0500 Subject: [PATCH 3/6] Update src/GraphQLParser.Tests/Extensions.cs Co-authored-by: Ivan Maximov --- src/GraphQLParser.Tests/Extensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GraphQLParser.Tests/Extensions.cs b/src/GraphQLParser.Tests/Extensions.cs index 9f2f4592..9822d623 100644 --- a/src/GraphQLParser.Tests/Extensions.cs +++ b/src/GraphQLParser.Tests/Extensions.cs @@ -7,7 +7,6 @@ namespace GraphQLParser.Tests { internal static class ParserTestExtensions { - /// /// Generates token based on input text. /// From 3aea3222f1b61623e4c7e94ab50e12a5e05f0f29 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:31:10 -0500 Subject: [PATCH 4/6] Update src/GraphQLParser.Tests/Extensions.cs Co-authored-by: Ivan Maximov --- src/GraphQLParser.Tests/Extensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GraphQLParser.Tests/Extensions.cs b/src/GraphQLParser.Tests/Extensions.cs index 9822d623..220c8cc0 100644 --- a/src/GraphQLParser.Tests/Extensions.cs +++ b/src/GraphQLParser.Tests/Extensions.cs @@ -22,6 +22,5 @@ internal static class ParserTestExtensions /// Parser options. /// AST (Abstract Syntax Tree) for GraphQL document. public static GraphQLDocument Parse(this string source, ParserOptions options = default) => Parser.Parse(source, options); - } } From be90b3e9305a1a663b5b6471810cf342a945c236 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:32:09 -0500 Subject: [PATCH 5/6] Fix usings --- src/GraphQLParser.Tests/Extensions.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/GraphQLParser.Tests/Extensions.cs b/src/GraphQLParser.Tests/Extensions.cs index 9f2f4592..e3febbee 100644 --- a/src/GraphQLParser.Tests/Extensions.cs +++ b/src/GraphQLParser.Tests/Extensions.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Text; using GraphQLParser.AST; namespace GraphQLParser.Tests From 208592c1610fec225f90fc356559de961b64a3f2 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Fri, 22 Jan 2021 16:33:36 -0500 Subject: [PATCH 6/6] Update readme --- README.md | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/README.md b/README.md index 91c5d756..8c48514e 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,10 @@ does not allocate memory on the managed heap at all. ### Usage -Directly: - ```c# var token = Lexer.Lex("\"str\""); ``` -Or via extension method: - -```c# -var token = "\"str\"".Lex(); -``` - Lex method always returns the first token it finds. In this case case the result would look like following. ![lexer example](assets/lexer-example.png) @@ -45,8 +37,6 @@ Parses provided GraphQL expression into AST (abstract syntax tree). Parser also ### Usage -Directly: - ```c# var ast1 = Parser.Parse(@" { @@ -59,20 +49,6 @@ var ast2 = Parser.Parse(@" }", new ParserOptions { Ignore = IgnoreOptions.IgnoreComments }); ``` -Or via extension method: - -```c# -var ast = @" -{ - field -}".Parse(); - -var ast = @" -{ - field -}".Parse(new ParserOptions { Ignore = IgnoreOptions.IgnoreCommentsAndLocations }); -``` - By default `ParserOptions.Ignore` is `IgnoreOptions.IgnoreComments` to improve performance. If you don't need information about tokens locations in the source document, then use `IgnoreOptions.IgnoreCommentsAndLocations`. This will maximize the saving of memory allocated in the managed heap for AST.