parser.js 42 KB

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