parser.mjs 39 KB

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