Skip to main content

oak_javascript/parser/
element_type.rs

1//! JavaScript element types.
2
3use oak_core::{ElementType, UniversalElementRole};
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7/// JavaScript element types.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[repr(u8)]
11pub enum JavaScriptElementType {
12    /// Root node
13    Root,
14    /// Statement
15    Statement,
16    /// Expression
17    Expression,
18    /// Block
19    Block,
20    /// Error node
21    Error,
22
23    /// Function declaration
24    FunctionDeclaration,
25    /// Variable declaration
26    VariableDeclaration,
27    /// If statement
28    IfStatement,
29    /// While statement
30    WhileStatement,
31    /// For statement
32    ForStatement,
33    /// Return statement
34    ReturnStatement,
35    /// Block statement
36    BlockStatement,
37
38    /// Identifier
39    Identifier,
40    /// Literal
41    Literal,
42
43    /// Call expression
44    CallExpression,
45    /// Member expression
46    MemberExpression,
47    /// Assignment expression
48    AssignmentExpression,
49    /// Logical expression
50    LogicalExpression,
51    /// Binary expression
52    BinaryExpression,
53
54    // Keywords
55    /// `abstract`
56    Abstract,
57    /// `as`
58    As,
59    /// `async`
60    Async,
61    /// `await`
62    Await,
63    /// `break`
64    Break,
65    /// `case`
66    Case,
67    /// `catch`
68    Catch,
69    /// `class`
70    Class,
71    /// `const`
72    Const,
73    /// `continue`
74    Continue,
75    /// `debugger`
76    Debugger,
77    /// `default`
78    Default,
79    /// `delete`
80    Delete,
81    /// `do`
82    Do,
83    /// `else`
84    Else,
85    /// `enum`
86    Enum,
87    /// `export`
88    Export,
89    /// `extends`
90    Extends,
91    /// `false`
92    False,
93    /// `finally`
94    Finally,
95    /// `for`
96    For,
97    /// `function`
98    Function,
99    /// `if`
100    If,
101    /// `implements`
102    Implements,
103    /// `import`
104    Import,
105    /// `in`
106    In,
107    /// `instanceof`
108    Instanceof,
109    /// `interface`
110    Interface,
111    /// `let`
112    Let,
113    /// `new`
114    New,
115    /// `null`
116    Null,
117    /// `package`
118    Package,
119    /// `private`
120    Private,
121    /// `protected`
122    Protected,
123    /// `public`
124    Public,
125    /// `return`
126    Return,
127    /// `static`
128    Static,
129    /// `super`
130    Super,
131    /// `switch`
132    Switch,
133    /// `this`
134    This,
135    /// `throw`
136    Throw,
137    /// `true`
138    True,
139    /// `try`
140    Try,
141    /// `typeof`
142    Typeof,
143    /// `undefined`
144    Undefined,
145    /// `var`
146    Var,
147    /// `void`
148    Void,
149    /// `while`
150    While,
151    /// `with`
152    With,
153    /// `yield`
154    Yield,
155}
156
157impl ElementType for JavaScriptElementType {
158    type Role = UniversalElementRole;
159
160    fn role(&self) -> Self::Role {
161        use UniversalElementRole::*;
162        match self {
163            Self::Root => Root,
164            Self::Statement | Self::Block | Self::BlockStatement | Self::IfStatement | Self::WhileStatement | Self::ForStatement | Self::ReturnStatement | Self::FunctionDeclaration | Self::VariableDeclaration => Statement,
165            Self::Expression | Self::AssignmentExpression | Self::LogicalExpression | Self::BinaryExpression | Self::CallExpression | Self::MemberExpression | Self::Identifier | Self::Literal => Expression,
166            Self::Error => Error,
167            _ => None,
168        }
169    }
170}
171
172impl JavaScriptElementType {
173    /// Returns true if the element type is a keyword.
174    pub fn is_keyword(&self) -> bool {
175        matches!(
176            self,
177            Self::Abstract
178                | Self::As
179                | Self::Async
180                | Self::Await
181                | Self::Break
182                | Self::Case
183                | Self::Catch
184                | Self::Class
185                | Self::Const
186                | Self::Continue
187                | Self::Debugger
188                | Self::Default
189                | Self::Delete
190                | Self::Do
191                | Self::Else
192                | Self::Enum
193                | Self::Export
194                | Self::Extends
195                | Self::False
196                | Self::Finally
197                | Self::For
198                | Self::Function
199                | Self::If
200                | Self::Implements
201                | Self::Import
202                | Self::In
203                | Self::Instanceof
204                | Self::Interface
205                | Self::Let
206                | Self::New
207                | Self::Null
208                | Self::Package
209                | Self::Private
210                | Self::Protected
211                | Self::Public
212                | Self::Return
213                | Self::Static
214                | Self::Super
215                | Self::Switch
216                | Self::This
217                | Self::Throw
218                | Self::True
219                | Self::Try
220                | Self::Typeof
221                | Self::Undefined
222                | Self::Var
223                | Self::Void
224                | Self::While
225                | Self::With
226                | Self::Yield
227        )
228    }
229
230    /// Returns the element type for the given keyword string.
231    pub fn from_keyword(s: &str) -> Option<Self> {
232        match s {
233            "abstract" => Some(Self::Abstract),
234            "as" => Some(Self::As),
235            "async" => Some(Self::Async),
236            "await" => Some(Self::Await),
237            "break" => Some(Self::Break),
238            "case" => Some(Self::Case),
239            "catch" => Some(Self::Catch),
240            "class" => Some(Self::Class),
241            "const" => Some(Self::Const),
242            "continue" => Some(Self::Continue),
243            "debugger" => Some(Self::Debugger),
244            "default" => Some(Self::Default),
245            "delete" => Some(Self::Delete),
246            "do" => Some(Self::Do),
247            "else" => Some(Self::Else),
248            "enum" => Some(Self::Enum),
249            "export" => Some(Self::Export),
250            "extends" => Some(Self::Extends),
251            "false" => Some(Self::False),
252            "finally" => Some(Self::Finally),
253            "for" => Some(Self::For),
254            "function" => Some(Self::Function),
255            "if" => Some(Self::If),
256            "implements" => Some(Self::Implements),
257            "import" => Some(Self::Import),
258            "in" => Some(Self::In),
259            "instanceof" => Some(Self::Instanceof),
260            "interface" => Some(Self::Interface),
261            "let" => Some(Self::Let),
262            "new" => Some(Self::New),
263            "null" => Some(Self::Null),
264            "package" => Some(Self::Package),
265            "private" => Some(Self::Private),
266            "protected" => Some(Self::Protected),
267            "public" => Some(Self::Public),
268            "return" => Some(Self::Return),
269            "static" => Some(Self::Static),
270            "super" => Some(Self::Super),
271            "switch" => Some(Self::Switch),
272            "this" => Some(Self::This),
273            "throw" => Some(Self::Throw),
274            "true" => Some(Self::True),
275            "try" => Some(Self::Try),
276            "typeof" => Some(Self::Typeof),
277            "undefined" => Some(Self::Undefined),
278            "var" => Some(Self::Var),
279            "void" => Some(Self::Void),
280            "while" => Some(Self::While),
281            "with" => Some(Self::With),
282            "yield" => Some(Self::Yield),
283            _ => None,
284        }
285    }
286}
287
288impl From<crate::lexer::token_type::JavaScriptTokenType> for JavaScriptElementType {
289    fn from(token: crate::lexer::token_type::JavaScriptTokenType) -> Self {
290        unsafe { std::mem::transmute(token) }
291    }
292}