Skip to main content

libgraphql_parser/token/
graphql_trivia_token.rs

1use crate::GraphQLSourceSpan;
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 and commas, which are attached to the following
8/// token as "preceding trivia". This allows formatters and linters to preserve
9/// these elements without the parser needing to handle them explicitly.
10///
11/// # Lifetime Parameter
12///
13/// The `'src` lifetime enables zero-copy lexing for comment values:
14/// `StrGraphQLTokenSource` can borrow comment text directly from the source.
15#[derive(Clone, Debug, PartialEq)]
16pub enum GraphQLTriviaToken<'src> {
17    /// A GraphQL comment, which starts with `#` and extends to the end of the
18    /// line.
19    Comment {
20        /// The comment text (excluding the leading `#`).
21        /// Uses `Cow<'src, str>` to enable zero-copy lexing from string sources.
22        value: Cow<'src, str>,
23        /// The source location of the comment.
24        span: GraphQLSourceSpan,
25    },
26
27    /// A comma separator. In GraphQL, commas are optional and treated as
28    /// whitespace, but we preserve them as trivia.
29    Comma {
30        /// The source location of the comma.
31        span: GraphQLSourceSpan,
32    },
33}