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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! `bit_seq` provides procedural macros for generating bit sequences.
//!
//! # Overview
//!
//! This crate provides the macro [`bseq!`](bseq!), which allows for the creation of bit sequences
//! using a simple and intuitive syntax. Bit sequences can be created from raw binary values,
//! hexadecimal values, or even variable expressions. This makes the `bit_seq` crate a useful tool for
//! systems programming, hardware interfacing, or any application where bit manipulation is common.
//!
//! `bit_seq` also provides [`bseq_8!`](bseq_8!), [`bseq_16!`](bseq_16!), [`bseq_32!`](bseq_32!), [`bseq_64!`](bseq_64!) and [`bseq_128!`](bseq_128!) to
//! simply type mixing.
//!
//! # Examples
//!
//! The following examples illustrate some of the ways `bseq!` can be used.
//!
//! ## Raw Bit Sequences
//!
//! ```
//! use bit_seq::bseq;
//!
//! let t = bseq!(0110 01 0 1);
//! assert_eq!(t, 0b0110_01_0_1);
//! ```
//!
//! ## Hex Values
//!
//! Hexadecimal values are interpreted as 4-bit sequences.
//!
//! ```
//! use bit_seq::bseq;
//!
//! let t = bseq!(01 0x1f);
//! assert_eq!(t, 0b01_0001_1111);
//! ```
//!
//! ## Length Expressions
//!
//! Length expressions take the form `<val>:<len>`, where `<len>` is the number of bits from `<val>` to be used.
//!
//! ```
//! use bit_seq::bseq;
//!
//! let t = bseq!(3:1 0 0xf:2);
//! assert_eq!(t, 0b1_0_11);
//! ```
//!
//! ## Variable Interpolation
//!
//! Variable interpolation is supported for length expressions.
//!
//! ```
//! use bit_seq::bseq;
//! let var = 0xf;
//! let t = bseq!(10 var:2);
//! assert_eq!(t, 0b10_11);
//! ```
//!
//! ## Unary Operations
//!
//! The bseq syntax supports unary operations for length expressions. This simplifies bit sequences like
//! `0b111111`.
//!
//! ```
//! use bit_seq::bseq;
//! // bit negation
//! assert_eq!(bseq!(!0:6), 0b111111);
//!
//! // numerical negation with variable interpolation
//! let var = 1;
//! assert_eq!(bseq!(-var:8), 0xff);
//! ```
//!
//! # Performance
//!
//! The `bseq!` macro compiles down to standard bit manipulation operations, meaning there is no runtime overhead to using it.
use TokenStream;
use *;
use ;
use ;
use TokenStream2;
use Spanned;
use crate;
/// `bseq` is a procedural macro for creating bit sequences.
///
/// This macro enables the generation of bit sequences using a simple syntax.
/// Bit sequences can be specified directly, through hex values, or by using identifiers or integers
/// each with a specific length. This proves especially useful in systems programming and when interacting
/// with low-level hardware or protocols where bit manipulation is a common requirement.
///
/// # Examples
///
/// #### Direct raw bit sequence:
/// ```
/// use bit_seq::bseq;
///
/// let t = bseq!(0110 01 0 1);
/// assert_eq!(t, 0b0110_01_0_1);
/// ```
///
/// #### Using hex values:
///
/// Hex values conveniently add 4 bits per hexadecimal place.
/// ```
/// use bit_seq::bseq;
///
/// let t = bseq!(01 0x1f);
/// assert_eq!(t, 0b01_0001_1111);
/// ```
///
/// #### Using value length expression:
///
/// Employ the format `<val>:<len>` where `len` specifies how many of the
/// least significant bits from `val` should be used.
/// ```
/// use bit_seq::bseq;
///
/// let t = bseq!(3:1 0 0xf:2);
/// assert_eq!(t, 0b1_0_11);
/// ```
///
/// #### Using variable length expression:
///
/// It is also possible to interpolate outer variables for length expressions.
/// ```
/// use bit_seq::bseq;
/// let var = 0xf;
/// let t = bseq!(10 var:2);
/// assert_eq!(t, 0b10_11);
/// ```
///
/// ## Unary Operations
///
/// The bseq syntax supports unary operations for length expressions. This simplifies bit sequences like
/// `0b111111`.
///
/// ```
/// use bit_seq::bseq;
/// // bit negation
/// assert_eq!(bseq!(!0:6), 0b111111);
///
/// // numerical negation with variable interpolation
/// let var = 1;
/// assert_eq!(bseq!(-var:8), 0xff);
/// ```
///
/// Note: Since the macros are compiled into common bit manipulation operations,
/// the usage of this macro doesn't introduce additional runtime overhead.
///
/// The macro outputs a numerical value with the appropriate bits set, providing an
/// efficient method to generate specific bit sequences.
/// The `bseq_8` procedural macro is specifically tailored for creating 8-bit sequences.
///
/// It is primarily utilized when there's a need to accommodate variable types different from those
/// provided by the macro or when working with variable-length expressions involving mixed types.
///
/// For instance, the following example would fail to compile due to a type mismatch:
/// ```compile_fail
/// use bit_seq::bseq;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u8 = bseq!(foo:5 bar:3);
/// ```
///
/// The `bseq_8` macro addresses such scenarios, as demonstrated below:
/// ```
/// use bit_seq::bseq_8;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u8 = bseq_8!(foo:5 bar:3);
/// ```
///
/// It is important to note that `bseq_8` effectively performs as `bseq!(...)`, albeit with intermediate type casts.
/// For a comprehensive understanding on the usage of `bseq_8`, please refer to the [`bseq!`](bseq!) documentation.
/// The `bseq_16` procedural macro is specifically tailored for creating 16-bit sequences.
///
/// It is primarily utilized when there's a need to accommodate variable types different from those
/// provided by the macro or when working with variable-length expressions involving mixed types.
///
/// For instance, the following example would fail to compile due to a type mismatch:
/// ```compile_fail
/// use bit_seq::bseq;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u16 = bseq!(foo:5 bar:11);
/// ```
///
/// The `bseq_16` macro addresses such scenarios, as demonstrated below:
/// ```
/// use bit_seq::bseq_16;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u16 = bseq_16!(foo:5 bar:11);
/// ```
///
/// It is important to note that `bseq_16` effectively performs as `bseq!(...)`, albeit with intermediate type casts.
/// For a comprehensive understanding on the usage of `bseq_16`, please refer to the [`bseq!`](bseq!) documentation.
/// The `bseq_32` procedural macro is specifically tailored for creating 32-bit sequences.
///
/// It is primarily utilized when there's a need to accommodate variable types different from those
/// provided by the macro or when working with variable-length expressions involving mixed types.
///
/// For instance, the following example would fail to compile due to a type mismatch:
/// ```compile_fail
/// use bit_seq::bseq;
/// let foo: u8 = 4;
/// let bar: u64 = 2;
/// let t: u32 = bseq!(foo:5 bar:27);
/// ```
///
/// The `bseq_32` macro addresses such scenarios, as demonstrated below:
/// ```
/// use bit_seq::bseq_32;
/// let foo: u8 = 4;
/// let bar: u64 = 2;
/// let t: u32 = bseq_32!(foo:5 bar:27);
/// ```
///
/// It is important to note that `bseq_32` effectively performs as `bseq!(...)`, albeit with intermediate type casts.
/// For a comprehensive understanding on the usage of `bseq_32`, please refer to the [`bseq!`](bseq!) documentation.
/// The `bseq_64` procedural macro is designed for creating 64-bit sequences.
///
/// It is primarily utilized when dealing with variable types that are different from those
/// provided by the macro or when working with variable-length expressions that involve mixed types.
///
/// The following example won't compile due to a type mismatch:
/// ```compile_fail
/// use bit_seq::bseq;
/// let foo: u32 = 4;
/// let bar: u16 = 2;
/// let t: u64 = bseq!(foo:5 bar:59);
/// ```
///
/// The `bseq_64` macro provides a solution for such cases:
/// ```
/// use bit_seq::bseq_64;
/// let foo: u32 = 4;
/// let bar: u16 = 2;
/// let t: u64 = bseq_64!(foo:5 bar:59);
/// ```
///
/// Note that `bseq_64` is essentially `bseq!(...)` with intermediate type casts. For details on how to use `bseq_64`,
/// please refer to the [`bseq!`](bseq!) documentation.
/// The `bseq_128` procedural macro is designed for creating 128-bit sequences.
///
/// It is primarily utilized when dealing with variable types that are different from those
/// provided by the macro or when working with variable-length expressions that involve mixed types.
///
/// The following example won't compile due to a type mismatch:
/// ```compile_fail
/// use bit_seq::bseq;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u128 = bseq!(foo:5 bar:59);
/// ```
///
/// The `bseq_128` macro provides a solution for such cases:
/// ```
/// use bit_seq::bseq_128;
/// let foo: u32 = 4;
/// let bar: u64 = 2;
/// let t: u128 = bseq_128!(foo:5 bar:59);
/// ```
///
/// Note that `bseq_128` is essentially `bseq!(...)` with intermediate type casts. For details on how to use `bseq_128`,
/// please refer to the [`bseq!`](bseq!) documentation.
/// Processes the `bseq` input stream with a specified variable type.
///
/// `bseq!` has variable type None \
/// `bseq8!` has variable type Option<Type<u8>> \
/// ...