0

我正在编写我的第一个 proc 宏,尽管尝试通读此错误、structopt 和 derived_more 的源代码,但我似乎无法准确找到我正在寻找的内容。我想改变这个:

#[derive(Attach)]
#[attach(foo(SomeType, OtherType))]
#[attach(bar(OtherType))]
struct Plugin {}

进入这个:

impl Attach for Plugin {
    fn attach(self, srv: &mut Server) {
        let this = Arc::new(self);
        srv.foo_order(this.clone(), &[TypeId::of::<SomeType>(), TypeId::of::<OtherType>()]);
        srv.bar_order(this, &[TypeId::of::<OtherType>()]);
    }
}

我已经开始编写一个 proc 宏,但在尝试解析属性时遇到了麻烦:

extern crate proc_macro;

use proc_macro::{Span, TokenStream};
use quote::quote;
use std::any::TypeId;
use syn::{
    parse::ParseStream, parse_macro_input, Attribute, AttributeArgs, DeriveInput, Ident, Result,
};

#[proc_macro_derive(Attach, attributes(attach))]
pub fn register_macro(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    impl_register(input)
}

fn impl_register(input: DeriveInput) -> TokenStream {
    let name = &input.ident;
    let attrs = &input.attrs;

    for attr in attrs {
        if attr.path.is_ident("attach") {
            parse_attach_attribute(&attr);
        }
    }
    println!("{:#?}", input);

    TokenStream::from(quote! {
        impl ::corten::Attach for #name {
            fn attach(self, srv: &mut ::corten::Server) {

            }
        }
    })
}

fn parse_attach_attribute(attr: &Attribute) -> Result<Dependency> {
    let list: syn::MetaList = attr.parse_args()?;
    // println!("{:#?}", list);
    let ident = list.path.get_ident().expect("expected identifier");
    let method = Ident::new(&format!("{}_order", ident), ident.span());
    println!("{:#?}", method);
    let dependencies = list
        .nested
        .into_pairs()
        .map(|pair| pair.into_value())
        .collect::<Vec<_>>();
    // How does one get the identifiers out of a NestedMeta?
    println!("{:#?}", dependencies);
    // attr.parse_args_with(|input: ParseStream| {
    //     let ident = input.p
    //     let method = Ident::new(&format!("{}_order", ident), Span::call_site());
    //     let dep = Dependency {
    //         method,

    //     }
    // })
    unimplemented!()
}
struct Dependency {
    method: Ident,
    dependencies: Vec<Ident>,
}

我遇到的困难是如何将属性列表实际变成可用的形式?据我所知,我需要从中解析“foo”和“bar”,&[Attribute]以便构建方法标识符,以及“SomeType”和“OtherType”标识符,我最终都会将它们输入quote!. TokenStream如果我在控制台中打印,所有信息都在那里:

[
    Attribute {
        pound_token: Pound,
        style: Outer,
        bracket_token: Bracket,
        path: Path {
            leading_colon: None,
            segments: [
                PathSegment {
                    ident: Ident {
                        ident: "attach",
                        span: #0 bytes(103..109),
                    },
                    arguments: None,
                },
            ],
        },
        tokens: TokenStream [
            Group {
                delimiter: Parenthesis,
                stream: TokenStream [
                    Ident {
                        ident: "predecode",
                        span: #0 bytes(110..119),
                    },
                    Group {
                        delimiter: Parenthesis,
                        stream: TokenStream [
                            Ident {
                                ident: "SomeType",
                                span: #0 bytes(120..128),
                            },
                            Punct {
                                ch: ',',
                                spacing: Alone,
                                span: #0 bytes(128..129),
                            },
                            Ident {
                                ident: "OtherType",
                                span: #0 bytes(130..139),
                            },
                        ],
                        span: #0 bytes(119..140),
                    },
                ],
                span: #0 bytes(109..141),
            },
        ],
    },
    Attribute {
        pound_token: Pound,
        style: Outer,
        bracket_token: Bracket,
        path: Path {
            leading_colon: None,
            segments: [
                PathSegment {
                    ident: Ident {
                        ident: "attach",
                        span: #0 bytes(145..151),
                    },
                    arguments: None,
                },
            ],
        },
        tokens: TokenStream [
            Group {
                delimiter: Parenthesis,
                stream: TokenStream [
                    Ident {
                        ident: "preresolve",
                        span: #0 bytes(152..162),
                    },
                    Group {
                        delimiter: Parenthesis,
                        stream: TokenStream [
                            Ident {
                                ident: "OtherType",
                                span: #0 bytes(163..172),
                            },
                        ],
                        span: #0 bytes(162..173),
                    },
                ],
                span: #0 bytes(151..174),
            },
        ],
    },
]

但我没有办法真正做到这一点。我怎么去tokens[0].stream.ident

4

1 回答 1

3

经过很多混乱,我认为我有一些可行的方法,尽管我很高兴接受其他更好的答案,因为我觉得这有点混乱:

extern crate proc_macro;

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{parse_macro_input, Attribute, DeriveInput, Ident, Result};

#[proc_macro_derive(Attach, attributes(attach))]
pub fn register_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    proc_macro::TokenStream::from(impl_register(input))
}

fn impl_register(input: DeriveInput) -> TokenStream {
    let name = &input.ident;
    let attrs = &input.attrs;
    // println!("{:#?}", input);

    let attrs = attrs
        .iter()
        .filter(|attr| attr.path.is_ident("attach"))
        .map(|attr| parse_attach_attribute(&attr).expect("parse failed"))
        .map(|dep| {
            let method: Ident = dep.method;
            let dependencies = dep.dependencies.iter().map(|ident: &Ident| {
                quote! {
                    std::any::TypeId::of::<#ident>()
                }
            });
            quote! {
                srv.#method::<#name, _>(Arc::clone(&this), &[ #(#dependencies),* ]);
            }
        });

    quote! {
        impl corten::Attach for #name {
            fn attach(self, srv: &mut corten::Server) {
                let this = std::sync::Arc::new(self);
                #(#attrs)*
            }
        }
    }
}

fn parse_attach_attribute(attr: &Attribute) -> Result<Dependency> {
    let list: syn::MetaList = attr.parse_args()?;
    // println!("{:#?}", list);
    let ident = list.path.get_ident().expect("expected identifier");
    let method = Ident::new(&format!("{}_order", ident), Span::call_site());
    println!("{:#?}", method);
    let dependencies = list
        .nested
        .into_pairs()
        .map(|pair| pair.into_value())
        .filter_map(|pair| match pair {
            syn::NestedMeta::Meta(meta) => match meta {
                syn::Meta::Path(path) => path.get_ident().cloned(),
                _ => panic!("only path meta supported"),
            },
            _ => panic!("lit not supported"),
        })
        .collect();
    println!("{:#?}", dependencies);

    Ok(Dependency {
        method,
        dependencies,
    })
}
struct Dependency {
    method: Ident,
    dependencies: Vec<Ident>,
}

于 2020-09-28T05:25:09.900 回答