From f21886acaf8f1194e1bf2546926929986bf45235 Mon Sep 17 00:00:00 2001 From: Igor Date: Thu, 14 Nov 2024 17:48:48 +0400 Subject: [PATCH] fix(dispatch): handle `cfg` attributes --- src/macros/dispatch.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/macros/dispatch.rs b/src/macros/dispatch.rs index b763d4ba..4eec3dc0 100644 --- a/src/macros/dispatch.rs +++ b/src/macros/dispatch.rs @@ -40,16 +40,37 @@ #[macro_export] #[doc(hidden)] // forced to be visible in intended location macro_rules! dispatch { - ($match_parser: expr; $( $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { + ($match_parser: expr; $($(#[$meta:meta])* $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { $crate::combinator::trace("dispatch", move |i: &mut _| { use $crate::Parser; let initial = $match_parser.parse_next(i)?; match initial { $( + $(#[$meta])* $pat $(if $pred)? => $expr.parse_next(i), )* } }) } } + +#[cfg(test)] +mod tests { + use crate::{combinator::fail, error::ContextError}; + use crate::token::any; + use crate::Parser; + + #[test] + fn handle_cfg_attributes() { + assert!(matches!( + dispatch! {any::<_, ContextError>; + #[cfg(any())] // always false + 'a' => crate::combinator::empty.void(), + _ => fail::<_, (), _>.void(), + } + .parse("a"), + Err(_) + )); + } +}