Skip to main content

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    ClassDeclaration,
130    InterfaceDeclaration,
131    MethodDeclaration,
132    FieldDeclaration,
133    Parameter,
134    BlockStatement,
135    IfStatement,
136    WhileStatement,
137    ForStatement,
138    ReturnStatement,
139    ExpressionStatement,
140    BinaryExpression,
141    MethodCall,
142    MemberSelect,
143    LiteralExpression,
144    VariableDeclaration,
145}
146
147impl JavaSyntaxKind {
148    pub fn is_keyword(&self) -> bool {
149        matches!(
150            self,
151            Self::Abstract
152                | Self::Assert
153                | Self::Boolean
154                | Self::Break
155                | Self::Byte
156                | Self::Case
157                | Self::Catch
158                | Self::Char
159                | Self::Class
160                | Self::Const
161                | Self::Continue
162                | Self::Default
163                | Self::Do
164                | Self::Double
165                | Self::Else
166                | Self::Enum
167                | Self::Extends
168                | Self::Final
169                | Self::Finally
170                | Self::Float
171                | Self::For
172                | Self::If
173                | Self::Goto
174                | Self::Implements
175                | Self::Import
176                | Self::Instanceof
177                | Self::Int
178                | Self::Interface
179                | Self::Long
180                | Self::Native
181                | Self::New
182                | Self::Package
183                | Self::Private
184                | Self::Protected
185                | Self::Public
186                | Self::Return
187                | Self::Short
188                | Self::Static
189                | Self::Strictfp
190                | Self::Super
191                | Self::Switch
192                | Self::Synchronized
193                | Self::This
194                | Self::Throw
195                | Self::Throws
196                | Self::Transient
197                | Self::Try
198                | Self::Void
199                | Self::Volatile
200                | Self::While
201        )
202    }
203
204    pub fn from_keyword(s: &str) -> Option<Self> {
205        match s {
206            "abstract" => Some(Self::Abstract),
207            "assert" => Some(Self::Assert),
208            "boolean" => Some(Self::Boolean),
209            "break" => Some(Self::Break),
210            "byte" => Some(Self::Byte),
211            "case" => Some(Self::Case),
212            "catch" => Some(Self::Catch),
213            "char" => Some(Self::Char),
214            "class" => Some(Self::Class),
215            "const" => Some(Self::Const),
216            "continue" => Some(Self::Continue),
217            "default" => Some(Self::Default),
218            "do" => Some(Self::Do),
219            "double" => Some(Self::Double),
220            "else" => Some(Self::Else),
221            "enum" => Some(Self::Enum),
222            "extends" => Some(Self::Extends),
223            "final" => Some(Self::Final),
224            "finally" => Some(Self::Finally),
225            "float" => Some(Self::Float),
226            "for" => Some(Self::For),
227            "if" => Some(Self::If),
228            "goto" => Some(Self::Goto),
229            "implements" => Some(Self::Implements),
230            "import" => Some(Self::Import),
231            "instanceof" => Some(Self::Instanceof),
232            "int" => Some(Self::Int),
233            "interface" => Some(Self::Interface),
234            "long" => Some(Self::Long),
235            "native" => Some(Self::Native),
236            "new" => Some(Self::New),
237            "package" => Some(Self::Package),
238            "private" => Some(Self::Private),
239            "protected" => Some(Self::Protected),
240            "public" => Some(Self::Public),
241            "return" => Some(Self::Return),
242            "short" => Some(Self::Short),
243            "static" => Some(Self::Static),
244            "strictfp" => Some(Self::Strictfp),
245            "super" => Some(Self::Super),
246            "switch" => Some(Self::Switch),
247            "synchronized" => Some(Self::Synchronized),
248            "this" => Some(Self::This),
249            "throw" => Some(Self::Throw),
250            "throws" => Some(Self::Throws),
251            "transient" => Some(Self::Transient),
252            "try" => Some(Self::Try),
253            "void" => Some(Self::Void),
254            "volatile" => Some(Self::Volatile),
255            "while" => Some(Self::While),
256            _ => None,
257        }
258    }
259}
260
261impl TokenType for JavaSyntaxKind {
262    const END_OF_STREAM: Self = Self::Eof;
263    type Role = UniversalTokenRole;
264
265    fn role(&self) -> Self::Role {
266        match self {
267            Self::Whitespace => UniversalTokenRole::Whitespace,
268            Self::LineComment | Self::BlockComment => UniversalTokenRole::Comment,
269            Self::Identifier => UniversalTokenRole::Name,
270            Self::IntegerLiteral | Self::FloatingPointLiteral | Self::BooleanLiteral | Self::CharacterLiteral | Self::StringLiteral | Self::NullLiteral => UniversalTokenRole::Literal,
271            _ if self.is_keyword() => UniversalTokenRole::Keyword,
272            Self::Assign
273            | Self::GreaterThan
274            | Self::LessThan
275            | Self::Bang
276            | Self::Tilde
277            | Self::Question
278            | Self::Colon
279            | Self::Equals
280            | Self::LessThanEquals
281            | Self::GreaterThanEquals
282            | Self::BangEquals
283            | Self::AmpersandAmpersand
284            | Self::PipePipe
285            | Self::PlusPlus
286            | Self::MinusMinus
287            | Self::Plus
288            | Self::Minus
289            | Self::Asterisk
290            | Self::Slash
291            | Self::Ampersand
292            | Self::Pipe
293            | Self::Caret
294            | Self::Percent
295            | Self::LeftShift
296            | Self::RightShift
297            | Self::UnsignedRightShift
298            | Self::PlusEquals
299            | Self::MinusEquals
300            | Self::AsteriskEquals
301            | Self::SlashEquals
302            | Self::AmpersandEquals
303            | Self::PipeEquals
304            | Self::CaretEquals
305            | Self::PercentEquals
306            | Self::LeftShiftEquals
307            | Self::RightShiftEquals
308            | Self::UnsignedRightShiftEquals => UniversalTokenRole::Operator,
309            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,
310            Self::Eof => UniversalTokenRole::Eof,
311            _ => UniversalTokenRole::None,
312        }
313    }
314
315    fn is_comment(&self) -> bool {
316        matches!(self, Self::LineComment | Self::BlockComment)
317    }
318
319    fn is_whitespace(&self) -> bool {
320        matches!(self, Self::Whitespace)
321    }
322}
323
324impl ElementType for JavaSyntaxKind {
325    type Role = UniversalElementRole;
326
327    fn role(&self) -> Self::Role {
328        match self {
329            Self::CompilationUnit => UniversalElementRole::Root,
330            Self::Error => UniversalElementRole::Error,
331            _ => UniversalElementRole::None,
332        }
333    }
334
335    fn is_root(&self) -> bool {
336        matches!(self, Self::CompilationUnit)
337    }
338
339    fn is_error(&self) -> bool {
340        matches!(self, Self::Error)
341    }
342}