3 // ignore-tidy-linelength
5 #![feature(proc_macro_quote)]
6 #![crate_type = "proc-macro"]
8 extern crate proc_macro;
9 use std::iter::FromIterator;
10 use std::str::FromStr;
14 pub fn custom_quote(input: TokenStream) -> TokenStream {
15 let mut tokens: Vec<_> = input.into_iter().collect();
16 assert_eq!(tokens.len(), 1, "Unexpected input: {:?}", tokens);
18 Some(TokenTree::Ident(ident)) => {
19 assert_eq!(ident.to_string(), "my_ident");
21 let proc_macro_crate = TokenStream::from_str("::proc_macro").unwrap();
22 let quoted_span = proc_macro::quote_span(proc_macro_crate, ident.span());
23 let prefix = TokenStream::from_str(r#"let mut ident = proc_macro::Ident::new("my_ident", proc_macro::Span::call_site());"#).unwrap();
24 let set_span_method = TokenStream::from_str("ident.set_span").unwrap();
25 let set_span_arg = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Parenthesis, quoted_span)));
26 let suffix = TokenStream::from_str(";proc_macro::TokenStream::from(proc_macro::TokenTree::Ident(ident))").unwrap();
27 let full_stream = TokenStream::from_iter([prefix, set_span_method, set_span_arg, suffix]);