//! A [macro](crate::choice_type) to generate choice type //! and generated choice types that has 2 to 15 variants, //! which are named `Choice{i}`. // TODO: peek to decide matched choice when possible. /// Generate choice type. /// /// ## Example /// /// ```rust,ignore /// pest3::choice_type!(Choice2, (Choice0, T0), (Choice1, T1)); /// ``` #[macro_export] macro_rules! choice_type { ( $name:ident, $( ( $variant:ident, $type:ident, $getter:ident ) ),* $(,)? ) => { /// Choice type generated by `pest::choice_type!`. #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub enum $name<$( $type ),*> { $( #[doc = ::core::stringify!(A choice for $type.)] $variant($type) ),* } impl<'i, R, $($type, )*> $crate::typed::TypedNode<'i, R> for $name<$($type, )*> where R: $crate::typed::RuleType, $( $type: $crate::typed::TypedNode<'i, R>, )* { #[inline] fn try_parse_with_partial( input: $crate::Position<'i>, stack: &mut $crate::Stack<$crate::Span<'i>>, tracker: &mut $crate::typed::Tracker<'i, R>, ) -> ::core::option::Option<($crate::Position<'i>, Self)> { $( stack.snapshot(); match $type::try_parse_with_partial(input, stack, tracker) { ::core::option::Option::Some((input, choice)) => { stack.clear_snapshot(); let res = Self::$variant(choice); return ::core::option::Option::Some((input, res)) } ::core::option::Option::None => { stack.restore(); } } )* ::core::option::Option::None } #[inline] fn check_with_partial( input: $crate::Position<'i>, stack: &mut $crate::Stack<$crate::Span<'i>>, tracker: &mut $crate::typed::Tracker<'i, R>, ) -> ::core::option::Option<$crate::Position<'i>> { $( stack.snapshot(); match $type::check_with_partial(input, stack, tracker) { ::core::option::Option::Some(input) => { stack.clear_snapshot(); return ::core::option::Option::Some(input) } ::core::option::Option::None => { stack.restore(); } } )* ::core::option::Option::None } } impl<'i, $($type, )*> $name<$($type, )*> { $( /// Try to get variant. pub fn $getter(&self) -> $crate::std::Option<&$type> { match self { Self::$variant(val) => $crate::std::Some(val), _ => $crate::std::None, } } )* } impl $crate::typed::PairContainer for $name<$($type, )*> where $( $type: $crate::typed::PairContainer, )* { fn for_each_child_pair(&self, f: &mut impl $crate::std::FnMut($crate::token::Pair)) { match self { $( Self::$variant(variant) => variant.for_self_or_for_each_child_pair(f), )* } } } }; } choice_type! { Choice2, (Choice0, T0, choice_0), (Choice1, T1, choice_1) } choice_type! { Choice3, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2) } choice_type! { Choice4, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3) } choice_type! { Choice5, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4) } choice_type! { Choice6, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4), (Choice5, T5, choice_5) } choice_type! { Choice7, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4), (Choice5, T5, choice_5), (Choice6, T6, choice_6) } choice_type! { Choice8, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4), (Choice5, T5, choice_5), (Choice6, T6, choice_6), (Choice7, T7, choice_7) } choice_type! { Choice9, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4), (Choice5, T5, choice_5), (Choice6, T6, choice_6), (Choice7, T7, choice_7), (Choice8, T8, choice_8) } choice_type! { Choice10, (Choice0, T0, choice_0), (Choice1, T1, choice_1), (Choice2, T2, choice_2), (Choice3, T3, choice_3), (Choice4, T4, choice_4), (Choice5, T5, choice_5), (Choice6, T6, choice_6), (Choice7, T7, choice_7), (Choice8, T8, choice_8), (Choice9, T9, choice_9) } choice_type! { Choice11, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10) } choice_type! { Choice12, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10), (Choice11, T11, choice_11) } choice_type! { Choice13, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10), (Choice11, T11, choice_11), (Choice12, T12, choice_12) } choice_type! { Choice14, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10), (Choice11, T11, choice_11), (Choice12, T12, choice_12), (Choice13, T13, choice_13), } choice_type! { Choice15, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10), (Choice11, T11, choice_11), (Choice12, T12, choice_12), (Choice13, T13, choice_13), (Choice14, T14, choice_14), } choice_type! { Choice16, (Choice0, T0, choice_0 ), (Choice1, T1, choice_1 ), (Choice2, T2, choice_2 ), (Choice3, T3, choice_3 ), (Choice4, T4, choice_4 ), (Choice5, T5, choice_5 ), (Choice6, T6, choice_6 ), (Choice7, T7, choice_7 ), (Choice8, T8, choice_8 ), (Choice9, T9, choice_9 ), (Choice10, T10, choice_10), (Choice11, T11, choice_11), (Choice12, T12, choice_12), (Choice13, T13, choice_13), (Choice14, T14, choice_14), (Choice15, T15, choice_15), }