Skip to main content

libgraphql_parser/token/
graphql_trivia_token.rs

1use crate::ByteSpan;
2use std::borrow::Cow;
3
4/// A "trivia token" is a token that doesn't affect parsing but is still
5/// preserved (e.g. for tooling use).
6///
7/// Trivia includes comments, commas, and whitespace, which are attached to
8/// the following token as "preceding trivia". This allows formatters and
9/// linters to preserve these elements without the parser needing to handle
10/// them explicitly.
11///
12/// # Lifetime Parameter
13///
14/// The `'src` lifetime enables zero-copy lexing for comment and whitespace
15/// values: `StrGraphQLTokenSource` can borrow text directly from the source.
16#[derive(Clone, Debug, PartialEq)]
17pub enum GraphQLTriviaToken<'src> {
18    /// A GraphQL comment, which starts with `#` and extends to the end of the
19    /// line.
20    Comment {
21        /// The comment text (excluding the leading `#`).
22        /// Uses `Cow<'src, str>` to enable zero-copy lexing from string sources.
23        value: Cow<'src, str>,
24        /// The source location of the comment.
25        span: ByteSpan,
26    },
27
28    /// A comma separator. In GraphQL, commas are optional and treated as
29    /// whitespace, but we preserve them as trivia.
30    Comma {
31        /// The source location of the comma.
32        span: ByteSpan,
33    },
34
35    /// A run of whitespace characters (spaces, tabs, newlines, BOM).
36    ///
37    /// In GraphQL, whitespace is insignificant (it doesn't affect parsing),
38    /// but preserving it as trivia enables lossless source reconstruction.
39    /// Each `Whitespace` trivia token captures a contiguous run of whitespace
40    /// exactly as it appears in the source.
41    Whitespace {
42        /// The whitespace text (spaces, tabs, newlines, BOM).
43        /// Uses `Cow<'src, str>` to enable zero-copy lexing from string sources.
44        value: Cow<'src, str>,
45        /// The source location of the whitespace run.
46        span: ByteSpan,
47    },
48}