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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::{
    kw,
    utils::{DebugPunctuated, ParseNested},
    Expr, SolIdent, Spanned,
};
use proc_macro2::Span;
use std::fmt;
use syn::{
    braced, parenthesized,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    token::{Brace, Paren},
    Result, Token,
};

/// A function call expression: `foo(42)` or `foo({ bar: 42 })`.
#[derive(Clone, Debug)]
pub struct ExprCall {
    pub expr: Box<Expr>,
    pub args: ArgList,
}

impl ParseNested for ExprCall {
    fn parse_nested(expr: Box<Expr>, input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { expr, args: input.parse()? })
    }
}

derive_parse!(ExprCall);

impl Spanned for ExprCall {
    fn span(&self) -> Span {
        let span = self.expr.span();
        span.join(self.args.span()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.expr.set_span(span);
        self.args.set_span(span);
    }
}

/// A `payable` expression: `payable(address(0x...))`.
#[derive(Clone)]
pub struct ExprPayable {
    pub payable_token: kw::payable,
    pub args: ArgList,
}

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

impl From<ExprPayable> for ExprCall {
    fn from(value: ExprPayable) -> Self {
        Self {
            expr: Box::new(Expr::Ident(SolIdent::new_spanned("payable", value.payable_token.span))),
            args: value.args,
        }
    }
}

impl Parse for ExprPayable {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { payable_token: input.parse()?, args: input.parse()? })
    }
}

impl Spanned for ExprPayable {
    fn span(&self) -> Span {
        let span = self.payable_token.span;
        span.join(self.args.span()).unwrap_or(span)
    }

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

/// A list of named or unnamed arguments: `{ foo: 42, bar: 64 }` or `(42, 64)`.
///
/// Solidity reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.callArgumentList>
#[derive(Clone)]
pub struct ArgList {
    pub paren_token: Paren,
    /// The list of arguments. Can be named or unnamed.
    ///
    /// When empty, this is an empty unnamed list.
    pub list: ArgListImpl,
}

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

impl Parse for ArgList {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self { paren_token: parenthesized!(content in input), list: content.parse()? })
    }
}

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

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

/// A list of either unnamed or named arguments.
#[derive(Clone)]
pub enum ArgListImpl {
    Unnamed(Punctuated<Expr, Token![,]>),
    Named(NamedArgList),
}

impl fmt::Debug for ArgListImpl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unnamed(list) => {
                f.debug_tuple("Unnamed").field(DebugPunctuated::new(list)).finish()
            }
            Self::Named(list) => f.debug_tuple("Named").field(list).finish(),
        }
    }
}

impl Parse for ArgListImpl {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        if input.peek(Brace) {
            input.parse().map(Self::Named)
        } else {
            input.parse_terminated(Expr::parse, Token![,]).map(Self::Unnamed)
        }
    }
}

/// Function call options: `foo.bar{ value: 1, gas: 2 }`.
#[derive(Clone, Debug)]
pub struct ExprCallOptions {
    pub expr: Box<Expr>,
    pub args: NamedArgList,
}

impl ParseNested for ExprCallOptions {
    fn parse_nested(expr: Box<Expr>, input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { expr, args: input.parse()? })
    }
}

derive_parse!(ExprCallOptions);

impl Spanned for ExprCallOptions {
    fn span(&self) -> Span {
        let span = self.expr.span();
        span.join(self.args.span()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.expr.set_span(span);
        self.args.set_span(span);
    }
}

/// A named argument list: `{ foo: uint256(42), bar: true }`.
#[derive(Clone)]
pub struct NamedArgList {
    pub brace_token: Brace,
    pub list: Punctuated<NamedArg, Token![,]>,
}

impl fmt::Debug for NamedArgList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NamedArgList").field("list", DebugPunctuated::new(&self.list)).finish()
    }
}

impl Parse for NamedArgList {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            brace_token: braced!(content in input),
            list: content.parse_terminated(NamedArg::parse, Token![,])?,
        })
    }
}

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

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

/// A named argument in an argument list: `foo: uint256(42)`.
#[derive(Clone)]
pub struct NamedArg {
    pub name: SolIdent,
    pub colon_token: Token![:],
    pub arg: Expr,
}

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

impl Parse for NamedArg {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { name: input.parse()?, colon_token: input.parse()?, arg: input.parse()? })
    }
}

impl Spanned for NamedArg {
    fn span(&self) -> Span {
        let span = self.name.span();
        span.join(self.arg.span()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.name.set_span(span);
        self.arg.set_span(span);
    }
}