forked from Roblox/graphql-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
1654 lines (1542 loc) · 47.6 KB
/
parser.lua
File metadata and controls
1654 lines (1542 loc) · 47.6 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
* Copyright (c) GraphQL Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
]]
-- ROBLOX upstream: https://github.com/graphql/graphql-js/blob/1951bce42092123e844763b6a8e985a8a3327511/src/language/parser.js
--!strict
local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
type Array<T> = LuauPolyfill.Array<T>
type void = nil
local errorImport = require("../error")
type GraphQLError = errorImport.GraphQLError
local TokenKindModule = require("./tokenKind")
type TokenKindEnum = TokenKindModule.TokenKindEnum
local AstModule = require("./ast")
local Location = AstModule.Location
type DocumentNode = AstModule.DocumentNode
type NonNullTypeNode = AstModule.NonNullTypeNode
type NameNode = AstModule.NameNode
type ListTypeNode = AstModule.ListTypeNode
type DefinitionNode = AstModule.DefinitionNode
type OperationDefinitionNode = AstModule.OperationDefinitionNode
type OperationTypeNode = AstModule.OperationTypeNode
type VariableDefinitionNode = AstModule.VariableDefinitionNode
type VariableNode = AstModule.VariableNode
type SelectionSetNode = AstModule.SelectionSetNode
type SelectionNode = AstModule.SelectionNode
type FieldNode = AstModule.FieldNode
type ArgumentNode = AstModule.ArgumentNode
type ValueNode = AstModule.ValueNode
type StringValueNode = AstModule.StringValueNode
type ListValueNode = AstModule.ListValueNode
type ObjectValueNode = AstModule.ObjectValueNode
type ObjectFieldNode = AstModule.ObjectFieldNode
type DirectiveNode = AstModule.DirectiveNode
type TypeNode = AstModule.TypeNode
type NamedTypeNode = AstModule.NamedTypeNode
type TypeSystemDefinitionNode = AstModule.TypeSystemDefinitionNode
type SchemaDefinitionNode = AstModule.SchemaDefinitionNode
type OperationTypeDefinitionNode = AstModule.OperationTypeDefinitionNode
type ScalarTypeDefinitionNode = AstModule.ScalarTypeDefinitionNode
type ObjectTypeDefinitionNode = AstModule.ObjectTypeDefinitionNode
type FieldDefinitionNode = AstModule.FieldDefinitionNode
type InputValueDefinitionNode = AstModule.InputValueDefinitionNode
type InterfaceTypeDefinitionNode = AstModule.InterfaceTypeDefinitionNode
type UnionTypeDefinitionNode = AstModule.UnionTypeDefinitionNode
type EnumTypeDefinitionNode = AstModule.EnumTypeDefinitionNode
type EnumValueDefinitionNode = AstModule.EnumValueDefinitionNode
type InputObjectTypeDefinitionNode = AstModule.InputObjectTypeDefinitionNode
type TypeSystemExtensionNode = AstModule.TypeSystemExtensionNode
type SchemaExtensionNode = AstModule.SchemaExtensionNode
type ScalarTypeExtensionNode = AstModule.ScalarTypeExtensionNode
type ObjectTypeExtensionNode = AstModule.ObjectTypeExtensionNode
type InterfaceTypeExtensionNode = AstModule.InterfaceTypeExtensionNode
type UnionTypeExtensionNode = AstModule.UnionTypeExtensionNode
type EnumTypeExtensionNode = AstModule.EnumTypeExtensionNode
type InputObjectTypeExtensionNode = AstModule.InputObjectTypeExtensionNode
type DirectiveDefinitionNode = AstModule.DirectiveDefinitionNode
type FragmentDefinitionNode = AstModule.FragmentDefinitionNode
type FragmentSpreadNode = AstModule.FragmentSpreadNode
type InlineFragmentNode = AstModule.InlineFragmentNode
type Location = AstModule.Location
type Token = AstModule.Token
local sourceModule = require("./source")
local Source = sourceModule.Source
type Source = sourceModule.Source
local lexer = require("./lexer")
local Lexer = lexer.Lexer
type Lexer = lexer.Lexer
local isPunctuatorTokenKind = lexer.isPunctuatorTokenKind
local TokenKind = TokenKindModule.TokenKind
local DirectiveLocation = require("./directiveLocation").DirectiveLocation
local Kind = require("./kinds").Kind
local syntaxError = require("../error/syntaxError").syntaxError
--[[
* Configuration options to control parser behavior
*]]
export type ParseOptions = {
--[[/**
* By default, the parser creates AST nodes that know the location
* in the source that they correspond to. This configuration flag
* disables that behavior for performance or testing.
*/
]]
noLocation: boolean?,
--[[
* @deprecated will be removed in 17.0.0
*
* If enabled, the parser will understand and parse variable definitions
* contained in a fragment definition. They'll be represented in the
* `variableDefinitions` field of the FragmentDefinitionNode.
*
* The syntax is identical to normal, query-defined variables. For example:
*
* fragment A($var: Boolean = false) on T {
* ...
* }
*
*]]
experimentalFragmentVariables: boolean?,
}
-- deviation: pre-declare functions
local getTokenDesc
local getTokenKindDesc
type Parser = {
_options: ParseOptions?,
_lexer: Lexer,
new: (source: string | Source, options: ParseOptions?) -> Parser,
parseName: (self: Parser) -> NameNode,
parseDocument: (self: Parser) -> DocumentNode,
parseDefinition: (self: Parser) -> DefinitionNode,
parseOperationDefinition: (self: Parser) -> OperationDefinitionNode,
parseOperationType: (self: Parser) -> OperationTypeNode,
parseVariableDefinitions: (self: Parser) -> Array<VariableDefinitionNode>,
parseVariableDefinition: (self: Parser) -> VariableDefinitionNode,
parseVariable: (self: Parser) -> VariableNode,
parseSelectionSet: (self: Parser) -> SelectionSetNode,
parseSelection: (self: Parser) -> SelectionNode,
parseField: (self: Parser) -> FieldNode,
parseArguments: (self: Parser, isConst: boolean) -> Array<ArgumentNode>,
parseArgument: (self: Parser) -> ArgumentNode,
parseConstArgument: (self: Parser) -> ArgumentNode,
parseFragment: (self: Parser) -> FragmentSpreadNode | InlineFragmentNode,
parseFragmentDefinition: (self: Parser) -> FragmentDefinitionNode,
parseFragmentName: (self: Parser) -> NameNode,
parseValueLiteral: (self: Parser, isConst: boolean) -> ValueNode,
parseStringLiteral: (self: Parser) -> StringValueNode,
parseList: (self: Parser, isConst: boolean) -> ListValueNode,
parseObject: (self: Parser, isConst: boolean) -> ObjectValueNode,
parseObjectField: (self: Parser, isConst: boolean) -> ObjectFieldNode,
parseDirectives: (self: Parser, isConst: boolean) -> Array<DirectiveNode>,
parseDirective: (self: Parser, isConst: boolean) -> DirectiveNode,
parseTypeReference: (self: Parser) -> TypeNode,
parseNamedType: (self: Parser) -> NamedTypeNode,
parseTypeSystemDefinition: (self: Parser) -> TypeSystemDefinitionNode,
peekDescription: (self: Parser) -> boolean,
parseDescription: (self: Parser) -> void | StringValueNode,
parseSchemaDefinition: (self: Parser) -> SchemaDefinitionNode,
parseOperationTypeDefinition: (self: Parser) -> OperationTypeDefinitionNode,
parseScalarTypeDefinition: (self: Parser) -> ScalarTypeDefinitionNode,
parseObjectTypeDefinition: (self: Parser) -> ObjectTypeDefinitionNode,
parseImplementsInterfaces: (self: Parser) -> Array<NamedTypeNode>,
parseFieldsDefinition: (self: Parser) -> Array<FieldDefinitionNode>,
parseFieldDefinition: (self: Parser) -> FieldDefinitionNode,
parseArgumentDefs: (self: Parser) -> Array<InputValueDefinitionNode>,
parseInputValueDef: (self: Parser) -> InputValueDefinitionNode,
parseInterfaceTypeDefinition: (self: Parser) -> InterfaceTypeDefinitionNode,
parseUnionTypeDefinition: (self: Parser) -> UnionTypeDefinitionNode,
parseUnionMemberTypes: (self: Parser) -> Array<NamedTypeNode>,
parseEnumTypeDefinition: (self: Parser) -> EnumTypeDefinitionNode,
parseEnumValuesDefinition: (self: Parser) -> Array<EnumValueDefinitionNode>,
parseEnumValueDefinition: (self: Parser) -> EnumValueDefinitionNode,
parseInputObjectTypeDefinition: (self: Parser) -> InputObjectTypeDefinitionNode,
parseInputFieldsDefinition: (self: Parser) -> Array<InputValueDefinitionNode>,
parseTypeSystemExtension: (self: Parser) -> TypeSystemExtensionNode,
parseSchemaExtension: (self: Parser) -> SchemaExtensionNode,
parseScalarTypeExtension: (self: Parser) -> ScalarTypeExtensionNode,
parseObjectTypeExtension: (self: Parser) -> ObjectTypeExtensionNode,
parseInterfaceTypeExtension: (self: Parser) -> InterfaceTypeExtensionNode,
parseUnionTypeExtension: (self: Parser) -> UnionTypeExtensionNode,
parseEnumTypeExtension: (self: Parser) -> EnumTypeExtensionNode,
parseInputObjectTypeExtension: (self: Parser) -> InputObjectTypeExtensionNode,
parseDirectiveDefinition: (self: Parser) -> DirectiveDefinitionNode,
parseDirectiveLocations: (self: Parser) -> Array<NameNode>,
parseDirectiveLocation: (self: Parser) -> NameNode,
loc: (self: Parser, startToken: Token) -> Location | void,
peek: (self: Parser, kind: TokenKindEnum) -> boolean,
expectToken: (self: Parser, kind: TokenKindEnum) -> Token,
expectOptionalToken: (self: Parser, kind: TokenKindEnum) -> Token?,
expectKeyword: (self: Parser, value: string) -> (),
expectOptionalKeyword: (self: Parser, value: string) -> boolean,
unexpected: (self: Parser, atToken: Token?) -> GraphQLError,
any: <T>(
self: Parser,
openKind: TokenKindEnum,
parseFn: (self: Parser) -> T,
closeKind: TokenKindEnum
) -> Array<T>,
optionalMany: <T>(
self: Parser,
openKind: TokenKindEnum,
parseFn: (self: Parser) -> T,
closeKind: TokenKindEnum
) -> Array<T>,
many: <T>(
self: Parser,
openKind: TokenKindEnum,
parseFn: (self: Parser) -> T,
closeKind: TokenKindEnum
) -> Array<T>,
delimitedMany: <T>(
self: Parser,
delimiterKind: TokenKindEnum,
parseFn: (self: Parser) -> T
) -> Array<T>,
}
local Parser: Parser = {} :: Parser;
(Parser :: any).__index = Parser
--[[*
-- * Given a GraphQL source, parses it into a Document.
-- * Throws GraphQLError if a syntax error is encountered.
-- *]]
local function parse(source: string | Source, options: ParseOptions?): DocumentNode
local parser = Parser.new(source, options)
return parser:parseDocument()
end
--[[
-- * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
-- * that value.
-- * Throws GraphQLError if a syntax error is encountered.
-- *
-- * This is useful within tools that operate upon GraphQL Values directly and
-- * in isolation of complete GraphQL documents.
-- *
-- * Consider providing the results to the utility function: valueFromAST().
]]
local function parseValue(source: string | Source, options: ParseOptions?): ValueNode
local parser = Parser.new(source, options)
parser:expectToken(TokenKind.SOF)
local value = parser:parseValueLiteral(false)
parser:expectToken(TokenKind.EOF)
return value
end
--[[*
-- * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
-- * that type.
-- * Throws GraphQLError if a syntax error is encountered.
-- *
-- * This is useful within tools that operate upon GraphQL Types directly and
-- * in isolation of complete GraphQL documents.
-- *
-- * Consider providing the results to the utility function: typeFromAST().
-- *]]
local function parseType(source: string | Source, options: ParseOptions?): TypeNode
local parser = Parser.new(source, options)
parser:expectToken(TokenKind.SOF)
local type_ = parser:parseTypeReference()
parser:expectToken(TokenKind.EOF)
return type_
end
function Parser.new(source: string | Source, options: ParseOptions?): Parser
local sourceObj
if typeof(source) == "string" then
sourceObj = Source.new(source)
else
sourceObj = source
end
local self = {}
self._lexer = Lexer.new(sourceObj)
self._options = options
return (setmetatable(self, Parser) :: any) :: Parser
end
--[[*
-- * Converts a name lex token into a name parse node.
-- *]]
function Parser:parseName(): NameNode
local token = self:expectToken(TokenKind.NAME)
return {
kind = Kind.NAME,
value = token.value,
loc = self:loc(token),
}
end
-- Implements the parsing rules in the Document section.
--[[*
-- * Document : Definition+
-- *]]
function Parser:parseDocument(): DocumentNode
local start = self._lexer.token
return {
kind = Kind.DOCUMENT,
definitions = self:many(TokenKind.SOF, self.parseDefinition, TokenKind.EOF),
loc = self:loc(start),
}
end
--[[*
-- * Definition :
-- * - ExecutableDefinition
-- * - TypeSystemDefinition
-- * - TypeSystemExtension
-- *
-- * ExecutableDefinition :
-- * - OperationDefinition
-- * - FragmentDefinition
-- *]]
function Parser:parseDefinition(): DefinitionNode
if self:peek(TokenKind.NAME) then
local tokenValue = self._lexer.token.value
if tokenValue == "query" or tokenValue == "mutation" or tokenValue == "subscription" then
return self:parseOperationDefinition()
elseif tokenValue == "fragment" then
return self:parseFragmentDefinition()
elseif
tokenValue == "schema"
or tokenValue == "scalar"
or tokenValue == "type"
or tokenValue == "interface"
or tokenValue == "union"
or tokenValue == "enum"
or tokenValue == "input"
or tokenValue == "directive"
then
return self:parseTypeSystemDefinition()
elseif tokenValue == "extend" then
return self:parseTypeSystemExtension()
end
elseif self:peek(TokenKind.BRACE_L) then
return self:parseOperationDefinition()
elseif self:peekDescription() then
return self:parseTypeSystemDefinition()
end
error(self:unexpected())
end
-- Implements the parsing rules in the Operations section.
--[[*
-- * OperationDefinition :
-- * - SelectionSet
-- * - OperationType Name? VariableDefinitions? Directives? SelectionSet
-- *]]
function Parser:parseOperationDefinition(): OperationDefinitionNode
local start = self._lexer.token
if self:peek(TokenKind.BRACE_L) then
return {
kind = Kind.OPERATION_DEFINITION,
operation = "query",
name = nil,
variableDefinitions = {},
directives = {},
selectionSet = self:parseSelectionSet(),
loc = self:loc(start),
}
end
-- ROBLOX FIXME Luau: both casts are needed to prevent singleton string union from widening to plain string type
local operation: OperationTypeNode = self:parseOperationType() :: OperationTypeNode
local name
if self:peek(TokenKind.NAME) then
name = self:parseName()
end
return {
kind = Kind.OPERATION_DEFINITION,
operation = operation,
name = name,
variableDefinitions = self:parseVariableDefinitions(),
directives = self:parseDirectives(false),
selectionSet = self:parseSelectionSet(),
loc = self:loc(start),
}
end
--[[*
-- * OperationType : one of query mutation subscription
-- *]]
function Parser:parseOperationType(): OperationTypeNode
local operationToken = self:expectToken(TokenKind.NAME)
if operationToken.value == "query" then
return "query"
elseif operationToken.value == "mutation" then
return "mutation"
elseif operationToken.value == "subscription" then
return "subscription"
end
error(self:unexpected(operationToken))
end
--[[*
-- * VariableDefinitions : ( VariableDefinition+ )
-- *]]
function Parser:parseVariableDefinitions(): Array<VariableDefinitionNode>
return self:optionalMany(TokenKind.PAREN_L, self.parseVariableDefinition, TokenKind.PAREN_R)
end
--[[*
-- * VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
-- *]]
function Parser:parseVariableDefinition(): VariableDefinitionNode
local start = self._lexer.token
local _ref0 = self:parseVariable()
self:expectToken(TokenKind.COLON)
local _ref1 = self:parseTypeReference()
return {
kind = Kind.VARIABLE_DEFINITION,
variable = _ref0,
type = _ref1,
defaultValue = (if self:expectOptionalToken(TokenKind.EQUALS)
then self:parseValueLiteral(true)
else nil),
directives = self:parseDirectives(true),
loc = self:loc(start),
}
end
--[[*
-- * Variable : $ Name
-- *]]
function Parser:parseVariable(): VariableNode
local start = self._lexer.token
self:expectToken(TokenKind.DOLLAR)
return {
kind = Kind.VARIABLE,
name = self:parseName(),
loc = self:loc(start),
}
end
--[[*
-- * SelectionSet : { Selection+ }
-- *]]
function Parser:parseSelectionSet(): SelectionSetNode
local start = self._lexer.token
return {
kind = Kind.SELECTION_SET,
selections = self:many(TokenKind.BRACE_L, self.parseSelection, TokenKind.BRACE_R),
loc = self:loc(start),
}
end
--[[*
-- * Selection :
-- * - Field
-- * - FragmentSpread
-- * - InlineFragment
-- *]]
function Parser:parseSelection(): SelectionNode
if self:peek(TokenKind.SPREAD) then
return self:parseFragment()
else
return self:parseField()
end
end
--[[*
-- * Field : Alias? Name Arguments? Directives? SelectionSet?
-- *
-- * Alias : Name :
-- *]]
function Parser:parseField(): FieldNode
local start = self._lexer.token
local nameOrAlias = self:parseName()
local alias
-- ROBLOX FIXME Luau: Luau infers `NameNode?` here because without type states it doesn't understand both branches assign NameNode to the variable?
local name
if self:expectOptionalToken(TokenKind.COLON) then
alias = nameOrAlias
name = self:parseName()
else
name = nameOrAlias
end
return {
kind = Kind.FIELD,
alias = alias,
name = name,
arguments = self:parseArguments(false),
directives = self:parseDirectives(false),
selectionSet = self:peek(TokenKind.BRACE_L) and self:parseSelectionSet() or nil,
loc = self:loc(start),
}
end
--[[*
-- * Arguments[Const] : ( Argument[?Const]+ )
-- *]]
function Parser:parseArguments(isConst: boolean): Array<ArgumentNode>
local item
if isConst then
item = self.parseConstArgument
else
item = self.parseArgument
end
return self:optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R)
end
--[[*
-- * Argument[Const] : Name : Value[?Const]
-- *]]
function Parser:parseArgument(): ArgumentNode
local start = self._lexer.token
local name = self:parseName()
self:expectToken(TokenKind.COLON)
return {
kind = Kind.ARGUMENT,
name = name,
value = self:parseValueLiteral(false),
loc = self:loc(start),
}
end
function Parser:parseConstArgument(): ArgumentNode
local start = self._lexer.token
local _ref0 = self:parseName()
self:expectToken(TokenKind.COLON)
local _ref1 = self:parseValueLiteral(true)
return {
kind = Kind.ARGUMENT,
name = _ref0,
value = _ref1,
loc = self:loc(start),
}
end
-- Implements the parsing rules in the Fragments section.
--[[*
-- * Corresponds to both FragmentSpread and InlineFragment in the spec.
-- *
-- * FragmentSpread : ... FragmentName Directives?
-- *
-- * InlineFragment : ... TypeCondition? Directives? SelectionSet
-- *]]
function Parser:parseFragment(): FragmentSpreadNode | InlineFragmentNode
local start = self._lexer.token
self:expectToken(TokenKind.SPREAD)
local hasTypeCondition = self:expectOptionalKeyword("on")
if not hasTypeCondition and self:peek(TokenKind.NAME) then
return {
kind = Kind.FRAGMENT_SPREAD,
name = self:parseFragmentName(),
directives = self:parseDirectives(false),
loc = self:loc(start),
}
end
return {
kind = Kind.INLINE_FRAGMENT,
typeCondition = if hasTypeCondition then self:parseNamedType() else nil,
directives = self:parseDirectives(false),
selectionSet = self:parseSelectionSet(),
loc = self:loc(start),
}
end
--[[*
-- * FragmentDefinition :
-- * - fragment FragmentName on TypeCondition Directives? SelectionSet
-- *
-- * TypeCondition : NamedType
-- *]]
function Parser:parseFragmentDefinition(): FragmentDefinitionNode
local start = self._lexer.token
self:expectKeyword("fragment")
-- Experimental support for defining variables within fragments changes
-- the grammar of FragmentDefinition:
-- - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet
if (self._options and self._options.experimentalFragmentVariables) == true then
local name = self:parseFragmentName()
local variableDefinitions = self:parseVariableDefinitions()
self:expectKeyword("on")
local typeConditions = self:parseNamedType()
local directives = self:parseDirectives(false)
local selectionSet = self:parseSelectionSet()
local loc = self:loc(start)
return {
kind = Kind.FRAGMENT_DEFINITION,
name = name,
variableDefinitions = variableDefinitions,
typeCondition = typeConditions,
directives = directives,
selectionSet = selectionSet,
loc = loc,
}
end
local name = self:parseFragmentName()
self:expectKeyword("on")
local typeCondition = self:parseNamedType()
local directives = self:parseDirectives(false)
local selectionSet = self:parseSelectionSet()
local loc = self:loc(start)
return {
kind = Kind.FRAGMENT_DEFINITION,
name = name,
typeCondition = typeCondition,
directives = directives,
selectionSet = selectionSet,
loc = loc,
}
end
--[[*
-- * FragmentName : Name but not `on`
-- *]]
function Parser:parseFragmentName(): NameNode
if self._lexer.token.value == "on" then
error(self:unexpected())
end
return self:parseName()
end
-- Implements the parsing rules in the Values section.
--[[*
-- * Value[Const] :
-- * - [~Const] Variable
-- * - IntValue
-- * - FloatValue
-- * - StringValue
-- * - BooleanValue
-- * - NullValue
-- * - EnumValue
-- * - ListValue[?Const]
-- * - ObjectValue[?Const]
-- *
-- * BooleanValue : one of `true` `false`
-- *
-- * NullValue : `null`
-- *
-- * EnumValue : Name but not `true`, `false` or `null`
-- *]]
function Parser:parseValueLiteral(isConst: boolean): ValueNode
local token = self._lexer.token
local kind = token.kind
if kind == TokenKind.BRACKET_L then
return self:parseList(isConst)
elseif kind == TokenKind.BRACE_L then
return self:parseObject(isConst)
elseif kind == TokenKind.INT then
self._lexer:advance()
return {
kind = Kind.INT,
value = token.value,
loc = self:loc(token),
}
elseif kind == TokenKind.FLOAT then
self._lexer:advance()
return {
kind = Kind.FLOAT,
value = token.value,
loc = self:loc(token),
}
elseif kind == TokenKind.STRING or kind == TokenKind.BLOCK_STRING then
return self:parseStringLiteral()
elseif kind == TokenKind.NAME then
self._lexer:advance()
local tokenValue = token.value
if tokenValue == "true" then
return { kind = Kind.BOOLEAN, value = true, loc = self:loc(token) }
elseif tokenValue == "false" then
return { kind = Kind.BOOLEAN, value = false, loc = self:loc(token) }
elseif tokenValue == "null" then
return { kind = Kind.NULL, loc = self:loc(token) }
else
return {
kind = Kind.ENUM,
value = tokenValue,
loc = self:loc(token),
}
end
elseif kind == TokenKind.DOLLAR then
if not isConst then
return self:parseVariable()
end
-- break
end
error(self:unexpected())
end
function Parser:parseStringLiteral(): StringValueNode
local token = self._lexer.token
self._lexer:advance()
return {
kind = Kind.STRING,
value = token.value,
block = token.kind == TokenKind.BLOCK_STRING,
loc = self:loc(token),
}
end
--[[*
-- * ListValue[Const] :
-- * - [ ]
-- * - [ Value[?Const]+ ]
-- *]]
function Parser:parseList(isConst: boolean): ListValueNode
local start = self._lexer.token
local item = function()
return self:parseValueLiteral(isConst)
end
return {
kind = Kind.LIST,
values = self:any(TokenKind.BRACKET_L, item, TokenKind.BRACKET_R),
loc = self:loc(start),
}
end
--[[*
-- * ObjectValue[Const] :
-- * - { }
-- * - { ObjectField[?Const]+ }
-- *]]
function Parser:parseObject(isConst: boolean): ObjectValueNode
local start = self._lexer.token
local item = function()
return self:parseObjectField(isConst)
end
return {
kind = Kind.OBJECT,
fields = self:any(TokenKind.BRACE_L, item, TokenKind.BRACE_R),
loc = self:loc(start),
}
end
--[[*
-- * ObjectField[Const] : Name : Value[?Const]
-- *]]
function Parser:parseObjectField(isConst: boolean): ObjectFieldNode
local start = self._lexer.token
local name = self:parseName()
self:expectToken(TokenKind.COLON)
return {
kind = Kind.OBJECT_FIELD,
name = name,
value = self:parseValueLiteral(isConst),
loc = self:loc(start),
}
end
function Parser:parseDirectives(isConst)
local directives = {}
while self:peek(TokenKind.AT) do
table.insert(directives, self:parseDirective(isConst))
end
return directives
end
--[[*
-- * Directive[Const] : @ Name Arguments[?Const]?
-- *]]
function Parser:parseDirective(isConst)
local start = self._lexer.token
self:expectToken(TokenKind.AT)
return {
kind = Kind.DIRECTIVE,
name = self:parseName(),
arguments = self:parseArguments(isConst),
loc = self:loc(start),
}
end
-- Implements the parsing rules in the Types section.
--[[*
-- * Type :
-- * - NamedType
-- * - ListType
-- * - NonNullType
-- *]]
function Parser:parseTypeReference(): TypeNode
local start = self._lexer.token
-- ROBLOX FIXME Luau: Luau should infer this annotation, needs type states
local type_: ListTypeNode | NamedTypeNode
if self:expectOptionalToken(TokenKind.BRACKET_L) then
local innerType = self:parseTypeReference()
self:expectToken(TokenKind.BRACKET_R)
type_ = {
kind = Kind.LIST_TYPE,
type = innerType,
loc = self:loc(start),
-- ROBLOX deviation START: upstream 16.x and main needed a template type here, so we'll narrow with a manual cast
} :: ListTypeNode
-- ROBLOX deviation END
else
type_ = self:parseNamedType()
end
if self:expectOptionalToken(TokenKind.BANG) then
return {
kind = Kind.NON_NULL_TYPE,
type = type_,
loc = self:loc(start),
-- ROBLOX deviation START: upstream 16.x and main needed a template type here, so we'll narrow with a manual cast
} :: NonNullTypeNode
-- ROBLOX deviation END
end
return type_
end
--[[*
-- * NamedType : Name
-- *]]
function Parser:parseNamedType(): NamedTypeNode
local start = self._lexer.token
return {
kind = Kind.NAMED_TYPE,
name = self:parseName(),
loc = self:loc(start),
}
end
--[[*
-- * TypeSystemDefinition :
-- * - SchemaDefinition
-- * - TypeDefinition
-- * - DirectiveDefinition
-- *
-- * TypeDefinition :
-- * - ScalarTypeDefinition
-- * - ObjectTypeDefinition
-- * - InterfaceTypeDefinition
-- * - UnionTypeDefinition
-- * - EnumTypeDefinition
-- * - InputObjectTypeDefinition
-- *]]
function Parser:parseTypeSystemDefinition()
-- Many definitions begin with a description and require a lookahead.
local keywordToken = if self:peekDescription()
then self._lexer:lookahead()
else self._lexer.token
if keywordToken.kind == TokenKind.NAME then
local tokenValue = keywordToken.value
if tokenValue == "schema" then
return self:parseSchemaDefinition()
elseif tokenValue == "scalar" then
return self:parseScalarTypeDefinition()
elseif tokenValue == "type" then
return self:parseObjectTypeDefinition()
elseif tokenValue == "interface" then
return self:parseInterfaceTypeDefinition()
elseif tokenValue == "union" then
return self:parseUnionTypeDefinition()
elseif tokenValue == "enum" then
return self:parseEnumTypeDefinition()
elseif tokenValue == "input" then
return self:parseInputObjectTypeDefinition()
elseif tokenValue == "directive" then
return self:parseDirectiveDefinition()
end
end
error(self:unexpected(keywordToken))
end
function Parser:peekDescription(): boolean
return self:peek(TokenKind.STRING) or self:peek(TokenKind.BLOCK_STRING)
end
--[[*
-- * Description : StringValue
-- *]]
function Parser:parseDescription()
if self:peekDescription() then
return self:parseStringLiteral()
end
return -- ROBLOX deviation: no implicit returns
end
--[[*
-- * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
-- *]]
function Parser:parseSchemaDefinition()
local start = self._lexer.token
local description = self:parseDescription()
self:expectKeyword("schema")
local directives = self:parseDirectives(true)
local operationTypes = self:many(
TokenKind.BRACE_L,
self.parseOperationTypeDefinition,
TokenKind.BRACE_R
)
return {
kind = Kind.SCHEMA_DEFINITION,
description = description,
directives = directives,
operationTypes = operationTypes,
loc = self:loc(start),
}
end
--[[*
-- * OperationTypeDefinition : OperationType : NamedType
-- *]]
function Parser:parseOperationTypeDefinition(): OperationTypeDefinitionNode
local start = self._lexer.token
-- ROBLOX FIXME Luau: both casts are needed to prevent singleton string union from widening to plain string type
local operation: OperationTypeNode = self:parseOperationType() :: OperationTypeNode
self:expectToken(TokenKind.COLON)
local type_ = self:parseNamedType()
return {
kind = Kind.OPERATION_TYPE_DEFINITION,
operation = operation,
type = type_,
loc = self:loc(start),
}
end
--[[*
-- * ScalarTypeDefinition : Description? scalar Name Directives[Const]?
-- *]]
function Parser:parseScalarTypeDefinition()
local start = self._lexer.token
local description = self:parseDescription()
self:expectKeyword("scalar")
local name = self:parseName()
local directives = self:parseDirectives(true)
return {
kind = Kind.SCALAR_TYPE_DEFINITION,
description = description,
name = name,
directives = directives,
loc = self:loc(start),
}
end
--[[*
-- * ObjectTypeDefinition :
-- * Description?
-- * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
-- *]]
function Parser:parseObjectTypeDefinition()
local start = self._lexer.token
local description = self:parseDescription()
self:expectKeyword("type")
local name = self:parseName()
local interfaces = self:parseImplementsInterfaces()
local directives = self:parseDirectives(true)
local fields = self:parseFieldsDefinition()
return {
kind = Kind.OBJECT_TYPE_DEFINITION,
description = description,
name = name,
interfaces = interfaces,
directives = directives,
fields = fields,
loc = self:loc(start),
}
end
--[[*
-- * ImplementsInterfaces :
-- * - implements `&`? NamedType
-- * - ImplementsInterfaces & NamedType
-- *]]
function Parser:parseImplementsInterfaces(): Array<any>
if self:expectOptionalKeyword("implements") then
return self:delimitedMany(TokenKind.AMP, self.parseNamedType)
else
return {}
end
end
--[[*
-- * FieldsDefinition : { FieldDefinition+ }
-- *]]
function Parser:parseFieldsDefinition()
return self:optionalMany(TokenKind.BRACE_L, self.parseFieldDefinition, TokenKind.BRACE_R)
end
--[[*
-- * FieldDefinition :
-- * - Description? Name ArgumentsDefinition? : Type Directives[Const]?
-- *]]
function Parser:parseFieldDefinition()
local start = self._lexer.token
local description = self:parseDescription()
local name = self:parseName()
local args = self:parseArgumentDefs()
self:expectToken(TokenKind.COLON)
local type_ = self:parseTypeReference()
local directives = self:parseDirectives(true)
return {
kind = Kind.FIELD_DEFINITION,
description = description,
name = name,
arguments = args,
type = type_,
directives = directives,
loc = self:loc(start),