From 97d74f514bd8c5c4b96fb4f4071f4a93ac28572d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 2 Sep 2019 18:59:30 +0200 Subject: Separate both parser proc_macros into their own files --- dhall_proc_macros/src/make_parser.rs | 90 ++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 dhall_proc_macros/src/make_parser.rs (limited to 'dhall_proc_macros/src/make_parser.rs') diff --git a/dhall_proc_macros/src/make_parser.rs b/dhall_proc_macros/src/make_parser.rs new file mode 100644 index 0000000..63ce779 --- /dev/null +++ b/dhall_proc_macros/src/make_parser.rs @@ -0,0 +1,90 @@ +use quote::quote; +use syn::parse::{ParseStream, Result}; +use syn::spanned::Spanned; +use syn::{ + parse_quote, Error, Expr, Ident, ImplItem, ImplItemMethod, ItemImpl, + ReturnType, Token, +}; + +fn apply_special_attrs( + rule_enum: &Ident, + function: &mut ImplItemMethod, +) -> Result<()> { + let recognized_attrs: Vec<_> = function + .attrs + .drain_filter(|attr| attr.path.is_ident("prec_climb")) + .collect(); + + let name = function.sig.ident.clone(); + let output_type = match &function.sig.output { + ReturnType::Default => parse_quote!(()), + ReturnType::Type(_, t) => (**t).clone(), + }; + + if recognized_attrs.is_empty() { + } else if recognized_attrs.len() > 1 { + return Err(Error::new( + recognized_attrs[1].span(), + "expected a single prec_climb attribute", + )); + } else { + let attr = recognized_attrs.into_iter().next().unwrap(); + let (child_rule, climber) = + attr.parse_args_with(|input: ParseStream| { + let child_rule: Ident = input.parse()?; + let _: Token![,] = input.parse()?; + let climber: Expr = input.parse()?; + Ok((child_rule, climber)) + })?; + + *function = parse_quote!( + fn #name<'a>( + input: ParseInput<'a, #rule_enum>, + ) -> #output_type { + #[allow(non_snake_case, dead_code)] + #function + + #climber.climb( + input.pair.clone().into_inner(), + |p| Self::#child_rule(input.with_pair(p)), + |l, op, r| { + #name(input.clone(), l?, op, r?) + }, + ) + } + ); + } + + *function = parse_quote!( + #[allow(non_snake_case, dead_code)] + #function + ); + + Ok(()) +} + +pub fn make_parser( + attrs: proc_macro::TokenStream, + input: proc_macro::TokenStream, +) -> Result { + let rule_enum: Ident = syn::parse(attrs)?; + + let mut imp: ItemImpl = syn::parse(input)?; + imp.items + .iter_mut() + .map(|item| match item { + ImplItem::Method(m) => apply_special_attrs(&rule_enum, m), + _ => Ok(()), + }) + .collect::>()?; + + let ty = &imp.self_ty; + let (impl_generics, _, where_clause) = imp.generics.split_for_impl(); + Ok(quote!( + impl #impl_generics PestConsumer for #ty #where_clause { + type RuleEnum = #rule_enum; + } + + #imp + )) +} -- cgit v1.2.3 From 5dde11c8ffb13fbaf5dbc9c2b544270c22a7d2f5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 2 Sep 2019 22:50:16 +0200 Subject: Parse polymorphically in the Embed parameter --- dhall_proc_macros/src/make_parser.rs | 83 ++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 31 deletions(-) (limited to 'dhall_proc_macros/src/make_parser.rs') diff --git a/dhall_proc_macros/src/make_parser.rs b/dhall_proc_macros/src/make_parser.rs index 63ce779..268a639 100644 --- a/dhall_proc_macros/src/make_parser.rs +++ b/dhall_proc_macros/src/make_parser.rs @@ -2,26 +2,25 @@ use quote::quote; use syn::parse::{ParseStream, Result}; use syn::spanned::Spanned; use syn::{ - parse_quote, Error, Expr, Ident, ImplItem, ImplItemMethod, ItemImpl, - ReturnType, Token, + parse_quote, Error, Expr, FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, + Pat, Token, }; -fn apply_special_attrs( - rule_enum: &Ident, - function: &mut ImplItemMethod, -) -> Result<()> { +fn apply_special_attrs(function: &mut ImplItemMethod) -> Result<()> { + *function = parse_quote!( + #[allow(non_snake_case, dead_code)] + #function + ); + let recognized_attrs: Vec<_> = function .attrs .drain_filter(|attr| attr.path.is_ident("prec_climb")) .collect(); let name = function.sig.ident.clone(); - let output_type = match &function.sig.output { - ReturnType::Default => parse_quote!(()), - ReturnType::Type(_, t) => (**t).clone(), - }; if recognized_attrs.is_empty() { + // do nothing } else if recognized_attrs.len() > 1 { return Err(Error::new( recognized_attrs[1].span(), @@ -37,28 +36,50 @@ fn apply_special_attrs( Ok((child_rule, climber)) })?; - *function = parse_quote!( - fn #name<'a>( - input: ParseInput<'a, #rule_enum>, - ) -> #output_type { - #[allow(non_snake_case, dead_code)] - #function - - #climber.climb( - input.pair.clone().into_inner(), - |p| Self::#child_rule(input.with_pair(p)), - |l, op, r| { - #name(input.clone(), l?, op, r?) - }, - ) + // Get the name of the first (`input`) function argument + let first_arg = function.sig.inputs.first().ok_or_else(|| { + Error::new( + function.sig.inputs.span(), + "a prec_climb function needs 4 arguments", + ) + })?; + let first_arg = match &first_arg { + FnArg::Receiver(_) => return Err(Error::new( + first_arg.span(), + "a prec_climb function should not have a `self` argument", + )), + FnArg::Typed(first_arg) => match &*first_arg.pat{ + Pat::Ident(ident) => &ident.ident, + _ => return Err(Error::new( + first_arg.span(), + "this argument should be a plain identifier instead of a pattern", + )), } - ); - } + }; - *function = parse_quote!( - #[allow(non_snake_case, dead_code)] - #function - ); + function.block = parse_quote!({ + #function + + #climber.climb( + #first_arg.pair.clone().into_inner(), + |p| Self::#child_rule(#first_arg.with_pair(p)), + |l, op, r| { + #name(#first_arg.clone(), l?, op, r?) + }, + ) + }); + // Remove the 3 last arguments to keep only the `input` one + function.sig.inputs.pop(); + function.sig.inputs.pop(); + function.sig.inputs.pop(); + // Check that an argument remains + function.sig.inputs.first().ok_or_else(|| { + Error::new( + function.sig.inputs.span(), + "a prec_climb function needs 4 arguments", + ) + })?; + } Ok(()) } @@ -73,7 +94,7 @@ pub fn make_parser( imp.items .iter_mut() .map(|item| match item { - ImplItem::Method(m) => apply_special_attrs(&rule_enum, m), + ImplItem::Method(m) => apply_special_attrs(m), _ => Ok(()), }) .collect::>()?; -- cgit v1.2.3