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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#![allow(dead_code)]
extern crate proc_macro;
use proc_macro::{TokenTree, Span, TokenStream, Delimiter, Group, Literal, Ident, Punct, Spacing};
use proc_macro::token_stream::IntoIter;

// little macro utility lib

pub fn error_span(err: &str, span: Span) -> TokenStream {
    let mut tb = TokenBuilder::new();
    tb.ident_with_span("compile_error", span).add("! (").string(err).add(") ;");
    tb.end()
}

pub fn error(err: &str) -> TokenStream {
    let mut tb = TokenBuilder::new();
    tb.add("compile_error ! (").string(err).add(") ;");
    tb.end()
}

pub fn error_result(err: &str) -> Result<(), TokenStream> {
    let mut tb = TokenBuilder::new();
    tb.add("compile_error ! (").string(err).add(") ;");
    Err(tb.end())
}

pub fn unwrap_option(input: TokenStream) -> Result<TokenStream, TokenStream> {
    let mut ty_parser = TokenParser::new(input.clone());
    if ty_parser.eat_ident("Option") {
        if !ty_parser.eat_punct_alone('<') {
            panic!()
        }
        Ok(ty_parser.eat_level_or_punct('>'))
    }
    else {
        Err(input)
    }
}

pub struct TokenBuilder {
    pub groups: Vec<(Delimiter, TokenStream)>
}

impl TokenBuilder {
    pub fn new() -> Self {
        Self {
            groups: vec![(Delimiter::None, TokenStream::new())]
        }
    }
    
    pub fn is_empty(&self) -> bool {
        self.groups.len() == 1 && self.groups[0].1.is_empty()
    }
    
    pub fn end(mut self) -> TokenStream {
        if self.groups.len() != 1 {
            panic!("Groups not empty, you missed a pop_group")
        }
        self.groups.pop().unwrap().1
    }
    
    pub fn eprint(&self) {
        eprintln!("{}", self.groups.last().unwrap().1.to_string());
    }
    
    pub fn extend(&mut self, tt: TokenTree) -> &mut Self {
        self.groups.last_mut().unwrap().1.extend(Some(tt));
        self
    }
    
    pub fn stream(&mut self, what: Option<TokenStream>) -> &mut Self {
        if let Some(what) = what {
            for c in what.into_iter() {
                self.extend(c);
            }
            self
        }
        else {
            self
        }
    }
    
    pub fn add(&mut self, what: &str) -> &mut Self {
        let b = what.as_bytes();
        let mut o = 0;
        while o < b.len() {
            let c0 = b[o] as char;
            let c1 = if o + 1 < b.len() {b[o + 1] as char}else {'\0'};
            match (c0, c1) {
                ('\r', _) | ('\n', _) | (' ', _) | ('\t', _) => {o += 1;}
                ('{', _) => {self.push_group(Delimiter::Brace); o += 1;},
                ('(', _) => {self.push_group(Delimiter::Parenthesis); o += 1;},
                ('[', _) => {self.push_group(Delimiter::Bracket); o += 1;},
                ('}', _) => {self.pop_group(Delimiter::Brace); o += 1;},
                (')', _) => {self.pop_group(Delimiter::Parenthesis); o += 1;},
                (']', _) => {self.pop_group(Delimiter::Bracket); o += 1;},
                ('<', '<') | ('>', '>') | ('&', '&') | ('|', '|') |
                ('-', '>') | ('=', '>') |
                ('<', '=') | ('>', '=') | ('=', '=') | ('!', '=') | (':', ':') |
                ('+', '=') | ('-', '=') | ('*', '=') | ('/', '=') | ('.', '.') => {
                    self.punct(std::str::from_utf8(&b[o..o + 2]).unwrap());
                    o += 2;
                }
                ('+', _) | ('-', _) | ('*', _) | ('/', _) | ('#', _) |
                ('=', _) | ('<', _) | ('>', _) | ('?', _) | (';', _) | ('&', _) |
                ('^', _) | (':', _) | (',', _) | ('!', _) | ('.', _) | ('|', _) => {
                    self.punct(std::str::from_utf8(&b[o..o + 1]).unwrap());
                    o += 1;
                },
                ('0', 'x') => { // this needs to be fancier but whatever.
                    let mut e = o + 2;
                    let mut out: u64 = 0;
                    while e < b.len() {
                        match b[e] {
                            b'0'..=b'9' => out = (out << 4) | (b[e] - b'0') as u64,
                            b'a'..=b'f' => out = (out << 4) | (b[e] - b'a' + 10) as u64,
                            b'A'..=b'F' => out = (out << 4) | (b[e] - b'A' + 10) as u64,
                            b'_' => (),
                            _ => break,
                        };
                        e += 1;
                    }
                    self.suf_u64(out);
                    o = e;
                }
                ('0'..='9', _) => {
                    let mut e = o + 1;
                    while e < b.len() {
                        match b[e] {
                            b'0'..=b'9' => e += 1,
                            _ => break,
                        }
                    }
                    let num = std::str::from_utf8(&b[o..e]).unwrap();
                    self.unsuf_usize(num.parse().unwrap_or_else(|_| panic!("Can't parse usize number \"{}\"", what)));
                    o = e;
                }
                ('"', _) => {
                    let mut e = o + 1;
                    while e < b.len() {
                        match b[e] {
                            b'"' => break,
                            _ => e += 1,
                        }
                    }
                    self.string(std::str::from_utf8(&b[o + 1..e]).unwrap());
                    o = e + 1;
                }
                ('\'', _) => {
                    let mut e = o + 1;
                    while e < b.len() {
                        match b[e] {
                            b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_' => e += 1,
                            _ => break,
                        }
                    }
                    if o == e {
                        panic!("Unexpected character {:?}", b[e] as char);
                    }
                    let ident = std::str::from_utf8(&b[o + 1..e]).unwrap();
                    self.lifetime_mark();
                    self.ident(ident);
                    o = e;
                }
                _ => {
                    let mut e = o;
                    while e < b.len() {
                        match b[e] {
                            b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'_' => e += 1,
                            _ => break,
                        }
                    }
                    if o == e {
                        panic!("Unexpected character {:?}", b[e] as char);
                    }
                    let ident = std::str::from_utf8(&b[o..e]).unwrap();
                    self.ident(ident);
                    o = e;
                }
            }
        }
        self
    }
    
    pub fn ident(&mut self, id: &str) -> &mut Self {
        self.extend(TokenTree::from(Ident::new(id, Span::call_site())))
    }
    
    pub fn ident_with_span(&mut self, id: &str, span: Span) -> &mut Self {
        self.extend(TokenTree::from(Ident::new(id, span)))
    }
    
    pub fn punct(&mut self, s: &str) -> &mut Self {
        for (last, c) in s.chars().identify_last() {
            self.extend(TokenTree::from(Punct::new(c, if last {Spacing::Alone} else {Spacing::Joint})));
        }
        self
    }
    
    pub fn lifetime_mark(&mut self) -> &mut Self {
        self.extend(TokenTree::from(Punct::new('\'', Spacing::Joint)));
        self
    }
    
    pub fn sep(&mut self) -> &mut Self {
        self.extend(TokenTree::from(Punct::new(':', Spacing::Joint)));
        self.extend(TokenTree::from(Punct::new(':', Spacing::Alone)));
        self
    }
    
    pub fn string(&mut self, val: &str) -> &mut Self {self.extend(TokenTree::from(Literal::string(val)))}
    pub fn unsuf_usize(&mut self, val: usize) -> &mut Self {self.extend(TokenTree::from(Literal::usize_unsuffixed(val)))}
    pub fn suf_u16(&mut self, val: u16) -> &mut Self {self.extend(TokenTree::from(Literal::u16_suffixed(val)))}
    pub fn suf_u32(&mut self, val: u32) -> &mut Self {self.extend(TokenTree::from(Literal::u32_suffixed(val)))}
    pub fn suf_u64(&mut self, val: u64) -> &mut Self {self.extend(TokenTree::from(Literal::u64_suffixed(val)))}
    pub fn unsuf_f32(&mut self, val: f32) -> &mut Self {self.extend(TokenTree::from(Literal::f32_unsuffixed(val)))}
    pub fn unsuf_f64(&mut self, val: f64) -> &mut Self {self.extend(TokenTree::from(Literal::f64_unsuffixed(val)))}
    pub fn unsuf_i64(&mut self, val: i64) -> &mut Self {self.extend(TokenTree::from(Literal::i64_unsuffixed(val)))}
    
    pub fn chr(&mut self, val: char) -> &mut Self {self.extend(TokenTree::from(Literal::character(val)))}
    pub fn _lit(&mut self, lit: Literal) -> &mut Self {self.extend(TokenTree::from(lit))}
    
    pub fn push_group(&mut self, delim: Delimiter) -> &mut Self {
        self.groups.push((delim, TokenStream::new()));
        self
    }
    
    pub fn stack_as_string(&self) -> String {
        let mut ret = String::new();
        for i in (0..self.groups.len() - 1).rev() {
            ret.push_str(&format!("Level {}: {}", i, self.groups[i].1.to_string()));
        }
        ret
    }
    
    pub fn pop_group(&mut self, delim: Delimiter) -> &mut Self {
        if self.groups.len() < 2 {
            eprintln!("Stack dump for error:\n{}", self.stack_as_string());
            panic!("pop_group stack is empty {}", self.groups.len());
        }
        let ts = self.groups.pop().unwrap();
        if ts.0 != delim {
            eprintln!("Stack dump for error:\n{}", self.stack_as_string());
            panic!("pop_group Delimiter mismatch, got {:?} expected {:?}", ts.0, delim);
        }
        self.extend(TokenTree::from(Group::new(delim, ts.1)));
        self
    }
}

impl Default for TokenBuilder {
    fn default() -> Self {
        Self::new()
    }
}

pub trait IdentifyLast: Iterator + Sized {
    fn identify_last(self) -> Iter<Self>;
}

impl<It> IdentifyLast for It where It: Iterator {
    fn identify_last(mut self) -> Iter<Self> {
        let e = self.next();
        Iter {
            iter: self,
            buffer: e,
        }
    }
}

pub struct Iter<It> where It: Iterator {
    iter: It,
    buffer: Option<It::Item>,
}

#[derive(Debug)]
pub struct Attribute {
    pub name: String,
    pub args: Option<TokenStream>
}

pub struct StructField {
    pub name: String,
    pub ty: TokenStream,
    pub attrs: Vec<Attribute>
}

impl<It> Iterator for Iter<It> where It: Iterator {
    type Item = (bool, It::Item);
    
    fn next(&mut self) -> Option<Self::Item> {
        match self.buffer.take() {
            None => None,
            Some(e) => {
                match self.iter.next() {
                    None => Some((true, e)),
                    Some(f) => {
                        self.buffer = Some(f);
                        Some((false, e))
                    },
                }
            },
        }
    }
}

pub struct TokenParser {
    iter_stack: Vec<IntoIter>,
    pub current: Option<TokenTree>
}

// this parser is optimized for parsing type definitions, not general Rust code

impl TokenParser {
    pub fn new(start: TokenStream) -> Self {
        let mut ret = Self {iter_stack: vec![start.into_iter()], current: None};
        ret.advance();
        ret
    }
    
    pub fn advance(&mut self) {
        let last = self.iter_stack.last_mut().unwrap();
        let value = last.next();
        if let Some(tok) = value {
            self.current = Some(tok);
        }
        else {
            self.current = None;
        }
        // skip over ///
        
    }
    
    pub fn unexpected(&self) -> TokenStream {
        error("Unexpected token")
    }
    
    pub fn is_delim(&mut self, delim: Delimiter) -> bool {
        if let Some(TokenTree::Group(group)) = &self.current {
            group.delimiter() == delim
        }
        else {
            false
        }
    }
    
    pub fn is_brace(&mut self) -> bool {
        self.is_delim(Delimiter::Brace)
    }
    
    pub fn is_paren(&mut self) -> bool {
        self.is_delim(Delimiter::Parenthesis)
    }
    
    pub fn is_bracket(&mut self) -> bool {
        self.is_delim(Delimiter::Bracket)
    }
    
    pub fn open_delim(&mut self, delim: Delimiter) -> bool {
        if let Some(TokenTree::Group(group)) = &self.current {
            if group.delimiter() == delim {
                self.iter_stack.push(group.stream().into_iter());
                self.advance();
                return true
            }
        }
        false
    }
    
    pub fn is_group_with_delim(&mut self, delim: Delimiter) -> bool {
        if let Some(TokenTree::Group(group)) = &self.current {
            delim == group.delimiter()
        }
        else {
            false
        }
    }
    
    pub fn open_group(&mut self) -> Option<Delimiter> {
        if let Some(TokenTree::Group(group)) = &self.current {
            let delim = group.delimiter();
            self.iter_stack.push(group.stream().into_iter());
            self.advance();
            return Some(delim)
        }
        None
    }
    
    pub fn open_brace(&mut self) -> bool {
        self.open_delim(Delimiter::Brace)
    }
    
    pub fn open_paren(&mut self) -> bool {
        self.open_delim(Delimiter::Parenthesis)
    }
    
    pub fn open_bracket(&mut self) -> bool {
        self.open_delim(Delimiter::Bracket)
    }
    
    pub fn is_eot(&mut self) -> bool {
        self.current.is_none() && !self.iter_stack.is_empty()
    }
    
    pub fn eat_eot(&mut self) -> bool {
        // current is None
        if self.is_eot() {
            self.iter_stack.pop();
            if !self.iter_stack.is_empty() {
                self.advance()
            }
            return true;
        }
        false
    }
    
    pub fn eat_level(&mut self) -> TokenStream {
        let mut tb = TokenBuilder::new();
        while !self.eat_eot() {
            tb.extend(self.current.clone().unwrap());
            self.advance();
        }
        tb.end()
    }
    
    pub fn eat_level_or_punct(&mut self, what: char) -> TokenStream {
        let mut tb = TokenBuilder::new();
        while !self.eat_eot() {
            if self.is_punct_alone(what) {
                self.advance();
                return tb.end();
            }
            tb.extend(self.current.clone().unwrap());
            self.advance();
        }
        tb.end()
    }
    
    pub fn eat_ident(&mut self, what: &str) -> bool {
        // check if our current thing is an ident, ifso eat it.
        if let Some(TokenTree::Ident(ident)) = &self.current {
            if ident.to_string() == what {
                self.advance();
                return true
            }
        }
        false
    }
    
    pub fn is_literal(&mut self) -> bool {
        // check if our current thing is an ident, ifso eat it.
        if let Some(TokenTree::Literal(_)) = &self.current {
            return true
        }
        false
    }
    
    pub fn eat_literal(&mut self) -> Option<Literal> {
        // check if our current thing is an ident, ifso eat it.
        if let Some(TokenTree::Literal(lit)) = &self.current {
            let ret = Some(lit.clone());
            self.advance();
            return ret
        }
        None
    }
    
    pub fn span(&self) -> Option<Span> {
        self.current.as_ref().map(|current| current.span())
    }
    
    pub fn is_punct_alone(&mut self, what: char) -> bool {
        // check if our punct is multichar.
        if let Some(TokenTree::Punct(current)) = &self.current {
            if current.as_char() == what && (current.as_char() == '>' || current.spacing() == Spacing::Alone) {
                return true
            }
        }
        false
    }
    
    pub fn is_punct_any(&mut self, what: char) -> bool {
        // check if our punct is multichar.
        if let Some(TokenTree::Punct(current)) = &self.current {
            if current.as_char() == what {
                return true
            }
        }
        false
    }
    
    
    pub fn eat_double_colon_destruct(&mut self) -> bool {
        // check if our punct is multichar.
        if let Some(TokenTree::Punct(current)) = &self.current {
            if current.as_char() == ':' && current.spacing() == Spacing::Joint {
                self.advance();
                if let Some(TokenTree::Punct(current)) = &self.current {
                    if current.as_char() == ':' && current.spacing() == Spacing::Alone {
                        self.advance();
                        return true
                    }
                }
            }
        }
        false
    }
    
    pub fn eat_sep(&mut self) -> bool {
        // check if our punct is multichar.
        if let Some(TokenTree::Punct(current)) = &self.current {
            if current.as_char() == ':' && current.spacing() == Spacing::Joint {
                self.advance();
                if let Some(TokenTree::Punct(current)) = &self.current {
                    if current.as_char() == ':' && current.spacing() == Spacing::Alone {
                        self.advance();
                        return true
                    }
                }
            }
        }
        false
    }
    
    pub fn eat_punct_alone(&mut self, what: char) -> bool {
        if self.is_punct_alone(what) {
            self.advance();
            return true
        }
        false
    }
    
    pub fn eat_punct_any(&mut self, what: char) -> bool {
        if self.is_punct_any(what) {
            self.advance();
            return true
        }
        false
    }
    
    pub fn eat_any_punct(&mut self) -> Option<String> {
        let mut out = String::new();
        while let Some(TokenTree::Punct(current)) = &self.current {
            out.push(current.as_char());
            if current.spacing() == Spacing::Alone {
                self.advance();
                return Some(out);
            }
            self.advance();
        }
        None
    }
    
    pub fn eat_any_ident(&mut self) -> Option<String> {
        if let Some(TokenTree::Ident(ident)) = &self.current {
            let ret = Some(ident.to_string());
            self.advance();
            return ret
        }
        None
    }
    
    pub fn eat_any_ident_with_span(&mut self) -> Option<(String, Span)> {
        if let Some(TokenTree::Ident(ident)) = &self.current {
            let ret = Some((ident.to_string(), self.span().unwrap()));
            self.advance();
            return ret
        }
        None
    }
    
    pub fn expect_any_ident(&mut self) -> Result<String, TokenStream> {
        if let Some(TokenTree::Ident(ident)) = &self.current {
            let ret = ident.to_string();
            self.advance();
            return Ok(ret)
        }
        Err(error("Expected any ident"))
    }
    
    
    pub fn expect_punct_alone(&mut self, what: char) -> Result<(), TokenStream> {
        if self.is_punct_alone(what) {
            self.advance();
            Ok(())
        }
        else {
            Err(error(&format!("Expected punct {}", what)))
        }
    }
    
    pub fn expect_punct_any(&mut self, what: char) -> Result<(), TokenStream> {
        if self.is_punct_any(what) {
            self.advance();
            Ok(())
        }
        else {
            Err(error(&format!("Expected punct {}", what)))
        }
    }
    
    pub fn eat_ident_path(&mut self) -> Option<TokenStream> {
        let mut tb = TokenBuilder::new();
        while let Some(ident) = self.eat_any_ident() {
            tb.ident(&ident);
            if !self.eat_sep() {
                break
            }
            tb.sep();
        }

        let ts = tb.end();
        if !ts.is_empty() {
            return Some(ts)
        }
        None
    }
    
    pub fn eat_where_clause(&mut self, add_where: Option<&str>) -> Option<TokenStream> {
        let mut tb = TokenBuilder::new();
        if self.eat_ident("where") {
            tb.add("where");
            // ok now we parse an ident
            loop {
                if let Some(ident) = self.eat_any_ident() {
                    tb.ident(&ident);
                    tb.stream(self.eat_generic());
                    
                    if !self.eat_punct_alone(':') {
                        return None
                    }
                    tb.add(":");
                    loop {
                        if let Some(ident) = self.eat_any_ident() {
                            tb.add(&ident);
                            tb.stream(self.eat_generic());
                            // check if we have upnext
                            // {, + or ,
                            if self.eat_punct_alone('+') {
                                tb.add("+");
                                continue
                            }
                            if self.eat_punct_alone(',') { // next one
                                if let Some(add_where) = add_where {
                                    tb.add("+");
                                    tb.ident(add_where);
                                }
                                tb.add(",");
                                break
                            }
                            if self.is_brace() || self.is_punct_alone(';') { // upnext is a brace.. we're done
                                if let Some(add_where) = add_where {
                                    tb.add("+");
                                    tb.ident(add_where);
                                }
                                return Some(tb.end())
                            }
                        }
                        else {
                            return None // unexpected
                        }
                    }
                }
                else {
                    return None // unexpected
                }
            }
        }
        None
    }
    
    pub fn eat_struct_field(&mut self) -> Option<StructField> {
        // letsparse an ident
        let attrs = self.eat_attributes();
        
        self.eat_ident("pub");
        if let Some(field) = self.eat_any_ident() {
            if self.eat_punct_alone(':') {
                if let Some(ty) = self.eat_type() {
                    return Some(StructField {name: field, ty, attrs})
                }
            }
        }
        None
    }
    
    pub fn eat_attributes(&mut self) -> Vec<Attribute> {
        let mut results = Vec::new();
        while self.eat_punct_alone('#') { // parse our attribute
            if !self.open_bracket() {
                break;
            }
            let mut assign_form = false;
            while let Some(ident) = self.eat_any_ident() {
                // we might have an =
                if self.eat_punct_alone('=') {
                    let level = self.eat_level();
                    results.push(Attribute {name: ident, args: Some(level)});
                    //eprintln!("{} {}", results.last().unwrap().name, results.last().as_ref().unwrap().args.as_ref().unwrap().to_string());
                    assign_form = true;
                    break;
                }
                if !self.open_paren() && !self.open_brace() {
                    results.push(Attribute {name: ident, args: None});
                    break;
                }
                // lets take the whole ts
                results.push(Attribute {name: ident, args: Some(self.eat_level())});
                self.eat_punct_alone(',');
            }
            if !assign_form && !self.eat_eot() {
                break
            }
        }
        results
    }
    
    pub fn eat_all_struct_fields(&mut self,) -> Option<Vec<StructField >> {
        
        if self.open_brace() {
            let mut fields = Vec::new();
            while !self.eat_eot() {
                // lets eat an attrib
                if let Some(sf) = self.eat_struct_field() {
                    fields.push(sf);
                    self.eat_punct_alone(',');
                }
                else {
                    return None
                }
            }
            return Some(fields)
        }
        None
    }
    
    
    pub fn eat_generic(&mut self) -> Option<TokenStream> {
        let mut tb = TokenBuilder::new();
        // if we have a <, keep running and keep a < stack
        
        if self.eat_punct_alone('<') {
            tb.add("<");
            let mut stack = 1;
            // keep eating things till we are at stack 0 for a ">"
            while stack > 0 {
                if self.eat_punct_alone('<') {
                    tb.add("<");
                    stack += 1;
                }
                if self.eat_punct_alone('>') {
                    tb.add(">");
                    stack -= 1;
                }
                else if self.eat_eot() { // shits broken
                    return None
                }
                else { // store info here in generics struct
                    if let Some(current) = &self.current {
                        tb.extend(current.clone());
                    }
                    self.advance();
                }
            }
            
            return Some(tb.end())
        }
        None
    }
    
    pub fn eat_all_types(&mut self) -> Option<Vec<TokenStream >> {
        if self.open_paren() {
            let mut ret = Vec::new();
            while !self.eat_eot() {
                self.eat_ident("pub");
                if let Some(tt) = self.eat_type() {
                    ret.push(tt);
                    self.eat_punct_alone(',');
                }
                else {
                    return None
                }
            }
            Some(ret)
        }
        else {
            None
        }
    }
    
    pub fn eat_type(&mut self) -> Option<TokenStream> {
        let mut tb = TokenBuilder::new();
        if self.eat_punct_alone('&'){
            tb.add("&");
            if self.eat_punct_any('\''){
                tb.lifetime_mark();
                if let Some((ty, span)) = self.eat_any_ident_with_span() {
                    tb.ident_with_span(&ty, span);
                }
            }
        }
        if self.open_bracket() { // array type
            tb.add("[");
            while !self.eat_eot() {
                if let Some(current) = &self.current {
                    tb.extend(current.clone());
                }
                self.advance();
            }
            tb.add("]");
            return Some(tb.end())
        }
        else if self.open_paren() { // tuple type
            tb.add("(");
            while !self.eat_eot() {
                tb.stream(self.eat_type());
                self.eat_punct_alone(',');
            }
            tb.add(")");
            return Some(tb.end());
        }
        else if let Some((ty, span)) = self.eat_any_ident_with_span() {
            tb.ident_with_span(&ty, span);
            if ty == "dyn" {
                if let Some((ty, span)) = self.eat_any_ident_with_span() {
                    tb.ident_with_span(&ty, span);
                }
            }
            tb.stream(self.eat_generic());
            return Some(tb.end())
        }
        None
    }
    
}