From 84fb9b44c3b06e1a542608570bb6d7212c8505b7 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 1 Apr 2020 13:11:26 +0800 Subject: Introduce ra_proc_macro_srv --- crates/ra_proc_macro_srv/Cargo.toml | 24 ++++++++++++++++ crates/ra_proc_macro_srv/src/lib.rs | 21 ++++++++++++++ crates/ra_proc_macro_srv/src/main.rs | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 crates/ra_proc_macro_srv/Cargo.toml create mode 100644 crates/ra_proc_macro_srv/src/lib.rs create mode 100644 crates/ra_proc_macro_srv/src/main.rs (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml new file mode 100644 index 000000000..fa2de2c91 --- /dev/null +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -0,0 +1,24 @@ +[package] +edition = "2018" +name = "ra_proc_macro_srv" +version = "0.1.0" +authors = ["rust-analyzer developers"] +publish = false + +[lib] +doctest = false + +[dependencies] +ra_tt = { path = "../ra_tt" } +ra_mbe = { path = "../ra_mbe" } +ra_proc_macro = { path = "../ra_proc_macro" } + +serde_derive = "1.0.104" +serde = "1.0.104" +serde_json = "1.0.48" +libloading = "0.5.2" +goblin = "0.2.0" + +[dev-dependencies] +cargo_metadata = "0.9.1" +difference = "2.0.0" diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs new file mode 100644 index 000000000..f77be1475 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -0,0 +1,21 @@ +//! 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. +//! +//! But we change some several design for fitting RA needs: +//! +//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with +//! RA then proc-macro2 token stream. +//! * 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) + +use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; + +pub fn expand_task(_task: &ExpansionTask) -> Result { + unimplemented!() +} + +pub fn list_macros(_task: &ListMacrosTask) -> Result { + unimplemented!() +} diff --git a/crates/ra_proc_macro_srv/src/main.rs b/crates/ra_proc_macro_srv/src/main.rs new file mode 100644 index 000000000..54ce0224d --- /dev/null +++ b/crates/ra_proc_macro_srv/src/main.rs @@ -0,0 +1,53 @@ +use ra_proc_macro::msg::{self, Message}; +use ra_proc_macro_srv::{expand_task, list_macros}; + +use std::io; + +fn read_request() -> Result, io::Error> { + let stdin = io::stdin(); + let mut stdin = stdin.lock(); + msg::Request::read(&mut stdin) +} + +fn write_response(res: Result) -> Result<(), io::Error> { + let msg: msg::Response = match res { + Ok(res) => res, + Err(err) => msg::Response::Error(msg::ResponseError { + code: msg::ErrorCode::ExpansionError, + message: err, + }), + }; + + let stdout = io::stdout(); + let mut stdout = stdout.lock(); + msg.write(&mut stdout) +} +fn main() { + loop { + let req = match read_request() { + Err(err) => { + eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string()); + continue; + } + Ok(None) => continue, + Ok(Some(req)) => req, + }; + + match req { + msg::Request::ListMacro(task) => { + if let Err(err) = + write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it))) + { + eprintln!("Write message error on list macro: {}", err); + } + } + msg::Request::ExpansionMacro(task) => { + if let Err(err) = + write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it))) + { + eprintln!("Write message error on expansion macro: {}", err); + } + } + } + } +} -- cgit v1.2.3 From 9a2114b0dd6d3292216fa4d05e3c4cd219633f4b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 3 Apr 2020 19:16:54 +0800 Subject: Add doc comment on main.rs --- crates/ra_proc_macro_srv/src/main.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/main.rs b/crates/ra_proc_macro_srv/src/main.rs index 54ce0224d..70743c1f4 100644 --- a/crates/ra_proc_macro_srv/src/main.rs +++ b/crates/ra_proc_macro_srv/src/main.rs @@ -1,3 +1,5 @@ +//! Driver for proc macro server + use ra_proc_macro::msg::{self, Message}; use ra_proc_macro_srv::{expand_task, list_macros}; -- cgit v1.2.3