1+ use crate :: compiler:: plugin:: proc_macro:: v2:: host:: attribute:: child_nodes:: {
2+ ChildNodesWithoutAttributes , ItemWithAttributes ,
3+ } ;
14use crate :: compiler:: plugin:: proc_macro:: v2:: host:: aux_data:: { EmittedAuxData , ProcMacroAuxData } ;
25use crate :: compiler:: plugin:: proc_macro:: v2:: host:: conversion:: {
36 CallSiteLocation , into_cairo_diagnostics,
@@ -8,8 +11,8 @@ use crate::compiler::plugin::proc_macro::v2::{
811} ;
912use cairo_lang_defs:: plugin:: { DynGeneratedFileAuxData , PluginGeneratedFile , PluginResult } ;
1013use cairo_lang_macro:: { AllocationContext , TokenStream } ;
14+ use cairo_lang_syntax:: node:: ast;
1115use cairo_lang_syntax:: node:: db:: SyntaxGroup ;
12- use cairo_lang_syntax:: node:: { TypedSyntaxNode , ast} ;
1316use smol_str:: SmolStr ;
1417
1518impl ProcMacroHostPlugin {
@@ -24,88 +27,29 @@ impl ProcMacroHostPlugin {
2427 ) -> ( AttrExpansionFound , TokenStream ) {
2528 let mut token_stream_builder = TokenStreamBuilder :: new ( db) ;
2629 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)
3632 }
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)
4835 }
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)
5738 }
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)
6541 }
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)
7444 }
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)
8547 }
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)
9750 }
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)
10953 }
11054 _ => AttrExpansionFound :: None ,
11155 } ;
@@ -189,6 +133,19 @@ impl ProcMacroHostPlugin {
189133 }
190134}
191135
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+
192149pub enum AttrExpansionFound {
193150 Some ( AttrExpansionArgs ) ,
194151 Last ( AttrExpansionArgs ) ,
0 commit comments