Compiling playground v0.0.1 (/playground)
error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
--> src/main.rs:12:19
|
12 | $x.$access$(())?
| ^^^^
error: could not compile `playground` (bin "playground") due to 1 previous error
You can't do this. An expansion has to have an actual captured variable, and you can't capture "nothing" or arbitrary literal tokens.
The simplest way to achieve what you want is to have two rules: one that ends in () and one that doesn't.
Another approach would be to use something like $access:ident $($tail:tt)*, with the expansion being $access $($tail)*?. This matches and substitutes anything at the end of the input. The downside is that it matches and substitutes absolutely anything at the end of the input.
In that case, you'd want two rules to match "ends in ();" and "ends in ;". Both of those rules would then need to capture "everything else" after the end of the first chunk using $($tail:tt)*, and recurse on that tail.
Basically, anywhere you want to repeat matching something you can't directly write as a single capture pattern, you need to use recursion and multiple rules (one for each different form).