Is it possible to define a macro which handles grammatical cases?

Viewed 70

I'd like to define a macro callme which can be applied like this.

fn main() {
    let a=4;
    let b=5;
    callme!(
        a (b) => { a+b } ;
        a (b) => { a*b } ;
        a (b) ~ C
    );
}

I do not know how to get a working macro definition for callme. Currently I'm trying something like this:

macro_rules! callme {
    (
        $($A: ident ($B: ident) => {$E: expr}) ; *
    ) => (
        $(
            println!("{:?} {:?} {:?}", $A, $B, $E);
        ) *
    );
    (
        $($A: ident ($B: ident) ~ $Q: ident) ; *
    ) => (
        $(
            println!("We got {:?} . {:?} . {:?}", $A, $B, $Q);
        ) *
    );
}

This does not work, because I cannot use both syntax cases at once.

1 Answers
Related