parser.mjs 41 KB

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