1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)]
6pub enum JavaSyntaxKind {
7 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 IntegerLiteral,
60 FloatingPointLiteral,
61 BooleanLiteral,
62 CharacterLiteral,
63 StringLiteral,
64 NullLiteral,
65 Assign, GreaterThan, LessThan, Bang, Tilde, Question, Colon, Equals, LessThanEquals, GreaterThanEquals, BangEquals, AmpersandAmpersand, PipePipe, PlusPlus, MinusMinus, Plus, Minus, Asterisk, Slash, Ampersand, Pipe, Caret, Percent, LeftShift, RightShift, UnsignedRightShift, PlusEquals, MinusEquals, AsteriskEquals, SlashEquals, AmpersandEquals, PipeEquals, CaretEquals, PercentEquals, LeftShiftEquals, RightShiftEquals, UnsignedRightShiftEquals, LeftParen, RightParen, LeftBrace, RightBrace, LeftBracket, RightBracket, Semicolon, Comma, Dot, Ellipsis, At, DoubleColon, Identifier,
118 LineComment,
120 BlockComment,
121 Whitespace,
123 Eof,
125 Error,
127 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}