sway_ast/
statement.rs

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
use sway_error::handler::ErrorEmitted;

use crate::priv_prelude::*;

#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Serialize)]
pub enum Statement {
    Let(StatementLet),
    Item(Item),
    Expr {
        expr: Expr,
        semicolon_token_opt: Option<SemicolonToken>,
    },
    // to handle parser recovery: Error represents an unknown statement
    Error(Box<[Span]>, #[serde(skip_serializing)] ErrorEmitted),
}

#[derive(Clone, Debug, Serialize)]
pub struct StatementLet {
    pub let_token: LetToken,
    pub pattern: Pattern,
    pub ty_opt: Option<(ColonToken, Ty)>,
    pub eq_token: EqToken,
    pub expr: Expr,
    pub semicolon_token: SemicolonToken,
}

impl Spanned for Statement {
    fn span(&self) -> Span {
        match self {
            Statement::Let(statement_let) => statement_let.span(),
            Statement::Item(item) => item.span(),
            Statement::Expr {
                expr,
                semicolon_token_opt,
            } => match semicolon_token_opt {
                None => expr.span(),
                Some(semicolon_token) => Span::join(expr.span(), &semicolon_token.span()),
            },
            Statement::Error(spans, _) => Span::join_all(spans.iter().cloned()),
        }
    }
}

impl Spanned for StatementLet {
    fn span(&self) -> Span {
        Span::join(self.let_token.span(), &self.semicolon_token.span())
    }
}