aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro_srv
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro_srv')
-rw-r--r--crates/ra_proc_macro_srv/Cargo.toml24
-rw-r--r--crates/ra_proc_macro_srv/src/lib.rs21
-rw-r--r--crates/ra_proc_macro_srv/src/main.rs53
3 files changed, 98 insertions, 0 deletions
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 @@
1[package]
2edition = "2018"
3name = "ra_proc_macro_srv"
4version = "0.1.0"
5authors = ["rust-analyzer developers"]
6publish = false
7
8[lib]
9doctest = false
10
11[dependencies]
12ra_tt = { path = "../ra_tt" }
13ra_mbe = { path = "../ra_mbe" }
14ra_proc_macro = { path = "../ra_proc_macro" }
15
16serde_derive = "1.0.104"
17serde = "1.0.104"
18serde_json = "1.0.48"
19libloading = "0.5.2"
20goblin = "0.2.0"
21
22[dev-dependencies]
23cargo_metadata = "0.9.1"
24difference = "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 @@
1//! RA Proc Macro Server
2//!
3//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
4//! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander.
5//!
6//! But we change some several design for fitting RA needs:
7//!
8//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with
9//! RA then proc-macro2 token stream.
10//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
11//! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue)
12
13use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
14
15pub fn expand_task(_task: &ExpansionTask) -> Result<ExpansionResult, String> {
16 unimplemented!()
17}
18
19pub fn list_macros(_task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
20 unimplemented!()
21}
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 @@
1use ra_proc_macro::msg::{self, Message};
2use ra_proc_macro_srv::{expand_task, list_macros};
3
4use std::io;
5
6fn read_request() -> Result<Option<msg::Request>, io::Error> {
7 let stdin = io::stdin();
8 let mut stdin = stdin.lock();
9 msg::Request::read(&mut stdin)
10}
11
12fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
13 let msg: msg::Response = match res {
14 Ok(res) => res,
15 Err(err) => msg::Response::Error(msg::ResponseError {
16 code: msg::ErrorCode::ExpansionError,
17 message: err,
18 }),
19 };
20
21 let stdout = io::stdout();
22 let mut stdout = stdout.lock();
23 msg.write(&mut stdout)
24}
25fn main() {
26 loop {
27 let req = match read_request() {
28 Err(err) => {
29 eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string());
30 continue;
31 }
32 Ok(None) => continue,
33 Ok(Some(req)) => req,
34 };
35
36 match req {
37 msg::Request::ListMacro(task) => {
38 if let Err(err) =
39 write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it)))
40 {
41 eprintln!("Write message error on list macro: {}", err);
42 }
43 }
44 msg::Request::ExpansionMacro(task) => {
45 if let Err(err) =
46 write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it)))
47 {
48 eprintln!("Write message error on expansion macro: {}", err);
49 }
50 }
51 }
52 }
53}