rune/ast/
expr_let.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    rt::<ast::ExprLet>("let x = 1");
7    rt::<ast::ExprLet>("#[attr] let a = f()");
8}
9
10/// A let expression.
11///
12/// * `let <name> = <expr>`
13#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
14#[non_exhaustive]
15pub struct ExprLet {
16    /// The attributes for the let expression
17    #[rune(iter)]
18    pub attributes: Vec<ast::Attribute>,
19    /// The `let` token.
20    pub let_token: T![let],
21    /// The `mut` token.
22    #[rune(iter)]
23    pub mut_token: Option<T![mut]>,
24    /// The name of the binding.
25    pub pat: ast::Pat,
26    /// The equality token.
27    pub eq: T![=],
28    /// The expression the binding is assigned to.
29    pub expr: Box<ast::Expr>,
30}
31
32impl ExprLet {
33    /// Parse with the given meta.
34    pub(crate) fn parse_with_meta(
35        parser: &mut Parser<'_>,
36        attributes: Vec<ast::Attribute>,
37    ) -> Result<Self> {
38        Ok(Self {
39            attributes,
40            let_token: parser.parse()?,
41            mut_token: parser.parse()?,
42            pat: parser.parse()?,
43            eq: parser.parse()?,
44            expr: Box::try_new(ast::Expr::parse_without_eager_brace(parser)?)?,
45        })
46    }
47
48    /// Parse a let expression without eager bracing.
49    pub(crate) fn parse_without_eager_brace(parser: &mut Parser<'_>) -> Result<Self> {
50        Ok(Self {
51            attributes: Vec::new(),
52            let_token: parser.parse()?,
53            mut_token: parser.parse()?,
54            pat: parser.parse()?,
55            eq: parser.parse()?,
56            expr: Box::try_new(ast::Expr::parse_without_eager_brace(parser)?)?,
57        })
58    }
59}
60
61expr_parse!(Let, ExprLet, "let expression");