rune/ast/
expr_block.rs

1use crate::ast::prelude::*;
2
3#[test]
4#[cfg(not(miri))]
5fn ast_parse() {
6    let expr = rt::<ast::ExprBlock>("async {}");
7    assert_eq!(expr.block.statements.len(), 0);
8
9    let expr = rt::<ast::ExprBlock>("async move {}");
10    assert_eq!(expr.block.statements.len(), 0);
11
12    let expr = rt::<ast::ExprBlock>("const {}");
13    assert_eq!(expr.block.statements.len(), 0);
14
15    let expr = rt::<ast::ExprBlock>("async { 42 }");
16    assert_eq!(expr.block.statements.len(), 1);
17
18    let expr = rt::<ast::ExprBlock>("'foo: { 42 }");
19    assert_eq!(expr.block.statements.len(), 1);
20    assert!(expr.label.is_some());
21
22    let expr = rt::<ast::ExprBlock>("#[retry] async { 42 }");
23    assert_eq!(expr.block.statements.len(), 1);
24    assert_eq!(expr.attributes.len(), 1);
25}
26
27/// A block expression.
28///
29/// * `<block>`.
30/// * `async <block>`.
31/// * `const <block>`.
32#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
33#[non_exhaustive]
34pub struct ExprBlock {
35    /// The attributes for the block.
36    #[rune(iter, meta)]
37    pub attributes: Vec<ast::Attribute>,
38    /// The optional async token.
39    #[rune(iter, meta)]
40    pub async_token: Option<T![async]>,
41    /// The optional const token.
42    #[rune(iter, meta)]
43    pub const_token: Option<T![const]>,
44    /// The optional move token.
45    #[rune(iter, meta)]
46    pub move_token: Option<T![move]>,
47    /// An optional label for the block.
48    #[rune(iter)]
49    pub label: Option<(ast::Label, T![:])>,
50    /// The close brace.
51    pub block: ast::Block,
52}
53
54expr_parse!(Block, ExprBlock, "block expression");