1 | import { syntaxError } from "../error/syntaxError.mjs";
|
2 | import { Kind } from "./kinds.mjs";
|
3 | import { Location } from "./ast.mjs";
|
4 | import { TokenKind } from "./tokenKind.mjs";
|
5 | import { Source, isSource } from "./source.mjs";
|
6 | import { DirectiveLocation } from "./directiveLocation.mjs";
|
7 | import { Lexer, isPunctuatorTokenKind } from "./lexer.mjs";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function parse(source, options) {
|
17 | var parser = new Parser(source, options);
|
18 | return parser.parseDocument();
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export 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 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export 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 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export var Parser = 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 |
|
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 | }
|
89 |
|
90 | |
91 |
|
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 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
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 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
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 |
|
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 |
|
210 |
|
211 | ;
|
212 |
|
213 | _proto.parseVariableDefinitions = function parseVariableDefinitions() {
|
214 | return this.optionalMany(TokenKind.PAREN_L, this.parseVariableDefinition, TokenKind.PAREN_R);
|
215 | }
|
216 | |
217 |
|
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 |
|
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 |
|
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 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | ;
|
266 |
|
267 | _proto.parseSelection = function parseSelection() {
|
268 | return this.peek(TokenKind.SPREAD) ? this.parseFragment() : this.parseField();
|
269 | }
|
270 | |
271 |
|
272 |
|
273 |
|
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 |
|
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 |
|
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 | }
|
335 |
|
336 | |
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
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 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | ;
|
374 |
|
375 | _proto.parseFragmentDefinition = function parseFragmentDefinition() {
|
376 | var _this$_options;
|
377 |
|
378 | var start = this._lexer.token;
|
379 | this.expectKeyword('fragment');
|
380 |
|
381 |
|
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 |
|
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 | }
|
416 |
|
417 | |
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
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 |
|
526 |
|
527 |
|
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 |
|
548 |
|
549 |
|
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 |
|
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 | }
|
584 |
|
585 | |
586 |
|
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 |
|
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 | }
|
614 |
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
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 |
|
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 | }
|
662 |
|
663 | |
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 | ;
|
678 |
|
679 | _proto.parseTypeSystemDefinition = function parseTypeSystemDefinition() {
|
680 |
|
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 |
|
719 |
|
720 | ;
|
721 |
|
722 | _proto.parseDescription = function parseDescription() {
|
723 | if (this.peekDescription()) {
|
724 | return this.parseStringLiteral();
|
725 | }
|
726 | }
|
727 | |
728 |
|
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 |
|
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 |
|
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 |
|
784 |
|
785 |
|
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 |
|
809 |
|
810 |
|
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 = [];
|
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 |
|
837 |
|
838 | ;
|
839 |
|
840 | _proto.parseFieldsDefinition = function parseFieldsDefinition() {
|
841 | var _this$_options3;
|
842 |
|
843 |
|
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 |
|
856 |
|
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 |
|
880 |
|
881 | ;
|
882 |
|
883 | _proto.parseArgumentDefs = function parseArgumentDefs() {
|
884 | return this.optionalMany(TokenKind.PAREN_L, this.parseInputValueDef, TokenKind.PAREN_R);
|
885 | }
|
886 | |
887 |
|
888 |
|
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 |
|
917 |
|
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 |
|
941 |
|
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 |
|
963 |
|
964 |
|
965 |
|
966 | ;
|
967 |
|
968 | _proto.parseUnionMemberTypes = function parseUnionMemberTypes() {
|
969 | return this.expectOptionalToken(TokenKind.EQUALS) ? this.delimitedMany(TokenKind.PIPE, this.parseNamedType) : [];
|
970 | }
|
971 | |
972 |
|
973 |
|
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 |
|
995 |
|
996 | ;
|
997 |
|
998 | _proto.parseEnumValuesDefinition = function parseEnumValuesDefinition() {
|
999 | return this.optionalMany(TokenKind.BRACE_L, this.parseEnumValueDefinition, TokenKind.BRACE_R);
|
1000 | }
|
1001 | |
1002 |
|
1003 |
|
1004 |
|
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 |
|
1023 |
|
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 |
|
1045 |
|
1046 | ;
|
1047 |
|
1048 | _proto.parseInputFieldsDefinition = function parseInputFieldsDefinition() {
|
1049 | return this.optionalMany(TokenKind.BRACE_L, this.parseInputValueDef, TokenKind.BRACE_R);
|
1050 | }
|
1051 | |
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
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 |
|
1098 |
|
1099 |
|
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 |
|
1123 |
|
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 |
|
1147 |
|
1148 |
|
1149 |
|
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 |
|
1177 |
|
1178 |
|
1179 |
|
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 |
|
1207 |
|
1208 |
|
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 |
|
1234 |
|
1235 |
|
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 |
|
1261 |
|
1262 |
|
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 |
|
1288 |
|
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 |
|
1314 |
|
1315 |
|
1316 |
|
1317 | ;
|
1318 |
|
1319 | _proto.parseDirectiveLocations = function parseDirectiveLocations() {
|
1320 | return this.delimitedMany(TokenKind.PIPE, this.parseDirectiveLocation);
|
1321 | }
|
1322 | |
1323 |
|
1324 |
|
1325 |
|
1326 |
|
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
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 | }
|
1361 |
|
1362 | |
1363 |
|
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 |
|
1376 |
|
1377 | ;
|
1378 |
|
1379 | _proto.peek = function peek(kind) {
|
1380 | return this._lexer.token.kind === kind;
|
1381 | }
|
1382 | |
1383 |
|
1384 |
|
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 |
|
1401 |
|
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 |
|
1418 |
|
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 |
|
1433 |
|
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 |
|
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 |
|
1459 |
|
1460 |
|
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 |
|
1476 |
|
1477 |
|
1478 |
|
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 |
|
1497 |
|
1498 |
|
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 |
|
1514 |
|
1515 |
|
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 |
|
1534 |
|
1535 |
|
1536 | function getTokenDesc(token) {
|
1537 | var value = token.value;
|
1538 | return getTokenKindDesc(token.kind) + (value != null ? " \"".concat(value, "\"") : '');
|
1539 | }
|
1540 |
|
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 | function getTokenKindDesc(kind) {
|
1546 | return isPunctuatorTokenKind(kind) ? "\"".concat(kind, "\"") : kind;
|
1547 | }
|