Skip to main content

oak_ejs/parser/
element_type.rs

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