rune/ast/
local.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
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    rt::<ast::Local>("let x = 1;");
    rt::<ast::Local>("#[attr] let a = f();");
    rt::<ast::Local>("let a = b{}().foo[0].await;");
}

/// A local variable declaration.
///
/// * `let <pattern> = <expr>;`
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Parse, Spanned)]
#[non_exhaustive]
pub struct Local {
    /// The attributes for the let expression
    #[rune(iter, meta)]
    pub attributes: Vec<ast::Attribute>,
    /// The `let` keyword.
    pub let_token: T![let],
    /// The `mut` token.
    #[rune(iter)]
    pub mut_token: Option<T![mut]>,
    /// The name of the binding.
    pub pat: ast::Pat,
    /// The equality keyword.
    pub eq: T![=],
    /// The expression the binding is assigned to.
    #[rune(parse_with = "parse_expr")]
    pub expr: ast::Expr,
    /// Trailing semicolon of the local.
    pub semi: T![;],
}

fn parse_expr(p: &mut Parser<'_>) -> Result<ast::Expr> {
    ast::Expr::parse_with(
        p,
        ast::expr::EAGER_BRACE,
        ast::expr::EAGER_BINARY,
        ast::expr::CALLABLE,
    )
}