Skip to content

Commit

Permalink
fix(dispatch): handle cfg attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
39555 committed Nov 14, 2024
1 parent e8d2d3f commit f21886a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/macros/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
));
}
}

0 comments on commit f21886a

Please sign in to comment.