The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /* |
| 2 | * Adapted from graphql-js v16.14.2 (https://github.com/graphql/graphql-js, |
| 3 | * `src/language/parser.ts`), distributed under the MIT License: |
| 4 | * |
| 5 | * Copyright (c) GraphQL Contributors |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in all |
| 15 | * copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | /** |
| 26 | * GraphQL parser: `Source` to {@link Ast.Document}, covering the full grammar |
| 27 | * of the current spec edition (September 2025) for executable and type-system |
| 28 | * documents. Recursive descent over {@link Lexer}; stops at the first error. |
| 29 | * |
| 30 | * Structure and messages follow graphql-js so disputes can be settled against |
| 31 | * the reference implementation. |
| 32 | * |
| 33 | * @internal |
| 34 | */ |
| 35 | import type * as Result from "effect/Result" |
| 36 | import type * as Ast from "./Ast.ts" |
| 37 | import { catchDiagnostic, type Diagnostic, make, type Source } from "./Diagnostic.ts" |
| 38 | import { isPunctuatorKind, Lexer, type Token, type TokenKind } from "./Lexer.ts" |
| 39 | |
| 40 | /** |
| 41 | * Parses a complete document, executable or type-system. Fails with the first |
| 42 | * lexical or syntactic error; there is no recovery. |
| 43 | */ |
| 44 | export const parse = (source: Source): Result.Result<Ast.Document, Diagnostic> => |
| 45 | catchDiagnostic(() => new Parser(source).parseDocument()) |
| 46 | |
| 47 | /** |
| 48 | * Parses a single `Value[Const]` that spans the whole source, as found in |
| 49 | * introspection `defaultValue` strings. Variables and trailing tokens are |
| 50 | * errors. |
| 51 | */ |
| 52 | export const parseConstValue = (source: Source): Result.Result<Ast.ConstValue, Diagnostic> => |
| 53 | catchDiagnostic(() => { |
| 54 | const parser = new Parser(source) |
| 55 | parser.expectToken("<SOF>") |
| 56 | const value = parser.parseValueLiteral(true) |
| 57 | parser.expectToken("<EOF>") |
| 58 | return value |
| 59 | }) |
| 60 | |
| 61 | const directiveLocations: ReadonlySet<string> = new Set([ |
| 62 | "QUERY", |
| 63 | "MUTATION", |
| 64 | "SUBSCRIPTION", |
| 65 | "FIELD", |
| 66 | "FRAGMENT_DEFINITION", |
| 67 | "FRAGMENT_SPREAD", |
| 68 | "INLINE_FRAGMENT", |
| 69 | "VARIABLE_DEFINITION", |
| 70 | "SCHEMA", |
| 71 | "SCALAR", |
| 72 | "OBJECT", |
| 73 | "FIELD_DEFINITION", |
| 74 | "ARGUMENT_DEFINITION", |
| 75 | "INTERFACE", |
| 76 | "UNION", |
| 77 | "ENUM", |
| 78 | "ENUM_VALUE", |
| 79 | "INPUT_OBJECT", |
| 80 | "INPUT_FIELD_DEFINITION" |
| 81 | ]) |
| 82 | |
| 83 | const describeKind = (kind: TokenKind): string => (isPunctuatorKind(kind) ? `"${kind}"` : kind) |
| 84 | |
| 85 | const describeToken = (token: Token): string => |
| 86 | token.value === undefined ? describeKind(token.kind) : `${describeKind(token.kind)} "${token.value}"` |
| 87 | |
| 88 | class Parser { |
| 89 | readonly lexer: Lexer |
| 90 | |
| 91 | constructor(source: Source) { |
| 92 | this.lexer = new Lexer(source) |
| 93 | } |
| 94 | |
| 95 | // --------------------------------------------------------------------------- |
| 96 | // Document |
| 97 | // --------------------------------------------------------------------------- |
| 98 | |
| 99 | parseDocument(): Ast.Document { |
| 100 | const start = this.lexer.token |
| 101 | const definitions = this.many("<SOF>", () => this.parseDefinition(), "<EOF>") |
| 102 | return { _tag: "Document", definitions, loc: this.loc(start) } |
| 103 | } |
| 104 | |
| 105 | parseDefinition(): Ast.Definition { |
| 106 | if (this.peek("{")) { |
| 107 | return this.parseOperationDefinition() |
| 108 | } |
| 109 | // Many definitions begin with a description and need a lookahead. |
| 110 | const hasDescription = this.peekDescription() |
| 111 | const keywordToken = hasDescription ? this.lexer.lookahead() : this.lexer.token |
| 112 | if (hasDescription && keywordToken.kind === "{") { |
| 113 | throw this.fail( |
| 114 | this.lexer.token.start, |
| 115 | "Unexpected description, descriptions are not supported on shorthand queries." |
| 116 | ) |
| 117 | } |
| 118 | if (keywordToken.kind === "Name") { |
| 119 | switch (keywordToken.value) { |
| 120 | case "schema": |
| 121 | return this.parseSchemaDefinition() |
| 122 | case "scalar": |
| 123 | return this.parseScalarTypeDefinition() |
| 124 | case "type": |
| 125 | return this.parseObjectTypeDefinition() |
| 126 | case "interface": |
| 127 | return this.parseInterfaceTypeDefinition() |
| 128 | case "union": |
| 129 | return this.parseUnionTypeDefinition() |
| 130 | case "enum": |
| 131 | return this.parseEnumTypeDefinition() |
| 132 | case "input": |
| 133 | return this.parseInputObjectTypeDefinition() |
| 134 | case "directive": |
| 135 | return this.parseDirectiveDefinition() |
| 136 | case "query": |
| 137 | case "mutation": |
| 138 | case "subscription": |
| 139 | return this.parseOperationDefinition() |
| 140 | case "fragment": |
| 141 | return this.parseFragmentDefinition() |
| 142 | } |
| 143 | if (hasDescription) { |
| 144 | throw this.fail( |
| 145 | this.lexer.token.start, |
| 146 | "Unexpected description, only GraphQL definitions support descriptions." |
| 147 | ) |
| 148 | } |
| 149 | if (keywordToken.value === "extend") { |
| 150 | return this.parseTypeSystemExtension() |
| 151 | } |
| 152 | } |
| 153 | throw this.unexpected(keywordToken) |
| 154 | } |
| 155 | |
| 156 | // --------------------------------------------------------------------------- |
| 157 | // Executable definitions |
| 158 | // --------------------------------------------------------------------------- |
| 159 | |
| 160 | parseOperationDefinition(): Ast.OperationDefinition { |
| 161 | const start = this.lexer.token |
| 162 | if (this.peek("{")) { |
| 163 | return { |
| 164 | _tag: "OperationDefinition", |
| 165 | description: undefined, |
| 166 | operation: "query", |
| 167 | name: undefined, |
| 168 | variableDefinitions: [], |
| 169 | directives: [], |
| 170 | selectionSet: this.parseSelectionSet(), |
| 171 | loc: this.loc(start) |
| 172 | } |
| 173 | } |
| 174 | const description = this.parseDescription() |
| 175 | const operation = this.parseOperationType() |
| 176 | const name = this.peek("Name") ? this.parseName() : undefined |
| 177 | const variableDefinitions = this.optionalMany("(", () => this.parseVariableDefinition(), ")") |
| 178 | const directives = this.parseDirectives(false) |
| 179 | const selectionSet = this.parseSelectionSet() |
| 180 | return { |
| 181 | _tag: "OperationDefinition", |
| 182 | description, |
| 183 | operation, |
| 184 | name, |
| 185 | variableDefinitions, |
| 186 | directives, |
| 187 | selectionSet, |
| 188 | loc: this.loc(start) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | parseOperationType(): Ast.OperationType { |
| 193 | const token = this.expectToken("Name") |
| 194 | switch (token.value) { |
| 195 | case "query": |
| 196 | case "mutation": |
| 197 | case "subscription": |
| 198 | return token.value |
| 199 | } |
| 200 | throw this.unexpected(token) |
| 201 | } |
| 202 | |
| 203 | parseVariableDefinition(): Ast.VariableDefinition { |
| 204 | const start = this.lexer.token |
| 205 | const description = this.parseDescription() |
| 206 | const variable = this.parseVariable() |
| 207 | this.expectToken(":") |
| 208 | const type = this.parseTypeReference() |
| 209 | const defaultValue = this.expectOptionalToken("=") ? this.parseValueLiteral(true) : undefined |
| 210 | const directives = this.parseDirectives(true) |
| 211 | return { _tag: "VariableDefinition", description, variable, type, defaultValue, directives, loc: this.loc(start) } |
| 212 | } |
| 213 | |
| 214 | parseVariable(): Ast.Variable { |
| 215 | const start = this.lexer.token |
| 216 | this.expectToken("$") |
| 217 | const name = this.parseName() |
| 218 | return { _tag: "Variable", name, loc: this.loc(start) } |
| 219 | } |
| 220 | |
| 221 | parseSelectionSet(): Ast.SelectionSet { |
| 222 | const start = this.lexer.token |
| 223 | const selections = this.many("{", () => this.peek("...") ? this.parseFragment() : this.parseField(), "}") |
| 224 | return { _tag: "SelectionSet", selections, loc: this.loc(start) } |
| 225 | } |
| 226 | |
| 227 | parseField(): Ast.Field { |
| 228 | const start = this.lexer.token |
| 229 | const nameOrAlias = this.parseName() |
| 230 | const alias = this.expectOptionalToken(":") ? nameOrAlias : undefined |
| 231 | const name = alias === undefined ? nameOrAlias : this.parseName() |
| 232 | const args = this.parseArguments(false) |
| 233 | const directives = this.parseDirectives(false) |
| 234 | const selectionSet = this.peek("{") ? this.parseSelectionSet() : undefined |
| 235 | return { _tag: "Field", alias, name, arguments: args, directives, selectionSet, loc: this.loc(start) } |
| 236 | } |
| 237 | |
| 238 | parseArguments(isConst: boolean): Array<Ast.Argument> { |
| 239 | return this.optionalMany("(", () => this.parseArgument(isConst), ")") |
| 240 | } |
| 241 | |
| 242 | parseArgument(isConst: boolean): Ast.Argument { |
| 243 | const start = this.lexer.token |
| 244 | const name = this.parseName() |
| 245 | this.expectToken(":") |
| 246 | const value = this.parseValueLiteral(isConst) |
| 247 | return { _tag: "Argument", name, value, loc: this.loc(start) } |
| 248 | } |
| 249 | |
| 250 | parseFragment(): Ast.FragmentSpread | Ast.InlineFragment { |
| 251 | const start = this.lexer.token |
| 252 | this.expectToken("...") |
| 253 | const hasTypeCondition = this.expectOptionalKeyword("on") |
| 254 | if (!hasTypeCondition && this.peek("Name")) { |
| 255 | const name = this.parseFragmentName() |
| 256 | const directives = this.parseDirectives(false) |
| 257 | return { _tag: "FragmentSpread", name, directives, loc: this.loc(start) } |
| 258 | } |
| 259 | const typeCondition = hasTypeCondition ? this.parseNamedType() : undefined |
| 260 | const directives = this.parseDirectives(false) |
| 261 | const selectionSet = this.parseSelectionSet() |
| 262 | return { _tag: "InlineFragment", typeCondition, directives, selectionSet, loc: this.loc(start) } |
| 263 | } |
| 264 | |
| 265 | parseFragmentDefinition(): Ast.FragmentDefinition { |
| 266 | const start = this.lexer.token |
| 267 | const description = this.parseDescription() |
| 268 | this.expectKeyword("fragment") |
| 269 | const name = this.parseFragmentName() |
| 270 | this.expectKeyword("on") |
| 271 | const typeCondition = this.parseNamedType() |
| 272 | const directives = this.parseDirectives(false) |
| 273 | const selectionSet = this.parseSelectionSet() |
| 274 | return { |
| 275 | _tag: "FragmentDefinition", |
| 276 | description, |
| 277 | name, |
| 278 | typeCondition, |
| 279 | directives, |
| 280 | selectionSet, |
| 281 | loc: this.loc(start) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | parseFragmentName(): Ast.Name { |
| 286 | if (this.lexer.token.value === "on") { |
| 287 | throw this.unexpected() |
| 288 | } |
| 289 | return this.parseName() |
| 290 | } |
| 291 | |
| 292 | // --------------------------------------------------------------------------- |
| 293 | // Values |
| 294 | // --------------------------------------------------------------------------- |
| 295 | |
| 296 | parseValueLiteral(isConst: true): Ast.ConstValue |
| 297 | parseValueLiteral(isConst: boolean): Ast.Value |
| 298 | parseValueLiteral(isConst: boolean): Ast.Value { |
| 299 | const token = this.lexer.token |
| 300 | switch (token.kind) { |
| 301 | case "[": |
| 302 | return this.parseList(isConst) |
| 303 | case "{": |
| 304 | return this.parseObject(isConst) |
| 305 | case "Int": |
| 306 | this.lexer.advance() |
| 307 | return { _tag: "IntValue", value: token.value!, loc: this.loc(token) } |
| 308 | case "Float": |
| 309 | this.lexer.advance() |
| 310 | return { _tag: "FloatValue", value: token.value!, loc: this.loc(token) } |
| 311 | case "String": |
| 312 | case "BlockString": |
| 313 | return this.parseStringLiteral() |
| 314 | case "Name": |
| 315 | this.lexer.advance() |
| 316 | switch (token.value) { |
| 317 | case "true": |
| 318 | return { _tag: "BooleanValue", value: true, loc: this.loc(token) } |
| 319 | case "false": |
| 320 | return { _tag: "BooleanValue", value: false, loc: this.loc(token) } |
| 321 | case "null": |
| 322 | return { _tag: "NullValue", loc: this.loc(token) } |
| 323 | default: |
| 324 | return { _tag: "EnumValue", value: token.value!, loc: this.loc(token) } |
| 325 | } |
| 326 | case "$": |
| 327 | if (isConst) { |
| 328 | this.expectToken("$") |
| 329 | if (this.lexer.token.kind === "Name") { |
| 330 | throw this.fail(token.start, `Unexpected variable "$${this.lexer.token.value}" in constant value.`) |
| 331 | } |
| 332 | throw this.unexpected(token) |
| 333 | } |
| 334 | return this.parseVariable() |
| 335 | default: |
| 336 | throw this.unexpected() |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | parseStringLiteral(): Ast.StringValue { |
| 341 | const token = this.lexer.token |
| 342 | this.lexer.advance() |
| 343 | return { _tag: "StringValue", value: token.value!, loc: this.loc(token) } |
| 344 | } |
| 345 | |
| 346 | parseList(isConst: boolean): Ast.ListValue { |
| 347 | const start = this.lexer.token |
| 348 | const values = this.any("[", () => this.parseValueLiteral(isConst), "]") |
| 349 | return { _tag: "ListValue", values, loc: this.loc(start) } |
| 350 | } |
| 351 | |
| 352 | parseObject(isConst: boolean): Ast.ObjectValue { |
| 353 | const start = this.lexer.token |
| 354 | const fields = this.any("{", () => this.parseObjectField(isConst), "}") |
| 355 | return { _tag: "ObjectValue", fields, loc: this.loc(start) } |
| 356 | } |
| 357 | |
| 358 | parseObjectField(isConst: boolean): Ast.ObjectField { |
| 359 | const start = this.lexer.token |
| 360 | const name = this.parseName() |
| 361 | this.expectToken(":") |
| 362 | const value = this.parseValueLiteral(isConst) |
| 363 | return { _tag: "ObjectField", name, value, loc: this.loc(start) } |
| 364 | } |
| 365 | |
| 366 | // --------------------------------------------------------------------------- |
| 367 | // Directives and types |
| 368 | // --------------------------------------------------------------------------- |
| 369 | |
| 370 | parseDirectives(isConst: true): Array<Ast.ConstDirective> |
| 371 | parseDirectives(isConst: boolean): Array<Ast.Directive> |
| 372 | parseDirectives(isConst: boolean): Array<Ast.Directive> { |
| 373 | const directives: Array<Ast.Directive> = [] |
| 374 | while (this.peek("@")) { |
| 375 | directives.push(this.parseDirective(isConst)) |
| 376 | } |
| 377 | return directives |
| 378 | } |
| 379 | |
| 380 | parseDirective(isConst: boolean): Ast.Directive { |
| 381 | const start = this.lexer.token |
| 382 | this.expectToken("@") |
| 383 | const name = this.parseName() |
| 384 | const args = this.parseArguments(isConst) |
| 385 | return { _tag: "Directive", name, arguments: args, loc: this.loc(start) } |
| 386 | } |
| 387 | |
| 388 | parseTypeReference(): Ast.Type { |
| 389 | const start = this.lexer.token |
| 390 | let type: Ast.NamedType | Ast.ListType |
| 391 | if (this.expectOptionalToken("[")) { |
| 392 | const innerType = this.parseTypeReference() |
| 393 | this.expectToken("]") |
| 394 | type = { _tag: "ListType", type: innerType, loc: this.loc(start) } |
| 395 | } else { |
| 396 | type = this.parseNamedType() |
| 397 | } |
| 398 | if (this.expectOptionalToken("!")) { |
| 399 | return { _tag: "NonNullType", type, loc: this.loc(start) } |
| 400 | } |
| 401 | return type |
| 402 | } |
| 403 | |
| 404 | parseNamedType(): Ast.NamedType { |
| 405 | const start = this.lexer.token |
| 406 | const name = this.parseName() |
| 407 | return { _tag: "NamedType", name, loc: this.loc(start) } |
| 408 | } |
| 409 | |
| 410 | parseName(): Ast.Name { |
| 411 | const token = this.expectToken("Name") |
| 412 | return { _tag: "Name", value: token.value!, loc: this.loc(token) } |
| 413 | } |
| 414 | |
| 415 | // --------------------------------------------------------------------------- |
| 416 | // Type-system definitions |
| 417 | // --------------------------------------------------------------------------- |
| 418 | |
| 419 | peekDescription(): boolean { |
| 420 | return this.peek("String") || this.peek("BlockString") |
| 421 | } |
| 422 | |
| 423 | parseDescription(): Ast.StringValue | undefined { |
| 424 | return this.peekDescription() ? this.parseStringLiteral() : undefined |
| 425 | } |
| 426 | |
| 427 | parseSchemaDefinition(): Ast.SchemaDefinition { |
| 428 | const start = this.lexer.token |
| 429 | const description = this.parseDescription() |
| 430 | this.expectKeyword("schema") |
| 431 | const directives = this.parseDirectives(true) |
| 432 | const operationTypes = this.many("{", () => this.parseOperationTypeDefinition(), "}") |
| 433 | return { _tag: "SchemaDefinition", description, directives, operationTypes, loc: this.loc(start) } |
| 434 | } |
| 435 | |
| 436 | parseOperationTypeDefinition(): Ast.OperationTypeDefinition { |
| 437 | const start = this.lexer.token |
| 438 | const operation = this.parseOperationType() |
| 439 | this.expectToken(":") |
| 440 | const type = this.parseNamedType() |
| 441 | return { _tag: "OperationTypeDefinition", operation, type, loc: this.loc(start) } |
| 442 | } |
| 443 | |
| 444 | parseScalarTypeDefinition(): Ast.ScalarTypeDefinition { |
| 445 | const start = this.lexer.token |
| 446 | const description = this.parseDescription() |
| 447 | this.expectKeyword("scalar") |
| 448 | const name = this.parseName() |
| 449 | const directives = this.parseDirectives(true) |
| 450 | return { _tag: "ScalarTypeDefinition", description, name, directives, loc: this.loc(start) } |
| 451 | } |
| 452 | |
| 453 | parseObjectTypeDefinition(): Ast.ObjectTypeDefinition { |
| 454 | const start = this.lexer.token |
| 455 | const description = this.parseDescription() |
| 456 | this.expectKeyword("type") |
| 457 | const name = this.parseName() |
| 458 | const interfaces = this.parseImplementsInterfaces() |
| 459 | const directives = this.parseDirectives(true) |
| 460 | const fields = this.parseFieldsDefinition() |
| 461 | return { _tag: "ObjectTypeDefinition", description, name, interfaces, directives, fields, loc: this.loc(start) } |
| 462 | } |
| 463 | |
| 464 | parseImplementsInterfaces(): Array<Ast.NamedType> { |
| 465 | return this.expectOptionalKeyword("implements") ? this.delimitedMany("&", () => this.parseNamedType()) : [] |
| 466 | } |
| 467 | |
| 468 | parseFieldsDefinition(): Array<Ast.FieldDefinition> { |
| 469 | return this.optionalMany("{", () => this.parseFieldDefinition(), "}") |
| 470 | } |
| 471 | |
| 472 | parseFieldDefinition(): Ast.FieldDefinition { |
| 473 | const start = this.lexer.token |
| 474 | const description = this.parseDescription() |
| 475 | const name = this.parseName() |
| 476 | const args = this.parseArgumentDefs() |
| 477 | this.expectToken(":") |
| 478 | const type = this.parseTypeReference() |
| 479 | const directives = this.parseDirectives(true) |
| 480 | return { _tag: "FieldDefinition", description, name, arguments: args, type, directives, loc: this.loc(start) } |
| 481 | } |
| 482 | |
| 483 | parseArgumentDefs(): Array<Ast.InputValueDefinition> { |
| 484 | return this.optionalMany("(", () => this.parseInputValueDef(), ")") |
| 485 | } |
| 486 | |
| 487 | parseInputValueDef(): Ast.InputValueDefinition { |
| 488 | const start = this.lexer.token |
| 489 | const description = this.parseDescription() |
| 490 | const name = this.parseName() |
| 491 | this.expectToken(":") |
| 492 | const type = this.parseTypeReference() |
| 493 | const defaultValue = this.expectOptionalToken("=") ? this.parseValueLiteral(true) : undefined |
| 494 | const directives = this.parseDirectives(true) |
| 495 | return { _tag: "InputValueDefinition", description, name, type, defaultValue, directives, loc: this.loc(start) } |
| 496 | } |
| 497 | |
| 498 | parseInterfaceTypeDefinition(): Ast.InterfaceTypeDefinition { |
| 499 | const start = this.lexer.token |
| 500 | const description = this.parseDescription() |
| 501 | this.expectKeyword("interface") |
| 502 | const name = this.parseName() |
| 503 | const interfaces = this.parseImplementsInterfaces() |
| 504 | const directives = this.parseDirectives(true) |
| 505 | const fields = this.parseFieldsDefinition() |
| 506 | return { _tag: "InterfaceTypeDefinition", description, name, interfaces, directives, fields, loc: this.loc(start) } |
| 507 | } |
| 508 | |
| 509 | parseUnionTypeDefinition(): Ast.UnionTypeDefinition { |
| 510 | const start = this.lexer.token |
| 511 | const description = this.parseDescription() |
| 512 | this.expectKeyword("union") |
| 513 | const name = this.parseName() |
| 514 | const directives = this.parseDirectives(true) |
| 515 | const types = this.parseUnionMemberTypes() |
| 516 | return { _tag: "UnionTypeDefinition", description, name, directives, types, loc: this.loc(start) } |
| 517 | } |
| 518 | |
| 519 | parseUnionMemberTypes(): Array<Ast.NamedType> { |
| 520 | return this.expectOptionalToken("=") ? this.delimitedMany("|", () => this.parseNamedType()) : [] |
| 521 | } |
| 522 | |
| 523 | parseEnumTypeDefinition(): Ast.EnumTypeDefinition { |
| 524 | const start = this.lexer.token |
| 525 | const description = this.parseDescription() |
| 526 | this.expectKeyword("enum") |
| 527 | const name = this.parseName() |
| 528 | const directives = this.parseDirectives(true) |
| 529 | const values = this.parseEnumValuesDefinition() |
| 530 | return { _tag: "EnumTypeDefinition", description, name, directives, values, loc: this.loc(start) } |
| 531 | } |
| 532 | |
| 533 | parseEnumValuesDefinition(): Array<Ast.EnumValueDefinition> { |
| 534 | return this.optionalMany("{", () => this.parseEnumValueDefinition(), "}") |
| 535 | } |
| 536 | |
| 537 | parseEnumValueDefinition(): Ast.EnumValueDefinition { |
| 538 | const start = this.lexer.token |
| 539 | const description = this.parseDescription() |
| 540 | const name = this.parseEnumValueName() |
| 541 | const directives = this.parseDirectives(true) |
| 542 | return { _tag: "EnumValueDefinition", description, name, directives, loc: this.loc(start) } |
| 543 | } |
| 544 | |
| 545 | parseEnumValueName(): Ast.Name { |
| 546 | const token = this.lexer.token |
| 547 | if (token.value === "true" || token.value === "false" || token.value === "null") { |
| 548 | throw this.fail(token.start, `${describeToken(token)} is reserved and cannot be used for an enum value.`) |
| 549 | } |
| 550 | return this.parseName() |
| 551 | } |
| 552 | |
| 553 | parseInputObjectTypeDefinition(): Ast.InputObjectTypeDefinition { |
| 554 | const start = this.lexer.token |
| 555 | const description = this.parseDescription() |
| 556 | this.expectKeyword("input") |
| 557 | const name = this.parseName() |
| 558 | const directives = this.parseDirectives(true) |
| 559 | const fields = this.parseInputFieldsDefinition() |
| 560 | return { _tag: "InputObjectTypeDefinition", description, name, directives, fields, loc: this.loc(start) } |
| 561 | } |
| 562 | |
| 563 | parseInputFieldsDefinition(): Array<Ast.InputValueDefinition> { |
| 564 | return this.optionalMany("{", () => this.parseInputValueDef(), "}") |
| 565 | } |
| 566 | |
| 567 | parseDirectiveDefinition(): Ast.DirectiveDefinition { |
| 568 | const start = this.lexer.token |
| 569 | const description = this.parseDescription() |
| 570 | this.expectKeyword("directive") |
| 571 | this.expectToken("@") |
| 572 | const name = this.parseName() |
| 573 | const args = this.parseArgumentDefs() |
| 574 | const repeatable = this.expectOptionalKeyword("repeatable") |
| 575 | this.expectKeyword("on") |
| 576 | const locations = this.delimitedMany("|", () => this.parseDirectiveLocation()) |
| 577 | return { |
| 578 | _tag: "DirectiveDefinition", |
| 579 | description, |
| 580 | name, |
| 581 | arguments: args, |
| 582 | repeatable, |
| 583 | locations, |
| 584 | loc: this.loc(start) |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | parseDirectiveLocation(): Ast.Name { |
| 589 | const start = this.lexer.token |
| 590 | const name = this.parseName() |
| 591 | if (directiveLocations.has(name.value)) { |
| 592 | return name |
| 593 | } |
| 594 | throw this.unexpected(start) |
| 595 | } |
| 596 | |
| 597 | // --------------------------------------------------------------------------- |
| 598 | // Type-system extensions |
| 599 | // --------------------------------------------------------------------------- |
| 600 | |
| 601 | parseTypeSystemExtension(): Ast.TypeSystemExtension { |
| 602 | const keywordToken = this.lexer.lookahead() |
| 603 | if (keywordToken.kind === "Name") { |
| 604 | switch (keywordToken.value) { |
| 605 | case "schema": |
| 606 | return this.parseSchemaExtension() |
| 607 | case "scalar": |
| 608 | return this.parseScalarTypeExtension() |
| 609 | case "type": |
| 610 | return this.parseObjectTypeExtension() |
| 611 | case "interface": |
| 612 | return this.parseInterfaceTypeExtension() |
| 613 | case "union": |
| 614 | return this.parseUnionTypeExtension() |
| 615 | case "enum": |
| 616 | return this.parseEnumTypeExtension() |
| 617 | case "input": |
| 618 | return this.parseInputObjectTypeExtension() |
| 619 | } |
| 620 | } |
| 621 | throw this.unexpected(keywordToken) |
| 622 | } |
| 623 | |
| 624 | parseSchemaExtension(): Ast.SchemaExtension { |
| 625 | const start = this.lexer.token |
| 626 | this.expectKeyword("extend") |
| 627 | this.expectKeyword("schema") |
| 628 | const directives = this.parseDirectives(true) |
| 629 | const operationTypes = this.optionalMany("{", () => this.parseOperationTypeDefinition(), "}") |
| 630 | this.expectNonEmpty(directives, operationTypes) |
| 631 | return { _tag: "SchemaExtension", directives, operationTypes, loc: this.loc(start) } |
| 632 | } |
| 633 | |
| 634 | parseScalarTypeExtension(): Ast.ScalarTypeExtension { |
| 635 | const start = this.lexer.token |
| 636 | this.expectKeyword("extend") |
| 637 | this.expectKeyword("scalar") |
| 638 | const name = this.parseName() |
| 639 | const directives = this.parseDirectives(true) |
| 640 | this.expectNonEmpty(directives) |
| 641 | return { _tag: "ScalarTypeExtension", name, directives, loc: this.loc(start) } |
| 642 | } |
| 643 | |
| 644 | parseObjectTypeExtension(): Ast.ObjectTypeExtension { |
| 645 | const start = this.lexer.token |
| 646 | this.expectKeyword("extend") |
| 647 | this.expectKeyword("type") |
| 648 | const name = this.parseName() |
| 649 | const interfaces = this.parseImplementsInterfaces() |
| 650 | const directives = this.parseDirectives(true) |
| 651 | const fields = this.parseFieldsDefinition() |
| 652 | this.expectNonEmpty(interfaces, directives, fields) |
| 653 | return { _tag: "ObjectTypeExtension", name, interfaces, directives, fields, loc: this.loc(start) } |
| 654 | } |
| 655 | |
| 656 | parseInterfaceTypeExtension(): Ast.InterfaceTypeExtension { |
| 657 | const start = this.lexer.token |
| 658 | this.expectKeyword("extend") |
| 659 | this.expectKeyword("interface") |
| 660 | const name = this.parseName() |
| 661 | const interfaces = this.parseImplementsInterfaces() |
| 662 | const directives = this.parseDirectives(true) |
| 663 | const fields = this.parseFieldsDefinition() |
| 664 | this.expectNonEmpty(interfaces, directives, fields) |
| 665 | return { _tag: "InterfaceTypeExtension", name, interfaces, directives, fields, loc: this.loc(start) } |
| 666 | } |
| 667 | |
| 668 | parseUnionTypeExtension(): Ast.UnionTypeExtension { |
| 669 | const start = this.lexer.token |
| 670 | this.expectKeyword("extend") |
| 671 | this.expectKeyword("union") |
| 672 | const name = this.parseName() |
| 673 | const directives = this.parseDirectives(true) |
| 674 | const types = this.parseUnionMemberTypes() |
| 675 | this.expectNonEmpty(directives, types) |
| 676 | return { _tag: "UnionTypeExtension", name, directives, types, loc: this.loc(start) } |
| 677 | } |
| 678 | |
| 679 | parseEnumTypeExtension(): Ast.EnumTypeExtension { |
| 680 | const start = this.lexer.token |
| 681 | this.expectKeyword("extend") |
| 682 | this.expectKeyword("enum") |
| 683 | const name = this.parseName() |
| 684 | const directives = this.parseDirectives(true) |
| 685 | const values = this.parseEnumValuesDefinition() |
| 686 | this.expectNonEmpty(directives, values) |
| 687 | return { _tag: "EnumTypeExtension", name, directives, values, loc: this.loc(start) } |
| 688 | } |
| 689 | |
| 690 | parseInputObjectTypeExtension(): Ast.InputObjectTypeExtension { |
| 691 | const start = this.lexer.token |
| 692 | this.expectKeyword("extend") |
| 693 | this.expectKeyword("input") |
| 694 | const name = this.parseName() |
| 695 | const directives = this.parseDirectives(true) |
| 696 | const fields = this.parseInputFieldsDefinition() |
| 697 | this.expectNonEmpty(directives, fields) |
| 698 | return { _tag: "InputObjectTypeExtension", name, directives, fields, loc: this.loc(start) } |
| 699 | } |
| 700 | |
| 701 | // --------------------------------------------------------------------------- |
| 702 | // Core parsing utilities |
| 703 | // --------------------------------------------------------------------------- |
| 704 | |
| 705 | loc(startToken: Token): Ast.Loc { |
| 706 | return { start: startToken.start, end: this.lexer.lastToken.end } |
| 707 | } |
| 708 | |
| 709 | peek(kind: TokenKind): boolean { |
| 710 | return this.lexer.token.kind === kind |
| 711 | } |
| 712 | |
| 713 | expectToken(kind: TokenKind): Token { |
| 714 | const token = this.lexer.token |
| 715 | if (this.expectOptionalToken(kind)) return token |
| 716 | throw this.fail(token.start, `Expected ${describeKind(kind)}, found ${describeToken(token)}.`) |
| 717 | } |
| 718 | |
| 719 | expectOptionalToken(kind: TokenKind): boolean { |
| 720 | if (this.lexer.token.kind === kind) { |
| 721 | this.lexer.advance() |
| 722 | return true |
| 723 | } |
| 724 | return false |
| 725 | } |
| 726 | |
| 727 | expectKeyword(value: string): void { |
| 728 | if (!this.expectOptionalKeyword(value)) { |
| 729 | throw this.fail(this.lexer.token.start, `Expected "${value}", found ${describeToken(this.lexer.token)}.`) |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | expectOptionalKeyword(value: string): boolean { |
| 734 | const token = this.lexer.token |
| 735 | if (token.kind === "Name" && token.value === value) { |
| 736 | this.lexer.advance() |
| 737 | return true |
| 738 | } |
| 739 | return false |
| 740 | } |
| 741 | |
| 742 | /** An extension must add something; fails at the current token otherwise. */ |
| 743 | expectNonEmpty(...lists: ReadonlyArray<ReadonlyArray<unknown>>): void { |
| 744 | if (lists.every((list) => list.length === 0)) { |
| 745 | throw this.unexpected() |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | unexpected(atToken: Token = this.lexer.token): Diagnostic { |
| 750 | return this.fail(atToken.start, `Unexpected ${describeToken(atToken)}.`) |
| 751 | } |
| 752 | |
| 753 | fail(offset: number, message: string): Diagnostic { |
| 754 | return make(this.lexer.source, offset, message) |
| 755 | } |
| 756 | |
| 757 | /** `open item* close`: zero or more items between the delimiters. */ |
| 758 | any<A>(open: TokenKind, parseItem: () => A, close: TokenKind): Array<A> { |
| 759 | this.expectToken(open) |
| 760 | const nodes: Array<A> = [] |
| 761 | while (!this.expectOptionalToken(close)) { |
| 762 | nodes.push(parseItem()) |
| 763 | } |
| 764 | return nodes |
| 765 | } |
| 766 | |
| 767 | /** `(open item+ close)?`: one or more items when the opener is present, otherwise nothing. */ |
| 768 | optionalMany<A>(open: TokenKind, parseItem: () => A, close: TokenKind): Array<A> { |
| 769 | return this.peek(open) ? this.many(open, parseItem, close) : [] |
| 770 | } |
| 771 | |
| 772 | /** `open item+ close`: one or more items between the delimiters. */ |
| 773 | many<A>(open: TokenKind, parseItem: () => A, close: TokenKind): Array<A> { |
| 774 | this.expectToken(open) |
| 775 | const nodes: Array<A> = [] |
| 776 | do { |
| 777 | nodes.push(parseItem()) |
| 778 | } while (!this.expectOptionalToken(close)) |
| 779 | return nodes |
| 780 | } |
| 781 | |
| 782 | /** `delimiter? item (delimiter item)*`: one or more delimited items with an optional leading delimiter. */ |
| 783 | delimitedMany<A>(delimiter: TokenKind, parseItem: () => A): Array<A> { |
| 784 | this.expectOptionalToken(delimiter) |
| 785 | const nodes: Array<A> = [] |
| 786 | do { |
| 787 | nodes.push(parseItem()) |
| 788 | } while (this.expectOptionalToken(delimiter)) |
| 789 | return nodes |
| 790 | } |
| 791 | } |