-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `erg_proc_macros` crate
- Loading branch information
Showing
6 changed files
with
107 additions
and
5 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
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,18 @@ | ||
[package] | ||
name = "erg_proc_macros" | ||
version.workspace = true | ||
authors.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
homepage.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
erg_common = { workspace = true } | ||
syn = { version = "1.0", features = ["full"] } | ||
quote = "1.0" | ||
|
||
[lib] | ||
proc-macro = true |
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,56 @@ | ||
use proc_macro::TokenStream; | ||
|
||
use quote::quote; | ||
use syn::{PathArguments, ReturnType, Type, TypePath}; | ||
|
||
/// ```ignore | ||
/// #[exec_new_thread] | ||
/// fn foo() -> Result<isize, Box<dyn std::error::Error>> { | ||
/// ... | ||
/// } | ||
/// ``` | ||
/// ↓ ↓ | ||
/// ```ignore | ||
/// fn foo() -> Result<isize, Box<dyn std::error::Error>> { | ||
/// fn error(msg: impl Into<String>) -> std::io::Error { | ||
/// std::io::Error::new(std::io::ErrorKind::Other, msg.into()) | ||
/// } | ||
/// fn f() -> Result<(), Box<dyn std::error::Error + Send>> { | ||
/// {...}.map_err(|e| Box::new(error(e.to_string())) as _) | ||
/// } | ||
/// exec_new_thread(f, "foo").map_err(|e| e as _) | ||
/// } | ||
/// ``` | ||
#[proc_macro_attribute] | ||
pub fn exec_new_thread(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let mut item_fn = syn::parse_macro_input!(item as syn::ItemFn); | ||
let name = item_fn.sig.ident.to_string(); | ||
let ReturnType::Type(_, out) = &item_fn.sig.output else { | ||
todo!() | ||
}; | ||
let Type::Path(TypePath { path, .. }) = out.as_ref() else { | ||
todo!() | ||
}; | ||
let result_t = path.segments.first().unwrap(); | ||
let PathArguments::AngleBracketed(args) = &result_t.arguments else { | ||
todo!() | ||
}; | ||
let t = args.args.first().unwrap(); | ||
let name = syn::LitStr::new(&name, item_fn.sig.ident.span()); | ||
let block = item_fn.block; | ||
let block = syn::parse_quote! {{ | ||
fn error(msg: impl Into<String>) -> std::io::Error { | ||
std::io::Error::new(std::io::ErrorKind::Other, msg.into()) | ||
} | ||
fn _f() -> Result<#t, Box<dyn std::error::Error>> { | ||
#block | ||
} | ||
fn f() -> Result<#t, Box<dyn std::error::Error + Send>> { | ||
_f().map_err(|e| Box::new(error(e.to_string())) as _) | ||
} | ||
erg_common::spawn::exec_new_thread(f, #name).map_err(|e| e as _) | ||
}}; | ||
item_fn.block = Box::new(block); | ||
let item = quote! { #item_fn }; | ||
item.into() | ||
} |