syn_solidity/stmt/
mod.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::{kw, Spanned};
use proc_macro2::Span;
use std::fmt;
use syn::{
    parse::{Parse, ParseStream},
    token::{Brace, Bracket, Paren},
    Result, Token,
};

mod assembly;
pub use assembly::{AssemblyFlags, StmtAssembly};

mod blocks;
pub use blocks::{Block, UncheckedBlock};

mod r#break;
pub use r#break::StmtBreak;

mod r#continue;
pub use r#continue::StmtContinue;

mod do_while;
pub use do_while::StmtDoWhile;

mod emit;
pub use emit::StmtEmit;

mod expr;
pub use expr::StmtExpr;

mod r#for;
pub use r#for::{ForInitStmt, StmtFor};

mod r#if;
pub use r#if::StmtIf;

mod r#return;
pub use r#return::StmtReturn;

mod revert;
pub use revert::StmtRevert;

mod r#try;
pub use r#try::{CatchClause, StmtTry};

mod var_decl;
pub use var_decl::{StmtVarDecl, VarDeclDecl, VarDeclTuple};

mod r#while;
pub use r#while::StmtWhile;

/// A statement, usually ending in a semicolon.
///
/// Solidity reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.statement>
#[derive(Clone)]
pub enum Stmt {
    /// An assembly block, with optional flags: `assembly "evmasm" { ... }`.
    Assembly(StmtAssembly),

    /// A blocked scope: `{ ... }`.
    Block(Block),

    /// A break statement: `break;`.
    Break(StmtBreak),

    /// A continue statement: `continue;`.
    Continue(StmtContinue),

    /// A do-while statement: `do { ... } while (condition);`.
    DoWhile(StmtDoWhile),

    /// An emit statement: `emit FooBar(42);`.
    Emit(StmtEmit),

    /// An expression with a trailing semicolon.
    Expr(StmtExpr),

    /// A for statement: `for (uint256 i; i < 42; ++i) { ... }`.
    For(StmtFor),

    /// An `if` statement with an optional `else` block: `if (expr) { ... } else
    /// { ... }`.
    If(StmtIf),

    /// A return statement: `return 42;`.
    Return(StmtReturn),

    /// A revert statement: `revert("error");`.
    Revert(StmtRevert),

    /// A try statement: `try fooBar(42) catch { ... }`.
    Try(StmtTry),

    /// An unchecked block: `unchecked { ... }`.
    UncheckedBlock(UncheckedBlock),

    /// A variable declaration statement: `uint256 foo = 42;`.
    VarDecl(StmtVarDecl),

    /// A while statement: `while (i < 42) { ... }`.
    While(StmtWhile),
}

impl fmt::Debug for Stmt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Stmt::")?;
        match self {
            Self::Assembly(stmt) => stmt.fmt(f),
            Self::Block(block) => block.fmt(f),
            Self::Break(stmt) => stmt.fmt(f),
            Self::Continue(stmt) => stmt.fmt(f),
            Self::DoWhile(stmt) => stmt.fmt(f),
            Self::Emit(stmt) => stmt.fmt(f),
            Self::Expr(stmt) => stmt.fmt(f),
            Self::For(stmt) => stmt.fmt(f),
            Self::If(stmt) => stmt.fmt(f),
            Self::Return(stmt) => stmt.fmt(f),
            Self::Revert(stmt) => stmt.fmt(f),
            Self::Try(stmt) => stmt.fmt(f),
            Self::UncheckedBlock(block) => block.fmt(f),
            Self::VarDecl(stmt) => stmt.fmt(f),
            Self::While(stmt) => stmt.fmt(f),
        }
    }
}

impl Parse for Stmt {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        // skip any attributes
        let _ = input.call(syn::Attribute::parse_outer)?;

        debug!("  > Stmt: {:?}", input.to_string());
        let stmt = Self::parse_simple(input)?;
        debug!("  < Stmt: {stmt:?}\n");
        Ok(stmt)
    }
}

impl Spanned for Stmt {
    fn span(&self) -> Span {
        match self {
            Self::Assembly(stmt) => stmt.span(),
            Self::Block(block) => block.span(),
            Self::Break(stmt) => stmt.span(),
            Self::Continue(stmt) => stmt.span(),
            Self::DoWhile(stmt) => stmt.span(),
            Self::Emit(stmt) => stmt.span(),
            Self::Expr(stmt) => stmt.span(),
            Self::For(stmt) => stmt.span(),
            Self::If(stmt) => stmt.span(),
            Self::Return(stmt) => stmt.span(),
            Self::Revert(stmt) => stmt.span(),
            Self::Try(stmt) => stmt.span(),
            Self::UncheckedBlock(block) => block.span(),
            Self::VarDecl(stmt) => stmt.span(),
            Self::While(stmt) => stmt.span(),
        }
    }

    fn set_span(&mut self, span: Span) {
        match self {
            Self::Assembly(stmt) => stmt.set_span(span),
            Self::Block(block) => block.set_span(span),
            Self::Break(stmt) => stmt.set_span(span),
            Self::Continue(stmt) => stmt.set_span(span),
            Self::DoWhile(stmt) => stmt.set_span(span),
            Self::Emit(stmt) => stmt.set_span(span),
            Self::Expr(stmt) => stmt.set_span(span),
            Self::For(stmt) => stmt.set_span(span),
            Self::If(stmt) => stmt.set_span(span),
            Self::Return(stmt) => stmt.set_span(span),
            Self::Revert(stmt) => stmt.set_span(span),
            Self::Try(stmt) => stmt.set_span(span),
            Self::UncheckedBlock(block) => block.set_span(span),
            Self::VarDecl(stmt) => stmt.set_span(span),
            Self::While(stmt) => stmt.set_span(span),
        }
    }
}

impl Stmt {
    fn parse_simple(input: ParseStream<'_>) -> Result<Self> {
        let lookahead = input.lookahead1();
        if lookahead.peek(Brace) {
            input.parse().map(Self::Block)
        } else if lookahead.peek(Paren) {
            StmtVarDecl::parse_or_expr(input)
        } else if lookahead.peek(Bracket) {
            input.parse().map(Self::Expr)
        } else if lookahead.peek(kw::unchecked) {
            input.parse().map(Self::UncheckedBlock)
        } else if lookahead.peek(Token![if]) {
            input.parse().map(Self::If)
        } else if lookahead.peek(Token![for]) {
            input.parse().map(Self::For)
        } else if lookahead.peek(Token![while]) {
            input.parse().map(Self::While)
        } else if lookahead.peek(Token![do]) {
            input.parse().map(Self::DoWhile)
        } else if lookahead.peek(Token![continue]) {
            input.parse().map(Self::Continue)
        } else if lookahead.peek(Token![break]) {
            input.parse().map(Self::Break)
        } else if lookahead.peek(Token![try]) {
            input.parse().map(Self::Try)
        } else if lookahead.peek(Token![return]) {
            input.parse().map(Self::Return)
        } else if lookahead.peek(kw::emit) {
            input.parse().map(Self::Emit)
        } else if lookahead.peek(kw::revert) {
            input.parse().map(Self::Revert)
        } else if lookahead.peek(kw::assembly) {
            input.parse().map(Self::Assembly)
        } else {
            StmtVarDecl::parse_or_expr(input)
        }
    }
}