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
344
345
346
347
348
349
//! Macros helpers in genco.

macro_rules! impl_lang_item {
    (
        impl LangItem<$lang:ty> for $ty:ident {
            $($item:item)*
        }
    ) => {
        impl crate::tokens::FormatInto<$lang> for $ty {
            fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                tokens.item(crate::tokens::Item::LangBox(self.into()));
            }
        }

        impl<'a> crate::tokens::FormatInto<$lang> for &'a $ty {
            fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                tokens.item(crate::tokens::Item::LangBox(self.into()));
            }
        }

        impl From<$ty> for crate::lang::LangBox<$lang> {
            fn from(value: $ty) -> Self {
                crate::lang::LangBox::from(Box::new(value) as Box<dyn crate::lang::LangItem<$lang>>)
            }
        }

        impl<'a> From<&'a $ty> for crate::lang::LangBox<$lang> {
            fn from(value: &'a $ty) -> Self {
                crate::lang::LangBox::from(Box::new(value.clone()) as Box<dyn crate::lang::LangItem<$lang>>)
            }
        }

        impl crate::lang::LangItem<$lang> for $ty {
            $($item)*

            fn __lang_item_as_any(&self) -> &dyn std::any::Any {
                self
            }

            fn __lang_item_clone(&self) -> Box<dyn crate::lang::LangItem<$lang>> {
                Box::new(self.clone())
            }

            fn __lang_item_eq(&self, other: &dyn crate::lang::LangItem<$lang>) -> bool {
                other
                    .__lang_item_as_any()
                    .downcast_ref::<Self>()
                    .map_or(false, |x| x == self)
            }
        }
    }
}

macro_rules! impl_variadic_type_args {
    ($args_vis:vis $args:ident, $type_trait:ident, $any_type:ident) => {
        /// Helper trait for things that can be turned into generic arguments.
        $args_vis trait $args {
            /// Convert the given type into a collection of arguments.
            fn into_args(self) -> Vec<$any_type>;
        }

        impl<T> $args for T
        where
            T: Into<$any_type>,
        {
            fn into_args(self) -> Vec<$any_type> {
                vec![self.into()]
            }
        }

        impl_variadic_type_args!(@args $args, $type_trait, $any_type, A => a);
        impl_variadic_type_args!(@args $args, $type_trait, $any_type, A => a, B => b);
        impl_variadic_type_args!(@args $args, $type_trait, $any_type, A => a, B => b, C => c);
        impl_variadic_type_args!(@args $args, $type_trait, $any_type, A => a, B => b, C => c, D => d);
        impl_variadic_type_args!(@args $args, $type_trait, $any_type, A => a, B => b, C => c, D => d, E => e);
    };

    (@args $args:ty, $ty:ident, $any_type:ident, $($ident:ident => $var:ident),*) => {
        impl<$($ident,)*> $args for ($($ident,)*) where $($ident: Into<$any_type>,)* {
            fn into_args(self) -> Vec<$any_type> {
                let ($($var,)*) = self;
                vec![$($var.into(),)*]
            }
        }
    };
}

macro_rules! impl_plain_variadic_args {
    ($args:ident, $ty:ident) => {
        /// Helper trait for things that can be turned into generic arguments.
        pub trait $args {
            /// Convert the given type into a collection of arguments.
            fn into_args(self) -> Vec<$ty>;
        }

        impl<T> $args for T
        where
            T: Into<$ty>,
        {
            fn into_args(self) -> Vec<$ty> {
                vec![self.into()]
            }
        }

        impl_plain_variadic_args!(@args $args, $ty, A => a);
        impl_plain_variadic_args!(@args $args, $ty, A => a, B => b);
        impl_plain_variadic_args!(@args $args, $ty, A => a, B => b, C => c);
        impl_plain_variadic_args!(@args $args, $ty, A => a, B => b, C => c, D => d);
        impl_plain_variadic_args!(@args $args, $ty, A => a, B => b, C => c, D => d, E => e);
    };

    (@args $args:ty, $ty:ident, $($ident:ident => $var:ident),*) => {
        impl<$($ident,)*> $args for ($($ident,)*) where $($ident: Into<$ty>,)* {
            fn into_args(self) -> Vec<$ty> {
                let ($($var,)*) = self;
                vec![$($var.into(),)*]
            }
        }
    };
}

macro_rules! impl_dynamic_types {
    ($lang:ty =>
        trait TypeTrait {
            $($type_trait_item:tt)*
        }

        $(
            $ty:ident {
                impl TypeTrait {
                    $($ty_item:tt)*
                }

                impl LangItem {
                    $($ty_lang_item_item:tt)*
                }
            }
        )*
    ) => {
        /// Trait implemented by all types
        pub trait TypeTrait: 'static + std::fmt::Debug + crate::lang::LangItem<$lang> {
            /// Coerce trait into an enum that can be used for type-specific operations
            fn as_enum(&self) -> AnyRef<'_>;

            $($type_trait_item)*
        }

        /// Private internals for the any type.
        #[derive(Clone)]
        enum AnyInner {
            $(
                #[doc = "Generated variant."]
                $ty(Box<$ty>),
            )*
        }

        #[derive(Clone)]
        #[doc = "Type that can contain any language type. Derefs to the type trait."]
        pub struct Any {
            inner: AnyInner,
        }

        $(
            impl From<$ty> for Any {
                fn from(value: $ty) -> Self {
                    Self {
                        inner: AnyInner::$ty(Box::new(value))
                    }
                }
            }
        )*

        impl crate::tokens::FormatInto<$lang> for Any {
            fn format_into(self, tokens: &mut $crate::Tokens<$lang>) {
                let value = match self.inner {
                    $(AnyInner::$ty(value) => value as Box<dyn LangItem<$lang>>,)*
                };

                tokens.item(crate::tokens::Item::LangBox(value.into()));
            }
        }

        impl<'a> crate::tokens::FormatInto<$lang> for &'a Any {
            fn format_into(self, tokens: &mut $crate::Tokens<$lang>) {
                let value = match &self.inner {
                    $(AnyInner::$ty(value) => Box::new(Clone::clone(&**value)) as Box<dyn LangItem<$lang>>,)*
                };

                tokens.item(crate::tokens::Item::LangBox(value.into()));
            }
        }

        impl std::ops::Deref for Any {
            type Target = dyn TypeTrait;

            fn deref(&self) -> &Self::Target {
                match &self.inner {
                    $(AnyInner::$ty(value) => &**value,)*
                }
            }
        }

        impl std::fmt::Debug for Any {
            fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
                self.as_enum().fmt(fmt)
            }
        }

        impl std::cmp::PartialEq for Any {
            fn eq(&self, other: &Self) -> bool {
                self.as_enum() == other.as_enum()
            }
        }

        impl std::cmp::Eq for Any {}

        impl std::hash::Hash for Any {
            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                self.as_enum().hash(state);
            }
        }

        impl std::cmp::PartialOrd for Any {
            fn partial_cmp(&self, other: &Any) -> Option<std::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl std::cmp::Ord for Any {
            fn cmp(&self, other: &Any) -> std::cmp::Ordering {
                self.as_enum().cmp(&other.as_enum())
            }
        }

        #[doc = "Enum that can be used for casting between variants of the same type"]
        #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
        pub enum AnyRef<'a> {
            $(
                #[doc = "Type variant"]
                $ty(&'a $ty),
            )*
        }

        $(
            impl TypeTrait for $ty {
                fn as_enum(&self) -> AnyRef<'_> {
                    AnyRef::$ty(self)
                }

                $($ty_item)*
            }

            impl crate::tokens::FormatInto<$lang> for $ty {
                fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                    tokens.item(crate::tokens::Item::LangBox(self.into()));
                }
            }

            impl<'a> crate::tokens::FormatInto<$lang> for &'a $ty {
                fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                    tokens.item(crate::tokens::Item::LangBox(self.into()));
                }
            }

            impl From<$ty> for crate::lang::LangBox<$lang> {
                fn from(value: $ty) -> Self {
                    crate::lang::LangBox::from(Box::new(value) as Box<dyn crate::lang::LangItem<$lang>>)
                }
            }

            impl<'a> From<&'a $ty> for crate::lang::LangBox<$lang> {
                fn from(value: &'a $ty) -> Self {
                    crate::lang::LangBox::from(Box::new(value.clone()) as Box<dyn crate::lang::LangItem<$lang>>)
                }
            }

            impl LangItem<$lang> for $ty {
                $($ty_lang_item_item)*

                fn __lang_item_as_any(&self) -> &dyn std::any::Any {
                    self
                }

                fn __lang_item_clone(&self) -> Box<dyn crate::lang::LangItem<$lang>> {
                    Box::new(self.clone())
                }

                fn __lang_item_eq(&self, other: &dyn crate::lang::LangItem<$lang>) -> bool {
                    other
                        .__lang_item_as_any()
                        .downcast_ref::<Self>()
                        .map_or(false, |x| x == self)
                }
            }
        )*

        impl_variadic_type_args!(pub Args, TypeTrait, Any);
    }
}

macro_rules! impl_modifier {
    ($(#[$meta:meta])* pub enum $name:ident<$lang:ty> {
        $(
            $(#[$variant_meta:meta])*
            $variant:ident => $value:expr,
        )*
    }) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub enum $name {
            $(
                $(#[$variant_meta])*
                $variant,
            )*
        }

        impl $name {
            /// Get the name of the modifier.
            pub fn name(&self) -> &'static str {
                match self {
                    $(Self::$variant => $value,)*
                }
            }
        }

        impl crate::tokens::FormatInto<$lang> for $name {
            fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                tokens.append(self.name());
            }
        }

        impl crate::tokens::FormatInto<$lang> for Vec<$name> {
            fn format_into(self, tokens: &mut crate::Tokens<$lang>) {
                use std::collections::BTreeSet;

                let mut it = self.into_iter().collect::<BTreeSet<_>>().into_iter();

                if let Some(modifier) = it.next() {
                    modifier.format_into(tokens);
                }

                for modifier in it {
                    tokens.space();
                    modifier.format_into(tokens);
                }
            }
        }
    }
}