Skip to content

Commit

Permalink
Update syn
Browse files Browse the repository at this point in the history
  • Loading branch information
PikminGuts92 committed Nov 20, 2023
1 parent 0b03190 commit a358b72
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/grim_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ grim_traits = { path = "../grim_traits" }
lazy_static = { workspace = true }
proc-macro2 = "1.0.69"
quote = "1.0.33"
syn = { version = "1.0.109", default-features = false, features = [ "clone-impls", "derive", "parsing", "printing", "proc-macro" ] }
syn = { version = "2.0.39", default-features = false, features = [ "clone-impls", "derive", "parsing", "printing", "proc-macro" ] }
26 changes: 17 additions & 9 deletions core/grim_macros/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, Meta, NestedMeta, Path, parse::Parser};
use syn::{Attribute, DeriveInput, Meta, Path, parse::Parser, punctuated::Punctuated, Token};

/*
TODO: Finish migrating from v1
pub fn get_meta_list(args: &Vec<NestedMeta>) -> Vec<String> {
let mut metas = Vec::new();
Expand All @@ -17,18 +20,23 @@ pub fn get_meta_list(args: &Vec<NestedMeta>) -> Vec<String> {
}
metas
}
} */

pub fn get_meta_paths(args: &Vec<NestedMeta>) -> Vec<Path> {
pub fn get_meta_paths(args: &Vec<Attribute>) -> Vec<Path> {
let mut paths = Vec::new();

for arg in args {
if let NestedMeta::Meta(meta) = arg {
if let Meta::Path(path) = meta {
paths.push(path.to_owned());
} else {
panic!()
}
//arg.parse_nested_meta(logic)
let nested_meta = arg.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

let Ok(nested_meta) = nested_meta else {
continue;
};

//let nested = arg.parse_nested_meta(|_| Ok(())).unwrap(); // Accept all meta attributes

for meta in nested_meta {
paths.push(meta.path().clone());
}
}

Expand Down
40 changes: 35 additions & 5 deletions core/grim_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::scene::get_object_tokens;
use crate::scene::get_milo_object_tokens;
use proc_macro::TokenStream;
use syn::{AttributeArgs, DeriveInput, parse::Parser, parse_macro_input};
use syn::{DeriveInput, Meta, parse::Parser, parse_macro_input, punctuated::Punctuated, Token};
use quote::quote;

mod common;
Expand Down Expand Up @@ -75,8 +75,33 @@ pub fn version(_args: TokenStream, input: TokenStream) -> TokenStream {

#[proc_macro_attribute]
pub fn milo(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as AttributeArgs);
let paths = get_meta_paths(&args);
//let args = parse_macro_input!(args as AttributeArgs);

//syn::Attribute::parse_outer();
//let args = parse_macro_input!(args with syn::Attribute::parse_outer);

//Punctuated<Meta, Token![,]>

// TODO: Move to struct (Replaces get_meta_paths)
// https://docs.rs/syn/latest/syn/meta/fn.parser.html#example
// https://docs.rs/syn/latest/syn/meta/struct.ParseNestedMeta.html
let mut paths = Vec::new();
let meta_path_parser = syn::meta::parser(|m| {
paths.push(m.path);
Ok(())
});

//let args = syn::Attribute::parse_outer.parse(args);
parse_macro_input!(args with meta_path_parser);
/*let Ok(args) = args else {
println!("Failed to parse args");
return input;
};*/


//let paths = syn::Attribute::parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

//let paths = get_meta_paths(&args);

let mut input = parse_macro_input!(input as DeriveInput);
let mut transformed_input = proc_macro2::TokenStream::new();
Expand Down Expand Up @@ -108,8 +133,13 @@ pub fn milo(args: TokenStream, input: TokenStream) -> TokenStream {

#[proc_macro_attribute]
pub fn milo_super(args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as AttributeArgs);
let paths = get_meta_paths(&args);
let mut paths = Vec::new();
let meta_path_parser = syn::meta::parser(|m| {
paths.push(m.path);
Ok(())
});

parse_macro_input!(args with meta_path_parser);

let mut input = parse_macro_input!(input as DeriveInput);
let mut transformed_input = proc_macro2::TokenStream::new();
Expand Down

0 comments on commit a358b72

Please sign in to comment.