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
use crate::ast;
use crate::ast::utils;
use crate::error::ParseError;
use crate::parser::Parser;
use crate::traits::Parse;
use runestick::unit::Span;

/// A single selection branch.
#[derive(Debug, Clone)]
pub struct ExprSelectBranch {
    /// The identifier to bind the result to.
    pub pat: ast::Pat,
    /// `=`.
    pub eq: ast::Eq,
    /// The expression that should evaluate to a future.
    pub expr: Box<ast::Expr>,
    /// `=>`.
    pub rocket: ast::Rocket,
    /// The body of the expression.
    pub body: Box<ast::Expr>,
}

impl ExprSelectBranch {
    /// The span of the expression.
    pub fn span(&self) -> Span {
        self.pat.span().join(self.body.span())
    }
}

impl Parse for ExprSelectBranch {
    fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
        Ok(Self {
            pat: parser.parse()?,
            eq: parser.parse()?,
            expr: Box::new(parser.parse()?),
            rocket: parser.parse()?,
            body: Box::new(parser.parse()?),
        })
    }
}

/// A single selection branch.
#[derive(Debug, Clone)]
pub struct ExprDefaultBranch {
    /// The `default` keyword.
    pub default: ast::Default,
    /// `=>`.
    pub rocket: ast::Rocket,
    /// The body of the expression.
    pub body: Box<ast::Expr>,
}

impl ExprDefaultBranch {
    /// The span of the expression.
    pub fn span(&self) -> Span {
        self.default.span().join(self.body.span())
    }
}

impl Parse for ExprDefaultBranch {
    fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
        Ok(Self {
            default: parser.parse()?,
            rocket: parser.parse()?,
            body: Box::new(parser.parse()?),
        })
    }
}

/// A select expression that selects over a collection of futures.
#[derive(Debug, Clone)]
pub struct ExprSelect {
    /// The `select` keyword.
    pub select: ast::Select,
    /// The opening brace of the select.
    pub open: ast::OpenBrace,
    /// The branches of the select.
    pub branches: Vec<(ExprSelectBranch, Option<ast::Comma>)>,
    /// The default branch.
    pub default_branch: Option<(ExprDefaultBranch, Option<ast::Comma>)>,
    /// The closing brace of the select.
    pub close: ast::CloseBrace,
}

impl ExprSelect {
    /// Access the span of the expression.
    pub fn span(&self) -> Span {
        self.select.span().join(self.close.span())
    }
}

impl Parse for ExprSelect {
    fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
        let select = parser.parse()?;
        let open = parser.parse()?;

        let mut branches = Vec::new();
        let mut default_branch = None;

        while !parser.peek::<ast::CloseBrace>()? {
            let is_end;

            if parser.peek::<ast::Default>()? {
                let branch = parser.parse::<ExprDefaultBranch>()?;

                let comma = if parser.peek::<ast::Comma>()? {
                    Some(parser.parse()?)
                } else {
                    None
                };

                is_end = utils::is_block_end(&*branch.body, comma.as_ref());
                default_branch = Some((branch, comma));
            } else {
                let branch = parser.parse::<ExprSelectBranch>()?;

                let comma = if parser.peek::<ast::Comma>()? {
                    Some(parser.parse()?)
                } else {
                    None
                };

                is_end = utils::is_block_end(&*branch.body, comma.as_ref());
                branches.push((branch, comma));
            };

            if is_end {
                break;
            }
        }

        let close = parser.parse()?;

        Ok(Self {
            select,
            open,
            branches,
            default_branch,
            close,
        })
    }
}