tinygql 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/bench.rb +38 -0
- data/lib/tinygql/lexer.rb +118 -40
- data/lib/tinygql/nodes.rb +130 -81
- data/lib/tinygql/nodes.rb.erb +15 -2
- data/lib/tinygql/parser.rb +108 -47
- data/lib/tinygql/version.rb +1 -1
- data/test/lexer_test.rb +78 -0
- data/test/parser_test.rb +17 -0
- metadata +2 -2
data/lib/tinygql/parser.rb
CHANGED
@@ -13,8 +13,7 @@ module TinyGQL
|
|
13
13
|
|
14
14
|
def initialize doc
|
15
15
|
@lexer = Lexer.new doc
|
16
|
-
@lexer.advance
|
17
|
-
@token_name = @lexer.token_name
|
16
|
+
@token_name = @lexer.advance
|
18
17
|
end
|
19
18
|
|
20
19
|
def parse
|
@@ -25,8 +24,13 @@ module TinyGQL
|
|
25
24
|
|
26
25
|
attr_reader :token_name
|
27
26
|
|
27
|
+
def pos
|
28
|
+
@lexer.pos
|
29
|
+
end
|
30
|
+
|
28
31
|
def document
|
29
|
-
|
32
|
+
loc = pos
|
33
|
+
Nodes::Document.new loc, definition_list
|
30
34
|
end
|
31
35
|
|
32
36
|
def definition_list
|
@@ -65,52 +69,58 @@ module TinyGQL
|
|
65
69
|
end
|
66
70
|
|
67
71
|
def input_object_type_extension
|
72
|
+
loc = pos
|
68
73
|
expect_token :INPUT
|
69
74
|
name = self.name
|
70
75
|
directives = if at?(:DIR_SIGN); self.directives; end
|
71
76
|
input_fields_definition = if at?(:LCURLY); self.input_fields_definition; end
|
72
|
-
Nodes::InputObjectTypeExtension.new(name, directives, input_fields_definition)
|
77
|
+
Nodes::InputObjectTypeExtension.new(loc, name, directives, input_fields_definition)
|
73
78
|
end
|
74
79
|
|
75
80
|
def enum_type_extension
|
81
|
+
loc = pos
|
76
82
|
expect_token :ENUM
|
77
83
|
name = self.name
|
78
84
|
directives = if at?(:DIR_SIGN); self.directives; end
|
79
85
|
enum_values_definition = if at?(:LCURLY); self.enum_values_definition; end
|
80
|
-
Nodes::EnumTypeExtension.new(name, directives, enum_values_definition)
|
86
|
+
Nodes::EnumTypeExtension.new(loc, name, directives, enum_values_definition)
|
81
87
|
end
|
82
88
|
|
83
89
|
def union_type_extension
|
90
|
+
loc = pos
|
84
91
|
expect_token :UNION
|
85
92
|
name = self.name
|
86
93
|
directives = if at?(:DIR_SIGN); self.directives; end
|
87
94
|
union_member_types = if at?(:EQUALS); self.union_member_types; end
|
88
|
-
Nodes::UnionTypeExtension.new(name, directives, union_member_types)
|
95
|
+
Nodes::UnionTypeExtension.new(loc, name, directives, union_member_types)
|
89
96
|
end
|
90
97
|
|
91
98
|
def interface_type_extension
|
99
|
+
loc = pos
|
92
100
|
expect_token :INTERFACE
|
93
101
|
name = self.name
|
94
102
|
implements_interfaces = if at?(:IMPLEMENTS); self.implements_interfaces; end
|
95
103
|
directives = if at?(:DIR_SIGN); self.directives; end
|
96
104
|
fields_definition = if at?(:LCURLY); self.fields_definition; end
|
97
|
-
Nodes::InterfaceTypeExtension.new(name, implements_interfaces, directives, fields_definition)
|
105
|
+
Nodes::InterfaceTypeExtension.new(loc, name, implements_interfaces, directives, fields_definition)
|
98
106
|
end
|
99
107
|
|
100
108
|
def scalar_type_extension
|
109
|
+
loc = pos
|
101
110
|
expect_token :SCALAR
|
102
111
|
name = self.name
|
103
112
|
directives = if at?(:DIR_SIGN); self.directives; end
|
104
|
-
Nodes::ScalarTypeExtension.new(name, directives)
|
113
|
+
Nodes::ScalarTypeExtension.new(loc, name, directives)
|
105
114
|
end
|
106
115
|
|
107
116
|
def object_type_extension
|
117
|
+
loc = pos
|
108
118
|
expect_token :TYPE
|
109
119
|
name = self.name
|
110
120
|
implements_interfaces = if at?(:IMPLEMENTS); self.implements_interfaces; end
|
111
121
|
directives = if at?(:DIR_SIGN); self.directives; end
|
112
122
|
fields_definition = if at?(:LCURLY); self.fields_definition; end
|
113
|
-
Nodes::ObjectTypeExtension.new(name, implements_interfaces, directives, fields_definition)
|
123
|
+
Nodes::ObjectTypeExtension.new(loc, name, implements_interfaces, directives, fields_definition)
|
114
124
|
end
|
115
125
|
|
116
126
|
def type_system_definition desc
|
@@ -123,13 +133,14 @@ module TinyGQL
|
|
123
133
|
end
|
124
134
|
|
125
135
|
def directive_defintion desc
|
136
|
+
loc = pos
|
126
137
|
expect_token :DIRECTIVE
|
127
138
|
expect_token :DIR_SIGN
|
128
139
|
name = self.name
|
129
140
|
arguments_definition = if at?(:LPAREN); self.arguments_definition; end
|
130
141
|
expect_token :ON
|
131
142
|
directive_locations = self.directive_locations
|
132
|
-
Nodes::DirectiveDefinition.new(desc, name, arguments_definition, directive_locations)
|
143
|
+
Nodes::DirectiveDefinition.new(loc, desc, name, arguments_definition, directive_locations)
|
133
144
|
end
|
134
145
|
|
135
146
|
def directive_locations
|
@@ -142,11 +153,12 @@ module TinyGQL
|
|
142
153
|
end
|
143
154
|
|
144
155
|
def directive_location
|
156
|
+
loc = pos
|
145
157
|
directive = expect_token_value :IDENTIFIER
|
146
158
|
|
147
159
|
case directive
|
148
160
|
when "QUERY", "MUTATION", "SUBSCRIPTION", "FIELD", "FRAGMENT_DEFINITION", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"
|
149
|
-
Nodes::ExecutableDirectiveLocation.new(directive)
|
161
|
+
Nodes::ExecutableDirectiveLocation.new(loc, directive)
|
150
162
|
when "SCHEMA",
|
151
163
|
"SCALAR",
|
152
164
|
"OBJECT",
|
@@ -158,7 +170,7 @@ module TinyGQL
|
|
158
170
|
"ENUM_VALUE",
|
159
171
|
"INPUT_OBJECT",
|
160
172
|
"INPUT_FIELD_DEFINITION"
|
161
|
-
Nodes::TypeSystemDirectiveLocation.new(directive)
|
173
|
+
Nodes::TypeSystemDirectiveLocation.new(loc, directive)
|
162
174
|
else
|
163
175
|
raise UnexpectedToken, "Expected directive #{directive}"
|
164
176
|
end
|
@@ -178,11 +190,12 @@ module TinyGQL
|
|
178
190
|
end
|
179
191
|
|
180
192
|
def input_object_type_definition desc
|
193
|
+
loc = pos
|
181
194
|
expect_token :INPUT
|
182
195
|
name = self.name
|
183
196
|
directives = if at?(:DIR_SIGN); self.directives; end
|
184
197
|
input_fields_definition = if at?(:LCURLY); self.input_fields_definition; end
|
185
|
-
Nodes::InputObjectTypeDefinition.new(desc, name, directives, input_fields_definition)
|
198
|
+
Nodes::InputObjectTypeDefinition.new(loc, desc, name, directives, input_fields_definition)
|
186
199
|
end
|
187
200
|
|
188
201
|
def input_fields_definition
|
@@ -196,11 +209,12 @@ module TinyGQL
|
|
196
209
|
end
|
197
210
|
|
198
211
|
def enum_type_definition desc
|
212
|
+
loc = pos
|
199
213
|
expect_token :ENUM
|
200
214
|
name = self.name
|
201
215
|
directives = if at?(:DIR_SIGN); self.directives; end
|
202
216
|
enum_values_definition = if at?(:LCURLY); self.enum_values_definition; end
|
203
|
-
Nodes::EnumTypeDefinition.new(desc, name, directives, enum_values_definition)
|
217
|
+
Nodes::EnumTypeDefinition.new(loc, desc, name, directives, enum_values_definition)
|
204
218
|
end
|
205
219
|
|
206
220
|
def enum_values_definition
|
@@ -214,25 +228,28 @@ module TinyGQL
|
|
214
228
|
end
|
215
229
|
|
216
230
|
def enum_value_definition
|
231
|
+
loc = pos
|
217
232
|
description = if at?(:STRING); string_value; end
|
218
233
|
enum_value = self.enum_value
|
219
234
|
directives = if at?(:DIR_SIGN); self.directives; end
|
220
|
-
Nodes::EnumValueDefinition.new(description, enum_value, directives)
|
235
|
+
Nodes::EnumValueDefinition.new(loc, description, enum_value, directives)
|
221
236
|
end
|
222
237
|
|
223
238
|
def scalar_type_definition desc
|
239
|
+
loc = pos
|
224
240
|
expect_token :SCALAR
|
225
241
|
name = self.name
|
226
242
|
directives = if at?(:DIR_SIGN); self.directives; end
|
227
|
-
Nodes::ScalarTypeDefinition.new(desc, name, directives)
|
243
|
+
Nodes::ScalarTypeDefinition.new(loc, desc, name, directives)
|
228
244
|
end
|
229
245
|
|
230
246
|
def union_type_definition desc
|
247
|
+
loc = pos
|
231
248
|
expect_token :UNION
|
232
249
|
name = self.name
|
233
250
|
directives = if at?(:DIR_SIGN); self.directives; end
|
234
251
|
union_member_types = if at?(:EQUALS); self.union_member_types; end
|
235
|
-
Nodes::UnionTypeDefinition.new(desc, name, directives, union_member_types)
|
252
|
+
Nodes::UnionTypeDefinition.new(loc, desc, name, directives, union_member_types)
|
236
253
|
end
|
237
254
|
|
238
255
|
def union_member_types
|
@@ -246,21 +263,23 @@ module TinyGQL
|
|
246
263
|
end
|
247
264
|
|
248
265
|
def interface_type_definition desc
|
266
|
+
loc = pos
|
249
267
|
expect_token :INTERFACE
|
250
268
|
name = self.name
|
251
269
|
directives = if at?(:DIR_SIGN); self.directives; end
|
252
270
|
fields_definition = if at?(:LCURLY); self.fields_definition; end
|
253
|
-
Nodes::InterfaceTypeDefinition.new(desc, name, directives, fields_definition)
|
271
|
+
Nodes::InterfaceTypeDefinition.new(loc, desc, name, directives, fields_definition)
|
254
272
|
end
|
255
273
|
|
256
274
|
def object_type_definition desc
|
275
|
+
loc = pos
|
257
276
|
expect_token :TYPE
|
258
277
|
name = self.name
|
259
278
|
implements_interfaces = if at?(:IMPLEMENTS); self.implements_interfaces; end
|
260
279
|
directives = if at?(:DIR_SIGN); self.directives; end
|
261
280
|
fields_definition = if at?(:LCURLY); self.fields_definition; end
|
262
281
|
|
263
|
-
Nodes::ObjectTypeDefinition.new(desc, name, implements_interfaces, directives, fields_definition)
|
282
|
+
Nodes::ObjectTypeDefinition.new(loc, desc, name, implements_interfaces, directives, fields_definition)
|
264
283
|
end
|
265
284
|
|
266
285
|
def fields_definition
|
@@ -274,6 +293,7 @@ module TinyGQL
|
|
274
293
|
end
|
275
294
|
|
276
295
|
def field_definition
|
296
|
+
loc = pos
|
277
297
|
description = if at?(:STRING); string_value; end
|
278
298
|
name = self.name
|
279
299
|
arguments_definition = if at?(:LPAREN); self.arguments_definition; end
|
@@ -281,7 +301,7 @@ module TinyGQL
|
|
281
301
|
type = self.type
|
282
302
|
directives = if at?(:DIR_SIGN); self.directives; end
|
283
303
|
|
284
|
-
Nodes::FieldDefinition.new(description, name, arguments_definition, type, directives)
|
304
|
+
Nodes::FieldDefinition.new(loc, description, name, arguments_definition, type, directives)
|
285
305
|
end
|
286
306
|
|
287
307
|
def arguments_definition
|
@@ -295,13 +315,14 @@ module TinyGQL
|
|
295
315
|
end
|
296
316
|
|
297
317
|
def input_value_definition
|
318
|
+
loc = pos
|
298
319
|
description = if at?(:STRING); string_value; end
|
299
320
|
name = self.name
|
300
321
|
expect_token :COLON
|
301
322
|
type = self.type
|
302
323
|
default_value = if at?(:EQUALS); self.default_value; end
|
303
324
|
directives = if at?(:DIR_SIGN); self.directives; end
|
304
|
-
Nodes::InputValueDefinition.new(description, name, type, default_value, directives)
|
325
|
+
Nodes::InputValueDefinition.new(loc, description, name, type, default_value, directives)
|
305
326
|
end
|
306
327
|
|
307
328
|
def implements_interfaces
|
@@ -316,21 +337,23 @@ module TinyGQL
|
|
316
337
|
end
|
317
338
|
|
318
339
|
def schema_definition desc
|
340
|
+
loc = pos
|
319
341
|
expect_token :SCHEMA
|
320
342
|
|
321
343
|
directives = if at?(:DIR_SIGN); self.directives; end
|
322
344
|
expect_token :LCURLY
|
323
345
|
defs = root_operation_type_definition
|
324
346
|
expect_token :RCURLY
|
325
|
-
Nodes::SchemaDefinition.new(desc, directives, defs)
|
347
|
+
Nodes::SchemaDefinition.new(loc, desc, directives, defs)
|
326
348
|
end
|
327
349
|
|
328
350
|
def root_operation_type_definition
|
329
351
|
list = []
|
330
352
|
while !at?(:RCURLY)
|
353
|
+
loc = pos
|
331
354
|
operation_type = self.operation_type
|
332
355
|
expect_token :COLON
|
333
|
-
list << Nodes::RootOperationTypeDefinition.new(operation_type, named_type)
|
356
|
+
list << Nodes::RootOperationTypeDefinition.new(loc, operation_type, named_type)
|
334
357
|
end
|
335
358
|
list
|
336
359
|
end
|
@@ -344,6 +367,7 @@ module TinyGQL
|
|
344
367
|
end
|
345
368
|
|
346
369
|
def fragment_definition
|
370
|
+
loc = pos
|
347
371
|
expect_token :FRAGMENT
|
348
372
|
expect_token(:IDENTIFIER) if at?(:ON)
|
349
373
|
name = self.name
|
@@ -352,10 +376,11 @@ module TinyGQL
|
|
352
376
|
self.directives
|
353
377
|
end
|
354
378
|
|
355
|
-
Nodes::FragmentDefinition.new(name, tc, directives, selection_set)
|
379
|
+
Nodes::FragmentDefinition.new(loc, name, tc, directives, selection_set)
|
356
380
|
end
|
357
381
|
|
358
382
|
def operation_definition
|
383
|
+
loc = pos
|
359
384
|
case token_name
|
360
385
|
when :QUERY, :MUTATION, :SUBSCRIPTION
|
361
386
|
type = self.operation_type
|
@@ -365,6 +390,7 @@ module TinyGQL
|
|
365
390
|
end
|
366
391
|
|
367
392
|
Nodes::OperationDefinition.new(
|
393
|
+
loc,
|
368
394
|
type,
|
369
395
|
ident,
|
370
396
|
variable_definitions,
|
@@ -403,15 +429,17 @@ module TinyGQL
|
|
403
429
|
end
|
404
430
|
|
405
431
|
def fragment_spread
|
432
|
+
loc = pos
|
406
433
|
name = self.name
|
407
434
|
directives = if at?(:DIR_SIGN); self.directives; end
|
408
435
|
|
409
436
|
expect_token(:IDENTIFIER) if at?(:ON)
|
410
437
|
|
411
|
-
Nodes::FragmentSpread.new(name, directives)
|
438
|
+
Nodes::FragmentSpread.new(loc, name, directives)
|
412
439
|
end
|
413
440
|
|
414
441
|
def inline_fragment
|
442
|
+
loc = pos
|
415
443
|
type_condition = if at?(:ON)
|
416
444
|
self.type_condition
|
417
445
|
end
|
@@ -420,15 +448,17 @@ module TinyGQL
|
|
420
448
|
self.directives
|
421
449
|
end
|
422
450
|
|
423
|
-
Nodes::InlineFragment.new(type_condition, directives, selection_set)
|
451
|
+
Nodes::InlineFragment.new(loc, type_condition, directives, selection_set)
|
424
452
|
end
|
425
453
|
|
426
454
|
def type_condition
|
455
|
+
loc = pos
|
427
456
|
expect_token :ON
|
428
|
-
Nodes::TypeCondition.new(named_type)
|
457
|
+
Nodes::TypeCondition.new(loc, named_type)
|
429
458
|
end
|
430
459
|
|
431
460
|
def field
|
461
|
+
loc = pos
|
432
462
|
name = self.name
|
433
463
|
|
434
464
|
aliaz = nil
|
@@ -443,7 +473,7 @@ module TinyGQL
|
|
443
473
|
directives = if at?(:DIR_SIGN); self.directives; end
|
444
474
|
selection_set = if at?(:LCURLY); self.selection_set; end
|
445
475
|
|
446
|
-
Nodes::Field.new(aliaz, name, arguments, directives, selection_set)
|
476
|
+
Nodes::Field.new(loc, aliaz, name, arguments, directives, selection_set)
|
447
477
|
end
|
448
478
|
|
449
479
|
def operation_type
|
@@ -459,13 +489,14 @@ module TinyGQL
|
|
459
489
|
end
|
460
490
|
|
461
491
|
def directive
|
492
|
+
loc = pos
|
462
493
|
expect_token(:DIR_SIGN)
|
463
494
|
name = self.name
|
464
495
|
arguments = if at?(:LPAREN)
|
465
496
|
self.arguments
|
466
497
|
end
|
467
498
|
|
468
|
-
Nodes::Directive.new(name, arguments)
|
499
|
+
Nodes::Directive.new(loc, name, arguments)
|
469
500
|
end
|
470
501
|
|
471
502
|
def arguments
|
@@ -479,9 +510,10 @@ module TinyGQL
|
|
479
510
|
end
|
480
511
|
|
481
512
|
def argument
|
513
|
+
loc = pos
|
482
514
|
name = self.name
|
483
515
|
expect_token(:COLON)
|
484
|
-
Nodes::Argument.new(name, value)
|
516
|
+
Nodes::Argument.new(loc, name, value)
|
485
517
|
end
|
486
518
|
|
487
519
|
def variable_definitions
|
@@ -495,6 +527,7 @@ module TinyGQL
|
|
495
527
|
end
|
496
528
|
|
497
529
|
def variable_definition
|
530
|
+
loc = pos
|
498
531
|
var = variable
|
499
532
|
expect_token(:COLON)
|
500
533
|
type = self.type
|
@@ -502,7 +535,7 @@ module TinyGQL
|
|
502
535
|
self.default_value
|
503
536
|
end
|
504
537
|
|
505
|
-
Nodes::VariableDefinition.new(var, type, default_value)
|
538
|
+
Nodes::VariableDefinition.new(loc, var, type, default_value)
|
506
539
|
end
|
507
540
|
|
508
541
|
def default_value
|
@@ -527,49 +560,61 @@ module TinyGQL
|
|
527
560
|
end
|
528
561
|
|
529
562
|
def object_value
|
563
|
+
start = pos
|
530
564
|
expect_token(:LCURLY)
|
531
565
|
list = []
|
532
566
|
while !at?(:RCURLY)
|
567
|
+
loc = pos
|
533
568
|
n = name
|
534
569
|
expect_token(:COLON)
|
535
|
-
list << Nodes::ObjectField.new(n, value)
|
570
|
+
list << Nodes::ObjectField.new(loc, n, value)
|
536
571
|
end
|
537
572
|
expect_token(:RCURLY)
|
538
|
-
Nodes::ObjectValue.new(list)
|
573
|
+
Nodes::ObjectValue.new(start, list)
|
539
574
|
end
|
540
575
|
|
541
576
|
def list_value
|
577
|
+
loc = pos
|
542
578
|
expect_token(:LBRACKET)
|
543
579
|
list = []
|
544
580
|
while !at?(:RBRACKET)
|
545
581
|
list << value
|
546
582
|
end
|
547
583
|
expect_token(:RBRACKET)
|
548
|
-
Nodes::ListValue.new(list)
|
584
|
+
Nodes::ListValue.new(loc, list)
|
549
585
|
end
|
550
586
|
|
551
587
|
def enum_value
|
552
|
-
Nodes::EnumValue.new(expect_token_value(:IDENTIFIER))
|
588
|
+
Nodes::EnumValue.new(pos, expect_token_value(:IDENTIFIER))
|
553
589
|
end
|
554
590
|
|
555
591
|
def float_value
|
556
|
-
Nodes::FloatValue.new(expect_token_value(:FLOAT))
|
592
|
+
Nodes::FloatValue.new(pos, expect_token_value(:FLOAT))
|
557
593
|
end
|
558
594
|
|
559
595
|
def int_value
|
560
|
-
Nodes::IntValue.new(expect_token_value(:INT))
|
596
|
+
Nodes::IntValue.new(pos, expect_token_value(:INT))
|
561
597
|
end
|
562
598
|
|
563
599
|
def string_value
|
564
|
-
Nodes::StringValue.new(
|
600
|
+
Nodes::StringValue.new(pos, expect_string_value)
|
565
601
|
end
|
566
602
|
|
567
603
|
def boolean_value
|
568
|
-
|
604
|
+
if at?(:TRUE)
|
605
|
+
accept_token
|
606
|
+
Nodes::BooleanValue.new(pos, "true")
|
607
|
+
elsif at?(:FALSE)
|
608
|
+
accept_token
|
609
|
+
Nodes::BooleanValue.new(pos, "false")
|
610
|
+
else
|
611
|
+
expect_tokens([:TRUE, :FALSE])
|
612
|
+
end
|
569
613
|
end
|
570
614
|
|
571
615
|
def null_value
|
572
|
-
|
616
|
+
expect_token :NULL
|
617
|
+
Nodes::NullValue.new(pos, "null")
|
573
618
|
end
|
574
619
|
|
575
620
|
def type
|
@@ -579,40 +624,50 @@ module TinyGQL
|
|
579
624
|
end
|
580
625
|
|
581
626
|
if at?(:BANG)
|
582
|
-
Nodes::NotNullType.new type
|
627
|
+
Nodes::NotNullType.new pos, type
|
583
628
|
expect_token(:BANG)
|
584
629
|
end
|
585
630
|
type
|
586
631
|
end
|
587
632
|
|
588
633
|
def list_type
|
634
|
+
loc = pos
|
589
635
|
expect_token(:LBRACKET)
|
590
|
-
type = Nodes::ListType.new(self.type)
|
636
|
+
type = Nodes::ListType.new(loc, self.type)
|
591
637
|
expect_token(:RBRACKET)
|
592
638
|
type
|
593
639
|
end
|
594
640
|
|
595
641
|
def named_type
|
596
|
-
Nodes::NamedType.new(name)
|
642
|
+
Nodes::NamedType.new(pos, name)
|
597
643
|
end
|
598
644
|
|
599
645
|
def variable
|
600
646
|
return unless at?(:VAR_SIGN)
|
647
|
+
loc = pos
|
601
648
|
accept_token
|
602
|
-
Nodes::Variable.new name
|
649
|
+
Nodes::Variable.new loc, name
|
603
650
|
end
|
604
651
|
|
605
652
|
def name
|
606
653
|
case token_name
|
607
|
-
when :IDENTIFIER
|
654
|
+
when :IDENTIFIER then accept_token_value
|
655
|
+
when :TYPE then
|
656
|
+
accept_token
|
657
|
+
"type"
|
658
|
+
when :QUERY then
|
659
|
+
accept_token
|
660
|
+
"query"
|
661
|
+
when :INPUT then
|
662
|
+
accept_token
|
663
|
+
"input"
|
608
664
|
else
|
609
665
|
expect_token_value(:IDENTIFIER)
|
610
666
|
end
|
611
667
|
end
|
612
668
|
|
613
669
|
def accept_token
|
614
|
-
@lexer.advance
|
615
|
-
@token_name = @lexer.token_name
|
670
|
+
@token_name = @lexer.advance
|
616
671
|
end
|
617
672
|
|
618
673
|
# Only use when we care about the accepted token's value
|
@@ -636,6 +691,12 @@ module TinyGQL
|
|
636
691
|
token_value
|
637
692
|
end
|
638
693
|
|
694
|
+
def expect_string_value
|
695
|
+
token_value = @lexer.string_value
|
696
|
+
expect_token :STRING
|
697
|
+
token_value
|
698
|
+
end
|
699
|
+
|
639
700
|
def expect_tokens toks
|
640
701
|
token_value = @lexer.token_value
|
641
702
|
unless toks.any? { |tok| at?(tok) }
|
data/lib/tinygql/version.rb
CHANGED
data/test/lexer_test.rb
CHANGED
@@ -101,5 +101,83 @@ eod
|
|
101
101
|
[:RCURLY, "}"],
|
102
102
|
[:RCURLY, "}"]], toks
|
103
103
|
end
|
104
|
+
|
105
|
+
def test_lex_4
|
106
|
+
words = ["true", "null", "enum", "type"]
|
107
|
+
doc = words.join(" ")
|
108
|
+
|
109
|
+
lexer = Lexer.new doc
|
110
|
+
toks = []
|
111
|
+
while tok = lexer.next_token
|
112
|
+
toks << tok
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_lex_5
|
119
|
+
words = ["input", "false", "query", "union"]
|
120
|
+
doc = words.join(" ")
|
121
|
+
|
122
|
+
lexer = Lexer.new doc
|
123
|
+
toks = []
|
124
|
+
while tok = lexer.next_token
|
125
|
+
toks << tok
|
126
|
+
end
|
127
|
+
|
128
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_lex_6
|
132
|
+
words = ["extend", "scalar", "schema"]
|
133
|
+
doc = words.join(" ")
|
134
|
+
|
135
|
+
lexer = Lexer.new doc
|
136
|
+
toks = []
|
137
|
+
while tok = lexer.next_token
|
138
|
+
toks << tok
|
139
|
+
end
|
140
|
+
|
141
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_lex_8
|
145
|
+
words = ["mutation", "fragment"]
|
146
|
+
doc = words.join(" ")
|
147
|
+
|
148
|
+
lexer = Lexer.new doc
|
149
|
+
toks = []
|
150
|
+
while tok = lexer.next_token
|
151
|
+
toks << tok
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_lex_9
|
158
|
+
words = ["interface", "directive"]
|
159
|
+
doc = words.join(" ")
|
160
|
+
|
161
|
+
lexer = Lexer.new doc
|
162
|
+
toks = []
|
163
|
+
while tok = lexer.next_token
|
164
|
+
toks << tok
|
165
|
+
end
|
166
|
+
|
167
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_kw_lex
|
171
|
+
words = ["on", "fragment", "true", "false", "null", "query", "mutation", "subscription", "schema", "scalar", "type", "extend", "implements", "interface", "union", "enum", "input", "directive", "repeatable"]
|
172
|
+
doc = words.join(" ")
|
173
|
+
|
174
|
+
lexer = Lexer.new doc
|
175
|
+
toks = []
|
176
|
+
while tok = lexer.next_token
|
177
|
+
toks << tok
|
178
|
+
end
|
179
|
+
|
180
|
+
assert_equal words.map { |x| [x.upcase.to_sym, x] }, toks
|
181
|
+
end
|
104
182
|
end
|
105
183
|
end
|
data/test/parser_test.rb
CHANGED
@@ -46,6 +46,23 @@ eod
|
|
46
46
|
assert_equal ["likeStory", "story", "likeCount"], ast.find_all(&:field?).map(&:name)
|
47
47
|
end
|
48
48
|
|
49
|
+
def test_has_position_and_line
|
50
|
+
doc = <<-eod
|
51
|
+
mutation {
|
52
|
+
likeStory(sturyID: 12345) {
|
53
|
+
story {
|
54
|
+
likeCount
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
eod
|
59
|
+
parser = Parser.new doc
|
60
|
+
ast = parser.parse
|
61
|
+
expected = ["likeStory", "story", "likeCount"].map { |str| doc.index(str) + str.bytesize }
|
62
|
+
assert_equal expected, ast.find_all(&:field?).map(&:pos)
|
63
|
+
assert_equal [2, 3, 4], ast.find_all(&:field?).map { |n| n.line(doc) }
|
64
|
+
end
|
65
|
+
|
49
66
|
def test_field_alias
|
50
67
|
doc = <<-eod
|
51
68
|
mutation {
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tinygql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Patterson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-08-
|
11
|
+
date: 2023-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|