UNPKG

41.6 kBJavaScriptView Raw
1import { syntaxError } from "../error/syntaxError.mjs";
2import { Kind } from "./kinds.mjs";
3import { Location } from "./ast.mjs";
4import { TokenKind } from "./tokenKind.mjs";
5import { Source, isSource } from "./source.mjs";
6import { DirectiveLocation } from "./directiveLocation.mjs";
7import { Lexer, isPunctuatorTokenKind } from "./lexer.mjs";
8/**
9 * Configuration options to control parser behavior
10 */
11
12/**
13 * Given a GraphQL source, parses it into a Document.
14 * Throws GraphQLError if a syntax error is encountered.
15 */
16export function parse(source, options) {
17 var parser = new Parser(source, options);
18 return parser.parseDocument();
19}
20/**
21 * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
22 * that value.
23 * Throws GraphQLError if a syntax error is encountered.
24 *
25 * This is useful within tools that operate upon GraphQL Values directly and
26 * in isolation of complete GraphQL documents.
27 *
28 * Consider providing the results to the utility function: valueFromAST().
29 */
30
31export function parseValue(source, options) {
32 var parser = new Parser(source, options);
33 parser.expectToken(TokenKind.SOF);
34 var value = parser.parseValueLiteral(false);
35 parser.expectToken(TokenKind.EOF);
36 return value;
37}
38/**
39 * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
40 * that type.
41 * Throws GraphQLError if a syntax error is encountered.
42 *
43 * This is useful within tools that operate upon GraphQL Types directly and
44 * in isolation of complete GraphQL documents.
45 *
46 * Consider providing the results to the utility function: typeFromAST().
47 */
48
49export function parseType(source, options) {
50 var parser = new Parser(source, options);
51 parser.expectToken(TokenKind.SOF);
52 var type = parser.parseTypeReference();
53 parser.expectToken(TokenKind.EOF);
54 return type;
55}
56/**
57 * This class is exported only to assist people in implementing their own parsers
58 * without duplicating too much code and should be used only as last resort for cases
59 * such as experimental syntax or if certain features could not be contributed upstream.
60 *
61 * It is still part of the internal API and is versioned, so any changes to it are never
62 * considered breaking changes. If you still need to support multiple versions of the
63 * library, please use the `versionInfo` variable for version detection.
64 *
65 * @internal
66 */
67
68export var Parser = /*#__PURE__*/function () {
69 function Parser(source, options) {
70 var sourceObj = isSource(source) ? source : new Source(source);
71 this._lexer = new Lexer(sourceObj);
72 this._options = options;
73 }
74 /**
75 * Converts a name lex token into a name parse node.
76 */
77
78
79 var _proto = Parser.prototype;
80
81 _proto.parseName = function parseName() {
82 var token = this.expectToken(TokenKind.NAME);
83 return {
84 kind: Kind.NAME,
85 value: token.value,
86 loc: this.loc(token)
87 };
88 } // Implements the parsing rules in the Document section.
89
90 /**
91 * Document : Definition+
92 */
93 ;
94
95 _proto.parseDocument = function parseDocument() {
96 var start = this._lexer.token;
97 return {
98 kind: Kind.DOCUMENT,
99 definitions: this.many(TokenKind.SOF, this.parseDefinition, TokenKind.EOF),
100 loc: this.loc(start)
101 };
102 }
103 /**
104 * Definition :
105 * - ExecutableDefinition
106 * - TypeSystemDefinition
107 * - TypeSystemExtension
108 *
109 * ExecutableDefinition :
110 * - OperationDefinition
111 * - FragmentDefinition
112 */
113 ;
114
115 _proto.parseDefinition = function parseDefinition() {
116 if (this.peek(TokenKind.NAME)) {
117 switch (this._lexer.token.value) {
118 case 'query':
119 case 'mutation':
120 case 'subscription':
121 return this.parseOperationDefinition();
122
123 case 'fragment':
124 return this.parseFragmentDefinition();
125
126 case 'schema':
127 case 'scalar':
128 case 'type':
129 case 'interface':
130 case 'union':
131 case 'enum':
132 case 'input':
133 case 'directive':
134 return this.parseTypeSystemDefinition();
135
136 case 'extend':
137 return this.parseTypeSystemExtension();
138 }
139 } else if (this.peek(TokenKind.BRACE_L)) {
140 return this.parseOperationDefinition();
141 } else if (this.peekDescription()) {
142 return this.parseTypeSystemDefinition();
143 }
144
145 throw this.unexpected();
146 } // Implements the parsing rules in the Operations section.
147
148 /**
149 * OperationDefinition :
150 * - SelectionSet
151 * - OperationType Name? VariableDefinitions? Directives? SelectionSet
152 */
153 ;
154
155 _proto.parseOperationDefinition = function parseOperationDefinition() {
156 var start = this._lexer.token;
157
158 if (this.peek(TokenKind.BRACE_L)) {
159 return {
160 kind: Kind.OPERATION_DEFINITION,
161 operation: 'query',
162 name: undefined,
163 variableDefinitions: [],
164 directives: [],
165 selectionSet: this.parseSelectionSet(),
166 loc: this.loc(start)
167 };
168 }
169
170 var operation = this.parseOperationType();
171 var name;
172
173 if (this.peek(TokenKind.NAME)) {
174 name = this.parseName();
175 }
176
177 return {
178 kind: Kind.OPERATION_DEFINITION,
179 operation: operation,
180 name: name,
181 variableDefinitions: this.parseVariableDefinitions(),
182 directives: this.parseDirectives(false),
183 selectionSet: this.parseSelectionSet(),
184 loc: this.loc(start)
185 };
186 }
187 /**
188 * OperationType : one of query mutation subscription
189 */
190 ;
191
192 _proto.parseOperationType = function parseOperationType() {
193 var operationToken = this.expectToken(TokenKind.NAME);
194
195 switch (operationToken.value) {
196 case 'query':
197 return 'query';
198
199 case 'mutation':
200 return 'mutation';
201
202 case 'subscription':
203 return 'subscription';
204 }
205
206 throw this.unexpected(operationToken);
207 }
208 /**
209 * VariableDefinitions : ( VariableDefinition+ )
210 */
211 ;
212
213 _proto.parseVariableDefinitions = function parseVariableDefinitions() {
214 return this.optionalMany(TokenKind.PAREN_L, this.parseVariableDefinition, TokenKind.PAREN_R);
215 }
216 /**
217 * VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
218 */
219 ;
220
221 _proto.parseVariableDefinition = function parseVariableDefinition() {
222 var start = this._lexer.token;
223 return {
224 kind: Kind.VARIABLE_DEFINITION,
225 variable: this.parseVariable(),
226 type: (this.expectToken(TokenKind.COLON), this.parseTypeReference()),
227 defaultValue: this.expectOptionalToken(TokenKind.EQUALS) ? this.parseValueLiteral(true) : undefined,
228 directives: this.parseDirectives(true),
229 loc: this.loc(start)
230 };
231 }
232 /**
233 * Variable : $ Name
234 */
235 ;
236
237 _proto.parseVariable = function parseVariable() {
238 var start = this._lexer.token;
239 this.expectToken(TokenKind.DOLLAR);
240 return {
241 kind: Kind.VARIABLE,
242 name: this.parseName(),
243 loc: this.loc(start)
244 };
245 }
246 /**
247 * SelectionSet : { Selection+ }
248 */
249 ;
250
251 _proto.parseSelectionSet = function parseSelectionSet() {
252 var start = this._lexer.token;
253 return {
254 kind: Kind.SELECTION_SET,
255 selections: this.many(TokenKind.BRACE_L, this.parseSelection, TokenKind.BRACE_R),
256 loc: this.loc(start)
257 };
258 }
259 /**
260 * Selection :
261 * - Field
262 * - FragmentSpread
263 * - InlineFragment
264 */
265 ;
266
267 _proto.parseSelection = function parseSelection() {
268 return this.peek(TokenKind.SPREAD) ? this.parseFragment() : this.parseField();
269 }
270 /**
271 * Field : Alias? Name Arguments? Directives? SelectionSet?
272 *
273 * Alias : Name :
274 */
275 ;
276
277 _proto.parseField = function parseField() {
278 var start = this._lexer.token;
279 var nameOrAlias = this.parseName();
280 var alias;
281 var name;
282
283 if (this.expectOptionalToken(TokenKind.COLON)) {
284 alias = nameOrAlias;
285 name = this.parseName();
286 } else {
287 name = nameOrAlias;
288 }
289
290 return {
291 kind: Kind.FIELD,
292 alias: alias,
293 name: name,
294 arguments: this.parseArguments(false),
295 directives: this.parseDirectives(false),
296 selectionSet: this.peek(TokenKind.BRACE_L) ? this.parseSelectionSet() : undefined,
297 loc: this.loc(start)
298 };
299 }
300 /**
301 * Arguments[Const] : ( Argument[?Const]+ )
302 */
303 ;
304
305 _proto.parseArguments = function parseArguments(isConst) {
306 var item = isConst ? this.parseConstArgument : this.parseArgument;
307 return this.optionalMany(TokenKind.PAREN_L, item, TokenKind.PAREN_R);
308 }
309 /**
310 * Argument[Const] : Name : Value[?Const]
311 */
312 ;
313
314 _proto.parseArgument = function parseArgument() {
315 var start = this._lexer.token;
316 var name = this.parseName();
317 this.expectToken(TokenKind.COLON);
318 return {
319 kind: Kind.ARGUMENT,
320 name: name,
321 value: this.parseValueLiteral(false),
322 loc: this.loc(start)
323 };
324 };
325
326 _proto.parseConstArgument = function parseConstArgument() {
327 var start = this._lexer.token;
328 return {
329 kind: Kind.ARGUMENT,
330 name: this.parseName(),
331 value: (this.expectToken(TokenKind.COLON), this.parseValueLiteral(true)),
332 loc: this.loc(start)
333 };
334 } // Implements the parsing rules in the Fragments section.
335
336 /**
337 * Corresponds to both FragmentSpread and InlineFragment in the spec.
338 *
339 * FragmentSpread : ... FragmentName Directives?
340 *
341 * InlineFragment : ... TypeCondition? Directives? SelectionSet
342 */
343 ;
344
345 _proto.parseFragment = function parseFragment() {
346 var start = this._lexer.token;
347 this.expectToken(TokenKind.SPREAD);
348 var hasTypeCondition = this.expectOptionalKeyword('on');
349
350 if (!hasTypeCondition && this.peek(TokenKind.NAME)) {
351 return {
352 kind: Kind.FRAGMENT_SPREAD,
353 name: this.parseFragmentName(),
354 directives: this.parseDirectives(false),
355 loc: this.loc(start)
356 };
357 }
358
359 return {
360 kind: Kind.INLINE_FRAGMENT,
361 typeCondition: hasTypeCondition ? this.parseNamedType() : undefined,
362 directives: this.parseDirectives(false),
363 selectionSet: this.parseSelectionSet(),
364 loc: this.loc(start)
365 };
366 }
367 /**
368 * FragmentDefinition :
369 * - fragment FragmentName on TypeCondition Directives? SelectionSet
370 *
371 * TypeCondition : NamedType
372 */
373 ;
374
375 _proto.parseFragmentDefinition = function parseFragmentDefinition() {
376 var _this$_options;
377
378 var start = this._lexer.token;
379 this.expectKeyword('fragment'); // Experimental support for defining variables within fragments changes
380 // the grammar of FragmentDefinition:
381 // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet
382
383 if (((_this$_options = this._options) === null || _this$_options === void 0 ? void 0 : _this$_options.experimentalFragmentVariables) === true) {
384 return {
385 kind: Kind.FRAGMENT_DEFINITION,
386 name: this.parseFragmentName(),
387 variableDefinitions: this.parseVariableDefinitions(),
388 typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
389 directives: this.parseDirectives(false),
390 selectionSet: this.parseSelectionSet(),
391 loc: this.loc(start)
392 };
393 }
394
395 return {
396 kind: Kind.FRAGMENT_DEFINITION,
397 name: this.parseFragmentName(),
398 typeCondition: (this.expectKeyword('on'), this.parseNamedType()),
399 directives: this.parseDirectives(false),
400 selectionSet: this.parseSelectionSet(),
401 loc: this.loc(start)
402 };
403 }
404 /**
405 * FragmentName : Name but not `on`
406 */
407 ;
408
409 _proto.parseFragmentName = function parseFragmentName() {
410 if (this._lexer.token.value === 'on') {
411 throw this.unexpected();
412 }
413
414 return this.parseName();
415 } // Implements the parsing rules in the Values section.
416
417 /**
418 * Value[Const] :
419 * - [~Const] Variable
420 * - IntValue
421 * - FloatValue
422 * - StringValue
423 * - BooleanValue
424 * - NullValue
425 * - EnumValue
426 * - ListValue[?Const]
427 * - ObjectValue[?Const]
428 *
429 * BooleanValue : one of `true` `false`
430 *
431 * NullValue : `null`
432 *
433 * EnumValue : Name but not `true`, `false` or `null`
434 */
435 ;
436
437 _proto.parseValueLiteral = function parseValueLiteral(isConst) {
438 var token = this._lexer.token;
439
440 switch (token.kind) {
441 case TokenKind.BRACKET_L:
442 return this.parseList(isConst);
443
444 case TokenKind.BRACE_L:
445 return this.parseObject(isConst);
446
447 case TokenKind.INT:
448 this._lexer.advance();
449
450 return {
451 kind: Kind.INT,
452 value: token.value,
453 loc: this.loc(token)
454 };
455
456 case TokenKind.FLOAT:
457 this._lexer.advance();
458
459 return {
460 kind: Kind.FLOAT,
461 value: token.value,
462 loc: this.loc(token)
463 };
464
465 case TokenKind.STRING:
466 case TokenKind.BLOCK_STRING:
467 return this.parseStringLiteral();
468
469 case TokenKind.NAME:
470 this._lexer.advance();
471
472 switch (token.value) {
473 case 'true':
474 return {
475 kind: Kind.BOOLEAN,
476 value: true,
477 loc: this.loc(token)
478 };
479
480 case 'false':
481 return {
482 kind: Kind.BOOLEAN,
483 value: false,
484 loc: this.loc(token)
485 };
486
487 case 'null':
488 return {
489 kind: Kind.NULL,
490 loc: this.loc(token)
491 };
492
493 default:
494 return {
495 kind: Kind.ENUM,
496 value: token.value,
497 loc: this.loc(token)
498 };
499 }
500
501 case TokenKind.DOLLAR:
502 if (!isConst) {
503 return this.parseVariable();
504 }
505
506 break;
507 }
508
509 throw this.unexpected();
510 };
511
512 _proto.parseStringLiteral = function parseStringLiteral() {
513 var token = this._lexer.token;
514
515 this._lexer.advance();
516
517 return {
518 kind: Kind.STRING,
519 value: token.value,
520 block: token.kind === TokenKind.BLOCK_STRING,
521 loc: this.loc(token)
522 };
523 }
524 /**
525 * ListValue[Const] :
526 * - [ ]
527 * - [ Value[?Const]+ ]
528 */
529 ;
530
531 _proto.parseList = function parseList(isConst) {
532 var _this = this;
533
534 var start = this._lexer.token;
535
536 var item = function item() {
537 return _this.parseValueLiteral(isConst);
538 };
539
540 return {
541 kind: Kind.LIST,
542 values: this.any(TokenKind.BRACKET_L, item, TokenKind.BRACKET_R),
543 loc: this.loc(start)
544 };
545 }
546 /**
547 * ObjectValue[Const] :
548 * - { }
549 * - { ObjectField[?Const]+ }
550 */
551 ;
552
553 _proto.parseObject = function parseObject(isConst) {
554 var _this2 = this;
555
556 var start = this._lexer.token;
557
558 var item = function item() {
559 return _this2.parseObjectField(isConst);
560 };
561
562 return {
563 kind: Kind.OBJECT,
564 fields: this.any(TokenKind.BRACE_L, item, TokenKind.BRACE_R),
565 loc: this.loc(start)
566 };
567 }
568 /**
569 * ObjectField[Const] : Name : Value[?Const]
570 */
571 ;
572
573 _proto.parseObjectField = function parseObjectField(isConst) {
574 var start = this._lexer.token;
575 var name = this.parseName();
576 this.expectToken(TokenKind.COLON);
577 return {
578 kind: Kind.OBJECT_FIELD,
579 name: name,
580 value: this.parseValueLiteral(isConst),
581 loc: this.loc(start)
582 };
583 } // Implements the parsing rules in the Directives section.
584
585 /**
586 * Directives[Const] : Directive[?Const]+
587 */
588 ;
589
590 _proto.parseDirectives = function parseDirectives(isConst) {
591 var directives = [];
592
593 while (this.peek(TokenKind.AT)) {
594 directives.push(this.parseDirective(isConst));
595 }
596
597 return directives;
598 }
599 /**
600 * Directive[Const] : @ Name Arguments[?Const]?
601 */
602 ;
603
604 _proto.parseDirective = function parseDirective(isConst) {
605 var start = this._lexer.token;
606 this.expectToken(TokenKind.AT);
607 return {
608 kind: Kind.DIRECTIVE,
609 name: this.parseName(),
610 arguments: this.parseArguments(isConst),
611 loc: this.loc(start)
612 };
613 } // Implements the parsing rules in the Types section.
614
615 /**
616 * Type :
617 * - NamedType
618 * - ListType
619 * - NonNullType
620 */
621 ;
622
623 _proto.parseTypeReference = function parseTypeReference() {
624 var start = this._lexer.token;
625 var type;
626
627 if (this.expectOptionalToken(TokenKind.BRACKET_L)) {
628 type = this.parseTypeReference();
629 this.expectToken(TokenKind.BRACKET_R);
630 type = {
631 kind: Kind.LIST_TYPE,
632 type: type,
633 loc: this.loc(start)
634 };
635 } else {
636 type = this.parseNamedType();
637 }
638
639 if (this.expectOptionalToken(TokenKind.BANG)) {
640 return {
641 kind: Kind.NON_NULL_TYPE,
642 type: type,
643 loc: this.loc(start)
644 };
645 }
646
647 return type;
648 }
649 /**
650 * NamedType : Name
651 */
652 ;
653
654 _proto.parseNamedType = function parseNamedType() {
655 var start = this._lexer.token;
656 return {
657 kind: Kind.NAMED_TYPE,
658 name: this.parseName(),
659 loc: this.loc(start)
660 };
661 } // Implements the parsing rules in the Type Definition section.
662
663 /**
664 * TypeSystemDefinition :
665 * - SchemaDefinition
666 * - TypeDefinition
667 * - DirectiveDefinition
668 *
669 * TypeDefinition :
670 * - ScalarTypeDefinition
671 * - ObjectTypeDefinition
672 * - InterfaceTypeDefinition
673 * - UnionTypeDefinition
674 * - EnumTypeDefinition
675 * - InputObjectTypeDefinition
676 */
677 ;
678
679 _proto.parseTypeSystemDefinition = function parseTypeSystemDefinition() {
680 // Many definitions begin with a description and require a lookahead.
681 var keywordToken = this.peekDescription() ? this._lexer.lookahead() : this._lexer.token;
682
683 if (keywordToken.kind === TokenKind.NAME) {
684 switch (keywordToken.value) {
685 case 'schema':
686 return this.parseSchemaDefinition();
687
688 case 'scalar':
689 return this.parseScalarTypeDefinition();
690
691 case 'type':
692 return this.parseObjectTypeDefinition();
693
694 case 'interface':
695 return this.parseInterfaceTypeDefinition();
696
697 case 'union':
698 return this.parseUnionTypeDefinition();
699
700 case 'enum':
701 return this.parseEnumTypeDefinition();
702
703 case 'input':
704 return this.parseInputObjectTypeDefinition();
705
706 case 'directive':
707 return this.parseDirectiveDefinition();
708 }
709 }
710
711 throw this.unexpected(keywordToken);
712 };
713
714 _proto.peekDescription = function peekDescription() {
715 return this.peek(TokenKind.STRING) || this.peek(TokenKind.BLOCK_STRING);
716 }
717 /**
718 * Description : StringValue
719 */
720 ;
721
722 _proto.parseDescription = function parseDescription() {
723 if (this.peekDescription()) {
724 return this.parseStringLiteral();
725 }
726 }
727 /**
728 * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
729 */
730 ;
731
732 _proto.parseSchemaDefinition = function parseSchemaDefinition() {
733 var start = this._lexer.token;
734 var description = this.parseDescription();
735 this.expectKeyword('schema');
736 var directives = this.parseDirectives(true);
737 var operationTypes = this.many(TokenKind.BRACE_L, this.parseOperationTypeDefinition, TokenKind.BRACE_R);
738 return {
739 kind: Kind.SCHEMA_DEFINITION,
740 description: description,
741 directives: directives,
742 operationTypes: operationTypes,
743 loc: this.loc(start)
744 };
745 }
746 /**
747 * OperationTypeDefinition : OperationType : NamedType
748 */
749 ;
750
751 _proto.parseOperationTypeDefinition = function parseOperationTypeDefinition() {
752 var start = this._lexer.token;
753 var operation = this.parseOperationType();
754 this.expectToken(TokenKind.COLON);
755 var type = this.parseNamedType();
756 return {
757 kind: Kind.OPERATION_TYPE_DEFINITION,
758 operation: operation,
759 type: type,
760 loc: this.loc(start)
761 };
762 }
763 /**
764 * ScalarTypeDefinition : Description? scalar Name Directives[Const]?
765 */
766 ;
767
768 _proto.parseScalarTypeDefinition = function parseScalarTypeDefinition() {
769 var start = this._lexer.token;
770 var description = this.parseDescription();
771 this.expectKeyword('scalar');
772 var name = this.parseName();
773 var directives = this.parseDirectives(true);
774 return {
775 kind: Kind.SCALAR_TYPE_DEFINITION,
776 description: description,
777 name: name,
778 directives: directives,
779 loc: this.loc(start)
780 };
781 }
782 /**
783 * ObjectTypeDefinition :
784 * Description?
785 * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
786 */
787 ;
788
789 _proto.parseObjectTypeDefinition = function parseObjectTypeDefinition() {
790 var start = this._lexer.token;
791 var description = this.parseDescription();
792 this.expectKeyword('type');
793 var name = this.parseName();
794 var interfaces = this.parseImplementsInterfaces();
795 var directives = this.parseDirectives(true);
796 var fields = this.parseFieldsDefinition();
797 return {
798 kind: Kind.OBJECT_TYPE_DEFINITION,
799 description: description,
800 name: name,
801 interfaces: interfaces,
802 directives: directives,
803 fields: fields,
804 loc: this.loc(start)
805 };
806 }
807 /**
808 * ImplementsInterfaces :
809 * - implements `&`? NamedType
810 * - ImplementsInterfaces & NamedType
811 */
812 ;
813
814 _proto.parseImplementsInterfaces = function parseImplementsInterfaces() {
815 var _this$_options2;
816
817 if (!this.expectOptionalKeyword('implements')) {
818 return [];
819 }
820
821 if (((_this$_options2 = this._options) === null || _this$_options2 === void 0 ? void 0 : _this$_options2.allowLegacySDLImplementsInterfaces) === true) {
822 var types = []; // Optional leading ampersand
823
824 this.expectOptionalToken(TokenKind.AMP);
825
826 do {
827 types.push(this.parseNamedType());
828 } while (this.expectOptionalToken(TokenKind.AMP) || this.peek(TokenKind.NAME));
829
830 return types;
831 }
832
833 return this.delimitedMany(TokenKind.AMP, this.parseNamedType);
834 }
835 /**
836 * FieldsDefinition : { FieldDefinition+ }
837 */
838 ;
839
840 _proto.parseFieldsDefinition = function parseFieldsDefinition() {
841 var _this$_options3;
842
843 // Legacy support for the SDL?
844 if (((_this$_options3 = this._options) === null || _this$_options3 === void 0 ? void 0 : _this$_options3.allowLegacySDLEmptyFields) === true && this.peek(TokenKind.BRACE_L) && this._lexer.lookahead().kind === TokenKind.BRACE_R) {
845 this._lexer.advance();
846
847 this._lexer.advance();
848
849 return [];
850 }
851
852 return this.optionalMany(TokenKind.BRACE_L, this.parseFieldDefinition, TokenKind.BRACE_R);
853 }
854 /**
855 * FieldDefinition :
856 * - Description? Name ArgumentsDefinition? : Type Directives[Const]?
857 */
858 ;
859
860 _proto.parseFieldDefinition = function parseFieldDefinition() {
861 var start = this._lexer.token;
862 var description = this.parseDescription();
863 var name = this.parseName();
864 var args = this.parseArgumentDefs();
865 this.expectToken(TokenKind.COLON);
866 var type = this.parseTypeReference();
867 var directives = this.parseDirectives(true);
868 return {
869 kind: Kind.FIELD_DEFINITION,
870 description: description,
871 name: name,
872 arguments: args,
873 type: type,
874 directives: directives,
875 loc: this.loc(start)
876 };
877 }
878 /**
879 * ArgumentsDefinition : ( InputValueDefinition+ )
880 */
881 ;
882
883 _proto.parseArgumentDefs = function parseArgumentDefs() {
884 return this.optionalMany(TokenKind.PAREN_L, this.parseInputValueDef, TokenKind.PAREN_R);
885 }
886 /**
887 * InputValueDefinition :
888 * - Description? Name : Type DefaultValue? Directives[Const]?
889 */
890 ;
891
892 _proto.parseInputValueDef = function parseInputValueDef() {
893 var start = this._lexer.token;
894 var description = this.parseDescription();
895 var name = this.parseName();
896 this.expectToken(TokenKind.COLON);
897 var type = this.parseTypeReference();
898 var defaultValue;
899
900 if (this.expectOptionalToken(TokenKind.EQUALS)) {
901 defaultValue = this.parseValueLiteral(true);
902 }
903
904 var directives = this.parseDirectives(true);
905 return {
906 kind: Kind.INPUT_VALUE_DEFINITION,
907 description: description,
908 name: name,
909 type: type,
910 defaultValue: defaultValue,
911 directives: directives,
912 loc: this.loc(start)
913 };
914 }
915 /**
916 * InterfaceTypeDefinition :
917 * - Description? interface Name Directives[Const]? FieldsDefinition?
918 */
919 ;
920
921 _proto.parseInterfaceTypeDefinition = function parseInterfaceTypeDefinition() {
922 var start = this._lexer.token;
923 var description = this.parseDescription();
924 this.expectKeyword('interface');
925 var name = this.parseName();
926 var interfaces = this.parseImplementsInterfaces();
927 var directives = this.parseDirectives(true);
928 var fields = this.parseFieldsDefinition();
929 return {
930 kind: Kind.INTERFACE_TYPE_DEFINITION,
931 description: description,
932 name: name,
933 interfaces: interfaces,
934 directives: directives,
935 fields: fields,
936 loc: this.loc(start)
937 };
938 }
939 /**
940 * UnionTypeDefinition :
941 * - Description? union Name Directives[Const]? UnionMemberTypes?
942 */
943 ;
944
945 _proto.parseUnionTypeDefinition = function parseUnionTypeDefinition() {
946 var start = this._lexer.token;
947 var description = this.parseDescription();
948 this.expectKeyword('union');
949 var name = this.parseName();
950 var directives = this.parseDirectives(true);
951 var types = this.parseUnionMemberTypes();
952 return {
953 kind: Kind.UNION_TYPE_DEFINITION,
954 description: description,
955 name: name,
956 directives: directives,
957 types: types,
958 loc: this.loc(start)
959 };
960 }
961 /**
962 * UnionMemberTypes :
963 * - = `|`? NamedType
964 * - UnionMemberTypes | NamedType
965 */
966 ;
967
968 _proto.parseUnionMemberTypes = function parseUnionMemberTypes() {
969 return this.expectOptionalToken(TokenKind.EQUALS) ? this.delimitedMany(TokenKind.PIPE, this.parseNamedType) : [];
970 }
971 /**
972 * EnumTypeDefinition :
973 * - Description? enum Name Directives[Const]? EnumValuesDefinition?
974 */
975 ;
976
977 _proto.parseEnumTypeDefinition = function parseEnumTypeDefinition() {
978 var start = this._lexer.token;
979 var description = this.parseDescription();
980 this.expectKeyword('enum');
981 var name = this.parseName();
982 var directives = this.parseDirectives(true);
983 var values = this.parseEnumValuesDefinition();
984 return {
985 kind: Kind.ENUM_TYPE_DEFINITION,
986 description: description,
987 name: name,
988 directives: directives,
989 values: values,
990 loc: this.loc(start)
991 };
992 }
993 /**
994 * EnumValuesDefinition : { EnumValueDefinition+ }
995 */
996 ;
997
998 _proto.parseEnumValuesDefinition = function parseEnumValuesDefinition() {
999 return this.optionalMany(TokenKind.BRACE_L, this.parseEnumValueDefinition, TokenKind.BRACE_R);
1000 }
1001 /**
1002 * EnumValueDefinition : Description? EnumValue Directives[Const]?
1003 *
1004 * EnumValue : Name
1005 */
1006 ;
1007
1008 _proto.parseEnumValueDefinition = function parseEnumValueDefinition() {
1009 var start = this._lexer.token;
1010 var description = this.parseDescription();
1011 var name = this.parseName();
1012 var directives = this.parseDirectives(true);
1013 return {
1014 kind: Kind.ENUM_VALUE_DEFINITION,
1015 description: description,
1016 name: name,
1017 directives: directives,
1018 loc: this.loc(start)
1019 };
1020 }
1021 /**
1022 * InputObjectTypeDefinition :
1023 * - Description? input Name Directives[Const]? InputFieldsDefinition?
1024 */
1025 ;
1026
1027 _proto.parseInputObjectTypeDefinition = function parseInputObjectTypeDefinition() {
1028 var start = this._lexer.token;
1029 var description = this.parseDescription();
1030 this.expectKeyword('input');
1031 var name = this.parseName();
1032 var directives = this.parseDirectives(true);
1033 var fields = this.parseInputFieldsDefinition();
1034 return {
1035 kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
1036 description: description,
1037 name: name,
1038 directives: directives,
1039 fields: fields,
1040 loc: this.loc(start)
1041 };
1042 }
1043 /**
1044 * InputFieldsDefinition : { InputValueDefinition+ }
1045 */
1046 ;
1047
1048 _proto.parseInputFieldsDefinition = function parseInputFieldsDefinition() {
1049 return this.optionalMany(TokenKind.BRACE_L, this.parseInputValueDef, TokenKind.BRACE_R);
1050 }
1051 /**
1052 * TypeSystemExtension :
1053 * - SchemaExtension
1054 * - TypeExtension
1055 *
1056 * TypeExtension :
1057 * - ScalarTypeExtension
1058 * - ObjectTypeExtension
1059 * - InterfaceTypeExtension
1060 * - UnionTypeExtension
1061 * - EnumTypeExtension
1062 * - InputObjectTypeDefinition
1063 */
1064 ;
1065
1066 _proto.parseTypeSystemExtension = function parseTypeSystemExtension() {
1067 var keywordToken = this._lexer.lookahead();
1068
1069 if (keywordToken.kind === TokenKind.NAME) {
1070 switch (keywordToken.value) {
1071 case 'schema':
1072 return this.parseSchemaExtension();
1073
1074 case 'scalar':
1075 return this.parseScalarTypeExtension();
1076
1077 case 'type':
1078 return this.parseObjectTypeExtension();
1079
1080 case 'interface':
1081 return this.parseInterfaceTypeExtension();
1082
1083 case 'union':
1084 return this.parseUnionTypeExtension();
1085
1086 case 'enum':
1087 return this.parseEnumTypeExtension();
1088
1089 case 'input':
1090 return this.parseInputObjectTypeExtension();
1091 }
1092 }
1093
1094 throw this.unexpected(keywordToken);
1095 }
1096 /**
1097 * SchemaExtension :
1098 * - extend schema Directives[Const]? { OperationTypeDefinition+ }
1099 * - extend schema Directives[Const]
1100 */
1101 ;
1102
1103 _proto.parseSchemaExtension = function parseSchemaExtension() {
1104 var start = this._lexer.token;
1105 this.expectKeyword('extend');
1106 this.expectKeyword('schema');
1107 var directives = this.parseDirectives(true);
1108 var operationTypes = this.optionalMany(TokenKind.BRACE_L, this.parseOperationTypeDefinition, TokenKind.BRACE_R);
1109
1110 if (directives.length === 0 && operationTypes.length === 0) {
1111 throw this.unexpected();
1112 }
1113
1114 return {
1115 kind: Kind.SCHEMA_EXTENSION,
1116 directives: directives,
1117 operationTypes: operationTypes,
1118 loc: this.loc(start)
1119 };
1120 }
1121 /**
1122 * ScalarTypeExtension :
1123 * - extend scalar Name Directives[Const]
1124 */
1125 ;
1126
1127 _proto.parseScalarTypeExtension = function parseScalarTypeExtension() {
1128 var start = this._lexer.token;
1129 this.expectKeyword('extend');
1130 this.expectKeyword('scalar');
1131 var name = this.parseName();
1132 var directives = this.parseDirectives(true);
1133
1134 if (directives.length === 0) {
1135 throw this.unexpected();
1136 }
1137
1138 return {
1139 kind: Kind.SCALAR_TYPE_EXTENSION,
1140 name: name,
1141 directives: directives,
1142 loc: this.loc(start)
1143 };
1144 }
1145 /**
1146 * ObjectTypeExtension :
1147 * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
1148 * - extend type Name ImplementsInterfaces? Directives[Const]
1149 * - extend type Name ImplementsInterfaces
1150 */
1151 ;
1152
1153 _proto.parseObjectTypeExtension = function parseObjectTypeExtension() {
1154 var start = this._lexer.token;
1155 this.expectKeyword('extend');
1156 this.expectKeyword('type');
1157 var name = this.parseName();
1158 var interfaces = this.parseImplementsInterfaces();
1159 var directives = this.parseDirectives(true);
1160 var fields = this.parseFieldsDefinition();
1161
1162 if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
1163 throw this.unexpected();
1164 }
1165
1166 return {
1167 kind: Kind.OBJECT_TYPE_EXTENSION,
1168 name: name,
1169 interfaces: interfaces,
1170 directives: directives,
1171 fields: fields,
1172 loc: this.loc(start)
1173 };
1174 }
1175 /**
1176 * InterfaceTypeExtension :
1177 * - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
1178 * - extend interface Name ImplementsInterfaces? Directives[Const]
1179 * - extend interface Name ImplementsInterfaces
1180 */
1181 ;
1182
1183 _proto.parseInterfaceTypeExtension = function parseInterfaceTypeExtension() {
1184 var start = this._lexer.token;
1185 this.expectKeyword('extend');
1186 this.expectKeyword('interface');
1187 var name = this.parseName();
1188 var interfaces = this.parseImplementsInterfaces();
1189 var directives = this.parseDirectives(true);
1190 var fields = this.parseFieldsDefinition();
1191
1192 if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) {
1193 throw this.unexpected();
1194 }
1195
1196 return {
1197 kind: Kind.INTERFACE_TYPE_EXTENSION,
1198 name: name,
1199 interfaces: interfaces,
1200 directives: directives,
1201 fields: fields,
1202 loc: this.loc(start)
1203 };
1204 }
1205 /**
1206 * UnionTypeExtension :
1207 * - extend union Name Directives[Const]? UnionMemberTypes
1208 * - extend union Name Directives[Const]
1209 */
1210 ;
1211
1212 _proto.parseUnionTypeExtension = function parseUnionTypeExtension() {
1213 var start = this._lexer.token;
1214 this.expectKeyword('extend');
1215 this.expectKeyword('union');
1216 var name = this.parseName();
1217 var directives = this.parseDirectives(true);
1218 var types = this.parseUnionMemberTypes();
1219
1220 if (directives.length === 0 && types.length === 0) {
1221 throw this.unexpected();
1222 }
1223
1224 return {
1225 kind: Kind.UNION_TYPE_EXTENSION,
1226 name: name,
1227 directives: directives,
1228 types: types,
1229 loc: this.loc(start)
1230 };
1231 }
1232 /**
1233 * EnumTypeExtension :
1234 * - extend enum Name Directives[Const]? EnumValuesDefinition
1235 * - extend enum Name Directives[Const]
1236 */
1237 ;
1238
1239 _proto.parseEnumTypeExtension = function parseEnumTypeExtension() {
1240 var start = this._lexer.token;
1241 this.expectKeyword('extend');
1242 this.expectKeyword('enum');
1243 var name = this.parseName();
1244 var directives = this.parseDirectives(true);
1245 var values = this.parseEnumValuesDefinition();
1246
1247 if (directives.length === 0 && values.length === 0) {
1248 throw this.unexpected();
1249 }
1250
1251 return {
1252 kind: Kind.ENUM_TYPE_EXTENSION,
1253 name: name,
1254 directives: directives,
1255 values: values,
1256 loc: this.loc(start)
1257 };
1258 }
1259 /**
1260 * InputObjectTypeExtension :
1261 * - extend input Name Directives[Const]? InputFieldsDefinition
1262 * - extend input Name Directives[Const]
1263 */
1264 ;
1265
1266 _proto.parseInputObjectTypeExtension = function parseInputObjectTypeExtension() {
1267 var start = this._lexer.token;
1268 this.expectKeyword('extend');
1269 this.expectKeyword('input');
1270 var name = this.parseName();
1271 var directives = this.parseDirectives(true);
1272 var fields = this.parseInputFieldsDefinition();
1273
1274 if (directives.length === 0 && fields.length === 0) {
1275 throw this.unexpected();
1276 }
1277
1278 return {
1279 kind: Kind.INPUT_OBJECT_TYPE_EXTENSION,
1280 name: name,
1281 directives: directives,
1282 fields: fields,
1283 loc: this.loc(start)
1284 };
1285 }
1286 /**
1287 * DirectiveDefinition :
1288 * - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
1289 */
1290 ;
1291
1292 _proto.parseDirectiveDefinition = function parseDirectiveDefinition() {
1293 var start = this._lexer.token;
1294 var description = this.parseDescription();
1295 this.expectKeyword('directive');
1296 this.expectToken(TokenKind.AT);
1297 var name = this.parseName();
1298 var args = this.parseArgumentDefs();
1299 var repeatable = this.expectOptionalKeyword('repeatable');
1300 this.expectKeyword('on');
1301 var locations = this.parseDirectiveLocations();
1302 return {
1303 kind: Kind.DIRECTIVE_DEFINITION,
1304 description: description,
1305 name: name,
1306 arguments: args,
1307 repeatable: repeatable,
1308 locations: locations,
1309 loc: this.loc(start)
1310 };
1311 }
1312 /**
1313 * DirectiveLocations :
1314 * - `|`? DirectiveLocation
1315 * - DirectiveLocations | DirectiveLocation
1316 */
1317 ;
1318
1319 _proto.parseDirectiveLocations = function parseDirectiveLocations() {
1320 return this.delimitedMany(TokenKind.PIPE, this.parseDirectiveLocation);
1321 }
1322 /*
1323 * DirectiveLocation :
1324 * - ExecutableDirectiveLocation
1325 * - TypeSystemDirectiveLocation
1326 *
1327 * ExecutableDirectiveLocation : one of
1328 * `QUERY`
1329 * `MUTATION`
1330 * `SUBSCRIPTION`
1331 * `FIELD`
1332 * `FRAGMENT_DEFINITION`
1333 * `FRAGMENT_SPREAD`
1334 * `INLINE_FRAGMENT`
1335 *
1336 * TypeSystemDirectiveLocation : one of
1337 * `SCHEMA`
1338 * `SCALAR`
1339 * `OBJECT`
1340 * `FIELD_DEFINITION`
1341 * `ARGUMENT_DEFINITION`
1342 * `INTERFACE`
1343 * `UNION`
1344 * `ENUM`
1345 * `ENUM_VALUE`
1346 * `INPUT_OBJECT`
1347 * `INPUT_FIELD_DEFINITION`
1348 */
1349 ;
1350
1351 _proto.parseDirectiveLocation = function parseDirectiveLocation() {
1352 var start = this._lexer.token;
1353 var name = this.parseName();
1354
1355 if (DirectiveLocation[name.value] !== undefined) {
1356 return name;
1357 }
1358
1359 throw this.unexpected(start);
1360 } // Core parsing utility functions
1361
1362 /**
1363 * Returns a location object, used to identify the place in the source that created a given parsed object.
1364 */
1365 ;
1366
1367 _proto.loc = function loc(startToken) {
1368 var _this$_options4;
1369
1370 if (((_this$_options4 = this._options) === null || _this$_options4 === void 0 ? void 0 : _this$_options4.noLocation) !== true) {
1371 return new Location(startToken, this._lexer.lastToken, this._lexer.source);
1372 }
1373 }
1374 /**
1375 * Determines if the next token is of a given kind
1376 */
1377 ;
1378
1379 _proto.peek = function peek(kind) {
1380 return this._lexer.token.kind === kind;
1381 }
1382 /**
1383 * If the next token is of the given kind, return that token after advancing the lexer.
1384 * Otherwise, do not change the parser state and throw an error.
1385 */
1386 ;
1387
1388 _proto.expectToken = function expectToken(kind) {
1389 var token = this._lexer.token;
1390
1391 if (token.kind === kind) {
1392 this._lexer.advance();
1393
1394 return token;
1395 }
1396
1397 throw syntaxError(this._lexer.source, token.start, "Expected ".concat(getTokenKindDesc(kind), ", found ").concat(getTokenDesc(token), "."));
1398 }
1399 /**
1400 * If the next token is of the given kind, return that token after advancing the lexer.
1401 * Otherwise, do not change the parser state and return undefined.
1402 */
1403 ;
1404
1405 _proto.expectOptionalToken = function expectOptionalToken(kind) {
1406 var token = this._lexer.token;
1407
1408 if (token.kind === kind) {
1409 this._lexer.advance();
1410
1411 return token;
1412 }
1413
1414 return undefined;
1415 }
1416 /**
1417 * If the next token is a given keyword, advance the lexer.
1418 * Otherwise, do not change the parser state and throw an error.
1419 */
1420 ;
1421
1422 _proto.expectKeyword = function expectKeyword(value) {
1423 var token = this._lexer.token;
1424
1425 if (token.kind === TokenKind.NAME && token.value === value) {
1426 this._lexer.advance();
1427 } else {
1428 throw syntaxError(this._lexer.source, token.start, "Expected \"".concat(value, "\", found ").concat(getTokenDesc(token), "."));
1429 }
1430 }
1431 /**
1432 * If the next token is a given keyword, return "true" after advancing the lexer.
1433 * Otherwise, do not change the parser state and return "false".
1434 */
1435 ;
1436
1437 _proto.expectOptionalKeyword = function expectOptionalKeyword(value) {
1438 var token = this._lexer.token;
1439
1440 if (token.kind === TokenKind.NAME && token.value === value) {
1441 this._lexer.advance();
1442
1443 return true;
1444 }
1445
1446 return false;
1447 }
1448 /**
1449 * Helper function for creating an error when an unexpected lexed token is encountered.
1450 */
1451 ;
1452
1453 _proto.unexpected = function unexpected(atToken) {
1454 var token = atToken !== null && atToken !== void 0 ? atToken : this._lexer.token;
1455 return syntaxError(this._lexer.source, token.start, "Unexpected ".concat(getTokenDesc(token), "."));
1456 }
1457 /**
1458 * Returns a possibly empty list of parse nodes, determined by the parseFn.
1459 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
1460 * Advances the parser to the next lex token after the closing token.
1461 */
1462 ;
1463
1464 _proto.any = function any(openKind, parseFn, closeKind) {
1465 this.expectToken(openKind);
1466 var nodes = [];
1467
1468 while (!this.expectOptionalToken(closeKind)) {
1469 nodes.push(parseFn.call(this));
1470 }
1471
1472 return nodes;
1473 }
1474 /**
1475 * Returns a list of parse nodes, determined by the parseFn.
1476 * It can be empty only if open token is missing otherwise it will always return non-empty list
1477 * that begins with a lex token of openKind and ends with a lex token of closeKind.
1478 * Advances the parser to the next lex token after the closing token.
1479 */
1480 ;
1481
1482 _proto.optionalMany = function optionalMany(openKind, parseFn, closeKind) {
1483 if (this.expectOptionalToken(openKind)) {
1484 var nodes = [];
1485
1486 do {
1487 nodes.push(parseFn.call(this));
1488 } while (!this.expectOptionalToken(closeKind));
1489
1490 return nodes;
1491 }
1492
1493 return [];
1494 }
1495 /**
1496 * Returns a non-empty list of parse nodes, determined by the parseFn.
1497 * This list begins with a lex token of openKind and ends with a lex token of closeKind.
1498 * Advances the parser to the next lex token after the closing token.
1499 */
1500 ;
1501
1502 _proto.many = function many(openKind, parseFn, closeKind) {
1503 this.expectToken(openKind);
1504 var nodes = [];
1505
1506 do {
1507 nodes.push(parseFn.call(this));
1508 } while (!this.expectOptionalToken(closeKind));
1509
1510 return nodes;
1511 }
1512 /**
1513 * Returns a non-empty list of parse nodes, determined by the parseFn.
1514 * This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind.
1515 * Advances the parser to the next lex token after last item in the list.
1516 */
1517 ;
1518
1519 _proto.delimitedMany = function delimitedMany(delimiterKind, parseFn) {
1520 this.expectOptionalToken(delimiterKind);
1521 var nodes = [];
1522
1523 do {
1524 nodes.push(parseFn.call(this));
1525 } while (this.expectOptionalToken(delimiterKind));
1526
1527 return nodes;
1528 };
1529
1530 return Parser;
1531}();
1532/**
1533 * A helper function to describe a token as a string for debugging.
1534 */
1535
1536function getTokenDesc(token) {
1537 var value = token.value;
1538 return getTokenKindDesc(token.kind) + (value != null ? " \"".concat(value, "\"") : '');
1539}
1540/**
1541 * A helper function to describe a token kind as a string for debugging.
1542 */
1543
1544
1545function getTokenKindDesc(kind) {
1546 return isPunctuatorTokenKind(kind) ? "\"".concat(kind, "\"") : kind;
1547}