Skip to content

Commit

Permalink
fix(macros): use structure! macro inside of body! macro
Browse files Browse the repository at this point in the history
This avoids the definition of duplicated match pattern which would need
to be kept in sync otherwise.
  • Loading branch information
martinohmann committed Jun 12, 2022
1 parent 0661eba commit 1d9a00c
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ macro_rules! body_internal {

// Next element is an attribute, munch into elems and proceed with next structure.
(@structures [$(($elems:expr))*] $key:tt = $expr:tt $($rest:tt)*) => {
$crate::body_internal!(@structures [$(($elems))* ($crate::body_internal!(@attribute $key = $expr))] $($rest)*)
$crate::body_internal!(@structures [$(($elems))* ($crate::structure!($key = $expr))] $($rest)*)
};

// Next element must be a block, invoke block muncher.
Expand All @@ -131,12 +131,12 @@ macro_rules! body_internal {
//////////////////////////////////////////////////////////////////////////
// TT muncher for parsing an HCL block.
//
// Must be invoked as: body_internal!(@block [$(($elems))*] $ident $($rest)*)
// Must be invoked as: body_internal!(@block [$(($elems))*] $ident $($rest)+)
//////////////////////////////////////////////////////////////////////////

// Found block body, munch block into elems and proceed with next structure.
(@block [$(($elems:expr))*] ($ident:expr) [$(($labels:expr))*] { $($body:tt)* } $($rest:tt)*) => {
$crate::body_internal!(@structures [$(($elems))* ($crate::body_internal!(@block ($ident) [$(($labels))*] { $($body)* }))] $($rest)*)
$crate::body_internal!(@structures [$(($elems))* ($crate::structure!(($ident) [$(($labels))*] { $($body)* }))] $($rest)*)
};

// Munch an identifier block label.
Expand Down Expand Up @@ -170,16 +170,6 @@ macro_rules! body_internal {
// Must be invoked as: body_internal!($($tt)+)
//////////////////////////////////////////////////////////////////////////

// Attribute structure.
(@attribute $key:tt = $expr:tt) => {
$crate::Structure::Attribute($crate::attribute!($key = $expr))
};

// Block structure.
(@block ($ident:expr) [$(($labels:expr))*] { $($body:tt)* }) => {
$crate::Structure::Block($crate::block!(($ident) [$(($labels))*] { $($body)* }))
};

// Body in curly braces.
({ $($tt:tt)* }) => {
$crate::body_internal!($($tt)*)
Expand Down

0 comments on commit 1d9a00c

Please sign in to comment.