Skip to content
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

Local inner macros #458

Merged
merged 1 commit into from
Aug 10, 2018
Merged
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
21 changes: 16 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
/// ]);
/// # }
/// ```
#[macro_export]
#[macro_export(local_inner_macros)]
macro_rules! json {
// Hide distracting implementation details from the generated rustdoc.
($($json:tt)+) => {
Expand All @@ -81,7 +81,7 @@ macro_rules! json {
//
// Changes are fine as long as `json_internal!` does not call any new helper
// macros and can still be invoked as `json_internal!($($json)+)`.
#[macro_export]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! json_internal {
//////////////////////////////////////////////////////////////////////////
Expand All @@ -93,12 +93,12 @@ macro_rules! json_internal {

// Done with trailing comma.
(@array [$($elems:expr,)*]) => {
vec![$($elems,)*]
json_internal_vec![$($elems,)*]
};

// Done without trailing comma.
(@array [$($elems:expr),*]) => {
vec![$($elems),*]
json_internal_vec![$($elems),*]
};

// Next element is `null`.
Expand Down Expand Up @@ -265,7 +265,7 @@ macro_rules! json_internal {
};

([]) => {
$crate::Value::Array(vec![])
$crate::Value::Array(json_internal_vec![])
};

([ $($tt:tt)+ ]) => {
Expand All @@ -291,6 +291,17 @@ macro_rules! json_internal {
};
}

// The json_internal macro above cannot invoke vec directly because it uses
// local_inner_macros. A vec invocation there would resolve to $crate::vec.
// Instead invoke vec here outside of local_inner_macros.
#[macro_export]
#[doc(hidden)]
macro_rules! json_internal_vec {
($($content:tt)*) => {
vec![$($content)*]
};
}

#[macro_export]
#[doc(hidden)]
macro_rules! json_unexpected {
Expand Down