shopify_draft_proxy/graphql/lexer

Mirrors graphql-js language/lexer.ts.

The lexer walks a Source.body codepoint-by-codepoint and produces a stream of Token values. The Gleam port differs from graphql-js in three intentional ways:

  1. Lexer state is immutable. next_token returns the next token plus a fresh Lexer to thread through the parser, instead of mutating this.line / this.token like graphql-js does.
  2. Errors are returned as Result(_, LexError) rather than thrown.
  3. Position semantics use Unicode code points, not UTF-16 code units. For ASCII Shopify GraphQL queries (which is all the proxy parses) this is identical to graphql-js; supplementary-plane characters would diverge by one position per surrogate pair.

Block strings ("""...""") are intentionally not implemented in the spike — operation documents under config/parity-requests/** do not contain them. A clear error is raised if one is encountered.

Types

A lex-time error. position/line/column use the same conventions as Token: 0-indexed code-point offset, 1-indexed line and column.

pub type LexError {
  LexError(
    message: String,
    position: Int,
    line: Int,
    column: Int,
  )
}

Constructors

  • LexError(message: String, position: Int, line: Int, column: Int)

Immutable lexer state. Thread it through next_token to advance.

pub type Lexer {
  Lexer(
    source: source.Source,
    remaining: List(Int),
    position: Int,
    line: Int,
    line_start: Int,
  )
}

Constructors

  • Lexer(
      source: source.Source,
      remaining: List(Int),
      position: Int,
      line: Int,
      line_start: Int,
    )

Values

pub fn lex(
  source: source.Source,
) -> Result(List(token.Token), LexError)

Return the full sequence of tokens from a Source, terminated by an Eof token. Comments are skipped. The returned list is stable and position-ordered.

pub fn new(source: source.Source) -> Lexer

Construct a fresh lexer at the start of the source body.

pub fn next_token(
  lexer: Lexer,
) -> Result(#(token.Token, Lexer), LexError)

Read the next non-comment, non-ignored token from the lexer.

Search Document