Skip to main content

oak_typescript/parser/
element_type.rs

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