oak_graphql/lexer/
token_type.rs1use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type GraphQLToken = Token<GraphQLTokenType>;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum GraphQLTokenType {
12 StringLiteral,
14 IntLiteral,
16 FloatLiteral,
18 BooleanLiteral,
20 NullLiteral,
22 Name,
24 QueryKeyword,
26 MutationKeyword,
28 SubscriptionKeyword,
30 FragmentKeyword,
32 OnKeyword,
34 TypeKeyword,
36 InterfaceKeyword,
38 UnionKeyword,
40 ScalarKeyword,
42 EnumKeyword,
44 InputKeyword,
46 ExtendKeyword,
48 SchemaKeyword,
50 DirectiveKeyword,
52 ImplementsKeyword,
54 RepeatsKeyword,
56 Spread,
58 LeftParen,
60 RightParen,
62 LeftBracket,
64 RightBracket,
66 LeftBrace,
68 RightBrace,
70 Comma,
72 Colon,
74 Semicolon,
76 Pipe,
78 Ampersand,
80 Equals,
82 Exclamation,
84 At,
86 Dollar,
88 Whitespace,
90 Comment,
92 SourceFile,
94 Newline,
96 Eof,
98 Error,
100}
101
102impl GraphQLTokenType {
103 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}