parser.js 40 KB

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