diff options
Diffstat (limited to 'vendor/pest_generator/src')
-rw-r--r-- | vendor/pest_generator/src/generator.rs | 72 | ||||
-rw-r--r-- | vendor/pest_generator/src/macros.rs | 14 |
2 files changed, 58 insertions, 28 deletions
diff --git a/vendor/pest_generator/src/generator.rs b/vendor/pest_generator/src/generator.rs index bed56f34a..0d3051e27 100644 --- a/vendor/pest_generator/src/generator.rs +++ b/vendor/pest_generator/src/generator.rs @@ -17,7 +17,6 @@ use pest_meta::ast::*; use pest_meta::optimizer::*; use pest_meta::UNICODE_PROPERTY_NAMES; -#[allow(clippy::needless_pass_by_value)] pub fn generate( name: Ident, generics: &Generics, @@ -52,17 +51,20 @@ pub fn generate( let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let result = result_type(); + let parser_impl = quote! { #[allow(clippy::all)] impl #impl_generics ::pest::Parser<Rule> for #name #ty_generics #where_clause { fn parse<'i>( rule: Rule, input: &'i str - ) -> ::std::result::Result< + ) -> #result< ::pest::iterators::Pairs<'i, Rule>, ::pest::error::Error<Rule> > { mod rules { + #![allow(clippy::upper_case_acronyms)] pub mod hidden { use super::super::Rule; #skip @@ -98,7 +100,7 @@ fn generate_builtin_rules() -> Vec<(&'static str, TokenStream)> { let mut builtins = Vec::new(); insert_builtin!(builtins, ANY, state.skip(1)); - insert_public_builtin!( + insert_builtin!( builtins, EOI, state.rule(Rule::EOI, |state| state.end_of_input()) @@ -149,13 +151,15 @@ fn generate_builtin_rules() -> Vec<(&'static str, TokenStream)> { .or_else(|state| state.match_string("\r")) ); + let box_ty = box_type(); + for property in UNICODE_PROPERTY_NAMES { let property_ident: Ident = syn::parse_str(property).unwrap(); // insert manually for #property substitution builtins.push((property, quote! { #[inline] #[allow(dead_code, non_snake_case, unused_variables)] - fn #property_ident(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + fn #property_ident(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.match_char_by(::pest::unicode::#property_ident) } })); @@ -183,7 +187,7 @@ fn generate_enum(rules: &[OptimizedRule], uses_eoi: bool) -> TokenStream { .map(|rule| Ident::new(rule.name.as_str(), Span::call_site())); if uses_eoi { quote! { - #[allow(dead_code, non_camel_case_types)] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Rule { EOI, @@ -192,7 +196,7 @@ fn generate_enum(rules: &[OptimizedRule], uses_eoi: bool) -> TokenStream { } } else { quote! { - #[allow(dead_code, non_camel_case_types)] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Rule { #( #rules ),* @@ -239,11 +243,13 @@ fn generate_rule(rule: OptimizedRule) -> TokenStream { generate_expr(rule.expr) }; + let box_ty = box_type(); + match rule.ty { RuleType::Normal => quote! { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn #name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn #name(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.rule(Rule::#name, |state| { #expr }) @@ -252,14 +258,14 @@ fn generate_rule(rule: OptimizedRule) -> TokenStream { RuleType::Silent => quote! { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn #name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn #name(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { #expr } }, RuleType::Atomic => quote! { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn #name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn #name(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.rule(Rule::#name, |state| { state.atomic(::pest::Atomicity::Atomic, |state| { #expr @@ -270,7 +276,7 @@ fn generate_rule(rule: OptimizedRule) -> TokenStream { RuleType::CompoundAtomic => quote! { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn #name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn #name(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.atomic(::pest::Atomicity::CompoundAtomic, |state| { state.rule(Rule::#name, |state| { #expr @@ -281,7 +287,7 @@ fn generate_rule(rule: OptimizedRule) -> TokenStream { RuleType::NonAtomic => quote! { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn #name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn #name(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.atomic(::pest::Atomicity::NonAtomic, |state| { state.rule(Rule::#name, |state| { #expr @@ -619,13 +625,38 @@ struct QuoteOption<T>(Option<T>); impl<T: ToTokens> ToTokens for QuoteOption<T> { fn to_tokens(&self, tokens: &mut TokenStream) { + let option = option_type(); tokens.append_all(match self.0 { - Some(ref t) => quote! { ::std::option::Option::Some(#t) }, - None => quote! { ::std::option::Option::None }, + Some(ref t) => quote! { #option::Some(#t) }, + None => quote! { #option::None }, }); } } +fn box_type() -> TokenStream { + #[cfg(feature = "std")] + quote! { ::std::boxed::Box } + + #[cfg(not(feature = "std"))] + quote! { ::alloc::boxed::Box } +} + +fn result_type() -> TokenStream { + #[cfg(feature = "std")] + quote! { ::std::result::Result } + + #[cfg(not(feature = "std"))] + quote! { ::core::result::Result } +} + +fn option_type() -> TokenStream { + #[cfg(feature = "std")] + quote! { ::std::option::Option } + + #[cfg(not(feature = "std"))] + quote! { ::core::option::Option } +} + #[cfg(test)] mod tests { use super::*; @@ -641,7 +672,7 @@ mod tests { assert_eq!( generate_enum(&rules, false).to_string(), quote! { - #[allow(dead_code, non_camel_case_types)] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Rule { f @@ -935,6 +966,8 @@ mod tests { expr: OptimizedExpr::Str("b".to_owned()), }]; let defaults = vec!["ANY"]; + let result = result_type(); + let box_ty = box_type(); let mut current_dir = std::env::current_dir().expect("Unable to get current directory"); current_dir.push("test.pest"); let test_path = current_dir.to_str().expect("path contains invalid unicode"); @@ -944,7 +977,7 @@ mod tests { #[allow(non_upper_case_globals)] const _PEST_GRAMMAR_MyParser: &'static str = include_str!(#test_path); - #[allow(dead_code, non_camel_case_types)] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum Rule { a @@ -955,17 +988,18 @@ mod tests { fn parse<'i>( rule: Rule, input: &'i str - ) -> ::std::result::Result< + ) -> #result< ::pest::iterators::Pairs<'i, Rule>, ::pest::error::Error<Rule> > { mod rules { + #![allow(clippy::upper_case_acronyms)] pub mod hidden { use super::super::Rule; #[inline] #[allow(dead_code, non_snake_case, unused_variables)] - pub fn skip(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn skip(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { Ok(state) } } @@ -975,13 +1009,13 @@ mod tests { #[inline] #[allow(non_snake_case, unused_variables)] - pub fn a(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn a(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.match_string("b") } #[inline] #[allow(dead_code, non_snake_case, unused_variables)] - pub fn ANY(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn ANY(state: #box_ty<::pest::ParserState<Rule>>) -> ::pest::ParseResult<#box_ty<::pest::ParserState<Rule>>> { state.skip(1) } } diff --git a/vendor/pest_generator/src/macros.rs b/vendor/pest_generator/src/macros.rs index 9d02725da..37ef531f3 100644 --- a/vendor/pest_generator/src/macros.rs +++ b/vendor/pest_generator/src/macros.rs @@ -13,30 +13,26 @@ macro_rules! insert_builtin { }; } -macro_rules! insert_public_builtin { - ($builtin: expr, $name: ident, $pattern: expr) => { - $builtin.push((stringify!($name), generate_public_rule!($name, $pattern))); - }; -} - +#[cfg(feature = "std")] macro_rules! generate_rule { ($name: ident, $pattern: expr) => { quote! { #[inline] #[allow(dead_code, non_snake_case, unused_variables)] - pub fn $name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn $name(state: ::std::boxed::Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<::std::boxed::Box<::pest::ParserState<Rule>>> { $pattern } } } } -macro_rules! generate_public_rule { +#[cfg(not(feature = "std"))] +macro_rules! generate_rule { ($name: ident, $pattern: expr) => { quote! { #[inline] #[allow(dead_code, non_snake_case, unused_variables)] - pub fn $name(state: Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<Box<::pest::ParserState<Rule>>> { + pub fn $name(state: ::alloc::boxed::Box<::pest::ParserState<Rule>>) -> ::pest::ParseResult<::alloc::boxed::Box<::pest::ParserState<Rule>>> { $pattern } } |