parser.js 42 KB

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