Skip to content

Commit

Permalink
fixes #22 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored and joemcbride committed Mar 20, 2019
1 parent aa3d963 commit 9b8937d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
Binary file added src/GraphQLParser.Benchmarks/BinaryTest.graphql
Binary file not shown.
14 changes: 12 additions & 2 deletions src/GraphQLParser.Benchmarks/GraphQLParser.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
</PropertyGroup>

<ItemGroup>
<None Remove="BinaryTest.graphql" />
</ItemGroup>

<ItemGroup>
<Content Include="BinaryTest.graphql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GraphQLParser\GraphQLParser.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.8" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.10.8" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.10.14" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/GraphQLParser.Benchmarks/LexerBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BenchmarkDotNet.Attributes;
using GraphQLParser.Exceptions;
using System.IO;

namespace GraphQLParser.Benchmarks
{
Expand All @@ -7,6 +9,7 @@ namespace GraphQLParser.Benchmarks
#endif
public class LexerBenchmark
{
private static readonly string Binary = File.ReadAllText("BinaryTest.graphql");
private const string KitchenSink = @"
query queryName($foo: ComplexType, $site: Site = MOBILE) {
whoever123is: node(id: [123, 456]) {
Expand Down Expand Up @@ -70,5 +73,18 @@ public void LexKitchenSink()
resetPosition = token.End;
}
}

[Benchmark]
public void ParseBinaryFile()
{
try
{
var parser = new Parser(new Lexer());
parser.Parse(new Source(Binary));
}
catch (GraphQLSyntaxErrorException)
{
}
}
}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser/AST/GraphQLListValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class GraphQLListValue : GraphQLValue
{
private ASTNodeKind kindField;
private readonly ASTNodeKind kindField;

public GraphQLListValue(ASTNodeKind kind)
{
Expand Down
42 changes: 39 additions & 3 deletions src/GraphQLParser/Exceptions/GraphQLSyntaxErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Linq;
using System.Text;

public class GraphQLSyntaxErrorException : Exception
{
Expand Down Expand Up @@ -49,13 +50,48 @@ private static string LeftPad(int length, string str)

private static string ReplaceWithUnicodeRepresentation(string str)
{
if (!HasReplacementCharacter(str))
return str;

var buffer = new StringBuilder(str.Length);

foreach (var code in str)
{
if (code < 0x0020 && code != 0x0009 && code != 0x000A && code != 0x000D)
str = str.Replace(string.Empty + code, "\\u" + ((int)code).ToString("D4"));
if (IsReplacementCharacter(code))
{
buffer.Append(GetUnicodeRepresentation(code));
}
else
{
buffer.Append(code);
}
}

return str;
return buffer.ToString();
}

private static bool HasReplacementCharacter(string str)
{
foreach (var code in str)
{
if (IsReplacementCharacter(code))
return true;
}

return false;
}

private static bool IsReplacementCharacter(char code) => code < 0x0020 && code != 0x0009 && code != 0x000A && code != 0x000D;

private static string GetUnicodeRepresentation(char code)
{
switch (code)
{
case '\0':
return "\\u0000";
default:
return "\\u" + ((int)code).ToString("D4");
}
}
}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser/LexerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class LexerContext : IDisposable
{
private int currentIndex;
private ISource source;
private readonly ISource source;

public LexerContext(ISource source, int index)
{
Expand Down
1 change: 0 additions & 1 deletion src/GraphQLParser/ParserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ private GraphQLValue ParseNameValue(bool isConstant)
$"Unexpected {this.currentToken}", this.source, this.currentToken.Start);
}


private GraphQLValue ParseObject(bool isConstant)
{
var comment = GetComment();
Expand Down

0 comments on commit 9b8937d

Please sign in to comment.