2020-04-01 05:11:26 +00:00
|
|
|
//! RA Proc Macro Server
|
|
|
|
//!
|
|
|
|
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
|
|
|
|
//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander.
|
|
|
|
//!
|
2020-04-20 18:26:10 +00:00
|
|
|
//! But we adapt it to better fit RA needs:
|
2020-04-01 05:11:26 +00:00
|
|
|
//!
|
2020-04-20 18:26:10 +00:00
|
|
|
//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipulate and interact with
|
|
|
|
//! RA than `proc-macro2` token stream.
|
2020-04-01 05:11:26 +00:00
|
|
|
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
|
|
|
|
//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)
|
|
|
|
|
2020-04-04 08:04:01 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[doc(hidden)]
|
|
|
|
mod proc_macro;
|
|
|
|
|
2020-04-04 08:07:22 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
mod rustc_server;
|
|
|
|
|
2020-04-04 08:09:36 +00:00
|
|
|
mod dylib;
|
|
|
|
|
2020-04-04 08:28:32 +00:00
|
|
|
use proc_macro::bridge::client::TokenStream;
|
2020-04-01 05:11:26 +00:00
|
|
|
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
|
2020-04-20 18:26:10 +00:00
|
|
|
use std::path::Path;
|
2020-04-01 05:11:26 +00:00
|
|
|
|
2020-04-16 13:13:57 +00:00
|
|
|
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
|
2020-04-20 18:26:10 +00:00
|
|
|
let expander = create_expander(&task.lib);
|
2020-04-04 08:10:45 +00:00
|
|
|
|
|
|
|
match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
|
|
|
|
Ok(expansion) => Ok(ExpansionResult { expansion }),
|
|
|
|
Err(msg) => {
|
2020-04-20 18:26:10 +00:00
|
|
|
Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg))
|
2020-04-04 08:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 05:11:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 18:26:10 +00:00
|
|
|
pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult {
|
|
|
|
let expander = create_expander(&task.lib);
|
2020-04-04 08:10:45 +00:00
|
|
|
|
2020-04-20 18:26:10 +00:00
|
|
|
ListMacrosResult { macros: expander.list_macros() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_expander(lib: &Path) -> dylib::Expander {
|
|
|
|
dylib::Expander::new(lib)
|
2020-04-20 18:54:43 +00:00
|
|
|
.unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err))
|
2020-04-01 05:11:26 +00:00
|
|
|
}
|
2020-04-09 17:43:47 +00:00
|
|
|
|
2020-04-16 13:13:57 +00:00
|
|
|
pub mod cli;
|
|
|
|
|
2020-04-09 17:43:47 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|