Macro repetition within repetition

I’d like to create a macro that takes a variable number of lines, each of which consists of a lhs and a varible number of Rhys elements. It’s to specify a BNF grammar and it’s own BNF grammar is approximately

Rules <- (Rule;)+
Rule <- RhsName : (LhsItem LhsType),*

So, for instance:

Express : Binary Expr, Unary Expr, Literal Expr;
Binary : Op1 Expr, Operator Token, Op2 Expr;
... for variable number of rules

So I need a pattern with repetition that has repetition within that repetition. I got a macro working that produces a class for a single rule but I also need to produce a class which references all the LhsName’s so that’s what the outer repetition is all about. I’m sorry I’m not including my code but I don’t have wi-fi in the house at the moment so am writing from my iPad at a coffee shop. If I don’t get an answer but I do get internet on my home machine I will put up my attempted code, etc. and in the meantime apologize for not being able to do so. Hopefully somebody will understand the situation (which really boils down to exactly what the title says) and be able to give a few ideas. Thanks very much. I’ve looked at similar topics on various forums and haven’t been able to form an answer.

Nested repetitions are allowed:

macro_rules! mac {
    ( $( $rhs_name:ident : $($lhs_name:ident $lhs_type:ident),* ;)+ )
    => {
        /* expanded output */

mac! {
    Express : Binary Expr, Unary Expr, Literal Expr;
    Binary : Op1 Expr, Operator Token, Op2 Expr;

Sometimes, there can be difficulties in writing the expanded output, especially when you need to mix tokens from different levels of repetition. But without the code it's difficult to say where you're running into trouble.


Man - okay - thanks so much. I apologize - I'd swear I tried exactly this but something failed. Given my internet state at the time, I was unable to supply exact problem, etc.. Anyway, I'm not sure what I did wrong, but this is great and thank you again!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.