forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.rs
102 lines (95 loc) · 2.66 KB
/
operations.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//@ run-pass
/// Count the number of idents in a macro repetition.
macro_rules! count_idents {
( $( $i:ident ),* ) => {
${count($i)}
};
}
/// Count the number of idents in a 2-dimensional macro repetition.
macro_rules! count_idents_2 {
( $( [ $( $i:ident ),* ] ),* ) => {
${count($i)}
};
}
/// Mostly counts the number of OUTER-MOST repetitions
macro_rules! count_depth_limits {
( $( { $( [ $( $outer:ident : ( $( $inner:ident )* ) )* ] )* } )* ) => {
(
(
${count($inner)},
${count($inner, 0)},
${count($inner, 1)},
${count($inner, 2)},
${count($inner, 3)},
),
(
${count($outer)},
${count($outer, 0)},
${count($outer, 1)},
${count($outer, 2)},
),
)
};
}
/// Produce (index, length) pairs for literals in a macro repetition.
/// The literal is not included in the output, so this macro uses the
/// `ignore` meta-variable expression to create a non-expanding
/// repetition binding.
macro_rules! enumerate_literals {
( $( ($l:stmt) ),* ) => {
[$( ${ignore($l)} (${index()}, ${length()}) ),*]
};
}
/// Produce index and length tuples for literals in a 2-dimensional
/// macro repetition.
macro_rules! enumerate_literals_2 {
( $( [ $( ($l:literal) ),* ] ),* ) => {
[
$(
$(
(
${index(1)},
${length(1)},
${index(0)},
${length(0)},
$l
),
)*
)*
]
};
}
fn main() {
assert_eq!(count_idents!(a, b, c), 3);
assert_eq!(count_idents_2!([a, b, c], [d, e], [f]), 6);
assert_eq!(
count_depth_limits! {
{
[ A: (a b c) D: (d e f) ]
[ G: (g h) I: (i j k l m) ]
[ N: (n) ]
}
{
[ O: (o) P: (p q) R: (r s) ]
[ T: (t u v w x y z) ]
}
},
((26, 26, 9, 5, 2), (9, 9, 5, 2))
);
assert_eq!(enumerate_literals![("foo"), ("bar")], [(0, 2), (1, 2)]);
assert_eq!(
enumerate_literals_2![
[("foo"), ("bar"), ("baz")],
[("qux"), ("quux"), ("quuz"), ("xyzzy")]
],
[
(0, 2, 0, 3, "foo"),
(0, 2, 1, 3, "bar"),
(0, 2, 2, 3, "baz"),
(1, 2, 0, 4, "qux"),
(1, 2, 1, 4, "quux"),
(1, 2, 2, 4, "quuz"),
(1, 2, 3, 4, "xyzzy"),
]
);
}