From e4003cc25efcae79c1332e3481d7edfca1067c4f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 10 Sep 2019 15:04:26 +0200 Subject: Allows using match_inputs outside of a make_parser impl --- pest_consume_macros/src/make_parser.rs | 15 ++++----------- pest_consume_macros/src/match_inputs.rs | 28 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'pest_consume_macros/src') diff --git a/pest_consume_macros/src/make_parser.rs b/pest_consume_macros/src/make_parser.rs index 0638c19..8a15deb 100644 --- a/pest_consume_macros/src/make_parser.rs +++ b/pest_consume_macros/src/make_parser.rs @@ -6,7 +6,7 @@ use syn::parse::{Parse, ParseStream, Result}; use syn::spanned::Spanned; use syn::{ parse_quote, Error, Expr, FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, - LitBool, Pat, Token, + LitBool, Pat, Path, Token, }; mod kw { @@ -14,8 +14,8 @@ mod kw { } struct MakeParserAttrs { - parser: Ident, - rule_enum: Ident, + parser: Path, + rule_enum: Path, } struct AliasArgs { @@ -159,7 +159,7 @@ fn parse_fn<'a>( }) } -fn apply_special_attrs(f: &mut ParsedFn, rule_enum: &Ident) -> Result<()> { +fn apply_special_attrs(f: &mut ParsedFn, rule_enum: &Path) -> Result<()> { let function = &mut *f.function; let fn_name = &f.fn_name; let input_arg = &f.input_arg; @@ -169,13 +169,6 @@ fn apply_special_attrs(f: &mut ParsedFn, rule_enum: &Ident) -> Result<()> { #function ); - // `prec_climb` attr - let _: () = function - .attrs - .drain_filter(|attr| attr.path.is_ident("entrypoint")) - .map(|_| ()) - .collect(); - // `prec_climb` attr let prec_climb_attrs: Vec<_> = function .attrs diff --git a/pest_consume_macros/src/match_inputs.rs b/pest_consume_macros/src/match_inputs.rs index 3a325a6..d4bc492 100644 --- a/pest_consume_macros/src/match_inputs.rs +++ b/pest_consume_macros/src/match_inputs.rs @@ -3,7 +3,10 @@ use quote::quote; use syn::parse::{Parse, ParseStream, Result}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; -use syn::{bracketed, parenthesized, token, Error, Expr, Ident, Pat, Token}; +use syn::{ + bracketed, parenthesized, parse_quote, token, Error, Expr, Ident, Pat, + Token, Type, +}; #[derive(Debug, Clone)] struct ChildrenBranch { @@ -20,6 +23,7 @@ enum ChildrenBranchPatternItem { #[derive(Debug, Clone)] struct ParseChildrenInput { + consumer: Type, input_expr: Expr, branches: Punctuated, } @@ -62,11 +66,21 @@ impl Parse for ChildrenBranchPatternItem { impl Parse for ParseChildrenInput { fn parse(input: ParseStream) -> Result { + let consumer = if input.peek(token::Lt) { + let _: token::Lt = input.parse()?; + let consumer = input.parse()?; + let _: token::Gt = input.parse()?; + let _: Token![;] = input.parse()?; + consumer + } else { + parse_quote!(Self) + }; let input_expr = input.parse()?; let _: Token![;] = input.parse()?; let branches = Punctuated::parse_terminated(input)?; Ok(ParseChildrenInput { + consumer, input_expr, branches, }) @@ -76,6 +90,7 @@ impl Parse for ParseChildrenInput { fn make_parser_branch( branch: &ChildrenBranch, i_inputs: &Ident, + consumer: &Type, ) -> Result { use ChildrenBranchPatternItem::{Multiple, Single}; @@ -139,7 +154,7 @@ fn make_parser_branch( let mut parses = Vec::new(); for (rule_name, binder) in singles_before_multiple.into_iter() { parses.push(quote!( - let #binder = Self::#rule_name( + let #binder = #consumer::#rule_name( #i_inputs.next().unwrap() )?; )) @@ -148,7 +163,7 @@ fn make_parser_branch( // only the unmatched inputs are left for the variable-length pattern, if any. for (rule_name, binder) in singles_after_multiple.into_iter().rev() { parses.push(quote!( - let #binder = Self::#rule_name( + let #binder = #consumer::#rule_name( #i_inputs.next_back().unwrap() )?; )) @@ -156,7 +171,7 @@ fn make_parser_branch( if let Some((rule_name, binder)) = multiple { parses.push(quote!( let #binder = #i_inputs - .map(|i| Self::#rule_name(i)) + .map(|i| #consumer::#rule_name(i)) .collect::, _>>()? .into_iter(); )) @@ -179,17 +194,18 @@ pub fn match_inputs( let i_inputs = Ident::new("___inputs", Span::call_site()); let input_expr = &input.input_expr; + let consumer = &input.consumer; let branches = input .branches .iter() - .map(|br| make_parser_branch(br, &i_inputs)) + .map(|br| make_parser_branch(br, &i_inputs, consumer)) .collect::>>()?; Ok(quote!({ #[allow(unused_mut)] let mut #i_inputs = #input_expr; - let #i_input_rules = #i_inputs.aliased_rules::(); + let #i_input_rules = #i_inputs.aliased_rules::<#consumer>(); let #i_input_rules: Vec<&str> = #i_input_rules .iter() .map(String::as_str) -- cgit v1.2.3