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
#![no_std]

//! Implementation crate of macros that were built with the [`proc_macro_hack`] until procedural
//! macros in expression context got stabilized.

extern crate alloc;

use core::fmt::Write;
use proc_macro::TokenStream;
use unicode_xid::UnicodeXID;

use alloc::string::{String, ToString};

#[proc_macro]
pub fn vector(input: TokenStream) -> TokenStream {
    // code expanded after the contents of the array
    static CODE: &str = "]);\
let mut temp_vec = Vector::with_capacity(arr.len());\
core::ptr::copy_nonoverlapping(arr.as_ptr(), temp_vec.as_mut_ptr(), arr.len());\
temp_vec.set_len(arr.len());\
temp_vec\
}";

    let input = input.to_string();
    let input_t = input.trim();

    let mut delimiters_count: isize = 0;
    let mut expansion = String::with_capacity(512);

    if let Some(i) = input_t.find(|c: char| {
        if c == '[' || c == '{' || c == '(' || c == '<' {
            delimiters_count += 1;
            return false;
        }

        if c == ']' || c == '}' || c == ')' || c == '>' {
            delimiters_count -= 1;
            return false;
        }

        delimiters_count == 0 && c == ';'
    }) {
        let mut elem = input_t[..i].trim();
        let y = input_t[i + 1..].trim();
        let x = y.parse::<usize>();
        let t = elem.rfind(|c: char| {
        if c == '[' || c == '{' || c == '(' || c == '<' {
            delimiters_count += 1;
            return false;
        }

        if c == ']' || c == '}' || c == ')' || c == '>' {
            delimiters_count -= 1;
            return false;
        }

        delimiters_count == 0 && c == ':'
    }).map(|e| { let t = &elem[e + 1..]; elem = &elem[..e]; t});
    let is_ident = is_xid_ident(elem);

        match x {
            Ok(wheels) if wheels > 0 => {

    expansion.push_str(
        "unsafe {\
let arr = core::mem::ManuallyDrop::new([",
    );

                // this checks if the element it's not an identifier actually this is pretty is strict and
                // does not take in count macros as `concat_idents`,at the time there's no other standard solution
                // to return identifiers so this is fine.
                if is_ident
                {
                    expansion.reserve_exact((wheels * (elem.len() + 9)).next_power_of_two());

                    for _ in 1..wheels {
                        write!(expansion, "{}.clone(),", elem).unwrap()
                    }
                } else {
                    expansion.reserve_exact((wheels * (elem.len() + 1)).next_power_of_two());

                    for _ in 1..wheels {
                        write!(expansion, "{},", elem).unwrap()
                    }
                }
                expansion.push_str(elem);

        match t {
            Some(e) => write!(
                expansion,
                "]);\
let mut temp_vec = <Vector<{}>>::with_capacity(arr.len());\
core::ptr::copy_nonoverlapping(arr.as_ptr(),temp_vec.as_mut_ptr(),arr.len());\
temp_vec.set_len(arr.len());\
temp_vec\
}}",
                e
            )
            .unwrap(),
            None => expansion.push_str(CODE),
        }
            }
            Err(_) =>  match t {
            Some(e) => write!(
                expansion,
                "{{\
let mut temp_vec = <Vector<{0}>>::with_capacity({1});\
for _ in 1..{1} {{ temp_vec.push({2}{3}) }}\
temp_vec.push({2});\
temp_vec\
}}",
                e, y, elem, if is_ident { ".clone()" } else { "" }
            )
            .unwrap(),
            None => write!(
                expansion,
                "{{\
let mut temp_vec = Vector::with_capacity({0});\
for _ in 1..{0} {{ temp_vec.push({1}{2}) }}\
temp_vec.push({1});\
temp_vec\
}}",
                y, elem, if is_ident { ".clone()" } else { "" }
            )
            .unwrap(),
        }
            _ => (),
        }
    } else {

    expansion.push_str(
        "unsafe {\
let arr = core::mem::ManuallyDrop::new([",
    );
        let mut it = input_t
            .split(|c: char| {
                if c == '[' || c == '{' || c == '(' || c == '<' {
                    delimiters_count += 1;
                    return false;
                }

                if c == ']' || c == '}' || c == ')' || c == '>' {
                    delimiters_count -= 1;
                    return false;
                }

                delimiters_count == 0 && c == ','
            })
            .filter_map(|mut e| {
                e = e.trim();
                if e == "" {
                    None
                } else {
                    Some(e)
                }
            });

        let mut last_elem = it.next_back().unwrap_or("");

        let mut delimiters_count_: isize = 0;

        let t = last_elem
            .rfind(|c: char| {
                if c == '[' || c == '{' || c == '(' || c == '<' {
                    delimiters_count_ += 1;
                    return false;
                }

                if c == ']' || c == '}' || c == ')' || c == '>' {
                    delimiters_count_ -= 1;
                    return false;
                }

                delimiters_count_ == 0 && c == ':'
            })
            .map(|e| {
                let t = &last_elem[e + 1..];
                last_elem = &last_elem[..e];
                t
            });

        for e in it {
            write!(expansion, "{},", e).unwrap();
        }

        expansion.push_str(last_elem);

        match t {
            Some(e) => write!(
                expansion,
                "]);\
let mut temp_vec = <Vector<{}>>::with_capacity(arr.len());\
core::ptr::copy_nonoverlapping(arr.as_ptr(),temp_vec.as_mut_ptr(),arr.len());\
temp_vec.set_len(arr.len());\
temp_vec\
}}",
                e
            )
            .unwrap(),
            None => expansion.push_str(CODE),
        }
    }

    expansion.parse().unwrap()
}

fn is_xid_ident(elem: &str) -> bool {
    let mut it = elem.chars();

    match it.next() {
        Some(c) => if !c.is_xid_start() { return false },
        None => return false,
    }

    for c in it {
        if !c.is_xid_continue() {
            return false; 
        }
    } 

    true
}