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
#![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::{builtin::*, text_size::TextSize, ConversionFlag, Node};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;

pub type Suite<R = TextRange> = Vec<Stmt<R>>;

#[cfg(feature = "all-nodes-with-ranges")]
pub type OptionalRange<R> = R;

#[cfg(not(feature = "all-nodes-with-ranges"))]
pub type OptionalRange<R> = EmptyRange<R>;

#[cfg(not(feature = "all-nodes-with-ranges"))]
impl<R> From<R> for OptionalRange<R> {
    fn from(_: R) -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
    phantom: PhantomData<R>,
}

impl<R> EmptyRange<R> {
    #[inline(always)]
    pub fn new(_start: TextSize, _end: TextSize) -> Self {
        Self {
            phantom: PhantomData,
        }
    }
}

impl<R> Display for EmptyRange<R> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str("()")
    }
}

impl<R> Debug for EmptyRange<R> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

impl<R> Default for EmptyRange<R> {
    fn default() -> Self {
        EmptyRange {
            phantom: PhantomData,
        }
    }
}

impl CmpOp {
    pub fn as_str(&self) -> &'static str {
        match self {
            CmpOp::Eq => "==",
            CmpOp::NotEq => "!=",
            CmpOp::Lt => "<",
            CmpOp::LtE => "<=",
            CmpOp::Gt => ">",
            CmpOp::GtE => ">=",
            CmpOp::Is => "is",
            CmpOp::IsNot => "is not",
            CmpOp::In => "in",
            CmpOp::NotIn => "not in",
        }
    }
}

impl<R> Arguments<R> {
    pub fn empty(range: OptionalRange<R>) -> Self {
        Self {
            range,
            posonlyargs: Vec::new(),
            args: Vec::new(),
            vararg: None,
            kwonlyargs: Vec::new(),
            kwarg: None,
        }
    }
}

#[allow(clippy::borrowed_box)] // local utility
fn clone_boxed_expr<R: Clone>(expr: &Box<Expr<R>>) -> Box<Expr<R>> {
    let expr: &Expr<_> = expr.as_ref();
    Box::new(expr.clone())
}

impl<R> ArgWithDefault<R> {
    pub fn from_arg(def: Arg<R>, default: Option<Expr<R>>) -> Self
    where
        R: Clone,
    {
        let range = {
            if cfg!(feature = "all-nodes-with-ranges") {
                todo!("range recovery is not implemented yet") // def.range.start()..default.range.end()
            } else {
                #[allow(clippy::useless_conversion)] // false positive by cfg
                OptionalRange::from(def.range.clone())
            }
        };
        Self {
            range,
            def,
            default: default.map(Box::new),
        }
    }

    pub fn as_arg(&self) -> &Arg<R> {
        &self.def
    }

    pub fn to_arg(&self) -> (Arg<R>, Option<Box<Expr<R>>>)
    where
        R: Clone,
    {
        let ArgWithDefault {
            range: _,
            def,
            default,
        } = self;
        (def.clone(), default.as_ref().map(clone_boxed_expr))
    }
    pub fn into_arg(self) -> (Arg<R>, Option<Box<Expr<R>>>) {
        let ArgWithDefault {
            range: _,
            def,
            default,
        } = self;
        (def, default)
    }
}

impl<R> Arguments<R> {
    pub fn defaults(&self) -> impl std::iter::Iterator<Item = &Expr<R>> {
        self.posonlyargs
            .iter()
            .chain(self.args.iter())
            .filter_map(|arg| arg.default.as_ref().map(|e| e.as_ref()))
    }

    #[allow(clippy::type_complexity)]
    pub fn split_kwonlyargs(&self) -> (Vec<&Arg<R>>, Vec<(&Arg<R>, &Expr<R>)>) {
        let mut args = Vec::new();
        let mut with_defaults = Vec::new();
        for arg in self.kwonlyargs.iter() {
            if let Some(ref default) = arg.default {
                with_defaults.push((arg.as_arg(), &**default));
            } else {
                args.push(arg.as_arg());
            }
        }
        (args, with_defaults)
    }

    pub fn to_python_arguments(&self) -> PythonArguments<R>
    where
        R: Clone,
    {
        let Arguments {
            range,
            posonlyargs,
            args,
            vararg,
            kwonlyargs,
            kwarg,
        } = self;

        let mut pos_only = Vec::with_capacity(posonlyargs.len());
        let mut pos_args = Vec::with_capacity(args.len());
        let mut defaults = Vec::new();
        for arg in posonlyargs {
            let (arg, default) = arg.to_arg();
            if let Some(default) = default {
                defaults.push(*default);
            }
            pos_only.push(arg);
        }
        for arg in args {
            let (arg, default) = arg.to_arg();
            if let Some(default) = default {
                defaults.push(*default);
            }
            pos_args.push(arg);
        }

        let mut kw_only = Vec::with_capacity(kwonlyargs.len());
        let mut kw_defaults = Vec::new();
        for arg in kwonlyargs {
            let (arg, default) = arg.to_arg();
            if let Some(default) = default {
                kw_defaults.push(*default);
            }
            kw_only.push(arg);
        }

        PythonArguments {
            range: range.clone(),
            posonlyargs: pos_only,
            args: pos_args,
            defaults,
            vararg: vararg.clone(),
            kwonlyargs: kw_only,
            kw_defaults,
            kwarg: kwarg.clone(),
        }
    }

    pub fn into_python_arguments(self) -> PythonArguments<R> {
        let Arguments {
            range,
            posonlyargs,
            args,
            vararg,
            kwonlyargs,
            kwarg,
        } = self;

        let mut pos_only = Vec::with_capacity(posonlyargs.len());
        let mut pos_args = Vec::with_capacity(args.len());
        let mut defaults = Vec::new();
        for arg in posonlyargs {
            let (arg, default) = arg.into_arg();
            if let Some(default) = default {
                defaults.push(*default);
            }
            pos_only.push(arg);
        }
        for arg in args {
            let (arg, default) = arg.into_arg();
            if let Some(default) = default {
                defaults.push(*default);
            }
            pos_args.push(arg);
        }

        let mut kw_only = Vec::with_capacity(kwonlyargs.len());
        let mut kw_defaults = Vec::new();
        for arg in kwonlyargs {
            let (arg, default) = arg.into_arg();
            if let Some(default) = default {
                kw_defaults.push(*default);
            }
            kw_only.push(arg);
        }

        PythonArguments {
            range,
            posonlyargs: pos_only,
            args: pos_args,
            defaults,
            vararg,
            kwonlyargs: kw_only,
            kw_defaults,
            kwarg,
        }
    }
}

impl<R> PythonArguments<R> {
    pub fn into_arguments(self) -> Arguments<R>
    where
        R: Clone,
    {
        let PythonArguments {
            range,
            posonlyargs,
            args,
            defaults,
            vararg,
            kwonlyargs,
            kw_defaults,
            kwarg,
        } = self;

        let mut pos_only = Vec::with_capacity(posonlyargs.len());
        let mut pos_args = Vec::with_capacity(args.len());
        let args_len = posonlyargs.len() + args.len();
        // not optimal
        let mut defaults: Vec<_> = std::iter::repeat_with(|| None)
            .take(args_len - defaults.len())
            .chain(defaults.into_iter().map(Some))
            .collect();
        debug_assert_eq!(args_len, defaults.len());

        for (arg, default) in std::iter::zip(args, defaults.drain(posonlyargs.len()..)) {
            let arg = ArgWithDefault::from_arg(arg, default);
            pos_args.push(arg);
        }

        for (arg, default) in std::iter::zip(posonlyargs, defaults.drain(..)) {
            let arg = ArgWithDefault::from_arg(arg, default);
            pos_only.push(arg);
        }

        let mut kw_only = Vec::with_capacity(kwonlyargs.len());
        let kw_defaults: Vec<_> = std::iter::repeat_with(|| None)
            .take(kw_only.len().saturating_sub(kw_defaults.len()))
            .chain(kw_defaults.into_iter().map(Some))
            .collect();
        for (arg, default) in std::iter::zip(kwonlyargs, kw_defaults) {
            let arg = ArgWithDefault::from_arg(arg, default);
            kw_only.push(arg);
        }

        Arguments {
            range,
            posonlyargs: pos_only,
            args: pos_args,
            vararg,
            kwonlyargs: kw_only,
            kwarg,
        }
    }
}

impl<R> From<Arguments<R>> for PythonArguments<R> {
    fn from(arguments: Arguments<R>) -> Self {
        arguments.into_python_arguments()
    }
}

include!("gen/generic.rs");