Skip to main content

libgraphql_parser/token/
graphql_token.rs

1use crate::token::GraphQLTokenKind;
2use crate::token::GraphQLTriviaToken;
3use crate::GraphQLSourceSpan;
4use smallvec::SmallVec;
5
6/// Type alias for trivia storage. Uses SmallVec to avoid heap allocation
7/// for the common case of 0-2 trivia items per token.
8///
9/// The `'src` lifetime matches the source text lifetime from the token source.
10pub type GraphQLTriviaTokenVec<'src> = SmallVec<[GraphQLTriviaToken<'src>; 2]>;
11
12/// A GraphQL token with location (span) information and an ordered list of any
13/// preceding trivia (comments, commas).
14///
15/// Trivia is attached to the *following* token, so parsers can simply
16/// call `peek()` and `consume()` without worrying about skipping trivia.
17///
18/// # Lifetime Parameter
19///
20/// The `'src` lifetime represents the source text that this token was lexed
21/// from. For `StrGraphQLTokenSource`, this enables zero-copy lexing where
22/// token values borrow directly from the input string. For
23/// `RustMacroGraphQLTokenSource`, tokens use owned strings and the lifetime
24/// can be `'static`.
25#[derive(Clone, Debug, PartialEq)]
26pub struct GraphQLToken<'src> {
27    /// The kind of token (including Error for lexer errors).
28    pub kind: GraphQLTokenKind<'src>,
29
30    /// Trivia (comments, commas) that precede this token.
31    pub preceding_trivia: GraphQLTriviaTokenVec<'src>,
32
33    /// The source location span of this token.
34    pub span: GraphQLSourceSpan,
35}
36
37impl<'src> GraphQLToken<'src> {
38    /// Convenience constructor for a token with no preceding trivia.
39    pub fn new(kind: GraphQLTokenKind<'src>, span: GraphQLSourceSpan) -> Self {
40        Self {
41            kind,
42            preceding_trivia: SmallVec::new(),
43            span,
44        }
45    }
46}