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