1
+ use crate :: compiler:: plugin:: proc_macro:: v2:: host:: attribute:: child_nodes:: {
2
+ ChildNodesWithoutAttributes , ItemWithAttributes ,
3
+ } ;
1
4
use crate :: compiler:: plugin:: proc_macro:: v2:: host:: aux_data:: { EmittedAuxData , ProcMacroAuxData } ;
2
5
use crate :: compiler:: plugin:: proc_macro:: v2:: host:: conversion:: {
3
6
CallSiteLocation , into_cairo_diagnostics,
@@ -8,8 +11,8 @@ use crate::compiler::plugin::proc_macro::v2::{
8
11
} ;
9
12
use cairo_lang_defs:: plugin:: { DynGeneratedFileAuxData , PluginGeneratedFile , PluginResult } ;
10
13
use cairo_lang_macro:: { AllocationContext , TokenStream } ;
14
+ use cairo_lang_syntax:: node:: ast;
11
15
use cairo_lang_syntax:: node:: db:: SyntaxGroup ;
12
- use cairo_lang_syntax:: node:: { TypedSyntaxNode , ast} ;
13
16
use smol_str:: SmolStr ;
14
17
15
18
impl ProcMacroHostPlugin {
@@ -24,88 +27,29 @@ impl ProcMacroHostPlugin {
24
27
) -> ( AttrExpansionFound , TokenStream ) {
25
28
let mut token_stream_builder = TokenStreamBuilder :: new ( db) ;
26
29
let input = match item_ast. clone ( ) {
27
- ast:: ModuleItem :: Trait ( trait_ast) => {
28
- let attrs = trait_ast. attributes ( db) . elements ( db) ;
29
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
30
- token_stream_builder. add_node ( trait_ast. visibility ( db) . as_syntax_node ( ) ) ;
31
- token_stream_builder. add_node ( trait_ast. trait_kw ( db) . as_syntax_node ( ) ) ;
32
- token_stream_builder. add_node ( trait_ast. name ( db) . as_syntax_node ( ) ) ;
33
- token_stream_builder. add_node ( trait_ast. generic_params ( db) . as_syntax_node ( ) ) ;
34
- token_stream_builder. add_node ( trait_ast. body ( db) . as_syntax_node ( ) ) ;
35
- expansion
30
+ ast:: ModuleItem :: Trait ( ast) => {
31
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
36
32
}
37
- ast:: ModuleItem :: Impl ( impl_ast) => {
38
- let attrs = impl_ast. attributes ( db) . elements ( db) ;
39
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
40
- token_stream_builder. add_node ( impl_ast. visibility ( db) . as_syntax_node ( ) ) ;
41
- token_stream_builder. add_node ( impl_ast. impl_kw ( db) . as_syntax_node ( ) ) ;
42
- token_stream_builder. add_node ( impl_ast. name ( db) . as_syntax_node ( ) ) ;
43
- token_stream_builder. add_node ( impl_ast. generic_params ( db) . as_syntax_node ( ) ) ;
44
- token_stream_builder. add_node ( impl_ast. of_kw ( db) . as_syntax_node ( ) ) ;
45
- token_stream_builder. add_node ( impl_ast. trait_path ( db) . as_syntax_node ( ) ) ;
46
- token_stream_builder. add_node ( impl_ast. body ( db) . as_syntax_node ( ) ) ;
47
- expansion
33
+ ast:: ModuleItem :: Impl ( ast) => {
34
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
48
35
}
49
- ast:: ModuleItem :: Module ( module_ast) => {
50
- let attrs = module_ast. attributes ( db) . elements ( db) ;
51
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
52
- token_stream_builder. add_node ( module_ast. visibility ( db) . as_syntax_node ( ) ) ;
53
- token_stream_builder. add_node ( module_ast. module_kw ( db) . as_syntax_node ( ) ) ;
54
- token_stream_builder. add_node ( module_ast. name ( db) . as_syntax_node ( ) ) ;
55
- token_stream_builder. add_node ( module_ast. body ( db) . as_syntax_node ( ) ) ;
56
- expansion
36
+ ast:: ModuleItem :: Module ( ast) => {
37
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
57
38
}
58
- ast:: ModuleItem :: FreeFunction ( free_func_ast) => {
59
- let attrs = free_func_ast. attributes ( db) . elements ( db) ;
60
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
61
- token_stream_builder. add_node ( free_func_ast. visibility ( db) . as_syntax_node ( ) ) ;
62
- token_stream_builder. add_node ( free_func_ast. declaration ( db) . as_syntax_node ( ) ) ;
63
- token_stream_builder. add_node ( free_func_ast. body ( db) . as_syntax_node ( ) ) ;
64
- expansion
39
+ ast:: ModuleItem :: FreeFunction ( ast) => {
40
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
65
41
}
66
- ast:: ModuleItem :: ExternFunction ( extern_func_ast) => {
67
- let attrs = extern_func_ast. attributes ( db) . elements ( db) ;
68
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
69
- token_stream_builder. add_node ( extern_func_ast. visibility ( db) . as_syntax_node ( ) ) ;
70
- token_stream_builder. add_node ( extern_func_ast. extern_kw ( db) . as_syntax_node ( ) ) ;
71
- token_stream_builder. add_node ( extern_func_ast. declaration ( db) . as_syntax_node ( ) ) ;
72
- token_stream_builder. add_node ( extern_func_ast. semicolon ( db) . as_syntax_node ( ) ) ;
73
- expansion
42
+ ast:: ModuleItem :: ExternFunction ( ast) => {
43
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
74
44
}
75
- ast:: ModuleItem :: ExternType ( extern_type_ast) => {
76
- let attrs = extern_type_ast. attributes ( db) . elements ( db) ;
77
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
78
- token_stream_builder. add_node ( extern_type_ast. visibility ( db) . as_syntax_node ( ) ) ;
79
- token_stream_builder. add_node ( extern_type_ast. extern_kw ( db) . as_syntax_node ( ) ) ;
80
- token_stream_builder. add_node ( extern_type_ast. type_kw ( db) . as_syntax_node ( ) ) ;
81
- token_stream_builder. add_node ( extern_type_ast. name ( db) . as_syntax_node ( ) ) ;
82
- token_stream_builder. add_node ( extern_type_ast. generic_params ( db) . as_syntax_node ( ) ) ;
83
- token_stream_builder. add_node ( extern_type_ast. semicolon ( db) . as_syntax_node ( ) ) ;
84
- expansion
45
+ ast:: ModuleItem :: ExternType ( ast) => {
46
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
85
47
}
86
- ast:: ModuleItem :: Struct ( struct_ast) => {
87
- let attrs = struct_ast. attributes ( db) . elements ( db) ;
88
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
89
- token_stream_builder. add_node ( struct_ast. visibility ( db) . as_syntax_node ( ) ) ;
90
- token_stream_builder. add_node ( struct_ast. struct_kw ( db) . as_syntax_node ( ) ) ;
91
- token_stream_builder. add_node ( struct_ast. name ( db) . as_syntax_node ( ) ) ;
92
- token_stream_builder. add_node ( struct_ast. generic_params ( db) . as_syntax_node ( ) ) ;
93
- token_stream_builder. add_node ( struct_ast. lbrace ( db) . as_syntax_node ( ) ) ;
94
- token_stream_builder. add_node ( struct_ast. members ( db) . as_syntax_node ( ) ) ;
95
- token_stream_builder. add_node ( struct_ast. rbrace ( db) . as_syntax_node ( ) ) ;
96
- expansion
48
+ ast:: ModuleItem :: Struct ( ast) => {
49
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
97
50
}
98
- ast:: ModuleItem :: Enum ( enum_ast) => {
99
- let attrs = enum_ast. attributes ( db) . elements ( db) ;
100
- let expansion = self . parse_attrs ( db, & mut token_stream_builder, attrs, ctx) ;
101
- token_stream_builder. add_node ( enum_ast. visibility ( db) . as_syntax_node ( ) ) ;
102
- token_stream_builder. add_node ( enum_ast. enum_kw ( db) . as_syntax_node ( ) ) ;
103
- token_stream_builder. add_node ( enum_ast. name ( db) . as_syntax_node ( ) ) ;
104
- token_stream_builder. add_node ( enum_ast. generic_params ( db) . as_syntax_node ( ) ) ;
105
- token_stream_builder. add_node ( enum_ast. lbrace ( db) . as_syntax_node ( ) ) ;
106
- token_stream_builder. add_node ( enum_ast. variants ( db) . as_syntax_node ( ) ) ;
107
- token_stream_builder. add_node ( enum_ast. rbrace ( db) . as_syntax_node ( ) ) ;
108
- expansion
51
+ ast:: ModuleItem :: Enum ( ast) => {
52
+ parse_item ( & ast, db, self , & mut token_stream_builder, ctx)
109
53
}
110
54
_ => AttrExpansionFound :: None ,
111
55
} ;
@@ -189,6 +133,19 @@ impl ProcMacroHostPlugin {
189
133
}
190
134
}
191
135
136
+ fn parse_item < T : ItemWithAttributes + ChildNodesWithoutAttributes > (
137
+ ast : & T ,
138
+ db : & dyn SyntaxGroup ,
139
+ host : & ProcMacroHostPlugin ,
140
+ token_stream_builder : & mut TokenStreamBuilder < ' _ > ,
141
+ ctx : & AllocationContext ,
142
+ ) -> AttrExpansionFound {
143
+ let attrs = ast. item_attributes ( db) ;
144
+ let expansion = host. parse_attrs ( db, token_stream_builder, attrs, ctx) ;
145
+ token_stream_builder. extend ( ast. child_nodes_without_attributes ( db) ) ;
146
+ expansion
147
+ }
148
+
192
149
pub enum AttrExpansionFound {
193
150
Some ( AttrExpansionArgs ) ,
194
151
Last ( AttrExpansionArgs ) ,
0 commit comments