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
#![forbid(unsafe_code)]
#![allow(nonstandard_style, unused_imports)]

/// For compatibility with older versions of Rust.
extern crate proc_macro;

use ::proc_macro::{
    TokenStream,
};
use ::proc_macro2::{
    Group,
    Span,
    TokenStream as TokenStream2,
};
use ::quote::{
    quote,
    quote_spanned,
    ToTokens,
};
use ::syn::{*,
    parse::{
        Parse,
        Parser,
        ParseStream,
    },
    punctuated::Punctuated,
    spanned::Spanned,
    Result,
};

use self::builtin_macros as builtin;

use compile_time_string::CompileTimeString;
mod compile_time_string;

supported_builtin_macros![
    concat!(...),
    concat_idents!(...),
    env!(...),
    option_env!(...),
    include_from_root!(...),
    include_bytes_from_root!(...),
    include_str_from_root!(...),
    stringify!(...),
];

/// Pre-processor (pattern) with hard-coded implementations for some of the
/// built-in macros, so as to be able to chain their result with custom macros.
///
/// By using a pre-processor, this also enables "using" macros where it is
/// otherwise forbidden.
///
/// For instance, the following code fails to compile:
///
/// ```rust,compile_fail
/// fn concat_idents!(f, o, o) ()
/// {}
///
/// fn main ()
/// {
///     foo();
/// }
/// ```
///
/// But the following call does not:
///
/// ```rust
/// # extern crate with_builtin_macros_proc_macros as with_builtin_macros;
/// use ::with_builtin_macros::with_builtin;
///
/// with_builtin!(let $fname = concat_idents!(f, o, o) in {
///     fn $fname ()
///     {}
/// });
///
/// fn main ()
/// {
///     foo();
/// }
/// ```
///
///   - Note: for this behavior, with some added neat features (such as case
///     conversion), see the [`::paste::paste!`](https://docs.rs/paste) macro.
///
/// Even if we forget about allowed macro call-sites, there is also the issue
/// of macro evaluation order leading to things like the following not
/// working:
///
/// ```rust,compile_fail
/// /// Some (proc-)macro that expects a string literal containing a hexadecimal
/// /// sequence of digits, that it decodes into the represented byte sequence.
/// use ::hex_literal::hex;
///
/// const BYTES: &[u8] = hex!(include_str!(concat!(
///     env!("CARGO_MANIFEST_DIR"), "/", "data.hex",
/// )));
/// ```
///
/// With [`with_builtin!`], however, the following works:
///
/// ```rust
/// # macro_rules! ignore {($($t:tt)*) => ()} ignore! {
/// /// Some (proc-)macro that expects a string literal containing a hexadecimal
/// /// sequence of digits, that it decodes into the represented byte sequence.
/// use ::hex_literal::hex;
/// use ::with_builtin_macros::with_builtin;
///
/// const BYTES: &[u8] = {
///     with_builtin!(let $hex_str = include_str_from_root!("data.hex") in {
///         hex!($hex_str)
///     })
/// };
/// # } fn main () {}
/// ```
#[proc_macro] pub
fn with_builtin (input: TokenStream)
  -> TokenStream
{
    let Input {
        metavar,
        macro_invocation: (builtin_macro, macro_input),
        template,
    } = parse_macro_input!(input);
    let input_span = macro_input.span();
    let expansion = match builtin_macro.call_on(macro_input) {
        | Ok(it) => it,
        | Err(mut err) => {
            if format!("{:?}", err.span()) == format!("{:?}", Span::call_site()) {
                err = Error::new(input_span, &err.to_string());
            }
            return err.to_compile_error().into();
        },
    };
    map_replace(&metavar, &expansion, template)
        .into()
}

struct Input {
    metavar: Ident,
    macro_invocation: (BuiltinMacro, TokenStream2),
    template: TokenStream2,
}

impl Parse for Input {
    fn parse (input: ParseStream<'_>)
      -> Result<Self>
    {
        let _:             Token![ let ]        = input.parse()?;
        let _:             Token![ $   ]        = input.parse()?;
        let metavar:               Ident        = input.parse()?;
        let _:             Token![ =   ]        = input.parse()?;
        let builtin_macro:         BuiltinMacro = input.parse()?;
        let _:             Token![ !   ]        = input.parse()?;
        let macro_input = { let g: Group        = input.parse()?; g.stream() };
        let _:             Token![ in  ]        = input.parse()?;
        let template;              braced!(template in input);
        Ok(Input {
            metavar,
            macro_invocation: (builtin_macro, macro_input),
            template: template.parse()?,
        })
    }
}

macro_rules! supported_builtin_macros {(
    $(
        $( #[cfg $cfg:tt] )?
        $macro_name:ident !(...)
    ),+ $(,)?
) => (
    mod kw {
        $(
            $( #[cfg $cfg] )?
            ::syn::custom_keyword!($macro_name);
        )+
    }

    mod builtin_macros {
        use super::*;

        $(
            $( #[cfg $cfg] )?
            pub
            mod $macro_name;
        )+
    }

    enum BuiltinMacro {
        $(
            $( #[cfg $cfg] )?
            $macro_name(kw::$macro_name),
        )*
    }

    impl Parse for BuiltinMacro {
        fn parse (input: ParseStream<'_>)
          -> Result<Self>
        {
            let lookahead = input.lookahead1();
            match () {
                $(
                    $( #[cfg $cfg] )?
                    _case if lookahead.peek(kw::$macro_name) => {
                        input
                            .parse::<kw::$macro_name>()
                            .map(Self::$macro_name)
                    },
                )*
                _default => Err(lookahead.error()),
            }
        }
    }

    impl Spanned for BuiltinMacro {
        fn span (self: &'_ Self)
          -> Span
        {
            match *self {
                $(
                    $( #[cfg $cfg] )?
                    | Self::$macro_name(ref it) => it.span(),
                )+
            }
        }
    }

    impl BuiltinMacro {
        fn call_on (self: &'_ Self, args: TokenStream2)
          -> Result<TokenStream2>
        {
            match *self {
                $(
                    $( #[cfg $cfg] )?
                    | Self::$macro_name(_) => {
                        builtin::$macro_name::call_on(args)
                            .map(|it| it.into_token_stream())
                    },
                )+
            }
        }
    }
)}
use supported_builtin_macros;

trait MockToTokens {
    fn into_token_stream (self: Self)
      -> TokenStream2
    ;

    const _impl: () = {
        impl MockToTokens for Vec<u8> {
            fn into_token_stream (self: Vec<u8>)
              -> TokenStream2
            {
                let each_byte = &self;
                match self.len() {
                    | 0 => quote!(
                        [0_u8; 0]
                    ),
                    | _ => quote!(
                        [
                            #(#each_byte),*
                        ]
                    ),
                }
            }
        }
    };
}

fn map_replace (
    metavar: &'_ Ident,
    tokenized: &'_ TokenStream2,
    template: TokenStream2,
) -> TokenStream2
{
    use ::proc_macro2::{*, TokenTree as TT};
    let mut tokens = template.into_iter().peekable();
    let mut ret = TokenStream2::new();
    loop {
        match (tokens.next(), tokens.peek()) {
            | (
                Some(TT::Punct(dollar)),
                Some(&TT::Ident(ref ident)),
            )
                if  dollar.as_char() == '$'
                &&  ident == metavar
            => {
                drop(tokens.next());
                ret.extend(tokenized.clone());
            },

            | (Some(TT::Group(group)), _) => {
                ret.extend(Some(TT::Group(Group::new(
                    group.delimiter(),
                    map_replace(metavar, tokenized, group.stream()),
                ))));
            },

            | (None, _) => break,

            | (tt, _) => ret.extend(tt),
        }
    }
    ret
}