diff options
author | Edwin Cheng <[email protected]> | 2020-04-01 06:11:26 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-04-03 12:01:44 +0100 |
commit | 84fb9b44c3b06e1a542608570bb6d7212c8505b7 (patch) | |
tree | e959b5bfef78d08b622e37fabdbe2f630abb7a87 | |
parent | ac91de1525662a602a1057709eb91a9b21ea3ac7 (diff) |
Introduce ra_proc_macro_srv
-rw-r--r-- | Cargo.lock | 63 | ||||
-rw-r--r-- | crates/ra_proc_macro/src/lib.rs | 3 | ||||
-rw-r--r-- | crates/ra_proc_macro_srv/Cargo.toml | 24 | ||||
-rw-r--r-- | crates/ra_proc_macro_srv/src/lib.rs | 21 | ||||
-rw-r--r-- | crates/ra_proc_macro_srv/src/main.rs | 53 |
5 files changed, 162 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock index c07a9614a..016044f11 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -425,6 +425,17 @@ dependencies = [ | |||
425 | ] | 425 | ] |
426 | 426 | ||
427 | [[package]] | 427 | [[package]] |
428 | name = "goblin" | ||
429 | version = "0.2.1" | ||
430 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
431 | checksum = "ddd5e3132801a1ac34ac53b97acde50c4685414dd2f291b9ea52afa6f07468c8" | ||
432 | dependencies = [ | ||
433 | "log", | ||
434 | "plain", | ||
435 | "scroll", | ||
436 | ] | ||
437 | |||
438 | [[package]] | ||
428 | name = "heck" | 439 | name = "heck" |
429 | version = "0.3.1" | 440 | version = "0.3.1" |
430 | source = "registry+https://github.com/rust-lang/crates.io-index" | 441 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -596,6 +607,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
596 | checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" | 607 | checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" |
597 | 608 | ||
598 | [[package]] | 609 | [[package]] |
610 | name = "libloading" | ||
611 | version = "0.5.2" | ||
612 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
613 | checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" | ||
614 | dependencies = [ | ||
615 | "cc", | ||
616 | "winapi 0.3.8", | ||
617 | ] | ||
618 | |||
619 | [[package]] | ||
599 | name = "linked-hash-map" | 620 | name = "linked-hash-map" |
600 | version = "0.5.2" | 621 | version = "0.5.2" |
601 | source = "registry+https://github.com/rust-lang/crates.io-index" | 622 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -835,6 +856,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
835 | checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" | 856 | checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" |
836 | 857 | ||
837 | [[package]] | 858 | [[package]] |
859 | name = "plain" | ||
860 | version = "0.2.3" | ||
861 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
862 | checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" | ||
863 | |||
864 | [[package]] | ||
838 | name = "ppv-lite86" | 865 | name = "ppv-lite86" |
839 | version = "0.2.6" | 866 | version = "0.2.6" |
840 | source = "registry+https://github.com/rust-lang/crates.io-index" | 867 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1084,6 +1111,22 @@ dependencies = [ | |||
1084 | ] | 1111 | ] |
1085 | 1112 | ||
1086 | [[package]] | 1113 | [[package]] |
1114 | name = "ra_proc_macro_srv" | ||
1115 | version = "0.1.0" | ||
1116 | dependencies = [ | ||
1117 | "cargo_metadata", | ||
1118 | "difference", | ||
1119 | "goblin", | ||
1120 | "libloading", | ||
1121 | "ra_mbe", | ||
1122 | "ra_proc_macro", | ||
1123 | "ra_tt", | ||
1124 | "serde", | ||
1125 | "serde_derive", | ||
1126 | "serde_json", | ||
1127 | ] | ||
1128 | |||
1129 | [[package]] | ||
1087 | name = "ra_prof" | 1130 | name = "ra_prof" |
1088 | version = "0.1.0" | 1131 | version = "0.1.0" |
1089 | dependencies = [ | 1132 | dependencies = [ |
@@ -1393,6 +1436,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1393 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" | 1436 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" |
1394 | 1437 | ||
1395 | [[package]] | 1438 | [[package]] |
1439 | name = "scroll" | ||
1440 | version = "0.10.1" | ||
1441 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1442 | checksum = "abb2332cb595d33f7edd5700f4cbf94892e680c7f0ae56adab58a35190b66cb1" | ||
1443 | dependencies = [ | ||
1444 | "scroll_derive", | ||
1445 | ] | ||
1446 | |||
1447 | [[package]] | ||
1448 | name = "scroll_derive" | ||
1449 | version = "0.10.1" | ||
1450 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1451 | checksum = "f8584eea9b9ff42825b46faf46a8c24d2cff13ec152fa2a50df788b87c07ee28" | ||
1452 | dependencies = [ | ||
1453 | "proc-macro2", | ||
1454 | "quote", | ||
1455 | "syn", | ||
1456 | ] | ||
1457 | |||
1458 | [[package]] | ||
1396 | name = "semver" | 1459 | name = "semver" |
1397 | version = "0.9.0" | 1460 | version = "0.9.0" |
1398 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1461 | source = "registry+https://github.com/rust-lang/crates.io-index" |
diff --git a/crates/ra_proc_macro/src/lib.rs b/crates/ra_proc_macro/src/lib.rs index 51fbb046a..63da9f1b4 100644 --- a/crates/ra_proc_macro/src/lib.rs +++ b/crates/ra_proc_macro/src/lib.rs | |||
@@ -11,13 +11,12 @@ pub mod msg; | |||
11 | 11 | ||
12 | use process::{ProcMacroProcessSrv, ProcMacroProcessThread}; | 12 | use process::{ProcMacroProcessSrv, ProcMacroProcessThread}; |
13 | use ra_tt::{SmolStr, Subtree}; | 13 | use ra_tt::{SmolStr, Subtree}; |
14 | use rpc::ProcMacroKind; | ||
15 | use std::{ | 14 | use std::{ |
16 | path::{Path, PathBuf}, | 15 | path::{Path, PathBuf}, |
17 | sync::Arc, | 16 | sync::Arc, |
18 | }; | 17 | }; |
19 | 18 | ||
20 | pub use rpc::{ExpansionResult, ExpansionTask}; | 19 | pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind}; |
21 | 20 | ||
22 | #[derive(Debug, Clone)] | 21 | #[derive(Debug, Clone)] |
23 | pub struct ProcMacroProcessExpander { | 22 | pub struct ProcMacroProcessExpander { |
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] | ||
2 | edition = "2018" | ||
3 | name = "ra_proc_macro_srv" | ||
4 | version = "0.1.0" | ||
5 | authors = ["rust-analyzer developers"] | ||
6 | publish = false | ||
7 | |||
8 | [lib] | ||
9 | doctest = false | ||
10 | |||
11 | [dependencies] | ||
12 | ra_tt = { path = "../ra_tt" } | ||
13 | ra_mbe = { path = "../ra_mbe" } | ||
14 | ra_proc_macro = { path = "../ra_proc_macro" } | ||
15 | |||
16 | serde_derive = "1.0.104" | ||
17 | serde = "1.0.104" | ||
18 | serde_json = "1.0.48" | ||
19 | libloading = "0.5.2" | ||
20 | goblin = "0.2.0" | ||
21 | |||
22 | [dev-dependencies] | ||
23 | cargo_metadata = "0.9.1" | ||
24 | 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 @@ | |||
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 | |||
13 | use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; | ||
14 | |||
15 | pub fn expand_task(_task: &ExpansionTask) -> Result<ExpansionResult, String> { | ||
16 | unimplemented!() | ||
17 | } | ||
18 | |||
19 | pub 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 @@ | |||
1 | use ra_proc_macro::msg::{self, Message}; | ||
2 | use ra_proc_macro_srv::{expand_task, list_macros}; | ||
3 | |||
4 | use std::io; | ||
5 | |||
6 | fn 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 | |||
12 | fn 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 | } | ||
25 | fn 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 | } | ||