| 1 |
//! Declarative feature macros. |
| 2 |
|
| 3 |
/// Note: pattern matches inside this macro must be unique. |
| 4 |
/// |
| 5 |
/// ## Shape |
| 6 |
/// |
| 7 |
/// ```text |
| 8 |
/// __declare_features!( |
| 9 |
/// MACRO_NAME: parse_macro_ident; |
| 10 |
/// |
| 11 |
/// // Optional default macro prefix. If specified, the parser will attempt to use it |
| 12 |
/// // if the first attribute parameter is "naked". |
| 13 |
/// @default: default_macro_prefix; |
| 14 |
/// |
| 15 |
/// /// Docs for this feature… |
| 16 |
/// feature_ident { |
| 17 |
/// /// crate Optional docs for the crate feature. |
| 18 |
/// // Ties the Rust symbol to a **Cargo feature** name. |
| 19 |
/// feature: "…"; |
| 20 |
/// |
| 21 |
/// /// attr Optional docs for the attribute fragment. |
| 22 |
/// // Attribute pattern. $feature_ident can be used in VALUE to take the first token. |
| 23 |
/// attr: [(PATTERN) => (VALUE)]; |
| 24 |
/// // Literal string used in generated **attribute** docs as a sample. |
| 25 |
/// example: "…"; |
| 26 |
/// // Comma-separated patterns this value must match. |
| 27 |
/// validate: [(…), …]; |
| 28 |
/// // Target- or cfg-specific defaults (`cfg` meta on each arm), optional |
| 29 |
/// // `#[warn("…")]` on the `_` arm, always end with `_ => …`. |
| 30 |
/// default { (cfg meta) => tokens, … _ => fallback }; |
| 31 |
/// }; |
| 32 |
/// ); |
| 33 |
/// ``` |
| 34 |
#[macro_export] |
| 35 |
#[doc(hidden)] |
| 36 |
macro_rules! __declare_features { |
| 37 |
( $macro_name:ident : $macro_internal:ident $($input:tt)* ) => { |
| 38 |
mod $macro_internal { |
| 39 |
$crate::__perform!( |
| 40 |
($macro_name : $macro_internal $($input)*), |
| 41 |
$crate::__chain[ |
| 42 |
$crate::__parse_feature_input[$], |
| 43 |
$crate::__parallel[ |
| 44 |
// (params) |
| 45 |
$crate::__pick[0], |
| 46 |
// (features) |
| 47 |
$crate::__chain[ |
| 48 |
$crate::__pick[1], |
| 49 |
$crate::__unbrace, |
| 50 |
$crate::__for_each[$crate::__chain[ |
| 51 |
$crate::__fix_docs, |
| 52 |
$crate::__fix_example_validate, |
| 53 |
$crate::__process_defaults, |
| 54 |
$crate::__evaluate_defaults, |
| 55 |
]], |
| 56 |
$crate::__brace[()], |
| 57 |
], |
| 58 |
// (features) |
| 59 |
$crate::__chain[ |
| 60 |
$crate::__pick[1], |
| 61 |
$crate::__feature_square, |
| 62 |
], |
| 63 |
], |
| 64 |
// (params) (features) (feature_square) |
| 65 |
$crate::__parallel[ |
| 66 |
$crate::__identity, |
| 67 |
$crate::__pick_doc_vars, |
| 68 |
], |
| 69 |
$crate::__make_macros[$], |
| 70 |
] |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[macro_export] |
| 77 |
#[doc(hidden)] |
| 78 |
macro_rules! __generate_docs { |
| 79 |
( $macro_parse:path ) => { |
| 80 |
$crate::__perform!( |
| 81 |
(), |
| 82 |
$crate::__chain[ |
| 83 |
$macro_parse[$macro_parse => @raw], |
| 84 |
$crate::__make_docs, |
| 85 |
] |
| 86 |
); |
| 87 |
}; |
| 88 |
} |
| 89 |
|
| 90 |
/// Parses a single, generic item decorated with an attribute macro into the |
| 91 |
/// main attribute features, other meta items, and the original item. |
| 92 |
#[macro_export] |
| 93 |
#[doc(hidden)] |
| 94 |
macro_rules! __parse_item { |
| 95 |
// Fast path: one meta, doc/allow metas, then pub/unsafe/fn/static/const. We |
| 96 |
// trade duplication for a significant reduction in macro recursion. |
| 97 |
( @entry next=$next:path[$next_args:tt], input=( |
| 98 |
#[$meta:ident $(( $($meta_args:tt)* ))?] $( #[allow $allow0:tt] )* $( #[doc = $doc:literal] $( #[allow $allow1:tt] )* )* |
| 99 |
fn $($rest:tt)*), args=[$macro_name:path]) => { |
| 100 |
$crate::__chain!(@entry next=$next[$next_args], input=( |
| 101 |
($meta $(($($meta_args)*))?) |
| 102 |
($(#[allow $allow0])* $( #[doc = $doc] $( #[allow $allow1])* )*) |
| 103 |
fn $($rest)* |
| 104 |
), args=[ |
| 105 |
// (self) (other) item -> (self) features (other) item |
| 106 |
$crate::__expand[ |
| 107 |
$crate::__extract_meta[$macro_name], |
| 108 |
], |
| 109 |
$crate::__finish_item, |
| 110 |
]); |
| 111 |
}; |
| 112 |
( @entry next=$next:path[$next_args:tt], input=( |
| 113 |
#[$meta:ident $(( $($meta_args:tt)* ))?] $( #[allow $allow0:tt] )* $( #[doc = $doc:literal] $( #[allow $allow1:tt] )* )* |
| 114 |
static $($rest:tt)*), args=[$macro_name:path]) => { |
| 115 |
$crate::__chain!(@entry next=$next[$next_args], input=( |
| 116 |
($meta $(($($meta_args)*))?) |
| 117 |
($(#[allow $allow0])* $( #[doc = $doc] $( #[allow $allow1])* )*) |
| 118 |
static $($rest)* |
| 119 |
), args=[ |
| 120 |
// (self) (other) item -> (self) features (other) item |
| 121 |
$crate::__expand[ |
| 122 |
$crate::__extract_meta[$macro_name], |
| 123 |
], |
| 124 |
$crate::__finish_item, |
| 125 |
]); |
| 126 |
}; |
| 127 |
( @entry next=$next:path[$next_args:tt], input=( |
| 128 |
#[$meta:ident $(( $($meta_args:tt)* ))?] $( #[allow $allow0:tt] )* $( #[doc = $doc:literal] $( #[allow $allow1:tt] )* )* |
| 129 |
pub $($rest:tt)*), args=[$macro_name:path]) => { |
| 130 |
$crate::__chain!(@entry next=$next[$next_args], input=( |
| 131 |
($meta $(($($meta_args)*))?) |
| 132 |
($(#[allow $allow0])* $( #[doc = $doc] $( #[allow $allow1])* )*) |
| 133 |
pub $($rest)* |
| 134 |
), args=[ |
| 135 |
// (self) (other) item -> (self) features (other) item |
| 136 |
$crate::__expand[ |
| 137 |
$crate::__extract_meta[$macro_name], |
| 138 |
], |
| 139 |
$crate::__finish_item, |
| 140 |
]); |
| 141 |
}; |
| 142 |
( @entry next=$next:path[$next_args:tt], input=( |
| 143 |
#[$meta:ident $(( $($meta_args:tt)* ))?] $( #[allow $allow0:tt] )* $( #[doc = $doc:literal] $( #[allow $allow1:tt] )* )* |
| 144 |
unsafe $($rest:tt)*), args=[$macro_name:path]) => { |
| 145 |
$crate::__chain!(@entry next=$next[$next_args], input=( |
| 146 |
($meta $(($($meta_args)*))?) |
| 147 |
($(#[allow $allow0])* $( #[doc = $doc] $( #[allow $allow1])* )*) |
| 148 |
unsafe $($rest)* |
| 149 |
), args=[ |
| 150 |
// (self) (other) item -> (self) features (other) item |
| 151 |
$crate::__expand[ |
| 152 |
$crate::__extract_meta[$macro_name], |
| 153 |
], |
| 154 |
$crate::__finish_item, |
| 155 |
]); |
| 156 |
}; |
| 157 |
( @entry next=$next:path[$next_args:tt], input=( |
| 158 |
#[$meta:ident $(( $($meta_args:tt)* ))?] $( #[allow $allow0:tt] )* $( #[doc = $doc:literal] $( #[allow $allow1:tt] )* )* |
| 159 |
const $($rest:tt)*), args=[$macro_name:path]) => { |
| 160 |
$crate::__chain!(@entry next=$next[$next_args], input=( |
| 161 |
($meta $(($($meta_args)*))?) |
| 162 |
($(#[allow $allow0])* $( #[doc = $doc] $( #[allow $allow1])* )*) |
| 163 |
const $($rest)* |
| 164 |
), args=[ |
| 165 |
// (self) (other) item -> (self) features (other) item |
| 166 |
$crate::__expand[ |
| 167 |
$crate::__extract_meta[$macro_name], |
| 168 |
], |
| 169 |
$crate::__finish_item, |
| 170 |
]); |
| 171 |
}; |
| 172 |
|
| 173 |
( @entry next=$next:path[$next_args:tt], input=($($item:tt)*), args=[$macro_name:path]) => { |
| 174 |
$crate::__chain!(@entry next=$next[$next_args], input=($($item)*), args=[ |
| 175 |
// Split meta from item and process them separately |
| 176 |
$crate::__split_meta, |
| 177 |
// (meta) (item) |
| 178 |
$crate::__separate[ |
| 179 |
// (meta) |
| 180 |
$crate::__chain[ |
| 181 |
// (meta) |
| 182 |
$macro_name[$macro_name => @self], |
| 183 |
// (self)(other) |
| 184 |
$crate::__expand[ |
| 185 |
// (self) -> (self) features |
| 186 |
$crate::__extract_meta[$macro_name], |
| 187 |
], |
| 188 |
], |
| 189 |
], |
| 190 |
// Assembles the final parsed item |
| 191 |
// input: self features #[other_meta] item |
| 192 |
$crate::__finish_item, |
| 193 |
]); |
| 194 |
}; |
| 195 |
} |
| 196 |
|
| 197 |
#[macro_export] |
| 198 |
#[doc(hidden)] |
| 199 |
macro_rules! __ensure_item { |
| 200 |
( $item:item ) => {}; |
| 201 |
( $($item:tt)* ) => { |
| 202 |
compile_error!(concat!("Expected an item, got: ", stringify!($($item)*))); |
| 203 |
}; |
| 204 |
} |
| 205 |
|
| 206 |
/// Finishes the item parsing by collecting the features, meta, and item. |
| 207 |
#[macro_export] |
| 208 |
#[doc(hidden)] |
| 209 |
macro_rules! __finish_item { |
| 210 |
( @entry next=$next:path[$next_args:tt], input=( |
| 211 |
$self:tt |
| 212 |
$($feature:ident = $feature_value:tt $feature_value_what:ident,)* |
| 213 |
($(#[$other_meta:meta])*) |
| 214 |
($($item:tt)*) |
| 215 |
) ) => { |
| 216 |
$crate::__ensure_item!($($item)*); |
| 217 |
$next ! ( $next_args, ( |
| 218 |
features = ($($feature = $feature_value: $feature_value_what,)*), |
| 219 |
self = $self, |
| 220 |
meta = ($(#[$other_meta])*), |
| 221 |
item = ($($item)*) |
| 222 |
) ); |
| 223 |
}; |
| 224 |
|
| 225 |
( @entry next=$next:path[$next_args:tt], input=( |
| 226 |
$self:tt |
| 227 |
$($feature:ident = $feature_value:tt $feature_value_what:ident,)* |
| 228 |
($(#[$other_meta:meta])*) |
| 229 |
$($item:tt)* |
| 230 |
) ) => { |
| 231 |
$crate::__ensure_item!($($item)*); |
| 232 |
$next ! ( $next_args, ( |
| 233 |
features = ($($feature = $feature_value: $feature_value_what,)*), |
| 234 |
self = $self, |
| 235 |
meta = ($(#[$other_meta])*), |
| 236 |
item = ($($item)*) |
| 237 |
) ); |
| 238 |
}; |
| 239 |
|
| 240 |
( $($input:tt)* ) => { |
| 241 |
const _: () = { compile_error!(concat!("Unexpected input to __finish_item: ", stringify!($($input)*))); }; |
| 242 |
}; |
| 243 |
} |
| 244 |
|
| 245 |
/// Splits the item input into meta and item. |
| 246 |
#[macro_export] |
| 247 |
#[doc(hidden)] |
| 248 |
macro_rules! __split_meta { |
| 249 |
// Optimization for known items |
| 250 |
( @entry next=$next:path[$next_args:tt], input=($(#$meta:tt)* fn $($rest:tt)*)) => { |
| 251 |
$next ! ( $next_args, (($(#$meta)*) ( fn $($rest)*))); |
| 252 |
}; |
| 253 |
( @entry next=$next:path[$next_args:tt], input=($(#$meta:tt)* const $($rest:tt)*)) => { |
| 254 |
$next ! ( $next_args, (($(#$meta)*) ( const $($rest)*))); |
| 255 |
}; |
| 256 |
( @entry next=$next:path[$next_args:tt], input=($(#$meta:tt)* static $($rest:tt)*)) => { |
| 257 |
$next ! ( $next_args, (($(#$meta)*) ( static $($rest)*))); |
| 258 |
}; |
| 259 |
( @entry next=$next:path[$next_args:tt], input=($(#$meta:tt)* pub $($rest:tt)*)) => { |
| 260 |
$next ! ( $next_args, (($(#$meta)*) ( pub $($rest)*))); |
| 261 |
}; |
| 262 |
|
| 263 |
( @entry next=$next:path[$next_args:tt], input=($($item:tt)*) ) => { |
| 264 |
$crate::__split_meta!(@loop meta=(), rest=($($item)*), item_check=($($item)*), next=[$next[$next_args]]); |
| 265 |
}; |
| 266 |
|
| 267 |
( @loop meta=($($metas:tt)*), rest=(#$meta:tt $($item:tt)*), item_check=($item_check:item), next=$next:tt ) => { |
| 268 |
$crate::__split_meta!(@loop meta=($($metas)* #$meta), rest=($($item)*), item_check=($item_check), next=$next); |
| 269 |
}; |
| 270 |
|
| 271 |
( @loop meta=($($meta:tt)*), rest=($($item:tt)*), item_check=($item_check:item), next=[$next:path[$next_args:tt]]) => { |
| 272 |
$next ! ( $next_args, (($($meta)*) ($($item)*)) ); |
| 273 |
}; |
| 274 |
|
| 275 |
( @loop meta=($($metas:tt)*), rest=$rest:tt, item_check=not_an_item:tt, next=$next:tt ) => { |
| 276 |
const _: () = { compile_error!(concat!("Expected an item, got: ", stringify!($($input)*))); }; |
| 277 |
}; |
| 278 |
|
| 279 |
( $($input:tt)* ) => { |
| 280 |
const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); }; |
| 281 |
}; |
| 282 |
} |
| 283 |
|
| 284 |
/// Extracts the unsafe flag from an item extracted via `__parse_item`. |
| 285 |
#[macro_export] |
| 286 |
#[doc(hidden)] |
| 287 |
macro_rules! __extract_unsafe { |
| 288 |
( @entry next=$next:path[$next_args:tt], input=( |
| 289 |
features = $features:tt, |
| 290 |
self = $self:tt, |
| 291 |
meta = $meta:tt, |
| 292 |
item = ($vis:vis unsafe $($rest:tt)*) |
| 293 |
) ) => { |
| 294 |
$next ! ( $next_args, ( |
| 295 |
features = $features, |
| 296 |
self = $self, |
| 297 |
meta = $meta, |
| 298 |
unsafe = (unsafe), |
| 299 |
item = ($vis unsafe $($rest)*) |
| 300 |
)); |
| 301 |
}; |
| 302 |
|
| 303 |
( @entry next=$next:path[$next_args:tt], input=( |
| 304 |
features = $features:tt, |
| 305 |
self = $self:tt, |
| 306 |
meta = $meta:tt, |
| 307 |
item = ($vis:vis static $ident:ident : & $lt:lifetime $ty:ty = &unsafe $($rest:tt)*) |
| 308 |
) ) => { |
| 309 |
$next ! ( $next_args, ( |
| 310 |
features = $features, |
| 311 |
self = $self, |
| 312 |
meta = $meta, |
| 313 |
unsafe = (unsafe), |
| 314 |
item = ($vis static $ident : & $lt $ty = &unsafe $($rest)*) |
| 315 |
)); |
| 316 |
}; |
| 317 |
|
| 318 |
( @entry next=$next:path[$next_args:tt], input=( |
| 319 |
features = $features:tt, |
| 320 |
self = $self:tt, |
| 321 |
meta = $meta:tt, |
| 322 |
item = ($vis:vis static $ident:ident : & $lt:lifetime $ty:ty = unsafe $($rest:tt)*) |
| 323 |
) ) => { |
| 324 |
$next ! ( $next_args, ( |
| 325 |
features = $features, |
| 326 |
self = $self, |
| 327 |
meta = $meta, |
| 328 |
unsafe = (unsafe), |
| 329 |
item = ($vis static $ident : & $lt $ty = unsafe $($rest)*) |
| 330 |
)); |
| 331 |
}; |
| 332 |
|
| 333 |
( @entry next=$next:path[$next_args:tt], input=( |
| 334 |
features = $features:tt, |
| 335 |
self = $self:tt, |
| 336 |
meta = $meta:tt, |
| 337 |
item = ($vis:vis static $ident:ident : $ty:ty = unsafe $($rest:tt)*) |
| 338 |
) ) => { |
| 339 |
$next ! ( $next_args, ( |
| 340 |
features = $features, |
| 341 |
self = $self, |
| 342 |
meta = $meta, |
| 343 |
unsafe = (unsafe), |
| 344 |
item = ($vis static $ident : $ty = unsafe $($rest)*) |
| 345 |
)); |
| 346 |
}; |
| 347 |
|
| 348 |
( @entry next=$next:path[$next_args:tt], input=( |
| 349 |
features = $features:tt, |
| 350 |
self = $self:tt, |
| 351 |
meta = $meta:tt, |
| 352 |
item = $item:tt |
| 353 |
) ) => { |
| 354 |
$next ! ( $next_args, ( |
| 355 |
features = $features, |
| 356 |
self = $self, |
| 357 |
meta = $meta, |
| 358 |
unsafe = (), |
| 359 |
item = $item |
| 360 |
)); |
| 361 |
}; |
| 362 |
} |
| 363 |
|
| 364 |
/// Parse a type, optionally followed with a semicolon. Also works for paths. |
| 365 |
#[macro_export] |
| 366 |
#[doc(hidden)] |
| 367 |
macro_rules! __parse_type { |
| 368 |
// Empty |
| 369 |
(@entry next=$next:path[$next_args:tt], input=($(;)?)) => { |
| 370 |
$next!($next_args, ( |
| 371 |
type = () |
| 372 |
)); |
| 373 |
}; |
| 374 |
// Basic forms |
| 375 |
(@entry next=$next:path[$next_args:tt], input=($final:ident $(< $($generic:ty),* >)? $(;)?)) => { |
| 376 |
$next!($next_args, ( |
| 377 |
type = ($final$(<$($generic),*>)?) |
| 378 |
prefix = () |
| 379 |
final = $final |
| 380 |
generics = ($($($generic),*)?) |
| 381 |
)); |
| 382 |
}; |
| 383 |
(@entry next=$next:path[$next_args:tt], input=(:: $final:ident $(< $($generic:ty),* >)? $(;)?)) => { |
| 384 |
$next!($next_args, ( |
| 385 |
type = (:: $final$(<$($generic),*>)?) |
| 386 |
prefix = (::) |
| 387 |
final = $final |
| 388 |
generics = ($($($generic),*)?) |
| 389 |
)); |
| 390 |
}; |
| 391 |
(@entry next=$next:path[$next_args:tt], input=($prefix:ident :: $final:ident $(< $($generic:ty),* >)? $(;)?)) => { |
| 392 |
$next!($next_args, ( |
| 393 |
type = ($prefix:: $final$(<$($generic),*>)?) |
| 394 |
prefix = ($prefix::) |
| 395 |
final = $final |
| 396 |
generics = ($($($generic),*)?) |
| 397 |
)); |
| 398 |
}; |
| 399 |
(@entry next=$next:path[$next_args:tt], input=(:: $prefix:ident :: $final:ident $(< $($generic:ty),* >)? $(;)?)) => { |
| 400 |
$next!($next_args, ( |
| 401 |
type = (:: $prefix:: $final$(<$($generic),*>)?) |
| 402 |
prefix = (::$prefix::) |
| 403 |
final = $final |
| 404 |
generics = ($($($generic),*)?) |
| 405 |
)); |
| 406 |
}; |
| 407 |
|
| 408 |
// Longer, needs a munch |
| 409 |
(@entry next=$next:path[$next_args:tt], input=($prefix:tt :: $($rest:tt)*)) => { |
| 410 |
$crate::__parse_type!(@accum prefix=( $prefix ), rest=( :: $( $rest )* ), next=[$next[$next_args]], input=($prefix::$($rest)*)); |
| 411 |
}; |
| 412 |
(@entry next=$next:path[$next_args:tt], input=(:: $prefix:tt :: $($rest:tt)*)) => { |
| 413 |
$crate::__parse_type!(@accum prefix=( :: $prefix ), rest=( :: $( $rest )* ), next=[$next[$next_args]], input=(::$prefix::$($rest)*)); |
| 414 |
}; |
| 415 |
|
| 416 |
(@accum prefix=($( $prefix:tt )*), rest=(:: $more_prefix:ident :: $( $rest:tt )*), next=$next:tt, input=$input:tt) => { |
| 417 |
$crate::__parse_type!(@accum prefix=( $( $prefix )* :: $more_prefix ), rest=( :: $( $rest )* ), next=$next, input=$input); |
| 418 |
}; |
| 419 |
(@accum prefix=($($prefix:tt)*), rest=(:: $final:ident $(< $($generic:ty),* >)? $(;)?), next=[$next:path[$next_args:tt]], input=$input:tt) => { |
| 420 |
$next!($next_args, ( |
| 421 |
type = ( $( $prefix )* :: $final$(<$($generic),*>)?) |
| 422 |
prefix = ( $( $prefix )* :: ) |
| 423 |
final = $final |
| 424 |
generics = ($($($generic),*)?) |
| 425 |
)); |
| 426 |
}; |
| 427 |
} |
| 428 |
|
| 429 |
/// Normalizes `__declare_features!` input into tuples for later passes (grammar: module `//!`). |
| 430 |
#[macro_export] |
| 431 |
#[doc(hidden)] |
| 432 |
macro_rules! __parse_feature_input { |
| 433 |
( @entry next=$next:path[$next_args:tt], input=( |
| 434 |
$macro_name:ident: $macro_parse:ident; |
| 435 |
|
| 436 |
$( |
| 437 |
@default: $df_feature:ident; |
| 438 |
)? |
| 439 |
|
| 440 |
$( |
| 441 |
$( #[doc = $doc:literal] )* |
| 442 |
$feature:ident { |
| 443 |
$( |
| 444 |
$( #[doc = r" crate"] $( #[doc = $doc_crate:literal] )* )? |
| 445 |
feature: $feature_name:literal; |
| 446 |
)? |
| 447 |
$( |
| 448 |
$( #[doc = r" attr"] $( #[doc = $doc_attr:literal] )* )? |
| 449 |
attr: [($($attr:tt)*) => ($($attr_value:tt)*)]; |
| 450 |
$( |
| 451 |
example: $example:literal; |
| 452 |
)? |
| 453 |
$( |
| 454 |
validate: [$( ($($validate:tt)*) ),*]; |
| 455 |
)? |
| 456 |
)? |
| 457 |
$( default { |
| 458 |
$( ($default_expr:meta) => $default_value:tt, )* |
| 459 |
$( #[warn($default_warn:literal)] )? |
| 460 |
_ => $default_fallback:tt $(,)? |
| 461 |
} )? |
| 462 |
}; |
| 463 |
)* |
| 464 |
), args=[$dollar:tt]) => { |
| 465 |
$next ! ( $next_args, ( |
| 466 |
( |
| 467 |
$macro_name |
| 468 |
$macro_parse |
| 469 |
$($df_feature)? |
| 470 |
) |
| 471 |
( |
| 472 |
$( |
| 473 |
( |
| 474 |
feature = $feature; |
| 475 |
docs = [$( $doc )*]; |
| 476 |
$( |
| 477 |
name = $feature_name; |
| 478 |
crate_docs = [$( $( $doc_crate )* )?]; |
| 479 |
)? |
| 480 |
$( |
| 481 |
attr = [($($attr)*) => ($($attr_value)*)]; |
| 482 |
attr_docs = [$( $( $doc_attr )* )?]; |
| 483 |
example = ($( ($example) )? (stringify!($($attr)*))); |
| 484 |
)? |
| 485 |
validate = ( |
| 486 |
$( $( [ |
| 487 |
$( $dollar ( [$($validate)*] )? )* $dollar ([()]) ? |
| 488 |
] )? )? |
| 489 |
[$dollar $feature:tt] |
| 490 |
); |
| 491 |
default = [ |
| 492 |
$( $( #[warn($default_warn)] )? )? |
| 493 |
$( |
| 494 |
((feature = $feature_name) => $feature) |
| 495 |
)? |
| 496 |
$( |
| 497 |
$( (($default_expr) => $default_value) )* |
| 498 |
(_ => $default_fallback) |
| 499 |
)? |
| 500 |
(_ => ()) |
| 501 |
] |
| 502 |
) |
| 503 |
)* |
| 504 |
) |
| 505 |
) ); |
| 506 |
}; |
| 507 |
} |
| 508 |
|
| 509 |
/// Concatenate the global docs with the crate/attr docs. |
| 510 |
#[macro_export] |
| 511 |
#[doc(hidden)] |
| 512 |
macro_rules! __fix_docs { |
| 513 |
( @entry next=$next:path[$next_args:tt], input=( |
| 514 |
( |
| 515 |
feature = $feature:ident; |
| 516 |
docs = [$($docs:tt)*]; |
| 517 |
name = $crate_name:literal; |
| 518 |
crate_docs = [$($crate_docs:tt)*]; |
| 519 |
attr = $attr:tt; |
| 520 |
attr_docs = [$($attr_docs:tt)*]; |
| 521 |
example = $example:tt; |
| 522 |
validate = $validate:tt; |
| 523 |
default = $default:tt |
| 524 |
) |
| 525 |
) ) => { |
| 526 |
$next ! ( $next_args, (( |
| 527 |
feature = $feature; |
| 528 |
name = $crate_name; |
| 529 |
crate_docs = [$($docs)* $($crate_docs)*]; |
| 530 |
attr = $attr; |
| 531 |
attr_docs = [$($docs)* $($attr_docs)*]; |
| 532 |
example = $example; |
| 533 |
validate = $validate; |
| 534 |
default = $default |
| 535 |
)) ); |
| 536 |
}; |
| 537 |
|
| 538 |
( @entry next=$next:path[$next_args:tt], input=( |
| 539 |
( |
| 540 |
feature = $feature:ident; |
| 541 |
docs = [$($docs:tt)*]; |
| 542 |
attr = $attr:tt; |
| 543 |
attr_docs = [$($attr_docs:tt)*]; |
| 544 |
example = $example:tt; |
| 545 |
validate = $validate:tt; |
| 546 |
default = $default:tt |
| 547 |
) |
| 548 |
) ) => { |
| 549 |
$next ! ( $next_args, (( |
| 550 |
feature = $feature; |
| 551 |
attr = $attr; |
| 552 |
attr_docs = [$($docs)* $($attr_docs)*]; |
| 553 |
example = $example; |
| 554 |
validate = $validate; |
| 555 |
default = $default |
| 556 |
)) ); |
| 557 |
}; |
| 558 |
|
| 559 |
( @entry next=$next:path[$next_args:tt], input=( |
| 560 |
( |
| 561 |
feature = $feature:ident; |
| 562 |
docs = [$($docs:tt)*]; |
| 563 |
name = $crate_name:literal; |
| 564 |
crate_docs = [$($crate_docs:tt)*]; |
| 565 |
validate = $validate:tt; |
| 566 |
default = $default:tt |
| 567 |
) |
| 568 |
) ) => { |
| 569 |
$next ! ( $next_args, (( |
| 570 |
feature = $feature; |
| 571 |
name = $crate_name; |
| 572 |
crate_docs = [$($docs)* $($crate_docs)*]; |
| 573 |
validate = $validate; |
| 574 |
default = $default |
| 575 |
)) ); |
| 576 |
}; |
| 577 |
|
| 578 |
( @entry next=$next:path[$next_args:tt], input=( |
| 579 |
( |
| 580 |
feature = $feature:ident; |
| 581 |
docs = [$($docs:tt)*]; |
| 582 |
validate = $validate:tt; |
| 583 |
default = $default:tt |
| 584 |
) |
| 585 |
) ) => { |
| 586 |
$next ! ( $next_args, (( |
| 587 |
feature = $feature; |
| 588 |
validate = $validate; |
| 589 |
default = $default |
| 590 |
)) ); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
/// Ensures the example and validation are correct. |
| 595 |
#[macro_export] |
| 596 |
#[doc(hidden)] |
| 597 |
macro_rules! __fix_example_validate { |
| 598 |
( @entry next=$next:path[$next_args:tt], input=( |
| 599 |
( |
| 600 |
feature = $feature:ident; |
| 601 |
$( |
| 602 |
name = $crate_name:literal; |
| 603 |
crate_docs = $crate_docs:tt; |
| 604 |
)? |
| 605 |
$( |
| 606 |
attr = $attr:tt; |
| 607 |
attr_docs = $attr_docs:tt; |
| 608 |
example = ($example:tt $( ($($example_extra:tt)*) )?); |
| 609 |
)? |
| 610 |
validate = ($validate:tt $( [$($validate_extra:tt)*] )?); |
| 611 |
default = $default:tt |
| 612 |
) |
| 613 |
) ) => { |
| 614 |
$next ! ( $next_args, (( |
| 615 |
feature = $feature; |
| 616 |
$( |
| 617 |
name = $crate_name; |
| 618 |
crate_docs = $crate_docs; |
| 619 |
)? |
| 620 |
$( |
| 621 |
attr = $attr; |
| 622 |
attr_docs = $attr_docs; |
| 623 |
example = $example; |
| 624 |
)? |
| 625 |
validate = $validate; |
| 626 |
default = $default |
| 627 |
)) ); |
| 628 |
}; |
| 629 |
} |
| 630 |
|
| 631 |
/// Process the defaults into full cfg chains. |
| 632 |
#[macro_export] |
| 633 |
#[doc(hidden)] |
| 634 |
macro_rules! __process_defaults { |
| 635 |
( @entry next=$next:path[$next_args:tt], input=( |
| 636 |
( |
| 637 |
feature = $feature:ident; |
| 638 |
$( |
| 639 |
name = $name_both:literal; |
| 640 |
crate_docs = $crate_docs:tt; |
| 641 |
)? |
| 642 |
$( |
| 643 |
attr = $attr:tt; |
| 644 |
attr_docs = $attr_docs:tt; |
| 645 |
example = $example:tt; |
| 646 |
)? |
| 647 |
validate = $validate:tt; |
| 648 |
default = [$( #[warn($default_warn:literal)] )? $(($($default:tt)*))*] |
| 649 |
) |
| 650 |
) ) => { |
| 651 |
$crate::__process_defaults!( @process accum=(), negative=(), defaults= |
| 652 |
[ |
| 653 |
$( |
| 654 |
($($default)*) |
| 655 |
)* |
| 656 |
], |
| 657 |
warn=($($default_warn)?), |
| 658 |
next=[$next[$next_args]], |
| 659 |
rest=( |
| 660 |
feature = $feature; |
| 661 |
$( |
| 662 |
feature_crate = $feature; |
| 663 |
name = $name_both; |
| 664 |
crate_docs = $crate_docs; |
| 665 |
)? |
| 666 |
$( |
| 667 |
feature_attr = $feature; |
| 668 |
attr = $attr; |
| 669 |
attr_docs = $attr_docs; |
| 670 |
example = $example; |
| 671 |
)? |
| 672 |
validate = $validate; |
| 673 |
original_defaults = {$(($($default)*))*}; |
| 674 |
) |
| 675 |
); |
| 676 |
}; |
| 677 |
|
| 678 |
// Stop when we hit the final default. |
| 679 |
(@process accum=($($accum:tt)*), negative=$negative:tt, defaults=[(_ => $default_value:tt) $($ignored:tt)*], warn=($($default_warn:literal)?), next=[$next:path[$next_args:tt]], rest=($($rest:tt)*)) => { |
| 680 |
|
| 681 |
$( |
| 682 |
#[cfg(not(any $negative))] |
| 683 |
const _: () = { |
| 684 |
#[deprecated(note = $default_warn)] |
| 685 |
const fn warn_unsupported_target() {} |
| 686 |
|
| 687 |
warn_unsupported_target() |
| 688 |
}; |
| 689 |
)? |
| 690 |
|
| 691 |
$next ! ( $next_args, (($($rest)* default = [ |
| 692 |
$($accum)* |
| 693 |
((not(any $negative)) => $default_value) |
| 694 |
])) ); |
| 695 |
}; |
| 696 |
|
| 697 |
// Accumulate the expression + negative and add to the negative. |
| 698 |
(@process accum=($($accum:tt)*), negative=($($negative:tt)*), defaults=[(($default_expr:meta) => $default_value:tt) $($default_rest:tt)*], $($rest:tt)*) => { |
| 699 |
$crate::__process_defaults!(@process |
| 700 |
accum=($($accum)* ((all($default_expr, not(any ($($negative)*)))) => $default_value) ), |
| 701 |
negative=($($negative)* $default_expr ,), |
| 702 |
defaults=[$($default_rest)*], $($rest)*); |
| 703 |
}; |
| 704 |
} |
| 705 |
|
| 706 |
#[macro_export] |
| 707 |
#[doc(hidden)] |
| 708 |
macro_rules! __evaluate_defaults { |
| 709 |
( @entry next=$next:path[$next_args:tt], input=(( |
| 710 |
feature = $feature:ident; |
| 711 |
$( |
| 712 |
feature_crate = $feature_crate:ident; |
| 713 |
name = $name:literal; |
| 714 |
crate_docs = $crate_docs:tt; |
| 715 |
)? |
| 716 |
$( |
| 717 |
feature_attr = $feature_attr:ident; |
| 718 |
attr = $attr:tt; |
| 719 |
attr_docs = $attr_docs:tt; |
| 720 |
example = $example:tt; |
| 721 |
)? |
| 722 |
validate = $validate:tt; |
| 723 |
original_defaults = $original_defaults:tt; |
| 724 |
default = [ |
| 725 |
$( ($default_expr:tt => $default_value:tt) )* |
| 726 |
] |
| 727 |
))) => { |
| 728 |
$crate::__evaluate_defaults!(@process next=[$next[$next_args]], input=($( ($default_expr => $default_value) )*), rest=( |
| 729 |
feature = $feature; |
| 730 |
$( |
| 731 |
feature_crate = $feature_crate; |
| 732 |
name = $name; |
| 733 |
crate_docs = $crate_docs; |
| 734 |
)? |
| 735 |
$( |
| 736 |
feature_attr = $feature_attr; |
| 737 |
attr = $attr; |
| 738 |
attr_docs = $attr_docs; |
| 739 |
example = $example; |
| 740 |
)? |
| 741 |
validate = $validate; |
| 742 |
original_defaults = $original_defaults; |
| 743 |
)); |
| 744 |
}; |
| 745 |
|
| 746 |
(@process next=$next:tt, input=($( ($default_expr:tt => $default_value:tt) )*), rest=$rest:tt) => { |
| 747 |
$( |
| 748 |
#[cfg $default_expr] |
| 749 |
$crate::__evaluate_defaults!(@final $next, $rest, default = $default_value); |
| 750 |
)* |
| 751 |
}; |
| 752 |
|
| 753 |
(@final [$next:path[$next_args:tt]], ($($rest:tt)*), default = (compile_error! $args:tt)) => { |
| 754 |
compile_error! $args; |
| 755 |
}; |
| 756 |
|
| 757 |
(@final [$next:path[$next_args:tt]], ($($rest:tt)*), default = $default_value:tt) => { |
| 758 |
$next ! ( $next_args, (($($rest)* default = $default_value;)) ); |
| 759 |
}; |
| 760 |
} |
| 761 |
|
| 762 |
#[macro_export] |
| 763 |
#[doc(hidden)] |
| 764 |
macro_rules! __feature_square { |
| 765 |
( @entry next=$next:path[$next_args:tt], input=( |
| 766 |
($(( |
| 767 |
feature = $all:ident; |
| 768 |
$($ignored:tt)* |
| 769 |
))*) |
| 770 |
) ) => { |
| 771 |
$crate::__feature_square!( @loop queue=[$($all)*], mult=[$($all)*], next=$next[$next_args] ); |
| 772 |
}; |
| 773 |
( @loop queue=[$($all:ident)*], mult=$mult:tt, next=$next:path[$next_args:tt] ) => { |
| 774 |
$next ! ( $next_args, ( |
| 775 |
($( ($all $mult) )*) |
| 776 |
) ); |
| 777 |
}; |
| 778 |
( $($input:tt)* ) => { |
| 779 |
const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); }; |
| 780 |
}; |
| 781 |
} |
| 782 |
|
| 783 |
#[macro_export] |
| 784 |
#[doc(hidden)] |
| 785 |
macro_rules! __extract_features { |
| 786 |
( @entry macro=$macro_parse:path, next=$next:tt, features=$features:tt, all_features=$all_features:tt) => { |
| 787 |
$crate::__extract_features!(@loop accum=(), macro=$macro_parse, next=$next, features=$features, all_features=$all_features); |
| 788 |
}; |
| 789 |
( @loop accum=$accum:tt, macro=$macro_parse:path, next=($next:path[$next_args:tt]), features=(), all_features=$all_features:tt) => { |
| 790 |
$next ! ( $next_args, $accum ); |
| 791 |
}; |
| 792 |
( @loop accum=$accum:tt, macro=$macro_parse:path, next=$next:tt, features=($feature:ident $($feature_rest:tt)*), all_features=$all_features:tt) => { |
| 793 |
$macro_parse!(@extract next=__extract_features[(@cont accum=$accum, macro=$macro_parse, next=$next, features=($($feature_rest)*), all_features=$all_features)] $feature $all_features); |
| 794 |
}; |
| 795 |
( (@cont accum=($($accum:tt)*), $($args:tt)*), ($name:ident = $output:tt $($extra:tt)+) ) => { |
| 796 |
$crate::__extract_features!((@cont accum=($($accum)*), $($args)*), ($name = $($extra)+)); |
| 797 |
}; |
| 798 |
( (@cont accum=($($accum:tt)*), $($args:tt)*), ($name:ident = $output:tt) ) => { |
| 799 |
$crate::__extract_features!(@loop accum=($($accum)* $name = $output ,), $($args)*); |
| 800 |
}; |
| 801 |
( (@cont accum=($($accum:tt)*), $($args:tt)*), ($name:ident = ) ) => { |
| 802 |
$crate::__extract_features!(@loop accum=($($accum)* $name = () ,), $($args)*); |
| 803 |
}; |
| 804 |
} |
| 805 |
|
| 806 |
/// Extracts the meta items from the proc-macro attribute. |
| 807 |
/// |
| 808 |
/// This one is complex, follow along with the comments below... |
| 809 |
#[macro_export] |
| 810 |
#[doc(hidden)] |
| 811 |
macro_rules! __extract_meta { |
| 812 |
// No args (could probably shortcut this too) |
| 813 |
( @entry next=$next:path[$next_args:tt], input=( |
| 814 |
$macro_name:ident $( () )? |
| 815 |
), args=[$macro_path:path]) => { |
| 816 |
// This will return to us after the generated macro has finished processing. |
| 817 |
$macro_path!(@meta 0 $macro_name macro=$macro_path, next=$crate::__extract_meta[[@finish $macro_path, next=$next[$next_args]]]); |
| 818 |
}; |
| 819 |
|
| 820 |
// Start with a rule that matches all the various types of meta attributes. |
| 821 |
// Note that we don't fully validate here, it just needs to be able to |
| 822 |
// handle every type of meta attribute (ie: paths, idents, literals, token-trees). |
| 823 |
// |
| 824 |
// If we can't get a match here, the error in the next rule triggers. |
| 825 |
( @entry next=$next:path[$next_args:tt], input=( |
| 826 |
$macro_name:ident ( |
| 827 |
$( |
| 828 |
$name:ident $( ($($args:tt)*) )? $( = $value:tt $( $value_ident:ident )? $( :: $value_path:ident )* )? |
| 829 |
),* |
| 830 |
) |
| 831 |
), args=[$macro_path:path]) => { |
| 832 |
// This will return to us after the generated macro has finished processing. |
| 833 |
$macro_path!(@meta 0 $macro_name macro=$macro_path, next=$crate::__extract_meta[[@finish $macro_path, next=$next[$next_args]]] $( |
| 834 |
// Pass each of the attributes down, but we separate with a `, $name ;` |
| 835 |
// sequence to help the downstream macro split things up. |
| 836 |
($name $( ($( $args )*) )? $( = $value $( $value_ident )? $( :: $value_path )* )?) , $name ; |
| 837 |
)*); |
| 838 |
}; |
| 839 |
|
| 840 |
( @entry next=$next:path[$next_args:tt], input=( |
| 841 |
$macro_name:ident ( |
| 842 |
$init_value:tt $( $init_value_ident:ident )? $( :: $init_value_path:ident )* |
| 843 |
$( |
| 844 |
, $name:ident $( ($($args:tt)*) )? $( = $value:tt $( $value_ident:ident )? $( :: $value_path:ident )* )? |
| 845 |
)* |
| 846 |
) |
| 847 |
), args=[$macro_path:path]) => { |
| 848 |
// This will return to us after the generated macro has finished processing. |
| 849 |
$macro_path!(@meta 0 $macro_name macro=$macro_path, next=$crate::__extract_meta[[@finish $macro_path, next=$next[$next_args]]] |
| 850 |
($init_value $( $init_value_ident )? $( :: $init_value_path )*) , initial ; $( |
| 851 |
// Pass each of the attributes down, but we separate with a `, $name ;` |
| 852 |
// sequence to help the downstream macro split things up. |
| 853 |
($name $( ($( $args )*) )? $( = $value $( $value_ident )? $( :: $value_path )* )?) , $name ; |
| 854 |
)*); |
| 855 |
}; |
| 856 |
|
| 857 |
// If we couldn't parse one of those forms above, this path gets hit. |
| 858 |
( @entry next=$next:path[$next_args:tt], input=( |
| 859 |
$macro_name:ident ( |
| 860 |
$($input:tt)* |
| 861 |
) |
| 862 |
), args=[$macro_path:path]) => { |
| 863 |
const _: () = { |
| 864 |
compile_error!(concat!("Unexpected form for meta attribute: ", |
| 865 |
stringify!($($input)*), |
| 866 |
"\n\n... expected 'attr', 'attr = value', 'attr(arg)', 'attr(arg) = value'")); |
| 867 |
}; |
| 868 |
}; |
| 869 |
// The generated macro passes this back to us with the value it parsed and |
| 870 |
// the default for the feature. If there was no value, we only get the |
| 871 |
// default. We'll emit the specified or default feature value and a flag |
| 872 |
// (value or default). |
| 873 |
// |
| 874 |
// If more than one value was specified for a given attribute, the rule |
| 875 |
// below will trigger and error out. |
| 876 |
// |
| 877 |
// If the generated macro doesn't recognize an attribute, it'll call back to |
| 878 |
// us with @error. |
| 879 |
( [@finish $macro_path:path, next=$next:path[$next_args:tt]], |
| 880 |
($( |
| 881 |
( $name:ident = $value:tt $value_what:ident $ignored:tt $( , $def_value:tt $def_value_what:ident $ignored2:tt )? ) |
| 882 |
)*) |
| 883 |
) => { |
| 884 |
// Pass the calculated feature back to @validate, which will continue on if all |
| 885 |
// features match the validate expressions. |
| 886 |
$macro_path!(@validate macro=$macro_path, next=$next[$next_args], |
| 887 |
test=( $($name = [$value],)* ) |
| 888 |
pass=( $($name = $value $value_what,)* ) ); |
| 889 |
}; |
| 890 |
// Catch duplicate items. |
| 891 |
( [@finish $macro_path:path, next=$next:path[$next_args:tt]], |
| 892 |
( |
| 893 |
$(( $name:ident = $value:tt $value_what:ident $ignore:tt $( , $def_value:tt $def_value_what:ident $ignore2:tt $( $comma:tt $($rest:tt)* )? )? ))* |
| 894 |
) |
| 895 |
) => { |
| 896 |
// TODO: This should show the underlying attribute rather than the internal name. |
| 897 |
const _: () = { compile_error!(concat!("Duplicate meta attribute: '", stringify!( |
| 898 |
$( $($($name = ...$comma)?)? )* |
| 899 |
))) }; |
| 900 |
}; |
| 901 |
// Unknown items (valid form, unrecognized pattern). |
| 902 |
( @error rest=( |
| 903 |
($($failing:tt)*) , $ignore:tt ; $($rest:tt)* |
| 904 |
) attrs=( $( ( $($example:tt)* ) )* )) => { |
| 905 |
const _: () = { compile_error!(concat!("Unexpected meta attribute: '", stringify!( |
| 906 |
$($failing)* |
| 907 |
), |
| 908 |
"'\n...expected one of:\n ", |
| 909 |
$($($example)*, "\n ",)*)); }; |
| 910 |
}; |
| 911 |
( $($input:tt)* ) => { |
| 912 |
const _: () = { compile_error!(concat!("Unexpected input in __extract_meta: ", stringify!($($input)*))); }; |
| 913 |
}; |
| 914 |
} |
| 915 |
|
| 916 |
/// Generates the various utility macros used to parse the actual macro under |
| 917 |
/// the hood. |
| 918 |
#[macro_export] |
| 919 |
#[doc(hidden)] |
| 920 |
macro_rules! __make_macros { |
| 921 |
( @entry next=$next:path[$next_args:tt], input=( |
| 922 |
( |
| 923 |
$macro_name:ident |
| 924 |
$macro_parse:ident |
| 925 |
$( $default_feature:ident )? |
| 926 |
) |
| 927 |
( |
| 928 |
$( |
| 929 |
( |
| 930 |
feature = $feature:ident; |
| 931 |
$( |
| 932 |
feature_crate = $feature_crate:ident; |
| 933 |
name = $name:literal; |
| 934 |
crate_docs = $crate_docs:tt; |
| 935 |
)? |
| 936 |
$( |
| 937 |
feature_attr = $feature_attr:ident; |
| 938 |
attr = [($($attr:tt)*) => ($($attr_output:tt)*)]; |
| 939 |
attr_docs = $attr_docs:tt; |
| 940 |
example = $example:tt; |
| 941 |
)? |
| 942 |
validate = [$($validate:tt)*]; |
| 943 |
original_defaults = $original_defaults:tt; |
| 944 |
default = $default_value:tt; |
| 945 |
) |
| 946 |
)* |
| 947 |
) |
| 948 |
( |
| 949 |
$( |
| 950 |
($feature_sq_1:ident [$($feature_sq_2:ident)*]) |
| 951 |
)* |
| 952 |
) |
| 953 |
$raw_features:tt |
| 954 |
), args=[$dollar:tt]) => { |
| 955 |
#[doc(hidden)] |
| 956 |
#[macro_export] |
| 957 |
macro_rules! $macro_parse { |
| 958 |
// @extract takes the full or partial feature tuple and extracts one |
| 959 |
// feature at a time. If present, the next macro receives |
| 960 |
// (name = $feature_value:tt), otherwise (). |
| 961 |
$( |
| 962 |
(@extract next=$next_macro:path[$next_macro_args:tt] $feature_sq_1 |
| 963 |
( |
| 964 |
$dollar ( |
| 965 |
$( |
| 966 |
$dollar ( $feature_sq_2 = $dollar $feature_sq_2:tt)? |
| 967 |
)* , |
| 968 |
)* |
| 969 |
)) => { |
| 970 |
$next_macro ! ( $next_macro_args, ( |
| 971 |
$feature_sq_1 = $dollar ( $dollar ( $dollar $feature_sq_1 )? )* |
| 972 |
) ); |
| 973 |
}; |
| 974 |
)* |
| 975 |
|
| 976 |
(@extract next=$next_macro:path[$next_macro_args:tt] $dollar feature:ident $dollar ($dollar rest:tt)*) => { |
| 977 |
const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($dollar feature))); }; |
| 978 |
}; |
| 979 |
|
| 980 |
// @meta extracts the meta items from the proc-macro attribute. The |
| 981 |
// items need to be pre-processed to ensure that each one ends with |
| 982 |
// a comma and a semicolon to disambiguate. |
| 983 |
(@meta $depth:literal $macro_name macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt] |
| 984 |
$dollar ( |
| 985 |
$($( |
| 986 |
$dollar( |
| 987 |
($($attr)*) |
| 988 |
, |
| 989 |
$dollar $feature:tt // first token |
| 990 |
)? |
| 991 |
)?)* |
| 992 |
; |
| 993 |
)* |
| 994 |
) => { |
| 995 |
$next_macro ! ( $next_macro_args, ( $(($feature = $( |
| 996 |
$dollar ( $dollar( $($attr_output)* value $dollar $feature, )? )* |
| 997 |
)? $default_value default _))* ) ); |
| 998 |
}; |
| 999 |
|
| 1000 |
$( |
| 1001 |
// If a default feature is specified and the previous parse failed, |
| 1002 |
// try again with the default feature. |
| 1003 |
(@meta 0 $macro_name macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt] |
| 1004 |
($dollar ($first:tt)*) $dollar ($rest:tt)*) => { |
| 1005 |
$macro_path ! (@meta 1 $macro_name macro=$macro_path, next=$next_macro[$next_macro_args] ($default_feature=$dollar ($first)*) $dollar ($rest)*); |
| 1006 |
}; |
| 1007 |
)? |
| 1008 |
|
| 1009 |
// Unrecognized, munch until end of recognized input. |
| 1010 |
(@meta $depth:literal $macro_name macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt] |
| 1011 |
$dollar ($dollar rest:tt)*) => { |
| 1012 |
$macro_path!(@metaerror macro=$macro_path, next=$next_macro[$next_macro_args] $dollar($dollar rest)*); |
| 1013 |
}; |
| 1014 |
|
| 1015 |
(@meta $depth:literal $wrong_macro:ident $dollar ($rest:tt)*) => { |
| 1016 |
compile_error!(concat!("Unexpected macro: #[", stringify!($wrong_macro), "], expected #[", stringify!($macro_name), "]")); |
| 1017 |
}; |
| 1018 |
|
| 1019 |
// Munch one item |
| 1020 |
(@metaerror macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt] |
| 1021 |
$($( |
| 1022 |
$dollar( |
| 1023 |
($($attr)*) |
| 1024 |
, |
| 1025 |
$dollar $feature:tt // first token |
| 1026 |
)? |
| 1027 |
)?)* |
| 1028 |
; |
| 1029 |
$dollar ($dollar rest:tt)*) => { |
| 1030 |
$macro_path!(@metaerror macro=$macro_path, next=$next_macro[$next_macro_args] $dollar($dollar rest)*); |
| 1031 |
}; |
| 1032 |
|
| 1033 |
// Found the error! |
| 1034 |
(@metaerror macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt] |
| 1035 |
$dollar ($dollar rest:tt)*) => { |
| 1036 |
$crate::__extract_meta!(@error rest=($dollar($dollar rest)*) attrs=($($($example)?)*)); |
| 1037 |
}; |
| 1038 |
|
| 1039 |
// @validate ensures that all features match the validate expressions. If this doesn't |
| 1040 |
// match, the next rule triggers. |
| 1041 |
(@validate macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt], |
| 1042 |
test=($( |
| 1043 |
$feature = $($validate)*, |
| 1044 |
)*) |
| 1045 |
pass=$pass:tt |
| 1046 |
) => { |
| 1047 |
$next_macro ! ( $next_macro_args, $pass ); |
| 1048 |
}; |
| 1049 |
|
| 1050 |
// @validate didn't match. |
| 1051 |
(@validate macro=$macro_path:path, next=$next_macro:path[$next_macro_args:tt], |
| 1052 |
test=$test:tt |
| 1053 |
pass=$pass:tt |
| 1054 |
) => { |
| 1055 |
const _: () = { stringify!($test); }; |
| 1056 |
$macro_path!(@validateerror macro=$macro_path, test=$test); |
| 1057 |
}; |
| 1058 |
|
| 1059 |
// Munch one item |
| 1060 |
(@validateerror macro=$macro_path:path, test=( |
| 1061 |
$( |
| 1062 |
$dollar( |
| 1063 |
$feature = $($validate)* |
| 1064 |
)? |
| 1065 |
)* |
| 1066 |
, $dollar ($dollar rest:tt)*) |
| 1067 |
) => { |
| 1068 |
// Pass, try next |
| 1069 |
$macro_path!(@validateerror macro=$macro_path, test=($dollar($dollar rest)*)); |
| 1070 |
}; |
| 1071 |
|
| 1072 |
// If we failed to munch an item, that was the bad one. |
| 1073 |
(@validateerror macro=$macro_path:path, test=( |
| 1074 |
$feature_name:ident = [$value_bad:tt], |
| 1075 |
$dollar ($dollar rest:tt)*) |
| 1076 |
) => { |
| 1077 |
compile_error!(concat!("Invalid attribute: ", stringify!($feature_name), " = ", stringify!($value_bad), "\nExpected one of:\n" $($(," ",concat! $example,"\n")?)*)); |
| 1078 |
}; |
| 1079 |
|
| 1080 |
// Extracts all features specified in $all_features and passes them |
| 1081 |
// to the next macro. |
| 1082 |
(@entry next=$next_macro:path [$next_macro_args:tt], |
| 1083 |
input=$all_features:tt, |
| 1084 |
args=[$macro:path => @extract $features:tt]) => { |
| 1085 |
$crate::__extract_features!(@entry macro=$macro, next=($next_macro [$next_macro_args]), |
| 1086 |
features=$features, all_features=$all_features); |
| 1087 |
}; |
| 1088 |
|
| 1089 |
// Extracts features from enabled crate features. |
| 1090 |
(@entry next=$next_macro:path [$next_macro_args:tt], |
| 1091 |
input=$all_features:tt, |
| 1092 |
args=[$macro:path => @crate]) => { |
| 1093 |
$next_macro ! ( $next_macro_args, ( |
| 1094 |
$( $feature = $default_value, )* |
| 1095 |
) ); |
| 1096 |
}; |
| 1097 |
|
| 1098 |
|
| 1099 |
// Extracts the self-attribute from a list of attributes. |
| 1100 |
|
| 1101 |
// Shortcut: #[$macro_name] is first |
| 1102 |
(@entry next=$next_macro:path [$next_macro_args:tt], |
| 1103 |
input=(#[$macro_name $dollar ($args:tt)?] $dollar ( # $dollar attr:tt )*), |
| 1104 |
args=[$macro:path => @self]) => { |
| 1105 |
$next_macro ! ( $next_macro_args, ( ( $macro_name $dollar ($args)? )($dollar ( # $dollar attr )*) ) ); |
| 1106 |
}; |
| 1107 |
// Different macro, recurse |
| 1108 |
(@entry next=$next_macro:path [$next_macro_args:tt], |
| 1109 |
input=(# $dollar first:tt $dollar ( # $dollar rest:tt )*), |
| 1110 |
args=[$macro:path => @self]) => { |
| 1111 |
$macro!(@self next=$next_macro [$next_macro_args], accum=(# $dollar first), input=($dollar ( # $dollar rest )*), args=[$macro => @self]); |
| 1112 |
}; |
| 1113 |
// Found |
| 1114 |
(@self next=$next_macro:path [$next_macro_args:tt], |
| 1115 |
accum=($dollar ( $dollar accum:tt )*), |
| 1116 |
input=(#[$macro_name $dollar ($args:tt)?] $dollar ( # $dollar rest:tt )*), |
| 1117 |
args=[$macro:path => @self]) => { |
| 1118 |
$next_macro ! ( $next_macro_args, ( ( $macro_name $dollar ($args)? )($dollar ( $dollar accum )* $dollar ( # $dollar rest )*) ) ); |
| 1119 |
}; |
| 1120 |
// Keep recursing |
| 1121 |
(@self next=$next_macro:path [$next_macro_args:tt], |
| 1122 |
accum=($dollar ( $dollar accum:tt )*), |
| 1123 |
input=(# $dollar first:tt $dollar ( # $dollar rest:tt )*), |
| 1124 |
args=[$macro:path => @self]) => { |
| 1125 |
$macro!(@self next=$next_macro [$next_macro_args], accum=($dollar ( $dollar accum )* # $dollar first), input=($dollar ( # $dollar rest )*), args=[$macro => @self]); |
| 1126 |
}; |
| 1127 |
// Not found |
| 1128 |
(@self next=$next_macro:path [$next_macro_args:tt], |
| 1129 |
accum=($dollar ( $dollar accum:tt )*), |
| 1130 |
input=(), |
| 1131 |
args=[$macro:path => @self]) => { |
| 1132 |
compile_error!(concat!("Expected #[",stringify!($macro_name), "], got ",stringify!($dollar($dollar accum)*),".")); |
| 1133 |
}; |
| 1134 |
|
| 1135 |
|
| 1136 |
// Extracts the raw features from the input and passes them to the next macro. |
| 1137 |
(@entry next=$next_macro:path [$next_macro_args:tt], |
| 1138 |
input=$input:tt, // ignored |
| 1139 |
args=[$macro:path => @raw]) => { |
| 1140 |
$next_macro ! ( $next_macro_args, $raw_features ); |
| 1141 |
}; |
| 1142 |
|
| 1143 |
(@entry $dollar ($rest:tt)*) => { |
| 1144 |
const _: () = { compile_error!(concat!("Unexpected input to __make_macros: ", stringify!($dollar ($rest)*))); }; |
| 1145 |
}; |
| 1146 |
} |
| 1147 |
|
| 1148 |
$next ! ( $next_args, () ); |
| 1149 |
}; |
| 1150 |
} |
| 1151 |
|
| 1152 |
/// Extract a subset of the feature configuration for later documentation generation. |
| 1153 |
#[macro_export] |
| 1154 |
#[doc(hidden)] |
| 1155 |
macro_rules! __pick_doc_vars { |
| 1156 |
( @entry next=$next:path[$next_args:tt], input=( |
| 1157 |
$params:tt |
| 1158 |
( |
| 1159 |
$( |
| 1160 |
( |
| 1161 |
feature = $feature:ident; |
| 1162 |
$( |
| 1163 |
feature_crate = $feature_crate:ident; |
| 1164 |
name = $name:literal; |
| 1165 |
crate_docs = $crate_docs:tt; |
| 1166 |
)? |
| 1167 |
$( |
| 1168 |
feature_attr = $feature_attr:ident; |
| 1169 |
attr = $attr:tt; |
| 1170 |
attr_docs = $attr_docs:tt; |
| 1171 |
example = $example:tt; |
| 1172 |
)? |
| 1173 |
validate = $validate:tt; |
| 1174 |
original_defaults = $original_defaults:tt; |
| 1175 |
default = $default_value:tt; |
| 1176 |
) |
| 1177 |
)* |
| 1178 |
) |
| 1179 |
$feature_square:tt |
| 1180 |
)) => { |
| 1181 |
$next ! ( $next_args, ( |
| 1182 |
($( |
| 1183 |
( |
| 1184 |
feature = $feature; |
| 1185 |
$( |
| 1186 |
feature_crate = $feature_crate; |
| 1187 |
name = $name; |
| 1188 |
crate_docs = $crate_docs; |
| 1189 |
)? |
| 1190 |
$( |
| 1191 |
feature_attr = $feature_attr; |
| 1192 |
attr_docs = $attr_docs; |
| 1193 |
example = $example; |
| 1194 |
)? |
| 1195 |
original_defaults = $original_defaults; |
| 1196 |
) |
| 1197 |
)*) |
| 1198 |
)); |
| 1199 |
}; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/// Generates a module named $macro_impl and inside, two crate-scoped macros: |
| 1203 |
/// |
| 1204 |
/// make_crate_docs and make_attr_docs. |
| 1205 |
/// |
| 1206 |
/// These macros are used to generate the documentation for the crate-scoped |
| 1207 |
/// features. |
| 1208 |
/// |
| 1209 |
/// `make_crate_docs` generates docs for crate features. `make_attr_docs` |
| 1210 |
/// generates docs for proc macro attributes. |
| 1211 |
#[macro_export] |
| 1212 |
#[doc(hidden)] |
| 1213 |
macro_rules! __make_docs { |
| 1214 |
( @entry next=$next:path[$next_args:tt], input=( |
| 1215 |
$( |
| 1216 |
( |
| 1217 |
feature = $feature:ident; |
| 1218 |
$( |
| 1219 |
feature_crate = $feature_crate:ident; |
| 1220 |
name = $feature_name:literal; |
| 1221 |
crate_docs = [ $( $crate_doc_lit:literal )* ]; |
| 1222 |
)? |
| 1223 |
$( |
| 1224 |
feature_attr = $feature_attr:ident; |
| 1225 |
attr_docs = [ $( $attr_doc_lit:literal )* ]; |
| 1226 |
example = ($($example:tt)*); |
| 1227 |
)? |
| 1228 |
original_defaults = $original_defaults:tt; |
| 1229 |
) |
| 1230 |
)* |
| 1231 |
)) => { |
| 1232 |
$crate::__make_docs!(@defaults accum=( |
| 1233 |
#![doc = "\n\n# Crate Features\n\n| Cargo feature | Description |\n| --- | --- |"] |
| 1234 |
$( |
| 1235 |
$( |
| 1236 |
#![doc = concat!("\n| `", $feature_name, "` | ", $( $crate_doc_lit, )* " |")] |
| 1237 |
)? |
| 1238 |
)* |
| 1239 |
#![doc = "\n\n# Macro Attributes\n\n<table><tr><th>Attribute</th><th>Description</th></tr>\n"] |
| 1240 |
$( |
| 1241 |
$( |
| 1242 |
#![doc = concat!("\n<tr><td><code>", $( $example )*, "</code></td><td>\n\n", $( $attr_doc_lit, "\n", )* "\n\n</td></tr>")] |
| 1243 |
)? |
| 1244 |
)* |
| 1245 |
#![doc = "</table>"] |
| 1246 |
#![doc = "\n\n# Defaults"] |
| 1247 |
), |
| 1248 |
($( |
| 1249 |
(feature = $feature; default = $original_defaults;) |
| 1250 |
)*) |
| 1251 |
); |
| 1252 |
}; |
| 1253 |
|
| 1254 |
// Emits one "defaults" subsection per feature that has a non-()` default. |
| 1255 |
(@defaults accum=($($accum:tt)*), ()) => { |
| 1256 |
mod __generated_docs { |
| 1257 |
$($accum)* |
| 1258 |
} |
| 1259 |
}; |
| 1260 |
|
| 1261 |
// Hide attributes with no default. |
| 1262 |
(@defaults accum=($($accum:tt)*), ((feature = $feature:ident; default = {(_ => ())};) $($rest:tt)*)) => { |
| 1263 |
$crate::__make_docs!(@defaults accum=( |
| 1264 |
$($accum)* |
| 1265 |
), ($($rest)*)); |
| 1266 |
}; |
| 1267 |
// Hide crate features with no default. |
| 1268 |
(@defaults accum=($($accum:tt)*), ((feature = $feature:ident; default = {((feature = $feature_lit:literal) => $feature_default_value:ident) (_ => ())};) $($rest:tt)*)) => { |
| 1269 |
$crate::__make_docs!(@defaults accum=( |
| 1270 |
$($accum)* |
| 1271 |
), ($($rest)*)); |
| 1272 |
}; |
| 1273 |
|
| 1274 |
(@defaults accum=($($accum:tt)*), ((feature = $feature:ident; default = $default_value:tt;) $($rest:tt)*)) => { |
| 1275 |
$crate::__make_docs!(@default accum=( |
| 1276 |
$($accum)* |
| 1277 |
//! |
| 1278 |
#![doc = concat!("## `", stringify!($feature), "`")] |
| 1279 |
//! |
| 1280 |
//! ```rust |
| 1281 |
//! # #[cfg(false)] { |
| 1282 |
), ((feature = $feature; default = $default_value;) $($rest)*)); |
| 1283 |
}; |
| 1284 |
(@default accum=($($accum:tt)*), ((feature = $feature:ident; default = { |
| 1285 |
(($($branch:tt)*) => $default_value:tt) $($branch_rest:tt)*};) $($rest:tt)*)) => { |
| 1286 |
$crate::__make_docs!(@default accum=( |
| 1287 |
$($accum)* |
| 1288 |
#![doc = concat!("#[cfg(", stringify!($($branch)*), ")]")] |
| 1289 |
//! # const _: () = { let |
| 1290 |
#![doc = concat!(stringify!($feature), " = ", stringify!($default_value))] |
| 1291 |
//! # ; }; |
| 1292 |
//! |
| 1293 |
), ((feature = $feature; default = {$($branch_rest)*};) $($rest)*)); |
| 1294 |
}; |
| 1295 |
(@default accum=($($accum:tt)*), ((feature = $feature:ident; default = { |
| 1296 |
(_ => $default_value:tt) $($branch_rest:tt)*};) $($rest:tt)*)) => { |
| 1297 |
$crate::__make_docs!(@defaults accum=( |
| 1298 |
$($accum)* |
| 1299 |
//! // default |
| 1300 |
#![doc = concat!(stringify!($feature), " = ", stringify!($default_value))] |
| 1301 |
//! # } |
| 1302 |
//! ``` |
| 1303 |
), ($($rest)*)); |
| 1304 |
}; |
| 1305 |
|
| 1306 |
( $($input:tt)* ) => { |
| 1307 |
const _: () = { compile_error!(concat!("Unexpected input: ", stringify!($($input)*))); }; |
| 1308 |
}; |
| 1309 |
} |