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
//!  //! //! This crate is meant to be a complement to [quote]. Where as [quote] does quasi-quote interpolations at //! compile-time, this crate does them at run-time. This is handy for macros receiving templates from client code with //! markers to be replaced when the macro is run. //! //! [quote]: https://github.com/dtolnay/quote //! //! # Examples //! ``` //! use proc_macro2::TokenStream; //! use tokenstream2-tmpl::interpolate; //! use quote::ToTokens; //! use std::collections::HashMap; //! use syn::{Ident, parse_str}; //! //! let input: TokenStream = parse_str("let NAME: int = 5;")?; //! let expected: TokenStream = parse_str("let age: int = 5;")?; //! //! let mut replacements: HashMap<&str, &dyn ToTokens> = HashMap::new(); //! let ident = parse_str::<Ident>("age")?; //! replacements.insert("NAME", &ident); //! //! let output = interpolate(input, &replacements); //! assert_eq!( //! format!("{}", output), //! format!("{}", expected) //! ); //! //! # Ok::<(), Box<dyn std::error::Error>>(()) //! ``` //! //! Here `input` might be some input to a macro that functions as a template. [quote] would have tried to expand `NAME` //! at the macro's compile-time. [tokenstream2-tmpl] will expand it at the macro's run-time. //! //! [tokenstream2-tmpl]: https://gitlab.com/chesedo/tokenstream2-tmpl //! //! ``` //! extern crate proc_macro; //! use proc_macro2::TokenStream; //! use std::collections::HashMap; //! use syn::{Ident, parse::{Parse, ParseStream, Result}, parse_macro_input, punctuated::Punctuated, Token}; //! use tokenstream2-tmpl::{Interpolate, interpolate}; //! use quote::ToTokens; //! //! /// Create a token for macro using [syn](syn) //! /// Type that holds a key and the value it maps to. //! /// An acceptable stream will have the following form: //! /// ```text //! /// key => value //! /// ``` //! struct KeyValue { //! pub key: Ident, //! pub arrow_token: Token![=>], //! pub value: Ident, //! } //! //! /// Make KeyValue parsable from a token stream //! impl Parse for KeyValue { //! fn parse(input: ParseStream) -> Result<Self> { //! Ok(KeyValue { //! key: input.parse()?, //! arrow_token: input.parse()?, //! value: input.parse()?, //! }) //! } //! } //! //! /// Make KeyValue interpolatible //! impl Interpolate for KeyValue { //! fn interpolate(&self, stream: TokenStream) -> TokenStream { //! let mut replacements: HashMap<_, &dyn ToTokens> = HashMap::new(); //! //! // Replace each "KEY" with the key //! replacements.insert("KEY", &self.key); //! //! // Replace each "VALUE" with the value //! replacements.insert("VALUE", &self.value); //! //! interpolate(stream, &replacements) //! } //! } //! //! /// Macro to take a list of key-values with a template to expand each key-value //! # const IGNORE: &str = stringify! { //! #[proc_macro_attribute] //! # }; //! pub fn map(tokens: proc_macro::TokenStream, template: proc_macro::TokenStream) -> proc_macro::TokenStream { //! // Parse a comma separated list of key-values //! let maps = //! parse_macro_input!(tokens with Punctuated::<KeyValue, Token![,]>::parse_terminated); //! //! maps.interpolate(template.into()).into() //! } //! //! pub fn main() { //! # const IGNORE: &str = stringify! { //! #[map( //! usize => 10, //! isize => -2, //! bool => false, //! )] //! let _: KEY = VALUE; //! # }; //! // Output: //! // let _: usize = 10; //! // let _: isize = -2; //! // let _: bool = false; //! } //! ``` use proc_macro2::{Group, TokenStream, TokenTree}; use quote::{ToTokens, TokenStreamExt}; use std::collections::HashMap; use syn::punctuated::Punctuated; /// Trait for tokens that can replace interpolation markers pub trait Interpolate { /// Take a token stream and replace interpolation markers with their actual values into a new stream /// using [interpolate](interpolate) fn interpolate(&self, stream: TokenStream) -> TokenStream; } /// Make a Punctuated list interpolatible if it holds interpolatible types impl<T: Interpolate, P> Interpolate for Punctuated<T, P> { fn interpolate(&self, stream: TokenStream) -> TokenStream { self.iter() .fold(TokenStream::new(), |mut implementations, t| { implementations.extend(t.interpolate(stream.clone())); implementations }) } } /// Replace the interpolation markers in a token stream with a specific text. /// See this [crate's](crate) documentation for an example on how to use this. pub fn interpolate( stream: TokenStream, replacements: &HashMap<&str, &dyn ToTokens>, ) -> TokenStream { let mut new = TokenStream::new(); // Loop over each token in the stream // `Literal`, `Punct`, and `Group` are kept as is for token in stream.into_iter() { match token { TokenTree::Literal(literal) => new.append(literal), TokenTree::Punct(punct) => new.append(punct), TokenTree::Group(group) => { // Recursively interpolate the stream in group let mut new_group = Group::new(group.delimiter(), interpolate(group.stream(), replacements)); new_group.set_span(group.span()); new.append(new_group); } TokenTree::Ident(ident) => { let ident_str: &str = &ident.to_string(); // Check if identifier is in the replacement set if let Some(value) = replacements.get(ident_str) { // Replace with replacement value value.to_tokens(&mut new); continue; } // Identifier did not match, so copy as is new.append(ident); } } } new } #[cfg(test)] mod tests { use super::*; use pretty_assertions::assert_eq; use quote::quote; use syn::{parse_str, Ident, Token, Type}; type Result = std::result::Result<(), Box<dyn std::error::Error>>; #[test] fn complete_replacements() -> Result { let input = quote! { let VAR: TRAIT = if true { CONCRETE{} } else { Alternative{} } }; let expected = quote! { let var: abstract_type = if true { concrete{} } else { Alternative{} } }; let mut r: HashMap<&str, &dyn ToTokens> = HashMap::new(); let v: Ident = parse_str("var")?; let a: Type = parse_str("abstract_type")?; let c: Type = parse_str("concrete")?; r.insert("VAR", &v); r.insert("TRAIT", &a); r.insert("CONCRETE", &c); assert_eq!( format!("{}", &interpolate(input, &r)), format!("{}", expected) ); Ok(()) } /// Partial replacements should preverse the uninterpolated identifiers #[test] fn partial_replacements() -> Result { let input: TokenStream = parse_str("let a: TRAIT = OTHER;")?; let expected: TokenStream = parse_str("let a: Display = OTHER;")?; let mut r: HashMap<&str, &dyn ToTokens> = HashMap::new(); let t: Type = parse_str("Display")?; r.insert("TRAIT", &t); assert_eq!( format!("{}", interpolate(input, &r)), format!("{}", expected) ); Ok(()) } /// Test the interpolation of Punctuated items #[test] fn interpolate_on_punctuated() -> Result { pub struct TraitSpecifier { pub abstract_trait: Type, pub arrow_token: Token![=>], pub concrete: Type, } /// Make TraitSpecifier interpolatible impl Interpolate for TraitSpecifier { fn interpolate(&self, stream: TokenStream) -> TokenStream { let mut replacements: HashMap<_, &dyn ToTokens> = HashMap::new(); // Replace each "TRAIT" with the absract trait replacements.insert("TRAIT", &self.abstract_trait); // Replace each "CONCRETE" with the concrete type replacements.insert("CONCRETE", &self.concrete); interpolate(stream, &replacements) } } let mut traits: Punctuated<TraitSpecifier, Token![,]> = Punctuated::new(); traits.push(TraitSpecifier { abstract_trait: parse_str("IButton")?, arrow_token: Default::default(), concrete: parse_str("BigButton")?, }); traits.push(TraitSpecifier { abstract_trait: parse_str("IWindow")?, arrow_token: Default::default(), concrete: parse_str("MinimalWindow")?, }); let input = quote! { let _: TRAIT = CONCRETE{}; }; let expected = quote! { let _: IButton = BigButton{}; let _: IWindow = MinimalWindow{}; }; assert_eq!( format!("{}", traits.interpolate(input)), format!("{}", expected) ); Ok(()) } }