Skip to main content

oak_typescript/lexer/
token_type.rs

1use oak_core::{Token, TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5/// Alias for `Token<TypeScriptTokenType>`.
6pub type TypeScriptToken = Token<TypeScriptTokenType>;
7
8/// Token types for TypeScript.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[repr(u16)]
12pub enum TypeScriptTokenType {
13    /// Named imports.
14    NamedImports,
15    /// A decorator.
16    Decorator,
17    /// Arrow function `=>`.
18    ArrowFunction,
19    /// Predefined type.
20    PredefinedType,
21    /// `abstract` keyword.
22    Abstract,
23    /// `any` keyword.
24    Any,
25    /// `as` keyword.
26    As,
27    /// `asserts` keyword.
28    Asserts,
29    /// `async` keyword.
30    Async,
31    /// `await` keyword.
32    Await,
33    /// `boolean` keyword.
34    Boolean,
35    /// `break` keyword.
36    Break,
37    /// `case` keyword.
38    Case,
39    /// `catch` keyword.
40    Catch,
41    /// `class` keyword.
42    Class,
43    /// `const` keyword.
44    Const,
45    /// `constructor` keyword.
46    Constructor,
47    /// `continue` keyword.
48    Continue,
49    /// `debugger` keyword.
50    Debugger,
51    /// `declare` keyword.
52    Declare,
53    /// `default` keyword.
54    Default,
55    /// `delete` keyword.
56    Delete,
57    /// `do` keyword.
58    Do,
59    /// `else` keyword.
60    Else,
61    /// `enum` keyword.
62    Enum,
63    /// `export` keyword.
64    Export,
65    /// `extends` keyword.
66    Extends,
67    /// `false` keyword.
68    False,
69    /// `finally` keyword.
70    Finally,
71    /// `for` keyword.
72    For,
73    /// `from` keyword.
74    From,
75    /// `function` keyword.
76    Function,
77    /// `get` keyword.
78    Get,
79    /// `global` keyword.
80    Global,
81    /// `if` keyword.
82    If,
83    /// `implements` keyword.
84    Implements,
85    /// `import` keyword.
86    Import,
87    /// `in` keyword.
88    In,
89    /// `infer` keyword.
90    Infer,
91    /// `instanceof` keyword.
92    Instanceof,
93    /// `interface` keyword.
94    Interface,
95    /// `is` keyword.
96    Is,
97    /// `keyof` keyword.
98    Keyof,
99    /// `let` keyword.
100    Let,
101    /// `namespace` keyword.
102    Namespace,
103    /// `never` keyword.
104    Never,
105    /// `new` keyword.
106    New,
107    /// `null` keyword.
108    Null,
109    /// `number` keyword.
110    Number,
111    /// `object` keyword.
112    Object,
113    /// `of` keyword.
114    Of,
115    /// `override` keyword.
116    Override,
117    /// `package` keyword.
118    Package,
119    /// `private` keyword.
120    Private,
121    /// `protected` keyword.
122    Protected,
123    /// `public` keyword.
124    Public,
125    /// `readonly` keyword.
126    Readonly,
127    /// `require` keyword.
128    Require,
129    /// `return` keyword.
130    Return,
131    /// `set` keyword.
132    Set,
133    /// `static` keyword.
134    Static,
135    /// `string` keyword.
136    String,
137    /// `super` keyword.
138    Super,
139    /// `switch` keyword.
140    Switch,
141    /// `symbol` keyword.
142    Symbol,
143    /// `this` keyword.
144    This,
145    /// `throw` keyword.
146    Throw,
147    /// `true` keyword.
148    True,
149    /// `try` keyword.
150    Try,
151    /// `type` keyword.
152    Type,
153    /// `typeof` keyword.
154    Typeof,
155    /// `undefined` keyword.
156    Undefined,
157    /// `unique` keyword.
158    Unique,
159    /// `unknown` keyword.
160    Unknown,
161    /// `var` keyword.
162    Var,
163    /// `void` keyword.
164    Void,
165    /// `while` keyword.
166    While,
167    /// `with` keyword.
168    With,
169    /// `yield` keyword.
170    Yield,
171    /// Plus `+`.
172    Plus,
173    /// Minus `-`.
174    Minus,
175    /// Star `*`.
176    Star,
177    /// Slash `/`.
178    Slash,
179    /// Percent `%`.
180    Percent,
181    /// StarStar `**`.
182    StarStar,
183    /// Question `?`.
184    Question,
185    /// DotDotDot `...`.
186    DotDotDot,
187    /// Less `<`.
188    Less,
189    /// Greater `>`.
190    Greater,
191    /// LessEqual `<=`.
192    LessEqual,
193    /// Greater than or equal `>=`.
194    GreaterEqual,
195    /// Equal `==`.
196    EqualEqual,
197    /// Not equal `!=`.
198    NotEqual,
199    /// Strict equal `===`.
200    EqualEqualEqual,
201    /// Strict not equal `!==`.
202    NotEqualEqual,
203    /// Logical AND `&&`.
204    AmpersandAmpersand,
205    /// Logical OR `||`.
206    PipePipe,
207    /// Logical NOT `!`.
208    Exclamation,
209    /// Bitwise AND `&`.
210    Ampersand,
211    /// Bitwise OR `|`.
212    Pipe,
213    /// Bitwise XOR `^`.
214    Caret,
215    /// Bitwise NOT `~`.
216    Tilde,
217    /// Left shift `<<`.
218    LeftShift,
219    /// Right shift `>>`.
220    RightShift,
221    /// Unsigned right shift `>>>`.
222    UnsignedRightShift,
223    /// Assignment `=`.
224    Equal,
225    /// Addition assignment `+=`.
226    PlusEqual,
227    /// Subtraction assignment `-=`.
228    MinusEqual,
229    /// Multiplication assignment `*=`.
230    StarEqual,
231    /// Division assignment `/=`.
232    SlashEqual,
233    /// Remainder assignment `%=`.
234    PercentEqual,
235    /// Exponentiation assignment `**=`.
236    StarStarEqual,
237    /// Left shift assignment `<<=`.
238    LeftShiftEqual,
239    /// Right shift assignment `>>=`.
240    RightShiftEqual,
241    /// Unsigned right shift assignment `>>>=`.
242    UnsignedRightShiftEqual,
243    /// Bitwise AND assignment `&=`.
244    AmpersandEqual,
245    /// Bitwise OR assignment `|=`.
246    PipeEqual,
247    /// Bitwise XOR assignment `^=`.
248    CaretEqual,
249    /// Logical AND assignment `&&=`.
250    AmpersandAmpersandEqual,
251    /// Logical OR assignment `||=`.
252    PipePipeEqual,
253    /// Nullish coalescing assignment `??=`.
254    QuestionQuestionEqual,
255    /// Increment `++`.
256    PlusPlus,
257    /// Decrement `--`.
258    MinusMinus,
259    /// Nullish coalescing `??`.
260    QuestionQuestion,
261    /// Optional chaining `?.`.
262    QuestionDot,
263    /// Arrow `=>`.
264    Arrow,
265    /// Left parenthesis `(`.
266    LeftParen,
267    /// Right parenthesis `)`.
268    RightParen,
269    /// Left brace `{`.
270    LeftBrace,
271    /// Right brace `}`.
272    RightBrace,
273    /// Left bracket `[`.
274    LeftBracket,
275    /// Right bracket `]`.
276    RightBracket,
277    /// Semicolon `;`.
278    Semicolon,
279    /// Comma `,`.
280    Comma,
281    /// Dot `.`.
282    Dot,
283    /// Colon `:`.
284    Colon,
285    /// At `@`.
286    At,
287    /// String literal.
288    StringLiteral,
289    /// Numeric literal.
290    NumericLiteral,
291    /// BigInt literal.
292    BigIntLiteral,
293    /// Boolean literal.
294    BooleanLiteral,
295    /// Template string.
296    TemplateString,
297    /// Regular expression literal.
298    RegexLiteral,
299    /// Identifier name.
300    IdentifierName,
301    /// Line comment `//`.
302    LineComment,
303    /// Block comment `/* */`.
304    BlockComment,
305    /// Whitespace.
306    Whitespace,
307    /// Newline.
308    Newline,
309    /// End of stream.
310    Eof,
311    /// Root node.
312    Root,
313    /// Source file.
314    SourceFile,
315    /// Module.
316    Module,
317    /// Variable declaration.
318    VariableDeclaration,
319    /// Function declaration.
320    FunctionDeclaration,
321    /// Class declaration.
322    ClassDeclaration,
323    /// Interface declaration.
324    InterfaceDeclaration,
325    /// Type alias declaration.
326    TypeAliasDeclaration,
327    /// Enum declaration.
328    EnumDeclaration,
329    /// Namespace declaration.
330    NamespaceDeclaration,
331    /// Class body.
332    ClassBody,
333    /// Import declaration.
334    ImportDeclaration,
335    /// Export declaration.
336    ExportDeclaration,
337    /// Import clause.
338    ImportClause,
339    /// Import specifier.
340    ImportSpecifier,
341    /// Parameter.
342    Parameter,
343    /// Call argument.
344    CallArgument,
345    /// Property declaration.
346    PropertyDeclaration,
347    /// Method declaration.
348    MethodDeclaration,
349    /// Constructor declaration.
350    ConstructorDeclaration,
351    /// Property assignment.
352    PropertyAssignment,
353    /// Shorthand property assignment.
354    ShorthandPropertyAssignment,
355    /// Spread element.
356    SpreadElement,
357    /// Error token.
358    Error,
359    /// JSX element.
360    JsxElement,
361    /// JSX self-closing element.
362    JsxSelfClosingElement,
363    /// JSX opening element.
364    JsxOpeningElement,
365    /// JSX closing element.
366    JsxClosingElement,
367    /// JSX fragment.
368    JsxFragment,
369    /// JSX opening fragment.
370    JsxOpeningFragment,
371    /// JSX closing fragment.
372    JsxClosingFragment,
373    /// JSX attribute.
374    JsxAttribute,
375    /// JSX attributes.
376    JsxAttributes,
377    /// JSX expression container.
378    JsxExpressionContainer,
379    /// JSX spread attribute.
380    JsxSpreadAttribute,
381    /// JSX text.
382    JsxText,
383    /// Binary expression.
384    BinaryExpression,
385    /// Unary expression.
386    UnaryExpression,
387    /// Conditional expression `a ? b : c`.
388    ConditionalExpression,
389    /// Call expression `f()`.
390    CallExpression,
391    /// New expression `new C()`.
392    NewExpression,
393    /// Member expression `a.b` or `a[b]`.
394    MemberExpression,
395    /// Array expression `[a, b]`.
396    ArrayExpression,
397    /// Object expression `{a: b}`.
398    ObjectExpression,
399    /// Function expression `function() {}`.
400    FunctionExpression,
401    /// Template expression `` `...` ``.
402    TemplateExpression,
403    /// Tagged template expression `f` `...` ``.
404    TaggedTemplateExpression,
405    /// As expression `a as T`.
406    AsExpression,
407    /// Type assertion expression `<T>a`.
408    TypeAssertionExpression,
409    /// Non-null expression `a!`.
410    NonNullExpression,
411    /// Update expression `++a` or `a--`.
412    UpdateExpression,
413    /// Expression statement.
414    ExpressionStatement,
415    /// Block statement.
416    BlockStatement,
417    /// If statement.
418    IfStatement,
419    /// While statement.
420    WhileStatement,
421    /// For statement.
422    ForStatement,
423    /// For-in statement.
424    ForInStatement,
425    /// For-of statement.
426    ForOfStatement,
427    /// Do-while statement.
428    DoWhileStatement,
429    /// Switch statement.
430    SwitchStatement,
431    /// Case clause.
432    CaseClause,
433    /// Default clause.
434    DefaultClause,
435    /// Try statement.
436    TryStatement,
437    /// Catch clause.
438    CatchClause,
439    /// Finally clause.
440    FinallyClause,
441    /// Throw statement.
442    ThrowStatement,
443    /// Return statement.
444    ReturnStatement,
445    /// Break statement.
446    BreakStatement,
447    /// Continue statement.
448    ContinueStatement,
449    /// Debugger statement.
450    DebuggerStatement,
451    /// With statement.
452    WithStatement,
453    /// Binding pattern.
454    BindingPattern,
455    /// Array binding pattern.
456    ArrayBindingPattern,
457    /// Object binding pattern.
458    ObjectBindingPattern,
459    /// Binding element.
460    BindingElement,
461    /// Type reference.
462    TypeReference,
463    /// Type literal.
464    TypeLiteral,
465    /// Function type.
466    FunctionType,
467    /// Constructor type.
468    ConstructorType,
469    /// Array type.
470    ArrayType,
471    /// Tuple type.
472    TupleType,
473    /// Union type.
474    UnionType,
475    /// Intersection type.
476    IntersectionType,
477    /// Conditional type.
478    ConditionalType,
479    /// Mapped type.
480    MappedType,
481    /// Indexed access type.
482    IndexedAccessType,
483    /// Property signature.
484    PropertySignature,
485    /// Method signature.
486    MethodSignature,
487    /// Literal type.
488    LiteralType,
489    /// Type query.
490    TypeQuery,
491    /// Type predicate.
492    TypePredicate,
493    /// Type annotation.
494    TypeAnnotation,
495    /// Type parameter.
496    TypeParameter,
497    /// Heritage clause.
498    HeritageClause,
499    /// Enum member.
500    EnumMember,
501}
502
503impl TypeScriptTokenType {
504    /// Returns the keyword token type for the given text, if any.
505    pub fn from_keyword(text: &str) -> Option<Self> {
506        match text {
507            "abstract" => Some(Self::Abstract),
508            "any" => Some(Self::Any),
509            "as" => Some(Self::As),
510            "asserts" => Some(Self::Asserts),
511            "async" => Some(Self::Async),
512            "await" => Some(Self::Await),
513            "boolean" => Some(Self::Boolean),
514            "break" => Some(Self::Break),
515            "case" => Some(Self::Case),
516            "catch" => Some(Self::Catch),
517            "class" => Some(Self::Class),
518            "const" => Some(Self::Const),
519            "constructor" => Some(Self::Constructor),
520            "continue" => Some(Self::Continue),
521            "debugger" => Some(Self::Debugger),
522            "declare" => Some(Self::Declare),
523            "default" => Some(Self::Default),
524            "delete" => Some(Self::Delete),
525            "do" => Some(Self::Do),
526            "else" => Some(Self::Else),
527            "enum" => Some(Self::Enum),
528            "export" => Some(Self::Export),
529            "extends" => Some(Self::Extends),
530            "false" => Some(Self::False),
531            "finally" => Some(Self::Finally),
532            "for" => Some(Self::For),
533            "from" => Some(Self::From),
534            "function" => Some(Self::Function),
535            "get" => Some(Self::Get),
536            "global" => Some(Self::Global),
537            "if" => Some(Self::If),
538            "implements" => Some(Self::Implements),
539            "import" => Some(Self::Import),
540            "in" => Some(Self::In),
541            "infer" => Some(Self::Infer),
542            "instanceof" => Some(Self::Instanceof),
543            "interface" => Some(Self::Interface),
544            "is" => Some(Self::Is),
545            "keyof" => Some(Self::Keyof),
546            "let" => Some(Self::Let),
547            "namespace" => Some(Self::Namespace),
548            "never" => Some(Self::Never),
549            "new" => Some(Self::New),
550            "null" => Some(Self::Null),
551            "number" => Some(Self::Number),
552            "object" => Some(Self::Object),
553            "of" => Some(Self::Of),
554            "override" => Some(Self::Override),
555            "package" => Some(Self::Package),
556            "private" => Some(Self::Private),
557            "protected" => Some(Self::Protected),
558            "public" => Some(Self::Public),
559            "readonly" => Some(Self::Readonly),
560            "require" => Some(Self::Require),
561            "return" => Some(Self::Return),
562            "set" => Some(Self::Set),
563            "static" => Some(Self::Static),
564            "string" => Some(Self::String),
565            "super" => Some(Self::Super),
566            "switch" => Some(Self::Switch),
567            "symbol" => Some(Self::Symbol),
568            "this" => Some(Self::This),
569            "throw" => Some(Self::Throw),
570            "true" => Some(Self::True),
571            "try" => Some(Self::Try),
572            "type" => Some(Self::Type),
573            "typeof" => Some(Self::Typeof),
574            "undefined" => Some(Self::Undefined),
575            "unique" => Some(Self::Unique),
576            "unknown" => Some(Self::Unknown),
577            "var" => Some(Self::Var),
578            "void" => Some(Self::Void),
579            "while" => Some(Self::While),
580            "with" => Some(Self::With),
581            "yield" => Some(Self::Yield),
582            _ => None,
583        }
584    }
585}
586
587impl TokenType for TypeScriptTokenType {
588    type Role = UniversalTokenRole;
589    const END_OF_STREAM: Self = Self::Eof;
590
591    fn is_ignored(&self) -> bool {
592        matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
593    }
594
595    fn role(&self) -> Self::Role {
596        match self {
597            Self::Whitespace => UniversalTokenRole::Whitespace,
598            Self::Newline => UniversalTokenRole::Whitespace,
599            Self::LineComment => UniversalTokenRole::Comment,
600            Self::BlockComment => UniversalTokenRole::Comment,
601            Self::Eof => UniversalTokenRole::Eof,
602            Self::Error => UniversalTokenRole::Error,
603            _ => UniversalTokenRole::None,
604        }
605    }
606}