Skip to main content

oak_graphql/lexer/
token_type.rs

1use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2
3/// Represents a token in a GraphQL source file.
4pub type GraphQLToken = Token<GraphQLTokenType>;
5
6/// Token types for GraphQL.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum GraphQLTokenType {
10    /// A string literal.
11    StringLiteral,
12    /// An integer literal.
13    IntLiteral,
14    /// A float literal.
15    FloatLiteral,
16    /// A boolean literal (`true` or `false`).
17    BooleanLiteral,
18    /// A null literal (`null`).
19    NullLiteral,
20    /// A name (identifier).
21    Name,
22    /// The `query` keyword.
23    QueryKeyword,
24    /// The `mutation` keyword.
25    MutationKeyword,
26    /// The `subscription` keyword.
27    SubscriptionKeyword,
28    /// The `fragment` keyword.
29    FragmentKeyword,
30    /// The `on` keyword.
31    OnKeyword,
32    /// The `type` keyword.
33    TypeKeyword,
34    /// The `interface` keyword.
35    InterfaceKeyword,
36    /// The `union` keyword.
37    UnionKeyword,
38    /// The `scalar` keyword.
39    ScalarKeyword,
40    /// The `enum` keyword.
41    EnumKeyword,
42    /// The `input` keyword.
43    InputKeyword,
44    /// The `extend` keyword.
45    ExtendKeyword,
46    /// The `schema` keyword.
47    SchemaKeyword,
48    /// The `directive` keyword.
49    DirectiveKeyword,
50    /// The `implements` keyword.
51    ImplementsKeyword,
52    /// The `repeats` keyword.
53    RepeatsKeyword,
54    /// The spread operator `...`.
55    Spread,
56    /// Left parenthesis `(`.
57    LeftParen,
58    /// Right parenthesis `)`.
59    RightParen,
60    /// Left bracket `[`.
61    LeftBracket,
62    /// Right bracket `]`.
63    RightBracket,
64    /// Left brace `{`.
65    LeftBrace,
66    /// Right brace `}`.
67    RightBrace,
68    /// Comma `,`.
69    Comma,
70    /// Colon `:`.
71    Colon,
72    /// Semicolon `;`.
73    Semicolon,
74    /// Pipe symbol `|`.
75    Pipe,
76    /// Ampersand symbol `&`.
77    Ampersand,
78    /// Equals symbol `=`.
79    Equals,
80    /// Exclamation mark `!`.
81    Exclamation,
82    /// At symbol `@`.
83    At,
84    /// Dollar sign `$`.
85    Dollar,
86    /// Whitespace.
87    Whitespace,
88    /// A comment.
89    Comment,
90    /// The root of the source file.
91    SourceFile,
92    /// A newline.
93    Newline,
94    /// End of file.
95    Eof,
96    /// An error token.
97    Error,
98}
99
100impl GraphQLTokenType {
101    /// Returns true if the token type is a keyword.
102    pub fn is_keyword(&self) -> bool {
103        matches!(
104            self,
105            Self::QueryKeyword
106                | Self::MutationKeyword
107                | Self::SubscriptionKeyword
108                | Self::FragmentKeyword
109                | Self::OnKeyword
110                | Self::TypeKeyword
111                | Self::InterfaceKeyword
112                | Self::UnionKeyword
113                | Self::ScalarKeyword
114                | Self::EnumKeyword
115                | Self::InputKeyword
116                | Self::ExtendKeyword
117                | Self::SchemaKeyword
118                | Self::DirectiveKeyword
119                | Self::ImplementsKeyword
120                | Self::RepeatsKeyword
121        )
122    }
123}
124
125impl TokenType for GraphQLTokenType {
126    type Role = UniversalTokenRole;
127    const END_OF_STREAM: Self = Self::Eof;
128
129    fn is_ignored(&self) -> bool {
130        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
131    }
132
133    fn role(&self) -> Self::Role {
134        match self {
135            Self::Whitespace => UniversalTokenRole::Whitespace,
136            Self::Newline => UniversalTokenRole::Whitespace,
137            Self::Comment => UniversalTokenRole::Comment,
138            Self::Eof => UniversalTokenRole::Eof,
139            Self::Error => UniversalTokenRole::Error,
140            _ => UniversalTokenRole::None,
141        }
142    }
143}