syn_solidity/stmt/
blocks.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
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
use crate::{kw, Spanned, Stmt};
use proc_macro2::Span;
use std::fmt;
use syn::{
    parse::{Parse, ParseStream},
    token::Brace,
    Result,
};

/// A curly-braced block of statements: `{ ... }`.
#[derive(Clone)]
pub struct Block {
    pub brace_token: Brace,
    pub stmts: Vec<Stmt>,
}

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

impl Parse for Block {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            brace_token: syn::braced!(content in input),
            stmts: {
                let mut vec = Vec::new();
                debug!("> Block: {:?}", content.to_string());
                while !content.is_empty() {
                    vec.push(content.parse()?);
                }
                debug!("< Block: {} stmts\n", vec.len());
                vec
            },
        })
    }
}

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

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

/// An unchecked block: `unchecked { ... }`.
#[derive(Clone)]
pub struct UncheckedBlock {
    pub unchecked_token: kw::unchecked,
    pub block: Block,
}

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

impl Parse for UncheckedBlock {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { unchecked_token: input.parse()?, block: input.parse()? })
    }
}

impl Spanned for UncheckedBlock {
    fn span(&self) -> Span {
        let span = self.unchecked_token.span;
        span.join(self.block.span()).unwrap_or(span)
    }

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