syn_solidity/yul/stmt/
block.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
50
51
52
use crate::{Spanned, YulStmt};
use proc_macro2::Span;
use std::fmt;
use syn::{
    braced,
    parse::{Parse, ParseStream},
    token::Brace,
    Result,
};

/// A Yul block contains `YulStmt` between curly braces.
///
/// Solidity Reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.yulBlock>
#[derive(Clone)]
pub struct YulBlock {
    pub brace_token: Brace,
    pub stmts: Vec<YulStmt>,
}

impl Parse for YulBlock {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            brace_token: braced!(content in input),
            stmts: {
                let mut stmts = Vec::new();
                while !content.is_empty() {
                    let stmt: YulStmt = content.parse()?;
                    stmts.push(stmt);
                }
                stmts
            },
        })
    }
}

impl Spanned for YulBlock {
    fn span(&self) -> Span {
        self.brace_token.span.join()
    }

    fn set_span(&mut self, span: Span) {
        self.brace_token = Brace(span);
    }
}

impl fmt::Debug for YulBlock {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("YulBlock").field("stmt", &self.stmts).finish()
    }
}