-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |