Skip to content

Fix expanding to short versions of constructs #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ language: rust
rust:
- beta
- stable
- 1.11.0
- 1.12.0
script:
- cargo test
68 changes: 51 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,44 +162,78 @@ macro_rules! if_chain {
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! __if_chain {
(@init ($($tt:tt)*) then $then:block else $other:block) => {
__if_chain! { @expand $other $($tt)* then $then }
// Expand with both a successful case and a fallback
(@init ($($tt:tt)*) then { $($then:tt)* } else { $($other:tt)* }) => {
__if_chain! { @expand { $($other)* } $($tt)* then { $($then)* } }
};
(@init ($($tt:tt)*) then $then:block) => {
__if_chain! { @expand {} $($tt)* then $then }
// Expand with no fallback
(@init ($($tt:tt)*) then { $($then:tt)* }) => {
__if_chain! { @expand {} $($tt)* then { $($then)* } }
};
// Munch everything until either of the arms above can be matched.
// Munched tokens are placed into `$($tt)*`
(@init ($($tt:tt)*) $head:tt $($tail:tt)*) => {
__if_chain! { @init ($($tt)* $head) $($tail)* }
};
(@expand $other:block let $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {

// `let` with a single pattern
(@expand { $($other:tt)* } let $pat:pat = $expr:expr; $($tt:tt)+) => {
{
let $pat = $expr;
__if_chain! { @expand { $($other)* } $($tt)+ }
}
};
// `let` with a single identifier and a type hint
(@expand { $($other:tt)* } let $ident:ident: $ty:ty = $expr:expr; $($tt:tt)+) => {
{
let $ident: $ty = $expr;
__if_chain! { @expand { $($other)* } $($tt)+ }
}
};
// `let` with multiple patterns
(@expand { $($other:tt)* } let $pat1:pat | $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
match $expr {
$($pat)|+ => __if_chain! { @expand $other $($tt)+ }
$pat1 | $($pat)|+ => __if_chain! { @expand { $($other)* } $($tt)+ }
}
};
(@expand $other:block let $ident:ident: $ty:ty = $expr:expr; $($tt:tt)+) => {
let $ident: $ty = $expr;
__if_chain! { @expand $other $($tt)+ }
// `if let` with a single pattern
(@expand {} if let $pat:pat = $expr:expr; $($tt:tt)+) => {
if let $pat = $expr {
__if_chain! { @expand {} $($tt)+ }
}
};
// `if let` with a single pattern and a fallback
(@expand { $($other:tt)+ } if let $pat:pat = $expr:expr; $($tt:tt)+) => {
if let $pat = $expr {
__if_chain! { @expand { $($other)+ } $($tt)+ }
} else {
$($other)+
}
};
(@expand $other:block if let $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
// `if let` with multiple matterns and a fallback (if present)
(@expand { $($other:tt)* } if let $pat1:pat | $($pat:pat)|+ = $expr:expr; $($tt:tt)+) => {
match $expr {
$($pat)|+ => __if_chain! { @expand $other $($tt)+ },
_ => $other
$pat1 | $($pat)|+ => { __if_chain! { @expand { $($other)* } $($tt)+ } },
_ => { $($other)* }
}
};
// `if` with a successful case
(@expand {} if $expr:expr; $($tt:tt)+) => {
if $expr {
__if_chain! { @expand {} $($tt)+ }
}
};
(@expand $other:block if $expr:expr; $($tt:tt)+) => {
// `if` with both a successful case and a fallback
(@expand { $($other:tt)+ } if $expr:expr; $($tt:tt)+) => {
if $expr {
__if_chain! { @expand $other $($tt)+ }
__if_chain! { @expand { $($other)+ } $($tt)+ }
} else {
$other
$($other)+
}
};
(@expand $other:block then $then:block) => {
$then
// Final macro call
(@expand { $($other:tt)* } then { $($then:tt)* }) => {
$($then)*
};
}

Expand Down