diff --git a/crates/macros/cgp-macro-core/src/functions/delegated_impls/provider_trait.rs b/crates/macros/cgp-macro-core/src/functions/delegated_impls/provider_trait.rs index 967f13ba..2f57ea1b 100644 --- a/crates/macros/cgp-macro-core/src/functions/delegated_impls/provider_trait.rs +++ b/crates/macros/cgp-macro-core/src/functions/delegated_impls/provider_trait.rs @@ -1,6 +1,7 @@ -use syn::{ImplItem, ItemTrait, Type, parse_quote}; +use syn::{ImplItem, ItemTrait, Type}; use crate::functions::trait_items_to_delegated_impl_items; +use crate::parse_internal; pub fn provider_trait_to_impl_items( item_trait: &ItemTrait, @@ -8,7 +9,7 @@ pub fn provider_trait_to_impl_items( ) -> syn::Result> { let provider_name = &item_trait.ident; let provider_type_generics = item_trait.generics.split_for_impl().1; - let provider_trait_path: Type = parse_quote!(#provider_name #provider_type_generics); + let provider_trait_path: Type = parse_internal!(#provider_name #provider_type_generics); trait_items_to_delegated_impl_items(&item_trait.items, delegate_type, &provider_trait_path) } diff --git a/crates/macros/cgp-macro-core/src/functions/delegated_impls/signature.rs b/crates/macros/cgp-macro-core/src/functions/delegated_impls/signature.rs index f4743873..4ca0652a 100644 --- a/crates/macros/cgp-macro-core/src/functions/delegated_impls/signature.rs +++ b/crates/macros/cgp-macro-core/src/functions/delegated_impls/signature.rs @@ -2,7 +2,9 @@ use proc_macro2::{Span, TokenStream}; use quote::{ToTokens, quote}; use syn::punctuated::Punctuated; use syn::token::Comma; -use syn::{FnArg, Ident, ImplItemFn, Signature, Type, Visibility, parse2}; +use syn::{FnArg, Ident, ImplItemFn, Signature, Type, Visibility}; + +use crate::functions::parse_internal; pub fn signature_to_delegated_impl_item_fn( signature: &Signature, @@ -18,11 +20,11 @@ pub fn signature_to_delegated_impl_item_fn( TokenStream::new() }; - let body = parse2(quote!({ + let body = parse_internal!({ #delegate_type :: #fn_name ( #args ) #await_expr - }))?; + }); let item = ImplItemFn { attrs: Vec::new(), @@ -42,7 +44,7 @@ fn signature_to_idents(sig: &Signature) -> syn::Result> fn arg_to_ident(arg: &FnArg) -> syn::Result { let ident = match arg { FnArg::Receiver(_) => Ident::new("self", Span::call_site()), - FnArg::Typed(pat) => parse2(pat.pat.to_token_stream())?, + FnArg::Typed(pat) => parse_internal(pat.pat.to_token_stream())?, }; Ok(ident) diff --git a/crates/macros/cgp-macro-core/src/functions/delegated_impls/trait_items.rs b/crates/macros/cgp-macro-core/src/functions/delegated_impls/trait_items.rs index 1100ade6..811c69c9 100644 --- a/crates/macros/cgp-macro-core/src/functions/delegated_impls/trait_items.rs +++ b/crates/macros/cgp-macro-core/src/functions/delegated_impls/trait_items.rs @@ -1,10 +1,11 @@ use proc_macro2::Span; -use quote::quote; use syn::spanned::Spanned; use syn::token::Eq; -use syn::{Error, ImplItem, ImplItemConst, TraitItem, Type, Visibility, parse2}; +use syn::{Error, ImplItem, ImplItemConst, TraitItem, Type, Visibility}; -use crate::functions::{signature_to_delegated_impl_item_fn, trait_to_impl_item_type}; +use crate::functions::{ + parse_internal, signature_to_delegated_impl_item_fn, trait_to_impl_item_type, +}; pub fn trait_items_to_delegated_impl_items( trait_items: &[TraitItem], @@ -33,9 +34,9 @@ pub fn trait_item_to_delegated_impl_items( TraitItem::Type(trait_type) => { let type_name = &trait_type.ident; let type_generics = trait_type.generics.split_for_impl().1; - let delegate_type = parse2(quote!( + let delegate_type = parse_internal! { < #delegate_type as #provider_trait_path > :: #type_name #type_generics - ))?; + }; let impl_type = trait_to_impl_item_type(trait_type, delegate_type); @@ -45,9 +46,9 @@ pub fn trait_item_to_delegated_impl_items( let const_ident = &trait_item_const.ident; let type_generics = trait_item_const.generics.split_for_impl().1; - let impl_expr = parse2(quote! { + let impl_expr = parse_internal! { < #delegate_type as #provider_trait_path > :: #const_ident #type_generics - })?; + }; let impl_item_const = ImplItemConst { attrs: trait_item_const.attrs.clone(), diff --git a/crates/macros/cgp-macro-core/src/functions/field/parse.rs b/crates/macros/cgp-macro-core/src/functions/field/parse.rs index 3064918d..50ce7930 100644 --- a/crates/macros/cgp-macro-core/src/functions/field/parse.rs +++ b/crates/macros/cgp-macro-core/src/functions/field/parse.rs @@ -1,10 +1,9 @@ -use quote::{ToTokens, quote}; +use quote::ToTokens; use syn::spanned::Spanned; use syn::token::Mut; -use syn::{ - Error, GenericArgument, PathArguments, PathSegment, Type, TypePath, parse_quote, parse2, -}; +use syn::{Error, GenericArgument, PathArguments, PathSegment, Type, TypePath}; +use crate::functions::parse_internal; use crate::types::getter::FieldMode; pub fn parse_field_type( @@ -23,10 +22,10 @@ pub fn parse_field_type( )); } - if type_ref.elem.as_ref() == &parse_quote! { str } { + if type_ref.elem.as_ref() == &parse_internal! { str } { // Special case to handle &str as String field - let field_type: Type = parse_quote! { String }; + let field_type: Type = parse_internal! { String }; Ok((field_type, FieldMode::Str)) } else if let (Type::Slice(slice), None) = (type_ref.elem.as_ref(), receiver_mut) { @@ -42,7 +41,7 @@ pub fn parse_field_type( Type::Path(type_path) => { if let Some(field_type) = try_parse_option_ref(type_path) { Ok(( - parse2(quote! { Option< #field_type > })?, + parse_internal! { Option< #field_type > }, FieldMode::OptionRef, )) } else if let (Some(field_type), None) = (try_parse_mref(type_path), receiver_mut) { diff --git a/crates/macros/cgp-macro-core/src/functions/getter/parse.rs b/crates/macros/cgp-macro-core/src/functions/getter/parse.rs index e2040109..5a422dbf 100644 --- a/crates/macros/cgp-macro-core/src/functions/getter/parse.rs +++ b/crates/macros/cgp-macro-core/src/functions/getter/parse.rs @@ -4,10 +4,11 @@ use syn::token::{Comma, Mut}; use syn::visit_mut::VisitMut; use syn::{ Error, FnArg, GenericArgument, Ident, ItemTrait, PathArguments, PathSegment, ReturnType, - Signature, TraitItem, TraitItemFn, TraitItemType, Type, parse_quote, + Signature, TraitItem, TraitItemFn, TraitItemType, Type, }; use crate::functions::{parse_field_type, parse_single_segment_type_path}; +use crate::parse_internal; use crate::types::cgp_getter::{GetterField, ReceiverMode}; use crate::visitors::ReplaceSelfTypeVisitor; @@ -68,8 +69,8 @@ pub fn parse_getter_fields( let field_assoc_type_ident = &field_assoc_type.ident; let field_type = &field.field_type; - if field_type != &parse_quote! { Self :: #field_assoc_type_ident } - && field_type != &parse_quote! { #context_type :: #field_assoc_type_ident } + if field_type != &parse_internal! { Self :: #field_assoc_type_ident } + && field_type != &parse_internal! { #context_type :: #field_assoc_type_ident } { return Err(Error::new( field.field_type.span(), @@ -229,7 +230,7 @@ fn parse_receiver(context_ident: &Ident, arg: &FnArg) -> syn::Result<(ReceiverMo let mut receiver = ty.elem.clone(); ReplaceSelfTypeVisitor { - replaced_type: &parse_quote!(#context_ident), + replaced_type: &parse_internal!(#context_ident), skip_assoc_types: &Vec::new(), } .visit_type_mut(&mut receiver); @@ -254,7 +255,7 @@ fn parse_return_type( let mut replaced_type = ty.as_ref().clone(); ReplaceSelfTypeVisitor { - replaced_type: &parse_quote!(#context_type), + replaced_type: &parse_internal!(#context_type), skip_assoc_types: &Vec::from_iter(field_assoc_type.clone()), } .visit_type_mut(&mut replaced_type); diff --git a/crates/macros/cgp-macro-core/src/functions/is_provider_params.rs b/crates/macros/cgp-macro-core/src/functions/is_provider_params.rs index 3df0ecf8..a23dd8a1 100644 --- a/crates/macros/cgp-macro-core/src/functions/is_provider_params.rs +++ b/crates/macros/cgp-macro-core/src/functions/is_provider_params.rs @@ -1,27 +1,31 @@ use syn::punctuated::Punctuated; use syn::token::Comma; -use syn::{GenericParam, Generics, Type, parse_quote}; +use syn::{GenericParam, Generics, Type}; +use crate::parse_internal; use crate::types::generics::TypeGenerics; pub fn parse_is_provider_params(generics: &Generics) -> syn::Result> { let params = TypeGenerics::try_from(generics)?.generics.params; - let params = params.into_iter().map(|param| -> Type { - match param { + let mut res = Punctuated::new(); + + for param in params { + let out = match param { GenericParam::Type(type_param) => { let ident = type_param.ident; - parse_quote! { #ident } + parse_internal! { #ident } } GenericParam::Lifetime(life_param) => { let life = &life_param.lifetime; - parse_quote! { Life<#life> } + parse_internal! { Life<#life> } } GenericParam::Const(_) => { unimplemented!("const generic parameters are not yet supported in CGP traits") } - } - }); + }; + res.push(out) + } - Ok(Punctuated::from_iter(params)) + Ok(res) } diff --git a/crates/macros/cgp-macro-core/src/functions/mod.rs b/crates/macros/cgp-macro-core/src/functions/mod.rs index 8e7e3890..4f687f53 100644 --- a/crates/macros/cgp-macro-core/src/functions/mod.rs +++ b/crates/macros/cgp-macro-core/src/functions/mod.rs @@ -5,6 +5,7 @@ mod generics; mod getter; mod implicits; mod is_provider_params; +mod parse_internal; mod snake_case; pub use camel_case::*; @@ -14,4 +15,5 @@ pub use generics::*; pub use getter::*; pub use implicits::*; pub use is_provider_params::*; +pub use parse_internal::*; pub use snake_case::*; diff --git a/crates/macros/cgp-macro-core/src/functions/parse_internal.rs b/crates/macros/cgp-macro-core/src/functions/parse_internal.rs new file mode 100644 index 00000000..99f82e9c --- /dev/null +++ b/crates/macros/cgp-macro-core/src/functions/parse_internal.rs @@ -0,0 +1,79 @@ +use core::any::type_name; + +use proc_macro2::{Group, TokenStream, TokenTree}; +use syn::parse::Parse; +use syn::spanned::Spanned; +use syn::{Error, parse2}; + +pub use crate::macros::parse_internal; + +pub fn parse_internal(body: TokenStream) -> Result +where + T: Parse, +{ + parse2(body.clone()).map_err(|mut e| { + e.combine(Error::new( + body.span(), + format!( + "failed to parse internal tokens to type `{}`:\n{}", + type_name::(), + strip_macro_prelude(body.clone()), + ), + )); + e + }) +} + +/// Strips the `::cgp::macro_prelude::` prefix from the [`TokenStream`] so the +/// error message shows the more readable, unqualified paths. The replacement is +/// done at the token level, recursing into nested groups. +fn strip_macro_prelude(body: TokenStream) -> TokenStream { + // The prefix `::cgp::macro_prelude::` is made up of the following tokens. + fn is_prefix(tokens: &[TokenTree]) -> bool { + matches!( + tokens, + [ + TokenTree::Punct(p1), + TokenTree::Punct(p2), + TokenTree::Ident(cgp), + TokenTree::Punct(p3), + TokenTree::Punct(p4), + TokenTree::Ident(prelude), + TokenTree::Punct(p5), + TokenTree::Punct(p6), + ] if p1.as_char() == ':' + && p2.as_char() == ':' + && cgp == "cgp" + && p3.as_char() == ':' + && p4.as_char() == ':' + && prelude == "macro_prelude" + && p5.as_char() == ':' + && p6.as_char() == ':' + ) + } + + const PREFIX_LEN: usize = 8; + + let tokens: Vec = body.into_iter().collect(); + let mut output = Vec::with_capacity(tokens.len()); + let mut i = 0; + + while i < tokens.len() { + if is_prefix(&tokens[i..(i + PREFIX_LEN).min(tokens.len())]) { + i += PREFIX_LEN; + } else { + match &tokens[i] { + TokenTree::Group(group) => { + let inner = strip_macro_prelude(group.stream()); + let mut new_group = Group::new(group.delimiter(), inner); + new_group.set_span(group.span()); + output.push(TokenTree::Group(new_group)); + } + other => output.push(other.clone()), + } + i += 1; + } + } + + output.into_iter().collect() +} diff --git a/crates/macros/cgp-macro-core/src/lib.rs b/crates/macros/cgp-macro-core/src/lib.rs index aa5aeac3..7ac9a313 100644 --- a/crates/macros/cgp-macro-core/src/lib.rs +++ b/crates/macros/cgp-macro-core/src/lib.rs @@ -5,4 +5,5 @@ pub mod functions; pub mod macros; pub mod traits; pub mod types; +pub mod vendor; pub mod visitors; diff --git a/crates/macros/cgp-macro-core/src/macros.rs b/crates/macros/cgp-macro-core/src/macros/export.rs similarity index 70% rename from crates/macros/cgp-macro-core/src/macros.rs rename to crates/macros/cgp-macro-core/src/macros/export.rs index fc68dc74..02db7ff4 100644 --- a/crates/macros/cgp-macro-core/src/macros.rs +++ b/crates/macros/cgp-macro-core/src/macros/export.rs @@ -20,14 +20,3 @@ macro_rules! export_constructs { $( $crate::export_construct! { $from $( => $to )* } )* }; } - -#[macro_export] -macro_rules! define_keyword { - ( $struct_ident:ident, $value:literal ) => { - pub struct $struct_ident; - - impl $crate::traits::IsKeyword for $struct_ident { - const IDENT: &'static str = $value; - } - }; -} diff --git a/crates/macros/cgp-macro-core/src/macros/keyword.rs b/crates/macros/cgp-macro-core/src/macros/keyword.rs new file mode 100644 index 00000000..a94e4241 --- /dev/null +++ b/crates/macros/cgp-macro-core/src/macros/keyword.rs @@ -0,0 +1,10 @@ +#[macro_export] +macro_rules! define_keyword { + ( $struct_ident:ident, $value:literal ) => { + pub struct $struct_ident; + + impl $crate::traits::IsKeyword for $struct_ident { + const IDENT: &'static str = $value; + } + }; +} diff --git a/crates/macros/cgp-macro-core/src/macros/mod.rs b/crates/macros/cgp-macro-core/src/macros/mod.rs new file mode 100644 index 00000000..1144cd36 --- /dev/null +++ b/crates/macros/cgp-macro-core/src/macros/mod.rs @@ -0,0 +1,5 @@ +mod export; +mod keyword; +mod parse; + +pub use parse::*; diff --git a/crates/macros/cgp-macro-core/src/macros/parse.rs b/crates/macros/cgp-macro-core/src/macros/parse.rs new file mode 100644 index 00000000..ca65e2b6 --- /dev/null +++ b/crates/macros/cgp-macro-core/src/macros/parse.rs @@ -0,0 +1,9 @@ +#[macro_export] +macro_rules! parse_internal { + ( $($body:tt)* ) => { + $crate::functions::parse_internal( + $crate::vendor::quote!( $( $body )* ))? + } +} + +pub use parse_internal; diff --git a/crates/macros/cgp-macro-core/src/types/attributes/default_impl/attribute.rs b/crates/macros/cgp-macro-core/src/types/attributes/default_impl/attribute.rs index d209fee7..f1708c71 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/default_impl/attribute.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/default_impl/attribute.rs @@ -1,7 +1,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::In; -use syn::{Generics, ItemImpl, Type, parse_quote}; +use syn::{Generics, ItemImpl, Type}; +use crate::parse_internal; use crate::types::ident::IdentWithTypeArgs; use crate::types::path::UniPathOrType; @@ -23,14 +24,14 @@ impl DefaultImplAttribute { namespace_trait_path .type_args .make_args() - .push(parse_quote!(__Components__)); + .push(parse_internal!(__Components__)); let mut generics = provider_generics.clone(); - generics.params.push(parse_quote!(__Components__)); + generics.params.push(parse_internal!(__Components__)); let (impl_generics, _, where_clause) = generics.split_for_impl(); - let item_impl = parse_quote! { + let item_impl = parse_internal! { impl #impl_generics #namespace_trait_path for #key_type #where_clause { diff --git a/crates/macros/cgp-macro-core/src/types/attributes/derive_delegate/attribute.rs b/crates/macros/cgp-macro-core/src/types/attributes/derive_delegate/attribute.rs index 42d96739..97a90d20 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/derive_delegate/attribute.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/derive_delegate/attribute.rs @@ -1,12 +1,11 @@ use proc_macro2::Span; -use quote::quote; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::token::{Comma, Gt, Lt, Paren}; -use syn::{Error, Ident, ItemImpl, ItemTrait, Path, parenthesized, parse_quote, parse2}; +use syn::{Error, Ident, ItemImpl, ItemTrait, Path, parenthesized}; use crate::exports::DelegateComponent; -use crate::functions::trait_items_to_delegated_impl_items; +use crate::functions::{parse_internal, trait_items_to_delegated_impl_items}; #[derive(Clone)] pub struct DeriveDelegateAttribute { @@ -26,34 +25,39 @@ impl DeriveDelegateAttribute { let mut generics = provider_trait.generics.clone(); - generics.params.push(parse_quote!( #components_ident )); - generics.params.push(parse_quote!( #delegate_ident )); + generics.params.push(parse_internal!( #components_ident )); + generics.params.push(parse_internal!( #delegate_ident )); let where_clause = generics.make_where_clause(); - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #components_ident: #DelegateComponent< ( #use_delegate_params ), Delegate = #delegate_ident, > - })?); + }); let type_generics = provider_trait.generics.split_for_impl().1; - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #delegate_ident : #provider_trait_ident #type_generics - })?); + }); let type_generics = provider_trait.generics.split_for_impl().1; let impl_items = trait_items_to_delegated_impl_items( &provider_trait.items, - &parse_quote!( #delegate_ident ), - &parse_quote!( #provider_trait_ident #type_generics ), + &parse_internal!( #delegate_ident ), + &parse_internal!( #provider_trait_ident #type_generics ), )?; - let provider_type = parse2(quote!(#wrapper_ident < #components_ident >))?; - let trait_path: Path = parse_quote!( #provider_trait_ident #type_generics ); + let provider_type = parse_internal! { + #wrapper_ident < #components_ident > + }; + + let trait_path: Path = parse_internal! { + #provider_trait_ident #type_generics + }; let item = ItemImpl { attrs: provider_trait.attrs.clone(), diff --git a/crates/macros/cgp-macro-core/src/types/attributes/prefix.rs b/crates/macros/cgp-macro-core/src/types/attributes/prefix.rs index 732b705e..70a64f64 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/prefix.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/prefix.rs @@ -1,9 +1,9 @@ -use quote::quote; +use syn::ItemImpl; use syn::parse::{Parse, ParseStream}; use syn::token::In; -use syn::{ItemImpl, parse_quote, parse2}; use crate::exports::RedirectLookup; +use crate::functions::parse_internal; use crate::types::ident::{IdentWithTypeArgs, IdentWithTypeGenerics}; use crate::types::path::UniPath; @@ -23,20 +23,22 @@ impl PrefixAttribute { namespace .type_args .make_args() - .push(parse_quote!(__Components__)); + .push(parse_internal!(__Components__)); let mut path = self.path.clone(); - path.append_type(parse_quote!(#component_name)); + path.append_type(parse_internal!(#component_name)); let mut type_generics = component_name.type_generics.clone(); - type_generics.params.insert(0, parse_quote!(__Components__)); + type_generics + .params + .insert(0, parse_internal!(__Components__)); - let item_impl = parse2(quote! { + let item_impl = parse_internal! { impl #type_generics #namespace for #component_name { type Delegate = #RedirectLookup< __Components__, #path >; } - })?; + }; Ok(item_impl) } diff --git a/crates/macros/cgp-macro-core/src/types/attributes/use_provider/attribute.rs b/crates/macros/cgp-macro-core/src/types/attributes/use_provider/attribute.rs index 5865f471..6824f94e 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/use_provider/attribute.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/use_provider/attribute.rs @@ -1,8 +1,9 @@ use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::token::{Colon, Plus}; -use syn::{Type, TypeParamBound, WherePredicate, parse_quote}; +use syn::{Type, TypeParamBound, WherePredicate}; +use crate::parse_internal; use crate::types::ident::IdentWithTypeArgs; pub struct UseProviderAttribute { @@ -24,9 +25,9 @@ impl UseProviderAttribute { bound .type_args .make_args() - .insert(0, parse_quote!(#context_type)); + .insert(0, parse_internal!(#context_type)); - bounds.push(parse_quote!(#bound)); + bounds.push(parse_internal!(#bound)); } Ok(bounds) @@ -36,7 +37,7 @@ impl UseProviderAttribute { let provider_type = &self.provider_type; let bounds = self.to_type_param_bounds(context_type)?; - let predicate = parse_quote! { + let predicate = parse_internal! { #provider_type: #bounds }; @@ -46,7 +47,7 @@ impl UseProviderAttribute { impl Parse for UseProviderAttribute { fn parse(input: ParseStream) -> syn::Result { - let context_type = parse_quote!(Self); + let context_type = parse_internal!(Self); let provider_type = input.parse()?; let colon: Colon = input.parse()?; diff --git a/crates/macros/cgp-macro-core/src/types/attributes/use_type/attribute.rs b/crates/macros/cgp-macro-core/src/types/attributes/use_type/attribute.rs index cb5b7891..617b8679 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/use_type/attribute.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/use_type/attribute.rs @@ -1,7 +1,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::{At, Brace, Colon, Comma, Gt, Lt}; -use syn::{Ident, Type, braced, parse_quote}; +use syn::{Ident, Type, braced}; +use crate::parse_internal; use crate::types::attributes::UseTypeIdent; use crate::types::ident::IdentWithTypeArgs; @@ -45,7 +46,7 @@ impl Parse for UseTypeAttribute { (context_type, input) } } else { - (parse_quote! { Self }, input) + (parse_internal! { Self }, input) }; let trait_path = if body.peek(Lt) { diff --git a/crates/macros/cgp-macro-core/src/types/attributes/use_type/attributes.rs b/crates/macros/cgp-macro-core/src/types/attributes/use_type/attributes.rs index 666ac9f7..afc6f230 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/use_type/attributes.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/use_type/attributes.rs @@ -1,7 +1,8 @@ use quote::ToTokens; use syn::visit_mut::VisitMut; -use syn::{ItemImpl, ItemTrait, parse_quote, parse2}; +use syn::{ItemImpl, ItemTrait}; +use crate::functions::parse_internal; use crate::types::attributes::UseTypeAttribute; use crate::types::attributes::use_type::type_predicates::derive_use_type_predicates; use crate::visitors::SubstituteAbstractType; @@ -32,13 +33,13 @@ impl UseTypeAttributes { self.substitute_abstract_types_in_item_trait(item_trait); for use_type in self.attributes.iter() { - if use_type.context_type != parse_quote! { Self } { + if use_type.context_type != parse_internal! { Self } { continue; } item_trait .supertraits - .push(parse2(use_type.trait_path.to_token_stream())?); + .push(parse_internal(use_type.trait_path.to_token_stream())?); } Ok(()) diff --git a/crates/macros/cgp-macro-core/src/types/attributes/use_type/type_predicates.rs b/crates/macros/cgp-macro-core/src/types/attributes/use_type/type_predicates.rs index a10f868e..def943b0 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/use_type/type_predicates.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/use_type/type_predicates.rs @@ -2,8 +2,9 @@ use proc_macro2::TokenStream; use quote::{ToTokens, quote}; use syn::punctuated::Punctuated; use syn::token::Comma; -use syn::{Ident, Type, WherePredicate, parse_quote, parse2}; +use syn::{Ident, Type, WherePredicate}; +use crate::functions::parse_internal; use crate::types::attributes::{UseTypeAttribute, UseTypeIdent}; pub fn derive_use_type_predicates(specs: &[UseTypeAttribute]) -> syn::Result> { @@ -15,16 +16,16 @@ pub fn derive_use_type_predicates(specs: &[UseTypeAttribute]) -> syn::Result = Punctuated::new(); @@ -34,9 +35,9 @@ pub fn derive_use_type_predicates(specs: &[UseTypeAttribute]) -> syn::Result - })?); + }); } } @@ -44,7 +45,7 @@ pub fn derive_use_type_predicates(specs: &[UseTypeAttribute]) -> syn::Result syn::Result> { - let Ok(context_ident) = parse2::(context_type.to_token_stream()) else { + let Ok(context_ident) = parse_internal::(context_type.to_token_stream()) else { return Ok(None); }; @@ -55,9 +56,9 @@ fn find_type_alias(specs: &[UseTypeAttribute], context_type: &Type) -> syn::Resu let type_ident = &ident.type_ident; let trait_path = &spec.trait_path; - let new_type = parse2(quote! { + let new_type = parse_internal! { <#new_context_type as #trait_path>::#type_ident - })?; + }; return Ok(Some(new_type)); } @@ -121,16 +122,17 @@ fn find_type_equality( } for match_use_type in spec.type_idents.iter() { - let match_type: Type = parse2(match_use_type.alias_ident().to_token_stream())?; + let match_type: Type = + parse_internal(match_use_type.alias_ident().to_token_stream())?; if match_type == equal_target { let trait_path = &spec.trait_path; let current_type_ident = ¤t_ident.type_ident; let match_type_ident = &match_use_type.type_ident; let context_type = &spec.context_type; - let equal_target: Type = parse2(quote! { + let equal_target: Type = parse_internal! { <#context_type as #trait_path>::#match_type_ident - })?; + }; return Ok(Some((current_type_ident.clone(), equal_target))); } diff --git a/crates/macros/cgp-macro-core/src/types/attributes/uses.rs b/crates/macros/cgp-macro-core/src/types/attributes/uses.rs index cc088b7d..9ce76a09 100644 --- a/crates/macros/cgp-macro-core/src/types/attributes/uses.rs +++ b/crates/macros/cgp-macro-core/src/types/attributes/uses.rs @@ -1,7 +1,8 @@ +use syn::TypeParamBound; use syn::punctuated::Punctuated; use syn::token::Plus; -use syn::{TypeParamBound, parse_quote}; +use crate::parse_internal; use crate::traits::ToTypeParamBounds; use crate::types::ident::IdentWithTypeArgs; @@ -15,7 +16,7 @@ impl ToTypeParamBounds for UsesAttributes { let mut bounds: Punctuated = Punctuated::default(); for import in &self.imports { - bounds.push(parse_quote! { #import }); + bounds.push(parse_internal! { #import }); } Ok(bounds) diff --git a/crates/macros/cgp-macro-core/src/types/cgp_auto_getter/blanket.rs b/crates/macros/cgp-macro-core/src/types/cgp_auto_getter/blanket.rs index b59ea605..dd4ee468 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_auto_getter/blanket.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_auto_getter/blanket.rs @@ -1,6 +1,7 @@ -use quote::{ToTokens, quote}; -use syn::{Ident, ImplItem, ItemImpl, ItemTrait, TraitItemType, parse_quote, parse2}; +use quote::ToTokens; +use syn::{Ident, ImplItem, ItemImpl, ItemTrait, TraitItemType}; +use crate::functions::parse_internal; use crate::types::cgp_getter::{GetterField, ReceiverMode}; use crate::types::field::{FieldName, HasFieldBound}; use crate::types::getter::{ContextArg, derive_getter_method}; @@ -22,32 +23,35 @@ pub fn derive_blanket_impl( generics .params - .insert(0, parse2(context_type.to_token_stream())?); + .insert(0, parse_internal(context_type.to_token_stream())?); if let Some(field_assoc_type) = field_assoc_type { let field_assoc_type_ident = &field_assoc_type.ident; generics .params - .push(parse2(field_assoc_type_ident.to_token_stream())?); + .push(parse_internal(field_assoc_type_ident.to_token_stream())?); - items.push(parse2(quote! { + items.push(parse_internal! { type #field_assoc_type_ident = #field_assoc_type_ident; - })?); + }); let field_constraints = get_bounds_and_replace_self_assoc_type(field_assoc_type); - generics.make_where_clause().predicates.push(parse2(quote! { - #field_assoc_type_ident: #field_constraints - })?); + generics + .make_where_clause() + .predicates + .push(parse_internal! { + #field_assoc_type_ident: #field_constraints + }); } let where_clause = generics.make_where_clause(); if !supertrait_constraints.is_empty() { - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #context_type: #supertrait_constraints - })?); + }); } for field in fields { @@ -57,7 +61,7 @@ pub fn derive_blanket_impl( }; let field_name = FieldName::from(field.field_name.clone()); - let tag_type = parse_quote!(#field_name); + let tag_type = parse_internal!(#field_name); let method = derive_getter_method(&context_arg, field, &tag_type, None)?; @@ -65,7 +69,7 @@ pub fn derive_blanket_impl( let field_type = if let Some(trait_item) = &field_assoc_type { let trait_item_ident = &trait_item.ident; - parse_quote!(#trait_item_ident) + parse_internal!(#trait_item_ident) } else { field.field_type.clone() }; @@ -77,21 +81,21 @@ pub fn derive_blanket_impl( tag_type, }; - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #receiver_type: #constraint - })?); + }); } let (_, type_generics, _) = consumer_trait.generics.split_for_impl(); let (impl_generics, _, where_clause) = generics.split_for_impl(); - let item_impl: ItemImpl = parse2(quote! { + let item_impl: ItemImpl = parse_internal! { impl #impl_generics #consumer_name #type_generics for #context_type #where_clause { #( #items )* } - })?; + }; Ok(item_impl) } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_redirect_lookup_impl.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_redirect_lookup_impl.rs index 5634fa40..def64433 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_redirect_lookup_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_redirect_lookup_impl.rs @@ -1,8 +1,8 @@ use quote::quote; -use syn::{GenericParam, Generics, ItemImpl, Path, parse_quote, parse2}; +use syn::{GenericParam, Generics, ItemImpl, Path}; use crate::exports::{ConcatPath, DelegateComponent, RedirectLookup}; -use crate::functions::provider_trait_to_impl_items; +use crate::functions::{parse_internal, provider_trait_to_impl_items}; use crate::types::cgp_component::EvaluatedCgpComponent; use crate::types::path::{PathElement, UniPath}; use crate::types::provider_impl::ItemProviderImpl; @@ -19,16 +19,16 @@ impl EvaluatedCgpComponent { let mut impl_generics = provider_trait.generics.clone(); - impl_generics.params.push(parse_quote!(__Components__)); + impl_generics.params.push(parse_internal!(__Components__)); - impl_generics.params.push(parse_quote!(__Path__)); + impl_generics.params.push(parse_internal!(__Path__)); let where_clause = impl_generics.make_where_clause(); let delegate_constraint = if let Some(generic_params) = &generic_params { - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { __Path__: #ConcatPath< #generic_params > - })?); + }); quote! { #DelegateComponent<<__Path__ as #ConcatPath< #generic_params >>::Output> @@ -39,23 +39,23 @@ impl EvaluatedCgpComponent { } }; - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { __Components__: #delegate_constraint - })?); + }); - let delegate_type = parse_quote! { + let delegate_type = parse_internal! { < __Components__ as #delegate_constraint > :: Delegate }; - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #delegate_type : #provider_name #provider_type_generics - })?); + }); let impl_items = provider_trait_to_impl_items(provider_trait, &delegate_type)?; - let self_type = parse2(quote!(#RedirectLookup<__Components__, __Path__>))?; + let self_type = parse_internal!(#RedirectLookup<__Components__, __Path__>); - let trait_path: Path = parse2(quote!( #provider_name #provider_type_generics ))?; + let trait_path: Path = parse_internal!( #provider_name #provider_type_generics ); let item_impl = ItemImpl { attrs: provider_trait.attrs.clone(), @@ -94,7 +94,8 @@ fn generic_params_to_path(generics: &Generics) -> syn::Result> { } else { let mut path = UniPath::default(); for param in type_params { - path.elements.push(PathElement::Type(parse_quote!(#param))) + path.elements + .push(PathElement::Type(parse_internal!(#param))) } Ok(Some(path)) diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_use_context_impl.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_use_context_impl.rs index 46f0200a..141ea0a9 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_use_context_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/evaluated/to_use_context_impl.rs @@ -1,8 +1,7 @@ -use quote::quote; -use syn::{ItemImpl, Path, Type, parse_quote, parse2}; +use syn::{ItemImpl, Path, Type}; use crate::exports::UseContext; -use crate::functions::trait_items_to_delegated_impl_items; +use crate::functions::{parse_internal, trait_items_to_delegated_impl_items}; use crate::types::cgp_component::EvaluatedCgpComponent; use crate::types::provider_impl::ItemProviderImpl; @@ -21,24 +20,24 @@ impl EvaluatedCgpComponent { let consumer_trait_generics = consumer_trait.generics.split_for_impl().1; let consumer_trait_path: Type = - parse_quote!(#consumer_trait_ident #consumer_trait_generics); + parse_internal!(#consumer_trait_ident #consumer_trait_generics); let mut impl_generics = provider_trait.generics.clone(); impl_generics .make_where_clause() .predicates - .push(parse2(quote! { + .push(parse_internal! { #context_type_ident : #consumer_trait_ident #consumer_trait_generics - })?); + }); let impl_items = trait_items_to_delegated_impl_items( &provider_trait.items, - &parse_quote!(#context_type_ident), + &parse_internal!(#context_type_ident), &consumer_trait_path, )?; - let provider_trait_path: Path = parse2(quote!( #provider_trait_ident #provider_generics ))?; + let provider_trait_path: Path = parse_internal!( #provider_trait_ident #provider_generics ); let item_impl = ItemImpl { attrs: provider_trait.attrs.clone(), @@ -47,13 +46,13 @@ impl EvaluatedCgpComponent { impl_token: Default::default(), generics: impl_generics, trait_: Some((None, provider_trait_path, Default::default())), - self_ty: Box::new(parse_quote!(#UseContext)), + self_ty: Box::new(parse_internal!(#UseContext)), brace_token: Default::default(), items: impl_items, }; Ok(ItemProviderImpl { - component_type: parse_quote!(#component_name), + component_type: parse_internal!(#component_name), item_impl, }) } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_consumer_impl.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_consumer_impl.rs index 9721643c..d5767b9d 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_consumer_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_consumer_impl.rs @@ -1,8 +1,7 @@ -use quote::quote; use syn::token::{Brace, For, Impl}; -use syn::{ItemImpl, Path, Type, parse_quote, parse2}; +use syn::{ItemImpl, Path, Type}; -use crate::functions::trait_items_to_delegated_impl_items; +use crate::functions::{parse_internal, trait_items_to_delegated_impl_items}; use crate::types::cgp_component::PreprocessedCgpComponent; use crate::types::generics::TypeGenerics; @@ -21,9 +20,9 @@ impl PreprocessedCgpComponent { provider_type_generics .generics .params - .insert(0, parse2(quote!(#context_type_ident))?); + .insert(0, parse_internal!(#context_type_ident)); - parse2(quote!(#provider_ident #provider_type_generics))? + parse_internal!(#provider_ident #provider_type_generics) }; let generics_for_impl = { @@ -31,31 +30,31 @@ impl PreprocessedCgpComponent { generics .params - .insert(0, parse2(quote!(#context_type_ident))?); + .insert(0, parse_internal!(#context_type_ident)); let where_clause = generics.make_where_clause(); if !consumer_trait.supertraits.is_empty() { let supertrait_constraints = consumer_trait.supertraits.clone(); - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #context_type_ident : #supertrait_constraints - })?); + }); } - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #context_type_ident : #provider_trait_path - })?); + }); generics }; let impl_items = trait_items_to_delegated_impl_items( &consumer_trait.items, - &parse_quote!(#context_type_ident), + &parse_internal!(#context_type_ident), &provider_trait_path, )?; - let consumer_trait_path: Path = parse2(quote!( #consumer_name #consumer_type_generics ))?; + let consumer_trait_path: Path = parse_internal!( #consumer_name #consumer_type_generics ); let item_impl = ItemImpl { attrs: consumer_trait.attrs.clone(), @@ -64,7 +63,7 @@ impl PreprocessedCgpComponent { impl_token: Impl::default(), generics: generics_for_impl, trait_: Some((None, consumer_trait_path, For::default())), - self_ty: Box::new(parse2(quote!(#context_type_ident))?), + self_ty: Box::new(parse_internal!(#context_type_ident)), brace_token: Brace::default(), items: impl_items, }; diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_blanket_impl.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_blanket_impl.rs index bca240cc..b94c5724 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_blanket_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_blanket_impl.rs @@ -2,10 +2,10 @@ use proc_macro2::Span; use quote::quote; use syn::punctuated::Punctuated; use syn::token::{Brace, For, Impl, Plus}; -use syn::{Ident, ItemImpl, ItemTrait, Path, TypeParamBound, parse_quote, parse2}; +use syn::{Ident, ItemImpl, ItemTrait, Path, TypeParamBound}; use crate::exports::{DelegateComponent, IsProviderFor}; -use crate::functions::{parse_is_provider_params, provider_trait_to_impl_items}; +use crate::functions::{parse_internal, parse_is_provider_params, provider_trait_to_impl_items}; use crate::types::cgp_component::PreprocessedCgpComponent; impl PreprocessedCgpComponent { @@ -23,7 +23,7 @@ impl PreprocessedCgpComponent { #DelegateComponent< #component_name > }; - let delegate_type = parse_quote! { + let delegate_type = parse_internal! { < #provider_type as #delegate_constraint > :: Delegate }; @@ -34,7 +34,7 @@ impl PreprocessedCgpComponent { impl_generics .params - .insert(0, parse2(quote!(#provider_type))?); + .insert(0, parse_internal!(#provider_type)); { let is_provider_params = parse_is_provider_params(&consumer_trait.generics)?; @@ -42,25 +42,25 @@ impl PreprocessedCgpComponent { let mut delegate_constraints: Punctuated = Punctuated::default(); - delegate_constraints.push(parse2(delegate_constraint)?); + delegate_constraints.push(parse_internal(delegate_constraint)?); - delegate_constraints.push(parse2(quote!( + delegate_constraints.push(parse_internal! { #IsProviderFor< #component_name, #context_type, ( #is_provider_params ) > - ))?); + }); - let provider_constraint: TypeParamBound = parse2(quote! { + let provider_constraint: TypeParamBound = parse_internal! { #provider_name #provider_type_generics - })?; + }; let where_clause = impl_generics.make_where_clause(); - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #provider_type : #delegate_constraints - })?); + }); - where_clause.predicates.push(parse2(quote! { + where_clause.predicates.push(parse_internal! { #delegate_type : #provider_constraint - })?); + }); } impl_generics @@ -68,7 +68,7 @@ impl PreprocessedCgpComponent { let impl_items = provider_trait_to_impl_items(&provider_trait, &delegate_type)?; - let trait_path: Path = parse2(quote!( #provider_name #provider_type_generics ))?; + let trait_path: Path = parse_internal!( #provider_name #provider_type_generics ); let provider_blanket_impl = ItemImpl { attrs: provider_trait.attrs.clone(), @@ -77,7 +77,7 @@ impl PreprocessedCgpComponent { impl_token: Impl::default(), generics: impl_generics, trait_: Some((None, trait_path, For::default())), - self_ty: Box::new(parse2(quote!(#provider_type))?), + self_ty: Box::new(parse_internal!(#provider_type)), brace_token: Brace::default(), items: impl_items, }; diff --git a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_trait.rs b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_trait.rs index 2f09a0d9..60e6230c 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_trait.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_component/preprocessed/to_provider_trait.rs @@ -1,10 +1,9 @@ -use quote::quote; use syn::punctuated::Punctuated; use syn::visit_mut::VisitMut; -use syn::{Ident, ItemTrait, TraitItem, Type, TypeParamBound, parse_quote, parse2}; +use syn::{Ident, ItemTrait, TraitItem, Type, TypeParamBound}; use crate::exports::IsProviderFor; -use crate::functions::{parse_is_provider_params, to_snake_case_ident}; +use crate::functions::{parse_internal, parse_is_provider_params, to_snake_case_ident}; use crate::types::cgp_component::PreprocessedCgpComponent; use crate::visitors::{ ReplaceSelfReceiverVisitor, ReplaceSelfTypeVisitor, ReplaceSelfValueVisitor, @@ -21,14 +20,14 @@ impl PreprocessedCgpComponent { provider_trait.ident = provider_name.clone(); - let context_type: Type = parse_quote!(#context_type_ident); + let context_type: Type = parse_internal!(#context_type_ident); // Add generic parameter `Context` to the front of generics { provider_trait .generics .params - .insert(0, parse_quote!(#context_type_ident)); + .insert(0, parse_internal!(#context_type_ident)); } let local_assoc_types: Vec = provider_trait @@ -51,16 +50,16 @@ impl PreprocessedCgpComponent { .generics .make_where_clause() .predicates - .push(parse_quote! { + .push(parse_internal! { #context_type_ident : #supertraits }); } let is_provider_params = parse_is_provider_params(&consumer_trait.generics)?; - let provider_supertrait: TypeParamBound = parse2(quote!( + let provider_supertrait: TypeParamBound = parse_internal! { #IsProviderFor< #component_name, #context_type_ident, ( #is_provider_params ) > - ))?; + }; provider_trait.supertraits = Punctuated::from_iter([provider_supertrait]); diff --git a/crates/macros/cgp-macro-core/src/types/cgp_fn/preprocessed.rs b/crates/macros/cgp-macro-core/src/types/cgp_fn/preprocessed.rs index 9d29c766..a7266a9e 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_fn/preprocessed.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_fn/preprocessed.rs @@ -1,11 +1,11 @@ -use quote::quote; use syn::punctuated::Punctuated; use syn::token::Plus; use syn::{ Generics, Ident, Item, ItemFn, ItemImpl, ItemTrait, TraitItemFn, Type, TypeParamBound, - Visibility, parse_quote, parse2, + Visibility, }; +use crate::functions::parse_internal; use crate::traits::AddTypeParamBounds; use crate::types::attributes::FunctionAttributes; use crate::types::implicits::ImplicitArgFields; @@ -44,11 +44,11 @@ impl PreprocessedItemCgpFn { semi_token: None, }; - let mut item_trait: ItemTrait = parse2(quote! { + let mut item_trait: ItemTrait = parse_internal! { pub trait #ident { #trait_item_fn } - })?; + }; item_trait.generics = generics.clone(); item_trait.generics.where_clause = None; @@ -83,19 +83,19 @@ impl PreprocessedItemCgpFn { let type_generics = generics.split_for_impl().1; - let self_type: Type = parse_quote!(Self); + let self_type: Type = parse_internal!(Self); - let mut item_impl: ItemImpl = parse2(quote! { + let mut item_impl: ItemImpl = parse_internal! { impl #ident #type_generics for __Context__ { #item_fn } - })?; + }; item_impl.generics = generics.clone(); item_impl .generics .params - .insert(0, parse_quote!(__Context__)); + .insert(0, parse_internal!(__Context__)); item_impl .generics @@ -107,7 +107,7 @@ impl PreprocessedItemCgpFn { bounds.extend(attributes.extend.clone()); for import in attributes.uses.iter() { - bounds.push(parse2(quote! { #import })?); + bounds.push(parse_internal! { #import }); } if !bounds.is_empty() { @@ -115,9 +115,9 @@ impl PreprocessedItemCgpFn { .generics .make_where_clause() .predicates - .push(parse2(quote! { + .push(parse_internal! { Self: #bounds - })?); + }); } } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_getter/to_use_fields_impl.rs b/crates/macros/cgp-macro-core/src/types/cgp_getter/to_use_fields_impl.rs index 2953e5e5..6c5447f5 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_getter/to_use_fields_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_getter/to_use_fields_impl.rs @@ -1,6 +1,7 @@ -use quote::{ToTokens, quote}; -use syn::{ImplItem, ItemImpl, Type, parse_quote, parse2}; +use quote::ToTokens; +use syn::{ImplItem, ItemImpl, Type}; +use crate::functions::parse_internal; use crate::types::cgp_getter::{ItemCgpGetter, ReceiverMode}; use crate::types::field::{HasFieldBound, Symbol}; use crate::types::getter::{ContextArg, derive_getter_method}; @@ -28,32 +29,32 @@ impl ItemCgpGetter { provider_generics .params - .push(parse2(field_assoc_type_ident.to_token_stream())?); + .push(parse_internal(field_assoc_type_ident.to_token_stream())?); - items.push(parse2(quote! { + items.push(parse_internal! { type #field_assoc_type_ident = #field_assoc_type_ident; - })?); + }); let field_constraints = get_bounds_and_replace_self_assoc_type(field_assoc_type); provider_generics .make_where_clause() .predicates - .push(parse2(quote! { + .push(parse_internal! { #field_assoc_type_ident: #field_constraints - })?); + }); } let where_clause = provider_generics.make_where_clause(); for field in &self.fields { let receiver_type = match &field.receiver_mode { - ReceiverMode::SelfReceiver => parse_quote!(#context_type), + ReceiverMode::SelfReceiver => parse_internal!(#context_type), ReceiverMode::Type(ty) => ty.clone(), }; let field_name = Symbol::new(field.field_name.clone()); - let tag_type: Type = parse_quote!(#field_name); + let tag_type: Type = parse_internal!(#field_name); let method = derive_getter_method( &ContextArg::Type(receiver_type.clone()), @@ -66,7 +67,7 @@ impl ItemCgpGetter { let field_type = if let Some(trait_item) = &field_assoc_type { let trait_item_ident = &trait_item.ident; - parse_quote!(#trait_item_ident) + parse_internal!(#trait_item_ident) } else { field.field_type.clone() }; @@ -80,19 +81,19 @@ impl ItemCgpGetter { where_clause .predicates - .push(parse2(quote! { #receiver_type: #constraint })?); + .push(parse_internal! { #receiver_type: #constraint }); } let (_, type_generics, _) = provider_trait.generics.split_for_impl(); let (impl_generics, _, where_clause) = provider_generics.split_for_impl(); - let item_impl: ItemImpl = parse2(quote! { + let item_impl: ItemImpl = parse_internal! { impl #impl_generics #provider_name #type_generics for UseFields #where_clause { #( #items )* } - })?; + }; Ok(ItemProviderImpl { component_type: component_name.to_type(), diff --git a/crates/macros/cgp-macro-core/src/types/cgp_getter/use_field.rs b/crates/macros/cgp-macro-core/src/types/cgp_getter/use_field.rs index aabb324f..533f4c9e 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_getter/use_field.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_getter/use_field.rs @@ -2,8 +2,9 @@ use proc_macro2::TokenStream; use quote::{ToTokens, quote}; use syn::punctuated::Punctuated; use syn::token::Plus; -use syn::{Generics, ItemImpl, Type, TypeParamBound, parse_quote, parse2}; +use syn::{Generics, ItemImpl, Type, TypeParamBound}; +use crate::functions::parse_internal; use crate::types::cgp_getter::{GetterField, ItemCgpGetter, ReceiverMode}; use crate::types::field::HasFieldBound; use crate::types::getter::{ContextArg, derive_getter_method}; @@ -37,13 +38,13 @@ impl ItemCgpGetter { let provider_name = &provider_trait.ident; let receiver_type = match &field.receiver_mode { - ReceiverMode::SelfReceiver => parse_quote!(#context_type), + ReceiverMode::SelfReceiver => parse_internal!(#context_type), ReceiverMode::Type(ty) => ty.clone(), }; let mut field_constraints: Punctuated = Punctuated::default(); - let tag_type: Type = parse_quote! { __Tag__ }; + let tag_type: Type = parse_internal! { __Tag__ }; let mut items = TokenStream::new(); @@ -54,7 +55,7 @@ impl ItemCgpGetter { provider_generics .params - .push(parse2(field_assoc_type_ident.to_token_stream())?); + .push(parse_internal(field_assoc_type_ident.to_token_stream())?); items.extend(quote! { type #field_assoc_type_ident = #field_assoc_type_ident; @@ -65,9 +66,9 @@ impl ItemCgpGetter { provider_generics .make_where_clause() .predicates - .push(parse2(quote! { + .push(parse_internal! { #field_assoc_type_ident: #field_constraints - })?); + }); } items.extend( @@ -82,7 +83,7 @@ impl ItemCgpGetter { let field_type = if let Some(trait_item) = &field_assoc_type { let trait_item_ident = &trait_item.ident; - parse_quote!(#trait_item_ident) + parse_internal!(#trait_item_ident) } else { field.field_type.clone() }; @@ -94,29 +95,29 @@ impl ItemCgpGetter { tag_type: tag_type.clone(), }; - field_constraints.push(parse_quote!(#constraint)); + field_constraints.push(parse_internal!(#constraint)); let mut where_clause = provider_generics.make_where_clause().clone(); - where_clause - .predicates - .push(parse2(quote! { #receiver_type: #field_constraints })?); + where_clause.predicates.push(parse_internal! { + #receiver_type: #field_constraints + }); let (_, type_generics, _) = provider_trait.generics.split_for_impl(); let (impl_generics, _, _) = provider_generics.split_for_impl(); let impl_generics = { - let mut generics: Generics = parse2(impl_generics.to_token_stream())?; - generics.params.push(parse_quote!(#tag_type)); + let mut generics: Generics = parse_internal(impl_generics.to_token_stream())?; + generics.params.push(parse_internal!(#tag_type)); generics }; - let use_field_impl: ItemImpl = parse2(quote! { + let use_field_impl: ItemImpl = parse_internal! { impl #impl_generics #provider_name #type_generics for UseField< #tag_type > #where_clause { #items } - })?; + }; Ok(use_field_impl) } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_getter/with_provider.rs b/crates/macros/cgp-macro-core/src/types/cgp_getter/with_provider.rs index edd8bfa8..eefd22be 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_getter/with_provider.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_getter/with_provider.rs @@ -1,7 +1,8 @@ use proc_macro2::Span; use quote::{ToTokens, quote}; -use syn::{Generics, Ident, ImplItem, ItemImpl, parse_quote, parse2}; +use syn::{Generics, Ident, ImplItem, ItemImpl}; +use crate::functions::parse_internal; use crate::types::cgp_getter::{GetterField, ItemCgpGetter, ReceiverMode}; use crate::types::getter::{ContextArg, FieldMode, derive_getter_method}; use crate::types::provider_impl::ItemProviderImpl; @@ -38,14 +39,14 @@ impl ItemCgpGetter { let provider_name = &args.provider_ident; let receiver_type = match &field.receiver_mode { - ReceiverMode::SelfReceiver => parse_quote!(#context_type), + ReceiverMode::SelfReceiver => parse_internal!(#context_type), ReceiverMode::Type(ty) => ty.clone(), }; let field_type = match field_assoc_type { Some(field_assoc_type) => { let field_assoc_type_ident = &field_assoc_type.ident; - parse_quote! { #field_assoc_type_ident } + parse_internal! { #field_assoc_type_ident } } None => field.field_type.clone(), }; @@ -61,20 +62,20 @@ impl ItemCgpGetter { provider_generics .params - .push(parse2(field_assoc_type_ident.to_token_stream())?); + .push(parse_internal(field_assoc_type_ident.to_token_stream())?); - items.push(parse2(quote! { + items.push(parse_internal! { type #field_assoc_type_ident = #field_assoc_type_ident; - })?); + }); let field_constraints = get_bounds_and_replace_self_assoc_type(field_assoc_type); provider_generics .make_where_clause() .predicates - .push(parse2(quote! { + .push(parse_internal! { #field_assoc_type_ident: #field_constraints - })?); + }); } let provider_constraint = if field.receiver_mut.is_none() { @@ -103,26 +104,26 @@ impl ItemCgpGetter { items.push(method.into()); let mut where_clause = provider_generics.make_where_clause().clone(); - where_clause - .predicates - .push(parse2(quote! { #provider_ident : #provider_constraint })?); + where_clause.predicates.push(parse_internal! { + #provider_ident : #provider_constraint + }); let (_, type_generics, _) = provider_trait.generics.split_for_impl(); let (impl_generics, _, _) = provider_generics.split_for_impl(); let impl_generics = { - let mut generics: Generics = parse2(impl_generics.to_token_stream())?; - generics.params.push(parse2(quote! { #provider_ident })?); + let mut generics: Generics = parse_internal(impl_generics.to_token_stream())?; + generics.params.push(parse_internal! { #provider_ident }); generics }; - let out = parse2(quote! { + let out = parse_internal! { impl #impl_generics #provider_name #type_generics for WithProvider< #provider_ident > #where_clause { #( #items )* } - })?; + }; Ok(out) } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_impl/item.rs b/crates/macros/cgp-macro-core/src/types/cgp_impl/item.rs index 4cdcf305..747350b5 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_impl/item.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_impl/item.rs @@ -1,6 +1,7 @@ use quote::ToTokens; -use syn::{ItemImpl, Type, parse_quote, parse2}; +use syn::{ItemImpl, Type}; +use crate::functions::parse_internal; use crate::traits::AddTypeParamBounds; use crate::types::attributes::CgpImplAttributes; use crate::types::cgp_impl::{ImplArgs, LoweredCgpImpl}; @@ -18,7 +19,7 @@ impl ItemCgpImpl { let attributes = CgpImplAttributes::parse(&item_impl.attrs)?; item_impl.attrs = attributes.raw_attributes; - let self_type: Type = parse_quote!(Self); + let self_type: Type = parse_internal!(Self); let implicit_args = ImplicitArgFields::extract_from_impl_items(&mut item_impl.items)?; implicit_args.add_type_param_bounds(&self_type, &mut item_impl.generics)?; @@ -37,18 +38,18 @@ impl ItemCgpImpl { let (provider_trait_path, context_type) = match &item_impl.trait_ { Some((_, path, _)) => { - let provider_trait_path = parse2(path.to_token_stream())?; + let provider_trait_path = parse_internal(path.to_token_stream())?; let context_type = item_impl.self_ty.as_ref().clone(); (provider_trait_path, context_type) } None => { - let provider_trait_path = parse2(item_impl.self_ty.to_token_stream())?; - let context_type = parse_quote! { __Context__ }; + let provider_trait_path = parse_internal(item_impl.self_ty.to_token_stream())?; + let context_type = parse_internal! { __Context__ }; item_impl .generics .params - .insert(0, parse_quote! { #context_type }); + .insert(0, parse_internal! { #context_type }); (provider_trait_path, context_type) } diff --git a/crates/macros/cgp-macro-core/src/types/cgp_impl/lowered.rs b/crates/macros/cgp-macro-core/src/types/cgp_impl/lowered.rs index 0bfe97b3..b5a6dca3 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_impl/lowered.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_impl/lowered.rs @@ -3,9 +3,9 @@ use quote::ToTokens; use syn::spanned::Spanned; use syn::token::For; use syn::visit_mut::VisitMut; -use syn::{Error, Ident, ImplItem, ItemImpl, Type, parse_quote, parse2}; +use syn::{Error, Ident, ImplItem, ItemImpl, Type}; -use crate::functions::to_snake_case_ident; +use crate::functions::{parse_internal, to_snake_case_ident}; use crate::types::cgp_impl::{CgpProviderOrBareImpl, ImplArgs}; use crate::types::cgp_provider::{ItemCgpProvider, ProviderArgs}; use crate::types::ident::IdentWithTypeArgs; @@ -23,7 +23,7 @@ pub struct LoweredCgpImpl { impl LoweredCgpImpl { pub fn lower(&self) -> syn::Result { - if self.args.provider_type == parse_quote!(Self) { + if self.args.provider_type == parse_internal!(Self) { if self.item_impl.trait_.is_none() { return Err(Error::new( self.item_impl.span(), @@ -57,11 +57,12 @@ impl LoweredCgpImpl { let mut provider_trait_path = self.provider_trait_path.clone(); let provider_type = &self.args.provider_type; - let context_ident = if let Ok(ident) = parse2::(context_type.to_token_stream()) { - to_snake_case_ident(&ident) - } else { - Ident::new("__context__", Span::call_site()) - }; + let context_ident = + if let Ok(ident) = parse_internal::(context_type.to_token_stream()) { + to_snake_case_ident(&ident) + } else { + Ident::new("__context__", Span::call_site()) + }; let local_assoc_types: Vec = item_impl .items @@ -82,11 +83,11 @@ impl LoweredCgpImpl { provider_trait_path .type_args .make_args() - .insert(0, parse_quote!(#context_type)); + .insert(0, parse_internal!(#context_type)); out_impl.trait_ = Some(( None, - parse2(provider_trait_path.to_token_stream())?, + parse_internal(provider_trait_path.to_token_stream())?, For(Span::call_site()), )); diff --git a/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs b/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs index ac9d856c..6ba5c312 100644 --- a/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs +++ b/crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs @@ -1,7 +1,8 @@ use quote::ToTokens; use syn::spanned::Spanned; -use syn::{Error, Ident, ItemImpl, Type, parse_quote, parse2}; +use syn::{Error, Ident, ItemImpl, Type}; +use crate::functions::parse_internal; use crate::types::cgp_provider::{LoweredCgpProvider, ProviderArgs}; use crate::types::empty_struct::EmptyStruct; use crate::types::ident::{IdentWithTypeArgs, IdentWithTypeGenerics}; @@ -36,14 +37,15 @@ impl ItemCgpProvider { Error::new(item_impl.span(), "expect provider trait name to be present") })?; - let provider_trait: IdentWithTypeArgs = parse2(provider_trait_path.to_token_stream())?; + let provider_trait: IdentWithTypeArgs = + parse_internal(provider_trait_path.to_token_stream())?; let component_ident = Ident::new( &format!("{}Component", provider_trait.ident), provider_trait.span(), ); - parse2(component_ident.to_token_stream()) + parse_internal(component_ident.to_token_stream()) } pub fn to_provider_struct(&self) -> syn::Result> { @@ -55,7 +57,7 @@ impl ItemCgpProvider { let impl_self_type = &provider_impl.self_ty; - let provider_type: IdentWithTypeGenerics = parse_quote!( #impl_self_type ); + let provider_type: IdentWithTypeGenerics = parse_internal!( #impl_self_type ); let provider_struct = EmptyStruct { ident: provider_type.ident.clone(), diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/key/path.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/key/path.rs index 9bd71af0..1b5c426f 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/key/path.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/key/path.rs @@ -1,8 +1,8 @@ use syn::parse::{Parse, ParseStream}; -use syn::parse_quote; use syn::token::At; use crate::functions::merge_generics; +use crate::parse_internal; use crate::types::delegate_component::{EvalDelegateKey, EvaluatedDelegateKey}; use crate::types::generics::ImplGenerics; use crate::types::path::PathHead; @@ -32,10 +32,10 @@ impl EvalDelegateKey for PathDelegateKey { for (inner_generics, path) in paths { let mut generics = merge_generics(outer_generics, &inner_generics); - generics.params.push(parse_quote!(__Wildcard__)); + generics.params.push(parse_internal!(__Wildcard__)); - let prefix = path.to_prefix(parse_quote!(__Wildcard__)); - let key_type = parse_quote!(#prefix); + let prefix = path.to_prefix(parse_internal!(__Wildcard__)); + let key_type = parse_internal!(#prefix); let key = EvaluatedDelegateKey { generics, diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/direct.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/direct.rs index e27694de..e98a7c0b 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/direct.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/direct.rs @@ -1,7 +1,8 @@ +use syn::Type; use syn::token::RArrow; -use syn::{Type, parse_quote}; use crate::exports::DelegateComponent; +use crate::parse_internal; use crate::types::delegate_component::{ DelegateKey, DelegateValue, EvalDelegateEntries, EvalDelegateKey, EvalDelegateValue, EvaluatedDelegateEntry, ExtractInnerDelegateTables, InnerDelegateTable, @@ -19,33 +20,32 @@ impl EvalDelegateEntries for DirectDelegateMapping { let keys = self.key.eval()?; let value_type = self.value.eval()?; - let entries = keys - .into_iter() - .map(|key| { - let key_type = key.key; - let mut generics = key.generics; - - let where_predicate = parse_quote! { - #value_type: #DelegateComponent< #key_type > - }; - - generics - .make_where_clause() - .predicates - .push(where_predicate); - - let direct_value_type = parse_quote! { - < #value_type as #DelegateComponent< #key_type > >::Delegate - }; - - EvaluatedDelegateEntry { - table_type: table_type.clone(), - generics, - key: key_type, - value: direct_value_type, - } - }) - .collect(); + let mut entries = Vec::new(); + + for key in keys { + let key_type = key.key; + let mut generics = key.generics; + + let where_predicate = parse_internal! { + #value_type: #DelegateComponent< #key_type > + }; + + generics + .make_where_clause() + .predicates + .push(where_predicate); + + let direct_value_type = parse_internal! { + < #value_type as #DelegateComponent< #key_type > >::Delegate + }; + + entries.push(EvaluatedDelegateEntry { + table_type: table_type.clone(), + generics, + key: key_type, + value: direct_value_type, + }); + } Ok(entries) } diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/eval.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/eval.rs index 70da8f30..fa77d167 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/eval.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/eval.rs @@ -1,8 +1,7 @@ -use quote::quote; -use syn::{Generics, ItemImpl, Type, parse_quote, parse2}; +use syn::{Generics, ItemImpl, Type}; use crate::exports::{DelegateComponent, IsProviderFor}; -use crate::functions::merge_generics; +use crate::functions::{merge_generics, parse_internal}; pub struct EvaluatedDelegateEntry { pub table_type: Type, @@ -33,7 +32,7 @@ impl EvaluatedDelegateEntry { let (impl_generics, _, where_clause) = generics.split_for_impl(); - parse2(quote! { + let item_impl = parse_internal! { impl #impl_generics #DelegateComponent< #key > for #table_type @@ -41,7 +40,9 @@ impl EvaluatedDelegateEntry { { type Delegate = #value; } - }) + }; + + Ok(item_impl) } pub fn build_is_provider_for_impl(&self, outer_generics: &Generics) -> syn::Result { @@ -52,22 +53,27 @@ impl EvaluatedDelegateEntry { let key = &self.key; let value = &self.value; - generics.params.push(parse_quote!(__Context__)); - generics.params.push(parse_quote!(__Params__)); + generics.params.push(parse_internal!(__Context__)); + generics.params.push(parse_internal!(__Params__)); - generics.make_where_clause().predicates.push(parse_quote! { - #value: #IsProviderFor<#key, __Context__, __Params__> - }); + generics + .make_where_clause() + .predicates + .push(parse_internal! { + #value: #IsProviderFor<#key, __Context__, __Params__> + }); let (impl_generics, _, where_clause) = generics.split_for_impl(); - parse2(quote! { + let item_impl = parse_internal! { impl #impl_generics #IsProviderFor< #key, __Context__, __Params__ > for #table_type #where_clause {} - }) + }; + + Ok(item_impl) } pub fn build_namespace_impl( @@ -82,7 +88,7 @@ impl EvaluatedDelegateEntry { let (impl_generics, _, where_clause) = generics.split_for_impl(); - parse2(quote! { + let item_impl = parse_internal! { impl #impl_generics #namespace_trait for #key @@ -90,6 +96,8 @@ impl EvaluatedDelegateEntry { { type Delegate = #value; } - }) + }; + + Ok(item_impl) } } diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/redirect.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/redirect.rs index 5e3f1558..44015fe7 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/redirect.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/mapping/redirect.rs @@ -1,7 +1,8 @@ +use syn::Type; use syn::token::FatArrow; -use syn::{Type, parse_quote}; use crate::exports::RedirectLookup; +use crate::parse_internal; use crate::types::delegate_component::{ DelegateKey, EvalDelegateEntries, EvalDelegateKey, EvaluatedDelegateEntry, }; @@ -18,14 +19,14 @@ impl EvalDelegateEntries for RedirectDelegateMapping { fn eval_entries(&self, table_type: &Type) -> syn::Result> { let value_type: Type = match &self.key { DelegateKey::Path(_) => { - let prefix = self.value.clone().to_prefix(parse_quote!(__Wildcard__)); - parse_quote! { + let prefix = self.value.clone().to_prefix(parse_internal!(__Wildcard__)); + parse_internal! { #RedirectLookup<#table_type, #prefix> } } _ => { let path = &self.value; - parse_quote!(#RedirectLookup<#table_type, #path>) + parse_internal!(#RedirectLookup<#table_type, #path>) } }; diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/eval.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/eval.rs index dfc8530e..965dd10c 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/eval.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/eval.rs @@ -1,5 +1,6 @@ -use syn::{Generics, Ident, Type, parse_quote}; +use syn::{Generics, Ident, Type}; +use crate::parse_internal; use crate::types::delegate_component::{EvalDelegateEntry, EvaluatedDelegateEntry}; use crate::types::ident::IdentWithTypeArgs; @@ -50,21 +51,21 @@ impl EvalDelegateEntry for EvaluatedForEntry { let mut namespace_generics = self.namespace.type_args.clone(); let namespace_generic_args = &mut namespace_generics.make_args(); - namespace_generic_args.push(parse_quote!(#table_type)); + namespace_generic_args.push(parse_internal!(#table_type)); - namespace_generic_args.push(parse_quote! { + namespace_generic_args.push(parse_internal! { Delegate = #mapping_value }); - parse_quote!( #namespace_ident #namespace_generics ) + parse_internal!( #namespace_ident #namespace_generics ) }; let mut generics = self.generics.clone(); - generics.params.push(parse_quote!(#for_key)); - generics.params.push(parse_quote!(#for_value)); + generics.params.push(parse_internal!(#for_key)); + generics.params.push(parse_internal!(#for_value)); let where_clause = generics.make_where_clause(); - where_clause.predicates.push(parse_quote! { + where_clause.predicates.push(parse_internal! { #for_key: #namespace_trait }); diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/namespace.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/namespace.rs index 04736773..586a85ce 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/namespace.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/namespace.rs @@ -1,7 +1,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::Semi; -use syn::{Generics, Ident, Type, parse_quote}; +use syn::{Generics, Ident, Type}; +use crate::parse_internal; use crate::types::delegate_component::{ EvalDelegateEntries, EvalForEntries, EvalForEntry, EvaluatedDelegateEntry, EvaluatedForEntry, eval_delegate_entries_via_for, @@ -35,10 +36,10 @@ impl EvalForEntry for NamespaceDelegateStatement { let entry = EvaluatedForEntry { generics: Generics::default(), table_type: table_type.clone(), - for_key: parse_quote!(__Key__), - for_value: parse_quote!(__Value__), - mapping_key: parse_quote!(__Key__), - mapping_value: parse_quote!(__Value__), + for_key: parse_internal!(__Key__), + for_value: parse_internal!(__Value__), + mapping_key: parse_internal!(__Key__), + mapping_value: parse_internal!(__Value__), namespace: self.ident.clone().into(), }; diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/open.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/open.rs index 29f99539..1d1dbd97 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/statement/open.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/statement/open.rs @@ -1,9 +1,10 @@ use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::token::{Comma, Semi}; -use syn::{Type, braced, parse_quote}; +use syn::{Type, braced}; use crate::exports::{Nil, PathCons, RedirectLookup}; +use crate::parse_internal; use crate::types::delegate_component::{EvalDelegateEntries, EvaluatedDelegateEntry}; use crate::types::keyword::Keyword; use crate::types::keywords::Open; @@ -41,7 +42,7 @@ impl EvalDelegateEntries for OpenDelegateStatement { let mut entries = Vec::new(); for component in &self.components { - let value: Type = parse_quote! { + let value: Type = parse_internal! { #RedirectLookup< #table_type, #PathCons<#component, #Nil>, diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/table/inner.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/table/inner.rs index c353b4d8..fa9bbf4e 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/table/inner.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/table/inner.rs @@ -1,7 +1,7 @@ -use quote::quote; use syn::parse::{Parse, ParseStream}; -use syn::{Ident, ItemImpl, Type, braced, parse2}; +use syn::{Ident, ItemImpl, Type, braced}; +use crate::functions::parse_internal; use crate::types::delegate_component::DelegateEntries; use crate::types::empty_struct::EmptyStruct; use crate::types::generics::TypeGenerics; @@ -43,7 +43,8 @@ impl InnerDelegateTable { let ident = &self.table_ident; let type_generics = self.table_generics.split_for_impl().1; - parse2(quote!( #ident #type_generics )) + let ty = parse_internal!( #ident #type_generics ); + Ok(ty) } pub fn build_table_struct(&self) -> EmptyStruct { diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/table/main.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/table/main.rs index 22f977c9..f6d0b53d 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/table/main.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/table/main.rs @@ -1,8 +1,9 @@ use proc_macro2::TokenStream; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; -use syn::{ItemImpl, Type, braced, parse2}; +use syn::{ItemImpl, Type, braced}; +use crate::functions::parse_internal; use crate::traits::ParseOptionalKeyword; use crate::types::delegate_component::{DelegateEntries, ExtractInnerDelegateTables}; use crate::types::empty_struct::EmptyStruct; @@ -53,7 +54,8 @@ impl DelegateTable { let mut item_structs = Vec::new(); if self.new.is_some() { - let struct_type: IdentWithTypeGenerics = parse2(self.table_type.to_token_stream())?; + let struct_type: IdentWithTypeGenerics = + parse_internal(self.table_type.to_token_stream())?; item_structs.push(EmptyStruct { ident: struct_type.ident, generics: struct_type.type_generics.generics, diff --git a/crates/macros/cgp-macro-core/src/types/delegate_component/value/inner_table.rs b/crates/macros/cgp-macro-core/src/types/delegate_component/value/inner_table.rs index d09e42c0..0be4b7d4 100644 --- a/crates/macros/cgp-macro-core/src/types/delegate_component/value/inner_table.rs +++ b/crates/macros/cgp-macro-core/src/types/delegate_component/value/inner_table.rs @@ -1,7 +1,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::{Gt, Lt}; -use syn::{Ident, Type, parse_quote}; +use syn::{Ident, Type}; +use crate::parse_internal; use crate::types::delegate_component::{ EvalDelegateValue, ExtractInnerDelegateTables, InnerDelegateTable, }; @@ -41,7 +42,7 @@ impl EvalDelegateValue for DelegateValueWithInnerTable { let struct_ident = &self.inner_table.table_ident; let struct_generics = &self.inner_table.table_generics; - let ty = parse_quote!( #wrapper_ident < #struct_ident #struct_generics > ); + let ty = parse_internal!( #wrapper_ident < #struct_ident #struct_generics > ); Ok(ty) } } diff --git a/crates/macros/cgp-macro-core/src/types/generics/impl_generics.rs b/crates/macros/cgp-macro-core/src/types/generics/impl_generics.rs index 614b50b1..f29f27c6 100644 --- a/crates/macros/cgp-macro-core/src/types/generics/impl_generics.rs +++ b/crates/macros/cgp-macro-core/src/types/generics/impl_generics.rs @@ -3,7 +3,9 @@ use core::ops::{Deref, DerefMut}; use proc_macro2::TokenStream; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; -use syn::{Error, Generics, parse2}; +use syn::{Error, Generics}; + +use crate::functions::parse_internal; #[derive(Debug, Clone, Default)] pub struct ImplGenerics { @@ -30,7 +32,7 @@ impl Parse for ImplGenerics { let (impl_generics, _, _) = generics.split_for_impl(); - let generics2: Generics = parse2(impl_generics.to_token_stream())?; + let generics2: Generics = parse_internal(impl_generics.to_token_stream())?; if generics != generics2 { return Err(Error::new_spanned(generics, "invalid impl generics syntax")); diff --git a/crates/macros/cgp-macro-core/src/types/generics/type_generics.rs b/crates/macros/cgp-macro-core/src/types/generics/type_generics.rs index 4924f0d3..664b21ab 100644 --- a/crates/macros/cgp-macro-core/src/types/generics/type_generics.rs +++ b/crates/macros/cgp-macro-core/src/types/generics/type_generics.rs @@ -3,7 +3,9 @@ use core::ops::{Deref, DerefMut}; use proc_macro2::TokenStream; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; -use syn::{Error, Generics, parse2}; +use syn::{Error, Generics}; + +use crate::functions::parse_internal; #[derive(Debug, Clone, Default)] pub struct TypeGenerics { @@ -30,7 +32,7 @@ impl Parse for TypeGenerics { let (_, type_generics, _) = generics.split_for_impl(); - let generics2: Generics = parse2(type_generics.to_token_stream())?; + let generics2: Generics = parse_internal(type_generics.to_token_stream())?; if generics != generics2 { return Err(Error::new_spanned(generics, "invalid type generics syntax")); @@ -45,7 +47,7 @@ impl<'a> TryFrom<&'a Generics> for TypeGenerics { fn try_from(generics: &'a Generics) -> syn::Result { let (_, type_generics, _) = generics.split_for_impl(); - let generics = parse2(type_generics.to_token_stream())?; + let generics = parse_internal(type_generics.to_token_stream())?; Ok(Self { generics }) } } diff --git a/crates/macros/cgp-macro-core/src/types/getter/method.rs b/crates/macros/cgp-macro-core/src/types/getter/method.rs index 9718061e..11ee4bfa 100644 --- a/crates/macros/cgp-macro-core/src/types/getter/method.rs +++ b/crates/macros/cgp-macro-core/src/types/getter/method.rs @@ -1,8 +1,9 @@ use proc_macro2::TokenStream; use quote::quote; use syn::token::Mut; -use syn::{Ident, ImplItemFn, Type, parse2}; +use syn::{Ident, ImplItemFn, Type}; +use crate::functions::parse_internal; use crate::types::cgp_getter::GetterField; use crate::types::getter::{ContextArg, FieldMode}; @@ -101,11 +102,13 @@ impl GetterMethod { let return_type = &getter_field.return_type; - parse2(quote! { + let item_fn = parse_internal! { fn #getter_ident( #context_fn_arg #phantom_arg ) -> #return_type { #call_expr } - }) + }; + + Ok(item_fn) } } diff --git a/crates/macros/cgp-macro-core/src/types/implicits/arg_field.rs b/crates/macros/cgp-macro-core/src/types/implicits/arg_field.rs index da7ff56a..ff66912a 100644 --- a/crates/macros/cgp-macro-core/src/types/implicits/arg_field.rs +++ b/crates/macros/cgp-macro-core/src/types/implicits/arg_field.rs @@ -1,6 +1,7 @@ use syn::token::Mut; -use syn::{Ident, Stmt, Type, parse_quote}; +use syn::{Ident, Stmt, Type}; +use crate::parse_internal; use crate::types::field::{FieldName, HasFieldBound}; use crate::types::getter::{FieldMode, GetFieldExpr, GetFieldWithModeExpr}; @@ -16,7 +17,7 @@ pub struct ImplicitArgField { impl ImplicitArgField { pub fn to_has_field_bound(&self) -> syn::Result { let field_name = FieldName::from(self.field_name.clone()); - let tag_type = parse_quote!(#field_name); + let tag_type = parse_internal!(#field_name); Ok(HasFieldBound { field_type: self.field_type.clone(), @@ -33,13 +34,13 @@ impl ImplicitArgField { let get_field_expr = GetFieldWithModeExpr { field_mode: self.field_mode.clone(), get_field: GetFieldExpr { - receiver: parse_quote!(self), + receiver: parse_internal!(self), field_mut: self.field_mut, field_name: self.field_name.clone().into(), }, }; - let statement = parse_quote! { + let statement = parse_internal! { let #field_name: #arg_type = #get_field_expr; }; diff --git a/crates/macros/cgp-macro-core/src/types/implicits/arg_fields.rs b/crates/macros/cgp-macro-core/src/types/implicits/arg_fields.rs index d79e8e8f..600e212f 100644 --- a/crates/macros/cgp-macro-core/src/types/implicits/arg_fields.rs +++ b/crates/macros/cgp-macro-core/src/types/implicits/arg_fields.rs @@ -1,8 +1,9 @@ use syn::punctuated::Punctuated; use syn::token::Plus; -use syn::{Block, ImplItem, TypeParamBound, parse_quote}; +use syn::{Block, ImplItem, TypeParamBound}; use crate::functions::extract_and_parse_implicit_args; +use crate::parse_internal; use crate::traits::ToTypeParamBounds; use crate::types::implicits::ImplicitArgField; @@ -23,7 +24,7 @@ impl ToTypeParamBounds for ImplicitArgFields { for field in &self.fields { let constraint = field.to_has_field_bound()?; - constraints.push(parse_quote!(#constraint)); + constraints.push(parse_internal!(#constraint)); } Ok(constraints) diff --git a/crates/macros/cgp-macro-core/src/types/namespace/inherit.rs b/crates/macros/cgp-macro-core/src/types/namespace/inherit.rs index e565c5ab..836fe8ec 100644 --- a/crates/macros/cgp-macro-core/src/types/namespace/inherit.rs +++ b/crates/macros/cgp-macro-core/src/types/namespace/inherit.rs @@ -1,5 +1,6 @@ -use syn::{Generics, Ident, Type, parse_quote}; +use syn::{Generics, Ident, Type}; +use crate::parse_internal; use crate::types::delegate_component::{EvalForEntry, EvaluatedForEntry}; use crate::types::ident::IdentWithTypeArgs; @@ -17,20 +18,23 @@ impl EvalForEntry for InheritNamespaceStatement { namespace_constraint .type_args .make_args() - .push(parse_quote!(#local_table_ident)); + .push(parse_internal!(#local_table_ident)); let mut generics = Generics::default(); - generics.make_where_clause().predicates.push(parse_quote! { - __Key__: #namespace_constraint - }); + generics + .make_where_clause() + .predicates + .push(parse_internal! { + __Key__: #namespace_constraint + }); let for_entry = EvaluatedForEntry { generics, table_type: table_type.clone(), - for_key: parse_quote!(__Key__), - for_value: parse_quote!(__Value__), - mapping_key: parse_quote!(__Key__), - mapping_value: parse_quote!(__Value__), + for_key: parse_internal!(__Key__), + for_value: parse_internal!(__Value__), + mapping_key: parse_internal!(__Key__), + mapping_value: parse_internal!(__Value__), namespace: self.namespace.clone(), }; diff --git a/crates/macros/cgp-macro-core/src/types/namespace/table.rs b/crates/macros/cgp-macro-core/src/types/namespace/table.rs index 2d5e4c58..54938e9e 100644 --- a/crates/macros/cgp-macro-core/src/types/namespace/table.rs +++ b/crates/macros/cgp-macro-core/src/types/namespace/table.rs @@ -1,7 +1,8 @@ use syn::parse::{Parse, ParseStream}; use syn::token::Colon; -use syn::{Error, Ident, ItemImpl, ItemStruct, ItemTrait, Type, braced, parse_quote}; +use syn::{Error, Ident, ItemImpl, ItemStruct, ItemTrait, Type, braced}; +use crate::parse_internal; use crate::traits::ParseOptionalKeyword; use crate::types::delegate_component::{ DelegateEntries, EvalDelegateEntries, EvalDelegateEntry, EvalForEntry, @@ -56,9 +57,9 @@ impl NamespaceTable { pub fn build_namespace_trait(&self) -> syn::Result { let namespace_ident = &self.namespace.ident; let mut namespace_generics = self.namespace.type_generics.clone(); - namespace_generics.params.push(parse_quote!(__Table__)); + namespace_generics.params.push(parse_internal!(__Table__)); - let namespace_trait: Type = parse_quote!( #namespace_ident #namespace_generics ); + let namespace_trait: Type = parse_internal!( #namespace_ident #namespace_generics ); Ok(namespace_trait) } @@ -66,7 +67,7 @@ impl NamespaceTable { let namespace_trait = self.build_namespace_trait()?; let item_trait: Option = if self.new.is_some() { - let item_trait = parse_quote! { + let item_trait = parse_internal! { pub trait #namespace_trait { type Delegate; } @@ -82,10 +83,10 @@ impl NamespaceTable { pub fn build_item_impls(&self) -> syn::Result> { let mut impl_generics = self.impl_generics.clone(); - impl_generics.params.push(parse_quote!(__Table__)); + impl_generics.params.push(parse_internal!(__Table__)); let namespace_trait = self.build_namespace_trait()?; - let table_type: Type = parse_quote!(__Table__); + let table_type: Type = parse_internal!(__Table__); let evaluated_entries = self.entries.eval_entries(&table_type)?; @@ -115,14 +116,14 @@ impl NamespaceTable { let namespace_ident = self.namespace.ident.clone(); - let table_type: Type = parse_quote!(__Table__); + let table_type: Type = parse_internal!(__Table__); let namespace_struct_ident = Ident::new( &format!("__{}Components", namespace_ident), namespace_ident.span(), ); - let namespace_struct: ItemStruct = parse_quote! { + let namespace_struct: ItemStruct = parse_internal! { pub struct #namespace_struct_ident; }; @@ -137,7 +138,7 @@ impl NamespaceTable { let namespace_trait = self.build_namespace_trait()?; let mut generics = self.impl_generics.generics.clone(); - generics.params.push(parse_quote!(#table_type)); + generics.params.push(parse_internal!(#table_type)); let item_impl = evaluated_entry.build_namespace_impl(&namespace_trait, &generics)?; diff --git a/crates/macros/cgp-macro-core/src/types/path/path_element.rs b/crates/macros/cgp-macro-core/src/types/path/path_element.rs index 75936d08..285ff91d 100644 --- a/crates/macros/cgp-macro-core/src/types/path/path_element.rs +++ b/crates/macros/cgp-macro-core/src/types/path/path_element.rs @@ -1,8 +1,9 @@ use proc_macro2::TokenStream; use quote::ToTokens; use syn::parse::{Parse, ParseStream}; -use syn::{Ident, Type, parse2}; +use syn::{Ident, Type}; +use crate::functions::parse_internal; use crate::traits::ToType; use crate::types::field::Symbol; @@ -16,7 +17,7 @@ impl Parse for PathElement { fn parse(input: ParseStream) -> syn::Result { let ty: Type = input.parse()?; - let parsed = if let Ok(path_ident) = parse2::(ty.to_token_stream()) { + let parsed = if let Ok(path_ident) = parse_internal::(ty.to_token_stream()) { let path_str = path_ident.to_string(); if let Some(path_char) = path_str.chars().next() diff --git a/crates/macros/cgp-macro-core/src/types/provider_impl.rs b/crates/macros/cgp-macro-core/src/types/provider_impl.rs index fa2f0eca..a6b7cff2 100644 --- a/crates/macros/cgp-macro-core/src/types/provider_impl.rs +++ b/crates/macros/cgp-macro-core/src/types/provider_impl.rs @@ -4,9 +4,10 @@ use proc_macro2::Span; use quote::ToTokens; use syn::spanned::Spanned; use syn::token::For; -use syn::{Error, ItemImpl, Path, Type, parse_quote, parse2}; +use syn::{Error, ItemImpl, Path, Type}; use crate::exports::IsProviderFor; +use crate::functions::parse_internal; use crate::types::cgp_provider::ProviderImplArgs; use crate::types::ident::IdentWithTypeArgs; use crate::visitors::replace_provider_in_generics; @@ -61,13 +62,13 @@ impl ItemProviderImpl { let IdentWithTypeArgs { ident: provider_ident, type_args: provider_generics, - } = parse2(provider_path.to_token_stream())?; + } = parse_internal(provider_path.to_token_stream())?; let impl_args = ProviderImplArgs::from_generic_args(&provider_generics)?; let context_type = &impl_args.context_type; let is_provider_path: Path = - parse_quote!( #IsProviderFor < #component_type, #context_type, ( #impl_args ) > ); + parse_internal!( #IsProviderFor < #component_type, #context_type, ( #impl_args ) > ); let mut is_provider_impl = item_impl.clone(); diff --git a/crates/macros/cgp-macro-core/src/vendor.rs b/crates/macros/cgp-macro-core/src/vendor.rs new file mode 100644 index 00000000..402f42de --- /dev/null +++ b/crates/macros/cgp-macro-core/src/vendor.rs @@ -0,0 +1 @@ +pub use quote::quote;