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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! The `iota!` macro constructs a set of related constants.
//!
//! ```rust
//! #[macro_use]
//! extern crate iota;
//!
//! iota! {
//!     const A: u8 = 1 << iota;
//!         | B
//!         | C
//!         | D
//! }
//!
//! fn main() {
//!     assert_eq!(A, 1);
//!     assert_eq!(B, 2);
//!     assert_eq!(C, 4);
//!     assert_eq!(D, 8);
//! }
//! ```
//!
//! Within an `iota!` block, the `iota` variable is an untyped integer constant
//! whose value begins at 0 and increments by 1 for every constant declared in
//! the block.
//!
//! ```rust
//! #[macro_use]
//! extern crate iota;
//!
//! iota! {
//!     const A: u8 = 1 << iota;
//!         | B
//!
//!     const C: i32 = -1; // iota is not used but still incremented
//!
//!     pub const D: u8 = iota * 2;
//!             | E
//!             | F
//! }
//!
//! // `iota` begins again from 0 in this block
//! iota! {
//!     const G: usize = 1 << (iota + 10);
//!         | H
//! }
//!
//! fn main() {
//!     assert_eq!(A, 1 << 0);
//!     assert_eq!(B, 1 << 1);
//!
//!     assert_eq!(C, -1);
//!
//!     assert_eq!(D, 3 * 2);
//!     assert_eq!(E, 4 * 2);
//!     assert_eq!(F, 5 * 2);
//!
//!     assert_eq!(G, 1 << (0 + 10));
//!     assert_eq!(H, 1 << (1 + 10));
//! }
//! ```

/// Please refer to the crate-level documentation.
#[macro_export]
macro_rules! iota {
    (const $n:ident : $t:ty = $($rest:tt)+) => {
        __iota_dup!((0) const $n : $t = $($rest)+);
    };

    (pub const $n:ident : $t:ty = $($rest:tt)+) => {
        __iota_dup!((0) pub const $n : $t = $($rest)+);
    };
}

// Duplicate the input tokens so we can match on one set and trigger errors on
// the other set.
#[macro_export]
#[doc(hidden)]
macro_rules! __iota_dup {
    (($v:expr)) => {};

    (($v:expr) const $n:ident : $t:ty = $($rest:tt)+) => {
        __iota_impl!(($v) () () const $n : $t = ($($rest)+) ($($rest)+));
    };

    (($v:expr) pub const $n:ident : $t:ty = $($rest:tt)+) => {
        __iota_impl!(($v) () (pub) const $n : $t = ($($rest)+) ($($rest)+));
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! __iota_impl {
    // ERROR: Premature semicolon.
    //
    //    const A: u8 = ;
    (($v:expr) () $vis:tt const $n:ident : $t:ty = (; $($x:tt)*) ($semi:tt $($y:tt)*)) => {
        // "no rules expected the token `;`"
        __iota_impl!($semi);
    };

    // ERROR: Unexpected const, probably due to a missing semicolon.
    //
    //    const A: u8 = 1 << iota
    //    const B: u8 = 0;
    (($v:expr) ($($seen:tt)*) $vis:tt const $n:ident : $t:ty = (const $($x:tt)*) ($cons:tt $($y:tt)*)) => {
        // "no rules expected the token `const`"
        __iota_impl!($cons);
    };

    // ERROR: Missing final semicolon.
    //
    //    const A: u8 = 1 << iota
    (($v:expr) ($($seen:tt)*) $vis:tt const $n:ident : $t:ty = () $y:tt) => {
        // "unexpected end of macro invocation"
        __iota_impl!();
    };

    // OK: Emit a const and reuse the same expression for the next one.
    //
    //    const A: u8 = 1 << iota;
    //        | B
    (($v:expr) ($($seen:tt)+) ($($vis:tt)*) const $n:ident : $t:ty = (; | $i:ident $($rest:tt)*) $y:tt) => {
        $($vis)* const $n : $t = __iota_replace!(($v) (()) $($seen)+);
        __iota_impl!(($v + 1) ($($seen)+) ($($vis)*) const $i : $t = (; $($rest)*) (; $($rest)*));
    };

    // OK: Emit a const and use a different expression for the next one, if any.
    (($v:expr) ($($seen:tt)+) ($($vis:tt)*) const $n:ident : $t:ty = (; $($rest:tt)*) $y:tt) => {
        $($vis)* const $n : $t = __iota_replace!(($v) (()) $($seen)+);
        __iota_dup!(($v + 1) $($rest)*);
    };

    // OK: Munch a token into the expression for the current const.
    (($v:expr) ($($seen:tt)*) $vis:tt const $n:ident : $t:ty = ($first:tt $($rest:tt)*) $y:tt) => {
        __iota_impl!(($v) ($($seen)* $first) $vis const $n : $t = ($($rest)*) ($($rest)*));
    };

    // DONE.
    (($v:expr) ()) => {};
}

#[macro_export]
#[doc(hidden)]
macro_rules! __iota_replace {
    // Open parenthesis.
    (($v:expr) ($($stack:tt)*) ($($first:tt)*) $($rest:tt)*) => {
        __iota_replace!(($v) (() $($stack)*) $($first)* __iota_close_paren $($rest)*)
    };

    // Open square bracket.
    (($v:expr) ($($stack:tt)*) [$($first:tt)*] $($rest:tt)*) => {
        __iota_replace!(($v) (() $($stack)*) $($first)* __iota_close_bracket $($rest)*)
    };

    // Open curly brace.
    (($v:expr) ($($stack:tt)*) {$($first:tt)*} $($rest:tt)*) => {
        __iota_replace!(($v) (() $($stack)*) $($first)* __iota_close_brace $($rest)*)
    };

    // Close parenthesis.
    (($v:expr) (($($close:tt)*) ($($top:tt)*) $($stack:tt)*) __iota_close_paren $($rest:tt)*) => {
        __iota_replace!(($v) (($($top)* ($($close)*)) $($stack)*) $($rest)*)
    };

    // Close square bracket.
    (($v:expr) (($($close:tt)*) ($($top:tt)*) $($stack:tt)*) __iota_close_bracket $($rest:tt)*) => {
        __iota_replace!(($v) (($($top)* [$($close)*]) $($stack)*) $($rest)*)
    };

    // Close curly brace.
    (($v:expr) (($($close:tt)*) ($($top:tt)*) $($stack:tt)*) __iota_close_brace $($rest:tt)*) => {
        __iota_replace!(($v) (($($top)* {$($close)*}) $($stack)*) $($rest)*)
    };

    // Replace `iota` token with the intended expression.
    (($v:expr) (($($top:tt)*) $($stack:tt)*) iota $($rest:tt)*) => {
        __iota_replace!(($v) (($($top)* $v) $($stack)*) $($rest)*)
    };

    // Munch a token that is not `iota`.
    (($v:expr) (($($top:tt)*) $($stack:tt)*) $first:tt $($rest:tt)*) => {
        __iota_replace!(($v) (($($top)* $first) $($stack)*) $($rest)*)
    };

    // Done.
    (($v:expr) (($($top:tt)+))) => {
        $($top)+
    };
}

#[test]
fn test_iota() {
    iota! {
        const A: u8 = 1 << iota;
            | B
            | C
        const D: usize = 3;
        const E: i64 = iota * 2;
            | F
    }
    assert_eq!(A, 1);
    assert_eq!(B, 2);
    assert_eq!(C, 4);
    assert_eq!(D, 3);
    assert_eq!(E, 8);
    assert_eq!(F, 10);
}

#[test]
fn test_delimiters() {
    const S: [u8; 3] = [4, 5, 6];
    iota! {
        const A: u8 = S[iota];
        const B: (u8, u8) = (1 << iota, (1 << iota) - 1);
        const C: u8 = { const X: u8 = iota; X * 2 };
    }
    assert_eq!(A, 4);
    assert_eq!(B, (2, 1));
    assert_eq!(C, 4);
}