Skip to content

Commit 7808ee6

Browse files
committed
Add initial comment support
1 parent 4cc21c7 commit 7808ee6

11 files changed

+152
-46
lines changed

src/GraphQLParser.Tests/ParserTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ public void Parse_FieldWithOperationTypeAndNameInput_SelectionSetContainsSingleF
125125
[Fact]
126126
public void Parse_KitchenSink_DoesNotThrowError()
127127
{
128-
new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
128+
var document = new Parser(new Lexer()).Parse(new Source(LoadKitchenSink()));
129+
if (document != null)
130+
{
131+
}
129132
}
130133

131134
[Fact]
@@ -219,14 +222,16 @@ fragment frag on Friend {
219222
# LICENSE file in the root directory of this source tree. An additional grant
220223
# of patent rights can be found in the PATENTS file in the same directory.
221224
222-
schema {
225+
schema {
223226
query: QueryType
224227
mutation: MutationType
225228
}
226229
227230
type Foo implements Bar
228231
{
229-
one: Type
232+
# comment 1
233+
one: Type
234+
# comment 2
230235
two(argument: InputType!): Type
231236
three(argument: InputType, other: String): Int
232237
four(argument: String = ""string""): String
@@ -236,6 +241,7 @@ type Foo implements Bar
236241
237242
type AnnotatedObject @onObject(arg: ""value"")
238243
{
244+
# a comment
239245
annotatedField(arg: Type = ""default"" @onArg): Type @onField
240246
}
241247

src/GraphQLParser/AST/ASTNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public abstract class ASTNode
44
{
55
public abstract ASTNodeKind Kind { get; }
66
public GraphQLLocation Location { get; set; }
7+
public GraphQLComment Comment { get; set; }
78
}
89
}

src/GraphQLParser/AST/Enums.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public enum ASTNodeKind
44
{
5+
Comment,
56
Name,
67
Document,
78
OperationDefinition,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace GraphQLParser.AST
4+
{
5+
public class GraphQLComment : ASTNode
6+
{
7+
public GraphQLComment(string comment)
8+
{
9+
Comment = comment;
10+
}
11+
12+
public override ASTNodeKind Kind => ASTNodeKind.Comment;
13+
14+
public string Comment { get; set; }
15+
}
16+
}

src/GraphQLParser/AST/GraphQLInterfaceTypeDefinition.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ public class GraphQLInterfaceTypeDefinition : GraphQLTypeDefinition
88

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

11-
public override ASTNodeKind Kind
12-
{
13-
get
14-
{
15-
return ASTNodeKind.InterfaceTypeDefinition;
16-
}
17-
}
11+
public override ASTNodeKind Kind => ASTNodeKind.InterfaceTypeDefinition;
1812

1913
public GraphQLName Name { get; set; }
2014
}

src/GraphQLParser/AST/GraphQLObjectTypeDefinition.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ public class GraphQLObjectTypeDefinition : ASTNode
1010

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

13-
public override ASTNodeKind Kind
14-
{
15-
get
16-
{
17-
return ASTNodeKind.ObjectTypeDefinition;
18-
}
19-
}
13+
public override ASTNodeKind Kind => ASTNodeKind.ObjectTypeDefinition;
2014

2115
public GraphQLName Name { get; set; }
2216
}

src/GraphQLParser/AST/GraphQLOperationDefinition.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ public class GraphQLOperationDefinition : ASTNode
66
{
77
public IEnumerable<GraphQLDirective> Directives { get; set; }
88

9-
public override ASTNodeKind Kind
10-
{
11-
get
12-
{
13-
return ASTNodeKind.OperationDefinition;
14-
}
15-
}
9+
public override ASTNodeKind Kind => ASTNodeKind.OperationDefinition;
1610

1711
public GraphQLName Name { get; set; }
1812
public OperationType Operation { get; set; }

src/GraphQLParser/AST/GraphQLSchemaDefinition.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ namespace GraphQLParser.AST
55
public class GraphQLSchemaDefinition : ASTNode
66
{
77
public IEnumerable<GraphQLDirective> Directives { get; set; }
8-
9-
public override ASTNodeKind Kind
10-
{
11-
get
12-
{
13-
return ASTNodeKind.SchemaDefinition;
14-
}
15-
}
16-
8+
public override ASTNodeKind Kind => ASTNodeKind.SchemaDefinition;
179
public IEnumerable<GraphQLOperationTypeDefinition> OperationTypes { get; set; }
1810
}
1911
}

src/GraphQLParser/LexerContext.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public Token GetToken()
3636
if (token != null)
3737
return token;
3838

39+
if (code == '#')
40+
return this.ReadComment();
41+
3942
if (char.IsLetter(code) || code == '_')
4043
return this.ReadName();
4144

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

60+
public Token ReadComment()
61+
{
62+
var start = this.currentIndex;
63+
64+
var chunkStart = ++this.currentIndex;
65+
var code = this.GetCode();
66+
var value = string.Empty;
67+
68+
while (this.IsNotAtTheEndOfQuery() && code != 0x000A && code != 0x000D)
69+
{
70+
code = this.ProcessCharacter(ref value, ref chunkStart);
71+
}
72+
73+
value += this.source.Body.Substring(chunkStart, this.currentIndex - chunkStart);
74+
75+
return new Token()
76+
{
77+
Kind = TokenKind.COMMENT,
78+
Value = value,
79+
Start = start,
80+
End = this.currentIndex + 1
81+
};
82+
}
83+
5784
public Token ReadNumber()
5885
{
5986
var isFloat = false;
@@ -274,9 +301,9 @@ private int GetPositionAfterWhitespace(string body, int start)
274301
++position;
275302
break;
276303

277-
case '#':
278-
position = this.WaitForEndOfComment(body, position, code);
279-
break;
304+
// case '#':
305+
// position = this.WaitForEndOfComment(body, position, code);
306+
// break;
280307

281308
default:
282309
return position;

0 commit comments

Comments
 (0)