I have a lot of structs and need to generically get types out of them. Before today I did not have a need for Option and everything worked fine, but now I can't figure out how to add it...
1 error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
--> src/main.rs:16:63
|
16 | .or_else(|| impl_field_access_opt!(ref: $($pt.$t $(?)?),*))
| ^^^
repetitions like this can be used in the pattern arms, but not in the expansion. you must have captured meta variables inside if you want to use it in the expansion, otherwise, there could be ambiguity, for examle:
in the most general case, as a last resort, you can always use multiple matcher rule clauses to match the cases with and without the optional tokens and ditch the $()? repetition all together, just like how you handle the first question mark. this will work, but it can expode exponentially with the number of optional repetitions.
but usually there exists simpler solutions for specific cases. take the $(?)? as an example, the optional question mark operator (or any postfix operators) can be captured as part of the expression before it. e.g., given the expression self.baz?, instead of matching the individual tokens as $pt:ident.$t:ident $(?), you can just capture it as $e:exp as a whole.
and further more, in your example, you are just forwarding the rest of the tokens to the same macros recursively, so you can capture the to-be-forwarded tokens with very loose matcher rules:
Thank you for your answer, but unfortunatelly, there this different code to be generated with ? and without ?, your solution removed required logic. I did not find any way to use information provided by you to get my use case covered