-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathparser.y
521 lines (424 loc) · 18.9 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
class GraphQL::Language::Parser
rule
target: document
document: definitions_list { return make_node(:Document, definitions: val[0])}
definitions_list:
definition { return [val[0]]}
| definitions_list definition { val[0] << val[1] }
definition:
executable_definition
| type_system_definition
| type_system_extension
executable_definition:
operation_definition
| fragment_definition
operation_definition:
operation_type operation_name_opt variable_definitions_opt directives_list_opt selection_set {
return make_node(
:OperationDefinition, {
operation_type: val[0],
name: val[1],
variables: val[2],
directives: val[3],
selections: val[4],
position_source: val[0],
}
)
}
| LCURLY selection_list RCURLY {
return make_node(
:OperationDefinition, {
operation_type: "query",
selections: val[1],
position_source: val[0],
}
)
}
| LCURLY RCURLY {
return make_node(
:OperationDefinition, {
operation_type: "query",
selections: [],
position_source: val[0],
}
)
}
operation_type:
QUERY
| MUTATION
| SUBSCRIPTION
operation_name_opt:
/* none */ { return nil }
| name
variable_definitions_opt:
/* none */ { return [] }
| LPAREN variable_definitions_list RPAREN { return val[1] }
variable_definitions_list:
variable_definition { return [val[0]] }
| variable_definitions_list variable_definition { val[0] << val[1] }
variable_definition:
VAR_SIGN name COLON type default_value_opt {
return make_node(:VariableDefinition, {
name: val[1],
type: val[3],
default_value: val[4],
position_source: val[0],
})
}
type:
name { return make_node(:TypeName, name: val[0])}
| type BANG { return make_node(:NonNullType, of_type: val[0]) }
| LBRACKET type RBRACKET { return make_node(:ListType, of_type: val[1]) }
default_value_opt:
/* none */ { return nil }
| EQUALS literal_value { return val[1] }
selection_set:
LCURLY selection_list RCURLY { return val[1] }
selection_set_opt:
/* none */ { return [] }
| selection_set { return val[0] }
selection_list:
selection { return [result] }
| selection_list selection { val[0] << val[1] }
selection:
field
| fragment_spread
| inline_fragment
field:
name arguments_opt directives_list_opt selection_set_opt {
return make_node(
:Field, {
name: val[0],
arguments: val[1],
directives: val[2],
selections: val[3],
position_source: val[0],
}
)
}
| name COLON name arguments_opt directives_list_opt selection_set_opt {
return make_node(
:Field, {
alias: val[0],
name: val[2],
arguments: val[3],
directives: val[4],
selections: val[5],
position_source: val[0],
}
)
}
name:
name_without_on
| ON
schema_keyword:
SCHEMA
| SCALAR
| TYPE
| IMPLEMENTS
| INTERFACE
| UNION
| ENUM
| INPUT
| DIRECTIVE
name_without_on:
IDENTIFIER
| FRAGMENT
| TRUE
| FALSE
| operation_type
| schema_keyword
enum_name: /* any identifier, but not "true", "false" or "null" */
IDENTIFIER
| FRAGMENT
| ON
| operation_type
| schema_keyword
enum_value_definition:
description_opt enum_name directives_list_opt { return make_node(:EnumValueDefinition, name: val[1], directives: val[2], description: val[0] || get_description(val[1]), position_source: val[0] || val[1]) }
enum_value_definitions:
enum_value_definition { return [val[0]] }
| enum_value_definitions enum_value_definition { return val[0] << val[1] }
arguments_opt:
/* none */ { return [] }
| LPAREN RPAREN { return [] }
| LPAREN arguments_list RPAREN { return val[1] }
arguments_list:
argument { return [val[0]] }
| arguments_list argument { val[0] << val[1] }
argument:
name COLON input_value { return make_node(:Argument, name: val[0], value: val[2], position_source: val[0])}
literal_value:
FLOAT { return val[0].to_f }
| INT { return val[0].to_i }
| STRING { return val[0].to_s }
| TRUE { return true }
| FALSE { return false }
| null_value
| enum_value
| list_value
| object_literal_value
input_value:
| literal_value
| variable
| object_value
null_value: NULL { return make_node(:NullValue, name: val[0], position_source: val[0]) }
variable: VAR_SIGN name { return make_node(:VariableIdentifier, name: val[1], position_source: val[0]) }
list_value:
LBRACKET RBRACKET { return [] }
| LBRACKET list_value_list RBRACKET { return val[1] }
list_value_list:
input_value { return [val[0]] }
| list_value_list input_value { val[0] << val[1] }
object_value:
LCURLY RCURLY { return make_node(:InputObject, arguments: [], position_source: val[0])}
| LCURLY object_value_list RCURLY { return make_node(:InputObject, arguments: val[1], position_source: val[0])}
object_value_list:
object_value_field { return [val[0]] }
| object_value_list object_value_field { val[0] << val[1] }
object_value_field:
name COLON input_value { return make_node(:Argument, name: val[0], value: val[2], position_source: val[0])}
/* like the previous, but with literals only: */
object_literal_value:
LCURLY RCURLY { return make_node(:InputObject, arguments: [], position_source: val[0])}
| LCURLY object_literal_value_list RCURLY { return make_node(:InputObject, arguments: val[1], position_source: val[0])}
object_literal_value_list:
object_literal_value_field { return [val[0]] }
| object_literal_value_list object_literal_value_field { val[0] << val[1] }
object_literal_value_field:
name COLON literal_value { return make_node(:Argument, name: val[0], value: val[2], position_source: val[0])}
enum_value: enum_name { return make_node(:Enum, name: val[0], position_source: val[0]) }
directives_list_opt:
/* none */ { return [] }
| directives_list
directives_list:
directive { return [val[0]] }
| directives_list directive { val[0] << val[1] }
directive: DIR_SIGN name arguments_opt { return make_node(:Directive, name: val[1], arguments: val[2], position_source: val[0]) }
fragment_spread:
ELLIPSIS name_without_on directives_list_opt { return make_node(:FragmentSpread, name: val[1], directives: val[2], position_source: val[0]) }
inline_fragment:
ELLIPSIS ON type directives_list_opt selection_set {
return make_node(:InlineFragment, {
type: val[2],
directives: val[3],
selections: val[4],
position_source: val[0]
})
}
| ELLIPSIS directives_list_opt selection_set {
return make_node(:InlineFragment, {
type: nil,
directives: val[1],
selections: val[2],
position_source: val[0]
})
}
fragment_definition:
FRAGMENT fragment_name_opt ON type directives_list_opt selection_set {
return make_node(:FragmentDefinition, {
name: val[1],
type: val[3],
directives: val[4],
selections: val[5],
position_source: val[0],
}
)
}
fragment_name_opt:
/* none */ { return nil }
| name_without_on
type_system_definition:
schema_definition
| type_definition
| directive_definition
schema_definition:
SCHEMA directives_list_opt LCURLY operation_type_definition_list RCURLY { return make_node(:SchemaDefinition, position_source: val[0], directives: val[1], **val[3]) }
operation_type_definition_list:
operation_type_definition
| operation_type_definition_list operation_type_definition { return val[0].merge(val[1]) }
operation_type_definition:
operation_type COLON name { return { val[0].to_s.to_sym => val[2] } }
type_definition:
scalar_type_definition
| object_type_definition
| interface_type_definition
| union_type_definition
| enum_type_definition
| input_object_type_definition
type_system_extension:
schema_extension
| type_extension
schema_extension:
EXTEND SCHEMA directives_list_opt LCURLY operation_type_definition_list RCURLY { return make_node(:SchemaExtension, position_source: val[0], directives: val[2], **val[4]) }
| EXTEND SCHEMA directives_list { return make_node(:SchemaExtension, position_source: val[0], directives: val[2]) }
type_extension:
scalar_type_extension
| object_type_extension
| interface_type_extension
| union_type_extension
| enum_type_extension
| input_object_type_extension
scalar_type_extension: EXTEND SCALAR name directives_list { return make_node(:ScalarTypeExtension, name: val[2], directives: val[3], position_source: val[0]) }
object_type_extension:
/* TODO - This first one shouldn't be necessary but parser is getting confused */
EXTEND TYPE name implements LCURLY field_definition_list RCURLY { return make_node(:ObjectTypeExtension, name: val[2], interfaces: val[3], directives: [], fields: val[5], position_source: val[0]) }
| EXTEND TYPE name implements_opt directives_list_opt LCURLY field_definition_list RCURLY { return make_node(:ObjectTypeExtension, name: val[2], interfaces: val[3], directives: val[4], fields: val[6], position_source: val[0]) }
| EXTEND TYPE name implements_opt directives_list { return make_node(:ObjectTypeExtension, name: val[2], interfaces: val[3], directives: val[4], fields: [], position_source: val[0]) }
| EXTEND TYPE name implements { return make_node(:ObjectTypeExtension, name: val[2], interfaces: val[3], directives: [], fields: [], position_source: val[0]) }
interface_type_extension:
EXTEND INTERFACE name directives_list_opt LCURLY field_definition_list RCURLY { return make_node(:InterfaceTypeExtension, name: val[2], directives: val[3], fields: val[5], position_source: val[0]) }
| EXTEND INTERFACE name directives_list { return make_node(:InterfaceTypeExtension, name: val[2], directives: val[3], fields: [], position_source: val[0]) }
union_type_extension:
EXTEND UNION name directives_list_opt EQUALS union_members { return make_node(:UnionTypeExtension, name: val[2], directives: val[3], types: val[5], position_source: val[0]) }
| EXTEND UNION name directives_list { return make_node(:UnionTypeExtension, name: val[2], directives: val[3], types: [], position_source: val[0]) }
enum_type_extension:
EXTEND ENUM name directives_list_opt LCURLY enum_value_definitions RCURLY { return make_node(:EnumTypeExtension, name: val[2], directives: val[3], values: val[5], position_source: val[0]) }
| EXTEND ENUM name directives_list { return make_node(:EnumTypeExtension, name: val[2], directives: val[3], values: [], position_source: val[0]) }
input_object_type_extension:
EXTEND INPUT name directives_list_opt LCURLY input_value_definition_list RCURLY { return make_node(:InputObjectTypeExtension, name: val[2], directives: val[3], fields: val[5], position_source: val[0]) }
| EXTEND INPUT name directives_list { return make_node(:InputObjectTypeExtension, name: val[2], directives: val[3], fields: [], position_source: val[0]) }
description: STRING
description_opt:
/* none */
| description
scalar_type_definition:
description_opt SCALAR name directives_list_opt {
return make_node(:ScalarTypeDefinition, name: val[2], directives: val[3], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
object_type_definition:
description_opt TYPE name implements_opt directives_list_opt LCURLY field_definition_list RCURLY {
return make_node(:ObjectTypeDefinition, name: val[2], interfaces: val[3], directives: val[4], fields: val[6], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
implements_opt:
/* none */ { return [] }
| implements
implements:
IMPLEMENTS AMP interfaces_list { return val[2] }
| IMPLEMENTS interfaces_list { return val[1] }
| IMPLEMENTS legacy_interfaces_list { return val[1] }
interfaces_list:
name { return [make_node(:TypeName, name: val[0], position_source: val[0])] }
| interfaces_list AMP name { val[0] << make_node(:TypeName, name: val[2], position_source: val[2]) }
legacy_interfaces_list:
name { return [make_node(:TypeName, name: val[0], position_source: val[0])] }
| legacy_interfaces_list name { val[0] << make_node(:TypeName, name: val[1], position_source: val[1]) }
input_value_definition:
description_opt name COLON type default_value_opt directives_list_opt {
return make_node(:InputValueDefinition, name: val[1], type: val[3], default_value: val[4], directives: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
input_value_definition_list:
input_value_definition { return [val[0]] }
| input_value_definition_list input_value_definition { val[0] << val[1] }
arguments_definitions_opt:
/* none */ { return [] }
| LPAREN input_value_definition_list RPAREN { return val[1] }
field_definition:
description_opt name arguments_definitions_opt COLON type directives_list_opt {
return make_node(:FieldDefinition, name: val[1], arguments: val[2], type: val[4], directives: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
field_definition_list:
/* none */ { return [] }
| field_definition { return [val[0]] }
| field_definition_list field_definition { val[0] << val[1] }
interface_type_definition:
description_opt INTERFACE name directives_list_opt LCURLY field_definition_list RCURLY {
return make_node(:InterfaceTypeDefinition, name: val[2], directives: val[3], fields: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
union_members:
name { return [make_node(:TypeName, name: val[0], position_source: val[0])]}
| union_members PIPE name { val[0] << make_node(:TypeName, name: val[2], position_source: val[2]) }
union_type_definition:
description_opt UNION name directives_list_opt EQUALS union_members {
return make_node(:UnionTypeDefinition, name: val[2], directives: val[3], types: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
enum_type_definition:
description_opt ENUM name directives_list_opt LCURLY enum_value_definitions RCURLY {
return make_node(:EnumTypeDefinition, name: val[2], directives: val[3], values: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
input_object_type_definition:
description_opt INPUT name directives_list_opt LCURLY input_value_definition_list RCURLY {
return make_node(:InputObjectTypeDefinition, name: val[2], directives: val[3], fields: val[5], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
directive_definition:
description_opt DIRECTIVE DIR_SIGN name arguments_definitions_opt ON directive_locations {
return make_node(:DirectiveDefinition, name: val[3], arguments: val[4], locations: val[6], description: val[0] || get_description(val[1]), position_source: val[0] || val[1])
}
directive_locations:
name { return [make_node(:DirectiveLocation, name: val[0].to_s, position_source: val[0])] }
| directive_locations PIPE name { val[0] << make_node(:DirectiveLocation, name: val[2].to_s, position_source: val[2]) }
end
---- header ----
---- inner ----
def initialize(query_string, filename:, tracer: Tracing::NullTracer)
@query_string = query_string
@filename = filename
@tracer = tracer
end
def parse_document
@document ||= begin
# Break the string into tokens
@tracer.trace("lex", {query_string: @query_string}) do
@tokens ||= GraphQL.scan(@query_string)
end
# From the tokens, build an AST
@tracer.trace("parse", {query_string: @query_string}) do
if @tokens.none?
make_node(:Document, definitions: [], filename: @filename)
else
do_parse
end
end
end
end
def self.parse(query_string, filename: nil, tracer: GraphQL::Tracing::NullTracer)
self.new(query_string, filename: filename, tracer: tracer).parse_document
end
private
def next_token
lexer_token = @tokens.shift
if lexer_token.nil?
nil
else
[lexer_token.name, lexer_token]
end
end
def get_description(token)
comments = []
loop do
prev_token = token
token = token.prev_token
break if token.nil?
break if token.name != :COMMENT
break if prev_token.line != token.line + 1
comments.unshift(token.to_s.sub(/^#\s*/, ""))
end
return nil if comments.empty?
comments.join("\n")
end
def on_error(parser_token_id, lexer_token, vstack)
if lexer_token == "$" || lexer_token == nil
raise GraphQL::ParseError.new("Unexpected end of document", nil, nil, @query_string, filename: @filename)
else
parser_token_name = token_to_str(parser_token_id)
if parser_token_name.nil?
raise GraphQL::ParseError.new("Parse Error on unknown token: {token_id: #{parser_token_id}, lexer_token: #{lexer_token}} from #{@query_string}", nil, nil, @query_string, filename: @filename)
else
line, col = lexer_token.line_and_column
if lexer_token.name == :BAD_UNICODE_ESCAPE
raise GraphQL::ParseError.new("Parse error on bad Unicode escape sequence: #{lexer_token.to_s.inspect} (#{parser_token_name}) at [#{line}, #{col}]", line, col, @query_string, filename: @filename)
else
raise GraphQL::ParseError.new("Parse error on #{lexer_token.to_s.inspect} (#{parser_token_name}) at [#{line}, #{col}]", line, col, @query_string, filename: @filename)
end
end
end
end
def make_node(node_name, assigns)
assigns.each do |key, value|
if key != :position_source && value.is_a?(GraphQL::Language::Token)
assigns[key] = value.to_s
end
end
assigns[:filename] = @filename
GraphQL::Language::Nodes.const_get(node_name).new(assigns)
end