Skip to content

Commit 1a8779b

Browse files
Merge #3800
3800: Introduce ra_proc_macro_srv r=matklad a=edwin0cheng This PR add preliminary for server side of proc macro : 1. Add crate setup 2. IO for server side Co-authored-by: Edwin Cheng <[email protected]>
2 parents ac91de1 + 9a2114b commit 1a8779b

File tree

5 files changed

+164
-2
lines changed

5 files changed

+164
-2
lines changed

Cargo.lock

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_proc_macro/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ pub mod msg;
1111

1212
use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
1313
use ra_tt::{SmolStr, Subtree};
14-
use rpc::ProcMacroKind;
1514
use std::{
1615
path::{Path, PathBuf},
1716
sync::Arc,
1817
};
1918

20-
pub use rpc::{ExpansionResult, ExpansionTask};
19+
pub use rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
2120

2221
#[derive(Debug, Clone)]
2322
pub struct ProcMacroProcessExpander {

crates/ra_proc_macro_srv/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

crates/ra_proc_macro_srv/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

crates/ra_proc_macro_srv/src/main.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Driver for proc macro server
2+
3+
use ra_proc_macro::msg::{self, Message};
4+
use ra_proc_macro_srv::{expand_task, list_macros};
5+
6+
use std::io;
7+
8+
fn read_request() -> Result<Option<msg::Request>, io::Error> {
9+
let stdin = io::stdin();
10+
let mut stdin = stdin.lock();
11+
msg::Request::read(&mut stdin)
12+
}
13+
14+
fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
15+
let msg: msg::Response = match res {
16+
Ok(res) => res,
17+
Err(err) => msg::Response::Error(msg::ResponseError {
18+
code: msg::ErrorCode::ExpansionError,
19+
message: err,
20+
}),
21+
};
22+
23+
let stdout = io::stdout();
24+
let mut stdout = stdout.lock();
25+
msg.write(&mut stdout)
26+
}
27+
fn main() {
28+
loop {
29+
let req = match read_request() {
30+
Err(err) => {
31+
eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string());
32+
continue;
33+
}
34+
Ok(None) => continue,
35+
Ok(Some(req)) => req,
36+
};
37+
38+
match req {
39+
msg::Request::ListMacro(task) => {
40+
if let Err(err) =
41+
write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it)))
42+
{
43+
eprintln!("Write message error on list macro: {}", err);
44+
}
45+
}
46+
msg::Request::ExpansionMacro(task) => {
47+
if let Err(err) =
48+
write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it)))
49+
{
50+
eprintln!("Write message error on expansion macro: {}", err);
51+
}
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)