/
Parser.purs
758 lines (628 loc) · 28 KB
/
Parser.purs
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
module Data.GraphQL.Parser where
import Prelude hiding (between)
import Control.Alt ((<|>))
import Control.Lazy (fix)
import Data.Array (many, singleton, some)
import Data.Enum (toEnum)
import Data.Foldable (fold)
import Data.GraphQL.AST as AST
import Data.Int as DI
import Data.List (List(..), (:))
import Data.List as L
import Data.Maybe (Maybe(..), maybe)
import Data.Number as DN
import Data.String.CodePoints as CP
import Data.String.CodeUnits (fromCharArray)
import Data.Traversable (sequence)
import Parsing (Parser, ParserT, fail)
import Parsing.Combinators (between, lookAhead, option, optional, sepBy1, try, (<?>))
import Parsing.String (anyChar, char, string)
import Parsing.String.Basic (noneOf, oneOf)
-------
-- util
-------
c2str ∷ Char → Parser String String
c2str = pure <<< fromCharArray <<< singleton
ca2str ∷ Array Char → Parser String String
ca2str = pure <<< fromCharArray
toCA ∷ Char → Parser String (Array Char)
toCA = pure <<< singleton
-- | Parse phrases delimited and optionally terminated by a separator.
sepEndBy_ :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
sepEndBy_ p sep = sepEndBy1_ p sep <|> pure Nil
-- | Parse phrases delimited and optionally terminated by a separator, requiring at least one match.
sepEndBy1_ :: forall m s a sep. Monad m => ParserT s m a -> ParserT s m sep -> ParserT s m (List a)
sepEndBy1_ p sep = do
a <- p
(do _ <- sep
as <- sepEndBy_ p sep
pure (a : as)) <|> pure (L.singleton a)
--------------
-- chars
--------------
upper :: Array Char
upper = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]
lower :: Array Char
lower = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ]
digits :: Array Char
digits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
nonZeroDigits :: Array Char
nonZeroDigits = [ '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
--------------
-- ignore
--------------
-- also needs unicode bom for ignore, this will fail on unicode...
whitespace ∷ Parser String Unit
whitespace = void $ oneOf [ ' ', '\t' ]
comment ∷ Parser String Unit
comment = void $ char '#' *> many (noneOf [ '\n' ])
comma ∷ Parser String Unit
comma = void $ char ','
lineTerminator ∷ Parser String Unit
lineTerminator = void $ char '\n'
ignorable ∷ Parser String Unit
ignorable = lineTerminator <|> comma <|> comment <|> whitespace
ignoreMe ∷ Parser String Unit
ignoreMe = void $ many ignorable
ignoreMe' ∷ Parser String Unit
ignoreMe' = void $ some ignorable
--------------
-- primitives
--------------
name ∷ Parser String String
name =
fromCharArray
<$> ( (<>)
<$> (oneOf (upper <> lower <> [ '_' ]) >>= toCA)
<*> (many (oneOf $ upper <> lower <> digits <> [ '_' ]))
)
description ∷ Parser String String
description = stringValue >>= (\(AST.StringValue s) → pure s)
negativeSign ∷ Parser String String
negativeSign = char '-' >>= c2str
ip0 ∷ Parser String String
ip0 = (<>) <$> (option "" negativeSign) <*> (char '0' >>= c2str)
ipOther ∷ Parser String String
ipOther =
fold
<$> sequence
[ option "" negativeSign
, oneOf nonZeroDigits >>= c2str
, many (oneOf digits) >>= ca2str
]
integerPart ∷ Parser String String
integerPart = (try ip0) <|> ipOther
intValue ∷ Parser String AST.IntValue
intValue = integerPart >>= maybe (fail "String not an int") (pure <<< AST.IntValue) <<< DI.fromString
fractionalPart ∷ Parser String String
fractionalPart =
(<>)
<$> (char '.' >>= c2str)
<*> (many (oneOf digits) >>= ca2str)
floatValueFrac ∷ Parser String String
floatValueFrac =
(<>)
<$> integerPart
<*> fractionalPart
exponentPart ∷ Parser String String
exponentPart =
fold
<$> sequence
[ oneOf [ 'e', 'E' ] >>= c2str
, option "" (oneOf [ '+', '-' ] >>= c2str)
, some (oneOf digits) >>= ca2str
]
floatValueExp ∷ Parser String String
floatValueExp = (<>) <$> integerPart <*> exponentPart
floatValueFracExp ∷ Parser String String
floatValueFracExp =
fold
<$> sequence [ integerPart, fractionalPart, exponentPart ]
floatValue ∷ Parser String AST.FloatValue
floatValue = (try floatValueFracExp <|> try floatValueExp <|> floatValueFrac) >>= maybe (fail "String not a float") (pure <<< AST.FloatValue) <<< DN.fromString
singleQuote ∷ Parser String String
singleQuote = char '"' >>= c2str
tripleQuote ∷ Parser String String
tripleQuote = sequence [ char '"', char '"', char '"' ] >>= ca2str
uni ∷ Parser String Char
uni = oneOf (digits <> [ 'A', 'B', 'C', 'D', 'E', 'F' ] <> [ 'a', 'b', 'c', 'd', 'e', 'f' ])
simpleUnescapedString ∷ Parser String String
simpleUnescapedString = noneOf [ '\\', '"', '\n' ] >>= c2str
simpleUnicodeString ∷ Parser String String
simpleUnicodeString =
(sequence [ char '\\' *> char 'u' *> uni, uni, uni, uni ]) >>= ca2str
>>= ( maybe
(fail "Unrepresentable code point")
( maybe
(fail "Unrepresentable code point")
(pure <<< CP.singleton <<< CP.codePointFromChar)
<<< toEnum
)
<<< DI.fromStringAs DI.hexadecimal
)
simpleEscapedString ∷ Parser String String
simpleEscapedString =
char '\\' *> oneOf [ '"', '\\', '/', 'b', 'f', 'n', 'r', 't' ]
>>= ( \x → case x of
'"' → pure '"'
'\\' → pure '\\'
'/' → pure '/'
'n' → pure '\n'
'r' → pure '\r'
't' → pure '\t'
'b' → fail "Cannot handle backspace yet"
'f' → fail "Cannot handle formfeed yet"
_ → fail "No clue how to parse this escapedString"
)
>>= c2str
simpleStringSingleton ∷ Parser String String
simpleStringSingleton =
(try simpleUnescapedString)
<|> (try simpleUnicodeString)
<|> simpleEscapedString
simpleStringValue ∷ Parser String String
simpleStringValue =
between singleQuote singleQuote
( fold
<$> (many simpleStringSingleton)
)
notTripleQuote ∷ Parser String String
notTripleQuote = (lookAhead (sequence [ anyChar, anyChar, anyChar ])) >>= (\s → if (s == [ '"', '"', '"' ]) then (fail "this is a triple quote") else anyChar >>= c2str)
blockStringValue ∷ Parser String String
blockStringValue = between tripleQuote tripleQuote (fold <$> many notTripleQuote)
stringValue ∷ Parser String AST.StringValue
stringValue = AST.StringValue <$> ((try blockStringValue) <|> simpleStringValue)
variable ∷ Parser String AST.Variable
variable = AST.Variable <$> (char '$' *> name)
booleanValue ∷ Parser String AST.BooleanValue
booleanValue = AST.BooleanValue <$> ((string "true" *> pure true) <|> (string "false" *> pure false))
nullValue ∷ Parser String AST.NullValue
nullValue = string "null" *> (pure AST.NullValue)
enumValue ∷ Parser String AST.EnumValue
enumValue =
AST.EnumValue
<$> (name >>= \x → if (x == "null" || x == "true" || x == "false") then fail "Name cannot be null, false or true" else pure x)
listValue ∷ Parser String (AST.Value) → Parser String (AST.ListValue)
listValue = (<$>) AST.ListValue <<< listish "[" "]"
argument ∷ Parser String (AST.Value) → Parser String (AST.Argument)
argument vc =
map AST.Argument $ { name: _, value: _ }
<$> name
<*> (ignoreMe *> char ':' *> ignoreMe *> vc)
_listish ∷ ∀ p. Parser String p → Parser String (L.List p)
_listish p = sepEndBy_ p ignoreMe
_listish1 ∷ ∀ p. Parser String p → Parser String (L.List p)
_listish1 p = L.fromFoldable <$> sepEndBy1_ p ignoreMe
listish ∷ ∀ p. String → String → Parser String p → Parser String (L.List p)
listish o c p = string o *> ignoreMe *> _listish p <* string c
objectValue ∷ Parser String (AST.Value) → Parser String (AST.ObjectValue)
objectValue = (<$>) AST.ObjectValue <<< listish "{" "}" <<< argument
arguments ∷ Parser String (AST.Arguments)
arguments = AST.Arguments <$> listish "(" ")" (argument value)
value ∷ Parser String (AST.Value)
value =
fix \p →
(try (AST.Value_Variable <$> variable))
<|> (try (AST.Value_NullValue <$> nullValue))
<|> (try (AST.Value_BooleanValue <$> booleanValue))
<|> (try (AST.Value_StringValue <$> stringValue))
<|> (try (AST.Value_FloatValue <$> floatValue))
<|> (try (AST.Value_IntValue <$> intValue))
<|> (try (AST.Value_EnumValue <$> enumValue))
<|> (try (AST.Value_ListValue <$> listValue p))
<|> (AST.Value_ObjectValue <$> objectValue p)
<?> "value"
--- util
ooo ∷ ∀ a. Parser String a → Parser String (Maybe a)
ooo p = option Nothing (try p >>= pure <<< Just)
optDesc ∷ Parser String (Maybe String)
optDesc = ooo description
optDir ∷ Parser String (Maybe AST.Directives)
optDir = ooo directives
optDv ∷ Parser String (Maybe AST.DefaultValue)
optDv = ooo defaultValue
--- spec
typeSystemDirectiveLocation ∷ Parser String AST.TypeSystemDirectiveLocation
typeSystemDirectiveLocation =
(try (string "SCHEMA") *> pure AST.SCHEMA)
<|> (try (string "SCALAR") *> pure AST.SCALAR)
<|> (try (string "OBJECT") *> pure AST.OBJECT)
<|> (try (string "FIELD_DEFINITION") *> pure AST.FIELD_DEFINITION)
<|> (try (string "ARGUMENT_DEFINITION") *> pure AST.ARGUMENT_DEFINITION)
<|> (try (string "INTERFACE") *> pure AST.INTERFACE)
<|> (try (string "UNION") *> pure AST.UNION)
<|> (try (string "ENUM") *> pure AST.ENUM)
<|> (try (string "ENUM_VALUE") *> pure AST.ENUM_VALUE)
<|> (try (string "INPUT_OBJECT") *> pure AST.INPUT_OBJECT)
<|> (string "INPUT_FIELD_DEFINITION" *> pure AST.INPUT_FIELD_DEFINITION)
<?> "typeSystemDirectiveLocation"
executableDirectiveLocation ∷ Parser String AST.ExecutableDirectiveLocation
executableDirectiveLocation =
(try (string "QUERY") *> pure AST.QUERY)
<|> (try (string "MUTATION") *> pure AST.MUTATION)
<|> (try (string "SUBSCRIPTION") *> pure AST.SUBSCRIPTION)
<|> (try (string "FIELD") *> pure AST.FIELD)
<|> (try (string "FRAGMENT_DEFINITION") *> pure AST.FRAGMENT_DEFINITION)
<|> (try (string "FRAGMENT_SPREAD") *> pure AST.FRAGMENT_SPREAD)
<|> (string "INLINE_FRAGMENT" *> pure AST.INLINE_FRAGMENT)
<?> "executableDirectiveLocation"
directiveLocation ∷ Parser String AST.DirectiveLocation
directiveLocation =
(try (AST.DirectiveLocation_TypeSystemDirectiveLocation <$> typeSystemDirectiveLocation))
<|> (AST.DirectiveLocation_ExecutableDirectiveLocation <$> executableDirectiveLocation)
<?> "directiveLocation"
directiveLocations ∷ Parser String AST.DirectiveLocations
directiveLocations =
AST.DirectiveLocations <<< L.fromFoldable
<$> ( ignoreMe
*> optional (char '|')
*> sepBy1 (ignoreMe *> directiveLocation <* ignoreMe) (char '|')
)
directive ∷ Parser String AST.Directive
directive =
map AST.Directive $ { name: _, arguments: _ }
<$> (char '@' *> name)
<*> (ignoreMe *> ooo arguments)
directives ∷ Parser String AST.Directives
directives = AST.Directives <$> _listish1 directive
namedType ∷ Parser String AST.NamedType
namedType = AST.NamedType <$> name
listType ∷ Parser String AST.Type → Parser String AST.ListType
listType t = AST.ListType <$> (string "[" *> ignoreMe *> t <* ignoreMe <* string "]")
nonNullType ∷ Parser String AST.Type → Parser String AST.NonNullType
nonNullType v =
(try (AST.NonNullType_NamedType <$> (namedType <* char '!')))
<|> (AST.NonNullType_ListType <$> (listType v <* char '!'))
<?> "nonNullType"
_type ∷ Parser String AST.Type
_type =
fix \p →
(try (AST.Type_NonNullType <$> nonNullType p))
<|> (try (AST.Type_NamedType <$> namedType))
<|> (AST.Type_ListType <$> listType p)
<?> "type"
defaultValue ∷ Parser String AST.DefaultValue
defaultValue = char '=' *> ignoreMe *> (AST.DefaultValue <$> value)
variableDefinition ∷ Parser String AST.VariableDefinition
variableDefinition =
map AST.VariableDefinition $ { variable: _, type: _, defaultValue: _ }
<$> variable
<*> (ignoreMe *> char ':' *> ignoreMe *> _type)
<*> (ignoreMe *> optDv)
variableDefinitions ∷ Parser String AST.VariableDefinitions
variableDefinitions = AST.VariableDefinitions <$> listish "(" ")" variableDefinition
fragmentName ∷ Parser String String
fragmentName = name >>= \x → if x == "on" then fail "Fragment name cannot be 'on'" else pure x
typeCondition ∷ Parser String AST.TypeCondition
typeCondition = AST.TypeCondition <$> (string "on" *> ignoreMe *> namedType)
fragmentSpread ∷ Parser String AST.FragmentSpread
fragmentSpread =
map AST.FragmentSpread $ { fragmentName: _, directives: _ }
<$> (string "..." *> ignoreMe *> fragmentName)
<*> (ignoreMe *> optDir)
ignorableExtension ∷ String → Parser String Unit
ignorableExtension s =
string "extend"
*> ignoreMe
*> string s
*> pure unit
unionMemberTypes ∷ Parser String AST.UnionMemberTypes
unionMemberTypes = AST.UnionMemberTypes <<< L.fromFoldable <$> sepBy1 (ignoreMe *> namedType <* ignoreMe) (char '|')
unionTypeExtensionWithDirectives ∷ Parser String AST.UnionTypeExtension
unionTypeExtensionWithDirectives =
map AST.UnionTypeExtension_With_Directives $ { name: _, directives: _ }
<$> name
<*> (ignoreMe *> directives)
unionTypeExtensionWithUnionMemberTypes ∷ Parser String AST.UnionTypeExtension
unionTypeExtensionWithUnionMemberTypes =
map AST.UnionTypeExtension_With_UnionMemberTypes $ { name: _, directives: _, unionMemberTypes: _ }
<$> name
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> char '=' *> ignoreMe *> unionMemberTypes)
unionTypeExtension ∷ Parser String AST.UnionTypeExtension
unionTypeExtension =
ignorableExtension "union"
*> ignoreMe
*> ( (try unionTypeExtensionWithUnionMemberTypes)
<|> unionTypeExtensionWithDirectives
<?> "unionTypeExtension"
)
unionTypeDefinition ∷ Parser String AST.UnionTypeDefinition
unionTypeDefinition =
map AST.UnionTypeDefinition $ { description: _, name: _, directives: _, unionMemberTypes: _ }
<$> optDesc
<*> (ignoreMe *> string "union" *> ignoreMe *> name)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> char '=' *> ignoreMe *> ooo unionMemberTypes)
enumValueDefinition ∷ Parser String AST.EnumValueDefinition
enumValueDefinition =
map AST.EnumValueDefinition $ { description: _, enumValue: _, directives: _ }
<$> optDesc
<*> (ignoreMe *> enumValue)
<*> (ignoreMe *> optDir)
enumValuesDefinition ∷ Parser String AST.EnumValuesDefinition
enumValuesDefinition = AST.EnumValuesDefinition <$> listish "{" "}" enumValueDefinition
enumTypeExtensionWithDirectives ∷ Parser String AST.EnumTypeExtension
enumTypeExtensionWithDirectives =
map AST.EnumTypeExtension_With_Directives $ { name: _, directives: _ }
<$> name
<*> (ignoreMe *> directives)
enumTypeExtensionWithEnumValuesDefinition ∷ Parser String AST.EnumTypeExtension
enumTypeExtensionWithEnumValuesDefinition =
map AST.EnumTypeExtension_With_EnumValuesDefinition $ { name: _, directives: _, enumValuesDefinition: _ }
<$> name
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> enumValuesDefinition)
enumTypeExtension ∷ Parser String AST.EnumTypeExtension
enumTypeExtension =
ignorableExtension "enum"
*> ignoreMe
*> ( (try enumTypeExtensionWithEnumValuesDefinition)
<|> enumTypeExtensionWithDirectives
<?> "enumTypeExtension"
)
enumTypeDefinition ∷ Parser String AST.EnumTypeDefinition
enumTypeDefinition =
map AST.EnumTypeDefinition $ { description: _, name: _, directives: _, enumValuesDefinition: _ }
<$> optDesc
<*> (ignoreMe *> string "enum" *> ignoreMe *> name)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ooo enumValuesDefinition)
inputValueDefinition ∷ Parser String AST.InputValueDefinition
inputValueDefinition =
map AST.InputValueDefinition $ { description: _, name: _, type: _, defaultValue: _, directives: _ }
<$> optDesc
<*> (ignoreMe *> name)
<*> (ignoreMe *> char ':' *> ignoreMe *> _type)
<*> (ignoreMe *> optDv)
<*> (ignoreMe *> optDir)
argumentsDefinition ∷ Parser String (AST.ArgumentsDefinition)
argumentsDefinition = AST.ArgumentsDefinition <$> listish "(" ")" inputValueDefinition
operationType ∷ Parser String (AST.OperationType)
operationType =
(try $ string "query" *> pure AST.Query)
<|> (try $ string "mutation" *> pure AST.Mutation)
<|> (string "subscription" *> pure AST.Subscription)
<?> "operation type"
operationTypeDefinition ∷ Parser String (AST.OperationTypeDefinition)
operationTypeDefinition =
map AST.OperationTypeDefinition $ { operationType: _, namedType: _ }
<$> operationType
<*> (ignoreMe *> char ':' *> ignoreMe *> namedType)
scalarTypeExtension ∷ Parser String (AST.ScalarTypeExtension)
scalarTypeExtension =
map AST.ScalarTypeExtension $ { name: _, directives: _ }
<$> (ignorableExtension "scalar" *> ignoreMe *> name)
<*> (ignoreMe *> directives)
implementsInterfaces ∷ Parser String (AST.ImplementsInterfaces)
implementsInterfaces =
string "implements"
*> ignoreMe
*> optional (char '&')
*> ignoreMe
*> (AST.ImplementsInterfaces <<< L.fromFoldable <$> sepBy1 (ignoreMe *> namedType <* ignoreMe) (char '&'))
scalarTypeDefinition ∷ Parser String (AST.ScalarTypeDefinition)
scalarTypeDefinition =
map AST.ScalarTypeDefinition $ { description: _, name: _, directives: _ }
<$> optDesc
<*> (ignoreMe *> string "scalar" *> ignoreMe *> name)
<*> (ignoreMe *> optDir)
fieldDefinition ∷ Parser String (AST.FieldDefinition)
fieldDefinition =
map AST.FieldDefinition $ { description: _, name: _, argumentsDefinition: _, type: _, directives: _ }
<$> optDesc
<*> (ignoreMe *> name)
<*> (ignoreMe *> ooo argumentsDefinition)
<*> (ignoreMe *> char ':' *> ignoreMe *> _type)
<*> (ignoreMe *> optDir)
fieldsDefinition ∷ Parser String (AST.FieldsDefinition)
fieldsDefinition = AST.FieldsDefinition <$> listish "{" "}" fieldDefinition
objectTypeDefinition ∷ Parser String (AST.ObjectTypeDefinition)
objectTypeDefinition =
map AST.ObjectTypeDefinition $ { description: _, name: _, implementsInterfaces: _, directives: _, fieldsDefinition: _ }
<$> optDesc
<*> (ignoreMe *> string "type" *> ignoreMe *> name)
<*> (ignoreMe *> ooo implementsInterfaces)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ooo fieldsDefinition)
schemaExtensionWithDirectives ∷ Parser String AST.SchemaExtension
schemaExtensionWithDirectives = map AST.SchemaExtension_With_Directives $ { directives: _ } <$> directives
operationTypesDefinition ∷ Parser String (L.List AST.OperationTypeDefinition)
operationTypesDefinition = listish "{" "}" operationTypeDefinition
schemaExtensionWithOperationTypeDefinition ∷ Parser String AST.SchemaExtension
schemaExtensionWithOperationTypeDefinition =
map AST.SchemaExtension_With_OperationTypeDefinition $ { directives: _, operationTypesDefinition: _ }
<$> optDir
<*> (ignoreMe *> operationTypesDefinition)
schemaExtension ∷ Parser String AST.SchemaExtension
schemaExtension =
ignorableExtension "schema"
*> ignoreMe
*> ( (try schemaExtensionWithOperationTypeDefinition)
<|> schemaExtensionWithDirectives
<?> "schemaExtension"
)
--------
objectTypeExtensionWithImplementsInterfaces ∷ Parser String AST.ObjectTypeExtension
objectTypeExtensionWithImplementsInterfaces =
map AST.ObjectTypeExtension_With_ImplementsInterfaces $ { name: _, implementsInterfaces: _ }
<$> name
<*> (ignoreMe *> implementsInterfaces)
objectTypeExtensionWithDirectives ∷ Parser String AST.ObjectTypeExtension
objectTypeExtensionWithDirectives =
map AST.ObjectTypeExtension_With_Directives $ { name: _, implementsInterfaces: _, directives: _ }
<$> name
<*> (ignoreMe *> ooo implementsInterfaces)
<*> (ignoreMe *> directives)
objectTypeExtensionWithFieldsDefinition ∷ Parser String AST.ObjectTypeExtension
objectTypeExtensionWithFieldsDefinition =
map AST.ObjectTypeExtension_With_FieldsDefinition $ { name: _, implementsInterfaces: _, directives: _, fieldsDefinition: _ }
<$> name
<*> (ignoreMe *> ooo implementsInterfaces)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> fieldsDefinition)
objectTypeExtension ∷ Parser String AST.ObjectTypeExtension
objectTypeExtension =
ignorableExtension "type"
*> ignoreMe
*> ( (try objectTypeExtensionWithFieldsDefinition)
<|> (try objectTypeExtensionWithDirectives)
<|> objectTypeExtensionWithImplementsInterfaces
<?> "objectTypeExtension"
)
-----
inputObjectTypeExtensionWithDirectives ∷ Parser String AST.InputObjectTypeExtension
inputObjectTypeExtensionWithDirectives =
map AST.InputObjectTypeExtension_With_Directives $ { name: _, directives: _ }
<$> name
<*> (ignoreMe *> directives)
inputFieldsDefinition ∷ Parser String AST.InputFieldsDefinition
inputFieldsDefinition = AST.InputFieldsDefinition <$> listish "{" "}" inputValueDefinition
inputObjectTypeExtensionWithInputFieldsDefinition ∷ Parser String AST.InputObjectTypeExtension
inputObjectTypeExtensionWithInputFieldsDefinition =
map AST.InputObjectTypeExtension_With_InputFieldsDefinition $ { name: _, directives: _, inputFieldsDefinition: _ }
<$> name
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> inputFieldsDefinition)
inputObjectTypeExtension ∷ Parser String AST.InputObjectTypeExtension
inputObjectTypeExtension =
ignorableExtension "input"
*> ignoreMe
*> ( (try inputObjectTypeExtensionWithInputFieldsDefinition)
<|> inputObjectTypeExtensionWithDirectives
<?> "inputObjectTypeExtension"
)
inputObjectTypeDefinition ∷ Parser String AST.InputObjectTypeDefinition
inputObjectTypeDefinition =
map AST.InputObjectTypeDefinition $ { description: _, name: _, directives: _, inputFieldsDefinition: _ }
<$> optDesc
<*> (ignoreMe *> string "input" *> ignoreMe *> name)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ooo inputFieldsDefinition)
-----
interfaceTypeExtensionWithDirectives ∷ Parser String AST.InterfaceTypeExtension
interfaceTypeExtensionWithDirectives =
map AST.InterfaceTypeExtension_With_Directives $ { name: _, directives: _ }
<$> name
<*> (ignoreMe *> directives)
interfaceTypeExtensionWithFieldsDefinition ∷ Parser String AST.InterfaceTypeExtension
interfaceTypeExtensionWithFieldsDefinition =
map AST.InterfaceTypeExtension_With_FieldsDefinition $ { name: _, directives: _, fieldsDefinition: _ }
<$> name
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> fieldsDefinition)
interfaceTypeExtension ∷ Parser String AST.InterfaceTypeExtension
interfaceTypeExtension =
ignorableExtension "input"
*> ignoreMe
*> ( (try interfaceTypeExtensionWithFieldsDefinition)
<|> interfaceTypeExtensionWithDirectives
<?> "interfaceTypeExtension"
)
interfaceTypeDefinition ∷ Parser String AST.InterfaceTypeDefinition
interfaceTypeDefinition =
map AST.InterfaceTypeDefinition $ { description: _, name: _, directives: _, fieldsDefinition: _ }
<$> optDesc
<*> (ignoreMe *> string "interface" *> ignoreMe *> name)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ooo fieldsDefinition)
typeDefinition ∷ Parser String AST.TypeDefinition
typeDefinition =
(try (AST.TypeDefinition_ScalarTypeDefinition <$> scalarTypeDefinition))
<|> (try (AST.TypeDefinition_ObjectTypeDefinition <$> objectTypeDefinition))
<|> (try (AST.TypeDefinition_InterfaceTypeDefinition <$> interfaceTypeDefinition))
<|> (try (AST.TypeDefinition_UnionTypeDefinition <$> unionTypeDefinition))
<|> (try (AST.TypeDefinition_EnumTypeDefinition <$> enumTypeDefinition))
<|> (AST.TypeDefinition_InputObjectTypeDefinition <$> inputObjectTypeDefinition)
<?> "typeDefinition"
typeExtension ∷ Parser String AST.TypeExtension
typeExtension =
(try (AST.TypeExtension_ScalarTypeExtension <$> scalarTypeExtension))
<|> (try (AST.TypeExtension_ObjectTypeExtension <$> objectTypeExtension))
<|> (try (AST.TypeExtension_InterfaceTypeExtension <$> interfaceTypeExtension))
<|> (try (AST.TypeExtension_UnionTypeExtension <$> unionTypeExtension))
<|> (try (AST.TypeExtension_EnumTypeExtension <$> enumTypeExtension))
<|> (AST.TypeExtension_InputObjectTypeExtension <$> inputObjectTypeExtension)
<?> "typeExtension"
rootOperationDefinition ∷ Parser String AST.RootOperationTypeDefinition
rootOperationDefinition =
map AST.RootOperationTypeDefinition $ { operationType: _, namedType: _ }
<$> operationType
<*> (ignoreMe *> char ':' *> ignoreMe *> namedType)
schemaDefinition ∷ Parser String AST.SchemaDefinition
schemaDefinition =
map AST.SchemaDefinition $ { directives: _, rootOperationTypeDefinition: _ }
<$> (string "schema" *> ignoreMe *> optDir)
<*> (ignoreMe *> listish "{" "}" rootOperationDefinition)
directiveDefinition ∷ Parser String AST.DirectiveDefinition
directiveDefinition =
map AST.DirectiveDefinition $ { description: _, name: _, argumentsDefinition: _, directiveLocations: _ }
<$> optDesc
<*> (ignoreMe *> string "directive" *> ignoreMe *> char '@' *> name)
<*> (ignoreMe *> ooo argumentsDefinition)
<*> (ignoreMe *> string "on" *> ignoreMe *> directiveLocations)
typeSystemDefinition ∷ Parser String AST.TypeSystemDefinition
typeSystemDefinition =
(try (AST.TypeSystemDefinition_SchemaDefinition <$> schemaDefinition))
<|> (try (AST.TypeSystemDefinition_TypeDefinition <$> typeDefinition))
<|> (AST.TypeSystemDefinition_DirectiveDefinition <$> directiveDefinition)
<?> "typeSystemDefinition"
alias ∷ Parser String String
alias = name <* ignoreMe <* char ':'
inlineFragment ∷ Parser String AST.SelectionSet → Parser String AST.InlineFragment
inlineFragment ss =
map AST.InlineFragment $ { typeCondition: _, directives: _, selectionSet: _ }
<$> (string "..." *> ignoreMe *> ooo typeCondition)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ss)
selection ∷ Parser String AST.SelectionSet → Parser String AST.Selection
selection ss =
(try (AST.Selection_Field <$> (field ss)))
<|> (try (AST.Selection_FragmentSpread <$> fragmentSpread))
<|> (AST.Selection_InlineFragment <$> (inlineFragment ss))
<?> "selection"
selectionSet ∷ Parser String AST.SelectionSet
selectionSet = fix \p → AST.SelectionSet <$> listish "{" "}" (selection p)
field ∷ Parser String AST.SelectionSet → Parser String AST.Field
field ss =
map AST.Field $ { alias: _, name: _, arguments: _, directives: _, selectionSet: _ }
<$> ooo alias
<*> (ignoreMe *> name)
<*> (ignoreMe *> ooo arguments)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> ooo ss)
operationDefinition ∷ Parser String AST.OperationDefinition
operationDefinition =
(try (AST.OperationDefinition_SelectionSet <$> selectionSet))
<|> ( map AST.OperationDefinition_OperationType $ { operationType: _, name: _, variableDefinitions: _, directives: _, selectionSet: _ }
<$> operationType
<*> (ignoreMe *> ooo name)
<*> (ignoreMe *> ooo variableDefinitions)
<*> (ignoreMe *> optDir)
<*> (ignoreMe *> selectionSet)
)
fragmentDefinition ∷ Parser String AST.FragmentDefinition
fragmentDefinition =
map AST.FragmentDefinition $ { fragmentName: _, typeCondition: _, directives: _, selectionSet: _ }
<$> (string "fragment" *> ignoreMe *> fragmentName)
<*> (ignoreMe *> typeCondition)
<*> (ignoreMe *> optDir)
<*> selectionSet
typeSystemExtension ∷ Parser String AST.TypeSystemExtension
typeSystemExtension =
(try (AST.TypeSystemExtension_SchemaExtension <$> schemaExtension))
<|> (AST.TypeSystemExtension_TypeExtension <$> typeExtension)
<?> "typeSystemExtension"
executableDefinition ∷ Parser String AST.ExecutableDefinition
executableDefinition =
(try (AST.ExecutableDefinition_OperationDefinition <$> operationDefinition))
<|> (AST.ExecutableDefinition_FragmentDefinition <$> fragmentDefinition)
<?> "executableDefinition"
definition ∷ Parser String AST.Definition
definition =
(try (AST.Definition_ExecutableDefinition <$> executableDefinition))
<|> (try (AST.Definition_TypeSystemDefinition <$> typeSystemDefinition))
<|> (AST.Definition_TypeSystemExtension <$> typeSystemExtension)
<?> "definition"
document ∷ Parser String AST.Document
document = AST.Document <$> (ignoreMe *> _listish definition)