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. diff --git a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt index 9625873c..a9a3c2db 100644 --- a/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt +++ b/src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt @@ -395,11 +395,6 @@ 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 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..77709d9f --- /dev/null +++ b/src/GraphQLParser.Tests/Extensions.cs @@ -0,0 +1,23 @@ +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..0dd90590 100644 --- a/src/GraphQLParser/ParserExtensions.cs +++ b/src/GraphQLParser/ParserExtensions.cs @@ -1,31 +1,14 @@ using System; using System.Buffers; using System.Collections.Generic; -using GraphQLParser.AST; namespace GraphQLParser { /// /// Extension methods for parsing GraphQL documents. /// - public static class ParserExtensions + internal 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();