graphql/language/lexing

Classes

Lexer

Given a Source object, creates a Lexer for that source. A Lexer is a stateful stream generator in that every time it is advanced, it returns the next token in the Source. Assuming the source lexes, the final Token emitted by the lexer will be of kind EOF, after which the lexer will repeatedly return the same EOF token whenever called.

Constructor

Creates a Lexer instance.

Signature:

new Lexer(source: Source)
Arguments
NameTypeDescription
sourceSourceSource document used to derive error locations.
Returns
TypeDescription
Lexer

Members

NameTypeDescription
sourceSourceSource document used to derive error locations.
lastTokenTokenMost recent non-ignored token returned by the lexer.
tokenTokenCurrent non-ignored token at the lexer cursor.
linenumberThe (1-indexed) line containing the current token.
lineStartnumberCharacter offset where the current line starts.

advance()

Advances the token stream to the next non-ignored token.

Signature:

advance(): Token
Returns
TypeDescription
TokenThe next non-ignored token.
Example
import { Lexer, Source } from 'graphql/language';
 
const lexer = new Lexer(new Source('{ hello }'));
const token = lexer.advance();
 
// token.kind: '{'

lookahead()

Looks ahead and returns the next non-ignored token, but does not change the state of Lexer.

Signature:

lookahead(): Token
Returns
TypeDescription
TokenThe next non-ignored token without advancing the lexer.
Example
import { Lexer, Source } from 'graphql/language';
 
const lexer = new Lexer(new Source('{ hello }'));
const token = lexer.lookahead();
 
// token.kind: '{'

Enumerations

TokenKind

An exported enum describing the different kinds of tokens that the lexer emits.

Members

NameValueDescription
SOF"<SOF>"Start-of-file token.
EOF"<EOF>"End-of-file token.
BANG"!"The ! punctuation token.
DOLLAR"$"The $ punctuation token.
AMP"&"The & punctuation token.
PAREN_L"("The ( punctuation token.
PAREN_R")"The ) punctuation token.
DOT"."The . punctuation token.
SPREAD"..."The ... spread punctuation token.
COLON":"The : punctuation token.
EQUALS"="The = punctuation token.
AT"@"The @ punctuation token.
BRACKET_L"["The [ punctuation token.
BRACKET_R"]"The ] punctuation token.
BRACE_L"{"The { punctuation token.
PIPE"|"The | punctuation token.
BRACE_R"}"The } punctuation token.
NAME"Name"A GraphQL name token or name AST node.
INT"Int"An integer value token or AST node.
FLOAT"Float"A floating-point value token or AST node.
STRING"String"A string value token or AST node.
BLOCK_STRING"BlockString"A block string value token.
COMMENT"Comment"A comment token.

Types

TokenKindEnum

Type alias. Deprecated in v16

Legacy alias for the enum type representing token kind values. This is retained for backwards compatibility; use TokenKind instead because TokenKindEnum will be removed in v17.

type TokenKindEnum = typeof TokenKind;