1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#![recursion_limit = "128"] #![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png")] #![doc(html_root_url = "https://docs.rs/rustpython-derive/")] extern crate proc_macro; #[macro_use] extern crate maplit; #[macro_use] mod error; mod compile_bytecode; mod from_args; mod pyclass; mod util; use error::{extract_spans, Diagnostic}; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use quote::ToTokens; use syn::{parse_macro_input, AttributeArgs, DeriveInput, Item}; fn result_to_tokens(result: Result<TokenStream2, Diagnostic>) -> TokenStream { result.unwrap_or_else(ToTokens::into_token_stream).into() } #[proc_macro_derive(FromArgs, attributes(pyarg))] pub fn derive_from_args(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); result_to_tokens(from_args::impl_from_args(input)) } #[proc_macro_attribute] pub fn pyclass(attr: TokenStream, item: TokenStream) -> TokenStream { let attr = parse_macro_input!(attr as AttributeArgs); let item = parse_macro_input!(item as Item); result_to_tokens(pyclass::impl_pyclass(attr, item)) } #[proc_macro_attribute] pub fn pyimpl(attr: TokenStream, item: TokenStream) -> TokenStream { let attr = parse_macro_input!(attr as AttributeArgs); let item = parse_macro_input!(item as Item); result_to_tokens(pyclass::impl_pyimpl(attr, item)) } #[proc_macro_attribute] pub fn pystruct_sequence(attr: TokenStream, item: TokenStream) -> TokenStream { let attr = parse_macro_input!(attr as AttributeArgs); let item = parse_macro_input!(item as Item); result_to_tokens(pyclass::impl_pystruct_sequence(attr, item)) } fn result_to_tokens_expr(result: Result<TokenStream2, Diagnostic>) -> TokenStream { let tokens2 = result.unwrap_or_else(ToTokens::into_token_stream); let ret = quote::quote! { macro_rules! __proc_macro_call { () => {{ #tokens2 }} } }; ret.into() } #[proc_macro] pub fn py_compile_bytecode(input: TokenStream) -> TokenStream { result_to_tokens_expr(compile_bytecode::impl_py_compile_bytecode(input.into())) }