1use oak_core::{Token, TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5pub type TypeScriptToken = Token<TypeScriptTokenType>;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9#[repr(u16)]
10pub enum TypeScriptTokenType {
11 NamedImports,
12 Decorator,
13 ArrowFunction,
14 PredefinedType,
15 Abstract,
16 Any,
17 As,
18 Asserts,
19 Async,
20 Await,
21 Boolean,
22 Break,
23 Case,
24 Catch,
25 Class,
26 Const,
27 Constructor,
28 Continue,
29 Debugger,
30 Declare,
31 Default,
32 Delete,
33 Do,
34 Else,
35 Enum,
36 Export,
37 Extends,
38 False,
39 Finally,
40 For,
41 From,
42 Function,
43 Get,
44 Global,
45 If,
46 Implements,
47 Import,
48 In,
49 Infer,
50 Instanceof,
51 Interface,
52 Is,
53 Keyof,
54 Let,
55 Namespace,
56 Never,
57 New,
58 Null,
59 Number,
60 Object,
61 Of,
62 Override,
63 Package,
64 Private,
65 Protected,
66 Public,
67 Readonly,
68 Require,
69 Return,
70 Set,
71 Static,
72 String,
73 Super,
74 Switch,
75 Symbol,
76 This,
77 Throw,
78 True,
79 Try,
80 Type,
81 Typeof,
82 Undefined,
83 Unique,
84 Unknown,
85 Var,
86 Void,
87 While,
88 With,
89 Yield,
90 Plus,
91 Minus,
92 Star,
93 Slash,
94 Percent,
95 StarStar,
96 Question,
97 DotDotDot,
98 Less,
99 Greater,
100 LessEqual,
101 GreaterEqual,
102 EqualEqual,
103 NotEqual,
104 EqualEqualEqual,
105 NotEqualEqual,
106 AmpersandAmpersand,
107 PipePipe,
108 Exclamation,
109 Ampersand,
110 Pipe,
111 Caret,
112 Tilde,
113 LeftShift,
114 RightShift,
115 UnsignedRightShift,
116 Equal,
117 PlusEqual,
118 MinusEqual,
119 StarEqual,
120 SlashEqual,
121 PercentEqual,
122 StarStarEqual,
123 LeftShiftEqual,
124 RightShiftEqual,
125 UnsignedRightShiftEqual,
126 AmpersandEqual,
127 PipeEqual,
128 CaretEqual,
129 AmpersandAmpersandEqual,
130 PipePipeEqual,
131 QuestionQuestionEqual,
132 PlusPlus,
133 MinusMinus,
134 QuestionQuestion,
135 QuestionDot,
136 Arrow,
137 LeftParen,
138 RightParen,
139 LeftBrace,
140 RightBrace,
141 LeftBracket,
142 RightBracket,
143 Semicolon,
144 Comma,
145 Dot,
146 Colon,
147 At,
148 StringLiteral,
149 NumericLiteral,
150 BigIntLiteral,
151 BooleanLiteral,
152 TemplateString,
153 RegexLiteral,
154 IdentifierName,
155 LineComment,
156 BlockComment,
157 Whitespace,
158 Newline,
159 Eof,
160 Root,
161 SourceFile,
162 Module,
163 VariableDeclaration,
164 FunctionDeclaration,
165 ClassDeclaration,
166 InterfaceDeclaration,
167 TypeAliasDeclaration,
168 EnumDeclaration,
169 NamespaceDeclaration,
170 ClassBody,
171 ImportDeclaration,
172 ExportDeclaration,
173 ImportClause,
174 ImportSpecifier,
175 Parameter,
176 CallArgument,
177 PropertyDeclaration,
178 MethodDeclaration,
179 ConstructorDeclaration,
180 PropertyAssignment,
181 ShorthandPropertyAssignment,
182 SpreadElement,
183 Error,
184 JsxElement,
185 JsxSelfClosingElement,
186 JsxOpeningElement,
187 JsxClosingElement,
188 JsxFragment,
189 JsxOpeningFragment,
190 JsxClosingFragment,
191 JsxAttribute,
192 JsxAttributes,
193 JsxExpressionContainer,
194 JsxSpreadAttribute,
195 JsxText,
196 BinaryExpression,
197 UnaryExpression,
198 ConditionalExpression,
199 CallExpression,
200 NewExpression,
201 MemberExpression,
202 ArrayExpression,
203 ObjectExpression,
204 FunctionExpression,
205 TemplateExpression,
206 TaggedTemplateExpression,
207 AsExpression,
208 TypeAssertionExpression,
209 NonNullExpression,
210 UpdateExpression,
211 ExpressionStatement,
212 BlockStatement,
213 IfStatement,
214 WhileStatement,
215 ForStatement,
216 ForInStatement,
217 ForOfStatement,
218 DoWhileStatement,
219 SwitchStatement,
220 CaseClause,
221 DefaultClause,
222 TryStatement,
223 CatchClause,
224 FinallyClause,
225 ThrowStatement,
226 ReturnStatement,
227 BreakStatement,
228 ContinueStatement,
229 DebuggerStatement,
230 WithStatement,
231 BindingPattern,
232 ArrayBindingPattern,
233 ObjectBindingPattern,
234 BindingElement,
235 TypeReference,
236 TypeLiteral,
237 FunctionType,
238 ConstructorType,
239 ArrayType,
240 TupleType,
241 UnionType,
242 IntersectionType,
243 ConditionalType,
244 MappedType,
245 IndexedAccessType,
246 PropertySignature,
247 MethodSignature,
248 LiteralType,
249 TypeQuery,
250 TypePredicate,
251 TypeAnnotation,
252 TypeParameter,
253 HeritageClause,
254 EnumMember,
255}
256
257impl TypeScriptTokenType {
258 pub fn from_keyword(text: &str) -> Option<Self> {
259 match text {
260 "abstract" => Some(Self::Abstract),
261 "any" => Some(Self::Any),
262 "as" => Some(Self::As),
263 "asserts" => Some(Self::Asserts),
264 "async" => Some(Self::Async),
265 "await" => Some(Self::Await),
266 "boolean" => Some(Self::Boolean),
267 "break" => Some(Self::Break),
268 "case" => Some(Self::Case),
269 "catch" => Some(Self::Catch),
270 "class" => Some(Self::Class),
271 "const" => Some(Self::Const),
272 "constructor" => Some(Self::Constructor),
273 "continue" => Some(Self::Continue),
274 "debugger" => Some(Self::Debugger),
275 "declare" => Some(Self::Declare),
276 "default" => Some(Self::Default),
277 "delete" => Some(Self::Delete),
278 "do" => Some(Self::Do),
279 "else" => Some(Self::Else),
280 "enum" => Some(Self::Enum),
281 "export" => Some(Self::Export),
282 "extends" => Some(Self::Extends),
283 "false" => Some(Self::False),
284 "finally" => Some(Self::Finally),
285 "for" => Some(Self::For),
286 "from" => Some(Self::From),
287 "function" => Some(Self::Function),
288 "get" => Some(Self::Get),
289 "global" => Some(Self::Global),
290 "if" => Some(Self::If),
291 "implements" => Some(Self::Implements),
292 "import" => Some(Self::Import),
293 "in" => Some(Self::In),
294 "infer" => Some(Self::Infer),
295 "instanceof" => Some(Self::Instanceof),
296 "interface" => Some(Self::Interface),
297 "is" => Some(Self::Is),
298 "keyof" => Some(Self::Keyof),
299 "let" => Some(Self::Let),
300 "namespace" => Some(Self::Namespace),
301 "never" => Some(Self::Never),
302 "new" => Some(Self::New),
303 "null" => Some(Self::Null),
304 "number" => Some(Self::Number),
305 "object" => Some(Self::Object),
306 "of" => Some(Self::Of),
307 "override" => Some(Self::Override),
308 "package" => Some(Self::Package),
309 "private" => Some(Self::Private),
310 "protected" => Some(Self::Protected),
311 "public" => Some(Self::Public),
312 "readonly" => Some(Self::Readonly),
313 "require" => Some(Self::Require),
314 "return" => Some(Self::Return),
315 "set" => Some(Self::Set),
316 "static" => Some(Self::Static),
317 "string" => Some(Self::String),
318 "super" => Some(Self::Super),
319 "switch" => Some(Self::Switch),
320 "symbol" => Some(Self::Symbol),
321 "this" => Some(Self::This),
322 "throw" => Some(Self::Throw),
323 "true" => Some(Self::True),
324 "try" => Some(Self::Try),
325 "type" => Some(Self::Type),
326 "typeof" => Some(Self::Typeof),
327 "undefined" => Some(Self::Undefined),
328 "unique" => Some(Self::Unique),
329 "unknown" => Some(Self::Unknown),
330 "var" => Some(Self::Var),
331 "void" => Some(Self::Void),
332 "while" => Some(Self::While),
333 "with" => Some(Self::With),
334 "yield" => Some(Self::Yield),
335 _ => None,
336 }
337 }
338}
339
340impl TokenType for TypeScriptTokenType {
341 type Role = UniversalTokenRole;
342 const END_OF_STREAM: Self = Self::Eof;
343
344 fn is_ignored(&self) -> bool {
345 matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
346 }
347
348 fn role(&self) -> Self::Role {
349 match self {
350 Self::Whitespace => UniversalTokenRole::Whitespace,
351 Self::Newline => UniversalTokenRole::Whitespace,
352 Self::LineComment => UniversalTokenRole::Comment,
353 Self::BlockComment => UniversalTokenRole::Comment,
354 Self::Eof => UniversalTokenRole::Eof,
355 Self::Error => UniversalTokenRole::Error,
356 _ => UniversalTokenRole::None,
357 }
358 }
359}