2
2
#![ doc( html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png" ) ]
3
3
#![ doc( html_root_url = "https://docs.rs/rustpython-derive/" ) ]
4
4
5
- extern crate proc_macro;
6
-
7
- #[ macro_use]
8
- extern crate maplit;
9
-
10
- #[ macro_use]
11
- mod error;
12
- #[ macro_use]
13
- mod util;
14
-
15
- mod compile_bytecode;
16
- mod from_args;
17
- mod pyclass;
18
- mod pymodule;
19
- mod pypayload;
20
- mod pystructseq;
21
-
22
- use error:: { extract_spans, Diagnostic } ;
23
- use proc_macro2:: TokenStream ;
24
- use quote:: ToTokens ;
25
- use rustpython_doc as doc;
26
- use syn:: { parse_macro_input, AttributeArgs , DeriveInput , Item } ;
27
-
28
- fn result_to_tokens ( result : Result < TokenStream , impl Into < Diagnostic > > ) -> proc_macro:: TokenStream {
29
- result
30
- . map_err ( |e| e. into ( ) )
31
- . unwrap_or_else ( ToTokens :: into_token_stream)
32
- . into ( )
33
- }
5
+ use proc_macro:: TokenStream ;
6
+ use rustpython_derive_impl as derive_impl;
7
+ use syn:: parse_macro_input;
34
8
35
9
#[ proc_macro_derive( FromArgs , attributes( pyarg) ) ]
36
- pub fn derive_from_args ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
37
- let input = parse_macro_input ! ( input as DeriveInput ) ;
38
- result_to_tokens ( from_args :: impl_from_args ( input) )
10
+ pub fn derive_from_args ( input : TokenStream ) -> TokenStream {
11
+ let input = parse_macro_input ! ( input) ;
12
+ derive_impl :: derive_from_args ( input) . into ( )
39
13
}
40
14
41
15
#[ proc_macro_attribute]
42
- pub fn pyclass (
43
- attr : proc_macro:: TokenStream ,
44
- item : proc_macro:: TokenStream ,
45
- ) -> proc_macro:: TokenStream {
46
- let attr = parse_macro_input ! ( attr as AttributeArgs ) ;
47
- let item = parse_macro_input ! ( item as Item ) ;
48
- if matches ! ( item, syn:: Item :: Impl ( _) | syn:: Item :: Trait ( _) ) {
49
- result_to_tokens ( pyclass:: impl_pyimpl ( attr, item) )
50
- } else {
51
- result_to_tokens ( pyclass:: impl_pyclass ( attr, item) )
52
- }
16
+ pub fn pyclass ( attr : TokenStream , item : TokenStream ) -> TokenStream {
17
+ let attr = parse_macro_input ! ( attr) ;
18
+ let item = parse_macro_input ! ( item) ;
19
+ derive_impl:: pyclass ( attr, item) . into ( )
53
20
}
54
21
55
22
/// This macro serves a goal of generating multiple
@@ -63,75 +30,64 @@ pub fn pyclass(
63
30
/// So, we use `extend_class!` macro as the second
64
31
/// step in exception type definition.
65
32
#[ proc_macro]
66
- pub fn define_exception ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
67
- let exc_def = parse_macro_input ! ( input as pyclass :: PyExceptionDef ) ;
68
- result_to_tokens ( pyclass :: impl_define_exception ( exc_def ) )
33
+ pub fn define_exception ( input : TokenStream ) -> TokenStream {
34
+ let input = parse_macro_input ! ( input) ;
35
+ derive_impl :: define_exception ( input ) . into ( )
69
36
}
70
37
71
38
/// Helper macro to define `Exception` types.
72
39
/// More-or-less is an alias to `pyclass` macro.
73
40
#[ proc_macro_attribute]
74
- pub fn pyexception (
75
- attr : proc_macro:: TokenStream ,
76
- item : proc_macro:: TokenStream ,
77
- ) -> proc_macro:: TokenStream {
78
- let attr = parse_macro_input ! ( attr as AttributeArgs ) ;
79
- let item = parse_macro_input ! ( item as Item ) ;
80
- result_to_tokens ( pyclass:: impl_pyexception ( attr, item) )
41
+ pub fn pyexception ( attr : TokenStream , item : TokenStream ) -> TokenStream {
42
+ let attr = parse_macro_input ! ( attr) ;
43
+ let item = parse_macro_input ! ( item) ;
44
+ derive_impl:: pyexception ( attr, item) . into ( )
81
45
}
82
46
83
47
#[ proc_macro_attribute]
84
- pub fn pymodule (
85
- attr : proc_macro:: TokenStream ,
86
- item : proc_macro:: TokenStream ,
87
- ) -> proc_macro:: TokenStream {
88
- let attr = parse_macro_input ! ( attr as AttributeArgs ) ;
89
- let item = parse_macro_input ! ( item as Item ) ;
90
- result_to_tokens ( pymodule:: impl_pymodule ( attr, item) )
48
+ pub fn pymodule ( attr : TokenStream , item : TokenStream ) -> TokenStream {
49
+ let attr = parse_macro_input ! ( attr) ;
50
+ let item = parse_macro_input ! ( item) ;
51
+ derive_impl:: pymodule ( attr, item) . into ( )
91
52
}
92
53
93
54
#[ proc_macro_derive( PyStructSequence ) ]
94
- pub fn pystruct_sequence ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
95
- let input = parse_macro_input ! ( input as DeriveInput ) ;
96
- result_to_tokens ( pystructseq :: impl_pystruct_sequence ( input) )
55
+ pub fn pystruct_sequence ( input : TokenStream ) -> TokenStream {
56
+ let input = parse_macro_input ! ( input) ;
57
+ derive_impl :: pystruct_sequence ( input) . into ( )
97
58
}
98
59
99
60
#[ proc_macro_derive( TryIntoPyStructSequence ) ]
100
- pub fn pystruct_sequence_try_from_object (
101
- input : proc_macro:: TokenStream ,
102
- ) -> proc_macro:: TokenStream {
103
- let input = parse_macro_input ! ( input as DeriveInput ) ;
104
- result_to_tokens ( pystructseq:: impl_pystruct_sequence_try_from_object ( input) )
61
+ pub fn pystruct_sequence_try_from_object ( input : TokenStream ) -> TokenStream {
62
+ let input = parse_macro_input ! ( input) ;
63
+ derive_impl:: pystruct_sequence_try_from_object ( input) . into ( )
105
64
}
106
65
107
- // would be cool to move all the macro implementation to a separate rustpython-derive-shared
108
- // that just depends on rustpython-compiler-core, and then rustpython-derive would hook -compiler
109
- // up to it; so that (the bulk of) rustpython-derive and rustpython-codegen could build in parallel
110
66
struct Compiler ;
111
- impl compile_bytecode :: Compiler for Compiler {
67
+ impl derive_impl :: Compiler for Compiler {
112
68
fn compile (
113
69
& self ,
114
70
source : & str ,
115
- mode : rustpython_compiler_core :: Mode ,
71
+ mode : rustpython_compiler :: Mode ,
116
72
module_name : String ,
117
- ) -> Result < rustpython_compiler_core :: CodeObject , Box < dyn std:: error:: Error > > {
73
+ ) -> Result < rustpython_compiler :: CodeObject , Box < dyn std:: error:: Error > > {
118
74
use rustpython_compiler:: { compile, CompileOpts } ;
119
75
Ok ( compile ( source, mode, module_name, CompileOpts :: default ( ) ) ?)
120
76
}
121
77
}
122
78
123
79
#[ proc_macro]
124
- pub fn py_compile ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
125
- result_to_tokens ( compile_bytecode :: impl_py_compile ( input. into ( ) , & Compiler ) )
80
+ pub fn py_compile ( input : TokenStream ) -> TokenStream {
81
+ derive_impl :: py_compile ( input. into ( ) , & Compiler ) . into ( )
126
82
}
127
83
128
84
#[ proc_macro]
129
- pub fn py_freeze ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
130
- result_to_tokens ( compile_bytecode :: impl_py_freeze ( input. into ( ) , & Compiler ) )
85
+ pub fn py_freeze ( input : TokenStream ) -> TokenStream {
86
+ derive_impl :: py_freeze ( input. into ( ) , & Compiler ) . into ( )
131
87
}
132
88
133
89
#[ proc_macro_derive( PyPayload ) ]
134
- pub fn pypayload ( input : proc_macro :: TokenStream ) -> proc_macro :: TokenStream {
135
- let input = parse_macro_input ! ( input as DeriveInput ) ;
136
- result_to_tokens ( pypayload :: impl_pypayload ( input) )
90
+ pub fn pypayload ( input : TokenStream ) -> TokenStream {
91
+ let input = parse_macro_input ! ( input) ;
92
+ derive_impl :: pypayload ( input) . into ( )
137
93
}
0 commit comments