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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use super::{
    ExpandFormatted, ExpandInto, ExpandWithFormatter, FormatArg, FormatArgs, FormatIfArgs,
    LocalVariable, UncheckedFormatArg, UncheckedFormatArgs, WriteArgs,
};

use crate::{
    format_str::{FmtArg, FmtStrComponent, FormatStr, WhichArg},
    parse_utils::{LitStr, MyParse, ParseBuffer, ParseStream, TokenTreeExt},
    shared_arg_parsing::ExprArg,
    spanned::Spans,
    utils::{dummy_ident, LinearResult},
};

use proc_macro2::{Ident, Span, TokenTree};

////////////////////////////////////////////////

impl MyParse for UncheckedFormatArg {
    fn parse(input: ParseStream<'_>) -> Result<Self, crate::Error> {
        // the compile wraps `:expr` in macro_rules macros in a TokenStream::Group
        // with no delimiters.
        input.parse_unwrap_group(|content| {
            let mut ident = None;
            if matches!(content.peek2(), Some(x) if x.is_punct('=')) {
                ident = Some(content.parse_ident()?);
                content.next();
            }

            // For some reason,
            // the compile wraps closures in parentheses when passing them as
            // expressions to proc macros.
            content.parse_unwrap_paren(|content| {
                let mut fmt_ident = None;

                if matches!(content.peek(), Some(x) if x.is_punct('|'))
                    && matches!(content.peek2(), Some(TokenTree::Ident(_)))
                {
                    content.next();
                    fmt_ident = Some(content.parse_ident()?);
                    content.parse_punct('|')?;
                }

                let (expr, spans) = if content.peek2().is_some() {
                    content.parse_token_stream_and_span()
                } else {
                    content.parse_unwrap_tt(|content| Ok(content.parse_token_stream_and_span()))?
                };

                Ok(Self {
                    spans,
                    ident,
                    fmt_ident,
                    expr,
                })
            })
        })
    }
}

////////////////////////////////////////////////

fn lit_str_to_fmt_lit(lit: &LitStr) -> Result<FormatStr, crate::Error> {
    let lit_str = lit.value();
    let format_str_span = lit.span;
    FormatStr::parse(lit.value(), lit.rawness)
        .map_err(|e| e.into_crate_err(format_str_span, lit_str))
}

fn parse_fmt_lit(this: &mut FormatStr, input: ParseStream<'_>) -> Result<(), crate::Error> {
    input.parse_unwrap_tt(|input| {
        let tt = input.next();

        match tt {
            Some(TokenTree::Literal(lit)) => {
                let mut lit = lit_str_to_fmt_lit(&LitStr::parse_from_literal(&lit)?)?;

                this.list.append(&mut lit.list);

                Ok(())
            }
            Some(TokenTree::Ident(ident)) if ident == "concat" => {
                input.next(); // skipping the `!`
                let paren = input.parse_paren()?;
                let mut input = ParseBuffer::new(paren.contents);

                while !input.is_empty() {
                    parse_fmt_lit(this, &mut input)?;
                    input.parse_opt_punct(',')?;
                }
                Ok(())
            }
            _ => Ok(()),
        }
    })
}

impl MyParse for UncheckedFormatArgs {
    fn parse(input: ParseStream<'_>) -> Result<Self, crate::Error> {
        let mut literal = FormatStr { list: Vec::new() };

        // Have to parse `concat!()` because it's not expanded before the proc macro is called.
        {
            let paren = input.parse_paren()?;
            let mut input = ParseBuffer::new(paren.contents);

            parse_fmt_lit(&mut literal, &mut input)?;
        }

        input.parse_opt_punct(',')?;

        let mut args = Vec::new();

        while !input.is_empty() {
            args.push(UncheckedFormatArg::parse(input)?);

            input.parse_opt_punct(',')?;
        }

        Ok(Self { literal, args })
    }
}

////////////////////////////////////////////////

impl MyParse for FormatArgs {
    fn parse(input: ParseStream<'_>) -> Result<Self, crate::Error> {
        let prefix = Ident::new("const_fmt_local_", Span::call_site());
        FormatArgs::parse_with(input, prefix)
    }
}

impl FormatArgs {
    pub fn parse_with(input: ParseStream<'_>, prefix: Ident) -> Result<FormatArgs, crate::Error> {
        let mut res = LinearResult::ok();

        let unchecked_fargs = UncheckedFormatArgs::parse(input)?;

        let mut first_named_arg = unchecked_fargs.args.len();

        let mut named_arg_names = Vec::<Ident>::new();
        let mut args = Vec::<FormatArg>::with_capacity(unchecked_fargs.args.len());
        let mut local_variables = Vec::<LocalVariable>::with_capacity(unchecked_fargs.args.len());

        let arg_span_idents: Vec<(Spans, Option<Ident>)> = unchecked_fargs
            .args
            .iter()
            .map(|x| (x.spans, x.ident.clone()))
            .collect();

        {
            let mut prev_is_named_arg = false;
            for (i, arg) in unchecked_fargs.args.into_iter().enumerate() {
                let expr_span = arg.spans;

                let make_ident = |s: String| Ident::new(&s, expr_span.start);

                let is_named_arg = arg.ident.is_some();

                let var_name = if let Some(ident) = arg.ident {
                    if !prev_is_named_arg {
                        first_named_arg = i;
                    }

                    let name = make_ident(format!("{}{}", prefix, ident));
                    named_arg_names.push(ident);
                    name
                } else {
                    if prev_is_named_arg {
                        return Err(crate::Error::spanned(
                            arg.spans,
                            "expected a named argument, \
                             named arguments cannot be followed by positional arguments.",
                        ));
                    }

                    make_ident(format!("{}{}", prefix, i))
                };

                let format_arg = if let Some(fmt_ident) = &arg.fmt_ident {
                    FormatArg::WithFormatter {
                        fmt_ident: fmt_ident.clone(),
                        expr: arg.expr.clone(),
                    }
                } else {
                    local_variables.push(LocalVariable {
                        ident: var_name.clone(),
                        expr: arg.expr.clone(),
                    });

                    FormatArg::WithLocal(var_name)
                };

                args.push(format_arg);

                prev_is_named_arg = is_named_arg;
            }
        }

        let mut unused_args = vec![true; args.len()];

        let first_named_arg = first_named_arg;
        let named_arg_names = named_arg_names;
        let args = args;

        let positional_args = &args[..first_named_arg];
        let named_args = &args[first_named_arg..];

        let fmt_str_components = unchecked_fargs.literal.list;

        let expanded_into: Vec<ExpandInto> = {
            let mut current_pos_arg = 0;
            let mut get_variable_name = |param: FmtArg| -> ExpandInto {
                let FmtArg {
                    which_arg,
                    formatting,
                    rawness,
                } = param;

                let arg = match which_arg {
                    WhichArg::Ident(ident) => {
                        if let Some(pos) = named_arg_names.iter().position(|x| *x == ident) {
                            unused_args[pos + first_named_arg] = false;
                            &named_args[pos]
                        } else {
                            // `formatcp!("{FOO}")` assumes that FOO is a constant in scope
                            return ExpandInto::Formatted(ExpandFormatted {
                                local_variable: Ident::new(&ident, rawness.span()),
                                format: formatting,
                            });
                        }
                    }
                    WhichArg::Positional(opt_pos) => {
                        let pos = opt_pos.unwrap_or_else(|| {
                            let pos = current_pos_arg;
                            current_pos_arg += 1;
                            pos
                        });

                        match positional_args.get(pos) {
                            Some(arg) => {
                                unused_args[pos] = false;
                                arg
                            }
                            None => {
                                res.push_err(crate::Error::new(
                                    rawness.span(),
                                    format!(
                                        "attempting to use nonexistent  positional argument `{}`",
                                        pos,
                                    ),
                                ));
                                return ExpandInto::Formatted(ExpandFormatted {
                                    local_variable: dummy_ident(),
                                    format: formatting,
                                });
                            }
                        }
                    }
                };

                match arg {
                    FormatArg::WithFormatter { fmt_ident, expr } => {
                        ExpandInto::WithFormatter(ExpandWithFormatter {
                            format: formatting,
                            fmt_ident: fmt_ident.clone(),
                            expr: expr.clone(),
                        })
                    }
                    FormatArg::WithLocal(local_variable) => {
                        ExpandInto::Formatted(ExpandFormatted {
                            format: formatting,
                            local_variable: local_variable.clone(),
                        })
                    }
                }
            };

            fmt_str_components
                .into_iter()
                .map(|fmt_str_comp| match fmt_str_comp {
                    FmtStrComponent::Str(str, str_rawness) => ExpandInto::Str(str, str_rawness),
                    FmtStrComponent::Arg(arg) => get_variable_name(arg),
                })
                .collect()
        };

        for (i, (is_it_unused, (spans, ident))) in
            unused_args.iter().zip(&arg_span_idents).enumerate()
        {
            if *is_it_unused {
                let msg = if let Some(ident) = ident {
                    format!("the '{}' argument is unused", ident)
                } else {
                    format!("argument number {} is unused", i)
                };
                res.push_err(crate::Error::spanned(*spans, msg));
            }
        }
        res.take()?;

        Ok(FormatArgs {
            condition: None,
            local_variables,
            expanded_into,
        })
    }
}

////////////////////////////////////////////////

impl MyParse for FormatIfArgs {
    fn parse(input: ParseStream) -> Result<Self, crate::Error> {
        let condition = ExprArg::parse(input)?;

        let mut inner = FormatArgs::parse(input)?;
        inner.condition = Some(condition);

        Ok(Self { inner })
    }
}

////////////////////////////////////////////////

impl MyParse for WriteArgs {
    fn parse(input: ParseStream) -> Result<Self, crate::Error> {
        let prefix = Ident::new("const_fmt_local_", Span::call_site());

        let paren = input.parse_paren()?;

        let mut content = ParseBuffer::new(paren.contents);

        let (writer_expr, spans) =
            content.parse_unwrap_tt(|content| Ok(content.parse_token_stream_and_span()))?;

        let format_args = FormatArgs::parse_with(input, prefix)?;

        Ok(Self {
            writer_expr,
            writer_span: spans.joined(),
            format_args,
        })
    }
}