|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use quote::quote; |
| 3 | +use syn::{Data, DeriveInput, Fields, parse_macro_input}; |
| 4 | + |
| 5 | +/// Derive macro for implementing `AstNode` as a leaf node (non-container). |
| 6 | +/// |
| 7 | +/// This automatically implements `HasSpan`, `HasNodeType`, and `AstNode` with |
| 8 | +/// `is_container() = false`. |
| 9 | +/// |
| 10 | +/// Requires a `span` field of type `SymbolSpan`. |
| 11 | +/// |
| 12 | +/// # Example |
| 13 | +/// |
| 14 | +/// ```rust |
| 15 | +/// #[derive(AstLeafNode)] |
| 16 | +/// struct StringLit { |
| 17 | +/// pub value: String, |
| 18 | +/// pub span: SymbolSpan, |
| 19 | +/// } |
| 20 | +/// ``` |
| 21 | +#[proc_macro_derive(AstLeafNode)] |
| 22 | +pub fn derive_ast_leaf_node(input: TokenStream) -> TokenStream { |
| 23 | + let input = parse_macro_input!(input as DeriveInput); |
| 24 | + let name = &input.ident; |
| 25 | + let generics = input.generics.clone(); |
| 26 | + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 27 | + |
| 28 | + // Verify the struct has a `span` field |
| 29 | + let has_span_field = match &input.data { |
| 30 | + Data::Struct(data_struct) => match &data_struct.fields { |
| 31 | + Fields::Named(fields) => fields.named.iter().any(|field| { |
| 32 | + field.ident.as_ref().is_some_and(|ident| ident == "span") |
| 33 | + }), |
| 34 | + _ => false, |
| 35 | + }, |
| 36 | + _ => false, |
| 37 | + }; |
| 38 | + |
| 39 | + if !has_span_field { |
| 40 | + return syn::Error::new_spanned( |
| 41 | + &input, |
| 42 | + "AstLeafNode can only be derived for structs with a `span: SymbolSpan` field" |
| 43 | + ).to_compile_error().into(); |
| 44 | + } |
| 45 | + |
| 46 | + let type_name = name.to_string(); |
| 47 | + |
| 48 | + let expanded = quote! { |
| 49 | + impl #impl_generics HasSpan for #name #ty_generics #where_clause { |
| 50 | + fn span(&self) -> &SymbolSpan { |
| 51 | + &self.span |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + impl #impl_generics HasNodeType for #name #ty_generics #where_clause { |
| 56 | + fn node_type(&self) -> &'static str { |
| 57 | + #type_name |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + impl #impl_generics AstNode for #name #ty_generics #where_clause {} |
| 62 | + |
| 63 | + impl #impl_generics AstVisitable for #name #ty_generics #where_clause {} |
| 64 | + }; |
| 65 | + |
| 66 | + TokenStream::from(expanded) |
| 67 | +} |
| 68 | + |
| 69 | +/// Derive macro for implementing `AstNode` as a container node. |
| 70 | +/// |
| 71 | +/// This automatically implements `HasSpan`, `HasNodeType`, and `AstNode` with |
| 72 | +/// `is_container() = true`. |
| 73 | +/// |
| 74 | +/// Requires a `span` field of type `SymbolSpan`. |
| 75 | +/// The `accept` method should be implemented manually for visiting children. |
| 76 | +/// |
| 77 | +/// # Example |
| 78 | +/// |
| 79 | +/// ```rust |
| 80 | +/// #[derive(AstContainerNode)] |
| 81 | +/// struct ModelDecl { |
| 82 | +/// pub name: Ident, |
| 83 | +/// pub members: Vec<ModelMember>, |
| 84 | +/// pub span: SymbolSpan, |
| 85 | +/// } |
| 86 | +/// ``` |
| 87 | +#[proc_macro_derive(AstContainerNode)] |
| 88 | +pub fn derive_ast_container_node(input: TokenStream) -> TokenStream { |
| 89 | + let input = parse_macro_input!(input as DeriveInput); |
| 90 | + let name = &input.ident; |
| 91 | + let generics = input.generics.clone(); |
| 92 | + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 93 | + |
| 94 | + // Verify the struct has a `span` field |
| 95 | + let has_span_field = match &input.data { |
| 96 | + Data::Struct(data_struct) => match &data_struct.fields { |
| 97 | + Fields::Named(fields) => fields.named.iter().any(|field| { |
| 98 | + field.ident.as_ref().is_some_and(|ident| ident == "span") |
| 99 | + }), |
| 100 | + _ => false, |
| 101 | + }, |
| 102 | + _ => false, |
| 103 | + }; |
| 104 | + |
| 105 | + if !has_span_field { |
| 106 | + return syn::Error::new_spanned( |
| 107 | + &input, |
| 108 | + "AstContainerNode can only be derived for structs with a `span: SymbolSpan` field" |
| 109 | + ).to_compile_error().into(); |
| 110 | + } |
| 111 | + |
| 112 | + let type_name = name.to_string(); |
| 113 | + |
| 114 | + let expanded = quote! { |
| 115 | + impl #impl_generics HasSpan for #name #ty_generics #where_clause { |
| 116 | + fn span(&self) -> &SymbolSpan { |
| 117 | + &self.span |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + impl #impl_generics HasNodeType for #name #ty_generics #where_clause { |
| 122 | + fn node_type(&self) -> &'static str { |
| 123 | + #type_name |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + impl #impl_generics AstNode for #name #ty_generics #where_clause { |
| 128 | + fn is_container(&self) -> bool { |
| 129 | + true |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + impl #impl_generics AstVisitable for #name #ty_generics #where_clause {} |
| 134 | + }; |
| 135 | + |
| 136 | + TokenStream::from(expanded) |
| 137 | +} |
| 138 | + |
| 139 | +/// Derive an inherent `name()` method for an enum that returns the variant name. |
| 140 | +/// |
| 141 | +/// For unit variants, the match arm uses `Type::Variant`. |
| 142 | +/// For tuple variants, it uses `Type::Variant(..)`. |
| 143 | +/// For struct variants, it uses `Type::Variant { .. }`. |
| 144 | +/// |
| 145 | +/// # Example |
| 146 | +/// |
| 147 | +/// ```rust |
| 148 | +/// #[derive(EnumKindName)] |
| 149 | +/// enum K { Unit, Tuple(u8), Struct { x: u8 } } |
| 150 | +/// # impl K { /* name() generated */ } |
| 151 | +/// ``` |
| 152 | +#[proc_macro_derive(EnumKindName)] |
| 153 | +pub fn derive_enum_kind_name(input: TokenStream) -> TokenStream { |
| 154 | + let input = parse_macro_input!(input as DeriveInput); |
| 155 | + let name = &input.ident; |
| 156 | + |
| 157 | + let Data::Enum(data_enum) = &input.data else { |
| 158 | + return syn::Error::new_spanned( |
| 159 | + &input, |
| 160 | + "EnumKindName can only be derived for enums", |
| 161 | + ) |
| 162 | + .to_compile_error() |
| 163 | + .into(); |
| 164 | + }; |
| 165 | + |
| 166 | + let arms = data_enum.variants.iter().map(|v| { |
| 167 | + let v_ident = &v.ident; |
| 168 | + let v_name = v_ident.to_string(); |
| 169 | + match &v.fields { |
| 170 | + Fields::Unit => quote! { #name::#v_ident => #v_name }, |
| 171 | + Fields::Unnamed(_) => quote! { #name::#v_ident(..) => #v_name }, |
| 172 | + Fields::Named(_) => quote! { #name::#v_ident { .. } => #v_name }, |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + let expanded = quote! { |
| 177 | + impl #name { |
| 178 | + /// Return the enum variant name. |
| 179 | + pub fn name(&self) -> &'static str { |
| 180 | + match self { |
| 181 | + #( #arms, )* |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + }; |
| 186 | + |
| 187 | + TokenStream::from(expanded) |
| 188 | +} |
0 commit comments