#Nested Proc Macros?

4 messages · Page 1 of 1 (latest)

graceful pulsar
#

I'm working on a game ECS toy project and am interested in using a lot of compile-time code generation for iteration logic. Ideally I'd like to be able to generate code on individual groups of components, but also generate top-level code based on collections of those component groups. For example,

ecs_world! {
    ecs_archetype!(ArchetypeTurret, Position, Health);
    ecs_archetype!(ArchetypeVehicle, Position, Velocity, Health);
    ecs_archetype!(ArchetypeMine, Position, Duration);
}

Is there a way to nest proc macros like this (where both ecs_world and ecs_archetype are proc macros) but also expand the inner macros during generation? Ideally ecs_archetype would expand to a struct and a couple of traits/functions, and ecs_world would also expand to a bunch of code that stems from reading all of the ecs_archetype definitions and the component structs they are defined with. Any guidance for how to go about this?

soft latch
#

it's not possible to do cleanly in general but in your particular case, because you are defining macros, you can have the ecs_world! macro detect the tokens ecs_archetype ! and call the ecs_archetype macro directly

#

like, call the function that's in your proc-macro crate