On macros being second class to builtins

macro_rules! itself used to be a "regular" macro (from the grammar perspective), just that no user macro could be defined with that syntax. Currently, it's its own kind of thing in the syntax tree.

I posted a topic a while back proposing the idea of trying to bring back macro_name! foo { /* */ }. It didn't get much discussion, mainly because there's just not all that much demand for it.

If you're willing to dip your toes into procedurally defined macros, they can have whatever syntax within you want, so you could get something like

catch::tests! {
test_case!("foo") {
    // code common to all tests goes here
    // declare some variables, init some things

    section!("a") {
        section!("x") {
            ...
        }
        section!("y") {
            ...
        }
    }
    section!("b") {
    }
    section!("c") {
        section!("z") {
            ...
        }
        section!("w") {
            ...
        }
    }
}
}

because within the proc macro, the only requirement on input is that it tokenizes, and you can manipulate said tokens however you want to output legal Rust code.

A less drastic option would be to look more like the standard #[test] framework:

#[catch::test_case]
fn foo() {
    // code common to all tests goes here
    // declare some variables, init some things

    section!("a") {
        section!("x") {
            ...
        }
        section!("y") {
            ...
        }
    }
    section!("b") {
    }
    section!("c") {
        section!("z") {
            ...
        }
        section!("w") {
            ...
        }
    }
}

though much the same processing would likely be required to implement it.

4 Likes