Skip to content

Commit

Permalink
remove change
Browse files Browse the repository at this point in the history
  • Loading branch information
mox692 committed Jan 3, 2024
1 parent fe66939 commit e316a63
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 64 deletions.
53 changes: 0 additions & 53 deletions or-rs-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,56 +58,3 @@ use proc_macro::TokenStream;
pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream {
parser::MacroParser::parse(item)
}

#[proc_macro]
pub fn my_first_proc_macro(item: TokenStream) -> TokenStream {
item
}

use proc_macro::TokenStream;
use quote::quote;
use syn::*;

#[proc_macro_attribute]
pub fn add_print(_attr: TokenStream, item: TokenStream) -> TokenStream {
// 入力をItemFn(関数を表現する構文木データ型に変換)
let input: ItemFn = parse_macro_input!(item as ItemFn);
// 関数名を取得
let name = &input.sig.ident;
// 関数のブロックを取得
let block = &input.block;

// quoteマクロでproc_macro2::TokenStreamを生成
let expanded: proc_macro2::TokenStream = quote! {
fn #name() {
println!("Function {} is called", stringify!(#name));
#block
}
};

// proc_macro2::TokenStreamからTokenStreamに変換
TokenStream::from(expanded)
}

use proc_macro::{TokenStream, TokenTree};

#[proc_macro_attribute]
pub fn log(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut iter = item.into_iter();

if let Some(TokenTree::Ident(ident)) = iter.next() {
let func_name = ident.to_string();
let rest_of_stream: TokenStream = iter.collect();

let new_stream = format!(
"fn {}() {{ println!(\"Function '{}' called\"); {} }}",
func_name, func_name, rest_of_stream
);
new_stream.parse().unwrap()
} else {
// トークンストリームが関数宣言でない場合はエラー
"compile_error!(\"Expected function declaration\")"
.parse()
.unwrap()
}
}
1 change: 0 additions & 1 deletion tests/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag

use or_rs::enums::*;
use or_rs_macros::my_first_proc_macro;
use or_rs_macros::or_gen;

fn main() {
Expand Down
10 changes: 0 additions & 10 deletions tests/tests/macro_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![feature(proc_macro_hygiene)]
#![allow(unused_variables)]

use or_rs_macros::add_print;
use or_rs_macros::my_first_proc_macro;
use or_rs_macros::or_gen;

#[test]
Expand Down Expand Up @@ -58,11 +56,3 @@ fn test_compile() {
_ => "hello".to_string(),
};
}

// fn foo() {
// my_first_proc_macro!(42);
// }

#[test]
#[add_print]
fn your_function() {}

0 comments on commit e316a63

Please sign in to comment.