Skip to content

Commit

Permalink
Merge #3800
Browse files Browse the repository at this point in the history
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 <edwin0cheng@gmail.com>
  • Loading branch information
bors[bot] and edwin0cheng authored Apr 3, 2020
2 parents ac91de1 + 9a2114b commit 1a8779b
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 2 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/ra_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions crates/ra_proc_macro_srv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions crates/ra_proc_macro_srv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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!()
}
55 changes: 55 additions & 0 deletions crates/ra_proc_macro_srv/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Driver for proc macro server
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);
}
}
}
}
}

0 comments on commit 1a8779b

Please sign in to comment.