parser.mjs 40 KB

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