wgsl-parse 0.3.2

Parse a wgsl source file to a syntax tree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! A syntax tree for WGSL and WESL files. The root of the tree is [`TranslationUnit`].
//!
//! The syntax tree closely mirrors WGSL spec syntax while allowing language extensions.
//!
//! ## Strictness
//!
//! This syntax tree is rather strict, meaning it cannot represent most syntactically
//! incorrect programs. But it is only syntactic, meaning it doesn't perform many
//! contextual checks: for example, certain attributes can only appear in certain places,
//! or declarations have different constraints depending on where they appear.
//!
//! ## WESL Extensions
//!
//! WESL extensions are enabled with the `imports`, `generics`, `attributes` and `condcomp`. Read more about WESL at <https://wesl-lang.dev>.
//!
//! ## Design considerations
//!
//! The parsing is not designed to be primarily efficient, but flexible and correct.
//! It is made with the ultimate goal to implement spec-compliant language extensions.

use std::sync::{Arc, RwLock, RwLockReadGuard};

use derive_more::{From, IsVariant, Unwrap};

pub use crate::span::{Span, Spanned};

pub use wgsl_types::syntax::*;

#[cfg(feature = "tokrepr")]
use tokrepr::TokRepr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Default, Clone, Debug, PartialEq)]
pub struct TranslationUnit {
    #[cfg(feature = "imports")]
    pub imports: Vec<ImportStatement>,
    pub global_directives: Vec<GlobalDirective>,
    pub global_declarations: Vec<GlobalDeclarationNode>,
}

/// Identifiers correspond to WGSL `ident` syntax node, except that they have several
/// convenience features:
/// * Can be shared by cloning (they are shared pointers)
/// * Can be [renamed][Self::rename] (with interior mutability)
/// * References to the same Ident can be [counted][Self::use_count]
/// * Equality and Hash compares the reference, NOT the internal string value
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct Ident(Arc<RwLock<String>>);

impl Ident {
    /// Create a new Ident
    pub fn new(name: String) -> Ident {
        // TODO: check that the name is a valid ident
        Ident(Arc::new(RwLock::new(name)))
    }
    /// Get the name of the Ident
    pub fn name(&self) -> RwLockReadGuard<'_, String> {
        self.0.read().unwrap()
    }
    /// Rename all shared instances of the ident
    pub fn rename(&mut self, name: String) {
        *self.0.write().unwrap() = name;
    }
    /// Count shared instances of the ident
    pub fn use_count(&self) -> usize {
        Arc::<_>::strong_count(&self.0)
    }
}

impl From<String> for Ident {
    fn from(name: String) -> Self {
        Ident::new(name)
    }
}

/// equality for idents is based on address, NOT internal value
impl PartialEq for Ident {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

/// equality for idents is based on address, NOT internal value
impl Eq for Ident {}

/// hash for idents is based on address, NOT internal value
impl std::hash::Hash for Ident {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::ptr::hash(&*self.0, state)
    }
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ImportStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub path: Option<ModulePath>,
    pub content: ImportContent,
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, IsVariant)]
pub enum PathOrigin {
    /// Import relative to the current package root, starting with 'package::'.
    Absolute,
    /// Import relative to the current module, starting with 'super::'. The  usize is the number of 'super'.
    Relative(usize),
    /// Import from a package dependency, starting with the extern package name.
    Package(String),
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ModulePath {
    pub origin: PathOrigin,
    pub components: Vec<String>,
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct Import {
    pub path: Vec<String>,
    pub content: ImportContent,
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, IsVariant)]
pub enum ImportContent {
    Item(ImportItem),
    Collection(Vec<Import>),
}

#[cfg(feature = "imports")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ImportItem {
    pub ident: Ident,
    pub rename: Option<Ident>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum GlobalDirective {
    Diagnostic(DiagnosticDirective),
    Enable(EnableDirective),
    Requires(RequiresDirective),
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct DiagnosticDirective {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub severity: DiagnosticSeverity,
    pub rule_name: String,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct EnableDirective {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub extensions: Vec<String>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct RequiresDirective {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub extensions: Vec<String>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum GlobalDeclaration {
    Void,
    Declaration(Declaration),
    TypeAlias(TypeAlias),
    Struct(Struct),
    Function(Function),
    ConstAssert(ConstAssert),
}

pub type GlobalDeclarationNode = Spanned<GlobalDeclaration>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct Declaration {
    pub attributes: Attributes,
    pub kind: DeclarationKind,
    pub ident: Ident,
    pub ty: Option<TypeExpression>,
    pub initializer: Option<ExpressionNode>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, IsVariant)]
pub enum DeclarationKind {
    Const,
    Override,
    Let,
    Var(Option<(AddressSpace, Option<AccessMode>)>), // "None" corresponds to handle space if it is a module-scope declaration, otherwise function space.
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct TypeAlias {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub ident: Ident,
    pub ty: TypeExpression,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct Struct {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub ident: Ident,
    pub members: Vec<StructMemberNode>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct StructMember {
    pub attributes: Attributes,
    pub ident: Ident,
    pub ty: TypeExpression,
}

pub type StructMemberNode = Spanned<StructMember>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
    pub attributes: Attributes,
    pub ident: Ident,
    pub parameters: Vec<FormalParameter>,
    pub return_attributes: Attributes,
    pub return_type: Option<TypeExpression>,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct FormalParameter {
    pub attributes: Attributes,
    pub ident: Ident,
    pub ty: TypeExpression,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ConstAssert {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct DiagnosticAttribute {
    pub severity: DiagnosticSeverity,
    pub rule: String,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct InterpolateAttribute {
    pub ty: InterpolationType,
    pub sampling: Option<InterpolationSampling>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct WorkgroupSizeAttribute {
    pub x: ExpressionNode,
    pub y: Option<ExpressionNode>,
    pub z: Option<ExpressionNode>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct CustomAttribute {
    pub name: String,
    pub arguments: Option<Vec<ExpressionNode>>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum Attribute {
    Align(ExpressionNode),
    Binding(ExpressionNode),
    BlendSrc(ExpressionNode),
    #[from]
    Builtin(BuiltinValue),
    Const,
    #[from]
    Diagnostic(DiagnosticAttribute),
    Group(ExpressionNode),
    Id(ExpressionNode),
    #[from]
    Interpolate(InterpolateAttribute),
    Invariant,
    Location(ExpressionNode),
    MustUse,
    Size(ExpressionNode),
    #[from]
    WorkgroupSize(WorkgroupSizeAttribute),
    Vertex,
    Fragment,
    Compute,
    #[cfg(feature = "imports")]
    Publish,
    #[cfg(feature = "condcomp")]
    If(ExpressionNode),
    #[cfg(feature = "condcomp")]
    Elif(ExpressionNode),
    #[cfg(feature = "condcomp")]
    Else,
    #[cfg(feature = "generics")]
    #[from]
    Type(TypeConstraint),
    #[cfg(feature = "naga-ext")]
    EarlyDepthTest(Option<ConservativeDepth>),
    #[from]
    Custom(CustomAttribute),
}

pub type AttributeNode = Spanned<Attribute>;

#[cfg(feature = "generics")]
#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From)]
pub struct TypeConstraint {
    pub ident: Ident,
    pub variants: Vec<TypeExpression>,
}

pub type Attributes = Vec<AttributeNode>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum Expression {
    Literal(LiteralExpression),
    Parenthesized(ParenthesizedExpression),
    NamedComponent(NamedComponentExpression),
    Indexing(IndexingExpression),
    Unary(UnaryExpression),
    Binary(BinaryExpression),
    FunctionCall(FunctionCallExpression),
    TypeOrIdentifier(TypeExpression),
}

pub type ExpressionNode = Spanned<Expression>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum LiteralExpression {
    Bool(bool),
    AbstractInt(i64),
    AbstractFloat(f64),
    I32(i32),
    U32(u32),
    F32(f32),
    #[from(skip)]
    F16(f32),
    #[cfg(feature = "naga-ext")]
    #[from(skip)]
    I64(i64),
    #[cfg(feature = "naga-ext")]
    #[from(skip)]
    U64(u64),
    #[cfg(feature = "naga-ext")]
    #[from(skip)]
    F64(f64),
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ParenthesizedExpression {
    pub expression: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct NamedComponentExpression {
    pub base: ExpressionNode,
    pub component: Ident,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct IndexingExpression {
    pub base: ExpressionNode,
    pub index: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct UnaryExpression {
    pub operator: UnaryOperator,
    pub operand: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct BinaryExpression {
    pub operator: BinaryOperator,
    pub left: ExpressionNode,
    pub right: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionCall {
    pub ty: TypeExpression,
    pub arguments: Vec<ExpressionNode>,
}

pub type FunctionCallExpression = FunctionCall;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct TypeExpression {
    #[cfg(feature = "imports")]
    pub path: Option<ModulePath>,
    pub ident: Ident,
    pub template_args: TemplateArgs,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct TemplateArg {
    pub expression: ExpressionNode,
}
pub type TemplateArgs = Option<Vec<TemplateArg>>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum Statement {
    Void,
    Compound(CompoundStatement),
    Assignment(AssignmentStatement),
    Increment(IncrementStatement),
    Decrement(DecrementStatement),
    If(IfStatement),
    Switch(SwitchStatement),
    Loop(LoopStatement),
    For(ForStatement),
    While(WhileStatement),
    Break(BreakStatement),
    Continue(ContinueStatement),
    Return(ReturnStatement),
    Discard(DiscardStatement),
    FunctionCall(FunctionCallStatement),
    ConstAssert(ConstAssertStatement),
    Declaration(DeclarationStatement),
}

pub type StatementNode = Spanned<Statement>;

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Default)]
pub struct CompoundStatement {
    pub attributes: Attributes,
    pub statements: Vec<StatementNode>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct AssignmentStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub operator: AssignmentOperator,
    pub lhs: ExpressionNode,
    pub rhs: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct IncrementStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct DecrementStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct IfStatement {
    pub attributes: Attributes,
    pub if_clause: IfClause,
    pub else_if_clauses: Vec<ElseIfClause>,
    pub else_clause: Option<ElseClause>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct IfClause {
    pub expression: ExpressionNode,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ElseIfClause {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: ExpressionNode,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ElseClause {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct SwitchStatement {
    pub attributes: Attributes,
    pub expression: ExpressionNode,
    pub body_attributes: Attributes,
    pub clauses: Vec<SwitchClause>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct SwitchClause {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub case_selectors: Vec<CaseSelector>,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, From, IsVariant, Unwrap)]
pub enum CaseSelector {
    Default,
    Expression(ExpressionNode),
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct LoopStatement {
    pub attributes: Attributes,
    pub body: CompoundStatement,
    // a ContinuingStatement can only appear inside a LoopStatement body, therefore it is
    // not part of the StatementNode enum. it appears here instead, but consider it part of
    // body as the last statement of the CompoundStatement.
    pub continuing: Option<ContinuingStatement>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ContinuingStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub body: CompoundStatement,
    // a BreakIfStatement can only appear inside a ContinuingStatement body, therefore it
    // not part of the StatementNode enum. it appears here instead, but consider it part of
    // body as the last statement of the CompoundStatement.
    pub break_if: Option<BreakIfStatement>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct BreakIfStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: ExpressionNode,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ForStatement {
    pub attributes: Attributes,
    pub initializer: Option<StatementNode>,
    pub condition: Option<ExpressionNode>,
    pub update: Option<StatementNode>,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct WhileStatement {
    pub attributes: Attributes,
    pub condition: ExpressionNode,
    pub body: CompoundStatement,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct BreakStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ContinueStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ReturnStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub expression: Option<ExpressionNode>,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct DiscardStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
}

#[cfg_attr(feature = "tokrepr", derive(TokRepr))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct FunctionCallStatement {
    #[cfg(feature = "attributes")]
    pub attributes: Attributes,
    pub call: FunctionCall,
}

pub type ConstAssertStatement = ConstAssert;

pub type DeclarationStatement = Declaration;