goffrie / plex

a parser and lexer generator as a Rust procedural macro
Apache License 2.0
409 stars 26 forks source link

One block for a set of two or more production rules? #55

Open maxrdz opened 8 months ago

maxrdz commented 8 months ago

I have a set of rules under one production in my CFG that both have basically duplicated blocks of code, and could be handled in one. Is there a way to do this? The following example is what my code looks like:

numeric_with_modulus: DCNumericType {
    numeric_type_token[mut nt] Percent number[n] => {
        // do something 
    }
    numeric_with_explicit_cast[mut nt] Percent number[n] => {
        // do something very similar
    }
}

Would be nice to have something like this:

numeric_with_modulus: DCNumericType {
    numeric_type_token[mut nt] Percent number[n] |
    numeric_with_explicit_cast[mut nt] Percent number[n] => {
        // do the similar thing :p
    }
}

of course, both rules would have to have the same parameters. or, same 'function' signature if that's the right term :p Since each block is a bit like a function; with the non-terminals / terminals being the parameters.