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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#![allow(unused)]

// Note(Lokathor): this extern crate is necessary even in 2018 for whatever
// reason that I'm sure is stupid.
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::{Group, Punct, Spacing, Span, TokenTree};
use quote::quote;
use std::collections::HashSet;
use syn::{
  parse::{Parse, ParseStream, Result},
  parse_macro_input, Expr, Ident, Token, Type,
};

struct BoolBits {
  wrapped_type: Type,
  entries: Vec<(u8, String)>,
}

impl Parse for BoolBits {
  fn parse(input: ParseStream) -> Result<Self> {
    let mut entries = vec![];
    //
    let wrapped_type: Type = input.parse()?;
    input.parse::<Token![,]>()?;
    let bit_entry_list: Group = input.parse()?;
    for tree in bit_entry_list.stream() {
      match tree {
        TokenTree::Group(bit_entry) => {
          let mut bit_entry_iter = bit_entry.stream().into_iter();
          let position: u8 = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Literal(l) => match format!("{}", l).parse::<u8>() {
                Ok(u) => u,
                Err(e) => {
                  return Err(syn::Error::new(l.span(), format!("Required u8 literal, got {}", l)));
                }
              },
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected bit_position, got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected bit_position, got Ident {}", i)));
              }
              TokenTree::Punct(p) => {
                return Err(syn::Error::new(p.span(), format!("Expected bit_position, got Punct {}", p)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected Group (bit_position, name), got an empty Group"),
            ));
          };
          let _comma: char = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Punct(p) => {
                if p.as_char() == ',' {
                  ','
                } else {
                  return Err(syn::Error::new(p.span(), format!("Expected Punct ',', got Punct {}", p)));
                }
              }
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Punct ',', got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Punct ',', got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected Punct ',', got Ident {}", i)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected Group (bit_position, name), got a 1 item Group"),
            ));
          };
          let name: String = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Ident(i) => format!("{}", i),
              TokenTree::Punct(p) => {
                return Err(syn::Error::new(p.span(), format!("Expected Ident, got Punct {}", p)));
              }
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Ident, got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Ident, got Group {}", g)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected Group (bit_position, name), got a 2 item Group"),
            ));
          };
          entries.push((position, name));
        }
        TokenTree::Punct(p) => {
          // we just eat as many commas as we see, it's probably not important
          // that they alternate the captured groups
          if p.as_char() == ',' {
            continue;
          } else {
            return Err(syn::Error::new(p.span(), format!("Expected (bit_position, name), got Punct {}", p)));
          }
        }
        TokenTree::Ident(i) => return Err(syn::Error::new(i.span(), format!("Expected (bit_position, name), got Ident {}", i))),
        TokenTree::Literal(l) => return Err(syn::Error::new(l.span(), format!("Expected (bit_position, name), got Literal {}", l))),
      }
    }
    Ok(BoolBits { wrapped_type, entries })
  }
}

/// Generates one or more virtual bool fields within a type.
///
/// You must use this within an `impl` block for a tuple struct that has an
/// integral type as the 0th field.
///
/// Give the type of `self.0`, a comma, and then a Group, which itself contains
/// comma separated Group values, each of which should be formatted as `(bit,
/// field_name)`, no more than one per bit. Something like this:
///
/// ```txt
/// impl Demo {
///   bool_bits!(
///     u16,
///     [
///       (3, cgb_mode),
///       (4, page1_enabled),
///       (5, hblank_interval_free),
///       (6, object_memory_1d),
///       (7, force_blank),
///       (8, display_bg0),
///       (9, display_bg1)
///     ]
///   );
/// }
/// ```
#[proc_macro]
pub fn bool_bits(input: TokenStream) -> TokenStream {
  let BoolBits { wrapped_type, entries } = parse_macro_input!(input as BoolBits);

  let mut bits: HashSet<u8> = HashSet::new();
  for entry in entries.iter() {
    let bit = entry.0;
    if bits.contains(&bit) {
      panic!("Can't list a bit more than once! Found repeats of {}", bit);
    } else {
      bits.insert(bit);
    }
  }

  let mut output = TokenStream::new();
  for (bit, name) in entries.into_iter() {
    let const_name = Ident::new(&name.to_uppercase(), Span::call_site());
    let read_name = Ident::new(&name.clone(), Span::call_site());
    let with_name = Ident::new(&format!("with_{}", name), Span::call_site());
    let more_tokens: TokenStream = TokenStream::from(quote! {

      #[allow(missing_docs)]
      #[allow(clippy::identity_op)]
      pub const #const_name: #wrapped_type = 1 << #bit;

      #[allow(missing_docs)]
      pub const fn #read_name(self) -> bool {
        (self.0 & Self::#const_name) != 0
      }

      // https://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching
      #[allow(missing_docs)]
      pub const fn #with_name(self, bit: bool) -> Self {
        Self(self.0 ^ (((#wrapped_type::wrapping_sub(0, bit as #wrapped_type) ^ self.0) & Self::#const_name)))
      }

    });
    output.extend(more_tokens);
  }
  output
}

//
// Multi Bit Fields
//

struct WrappedEntry {
  base: u8,
  width: u8,
  name: String,
}

struct EnumEntry {
  base: u8,
  width: u8,
  name: String,
  enum_type: String,
  variant_list: Vec<String>,
}

enum MultiBitEntry {
  Wrapped(WrappedEntry),
  Enum(EnumEntry),
}

struct MultiBitFields {
  wrapped_type: Type,
  entries: Vec<MultiBitEntry>,
}

impl Parse for MultiBitFields {
  fn parse(input: ParseStream) -> Result<Self> {
    let mut entries = vec![];
    //
    let wrapped_type: Type = input.parse()?;
    input.parse::<Token![,]>()?;
    let bit_entry_list: Group = input.parse()?;
    for tree in bit_entry_list.stream() {
      match tree {
        TokenTree::Group(bit_entry) => {
          let mut bit_entry_iter = bit_entry.stream().into_iter();
          let base: u8 = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Literal(l) => match format!("{}", l).parse::<u8>() {
                Ok(u) => u,
                Err(e) => {
                  return Err(syn::Error::new(l.span(), format!("Required u8 literal, got {}", l)));
                }
              },
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected base, got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected base, got Ident {}", i)));
              }
              TokenTree::Punct(p) => {
                return Err(syn::Error::new(p.span(), format!("Expected base, got Punct {}", p)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected multi-bit spec (min of 3), got an empty Group"),
            ));
          };
          let _comma: char = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Punct(p) => {
                if p.as_char() == ',' {
                  ','
                } else {
                  return Err(syn::Error::new(p.span(), format!("Expected Punct ',', got Punct {}", p)));
                }
              }
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Punct ',', got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Punct ',', got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected Punct ',', got Ident {}", i)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected multi-bit spec (min of 3), got a 1 item Group"),
            ));
          };
          let width: u8 = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Literal(l) => match format!("{}", l).parse::<u8>() {
                Ok(u) => u,
                Err(e) => {
                  return Err(syn::Error::new(l.span(), format!("Required u8 literal, got {}", l)));
                }
              },
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected width, got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected width, got Ident {}", i)));
              }
              TokenTree::Punct(p) => {
                return Err(syn::Error::new(p.span(), format!("Expected width, got Punct {}", p)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected multi-bit spec (min of 3), got a 1 item Group"),
            ));
          };
          let _comma: char = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Punct(p) => {
                if p.as_char() == ',' {
                  ','
                } else {
                  return Err(syn::Error::new(p.span(), format!("Expected Punct ',', got Punct {}", p)));
                }
              }
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Punct ',', got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Punct ',', got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected Punct ',', got Ident {}", i)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected multi-bit spec (min of 3), got a 2 item Group"),
            ));
          };
          let name: String = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Ident(i) => format!("{}", i),
              TokenTree::Punct(p) => {
                return Err(syn::Error::new(p.span(), format!("Expected Ident, got Punct {}", p)));
              }
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Ident, got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Ident, got Group {}", g)));
              }
            }
          } else {
            return Err(syn::Error::new(
              bit_entry.span(),
              format!("Expected multi-bit spec (min of 3), got a 2 item Group"),
            ));
          };
          let enum_comma: bool = if let Some(tt) = bit_entry_iter.next() {
            match tt {
              TokenTree::Punct(p) => true,
              TokenTree::Literal(l) => {
                return Err(syn::Error::new(l.span(), format!("Expected Punct ',' or End, got Literal {}", l)));
              }
              TokenTree::Group(g) => {
                return Err(syn::Error::new(g.span(), format!("Expected Punct ',' or End, got Group {}", g)));
              }
              TokenTree::Ident(i) => {
                return Err(syn::Error::new(i.span(), format!("Expected Punct ',' or End, got Ident {}", i)));
              }
            }
          } else {
            false
          };
          if enum_comma {
            // read the enum
            let enum_type: String = if let Some(tt) = bit_entry_iter.next() {
              match tt {
                TokenTree::Ident(i) => format!("{}", i),
                TokenTree::Punct(p) => {
                  return Err(syn::Error::new(p.span(), format!("Expected Ident, got Punct {}", p)));
                }
                TokenTree::Literal(l) => {
                  return Err(syn::Error::new(l.span(), format!("Expected Ident, got Literal {}", l)));
                }
                TokenTree::Group(g) => {
                  return Err(syn::Error::new(g.span(), format!("Expected Ident, got Group {}", g)));
                }
              }
            } else {
              return Err(syn::Error::new(
                bit_entry.span(),
                format!("Expected enum type name after trailing comma, found none"),
              ));
            };
            // begin reading accepted variants, in order
            let mut variant_list = vec![];
            'tag_gather_loop: loop {
              let _comma: char = if let Some(tt) = bit_entry_iter.next() {
                match tt {
                  TokenTree::Punct(p) => {
                    if p.as_char() == ',' {
                      ','
                    } else {
                      return Err(syn::Error::new(p.span(), format!("Expected Punct ',', got Punct {}", p)));
                    }
                  }
                  TokenTree::Literal(l) => {
                    return Err(syn::Error::new(l.span(), format!("Expected Punct ',', got Literal {}", l)));
                  }
                  TokenTree::Group(g) => {
                    return Err(syn::Error::new(g.span(), format!("Expected Punct ',', got Group {}", g)));
                  }
                  TokenTree::Ident(i) => {
                    return Err(syn::Error::new(i.span(), format!("Expected Punct ',', got Ident {}", i)));
                  }
                }
              } else {
                break 'tag_gather_loop;
              };
              let enum_tag: String = if let Some(tt) = bit_entry_iter.next() {
                match tt {
                  TokenTree::Ident(i) => format!("{}", i),
                  TokenTree::Punct(p) => {
                    return Err(syn::Error::new(p.span(), format!("Expected Ident, got Punct {}", p)));
                  }
                  TokenTree::Literal(l) => {
                    return Err(syn::Error::new(l.span(), format!("Expected Ident, got Literal {}", l)));
                  }
                  TokenTree::Group(g) => {
                    return Err(syn::Error::new(g.span(), format!("Expected Ident, got Group {}", g)));
                  }
                }
              } else {
                return Err(syn::Error::new(bit_entry.span(), format!("Expected enum tag after trailing comma")));
              };
              variant_list.push(enum_tag);
            }
            if variant_list.len() > 0 {
              entries.push(MultiBitEntry::Enum(EnumEntry {
                base,
                width,
                name,
                enum_type,
                variant_list,
              }));
            } else {
              return Err(syn::Error::new(Span::call_site(), "Enum tag list was missing!"));
            }
          } else {
            entries.push(MultiBitEntry::Wrapped(WrappedEntry { base, width, name }));
          }
        }
        TokenTree::Punct(p) => {
          // we just eat as many commas as we see, it's probably not important
          // that they alternate the captured groups
          if p.as_char() == ',' {
            continue;
          } else {
            return Err(syn::Error::new(p.span(), format!("Expected multi-bit spec or comma, got Punct {}", p)));
          }
        }
        TokenTree::Ident(i) => return Err(syn::Error::new(i.span(), format!("Expected multi-bit spec or comma, got Ident {}", i))),
        TokenTree::Literal(l) => return Err(syn::Error::new(l.span(), format!("Expected multi-bit spec or comma, got Literal {}", l))),
      }
    }
    Ok(MultiBitFields { wrapped_type, entries })
  }
}

/// Generates one or more virtual multi-bit fields within a type.
///
/// You must use this within an `impl` block for a tuple struct that has an
/// integral type as the 0th field.
///
/// * Each virtual field has a base position and width.
/// * Each virtual field is either of the 0th field integral type OR a specified
///   enum type which must have a repr that matches the 0th field type.
///
/// Specify the type of the 0th tuple field, a comma, and then a Group. The
/// Group must contain a comma separated list of Group values that each specify
/// one field.
///
/// The specification is one of:
///
/// * `(base, width, name)`
/// * `(base, width, name, EnumType, EnumTag1, EnumTagN...)`
///
/// With the Enum version, you must provide the tags in the order they should be
/// used. Even a proc-macro doesn't have the reflection required
///
/// Such as the following example:
///
/// ```txt
/// impl Demo {
///   multi_bits!(
///     u16,
///     [
///       (0, 2, priority),
///       (2, 2, cbb),
///       (8, 5, sbb),
///       (14, 2, size, SizeEnum, Small, Medium, Large),
///     ]
///   );
/// }
/// ```
#[proc_macro]
pub fn multi_bits(input: TokenStream) -> TokenStream {
  let MultiBitFields { wrapped_type, entries } = parse_macro_input!(input as MultiBitFields);

  let mut bits: HashSet<u8> = HashSet::new();
  for entry in entries.iter() {
    let (base, width): (u8, u8) = match entry {
      MultiBitEntry::Wrapped(WrappedEntry { base, width, .. }) => (*base, *width),
      MultiBitEntry::Enum(EnumEntry { base, width, .. }) => (*base, *width),
    };
    assert!(width > 0);

    for bit in base..width {
      if bits.contains(&bit) {
        panic!("Bit overlap found for bit {}", bit);
      } else {
        bits.insert(bit);
      }
    }
  }

  let mut output = TokenStream::new();
  for entry in entries.into_iter() {
    match entry {
      MultiBitEntry::Wrapped(WrappedEntry { base, width, name }) => {
        let mask_name = Ident::new(&format!("{}_MASK", name.to_uppercase()), Span::call_site());
        let read_name = Ident::new(&name.clone(), Span::call_site());
        let with_name = Ident::new(&format!("with_{}", name), Span::call_site());
        let more_tokens: TokenStream = TokenStream::from(quote! {

          #[allow(missing_docs)]
          #[allow(clippy::identity_op)]
          pub const #mask_name: #wrapped_type = ((1<<(#width))-1) << #base;

          #[allow(missing_docs)]
          pub const fn #read_name(self) -> #wrapped_type {
            (self.0 & Self::#mask_name) >> #base
          }

          #[allow(missing_docs)]
          pub const fn #with_name(self, #read_name: #wrapped_type) -> Self {
            Self((self.0 & !Self::#mask_name) | ((#read_name << #base) & Self::#mask_name))
          }

        });
        output.extend(more_tokens);
      }
      MultiBitEntry::Enum(EnumEntry {
        base,
        width,
        name,
        enum_type,
        variant_list,
      }) => {
        let mask_name = Ident::new(&format!("{}_MASK", name.to_uppercase()), Span::call_site());
        let read_name = Ident::new(&name.clone(), Span::call_site());
        let with_name = Ident::new(&format!("with_{}", name), Span::call_site());
        let enum_type_ident = Ident::new(&enum_type.clone(), Span::call_site());
        // add many, but not all, of our tokens
        let more_tokens: TokenStream = TokenStream::from(quote! {

          #[allow(missing_docs)]
          #[allow(clippy::identity_op)]
          pub const #mask_name: #wrapped_type = ((1<<(#width+1))-1) << #base;

          #[allow(missing_docs)]
          pub const fn #with_name(self, #read_name: #enum_type_ident) -> Self {
            Self((self.0 & !Self::#mask_name) | (((#read_name as #wrapped_type) << #base) & Self::#mask_name))
          }

          #[allow(missing_docs)]
          pub fn #read_name(self) -> #enum_type_ident
        });
        output.extend(more_tokens);
        // curly to enter a function, start the match
        let mut temp_string = "{".to_string();
        let tokens = TokenStream::from(quote! {

          match self.0 & Self::#mask_name

        });
        temp_string.push_str(&format!("{}", tokens));
        // curly to enter the match
        temp_string.push_str(" { ");
        // dump the match arms
        for i in 0..variant_list.len() {
          temp_string.push_str(&format!("\n{} => ", i));
          let enum_tag_ident = Ident::new(&variant_list[i].clone(), Span::call_site());
          let tokens = TokenStream::from(quote! {

            #enum_type_ident::#enum_tag_ident,

          });
          temp_string.push_str(&format!("{}", tokens));
        }
        // place the catch all arm
        if variant_list.len() == (1 << (width - 1)) {
          let tokens = TokenStream::from(quote! {

            _ => core::hint::unreachable_unchecked(),

          });
          temp_string.push_str(&format!("{}", tokens));
        } else {
          let tokens = TokenStream::from(quote! {

            _ => unreachable!(),

          });
          temp_string.push_str(&format!("{}", tokens));
        }
        // close those two unbalanced curly braces
        temp_string.push_str(" } } ");
        // push all that into the output
        use core::str::FromStr;
        let more_tokens: TokenStream = TokenStream::from_str(&temp_string).map_err(|e| panic!("{:?}", e)).unwrap();
        output.extend(more_tokens);
      }
    };
  }
  output
}