oak_graphql/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2
3/// GraphQL 语法节点类型
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum GraphQLSyntaxKind {
6    // 字面量
7    StringLiteral,
8    IntLiteral,
9    FloatLiteral,
10    BooleanLiteral,
11    NullLiteral,
12
13    // 标识符和名称
14    Name,
15
16    // 关键字
17    QueryKeyword,
18    MutationKeyword,
19    SubscriptionKeyword,
20    FragmentKeyword,
21    OnKeyword,
22    TypeKeyword,
23    InterfaceKeyword,
24    UnionKeyword,
25    ScalarKeyword,
26    EnumKeyword,
27    InputKeyword,
28    ExtendKeyword,
29    SchemaKeyword,
30    DirectiveKeyword,
31    ImplementsKeyword,
32    RepeatsKeyword,
33
34    // 操作符
35    Spread, // ...
36
37    // 分隔符
38    LeftParen,    // (
39    RightParen,   // )
40    LeftBracket,  // [
41    RightBracket, // ]
42    LeftBrace,    // {
43    RightBrace,   // }
44    Comma,        // ,
45    Colon,        // :
46    Semicolon,    // ;
47    Pipe,         // |
48    Ampersand,    // &
49    Equals,       // =
50    Exclamation,  // !
51    At,           // @
52    Dollar,       // $
53
54    // 空白和注释
55    Whitespace,
56    Comment,
57
58    // 特殊
59    SourceFile,
60    Newline,
61    Eof,
62    Error,
63}
64
65impl GraphQLSyntaxKind {
66    pub fn is_keyword(&self) -> bool {
67        matches!(
68            self,
69            Self::QueryKeyword
70                | Self::MutationKeyword
71                | Self::SubscriptionKeyword
72                | Self::FragmentKeyword
73                | Self::OnKeyword
74                | Self::TypeKeyword
75                | Self::InterfaceKeyword
76                | Self::UnionKeyword
77                | Self::ScalarKeyword
78                | Self::EnumKeyword
79                | Self::InputKeyword
80                | Self::ExtendKeyword
81                | Self::SchemaKeyword
82                | Self::DirectiveKeyword
83                | Self::ImplementsKeyword
84                | Self::RepeatsKeyword
85        )
86    }
87}
88
89impl TokenType for GraphQLSyntaxKind {
90    const END_OF_STREAM: Self = Self::Eof;
91    type Role = UniversalTokenRole;
92
93    fn role(&self) -> Self::Role {
94        match self {
95            Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
96            Self::Comment => UniversalTokenRole::Comment,
97            Self::Name => UniversalTokenRole::Name,
98            Self::StringLiteral | Self::IntLiteral | Self::FloatLiteral | Self::BooleanLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
99            _ if self.is_keyword() => UniversalTokenRole::Keyword,
100            Self::Spread | Self::Equals | Self::Exclamation | Self::At | Self::Dollar | Self::Pipe | Self::Ampersand => UniversalTokenRole::Operator,
101            Self::LeftParen | Self::RightParen | Self::LeftBracket | Self::RightBracket | Self::LeftBrace | Self::RightBrace | Self::Comma | Self::Colon | Self::Semicolon => UniversalTokenRole::Punctuation,
102            Self::Eof => UniversalTokenRole::Eof,
103            _ => UniversalTokenRole::None,
104        }
105    }
106
107    fn is_comment(&self) -> bool {
108        matches!(self, Self::Comment)
109    }
110
111    fn is_whitespace(&self) -> bool {
112        matches!(self, Self::Whitespace | Self::Newline)
113    }
114}
115
116impl ElementType for GraphQLSyntaxKind {
117    type Role = UniversalElementRole;
118
119    fn role(&self) -> Self::Role {
120        match self {
121            Self::Error => UniversalElementRole::Error,
122            _ => UniversalElementRole::None,
123        }
124    }
125
126    fn is_error(&self) -> bool {
127        matches!(self, Self::Error)
128    }
129}