Introduce ra_proc_macro_srv

This commit is contained in:
Edwin Cheng 2020-04-01 13:11:26 +08:00
parent ac91de1525
commit 84fb9b44c3
5 changed files with 162 additions and 2 deletions

63
Cargo.lock generated
View file

@ -424,6 +424,17 @@ dependencies = [
"regex",
]
[[package]]
name = "goblin"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd5e3132801a1ac34ac53b97acde50c4685414dd2f291b9ea52afa6f07468c8"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]]
name = "heck"
version = "0.3.1"
@ -595,6 +606,16 @@ version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0"
[[package]]
name = "libloading"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753"
dependencies = [
"cc",
"winapi 0.3.8",
]
[[package]]
name = "linked-hash-map"
version = "0.5.2"
@ -834,6 +855,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]]
name = "ppv-lite86"
version = "0.2.6"
@ -1083,6 +1110,22 @@ dependencies = [
"serde_json",
]
[[package]]
name = "ra_proc_macro_srv"
version = "0.1.0"
dependencies = [
"cargo_metadata",
"difference",
"goblin",
"libloading",
"ra_mbe",
"ra_proc_macro",
"ra_tt",
"serde",
"serde_derive",
"serde_json",
]
[[package]]
name = "ra_prof"
version = "0.1.0"
@ -1392,6 +1435,26 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scroll"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb2332cb595d33f7edd5700f4cbf94892e680c7f0ae56adab58a35190b66cb1"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8584eea9b9ff42825b46faf46a8c24d2cff13ec152fa2a50df788b87c07ee28"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "semver"
version = "0.9.0"

View file

@ -11,13 +11,12 @@ pub mod msg;
use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
use ra_tt::{SmolStr, Subtree};
use rpc::ProcMacroKind;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
pub use rpc::{ExpansionResult, ExpansionTask};
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
#[derive(Debug, Clone)]
pub struct ProcMacroProcessExpander {

View file

@ -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"

View file

@ -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<ExpansionResult, String> {
unimplemented!()
}
pub fn list_macros(_task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
unimplemented!()
}

View file

@ -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<Option<msg::Request>, io::Error> {
let stdin = io::stdin();
let mut stdin = stdin.lock();
msg::Request::read(&mut stdin)
}
fn write_response(res: Result<msg::Response, String>) -> 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);
}
}
}
}
}