From 5ba2440e12c6899b943b072a2d6e9fb3a42532e0 Mon Sep 17 00:00:00 2001 From: Luca Fulchir Date: Sun, 24 Mar 2024 22:32:49 +0100 Subject: [PATCH] Initial test to convert function to TokenSteram Signed-off-by: Luca Fulchir --- Cargo.lock | 2 + bok-macro/Cargo.toml | 2 +- bok-macro/src/lib.rs | 87 ++++++++++++++++++++++++++++++++++++++- bok-utils/Cargo.toml | 2 + bok-utils/src/pkgs/one.rs | 7 ++++ flake.lock | 18 ++++---- flake.nix | 6 ++- 7 files changed, 111 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efab024..fa9338c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,8 @@ version = "0.1.0" dependencies = [ "bok", "bok-macro", + "proc-macro2", + "quote", ] [[package]] diff --git a/bok-macro/Cargo.toml b/bok-macro/Cargo.toml index f06026f..b1479a0 100644 --- a/bok-macro/Cargo.toml +++ b/bok-macro/Cargo.toml @@ -13,7 +13,7 @@ categories = [ "config" ] proc-macro = true [dependencies] -syn = { version = "2.0" } +syn = { version = "2.0", features = [ "full" ] } quote = { version = "1.0" } bok = { path = "../bok" } proc-macro2 = "1.0" diff --git a/bok-macro/src/lib.rs b/bok-macro/src/lib.rs index f1f79d5..b7fd364 100644 --- a/bok-macro/src/lib.rs +++ b/bok-macro/src/lib.rs @@ -17,7 +17,10 @@ use ::proc_macro::TokenStream; use ::quote::quote; -use ::syn::{parse::Parser, parse_macro_input, DeriveInput}; +use ::syn::{ + parse::{ParseBuffer, ParseStream, Parser}, + parse_macro_input, DeriveInput, +}; /// Use as #[repository(MyBaseRepo)] /// Will setup a `base` field that is the given base repo @@ -91,6 +94,88 @@ pub fn derive_repository(input: TokenStream) -> TokenStream { TokenStream::from(expanded) } +/// Use on a function as #[to_code] +/// will create a new function, same name with `_code` that +/// returns the ::proc_macro2::TokenStream of the function +/// +/// e.g.: +/// ``` +/// impl MyPackage { +/// #[package(::bok_macro::to_code)] +/// fn build() { +/// ... +/// } +/// } +/// let p = MyPackage::default(); +/// let tokens : ::proc_macro2::TokenStream = p.build_code() +/// ``` +#[proc_macro_attribute] +pub fn to_code(_attrs: TokenStream, input: TokenStream) -> TokenStream { + let ast_orig: ::syn::ItemFn = parse_macro_input!(input as ::syn::ItemFn); + + let mut ast_new = ast_orig.clone(); + + let new_fn_name = + quote::format_ident!("{}", ast_orig.sig.ident.to_string() + "_code"); + ast_new.sig.ident = quote::format_ident!("{}", new_fn_name); + use ::quote::ToTokens; + let fn_block = ast_new.block.to_token_stream(); + + //let quote_macro = ::quote::format_ident!("quote"); + + let asdf = quote! { + #ast_orig + fn #new_fn_name () -> ::proc_macro2::TokenStream { + //::quote::#quote_macro! { + ::quote::quote! #fn_block + //} + } + }; + + asdf.into() + /* + let fn_block_pb = ::syn::punctuated::Punctuated::< + ::syn::Expr, + ::syn::Token![,], + >::parse_terminated + .parse2(fn_block) + .unwrap(); + */ + /* + let fn_block_pb = ::syn::parse::Parser::parse2( + //::syn::punctuated::Punctuated::<::syn::Expr, ::syn::Token![,]>::parse_terminated, + |input: ParseStream<'_>| ::syn::Result::Ok({ input.clone() }), + fn_block, + ) + .unwrap(); + */ + /* + let fn_block_pb = ::syn::punctuated::Punctuated::< + //::syn::parse::ParseBuffer<'_>, + proc_macro2::TokenStream, + ::syn::Token![,], + >::parse_terminated + .parse2(fn_block) + .unwrap(); + */ + /* + let wtf: ::syn::parse::ParseBuffer<'_> = + parse_macro_input!(fn_block_pb as ::syn::parse::ParseBuffer).into(); + */ + /* + match ::syn::Block::parse_within(&fn_block) { + //match ::syn::Block::parse_within(&wtf) { + Ok(_) => { + // + } + Err(_) => panic!("can't parse code block"), + } + */ + /* + use ::quote::ToTokens; + ast_new.into_token_stream().into() + */ +} /// Use as #[package(MyBasePackage)] /// Will setup a `base` field that is the given base package diff --git a/bok-utils/Cargo.toml b/bok-utils/Cargo.toml index d3d40a3..d8c2064 100644 --- a/bok-utils/Cargo.toml +++ b/bok-utils/Cargo.toml @@ -14,3 +14,5 @@ categories = [ "config" ] [dependencies] bok = { path = "../bok" } bok-macro = { path = "../bok-macro" } +proc-macro2 = "1.0" +quote = "1.0" diff --git a/bok-utils/src/pkgs/one.rs b/bok-utils/src/pkgs/one.rs index 1028953..8b8efa4 100644 --- a/bok-utils/src/pkgs/one.rs +++ b/bok-utils/src/pkgs/one.rs @@ -31,3 +31,10 @@ impl ::std::default::Default for One { } } } + +impl One { + #[::bok_macro::to_code] + fn test() -> u32 { + 42 + } +} diff --git a/flake.lock b/flake.lock index ec650cf..97dae76 100644 --- a/flake.lock +++ b/flake.lock @@ -38,11 +38,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1710420202, - "narHash": "sha256-MvFKESbq4rUWuaf2RKPNYENaSZEw/jaCLo2gU6oREcM=", + "lastModified": 1711124224, + "narHash": "sha256-l0zlN/3CiodvWDtfBOVxeTwYSRz93muVbXWSpaMjXxM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "878ef7d9721bee9f81f8a80819f9211ad1f993da", + "rev": "56528ee42526794d413d6f244648aaee4a7b56c0", "type": "github" }, "original": { @@ -54,11 +54,11 @@ }, "nixpkgs-unstable": { "locked": { - "lastModified": 1710451336, - "narHash": "sha256-pP86Pcfu3BrAvRO7R64x7hs+GaQrjFes+mEPowCfkxY=", + "lastModified": 1711163522, + "narHash": "sha256-YN/Ciidm+A0fmJPWlHBGvVkcarYWSC+s3NTPk/P+q3c=", "owner": "nixos", "repo": "nixpkgs", - "rev": "d691274a972b3165335d261cc4671335f5c67de9", + "rev": "44d0940ea560dee511026a53f0e2e2cde489b4d4", "type": "github" }, "original": { @@ -98,11 +98,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1710555016, - "narHash": "sha256-Lbdq3/TH4VrrR7A6FxIYwu5tFOcprYh8Q49Nc9s/i6c=", + "lastModified": 1711246447, + "narHash": "sha256-g9TOluObcOEKewFo2fR4cn51Y/jSKhRRo4QZckHLop0=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "42baa9e2e4713572d7481f917243b07dffdf54b8", + "rev": "dcc802a6ec4e9cc6a1c8c393327f0c42666f22e4", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index d341833..0b6e451 100644 --- a/flake.nix +++ b/flake.nix @@ -18,7 +18,7 @@ pkgs-unstable = import nixpkgs-unstable { inherit system overlays; }; - RUST_VERSION="1.76.0"; + RUST_VERSION="2024-03-24"; in { devShells.default = pkgs.mkShell { @@ -44,7 +44,9 @@ cargo-license cargo-expand lld - rust-bin.stable.${RUST_VERSION}.default + #rust-bin.stable.${RUST_VERSION}.default + (rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)) + #rust-bin.nightly.${RUST_VERSION}.default rustfmt pkgs-unstable.rust-analyzer #clang_16