syn_solidity/stmt/
blocks.rs1use crate::{kw, Spanned, Stmt};
2use proc_macro2::Span;
3use std::fmt;
4use syn::{
5 parse::{Parse, ParseStream},
6 token::Brace,
7 Result,
8};
9
10#[derive(Clone)]
12pub struct Block {
13 pub brace_token: Brace,
14 pub stmts: Vec<Stmt>,
15}
16
17impl fmt::Debug for Block {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 f.debug_tuple("Block").field(&self.stmts).finish()
20 }
21}
22
23impl Parse for Block {
24 fn parse(input: ParseStream<'_>) -> Result<Self> {
25 let content;
26 Ok(Self {
27 brace_token: syn::braced!(content in input),
28 stmts: {
29 let mut vec = Vec::new();
30 debug!("> Block: {:?}", content.to_string());
31 while !content.is_empty() {
32 vec.push(content.parse()?);
33 }
34 debug!("< Block: {} stmts\n", vec.len());
35 vec
36 },
37 })
38 }
39}
40
41impl Spanned for Block {
42 fn span(&self) -> Span {
43 self.brace_token.span.join()
44 }
45
46 fn set_span(&mut self, span: Span) {
47 self.brace_token = Brace(span);
48 }
49}
50
51#[derive(Clone)]
53pub struct UncheckedBlock {
54 pub unchecked_token: kw::unchecked,
55 pub block: Block,
56}
57
58impl fmt::Debug for UncheckedBlock {
59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60 f.debug_struct("UncheckedBlock").field("stmts", &self.block.stmts).finish()
61 }
62}
63
64impl Parse for UncheckedBlock {
65 fn parse(input: ParseStream<'_>) -> Result<Self> {
66 Ok(Self { unchecked_token: input.parse()?, block: input.parse()? })
67 }
68}
69
70impl Spanned for UncheckedBlock {
71 fn span(&self) -> Span {
72 let span = self.unchecked_token.span;
73 span.join(self.block.span()).unwrap_or(span)
74 }
75
76 fn set_span(&mut self, span: Span) {
77 self.unchecked_token.span = span;
78 self.block.set_span(span);
79 }
80}