Skip to main content

libgraphql_parser/token_source/
graphql_token_source.rs

1use crate::token::GraphQLToken;
2
3/// Marker trait for [`GraphQLToken`] lexers (iterators that generate
4/// [`GraphQLToken`]).
5///
6/// This trait enables extensibility over different sources of GraphQL text to
7/// be parsed. For example: [`StrToGraphQLTokenSource`] is a lexer over `&str`
8/// types, [`libgraphql_macros::RustMacroGraphQLTokenSource`] is a lexer over
9/// [`proc_macro2::Span`] (for lexing Rust procedural macro input), etc.
10///
11/// Implementors define an [`Iterator`] that produces tokens one at a time.
12/// All lookahead, buffering, and peeking is handled by `GraphQLTokenStream`.
13///
14/// Lexers are responsible for:
15/// - Skipping whitespace (an "ignored token" per the GraphQL spec)
16/// - Accumulating trivia (comments, commas) and attaching to the next token
17/// - Emitting [`GraphQLTokenKind::Error`] for lexer errors (enables error
18///   recovery)
19/// - Emitting a final token with [`GraphQLTokenKind::Eof`] carrying any trailing
20///   trivia
21///
22/// # Lifetime Parameter
23///
24/// The `'src` lifetime represents the source text that tokens are lexed from.
25/// For string-based lexers, this enables zero-copy lexing where token values
26/// can borrow directly from the input. For proc-macro lexers that must allocate
27/// strings, use `'static` as the lifetime.
28pub trait GraphQLTokenSource<'src>: Iterator<Item = GraphQLToken<'src>> {}
29
30impl<'src, T> GraphQLTokenSource<'src> for T where T: Iterator<Item = GraphQLToken<'src>> {}