1use oak_core::{ElementType, UniversalElementRole};
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[repr(u8)]
11pub enum JavaScriptElementType {
12 Root,
14 Statement,
16 Expression,
18 Block,
20 Error,
22
23 FunctionDeclaration,
25 VariableDeclaration,
27 IfStatement,
29 WhileStatement,
31 ForStatement,
33 ReturnStatement,
35 BlockStatement,
37
38 Identifier,
40 Literal,
42
43 CallExpression,
45 MemberExpression,
47 AssignmentExpression,
49 LogicalExpression,
51 BinaryExpression,
53
54 Abstract,
57 As,
59 Async,
61 Await,
63 Break,
65 Case,
67 Catch,
69 Class,
71 Const,
73 Continue,
75 Debugger,
77 Default,
79 Delete,
81 Do,
83 Else,
85 Enum,
87 Export,
89 Extends,
91 False,
93 Finally,
95 For,
97 Function,
99 If,
101 Implements,
103 Import,
105 In,
107 Instanceof,
109 Interface,
111 Let,
113 New,
115 Null,
117 Package,
119 Private,
121 Protected,
123 Public,
125 Return,
127 Static,
129 Super,
131 Switch,
133 This,
135 Throw,
137 True,
139 Try,
141 Typeof,
143 Undefined,
145 Var,
147 Void,
149 While,
151 With,
153 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 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 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}