Skip to main content

oak_graphql/lexer/
token_type.rs

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