rsmicro / kproc-macros

(proposal linux kernel) minimal procedural macros parser that produce a convenient AST by including only the necessary code
https://docs.rs/crate/kproc-parser
GNU General Public License v2.0
0 stars 0 forks source link

add an editor proc macros that allow to write the new code in a more easy way #5

Open vincenzopalazzo opened 1 year ago

vincenzopalazzo commented 1 year ago

quote is a great example of how we can use procedural macros to write code, and I would like to make some experiments to make something similar, but simpler that reflects the way that we format a string. Currently, we can do with kparser

  let code = format!(
        "impl{} {}{} {{ \
                    fn get_{}(&self) -> {} {{ \
                       return self.{}.clone()\
                    }}\
                }}",
        gen, name, gen, attributes[0].name, attributes[0].ty, attributes[0].name,
    );

The idea is to use procedural macros that write this format for us, like:

editor! {
        impl ${gen} ${name}${gen} {
              fn get_${attributes[0].name}(&self) -> ${attributes[0].ty} { 
                   return self.${attributes[0].name}.clone()
                }
          }
}

this is more Makefile syntax and you can inject commands like:

editor!{
   @foreach ${attributes} { 
     println!("{}", ${ir});
   }
}
vincenzopalazzo commented 1 year ago

we should look also at the quote present in the nightly version https://doc.rust-lang.org/proc_macro/fn.quote.html