parser.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import type { Maybe } from '../jsutils/Maybe';
  2. import type { GraphQLError } from '../error/GraphQLError';
  3. import type {
  4. ArgumentNode,
  5. ConstArgumentNode,
  6. ConstDirectiveNode,
  7. ConstListValueNode,
  8. ConstObjectFieldNode,
  9. ConstObjectValueNode,
  10. ConstValueNode,
  11. DefinitionNode,
  12. DirectiveDefinitionNode,
  13. DirectiveNode,
  14. DocumentNode,
  15. EnumTypeDefinitionNode,
  16. EnumTypeExtensionNode,
  17. EnumValueDefinitionNode,
  18. FieldDefinitionNode,
  19. FieldNode,
  20. FragmentDefinitionNode,
  21. FragmentSpreadNode,
  22. InlineFragmentNode,
  23. InputObjectTypeDefinitionNode,
  24. InputObjectTypeExtensionNode,
  25. InputValueDefinitionNode,
  26. InterfaceTypeDefinitionNode,
  27. InterfaceTypeExtensionNode,
  28. ListValueNode,
  29. NamedTypeNode,
  30. NameNode,
  31. ObjectFieldNode,
  32. ObjectTypeDefinitionNode,
  33. ObjectTypeExtensionNode,
  34. ObjectValueNode,
  35. OperationDefinitionNode,
  36. OperationTypeDefinitionNode,
  37. ScalarTypeDefinitionNode,
  38. ScalarTypeExtensionNode,
  39. SchemaDefinitionNode,
  40. SchemaExtensionNode,
  41. SelectionNode,
  42. SelectionSetNode,
  43. StringValueNode,
  44. Token,
  45. TypeNode,
  46. TypeSystemExtensionNode,
  47. UnionTypeDefinitionNode,
  48. UnionTypeExtensionNode,
  49. ValueNode,
  50. VariableDefinitionNode,
  51. VariableNode,
  52. } from './ast';
  53. import { Location, OperationTypeNode } from './ast';
  54. import { Lexer } from './lexer';
  55. import { Source } from './source';
  56. import { TokenKind } from './tokenKind';
  57. /**
  58. * Configuration options to control parser behavior
  59. */
  60. export interface ParseOptions {
  61. /**
  62. * By default, the parser creates AST nodes that know the location
  63. * in the source that they correspond to. This configuration flag
  64. * disables that behavior for performance or testing.
  65. */
  66. noLocation?: boolean;
  67. /**
  68. * Parser CPU and memory usage is linear to the number of tokens in a document
  69. * however in extreme cases it becomes quadratic due to memory exhaustion.
  70. * Parsing happens before validation so even invalid queries can burn lots of
  71. * CPU time and memory.
  72. * To prevent this you can set a maximum number of tokens allowed within a document.
  73. */
  74. maxTokens?: number | undefined;
  75. /**
  76. * @deprecated will be removed in the v17.0.0
  77. *
  78. * If enabled, the parser will understand and parse variable definitions
  79. * contained in a fragment definition. They'll be represented in the
  80. * `variableDefinitions` field of the FragmentDefinitionNode.
  81. *
  82. * The syntax is identical to normal, query-defined variables. For example:
  83. *
  84. * ```graphql
  85. * fragment A($var: Boolean = false) on T {
  86. * ...
  87. * }
  88. * ```
  89. */
  90. allowLegacyFragmentVariables?: boolean;
  91. }
  92. /**
  93. * Given a GraphQL source, parses it into a Document.
  94. * Throws GraphQLError if a syntax error is encountered.
  95. */
  96. export declare function parse(
  97. source: string | Source,
  98. options?: ParseOptions | undefined,
  99. ): DocumentNode;
  100. /**
  101. * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for
  102. * that value.
  103. * Throws GraphQLError if a syntax error is encountered.
  104. *
  105. * This is useful within tools that operate upon GraphQL Values directly and
  106. * in isolation of complete GraphQL documents.
  107. *
  108. * Consider providing the results to the utility function: valueFromAST().
  109. */
  110. export declare function parseValue(
  111. source: string | Source,
  112. options?: ParseOptions | undefined,
  113. ): ValueNode;
  114. /**
  115. * Similar to parseValue(), but raises a parse error if it encounters a
  116. * variable. The return type will be a constant value.
  117. */
  118. export declare function parseConstValue(
  119. source: string | Source,
  120. options?: ParseOptions | undefined,
  121. ): ConstValueNode;
  122. /**
  123. * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for
  124. * that type.
  125. * Throws GraphQLError if a syntax error is encountered.
  126. *
  127. * This is useful within tools that operate upon GraphQL Types directly and
  128. * in isolation of complete GraphQL documents.
  129. *
  130. * Consider providing the results to the utility function: typeFromAST().
  131. */
  132. export declare function parseType(
  133. source: string | Source,
  134. options?: ParseOptions | undefined,
  135. ): TypeNode;
  136. /**
  137. * This class is exported only to assist people in implementing their own parsers
  138. * without duplicating too much code and should be used only as last resort for cases
  139. * such as experimental syntax or if certain features could not be contributed upstream.
  140. *
  141. * It is still part of the internal API and is versioned, so any changes to it are never
  142. * considered breaking changes. If you still need to support multiple versions of the
  143. * library, please use the `versionInfo` variable for version detection.
  144. *
  145. * @internal
  146. */
  147. export declare class Parser {
  148. protected _options: ParseOptions;
  149. protected _lexer: Lexer;
  150. protected _tokenCounter: number;
  151. constructor(source: string | Source, options?: ParseOptions);
  152. /**
  153. * Converts a name lex token into a name parse node.
  154. */
  155. parseName(): NameNode;
  156. /**
  157. * Document : Definition+
  158. */
  159. parseDocument(): DocumentNode;
  160. /**
  161. * Definition :
  162. * - ExecutableDefinition
  163. * - TypeSystemDefinition
  164. * - TypeSystemExtension
  165. *
  166. * ExecutableDefinition :
  167. * - OperationDefinition
  168. * - FragmentDefinition
  169. *
  170. * TypeSystemDefinition :
  171. * - SchemaDefinition
  172. * - TypeDefinition
  173. * - DirectiveDefinition
  174. *
  175. * TypeDefinition :
  176. * - ScalarTypeDefinition
  177. * - ObjectTypeDefinition
  178. * - InterfaceTypeDefinition
  179. * - UnionTypeDefinition
  180. * - EnumTypeDefinition
  181. * - InputObjectTypeDefinition
  182. */
  183. parseDefinition(): DefinitionNode;
  184. /**
  185. * OperationDefinition :
  186. * - SelectionSet
  187. * - OperationType Name? VariableDefinitions? Directives? SelectionSet
  188. */
  189. parseOperationDefinition(): OperationDefinitionNode;
  190. /**
  191. * OperationType : one of query mutation subscription
  192. */
  193. parseOperationType(): OperationTypeNode;
  194. /**
  195. * VariableDefinitions : ( VariableDefinition+ )
  196. */
  197. parseVariableDefinitions(): Array<VariableDefinitionNode>;
  198. /**
  199. * VariableDefinition : Variable : Type DefaultValue? Directives[Const]?
  200. */
  201. parseVariableDefinition(): VariableDefinitionNode;
  202. /**
  203. * Variable : $ Name
  204. */
  205. parseVariable(): VariableNode;
  206. /**
  207. * ```
  208. * SelectionSet : { Selection+ }
  209. * ```
  210. */
  211. parseSelectionSet(): SelectionSetNode;
  212. /**
  213. * Selection :
  214. * - Field
  215. * - FragmentSpread
  216. * - InlineFragment
  217. */
  218. parseSelection(): SelectionNode;
  219. /**
  220. * Field : Alias? Name Arguments? Directives? SelectionSet?
  221. *
  222. * Alias : Name :
  223. */
  224. parseField(): FieldNode;
  225. /**
  226. * Arguments[Const] : ( Argument[?Const]+ )
  227. */
  228. parseArguments(isConst: true): Array<ConstArgumentNode>;
  229. parseArguments(isConst: boolean): Array<ArgumentNode>;
  230. /**
  231. * Argument[Const] : Name : Value[?Const]
  232. */
  233. parseArgument(isConst: true): ConstArgumentNode;
  234. parseArgument(isConst?: boolean): ArgumentNode;
  235. parseConstArgument(): ConstArgumentNode;
  236. /**
  237. * Corresponds to both FragmentSpread and InlineFragment in the spec.
  238. *
  239. * FragmentSpread : ... FragmentName Directives?
  240. *
  241. * InlineFragment : ... TypeCondition? Directives? SelectionSet
  242. */
  243. parseFragment(): FragmentSpreadNode | InlineFragmentNode;
  244. /**
  245. * FragmentDefinition :
  246. * - fragment FragmentName on TypeCondition Directives? SelectionSet
  247. *
  248. * TypeCondition : NamedType
  249. */
  250. parseFragmentDefinition(): FragmentDefinitionNode;
  251. /**
  252. * FragmentName : Name but not `on`
  253. */
  254. parseFragmentName(): NameNode;
  255. /**
  256. * Value[Const] :
  257. * - [~Const] Variable
  258. * - IntValue
  259. * - FloatValue
  260. * - StringValue
  261. * - BooleanValue
  262. * - NullValue
  263. * - EnumValue
  264. * - ListValue[?Const]
  265. * - ObjectValue[?Const]
  266. *
  267. * BooleanValue : one of `true` `false`
  268. *
  269. * NullValue : `null`
  270. *
  271. * EnumValue : Name but not `true`, `false` or `null`
  272. */
  273. parseValueLiteral(isConst: true): ConstValueNode;
  274. parseValueLiteral(isConst: boolean): ValueNode;
  275. parseConstValueLiteral(): ConstValueNode;
  276. parseStringLiteral(): StringValueNode;
  277. /**
  278. * ListValue[Const] :
  279. * - [ ]
  280. * - [ Value[?Const]+ ]
  281. */
  282. parseList(isConst: true): ConstListValueNode;
  283. parseList(isConst: boolean): ListValueNode;
  284. /**
  285. * ```
  286. * ObjectValue[Const] :
  287. * - { }
  288. * - { ObjectField[?Const]+ }
  289. * ```
  290. */
  291. parseObject(isConst: true): ConstObjectValueNode;
  292. parseObject(isConst: boolean): ObjectValueNode;
  293. /**
  294. * ObjectField[Const] : Name : Value[?Const]
  295. */
  296. parseObjectField(isConst: true): ConstObjectFieldNode;
  297. parseObjectField(isConst: boolean): ObjectFieldNode;
  298. /**
  299. * Directives[Const] : Directive[?Const]+
  300. */
  301. parseDirectives(isConst: true): Array<ConstDirectiveNode>;
  302. parseDirectives(isConst: boolean): Array<DirectiveNode>;
  303. parseConstDirectives(): Array<ConstDirectiveNode>;
  304. /**
  305. * ```
  306. * Directive[Const] : @ Name Arguments[?Const]?
  307. * ```
  308. */
  309. parseDirective(isConst: true): ConstDirectiveNode;
  310. parseDirective(isConst: boolean): DirectiveNode;
  311. /**
  312. * Type :
  313. * - NamedType
  314. * - ListType
  315. * - NonNullType
  316. */
  317. parseTypeReference(): TypeNode;
  318. /**
  319. * NamedType : Name
  320. */
  321. parseNamedType(): NamedTypeNode;
  322. peekDescription(): boolean;
  323. /**
  324. * Description : StringValue
  325. */
  326. parseDescription(): undefined | StringValueNode;
  327. /**
  328. * ```
  329. * SchemaDefinition : Description? schema Directives[Const]? { OperationTypeDefinition+ }
  330. * ```
  331. */
  332. parseSchemaDefinition(): SchemaDefinitionNode;
  333. /**
  334. * OperationTypeDefinition : OperationType : NamedType
  335. */
  336. parseOperationTypeDefinition(): OperationTypeDefinitionNode;
  337. /**
  338. * ScalarTypeDefinition : Description? scalar Name Directives[Const]?
  339. */
  340. parseScalarTypeDefinition(): ScalarTypeDefinitionNode;
  341. /**
  342. * ObjectTypeDefinition :
  343. * Description?
  344. * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition?
  345. */
  346. parseObjectTypeDefinition(): ObjectTypeDefinitionNode;
  347. /**
  348. * ImplementsInterfaces :
  349. * - implements `&`? NamedType
  350. * - ImplementsInterfaces & NamedType
  351. */
  352. parseImplementsInterfaces(): Array<NamedTypeNode>;
  353. /**
  354. * ```
  355. * FieldsDefinition : { FieldDefinition+ }
  356. * ```
  357. */
  358. parseFieldsDefinition(): Array<FieldDefinitionNode>;
  359. /**
  360. * FieldDefinition :
  361. * - Description? Name ArgumentsDefinition? : Type Directives[Const]?
  362. */
  363. parseFieldDefinition(): FieldDefinitionNode;
  364. /**
  365. * ArgumentsDefinition : ( InputValueDefinition+ )
  366. */
  367. parseArgumentDefs(): Array<InputValueDefinitionNode>;
  368. /**
  369. * InputValueDefinition :
  370. * - Description? Name : Type DefaultValue? Directives[Const]?
  371. */
  372. parseInputValueDef(): InputValueDefinitionNode;
  373. /**
  374. * InterfaceTypeDefinition :
  375. * - Description? interface Name Directives[Const]? FieldsDefinition?
  376. */
  377. parseInterfaceTypeDefinition(): InterfaceTypeDefinitionNode;
  378. /**
  379. * UnionTypeDefinition :
  380. * - Description? union Name Directives[Const]? UnionMemberTypes?
  381. */
  382. parseUnionTypeDefinition(): UnionTypeDefinitionNode;
  383. /**
  384. * UnionMemberTypes :
  385. * - = `|`? NamedType
  386. * - UnionMemberTypes | NamedType
  387. */
  388. parseUnionMemberTypes(): Array<NamedTypeNode>;
  389. /**
  390. * EnumTypeDefinition :
  391. * - Description? enum Name Directives[Const]? EnumValuesDefinition?
  392. */
  393. parseEnumTypeDefinition(): EnumTypeDefinitionNode;
  394. /**
  395. * ```
  396. * EnumValuesDefinition : { EnumValueDefinition+ }
  397. * ```
  398. */
  399. parseEnumValuesDefinition(): Array<EnumValueDefinitionNode>;
  400. /**
  401. * EnumValueDefinition : Description? EnumValue Directives[Const]?
  402. */
  403. parseEnumValueDefinition(): EnumValueDefinitionNode;
  404. /**
  405. * EnumValue : Name but not `true`, `false` or `null`
  406. */
  407. parseEnumValueName(): NameNode;
  408. /**
  409. * InputObjectTypeDefinition :
  410. * - Description? input Name Directives[Const]? InputFieldsDefinition?
  411. */
  412. parseInputObjectTypeDefinition(): InputObjectTypeDefinitionNode;
  413. /**
  414. * ```
  415. * InputFieldsDefinition : { InputValueDefinition+ }
  416. * ```
  417. */
  418. parseInputFieldsDefinition(): Array<InputValueDefinitionNode>;
  419. /**
  420. * TypeSystemExtension :
  421. * - SchemaExtension
  422. * - TypeExtension
  423. *
  424. * TypeExtension :
  425. * - ScalarTypeExtension
  426. * - ObjectTypeExtension
  427. * - InterfaceTypeExtension
  428. * - UnionTypeExtension
  429. * - EnumTypeExtension
  430. * - InputObjectTypeDefinition
  431. */
  432. parseTypeSystemExtension(): TypeSystemExtensionNode;
  433. /**
  434. * ```
  435. * SchemaExtension :
  436. * - extend schema Directives[Const]? { OperationTypeDefinition+ }
  437. * - extend schema Directives[Const]
  438. * ```
  439. */
  440. parseSchemaExtension(): SchemaExtensionNode;
  441. /**
  442. * ScalarTypeExtension :
  443. * - extend scalar Name Directives[Const]
  444. */
  445. parseScalarTypeExtension(): ScalarTypeExtensionNode;
  446. /**
  447. * ObjectTypeExtension :
  448. * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
  449. * - extend type Name ImplementsInterfaces? Directives[Const]
  450. * - extend type Name ImplementsInterfaces
  451. */
  452. parseObjectTypeExtension(): ObjectTypeExtensionNode;
  453. /**
  454. * InterfaceTypeExtension :
  455. * - extend interface Name ImplementsInterfaces? Directives[Const]? FieldsDefinition
  456. * - extend interface Name ImplementsInterfaces? Directives[Const]
  457. * - extend interface Name ImplementsInterfaces
  458. */
  459. parseInterfaceTypeExtension(): InterfaceTypeExtensionNode;
  460. /**
  461. * UnionTypeExtension :
  462. * - extend union Name Directives[Const]? UnionMemberTypes
  463. * - extend union Name Directives[Const]
  464. */
  465. parseUnionTypeExtension(): UnionTypeExtensionNode;
  466. /**
  467. * EnumTypeExtension :
  468. * - extend enum Name Directives[Const]? EnumValuesDefinition
  469. * - extend enum Name Directives[Const]
  470. */
  471. parseEnumTypeExtension(): EnumTypeExtensionNode;
  472. /**
  473. * InputObjectTypeExtension :
  474. * - extend input Name Directives[Const]? InputFieldsDefinition
  475. * - extend input Name Directives[Const]
  476. */
  477. parseInputObjectTypeExtension(): InputObjectTypeExtensionNode;
  478. /**
  479. * ```
  480. * DirectiveDefinition :
  481. * - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
  482. * ```
  483. */
  484. parseDirectiveDefinition(): DirectiveDefinitionNode;
  485. /**
  486. * DirectiveLocations :
  487. * - `|`? DirectiveLocation
  488. * - DirectiveLocations | DirectiveLocation
  489. */
  490. parseDirectiveLocations(): Array<NameNode>;
  491. parseDirectiveLocation(): NameNode;
  492. /**
  493. * Returns a node that, if configured to do so, sets a "loc" field as a
  494. * location object, used to identify the place in the source that created a
  495. * given parsed object.
  496. */
  497. node<
  498. T extends {
  499. loc?: Location;
  500. },
  501. >(startToken: Token, node: T): T;
  502. /**
  503. * Determines if the next token is of a given kind
  504. */
  505. peek(kind: TokenKind): boolean;
  506. /**
  507. * If the next token is of the given kind, return that token after advancing the lexer.
  508. * Otherwise, do not change the parser state and throw an error.
  509. */
  510. expectToken(kind: TokenKind): Token;
  511. /**
  512. * If the next token is of the given kind, return "true" after advancing the lexer.
  513. * Otherwise, do not change the parser state and return "false".
  514. */
  515. expectOptionalToken(kind: TokenKind): boolean;
  516. /**
  517. * If the next token is a given keyword, advance the lexer.
  518. * Otherwise, do not change the parser state and throw an error.
  519. */
  520. expectKeyword(value: string): void;
  521. /**
  522. * If the next token is a given keyword, return "true" after advancing the lexer.
  523. * Otherwise, do not change the parser state and return "false".
  524. */
  525. expectOptionalKeyword(value: string): boolean;
  526. /**
  527. * Helper function for creating an error when an unexpected lexed token is encountered.
  528. */
  529. unexpected(atToken?: Maybe<Token>): GraphQLError;
  530. /**
  531. * Returns a possibly empty list of parse nodes, determined by the parseFn.
  532. * This list begins with a lex token of openKind and ends with a lex token of closeKind.
  533. * Advances the parser to the next lex token after the closing token.
  534. */
  535. any<T>(openKind: TokenKind, parseFn: () => T, closeKind: TokenKind): Array<T>;
  536. /**
  537. * Returns a list of parse nodes, determined by the parseFn.
  538. * It can be empty only if open token is missing otherwise it will always return non-empty list
  539. * that begins with a lex token of openKind and ends with a lex token of closeKind.
  540. * Advances the parser to the next lex token after the closing token.
  541. */
  542. optionalMany<T>(
  543. openKind: TokenKind,
  544. parseFn: () => T,
  545. closeKind: TokenKind,
  546. ): Array<T>;
  547. /**
  548. * Returns a non-empty list of parse nodes, determined by the parseFn.
  549. * This list begins with a lex token of openKind and ends with a lex token of closeKind.
  550. * Advances the parser to the next lex token after the closing token.
  551. */
  552. many<T>(
  553. openKind: TokenKind,
  554. parseFn: () => T,
  555. closeKind: TokenKind,
  556. ): Array<T>;
  557. /**
  558. * Returns a non-empty list of parse nodes, determined by the parseFn.
  559. * This list may begin with a lex token of delimiterKind followed by items separated by lex tokens of tokenKind.
  560. * Advances the parser to the next lex token after last item in the list.
  561. */
  562. delimitedMany<T>(delimiterKind: TokenKind, parseFn: () => T): Array<T>;
  563. advanceLexer(): void;
  564. }