parser.js 41 KB

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