Skip to content

Add initial comment support #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions src/GraphQLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public void Parse_FieldWithOperationTypeAndNameInput_SelectionSetContainsSingleF
[Fact]
public void Parse_KitchenSink_DoesNotThrowError()
{
new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
var document = new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
if (document != null)
{
}
}

[Fact]
Expand Down Expand Up @@ -219,14 +222,16 @@ fragment frag on Friend {
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

schema {
schema {
query: QueryType
mutation: MutationType
}

type Foo implements Bar
{
one: Type
# comment 1
one: Type
# comment 2
two(argument: InputType!): Type
three(argument: InputType, other: String): Int
four(argument: String = ""string""): String
Expand All @@ -236,6 +241,7 @@ type Foo implements Bar

type AnnotatedObject @onObject(arg: ""value"")
{
# a comment
annotatedField(arg: Type = ""default"" @onArg): Type @onField
}

Expand Down
1 change: 1 addition & 0 deletions src/GraphQLParser/AST/ASTNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public abstract class ASTNode
{
public abstract ASTNodeKind Kind { get; }
public GraphQLLocation Location { get; set; }
public GraphQLComment Comment { get; set; }
}
}
1 change: 1 addition & 0 deletions src/GraphQLParser/AST/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum ASTNodeKind
{
Comment,
Name,
Document,
OperationDefinition,
Expand Down
16 changes: 16 additions & 0 deletions src/GraphQLParser/AST/GraphQLComment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace GraphQLParser.AST
{
public class GraphQLComment : ASTNode
{
public GraphQLComment(string comment)
{
Comment = comment;
}

public override ASTNodeKind Kind => ASTNodeKind.Comment;

public string Comment { get; set; }
}
}
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLInterfaceTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ public class GraphQLInterfaceTypeDefinition : GraphQLTypeDefinition

public IEnumerable<GraphQLFieldDefinition> Fields { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.InterfaceTypeDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.InterfaceTypeDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLObjectTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ public class GraphQLObjectTypeDefinition : ASTNode

public IEnumerable<GraphQLNamedType> Interfaces { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.ObjectTypeDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.ObjectTypeDefinition;

public GraphQLName Name { get; set; }
}
Expand Down
8 changes: 1 addition & 7 deletions src/GraphQLParser/AST/GraphQLOperationDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ public class GraphQLOperationDefinition : ASTNode
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.OperationDefinition;
}
}
public override ASTNodeKind Kind => ASTNodeKind.OperationDefinition;

public GraphQLName Name { get; set; }
public OperationType Operation { get; set; }
Expand Down
10 changes: 1 addition & 9 deletions src/GraphQLParser/AST/GraphQLSchemaDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ namespace GraphQLParser.AST
public class GraphQLSchemaDefinition : ASTNode
{
public IEnumerable<GraphQLDirective> Directives { get; set; }

public override ASTNodeKind Kind
{
get
{
return ASTNodeKind.SchemaDefinition;
}
}

public override ASTNodeKind Kind => ASTNodeKind.SchemaDefinition;
public IEnumerable<GraphQLOperationTypeDefinition> OperationTypes { get; set; }
}
}
33 changes: 30 additions & 3 deletions src/GraphQLParser/LexerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public Token GetToken()
if (token != null)
return token;

if (code == '#')
return this.ReadComment();

if (char.IsLetter(code) || code == '_')
return this.ReadName();

Expand All @@ -54,6 +57,30 @@ public bool OnlyHexInString(string test)
return System.Text.RegularExpressions.Regex.IsMatch(test, @"\A\b[0-9a-fA-F]+\b\Z");
}

public Token ReadComment()
{
var start = this.currentIndex;

var chunkStart = ++this.currentIndex;
var code = this.GetCode();
var value = string.Empty;

while (this.IsNotAtTheEndOfQuery() && code != 0x000A && code != 0x000D)
{
code = this.ProcessCharacter(ref value, ref chunkStart);
}

value += this.source.Body.Substring(chunkStart, this.currentIndex - chunkStart);

return new Token()
{
Kind = TokenKind.COMMENT,
Value = value,
Start = start,
End = this.currentIndex + 1
};
}

public Token ReadNumber()
{
var isFloat = false;
Expand Down Expand Up @@ -274,9 +301,9 @@ private int GetPositionAfterWhitespace(string body, int start)
++position;
break;

case '#':
position = this.WaitForEndOfComment(body, position, code);
break;
// case '#':
// position = this.WaitForEndOfComment(body, position, code);
// break;

default:
return position;
Expand Down
Loading