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