Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -45,8 +37,6 @@ Parses provided GraphQL expression into AST (abstract syntax tree). Parser also

### Usage

Directly:

```c#
var ast1 = Parser.Parse(@"
{
Expand All @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions src/GraphQLParser.ApiTests/GraphQL-Parser.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser.Benchmarks/Benchmarks/ParserBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]> NamesAndOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void ParseBinaryFile()
{
try
{
_binaryTest.Parse().Dispose();
Parser.Parse(_binaryTest).Dispose();
}
catch (GraphQLSyntaxErrorException)
{
Expand Down
23 changes: 23 additions & 0 deletions src/GraphQLParser.Tests/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using GraphQLParser.AST;

namespace GraphQLParser.Tests
{
internal static class ParserTestExtensions
{
/// <summary>
/// Generates token based on input text.
/// </summary>
/// <param name="source">Input data as a string.</param>
/// <param name="start">The index in the source at which to start searching the token.</param>
/// <returns></returns>
public static Token Lex(this string source, int start = 0) => Lexer.Lex(source, start);

/// <summary>
/// Generates AST based on input text.
/// </summary>
/// <param name="source">Input data as a string.</param>
/// <param name="options">Parser options.</param>
/// <returns>AST (Abstract Syntax Tree) for GraphQL document.</returns>
public static GraphQLDocument Parse(this string source, ParserOptions options = default) => Parser.Parse(source, options);
}
}
19 changes: 1 addition & 18 deletions src/GraphQLParser/ParserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using GraphQLParser.AST;

namespace GraphQLParser
{
/// <summary>
/// Extension methods for parsing GraphQL documents.
/// </summary>
public static class ParserExtensions
internal static class ParserExtensions
{
/// <summary>
/// Generates token based on input text.
/// </summary>
/// <param name="source">Input data as a string.</param>
/// <param name="start">The index in the source at which to start searching the token.</param>
/// <returns></returns>
public static Token Lex(this string source, int start = 0) => Lexer.Lex(source, start);

/// <summary>
/// Generates AST based on input text.
/// </summary>
/// <param name="source">Input data as a string.</param>
/// <param name="options">Parser options.</param>
/// <returns>AST (Abstract Syntax Tree) for GraphQL document.</returns>
public static GraphQLDocument Parse(this string source, ParserOptions options = default) => Parser.Parse(source, options);

internal static (IMemoryOwner<char> owner, ROM result) Concat(this List<ROM> parts)
{
var newLine = Environment.NewLine.AsSpan();
Expand Down