Implement ra_proc_macro client logic

This commit is contained in:
Edwin Cheng 2020-03-27 04:26:34 +08:00
parent fa3c7742af
commit 503cbd3f4b
11 changed files with 827 additions and 23 deletions

View file

@ -9,6 +9,15 @@ pub struct ProcMacroExpander {
proc_macro_id: ProcMacroId,
}
macro_rules! err {
($fmt:literal, $($tt:tt),*) => {
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*)))
};
($fmt:literal) => {
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string()))
}
}
impl ProcMacroExpander {
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander {
ProcMacroExpander { krate, proc_macro_id }
@ -25,8 +34,24 @@ impl ProcMacroExpander {
.proc_macro
.get(self.proc_macro_id.0 as usize)
.clone()
.ok_or_else(|| mbe::ExpandError::ConversionError)?;
.ok_or_else(|| err!("No derive macro found."))?;
let tt = remove_derive_atr(tt, &proc_macro.name)
.ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
}
}
fn remove_derive_atr(tt: &tt::Subtree, _name: &str) -> Option<tt::Subtree> {
// FIXME: proper handle the remove derive
// We assume the first 2 tokens are #[derive(name)]
if tt.token_trees.len() > 2 {
let mut tt = tt.clone();
tt.token_trees.remove(0);
tt.token_trees.remove(0);
return Some(tt);
}
None
}