1use oak_core::{Token, TokenType, UniversalTokenRole};
4
5pub type JavaScriptToken = Token<JavaScriptTokenType>;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[repr(u8)]
12pub enum JavaScriptTokenType {
13 Abstract,
16 As,
18 Async,
20 Await,
22 Break,
24 Case,
26 Catch,
28 Class,
30 Const,
32 Continue,
34 Debugger,
36 Default,
38 Delete,
40 Do,
42 Else,
44 Enum,
46 Export,
48 Extends,
50 False,
52 Finally,
54 For,
56 Function,
58 If,
60 Implements,
62 Import,
64 In,
66 Instanceof,
68 Interface,
70 Let,
72 New,
74 Null,
76 Package,
78 Private,
80 Protected,
82 Public,
84 Return,
86 Static,
88 Super,
90 Switch,
92 This,
94 Throw,
96 True,
98 Try,
100 Typeof,
102 Undefined,
104 Var,
106 Void,
108 While,
110 With,
112 Yield,
114
115 Plus,
118 Minus,
120 Star,
122 Slash,
124 Percent,
126 StarStar,
128 PlusPlus,
130 MinusMinus,
132 LeftShift,
134 RightShift,
136 UnsignedRightShift,
138 Less,
140 Greater,
142 LessEqual,
144 GreaterEqual,
146 EqualEqual,
148 NotEqual,
150 EqualEqualEqual,
152 NotEqualEqual,
154 Ampersand,
156 Pipe,
158 Caret,
160 Exclamation,
162 Tilde,
164 AmpersandAmpersand,
166 PipePipe,
168 Question,
170 QuestionQuestion,
172 QuestionDot,
174
175 Equal,
178 PlusEqual,
180 MinusEqual,
182 StarEqual,
184 SlashEqual,
186 PercentEqual,
188 StarStarEqual,
190 LeftShiftEqual,
192 RightShiftEqual,
194 UnsignedRightShiftEqual,
196 AmpersandEqual,
198 PipeEqual,
200 CaretEqual,
202 AmpersandAmpersandEqual,
204 PipePipeEqual,
206 QuestionQuestionEqual,
208
209 LeftParen,
212 RightParen,
214 LeftBrace,
216 RightBrace,
218 LeftBracket,
220 RightBracket,
222 Semicolon,
224 Comma,
226 Dot,
228 DotDotDot,
230 Colon,
232 Arrow,
234
235 StringLiteral,
238 NumericLiteral,
240 BigIntLiteral,
242 RegexLiteral,
244 TemplateString,
246 TemplateHead,
248 TemplateMiddle,
250 TemplateTail,
252
253 IdentifierName,
256
257 LineComment,
260 BlockComment,
262 Whitespace,
264 Newline,
266
267 Eof,
270 Error,
272}
273
274impl JavaScriptTokenType {
275 pub fn is_keyword(&self) -> bool {
277 matches!(
278 self,
279 Self::Abstract
280 | Self::As
281 | Self::Async
282 | Self::Await
283 | Self::Break
284 | Self::Case
285 | Self::Catch
286 | Self::Class
287 | Self::Const
288 | Self::Continue
289 | Self::Debugger
290 | Self::Default
291 | Self::Delete
292 | Self::Do
293 | Self::Else
294 | Self::Enum
295 | Self::Export
296 | Self::Extends
297 | Self::False
298 | Self::Finally
299 | Self::For
300 | Self::Function
301 | Self::If
302 | Self::Implements
303 | Self::Import
304 | Self::In
305 | Self::Instanceof
306 | Self::Interface
307 | Self::Let
308 | Self::New
309 | Self::Null
310 | Self::Package
311 | Self::Private
312 | Self::Protected
313 | Self::Public
314 | Self::Return
315 | Self::Static
316 | Self::Super
317 | Self::Switch
318 | Self::This
319 | Self::Throw
320 | Self::True
321 | Self::Try
322 | Self::Typeof
323 | Self::Undefined
324 | Self::Var
325 | Self::Void
326 | Self::While
327 | Self::With
328 | Self::Yield
329 )
330 }
331
332 pub fn from_keyword(s: &str) -> Option<Self> {
334 match s {
335 "abstract" => Some(Self::Abstract),
336 "as" => Some(Self::As),
337 "async" => Some(Self::Async),
338 "await" => Some(Self::Await),
339 "break" => Some(Self::Break),
340 "case" => Some(Self::Case),
341 "catch" => Some(Self::Catch),
342 "class" => Some(Self::Class),
343 "const" => Some(Self::Const),
344 "continue" => Some(Self::Continue),
345 "debugger" => Some(Self::Debugger),
346 "default" => Some(Self::Default),
347 "delete" => Some(Self::Delete),
348 "do" => Some(Self::Do),
349 "else" => Some(Self::Else),
350 "enum" => Some(Self::Enum),
351 "export" => Some(Self::Export),
352 "extends" => Some(Self::Extends),
353 "false" => Some(Self::False),
354 "finally" => Some(Self::Finally),
355 "for" => Some(Self::For),
356 "function" => Some(Self::Function),
357 "if" => Some(Self::If),
358 "implements" => Some(Self::Implements),
359 "import" => Some(Self::Import),
360 "in" => Some(Self::In),
361 "instanceof" => Some(Self::Instanceof),
362 "interface" => Some(Self::Interface),
363 "let" => Some(Self::Let),
364 "new" => Some(Self::New),
365 "null" => Some(Self::Null),
366 "package" => Some(Self::Package),
367 "private" => Some(Self::Private),
368 "protected" => Some(Self::Protected),
369 "public" => Some(Self::Public),
370 "return" => Some(Self::Return),
371 "static" => Some(Self::Static),
372 "super" => Some(Self::Super),
373 "switch" => Some(Self::Switch),
374 "this" => Some(Self::This),
375 "throw" => Some(Self::Throw),
376 "true" => Some(Self::True),
377 "try" => Some(Self::Try),
378 "typeof" => Some(Self::Typeof),
379 "undefined" => Some(Self::Undefined),
380 "var" => Some(Self::Var),
381 "void" => Some(Self::Void),
382 "while" => Some(Self::While),
383 "with" => Some(Self::With),
384 "yield" => Some(Self::Yield),
385 _ => None,
386 }
387 }
388}
389
390impl JavaScriptTokenType {
391 pub fn is_trivia(&self) -> bool {
393 matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
394 }
395}
396
397impl TokenType for JavaScriptTokenType {
398 type Role = UniversalTokenRole;
399 const END_OF_STREAM: Self = Self::Error;
400
401 fn is_ignored(&self) -> bool {
402 false
403 }
404
405 fn role(&self) -> Self::Role {
406 match self {
407 _ => UniversalTokenRole::None,
408 }
409 }
410}