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
5pub type GraphQLToken = Token<GraphQLTokenType>;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum GraphQLTokenType {
10    StringLiteral,
11    IntLiteral,
12    FloatLiteral,
13    BooleanLiteral,
14    NullLiteral,
15    Name,
16    QueryKeyword,
17    MutationKeyword,
18    SubscriptionKeyword,
19    FragmentKeyword,
20    OnKeyword,
21    TypeKeyword,
22    InterfaceKeyword,
23    UnionKeyword,
24    ScalarKeyword,
25    EnumKeyword,
26    InputKeyword,
27    ExtendKeyword,
28    SchemaKeyword,
29    DirectiveKeyword,
30    ImplementsKeyword,
31    RepeatsKeyword,
32    Spread,
33    LeftParen,
34    RightParen,
35    LeftBracket,
36    RightBracket,
37    LeftBrace,
38    RightBrace,
39    Comma,
40    Colon,
41    Semicolon,
42    Pipe,
43    Ampersand,
44    Equals,
45    Exclamation,
46    At,
47    Dollar,
48    Whitespace,
49    Comment,
50    SourceFile,
51    Newline,
52    Eof,
53    Error,
54}
55
56impl GraphQLTokenType {
57    pub fn is_keyword(&self) -> bool {
58        matches!(
59            self,
60            Self::QueryKeyword
61                | Self::MutationKeyword
62                | Self::SubscriptionKeyword
63                | Self::FragmentKeyword
64                | Self::OnKeyword
65                | Self::TypeKeyword
66                | Self::InterfaceKeyword
67                | Self::UnionKeyword
68                | Self::ScalarKeyword
69                | Self::EnumKeyword
70                | Self::InputKeyword
71                | Self::ExtendKeyword
72                | Self::SchemaKeyword
73                | Self::DirectiveKeyword
74                | Self::ImplementsKeyword
75                | Self::RepeatsKeyword
76        )
77    }
78}
79
80impl TokenType for GraphQLTokenType {
81    type Role = UniversalTokenRole;
82    const END_OF_STREAM: Self = Self::Eof;
83
84    fn is_ignored(&self) -> bool {
85        matches!(self, Self::Whitespace | Self::Newline | Self::Comment)
86    }
87
88    fn role(&self) -> Self::Role {
89        match self {
90            Self::Whitespace => UniversalTokenRole::Whitespace,
91            Self::Newline => UniversalTokenRole::Whitespace,
92            Self::Comment => UniversalTokenRole::Comment,
93            Self::Eof => UniversalTokenRole::Eof,
94            Self::Error => UniversalTokenRole::Error,
95            _ => UniversalTokenRole::None,
96        }
97    }
98}