oak_java/kind/
mod.rs

1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4/// Java 语言Token 类型
5#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)]
6pub enum JavaSyntaxKind {
7    // 关键
8    Abstract,
9    Assert,
10    Boolean,
11    Break,
12    Byte,
13    Case,
14    Catch,
15    Char,
16    Class,
17    Const,
18    Continue,
19    Default,
20    Do,
21    Double,
22    Else,
23    Enum,
24    Extends,
25    Final,
26    Finally,
27    Float,
28    For,
29    If,
30    Goto,
31    Implements,
32    Import,
33    Instanceof,
34    Int,
35    Interface,
36    Long,
37    Native,
38    New,
39    Package,
40    Private,
41    Protected,
42    Public,
43    Return,
44    Short,
45    Static,
46    Strictfp,
47    Super,
48    Switch,
49    Synchronized,
50    This,
51    Throw,
52    Throws,
53    Transient,
54    Try,
55    Void,
56    Volatile,
57    While,
58    // 字面
59    IntegerLiteral,
60    FloatingPointLiteral,
61    BooleanLiteral,
62    CharacterLiteral,
63    StringLiteral,
64    NullLiteral,
65    // 运算
66    Assign,                   // =
67    GreaterThan,              // >
68    LessThan,                 // <
69    Bang,                     // !
70    Tilde,                    // ~
71    Question,                 // ?
72    Colon,                    // :
73    Equals,                   // ==
74    LessThanEquals,           // <=
75    GreaterThanEquals,        // >=
76    BangEquals,               // !=
77    AmpersandAmpersand,       // &&
78    PipePipe,                 // ||
79    PlusPlus,                 // ++
80    MinusMinus,               // --
81    Plus,                     // +
82    Minus,                    // -
83    Asterisk,                 // *
84    Slash,                    // /
85    Ampersand,                // &
86    Pipe,                     // |
87    Caret,                    // ^
88    Percent,                  // %
89    LeftShift,                // <<
90    RightShift,               // >>
91    UnsignedRightShift,       // >>>
92    PlusEquals,               // +=
93    MinusEquals,              // -=
94    AsteriskEquals,           // *=
95    SlashEquals,              // /=
96    AmpersandEquals,          // &=
97    PipeEquals,               // |=
98    CaretEquals,              // ^=
99    PercentEquals,            // %=
100    LeftShiftEquals,          // <<=
101    RightShiftEquals,         // >>=
102    UnsignedRightShiftEquals, // >>>=
103    // 分隔
104    LeftParen,    // (
105    RightParen,   // )
106    LeftBrace,    // {
107    RightBrace,   // }
108    LeftBracket,  // [
109    RightBracket, // ]
110    Semicolon,    // ;
111    Comma,        // ,
112    Dot,          // .
113    Ellipsis,     // ...
114    At,           // @
115    DoubleColon,  // ::
116    // 标识
117    Identifier,
118    // 注释
119    LineComment,
120    BlockComment,
121    // 空白
122    Whitespace,
123    // 文件结束
124    Eof,
125    // 错误
126    Error,
127    // 非终端节点
128    CompilationUnit,
129}
130
131impl JavaSyntaxKind {
132    pub fn is_keyword(&self) -> bool {
133        matches!(
134            self,
135            Self::Abstract
136                | Self::Assert
137                | Self::Boolean
138                | Self::Break
139                | Self::Byte
140                | Self::Case
141                | Self::Catch
142                | Self::Char
143                | Self::Class
144                | Self::Const
145                | Self::Continue
146                | Self::Default
147                | Self::Do
148                | Self::Double
149                | Self::Else
150                | Self::Enum
151                | Self::Extends
152                | Self::Final
153                | Self::Finally
154                | Self::Float
155                | Self::For
156                | Self::If
157                | Self::Goto
158                | Self::Implements
159                | Self::Import
160                | Self::Instanceof
161                | Self::Int
162                | Self::Interface
163                | Self::Long
164                | Self::Native
165                | Self::New
166                | Self::Package
167                | Self::Private
168                | Self::Protected
169                | Self::Public
170                | Self::Return
171                | Self::Short
172                | Self::Static
173                | Self::Strictfp
174                | Self::Super
175                | Self::Switch
176                | Self::Synchronized
177                | Self::This
178                | Self::Throw
179                | Self::Throws
180                | Self::Transient
181                | Self::Try
182                | Self::Void
183                | Self::Volatile
184                | Self::While
185        )
186    }
187
188    pub fn from_keyword(s: &str) -> Option<Self> {
189        match s {
190            "abstract" => Some(Self::Abstract),
191            "assert" => Some(Self::Assert),
192            "boolean" => Some(Self::Boolean),
193            "break" => Some(Self::Break),
194            "byte" => Some(Self::Byte),
195            "case" => Some(Self::Case),
196            "catch" => Some(Self::Catch),
197            "char" => Some(Self::Char),
198            "class" => Some(Self::Class),
199            "const" => Some(Self::Const),
200            "continue" => Some(Self::Continue),
201            "default" => Some(Self::Default),
202            "do" => Some(Self::Do),
203            "double" => Some(Self::Double),
204            "else" => Some(Self::Else),
205            "enum" => Some(Self::Enum),
206            "extends" => Some(Self::Extends),
207            "final" => Some(Self::Final),
208            "finally" => Some(Self::Finally),
209            "float" => Some(Self::Float),
210            "for" => Some(Self::For),
211            "if" => Some(Self::If),
212            "goto" => Some(Self::Goto),
213            "implements" => Some(Self::Implements),
214            "import" => Some(Self::Import),
215            "instanceof" => Some(Self::Instanceof),
216            "int" => Some(Self::Int),
217            "interface" => Some(Self::Interface),
218            "long" => Some(Self::Long),
219            "native" => Some(Self::Native),
220            "new" => Some(Self::New),
221            "package" => Some(Self::Package),
222            "private" => Some(Self::Private),
223            "protected" => Some(Self::Protected),
224            "public" => Some(Self::Public),
225            "return" => Some(Self::Return),
226            "short" => Some(Self::Short),
227            "static" => Some(Self::Static),
228            "strictfp" => Some(Self::Strictfp),
229            "super" => Some(Self::Super),
230            "switch" => Some(Self::Switch),
231            "synchronized" => Some(Self::Synchronized),
232            "this" => Some(Self::This),
233            "throw" => Some(Self::Throw),
234            "throws" => Some(Self::Throws),
235            "transient" => Some(Self::Transient),
236            "try" => Some(Self::Try),
237            "void" => Some(Self::Void),
238            "volatile" => Some(Self::Volatile),
239            "while" => Some(Self::While),
240            _ => None,
241        }
242    }
243}
244
245impl TokenType for JavaSyntaxKind {
246    const END_OF_STREAM: Self = Self::Eof;
247    type Role = UniversalTokenRole;
248
249    fn role(&self) -> Self::Role {
250        match self {
251            Self::Whitespace => UniversalTokenRole::Whitespace,
252            Self::LineComment | Self::BlockComment => UniversalTokenRole::Comment,
253            Self::Identifier => UniversalTokenRole::Name,
254            Self::IntegerLiteral | Self::FloatingPointLiteral | Self::BooleanLiteral | Self::CharacterLiteral | Self::StringLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
255            _ if self.is_keyword() => UniversalTokenRole::Keyword,
256            Self::Assign
257            | Self::GreaterThan
258            | Self::LessThan
259            | Self::Bang
260            | Self::Tilde
261            | Self::Question
262            | Self::Colon
263            | Self::Equals
264            | Self::LessThanEquals
265            | Self::GreaterThanEquals
266            | Self::BangEquals
267            | Self::AmpersandAmpersand
268            | Self::PipePipe
269            | Self::PlusPlus
270            | Self::MinusMinus
271            | Self::Plus
272            | Self::Minus
273            | Self::Asterisk
274            | Self::Slash
275            | Self::Ampersand
276            | Self::Pipe
277            | Self::Caret
278            | Self::Percent
279            | Self::LeftShift
280            | Self::RightShift
281            | Self::UnsignedRightShift
282            | Self::PlusEquals
283            | Self::MinusEquals
284            | Self::AsteriskEquals
285            | Self::SlashEquals
286            | Self::AmpersandEquals
287            | Self::PipeEquals
288            | Self::CaretEquals
289            | Self::PercentEquals
290            | Self::LeftShiftEquals
291            | Self::RightShiftEquals
292            | Self::UnsignedRightShiftEquals => UniversalTokenRole::Operator,
293            Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Semicolon | Self::Comma | Self::Dot | Self::Ellipsis | Self::At | Self::DoubleColon => UniversalTokenRole::Punctuation,
294            Self::Eof => UniversalTokenRole::Eof,
295            _ => UniversalTokenRole::None,
296        }
297    }
298
299    fn is_comment(&self) -> bool {
300        matches!(self, Self::LineComment | Self::BlockComment)
301    }
302
303    fn is_whitespace(&self) -> bool {
304        matches!(self, Self::Whitespace)
305    }
306}
307
308impl ElementType for JavaSyntaxKind {
309    type Role = UniversalElementRole;
310
311    fn role(&self) -> Self::Role {
312        match self {
313            Self::CompilationUnit => UniversalElementRole::Root,
314            Self::Error => UniversalElementRole::Error,
315            _ => UniversalElementRole::None,
316        }
317    }
318
319    fn is_root(&self) -> bool {
320        matches!(self, Self::CompilationUnit)
321    }
322
323    fn is_error(&self) -> bool {
324        matches!(self, Self::Error)
325    }
326}