quick arrays
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
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use syn::{
    Error, Expr, Lit, Pat, PatConst, PatWild, Stmt, Token,
    parse::{self, Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    spanned::Spanned,
};

#[derive(Clone)]
struct Index {
    indices: Indices,
    value: Expr,
}
#[derive(Clone)]
enum Indices {
    Normal(Vec<Expr>),
    Wild(PatWild),
}
fn indices(index: &Pat) -> syn::Result<Indices> {
    match index {
        Pat::Lit(v) => match &v.lit {
            Lit::Int(_) => Ok(vec![v.clone().into()]),
            _ => Err(Error::new_spanned(v, "must be numeric literal"))?,
        },
        Pat::Or(v) => v
            .cases
            .iter()
            .map(indices)
            .map(|x| {
                x.and_then(|x| match x {
                    Indices::Normal(x) => Ok(x),
                    Indices::Wild(x) => Err(Error::new_spanned(x, "cant have _ in |")),
                })
            })
            .flatten_ok()
            .collect(),
        Pat::Range(r) => {
            let s = r.span();
            let r = r.clone();
            let begin = match *r.start.ok_or(Error::new(s, "range must be bounded"))? {
                Expr::Lit(v) => match v.lit {
                    Lit::Int(v) => v.base10_parse()?,
                    _ => Err(Error::new_spanned(
                        v,
                        "range start bound must be integer literal",
                    ))?,
                },
                e => Err(Error::new_spanned(
                    e,
                    "range start bound must include only literal ints",
                ))?,
            };
            let end = match *r.end.ok_or(Error::new(s, "range must be bounded"))? {
                Expr::Lit(v) => match v.lit {
                    Lit::Int(v) => v.base10_parse()?,
                    _ => Err(Error::new_spanned(
                        v,
                        "range end bound must be integer literal",
                    ))?,
                },
                e => Err(Error::new_spanned(
                    e,
                    "range end bound must include only literal ints",
                ))?,
            };

            match r.limits {
                syn::RangeLimits::Closed(..) => Ok((begin..=end)
                    .map(|x: usize| syn::parse::<Expr>(x.to_token_stream().into()).unwrap())
                    .collect()),
                syn::RangeLimits::HalfOpen(..) => Ok((begin..end)
                    .map(|x: usize| syn::parse::<Expr>(x.to_token_stream().into()).unwrap())
                    .collect()),
            }
        }
        Pat::Const(PatConst { block, .. }) => {
            Ok(vec![if let [Stmt::Expr(x, None)] = &block.stmts[..] {
                x.clone()
            } else {
                Expr::Block(syn::ExprBlock {
                    attrs: vec![],
                    label: None,
                    block: block.clone(),
                })
            }])
        }
        Pat::Wild(x) => return Ok(Indices::Wild(x.clone())),
        _ => Err(Error::new(
            index.span(),
            "pattern must be literal(5) | or(5 | 4) | range(4..5) | const { .. } | _",
        ))?,
    }
    .map(Indices::Normal)
}

impl Parse for Index {
    fn parse(input: ParseStream<'_>) -> parse::Result<Index> {
        let index = Pat::parse_multi(input)?;
        let indices = indices(&index)?;
        input.parse::<Token![=>]>()?;
        Ok(Index {
            indices,
            value: input.parse()?,
        })
    }
}

struct Map(Punctuated<Index, Token![,]>);
impl Parse for Map {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let parsed = Punctuated::<Index, Token![,]>::parse_terminated(input)?;
        if parsed.is_empty() {
            return Err(input.error("no keys"));
        }
        Ok(Map(parsed))
    }
}

impl Map {
    fn into(self, d: TokenStream, f: impl Fn(&Expr) -> TokenStream + Copy) -> TokenStream {
        let wild = self.0.iter().find_map(|x| match x.indices {
            Indices::Normal(_) => None,
            Indices::Wild(_) => Some(x.value.to_token_stream()),
        });
        let w = wild.is_some();
        let map = self
            .0
            .into_iter()
            .zip(1..)
            .flat_map(|(Index { indices, value }, i)| {
                match indices {
                    Indices::Normal(x) => x,
                    _ => vec![],
                }
                .into_iter()
                .map({
                    move |x| {
                        let s = format!(
                            "duplicate / overlapping key @ pattern `{}` (#{i})",
                            x.to_token_stream()
                                .to_string()
                                .replace('{', "{{")
                                .replace('}', "}}")
                        );
                        let value = if w {
                            value.to_token_stream()
                        } else {
                            f(&value)
                        };
                        quote! {{
                            let (index, value) = { let (__ඞඞ, __set) = ((), ()); (#x, #value) };
                            assert!(!__set[index], #s);
                            __set[index] = true;
                            __ඞඞ[index] = value;
                        }}
                    }
                })
            });
        let d = wild.unwrap_or(d);
        quote! {{
            let mut __ඞඞ = [#d; _];
            const fn steal<const N:usize, T>(_: &[T; N]) -> [bool; N] { [false; N] }
            let mut __set = steal(&__ඞඞ);
            #(#map)*
            __ඞඞ
        }}
    }
}

/// Easily make a `[Option<T>; N]`
///
/// ```
/// # use amap::amap;
/// #[derive(Debug, PartialEq)]
/// enum Y {
///     A,
///     B,
///     C,
///     D,
/// }
/// static X: [Option<Y>; 46] = amap! {
///     2..=25 => Y::A,
///     26 | 32 => Y::C,
///     27..32 => Y::D,
///     44 => Y::B,
/// };
/// assert_eq!(X[44].as_ref().unwrap(), &Y::B);
/// ```
///
/// Produces a `[T; N]` if a `_` branch is included.
#[proc_macro]
pub fn amap(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    parse_macro_input!(input as Map)
        .into(quote! { const { None } }, |x| quote! { Some(#x)})
        .into()
}

#[proc_macro]
/// This method uses default instead of Option<T>. Nightly required for use in const.
/// ```
/// # use amap::amap_d;
/// let x: [u8; 42] = amap_d! {
///     4 => 2,
///     16..25 => 4,
/// };
/// assert_eq!(x[17], 4);
/// ```
pub fn amap_d(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    parse_macro_input!(input as Map)
        .into(
            quote! { ::core::default::Default::default() },
            |x| quote! { #x },
        )
        .into()
}