| Crates.io | macro_rules_rec |
| lib.rs | macro_rules_rec |
| version | 0.1.0 |
| created_at | 2024-04-20 08:50:34.392965+00 |
| updated_at | 2024-04-20 08:50:34.392965+00 |
| description | Expand syntax of `macro_rules` to enable self recursive call |
| homepage | |
| repository | https://github.com/yasuo-ozu/macro_rules_rec |
| max_upload_size | |
| id | 1214460 |
| size | 19,601 |
The macro_rules_rec crate enables recursive calls to itself within macro_rules! in the Rust language.
In Rust, macros, especially defined with macro_rules! expands the macro's body in-place when used. This means that all intermediate states produced during the expansion must adhere to Rust's syntax.
Namely, when a macro recursively calls itself within its body, the position where this call is written must be within a syntactic element that supports macro invocation according to Rust's syntax. Typically, many macros are intended to expand into expressions, statements, types, or declaration items, and this rarely poses a problem. However, in special cases like the following, the position of the macro call does not correspond to any syntactic element in Rust, which traditionally prevented the recursive invocation of the macro:
macro_rules_rec extends the declarative macro writing with Rust's macro_rules! and enables recursive macro calls that are not constrained by Rust's syntax. Additionally, the macros generated by macro_rules_rec are pure macro_rules! and do not depend on external crates (including macro_rules_rec). This is very useful when creating libraries, as users of the library do not need to reference macro_rules_rec when using macros generated with it.
You can perform self-recursive call via $self! sentence.
# use macro_rules_rec::recursive;
#[recursive]
macro_rules! m {
($($id:ident)*) => {
$(
$self!(@ $id);
)*
};
(@ $id:ident) => {
#[allow(unused)]
let $id = 123;
};
}
m!(a b c);