1 // run-pass
2 
3 #![allow(unused_mut)]
4 
5 // Check that when `?` is followed by what looks like a Kleene operator (?, +, and *)
6 // then that `?` is not interpreted as a separator. In other words, `$(pat)?+` matches `pat +`
7 // or `+` but does not match `pat` or `pat ? pat`.
8 
9 // edition:2018
10 
11 macro_rules! foo {
12     // Check for `?`.
13     ($($a:ident)? ? $num:expr) => {
14         foo!($($a)? ; $num);
15     };
16     // Check for `+`.
17     ($($a:ident)? + $num:expr) => {
18         foo!($($a)? ; $num);
19     };
20     // Check for `*`.
21     ($($a:ident)? * $num:expr) => {
22         foo!($($a)? ; $num);
23     };
24     // Check for `;`, not a kleene operator.
25     ($($a:ident)? ; $num:expr) => {
26         let mut x = 0;
27 
28         $(
29             x += $a;
30         )?
31 
32         assert_eq!(x, $num);
33     };
34 }
35 
main()36 pub fn main() {
37     let a = 1;
38 
39     // Accept 0 repetitions.
40     foo!( ; 0);
41     foo!( + 0);
42     foo!( * 0);
43     foo!( ? 0);
44 
45     // Accept 1 repetition.
46     foo!(a ; 1);
47     foo!(a + 1);
48     foo!(a * 1);
49     foo!(a ? 1);
50 }
51