Skip to main content

oak_typescript/parser/
element_type.rs

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