1use crate::ast::{ClassMember, Decorator, EnumMember, Expression, TypeAnnotation, TypeParameter};
2use core::range::Range;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub enum Statement {
10 VariableDeclaration(VariableDeclaration),
12 FunctionDeclaration(FunctionDeclaration),
14 ClassDeclaration(ClassDeclaration),
16 ExpressionStatement(ExpressionStatement),
18 ImportDeclaration(ImportDeclaration),
20 ExportDeclaration(ExportDeclaration),
22 Interface(InterfaceDeclaration),
24 TypeAlias(TypeAliasDeclaration),
26 Enum(EnumDeclaration),
28 ReturnStatement(ReturnStatement),
30 IfStatement(IfStatement),
32 WhileStatement(WhileStatement),
34 DoWhileStatement(DoWhileStatement),
36 ForStatement(ForStatement),
38 ForInStatement(ForInStatement),
40 ForOfStatement(ForOfStatement),
42 SwitchStatement(SwitchStatement),
44 TryStatement(TryStatement),
46 ThrowStatement(ThrowStatement),
48 BreakStatement(BreakStatement),
50 ContinueStatement(ContinueStatement),
52 BlockStatement(BlockStatement),
54 Namespace(NamespaceDeclaration),
56}
57
58impl Statement {
59 pub fn span(&self) -> Range<usize> {
61 match self {
62 Statement::VariableDeclaration(d) => d.span.clone(),
63 Statement::FunctionDeclaration(d) => d.span.clone(),
64 Statement::ClassDeclaration(d) => d.span.clone(),
65 Statement::ExpressionStatement(s) => s.span.clone(),
66 Statement::ImportDeclaration(d) => d.span.clone(),
67 Statement::ExportDeclaration(d) => d.span.clone(),
68 Statement::Interface(d) => d.span.clone(),
69 Statement::TypeAlias(d) => d.span.clone(),
70 Statement::Enum(d) => d.span.clone(),
71 Statement::ReturnStatement(s) => s.span.clone(),
72 Statement::IfStatement(s) => s.span.clone(),
73 Statement::WhileStatement(s) => s.span.clone(),
74 Statement::DoWhileStatement(s) => s.span.clone(),
75 Statement::ForStatement(s) => s.span.clone(),
76 Statement::ForInStatement(s) => s.span.clone(),
77 Statement::ForOfStatement(s) => s.span.clone(),
78 Statement::SwitchStatement(s) => s.span.clone(),
79 Statement::TryStatement(s) => s.span.clone(),
80 Statement::ThrowStatement(s) => s.span.clone(),
81 Statement::BreakStatement(s) => s.span.clone(),
82 Statement::ContinueStatement(s) => s.span.clone(),
83 Statement::BlockStatement(s) => s.span.clone(),
84 Statement::Namespace(s) => s.span.clone(),
85 }
86 }
87}
88
89#[derive(Debug, Clone)]
91#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
92pub struct ExpressionStatement {
93 pub decorators: Vec<Decorator>,
95 pub is_declare: bool,
97 pub expression: Expression,
99 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
101 pub span: Range<usize>,
102}
103
104#[derive(Debug, Clone)]
106#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
107pub struct ReturnStatement {
108 pub decorators: Vec<Decorator>,
110 pub is_declare: bool,
112 pub argument: Option<Expression>,
114 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
116 pub span: Range<usize>,
117}
118
119#[derive(Debug, Clone)]
121#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
122pub struct ThrowStatement {
123 pub decorators: Vec<Decorator>,
125 pub is_declare: bool,
127 pub argument: Expression,
129 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
131 pub span: Range<usize>,
132}
133
134#[derive(Debug, Clone)]
136#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
137pub struct BreakStatement {
138 pub decorators: Vec<Decorator>,
140 pub is_declare: bool,
142 pub label: Option<String>,
144 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
146 pub span: Range<usize>,
147}
148
149#[derive(Debug, Clone)]
151#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
152pub struct ContinueStatement {
153 pub decorators: Vec<Decorator>,
155 pub is_declare: bool,
157 pub label: Option<String>,
159 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
161 pub span: Range<usize>,
162}
163
164#[derive(Debug, Clone)]
166#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
167pub struct NamespaceDeclaration {
168 pub decorators: Vec<Decorator>,
170 pub is_declare: bool,
172 pub name: String,
174 pub body: Vec<Statement>,
176 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
178 pub span: Range<usize>,
179}
180
181#[derive(Debug, Clone)]
183#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
184pub struct InterfaceDeclaration {
185 pub decorators: Vec<Decorator>,
187 pub is_declare: bool,
189 pub name: String,
191 pub type_params: Vec<TypeParameter>,
193 pub extends: Vec<TypeAnnotation>,
195 pub body: Vec<ClassMember>,
197 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
199 pub span: Range<usize>,
200}
201
202#[derive(Debug, Clone)]
204#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
205pub struct TypeAliasDeclaration {
206 pub decorators: Vec<Decorator>,
208 pub is_declare: bool,
210 pub name: String,
212 pub type_params: Vec<TypeParameter>,
214 pub ty: TypeAnnotation,
216 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
218 pub span: Range<usize>,
219}
220
221#[derive(Debug, Clone)]
223#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
224pub struct EnumDeclaration {
225 pub decorators: Vec<Decorator>,
227 pub is_declare: bool,
229 pub name: String,
231 pub members: Vec<EnumMember>,
233 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
235 pub span: Range<usize>,
236}
237
238#[derive(Debug, Clone)]
240#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
241pub struct IfStatement {
242 pub decorators: Vec<Decorator>,
244 pub is_declare: bool,
246 pub test: Expression,
248 pub consequent: Box<Statement>,
250 pub alternate: Option<Box<Statement>>,
252 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
254 pub span: Range<usize>,
255}
256
257#[derive(Debug, Clone)]
259#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
260pub struct WhileStatement {
261 pub decorators: Vec<Decorator>,
263 pub is_declare: bool,
265 pub test: Expression,
267 pub body: Box<Statement>,
269 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
271 pub span: Range<usize>,
272}
273
274#[derive(Debug, Clone)]
276#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
277pub struct DoWhileStatement {
278 pub decorators: Vec<Decorator>,
280 pub is_declare: bool,
282 pub body: Box<Statement>,
284 pub test: Expression,
286 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
288 pub span: Range<usize>,
289}
290
291#[derive(Debug, Clone)]
293#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
294pub struct ForStatement {
295 pub decorators: Vec<Decorator>,
297 pub is_declare: bool,
299 pub initializer: Option<Box<Statement>>,
301 pub test: Option<Expression>,
303 pub incrementor: Option<Expression>,
305 pub body: Box<Statement>,
307 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
309 pub span: Range<usize>,
310}
311
312#[derive(Debug, Clone)]
314#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
315pub struct ForInStatement {
316 pub decorators: Vec<Decorator>,
318 pub is_declare: bool,
320 pub left: Box<Statement>,
322 pub right: Expression,
324 pub body: Box<Statement>,
326 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
328 pub span: Range<usize>,
329}
330
331#[derive(Debug, Clone)]
333#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
334pub struct ForOfStatement {
335 pub decorators: Vec<Decorator>,
337 pub is_declare: bool,
339 pub left: Box<Statement>,
341 pub right: Expression,
343 pub body: Box<Statement>,
345 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
347 pub span: Range<usize>,
348}
349
350#[derive(Debug, Clone)]
352#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
353pub struct SwitchStatement {
354 pub decorators: Vec<Decorator>,
356 pub is_declare: bool,
358 pub discriminant: Expression,
360 pub cases: Vec<SwitchCase>,
362 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
364 pub span: Range<usize>,
365}
366
367#[derive(Debug, Clone)]
369#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
370pub struct SwitchCase {
371 pub test: Option<Expression>,
373 pub consequent: Vec<Statement>,
375 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
377 pub span: Range<usize>,
378}
379
380#[derive(Debug, Clone)]
382#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
383pub struct TryStatement {
384 pub decorators: Vec<Decorator>,
386 pub is_declare: bool,
388 pub block: Vec<Statement>,
390 pub handler: Option<CatchClause>,
392 pub finalizer: Option<Vec<Statement>>,
394 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
396 pub span: Range<usize>,
397}
398
399#[derive(Debug, Clone)]
401#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
402pub struct CatchClause {
403 pub param: Option<String>,
405 pub body: Vec<Statement>,
407 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
409 pub span: Range<usize>,
410}
411
412#[derive(Debug, Clone)]
414#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
415pub struct BlockStatement {
416 pub decorators: Vec<Decorator>,
418 pub is_declare: bool,
420 pub statements: Vec<Statement>,
422 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
424 pub span: Range<usize>,
425}
426
427#[derive(Debug, Clone)]
429#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
430pub enum ImportSpecifier {
431 Default(String),
433 Namespace(String),
435 Named {
437 local: String,
439 imported: String,
441 },
442}
443
444#[derive(Debug, Clone)]
446#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
447pub struct ExportSpecifier {
448 pub local: String,
450 pub exported: String,
452}
453
454#[derive(Debug, Clone)]
456#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
457pub struct ImportDeclaration {
458 pub module_specifier: String,
460 pub specifiers: Vec<ImportSpecifier>,
462 pub is_type_only: bool,
464 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
466 pub span: Range<usize>,
467}
468
469#[derive(Debug, Clone)]
471#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
472pub struct ExportDeclaration {
473 pub declaration: Option<Box<Statement>>,
475 pub specifiers: Vec<ExportSpecifier>,
477 pub source: Option<String>,
479 pub is_default: bool,
481 pub is_type_only: bool,
483 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
485 pub span: Range<usize>,
486}
487
488#[derive(Debug, Clone)]
490#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
491pub struct VariableDeclaration {
492 pub decorators: Vec<Decorator>,
494 pub is_declare: bool,
496 pub name: String,
498 pub ty: Option<TypeAnnotation>,
500 pub value: Option<Expression>,
502 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
504 pub span: Range<usize>,
505}
506
507#[derive(Debug, Clone)]
509#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
510pub struct FunctionDeclaration {
511 pub decorators: Vec<Decorator>,
513 pub is_declare: bool,
515 pub name: String,
517 pub type_params: Vec<TypeParameter>,
519 pub params: Vec<FunctionParam>,
521 pub return_type: Option<TypeAnnotation>,
523 pub body: Vec<Statement>,
525 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
527 pub span: Range<usize>,
528}
529
530#[derive(Debug, Clone)]
532#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
533pub struct ClassDeclaration {
534 pub decorators: Vec<Decorator>,
536 pub is_declare: bool,
538 pub name: String,
540 pub type_params: Vec<TypeParameter>,
542 pub extends: Option<TypeAnnotation>,
544 pub implements: Vec<TypeAnnotation>,
546 pub is_abstract: bool,
548 pub body: Vec<ClassMember>,
550 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
552 pub span: Range<usize>,
553}
554
555#[derive(Debug, Clone)]
557#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
558pub struct FunctionParam {
559 pub decorators: Vec<Decorator>,
561 pub name: String,
563 pub ty: Option<TypeAnnotation>,
565 pub optional: bool,
567 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
569 pub span: Range<usize>,
570}