sway_ast/
assignable.rs

1use crate::priv_prelude::*;
2
3/// Left-hand side of an assignment.
4#[derive(Clone, Debug, Serialize)]
5pub enum Assignable {
6    /// A single variable or a path to a part of an aggregate.
7    /// E.g.:
8    ///  - `my_variable`
9    ///  - `array[0].field.x.1`
10    ///
11    /// Note that within the path, we cannot have dereferencing
12    /// (except, of course, in expressions inside of array index operator).
13    /// This is guaranteed by the grammar.
14    /// E.g., an expression like this is not allowed by the grammar:
15    ///  `my_struct.*expr`
16    ElementAccess(ElementAccess),
17    /// Dereferencing of an arbitrary reference expression.
18    /// E.g.:
19    ///  - *my_ref
20    ///  - **if x > 0 { &mut &mut a } else { &mut &mut b }
21    Deref {
22        star_token: StarToken,
23        expr: Box<Expr>,
24    },
25}
26
27#[derive(Clone, Debug, Serialize)]
28pub enum ElementAccess {
29    Var(Ident),
30    Index {
31        target: Box<ElementAccess>,
32        arg: SquareBrackets<Box<Expr>>,
33    },
34    FieldProjection {
35        target: Box<ElementAccess>,
36        dot_token: DotToken,
37        name: Ident,
38    },
39    TupleFieldProjection {
40        target: Box<ElementAccess>,
41        dot_token: DotToken,
42        field: BigUint,
43        field_span: Span,
44    },
45}
46
47impl Spanned for Assignable {
48    fn span(&self) -> Span {
49        match self {
50            Assignable::ElementAccess(element_access) => element_access.span(),
51            Assignable::Deref { star_token, expr } => Span::join(star_token.span(), &expr.span()),
52        }
53    }
54}
55
56impl Spanned for ElementAccess {
57    fn span(&self) -> Span {
58        match self {
59            ElementAccess::Var(name) => name.span(),
60            ElementAccess::Index { target, arg } => Span::join(target.span(), &arg.span()),
61            ElementAccess::FieldProjection { target, name, .. } => {
62                Span::join(target.span(), &name.span())
63            }
64            ElementAccess::TupleFieldProjection {
65                target, field_span, ..
66            } => Span::join(target.span(), field_span),
67        }
68    }
69}