Initial test to convert function to TokenSteram

Signed-off-by: Luca Fulchir <luca.fulchir@runesauth.com>
This commit is contained in:
Luca Fulchir 2024-03-24 22:32:49 +01:00
parent a7ce242f09
commit 5ba2440e12
Signed by: luca.fulchir
GPG Key ID: 8F6440603D13A78E
7 changed files with 111 additions and 13 deletions

2
Cargo.lock generated
View File

@ -35,6 +35,8 @@ version = "0.1.0"
dependencies = [
"bok",
"bok-macro",
"proc-macro2",
"quote",
]
[[package]]

View File

@ -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"

View File

@ -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

View File

@ -14,3 +14,5 @@ categories = [ "config" ]
[dependencies]
bok = { path = "../bok" }
bok-macro = { path = "../bok-macro" }
proc-macro2 = "1.0"
quote = "1.0"

View File

@ -31,3 +31,10 @@ impl ::std::default::Default for One {
}
}
}
impl One {
#[::bok_macro::to_code]
fn test() -> u32 {
42
}
}

View File

@ -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": {

View File

@ -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