1use oak_core::{ElementType, TokenType, UniversalElementRole, UniversalTokenRole};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub enum TypeScriptSyntaxKind {
6 Root,
8 SourceFile,
9 Module,
10
11 VariableDeclaration,
13 FunctionDeclaration,
14 ClassDeclaration,
15 InterfaceDeclaration,
16 TypeAliasDeclaration,
17 EnumDeclaration,
18 NamespaceDeclaration,
19 ClassBody,
20 ImportDeclaration,
21 ExportDeclaration,
22 ImportClause,
23 NamedImports,
24 ImportSpecifier,
25 Parameter,
26 CallArgument,
27 PropertyDeclaration,
28 MethodDeclaration,
29 ConstructorDeclaration,
30
31 BinaryExpression,
33 UnaryExpression,
34 ConditionalExpression,
35 CallExpression,
36 NewExpression,
37 MemberExpression,
38 ArrayExpression,
39 ObjectExpression,
40 FunctionExpression,
41 ArrowFunction,
42 TemplateExpression,
43 TaggedTemplateExpression,
44 AsExpression,
45 TypeAssertionExpression,
46 NonNullExpression,
47
48 ExpressionStatement,
50 BlockStatement,
51 IfStatement,
52 WhileStatement,
53 ForStatement,
54 ForInStatement,
55 ForOfStatement,
56 DoWhileStatement,
57 SwitchStatement,
58 CaseClause,
59 DefaultClause,
60 TryStatement,
61 CatchClause,
62 FinallyClause,
63 ThrowStatement,
64 ReturnStatement,
65 BreakStatement,
66 ContinueStatement,
67 DebuggerStatement,
68 WithStatement,
69
70 BindingPattern,
72 ArrayBindingPattern,
73 ObjectBindingPattern,
74 BindingElement,
75
76 TypeReference,
78 TypeLiteral,
79 FunctionType,
80 ConstructorType,
81 ArrayType,
82 TupleType,
83 UnionType,
84 IntersectionType,
85 ConditionalType,
86 MappedType,
87 IndexedAccessType,
88 TypeQuery,
89 TypePredicate,
90
91 Error,
93
94 Abstract,
96 Any,
97 As,
98 Asserts,
99 Async,
100 Await,
101 Boolean,
102 Break,
103 Case,
104 Catch,
105 Class,
106 Const,
107 Constructor,
108 Continue,
109 Debugger,
110 Declare,
111 Default,
112 Delete,
113 Do,
114 Else,
115 Enum,
116 Export,
117 Extends,
118 False,
119 Finally,
120 For,
121 From,
122 Function,
123 Get,
124 Global,
125 If,
126 Implements,
127 Import,
128 In,
129 Infer,
130 Instanceof,
131 Interface,
132 Is,
133 Keyof,
134 Let,
135 Namespace,
136 Never,
137 New,
138 Null,
139 Number,
140 Object,
141 Of,
142 Package,
143 Private,
144 Protected,
145 Public,
146 Readonly,
147 Require,
148 Return,
149 Set,
150 Static,
151 String,
152 Super,
153 Switch,
154 Symbol,
155 This,
156 Throw,
157 True,
158 Try,
159 Type,
160 Typeof,
161 Undefined,
162 Unique,
163 Unknown,
164 Var,
165 Void,
166 While,
167 With,
168 Yield,
169
170 Plus,
172 Minus,
173 Star,
174 Slash,
175 Percent,
176 StarStar,
177
178 Less,
180 Greater,
181 LessEqual,
182 GreaterEqual,
183 EqualEqual,
184 NotEqual,
185 EqualEqualEqual,
186 NotEqualEqual,
187
188 AmpersandAmpersand,
190 PipePipe,
191 Exclamation,
192
193 Ampersand,
195 Pipe,
196 Caret,
197 Tilde,
198 LeftShift,
199 RightShift,
200 UnsignedRightShift,
201
202 Equal,
204 PlusEqual,
205 MinusEqual,
206 StarEqual,
207 SlashEqual,
208 PercentEqual,
209 StarStarEqual,
210 LeftShiftEqual,
211 RightShiftEqual,
212 UnsignedRightShiftEqual,
213 AmpersandEqual,
214 PipeEqual,
215 CaretEqual,
216 AmpersandAmpersandEqual,
217 PipePipeEqual,
218 QuestionQuestionEqual,
219
220 PlusPlus,
222 MinusMinus,
223
224 Question,
226 QuestionQuestion,
227 QuestionDot,
228 Arrow,
229
230 LeftParen,
232 RightParen,
233 LeftBrace,
234 RightBrace,
235 LeftBracket,
236 RightBracket,
237 Semicolon,
238 Comma,
239 Dot,
240 DotDotDot,
241 Colon,
242
243 StringLiteral,
245 NumericLiteral,
246 BigIntLiteral,
247 BooleanLiteral,
248 TemplateString,
249 RegexLiteral,
250
251 IdentifierName,
253
254 LineComment,
256 BlockComment,
257 Whitespace,
258 Newline,
259
260 Eof,
262}
263
264impl TypeScriptSyntaxKind {
265 pub fn is_keyword(&self) -> bool {
266 matches!(
267 self,
268 Self::Abstract
269 | Self::Any
270 | Self::As
271 | Self::Asserts
272 | Self::Async
273 | Self::Await
274 | Self::Boolean
275 | Self::Break
276 | Self::Case
277 | Self::Catch
278 | Self::Class
279 | Self::Const
280 | Self::Constructor
281 | Self::Continue
282 | Self::Debugger
283 | Self::Declare
284 | Self::Default
285 | Self::Delete
286 | Self::Do
287 | Self::Else
288 | Self::Enum
289 | Self::Export
290 | Self::Extends
291 | Self::False
292 | Self::Finally
293 | Self::For
294 | Self::From
295 | Self::Function
296 | Self::Get
297 | Self::Global
298 | Self::If
299 | Self::Implements
300 | Self::Import
301 | Self::In
302 | Self::Infer
303 | Self::Instanceof
304 | Self::Interface
305 | Self::Is
306 | Self::Keyof
307 | Self::Let
308 | Self::Namespace
309 | Self::Never
310 | Self::New
311 | Self::Null
312 | Self::Number
313 | Self::Object
314 | Self::Of
315 | Self::Package
316 | Self::Private
317 | Self::Protected
318 | Self::Public
319 | Self::Readonly
320 | Self::Require
321 | Self::Return
322 | Self::Set
323 | Self::Static
324 | Self::String
325 | Self::Super
326 | Self::Switch
327 | Self::Symbol
328 | Self::This
329 | Self::Throw
330 | Self::True
331 | Self::Try
332 | Self::Type
333 | Self::Typeof
334 | Self::Undefined
335 | Self::Unique
336 | Self::Unknown
337 | Self::Var
338 | Self::Void
339 | Self::While
340 | Self::With
341 | Self::Yield
342 )
343 }
344
345 pub fn from_keyword(s: &str) -> Option<Self> {
346 match s {
347 "abstract" => Some(Self::Abstract),
348 "any" => Some(Self::Any),
349 "as" => Some(Self::As),
350 "asserts" => Some(Self::Asserts),
351 "async" => Some(Self::Async),
352 "await" => Some(Self::Await),
353 "boolean" => Some(Self::Boolean),
354 "break" => Some(Self::Break),
355 "case" => Some(Self::Case),
356 "catch" => Some(Self::Catch),
357 "class" => Some(Self::Class),
358 "const" => Some(Self::Const),
359 "constructor" => Some(Self::Constructor),
360 "continue" => Some(Self::Continue),
361 "debugger" => Some(Self::Debugger),
362 "declare" => Some(Self::Declare),
363 "default" => Some(Self::Default),
364 "delete" => Some(Self::Delete),
365 "do" => Some(Self::Do),
366 "else" => Some(Self::Else),
367 "enum" => Some(Self::Enum),
368 "export" => Some(Self::Export),
369 "extends" => Some(Self::Extends),
370 "false" => Some(Self::False),
371 "finally" => Some(Self::Finally),
372 "for" => Some(Self::For),
373 "from" => Some(Self::From),
374 "function" => Some(Self::Function),
375 "get" => Some(Self::Get),
376 "global" => Some(Self::Global),
377 "if" => Some(Self::If),
378 "implements" => Some(Self::Implements),
379 "import" => Some(Self::Import),
380 "in" => Some(Self::In),
381 "infer" => Some(Self::Infer),
382 "instanceof" => Some(Self::Instanceof),
383 "interface" => Some(Self::Interface),
384 "is" => Some(Self::Is),
385 "keyof" => Some(Self::Keyof),
386 "let" => Some(Self::Let),
387 "namespace" => Some(Self::Namespace),
388 "never" => Some(Self::Never),
389 "new" => Some(Self::New),
390 "null" => Some(Self::Null),
391 "number" => Some(Self::Number),
392 "object" => Some(Self::Object),
393 "of" => Some(Self::Of),
394 "package" => Some(Self::Package),
395 "private" => Some(Self::Private),
396 "protected" => Some(Self::Protected),
397 "public" => Some(Self::Public),
398 "readonly" => Some(Self::Readonly),
399 "require" => Some(Self::Require),
400 "return" => Some(Self::Return),
401 "set" => Some(Self::Set),
402 "static" => Some(Self::Static),
403 "string" => Some(Self::String),
404 "super" => Some(Self::Super),
405 "switch" => Some(Self::Switch),
406 "symbol" => Some(Self::Symbol),
407 "this" => Some(Self::This),
408 "throw" => Some(Self::Throw),
409 "true" => Some(Self::True),
410 "try" => Some(Self::Try),
411 "type" => Some(Self::Type),
412 "typeof" => Some(Self::Typeof),
413 "undefined" => Some(Self::Undefined),
414 "unique" => Some(Self::Unique),
415 "unknown" => Some(Self::Unknown),
416 "var" => Some(Self::Var),
417 "void" => Some(Self::Void),
418 "while" => Some(Self::While),
419 "with" => Some(Self::With),
420 "yield" => Some(Self::Yield),
421 _ => None,
422 }
423 }
424}
425
426impl TokenType for TypeScriptSyntaxKind {
427 const END_OF_STREAM: Self = Self::Eof;
428 type Role = UniversalTokenRole;
429
430 fn role(&self) -> Self::Role {
431 match self {
432 Self::Whitespace | Self::Newline => UniversalTokenRole::Whitespace,
433 Self::LineComment | Self::BlockComment => UniversalTokenRole::Comment,
434 Self::IdentifierName => UniversalTokenRole::Name,
435 Self::True | Self::False | Self::Null | Self::NumericLiteral | Self::StringLiteral | Self::BigIntLiteral | Self::TemplateString | Self::RegexLiteral => UniversalTokenRole::Literal,
436 _ if self.is_keyword() => UniversalTokenRole::Keyword,
437 Self::Plus
438 | Self::Minus
439 | Self::Star
440 | Self::Slash
441 | Self::Percent
442 | Self::StarStar
443 | Self::Less
444 | Self::Greater
445 | Self::LessEqual
446 | Self::GreaterEqual
447 | Self::EqualEqual
448 | Self::NotEqual
449 | Self::EqualEqualEqual
450 | Self::NotEqualEqual
451 | Self::AmpersandAmpersand
452 | Self::PipePipe
453 | Self::Exclamation
454 | Self::Ampersand
455 | Self::Pipe
456 | Self::Caret
457 | Self::Tilde
458 | Self::LeftShift
459 | Self::RightShift
460 | Self::UnsignedRightShift
461 | Self::Equal
462 | Self::PlusEqual
463 | Self::MinusEqual
464 | Self::StarEqual
465 | Self::SlashEqual
466 | Self::PercentEqual
467 | Self::StarStarEqual
468 | Self::LeftShiftEqual
469 | Self::RightShiftEqual
470 | Self::UnsignedRightShiftEqual
471 | Self::AmpersandEqual
472 | Self::PipeEqual
473 | Self::CaretEqual
474 | Self::AmpersandAmpersandEqual
475 | Self::PipePipeEqual
476 | Self::QuestionQuestionEqual
477 | Self::PlusPlus
478 | Self::MinusMinus
479 | Self::Question
480 | Self::QuestionQuestion
481 | Self::QuestionDot
482 | Self::Arrow => UniversalTokenRole::Operator,
483 Self::LeftParen | Self::RightParen | Self::LeftBrace | Self::RightBrace | Self::LeftBracket | Self::RightBracket | Self::Semicolon | Self::Comma | Self::Dot | Self::DotDotDot | Self::Colon => UniversalTokenRole::Punctuation,
484 Self::Eof => UniversalTokenRole::Eof,
485 _ => UniversalTokenRole::None,
486 }
487 }
488
489 fn is_comment(&self) -> bool {
490 matches!(self, Self::LineComment | Self::BlockComment)
491 }
492
493 fn is_whitespace(&self) -> bool {
494 matches!(self, Self::Whitespace | Self::Newline)
495 }
496}
497
498impl ElementType for TypeScriptSyntaxKind {
499 type Role = UniversalElementRole;
500
501 fn role(&self) -> Self::Role {
502 match self {
503 Self::Root | Self::SourceFile => UniversalElementRole::Root,
504 Self::VariableDeclaration
505 | Self::FunctionDeclaration
506 | Self::ClassDeclaration
507 | Self::InterfaceDeclaration
508 | Self::TypeAliasDeclaration
509 | Self::EnumDeclaration
510 | Self::NamespaceDeclaration
511 | Self::ImportDeclaration
512 | Self::ExportDeclaration => UniversalElementRole::Definition,
513 Self::ExpressionStatement | Self::BlockStatement | Self::IfStatement | Self::WhileStatement | Self::ForStatement | Self::ReturnStatement | Self::BreakStatement | Self::ContinueStatement | Self::ThrowStatement | Self::TryStatement => {
514 UniversalElementRole::Statement
515 }
516 Self::BinaryExpression | Self::UnaryExpression | Self::ConditionalExpression | Self::CallExpression | Self::MemberExpression | Self::ArrayExpression | Self::ObjectExpression | Self::AsExpression => UniversalElementRole::Expression,
517 Self::Error => UniversalElementRole::Error,
518 _ => UniversalElementRole::None,
519 }
520 }
521
522 fn is_root(&self) -> bool {
523 matches!(self, Self::Root | Self::SourceFile)
524 }
525
526 fn is_error(&self) -> bool {
527 matches!(self, Self::Error)
528 }
529}