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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
use super::writer::{TaggedItemInfo, Writer};
use std::collections::HashMap;

// tokenizer types
#[derive(Debug, PartialEq)]
enum TokenType<'a> {
    Semicolon,
    Comma,
    OpenCurlyBracket,
    ClosedCurlyBracket,
    OpenSquareBracket,
    ClosedSquareBracket,
    OpenRoundBracket,
    ClosedRoundBracket,
    Repeat,
    Equals,
    Char,
    Int,
    Long,
    Int64,
    Uchar,
    Uint,
    Ulong,
    Uint64,
    Double,
    Float,
    Block,
    Enum,
    Struct,
    Taggedstruct,
    Taggedunion,
    Constant(i32),
    Identifier(&'a str),
    Tag(&'a str),
}

// parser representation types
#[derive(Debug, PartialEq, Clone)]
pub(crate) struct A2mlTaggedTypeSpec {
    pub(crate) tag: String,
    pub(crate) item: A2mlTypeSpec,
    pub(crate) is_block: bool,
    pub(crate) repeat: bool,
}

#[derive(Debug, PartialEq, Clone)]
pub(crate) enum A2mlTypeSpec {
    None,
    Char,
    Int,
    Long,
    Int64,
    UChar,
    UInt,
    ULong,
    UInt64,
    Float,
    Double,
    Array(Box<A2mlTypeSpec>, usize),
    Enum(HashMap<String, Option<i32>>),
    Struct(Vec<A2mlTypeSpec>),
    Sequence(Box<A2mlTypeSpec>),
    TaggedStruct(HashMap<String, A2mlTaggedTypeSpec>),
    TaggedUnion(HashMap<String, A2mlTaggedTypeSpec>),
}

struct TypeSet {
    enums: HashMap<String, A2mlTypeSpec>,
    structs: HashMap<String, A2mlTypeSpec>,
    taggedstructs: HashMap<String, A2mlTypeSpec>,
    taggedunions: HashMap<String, A2mlTypeSpec>,
}

type A2mlTokenIter<'a> = std::iter::Peekable<std::slice::Iter<'a, TokenType<'a>>>;

// parser output types (generic IF_DATA)

/// A tagged item (of taggedstruct or taggedunion) in the generic `IfData` representation
#[derive(Debug, Clone)]
pub struct GenericIfDataTaggedItem {
    pub incfile: Option<String>,
    pub line: u32,
    pub uid: u32,
    pub start_offset: u32,
    pub end_offset: u32,
    pub tag: String,
    pub data: GenericIfData,
    pub is_block: bool,
}

/// generic representation of data inside an `IF_DATA` block that can be loaded into application-specific data structures
#[derive(Debug, Clone)]
pub enum GenericIfData {
    None,
    Char(u32, (i8, bool)),
    Int(u32, (i16, bool)),
    Long(u32, (i32, bool)),
    Int64(u32, (i64, bool)),
    UChar(u32, (u8, bool)),
    UInt(u32, (u16, bool)),
    ULong(u32, (u32, bool)),
    UInt64(u32, (u64, bool)),
    Float(u32, f32),
    Double(u32, f64),
    String(u32, String),
    Array(Vec<GenericIfData>),
    EnumItem(u32, String),
    Sequence(Vec<GenericIfData>),
    TaggedStruct(HashMap<String, Vec<GenericIfDataTaggedItem>>),
    TaggedUnion(HashMap<String, Vec<GenericIfDataTaggedItem>>),
    Struct(Option<String>, u32, Vec<GenericIfData>),
    Block {
        incfile: Option<String>,
        line: u32,
        items: Vec<GenericIfData>,
    },
}

// tokenize()
// Tokenize the text of the a2ml section
fn tokenize_a2ml(input: &str) -> Result<Vec<TokenType>, String> {
    let mut amltokens = Vec::<TokenType>::new();
    let mut remaining = input;

    while !remaining.is_empty() {
        let mut chars = remaining.char_indices();
        let (mut idx, mut c) = chars.next().unwrap();

        if c.is_ascii_whitespace() {
            /* skip whitespace */
            while c.is_ascii_whitespace() {
                let pair = chars.next().unwrap_or((idx + 1, '\0'));
                idx = pair.0;
                c = pair.1;
            }
            remaining = &remaining[idx..];
            continue;
        } else if remaining.starts_with("/*") {
            /* get a block comment */
            chars.next(); /* skip over the '*' char of the opening sequence */
            let mut done = false;
            let mut star = false;
            while !done {
                if let Some(pair) = chars.next() {
                    idx = pair.0;
                    c = pair.1;
                    if c == '*' {
                        star = true;
                    } else if c == '/' && star {
                        done = true;
                    } else {
                        star = false;
                    }
                } else {
                    let displaylen = if remaining.len() > 16 {
                        16
                    } else {
                        remaining.len()
                    };
                    // slicing remaining in arbitrary ways is not safe, the end might be in the middle of a utf-8 sequence, so from_utf8_lossy is needed
                    let errtxt = String::from_utf8_lossy(&remaining.as_bytes()[..displaylen]);
                    return Err(format!("unclosed block quote starting with \"{errtxt}\""));
                }
            }
            remaining = &remaining[idx + 1..];
        } else if remaining.starts_with("//") {
            /* get a line comment */
            loop {
                if let Some(pair) = chars.next() {
                    idx = pair.0;
                    c = pair.1;
                    if c == '\n' {
                        break;
                    }
                } else {
                    idx = remaining.len() - 1; // results in an empty remaining
                    break;
                }
            }
            remaining = &remaining[idx + 1..];
        } else if c == '"' {
            /* tag - it is enclosed in double quotes, but contains neither spaces nor escape characters */
            loop {
                let pair = chars.next().unwrap_or((idx + 1, '\0'));
                idx = pair.0;
                c = pair.1;
                if c == '"' || c == '\0' {
                    break;
                }
            }
            if c == '"' {
                let tag = &remaining[1..idx];
                amltokens.push(TokenType::Tag(tag));
                remaining = &remaining[idx + 1..];
            } else {
                let displaylen = if remaining.len() > 16 {
                    16
                } else {
                    remaining.len()
                };
                // slicing remaining in arbitrary ways is not safe, the end might be in the middle of a utf-8 sequence, so from_utf8_lossy is needed
                let errtxt = String::from_utf8_lossy(&remaining.as_bytes()[..displaylen]);
                return Err(format!("unclosed tag string starting with {errtxt}"));
            }
        } else if c == ';' {
            amltokens.push(TokenType::Semicolon);
            remaining = &remaining[1..];
        } else if c == ',' {
            amltokens.push(TokenType::Comma);
            remaining = &remaining[1..];
        } else if c == '{' {
            amltokens.push(TokenType::OpenCurlyBracket);
            remaining = &remaining[1..];
        } else if c == '}' {
            amltokens.push(TokenType::ClosedCurlyBracket);
            remaining = &remaining[1..];
        } else if c == '[' {
            amltokens.push(TokenType::OpenSquareBracket);
            remaining = &remaining[1..];
        } else if c == ']' {
            amltokens.push(TokenType::ClosedSquareBracket);
            remaining = &remaining[1..];
        } else if c == '(' {
            amltokens.push(TokenType::OpenRoundBracket);
            remaining = &remaining[1..];
        } else if c == ')' {
            amltokens.push(TokenType::ClosedRoundBracket);
            remaining = &remaining[1..];
        } else if c == '*' {
            amltokens.push(TokenType::Repeat);
            remaining = &remaining[1..];
        } else if c == '=' {
            amltokens.push(TokenType::Equals);
            remaining = &remaining[1..];
        } else if c.is_ascii_digit() {
            loop {
                let pair = chars.next().unwrap_or((idx + 1, '\0'));
                idx = pair.0;
                c = pair.1;
                if !c.is_ascii_alphanumeric() && c != '_' {
                    break;
                }
            }
            let num_text = &remaining[0..idx];
            if let Some(hexval) = num_text.strip_prefix("0x") {
                // hex constant
                if let Ok(number) = i32::from_str_radix(hexval, 16) {
                    amltokens.push(TokenType::Constant(number));
                } else {
                    return Err(format!("Invalid sequence in AML: {num_text}"));
                }
            } else {
                // not hex format -> must be decimal
                if let Ok(number) = num_text.parse::<i32>() {
                    amltokens.push(TokenType::Constant(number));
                } else {
                    return Err(format!("Invalid sequence in AML: {num_text}"));
                }
            }
            remaining = &remaining[idx..];
        } else if c.is_ascii_alphabetic() || c == '_' {
            loop {
                let pair = chars.next().unwrap_or((idx + 1, '\0'));
                idx = pair.0;
                c = pair.1;
                if !c.is_ascii_alphanumeric() && c != '_' {
                    break;
                }
            }
            let kw_or_ident = &remaining[..idx];
            match kw_or_ident {
                "char" => {
                    amltokens.push(TokenType::Char);
                }
                "int" => {
                    amltokens.push(TokenType::Int);
                }
                "long" => {
                    amltokens.push(TokenType::Long);
                }
                "int64" => {
                    amltokens.push(TokenType::Int64);
                }
                "uint" => {
                    amltokens.push(TokenType::Uint);
                }
                "uchar" => {
                    amltokens.push(TokenType::Uchar);
                }
                "ulong" => {
                    amltokens.push(TokenType::Ulong);
                }
                "uint64" => {
                    amltokens.push(TokenType::Uint64);
                }
                "double" => {
                    amltokens.push(TokenType::Double);
                }
                "float" => {
                    amltokens.push(TokenType::Float);
                }
                "block" => {
                    amltokens.push(TokenType::Block);
                }
                "enum" => {
                    amltokens.push(TokenType::Enum);
                }
                "struct" => {
                    amltokens.push(TokenType::Struct);
                }
                "taggedstruct" => {
                    amltokens.push(TokenType::Taggedstruct);
                }
                "taggedunion" => {
                    amltokens.push(TokenType::Taggedunion);
                }
                _ => {
                    amltokens.push(TokenType::Identifier(kw_or_ident));
                }
            }
            remaining = &remaining[idx..];
        } else {
            let displaylen = if remaining.len() > 16 {
                16
            } else {
                remaining.len()
            };
            // slicing remaining in arbitrary ways is not safe, the end might be in the middle of a utf-8 sequence, so from_utf8_lossy is needed
            let errtxt = String::from_utf8_lossy(&remaining.as_bytes()[..displaylen]);
            return Err(format!("Unable to tokenize: {errtxt}..."));
        }
    }

    Ok(amltokens)
}

// parse an a2ml fragment in an a2l file
// The target data structure is the parsing definition used by the a2l parser, so that the
// a2ml can control the parsing of IF_DATA blocks
pub(crate) fn parse_a2ml(input: &str) -> Result<A2mlTypeSpec, String> {
    let tok_result = tokenize_a2ml(input)?;
    let mut tok_iter = tok_result.iter().peekable();

    let mut ifdata_block: Option<A2mlTypeSpec> = None;
    // complex data types can be defined at the beginning and then referenced by name later on.
    let mut types = TypeSet {
        enums: HashMap::<String, A2mlTypeSpec>::new(),
        structs: HashMap::<String, A2mlTypeSpec>::new(),
        taggedstructs: HashMap::<String, A2mlTypeSpec>::new(),
        taggedunions: HashMap::<String, A2mlTypeSpec>::new(),
    };

    // at the top level the applicable grammar rule is
    //    declaration = type_definition ";" | block_definition ";"
    while let Some(tok) = tok_iter.next() {
        match tok {
            TokenType::Block => {
                // the top level only _needs_ exactly one block.
                let tag = require_tag(&mut tok_iter)?;
                let blk = parse_aml_tagged_def(&mut tok_iter, &types)?;
                if tag == "IF_DATA" {
                    ifdata_block = Some(blk);
                }
            }

            TokenType::Taggedstruct => {
                let (optname, typ) = parse_aml_type(&mut tok_iter, &types, tok)?;
                if let Some(name) = optname {
                    types.taggedstructs.insert(name, typ);
                }
            }
            TokenType::Taggedunion => {
                let (optname, typ) = parse_aml_type(&mut tok_iter, &types, tok)?;
                if let Some(name) = optname {
                    types.taggedunions.insert(name, typ);
                }
            }
            TokenType::Enum => {
                let (optname, typ) = parse_aml_type(&mut tok_iter, &types, tok)?;
                if let Some(name) = optname {
                    types.enums.insert(name, typ);
                }
            }
            TokenType::Struct => {
                let (optname, typ) = parse_aml_type(&mut tok_iter, &types, tok)?;
                if let Some(name) = optname {
                    types.structs.insert(name, typ);
                }
            }

            // the grammar allows any type to be defined at the top level, even basic types.
            // however these do not have names, and even if they did, storing them would not help in any way
            TokenType::Char
            | TokenType::Int
            | TokenType::Long
            | TokenType::Int64
            | TokenType::Uchar
            | TokenType::Uint
            | TokenType::Ulong
            | TokenType::Uint64
            | TokenType::Double
            | TokenType::Float => {
                parse_aml_type(&mut tok_iter, &types, tok)?;
            }
            _ => {
                return Err(format!("found unexpected token {tok:?}"));
            }
        }
        require_token_type(&mut tok_iter, &TokenType::Semicolon)?;
    }

    // The integration point between the custom blocks in Aml and the A2l file is the IF_DATA block.
    if let Some(ifdata_block) = ifdata_block {
        Ok(ifdata_block)
    } else {
        Err("The A2ML declaration was fully parsed. However it does not contain an IF_DATA block, so it is not usable.".to_string())
    }
}

// parse_aml_type()
// Implements the grammar rules
//    type_name = predefined_type_name | struct_type_name | taggedstruct_type_name | taggedunion_type_name | enum_type_name
//    predefined_type_name = "char" | "int" | "long" | "uchar" | "uint" | "ulong" | "double" | "float"
fn parse_aml_type(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
    tok_start: &TokenType,
) -> Result<(Option<String>, A2mlTypeSpec), String> {
    match tok_start {
        TokenType::Char => Ok((None, A2mlTypeSpec::Char)),
        TokenType::Int => Ok((None, A2mlTypeSpec::Int)),
        TokenType::Long => Ok((None, A2mlTypeSpec::Long)),
        TokenType::Int64 => Ok((None, A2mlTypeSpec::Int64)),
        TokenType::Uchar => Ok((None, A2mlTypeSpec::UChar)),
        TokenType::Uint => Ok((None, A2mlTypeSpec::UInt)),
        TokenType::Ulong => Ok((None, A2mlTypeSpec::ULong)),
        TokenType::Uint64 => Ok((None, A2mlTypeSpec::UInt64)),
        TokenType::Float => Ok((None, A2mlTypeSpec::Float)),
        TokenType::Double => Ok((None, A2mlTypeSpec::Double)),
        TokenType::Enum => parse_aml_type_enum(tok_iter, types),
        TokenType::Struct => parse_aml_type_struct(tok_iter, types),
        TokenType::Taggedstruct => parse_aml_type_taggedstruct(tok_iter, types),
        TokenType::Taggedunion => parse_aml_type_taggedunion(tok_iter, types),
        _ => Err(format!(
            "unexpected token {tok_start:?} in type declaration"
        )),
    }
}

// parse_aml_type_enum()
// Parses enum definitions according to the grammar:
//    enum_type_name = "enum" [ identifier ] "{" enumerator_list "}" | "enum" identifier
//    enumerator_list = enumerator | enumerator "," enumerator_list
//    enumerator = keyword [ "=" constant ]
//
// If the short form "enum identifier;" is found, then the type is looked up in the hashmap of previously defined enums
// If not, a new enum definition is expected
fn parse_aml_type_enum(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
) -> Result<(Option<String>, A2mlTypeSpec), String> {
    let name: Option<String> = parse_optional_name(tok_iter);

    // check if this is a reference to a previous declaration or if there is also a list of items in {}
    match tok_iter.peek() {
        Some(TokenType::OpenCurlyBracket) => {
            // nothing to do here, a group definition follows
        }
        _ => {
            // no group with content follows, must be a reference to an existing type
            if let Some(name) = name {
                if let Some(A2mlTypeSpec::Enum(items)) = types.enums.get(&name) {
                    return Ok((Some(name), A2mlTypeSpec::Enum(items.clone())));
                } else {
                    return Err(format!("enum {name} was referenced but not defined"));
                }
            } else {
                return Err(String::from(
                    "expected either an identifier or an opening bracket after keyword enum.",
                ));
            }
        }
    }

    // parse the list of enum items
    require_token_type(tok_iter, &TokenType::OpenCurlyBracket)?; // guaranteed to succeed
    let mut enumvalues = HashMap::new();
    loop {
        let tag = require_tag(tok_iter)?;
        let mut token = nexttoken(tok_iter)?;
        /* optionally each enum item may include a constant. */
        let mut con = None;
        if *token == TokenType::Equals {
            con = Some(require_constant(tok_iter)?);
            token = nexttoken(tok_iter)?;
        }
        match token {
            TokenType::Comma => {
                enumvalues.insert(String::from(tag), con);
            }
            TokenType::ClosedCurlyBracket => {
                enumvalues.insert(String::from(tag), con);
                break;
            }
            _ => {
                return Err(format!("unexpected token type {token:?} in enum list"));
            }
        }
    }

    Ok((name, A2mlTypeSpec::Enum(enumvalues)))
}

// parse_aml_type_struct()
// Parses struct definitions according to the grammar:
//    struct_type_name = "struct" [ identifier ] "{" [struct_member_list ] "}" | "struct" identifier
//    struct_member_list = struct_member | struct_member struct_member_list
//    struct_member = member ";"
fn parse_aml_type_struct(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
) -> Result<(Option<String>, A2mlTypeSpec), String> {
    let name: Option<String> = parse_optional_name(tok_iter);

    // check if this is a reference to a previous declaration or if there is also a definition of the struct enclosed in {}
    match tok_iter.peek() {
        Some(TokenType::OpenCurlyBracket) => {
            // nothing to do here, a group definition follows
        }
        _ => {
            // no group with content follows, must be a reference to an existing type
            if let Some(name) = name {
                if let Some(A2mlTypeSpec::Struct(structitems)) = types.structs.get(&name) {
                    return Ok((Some(name), A2mlTypeSpec::Struct(structitems.clone())));
                } else {
                    return Err(format!("struct {name} was referenced but not defined"));
                }
            } else {
                return Err(String::from(
                    "expected either an identifier or an opening bracket after keyword struct.",
                ));
            }
        }
    }

    // parse the struct elements
    require_token_type(tok_iter, &TokenType::OpenCurlyBracket)?; // guaranteed to succeed
    let mut structdata = Vec::new();

    loop {
        structdata.push(parse_aml_member(tok_iter, types)?);
        require_token_type(tok_iter, &TokenType::Semicolon)?;

        if let Some(TokenType::ClosedCurlyBracket) = tok_iter.peek() {
            break;
        }
    }
    require_token_type(tok_iter, &TokenType::ClosedCurlyBracket)?;

    Ok((name, A2mlTypeSpec::Struct(structdata)))
}

// parse_aml_type_taggedstruct()
// Parses taggedstructs according to the grammar:
//    taggedstruct_type_name = "taggedstruct" [ identifier ] "{" [taggedstruct_member_list ] "}" | "taggedstruct" identifier
//    taggedstruct_member_list = taggedstruct_member | taggedstruct_member taggedstruct_member_list
fn parse_aml_type_taggedstruct(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
) -> Result<(Option<String>, A2mlTypeSpec), String> {
    let name: Option<String> = parse_optional_name(tok_iter);

    // check if this is a reference to a previous declaration or if there is also a definition of the taggedstruct enclosed in {}
    match tok_iter.peek() {
        Some(TokenType::OpenCurlyBracket) => {
            // nothing to do here, a group definition follows
        }
        _ => {
            // no group with content follows, must be a reference to an existing type
            if let Some(name) = name {
                if let Some(A2mlTypeSpec::TaggedStruct(tsitems)) = types.taggedstructs.get(&name) {
                    return Ok((Some(name), A2mlTypeSpec::TaggedStruct(tsitems.clone())));
                } else {
                    return Err(format!(
                        "taggedstruct {name} was referenced but not defined"
                    ));
                }
            } else {
                return Err(String::from("expected either an identifier or an opening bracket after keyword taggedstruct."));
            }
        }
    }

    // parse the taggedstruct elements
    require_token_type(tok_iter, &TokenType::OpenCurlyBracket)?; // guaranteed to succeed
    let mut taggedstructdata = HashMap::new();
    loop {
        let (itemname, itemdef) = parse_aml_taggedmember(tok_iter, types, true)?;
        taggedstructdata.insert(itemname, itemdef);
        require_token_type(tok_iter, &TokenType::Semicolon)?;

        if let Some(TokenType::ClosedCurlyBracket) = tok_iter.peek() {
            break;
        }
    }
    require_token_type(tok_iter, &TokenType::ClosedCurlyBracket)?;

    Ok((name, A2mlTypeSpec::TaggedStruct(taggedstructdata)))
}

// parse_aml_type_taggedunion()
//    taggedunion_type_name = "taggedunion" [ identifier ] "{" [taggedunion_member_list ] "}" | "taggedunion" identifier
//    taggedunion_member_list = tagged_union_member | tagged_union_member taggedunion_member_list
fn parse_aml_type_taggedunion(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
) -> Result<(Option<String>, A2mlTypeSpec), String> {
    let name: Option<String> = parse_optional_name(tok_iter);

    // check if this is a reference to a previous declaration or if there is also a definition of the taggedunion enclosed in {}
    match tok_iter.peek() {
        Some(TokenType::OpenCurlyBracket) => {
            // nothing to do here, a group definition follows
        }
        _ => {
            // no group with content follows, must be a reference to an existing type
            if let Some(name) = name {
                if let Some(A2mlTypeSpec::TaggedUnion(tsitems)) = types.taggedunions.get(&name) {
                    return Ok((Some(name), A2mlTypeSpec::TaggedUnion(tsitems.clone())));
                } else {
                    return Err(format!("taggedunion {name} was referenced but not defined"));
                }
            } else {
                return Err(String::from("A2ML error: expected either an identifier or an opening bracket after keyword taggedunion."));
            }
        }
    }

    /* parse the taggedunion elements */
    require_token_type(tok_iter, &TokenType::OpenCurlyBracket)?; // guaranteed to succeed
    let mut taggeduniondata = HashMap::new();
    loop {
        let (itemname, itemdef) = parse_aml_taggedmember(tok_iter, types, false)?;
        taggeduniondata.insert(itemname, itemdef);
        require_token_type(tok_iter, &TokenType::Semicolon)?;

        if let Some(TokenType::ClosedCurlyBracket) = tok_iter.peek() {
            break;
        }
    }
    require_token_type(tok_iter, &TokenType::ClosedCurlyBracket)?;

    Ok((name, A2mlTypeSpec::TaggedUnion(taggeduniondata)))
}

// parse_aml_taggedstructmember()
// Parses taggedstruct members according to the grammar:
//    taggedstruct_member = taggedstruct_definition ";" | "(" taggedstruct_definition ")*;" | block_definition ";" | "(" block_definition ")*;"
fn parse_aml_taggedmember(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
    allow_repeat: bool,
) -> Result<(String, A2mlTaggedTypeSpec), String> {
    let mut tok = nexttoken(tok_iter)?;

    let mut repeat = false;
    if allow_repeat && *tok == TokenType::OpenRoundBracket {
        repeat = true;
        tok = nexttoken(tok_iter)?;
    }

    let mut is_block = false;
    if let TokenType::Block = tok {
        is_block = true;
        tok = nexttoken(tok_iter)?;
    }

    let taggedmember = if let TokenType::Tag(tag) = tok {
        let tok_peek = tok_iter.peek();
        let item = if let Some(TokenType::Semicolon) = tok_peek {
            A2mlTypeSpec::None
        } else {
            parse_aml_tagged_def(tok_iter, types)?
        };
        (
            (*tag).to_string(),
            A2mlTaggedTypeSpec {
                tag: (*tag).to_string(),
                item,
                is_block,
                repeat,
            },
        )
    } else {
        return Err(format!(
            "invalid token type {tok:#?} while attempting to parse taggedstruct member"
        ));
    };

    if repeat {
        require_token_type(tok_iter, &TokenType::ClosedRoundBracket)?;
        require_token_type(tok_iter, &TokenType::Repeat)?;
    }

    Ok(taggedmember)
}

// parse_aml_tagged_def()
// Parses taggedstruct definitions according to the grammar:
//    taggedstruct_definition = tag [ member ] | tag "(" member ")*;"
fn parse_aml_tagged_def(
    tok_iter: &mut A2mlTokenIter,
    types: &TypeSet,
) -> Result<A2mlTypeSpec, String> {
    let mut inner_repeat = false;
    if let Some(TokenType::OpenRoundBracket) = tok_iter.peek() {
        inner_repeat = true;
        tok_iter.next();
    }

    let mut member = parse_aml_member(tok_iter, types)?;

    if inner_repeat {
        require_token_type(tok_iter, &TokenType::ClosedRoundBracket)?;
        require_token_type(tok_iter, &TokenType::Repeat)?;
        member = A2mlTypeSpec::Sequence(Box::new(member));
    }

    Ok(member)
}

// parse_aml_member()
// Parse a member of some other data structure. Each member could potentially have an arbitrary number of array dimensions
//    member = type_name [ array_specifier ]
//    array_specifier = "[" constant "]" | "[" constant "]" array_specifier
fn parse_aml_member(tok_iter: &mut A2mlTokenIter, types: &TypeSet) -> Result<A2mlTypeSpec, String> {
    let tok_start = nexttoken(tok_iter)?;
    let (_, mut base_type) = parse_aml_type(tok_iter, types, tok_start)?;

    while let Some(TokenType::OpenSquareBracket) = tok_iter.peek() {
        /* get the array dim */
        require_token_type(tok_iter, &TokenType::OpenSquareBracket)?;
        let dim = require_constant(tok_iter)?;
        require_token_type(tok_iter, &TokenType::ClosedSquareBracket)?;

        base_type = A2mlTypeSpec::Array(Box::new(base_type), dim as usize);
    }

    Ok(base_type)
}

// parse_optional_name()
// For enums, structs, taggedstructs and taggedunions the typename is optional.
// Called at the beginning of parsing one of these data strucutres, this function checks if the next token is a type name and returns it
fn parse_optional_name(tok_iter: &mut A2mlTokenIter) -> Option<String> {
    if let Some(TokenType::Identifier(ident)) = tok_iter.peek() {
        tok_iter.next(); // consume the token. no need to do anything with it since we already have the content
        Some(String::from(*ident))
    } else {
        None
    }
}

// require_token_type()
// get the next token, which is required to be of the provided type
fn require_token_type(tok_iter: &mut A2mlTokenIter, reference: &TokenType) -> Result<(), String> {
    let token = nexttoken(tok_iter)?;
    if *token != *reference {
        return Err(format!(
            "A2ML Error: expected token of type {reference:?}, got {token:?}"
        ));
    }
    Ok(())
}

// require_tag()
// get the content of the next token, which is required to be a tag
fn require_tag<'a>(tok_iter: &mut A2mlTokenIter<'a>) -> Result<&'a str, String> {
    match tok_iter.next() {
        Some(TokenType::Tag(tag)) => Ok(tag),
        Some(tok) => Err(format!(
            "A2ML Error: incorrect token type {tok:?} where tag was expected"
        )),
        None => Err(String::from(
            "A2ML Error: unexpected end of input where token type tag was expected",
        )),
    }
}

// require_constant()
// get the content of the next token, which is required to be a constant
fn require_constant(tok_iter: &mut A2mlTokenIter) -> Result<i32, String> {
    match tok_iter.next() {
        Some(TokenType::Constant(c)) => Ok(*c),
        Some(tok) => Err(format!(
            "A2ML Error: incorrect token type {tok:?} where a constant was expected"
        )),
        None => Err(String::from(
            "A2ML Error: unexpected end of input where token type constant was expected",
        )),
    }
}

// nexttoken
// get the next token from the iterator and centralize the handling of potential None-values
fn nexttoken<'a>(tok_iter: &mut A2mlTokenIter<'a>) -> Result<&'a TokenType<'a>, String> {
    match tok_iter.next() {
        Some(tok) => Ok(tok),
        None => Err(String::from("A2ML Error: unexpected end of input")),
    }
}

impl GenericIfData {
    pub fn get_block_items(
        &self,
    ) -> Result<(Option<String>, u32, &Vec<GenericIfData>), &'static str> {
        match self {
            GenericIfData::Block {
                incfile,
                line,
                items,
            } => Ok((incfile.clone(), *line, items)),
            _ => {
                Err("structural mismatch: get_block_items called on something that is not a Block")
            }
        }
    }

    pub fn get_struct_items(
        &self,
    ) -> Result<(Option<String>, u32, &Vec<GenericIfData>), &'static str> {
        match self {
            GenericIfData::Struct(file, line, blockitems) => Ok((file.clone(), *line, blockitems)),
            _ => Err(
                "structural mismatch: get_struct_items called on something that is not a Struct",
            ),
        }
    }

    pub fn get_int_is_hex(&self) -> Result<bool, &'static str> {
        match self {
            GenericIfData::UChar(_, (_, is_hex))
            | GenericIfData::UInt(_, (_, is_hex))
            | GenericIfData::ULong(_, (_, is_hex))
            | GenericIfData::UInt64(_, (_, is_hex))
            | GenericIfData::Char(_, (_, is_hex))
            | GenericIfData::Int(_, (_, is_hex))
            | GenericIfData::Long(_, (_, is_hex))
            | GenericIfData::Int64(_, (_, is_hex)) => Ok(*is_hex),
            _ => Err(
                "structural mismatch: get_int_is_hex called on something that is not an integer",
            ),
        }
    }

    pub fn get_integer_u8(&self) -> Result<u8, &'static str> {
        if let GenericIfData::UChar(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_u8 called on something that is not a UChar")
        }
    }

    pub fn get_integer_u16(&self) -> Result<u16, &'static str> {
        if let GenericIfData::UInt(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_u16 called on something that is not a UInt")
        }
    }

    pub fn get_integer_u32(&self) -> Result<u32, &'static str> {
        if let GenericIfData::ULong(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_u32 called on something that is not a ULong")
        }
    }

    pub fn get_integer_u64(&self) -> Result<u64, &'static str> {
        if let GenericIfData::UInt64(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_u64 called on something that is not a UInt64")
        }
    }

    pub fn get_integer_i8(&self) -> Result<i8, &'static str> {
        if let GenericIfData::Char(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_i8 called on something that is not a Char")
        }
    }

    pub fn get_integer_i16(&self) -> Result<i16, &'static str> {
        if let GenericIfData::Int(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_i16 called on something that is not an Int")
        }
    }

    pub fn get_integer_i32(&self) -> Result<i32, &'static str> {
        if let GenericIfData::Long(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_i32 called on something that is not a Long")
        }
    }

    pub fn get_integer_i64(&self) -> Result<i64, &'static str> {
        if let GenericIfData::Int64(_, (val, _)) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_integer_i64 called on something that is not an Int64")
        }
    }

    pub fn get_float(&self) -> Result<f32, &'static str> {
        if let GenericIfData::Float(_, val) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_float called on something that is not a Float")
        }
    }

    pub fn get_double(&self) -> Result<f64, &'static str> {
        if let GenericIfData::Double(_, val) = self {
            Ok(*val)
        } else {
            Err("structural mismatch: get_double called on something that is not a Double")
        }
    }

    pub fn get_stringval(&self) -> Result<String, &'static str> {
        if let GenericIfData::String(_, val) = self {
            Ok(val.to_owned())
        } else {
            Err("structural mismatch: get_stringval called on something that is not a String")
        }
    }

    pub fn get_array(&self) -> Result<&Vec<GenericIfData>, &'static str> {
        if let GenericIfData::Array(arrayitems) = self {
            Ok(arrayitems)
        } else {
            Err("structural mismatch: get_array called on something that is not an Array")
        }
    }

    pub fn get_sequence(&self) -> Result<&Vec<GenericIfData>, &'static str> {
        if let GenericIfData::Sequence(seqitems) = self {
            Ok(seqitems)
        } else {
            Err("structural mismatch: get_sequence called on something that is not a Sequence")
        }
    }

    pub fn get_line(&self) -> Result<u32, &'static str> {
        match self {
            GenericIfData::Char(line, _)
            | GenericIfData::Int(line, _)
            | GenericIfData::Long(line, _)
            | GenericIfData::Int64(line, _)
            | GenericIfData::UChar(line, _)
            | GenericIfData::UInt(line, _)
            | GenericIfData::ULong(line, _)
            | GenericIfData::UInt64(line, _)
            | GenericIfData::Float(line, _)
            | GenericIfData::Double(line, _)
            | GenericIfData::String(line, _)
            | GenericIfData::EnumItem(line, _)
            | GenericIfData::Struct(_, line, _)
            | GenericIfData::Block { line, .. } => Ok(*line),
            _ => Err("structural mismatch: get_line called on something that has no line info"),
        }
    }

    pub fn get_single_optitem<T>(
        &self,
        tag: &str,
        func: fn(&GenericIfData, u32, u32, u32) -> Result<T, &'static str>,
    ) -> Result<Option<T>, &'static str> {
        match self {
            GenericIfData::TaggedStruct(taggeditems) | GenericIfData::TaggedUnion(taggeditems) => {
                if let Some(itemlist) = taggeditems.get(tag) {
                    Ok(Some(func(
                        &itemlist[0].data,
                        itemlist[0].uid,
                        itemlist[0].start_offset,
                        itemlist[0].end_offset,
                    )?))
                } else {
                    Ok(None)
                }
            }
            _ => Err("structural mismatch: get_single_optitem called on unsuitable element"),
        }
    }

    pub fn get_multiple_optitems<T>(
        &self,
        tag: &str,
        func: fn(&GenericIfData, u32, u32, u32) -> Result<T, &'static str>,
    ) -> Result<Vec<T>, &'static str> {
        match self {
            GenericIfData::TaggedStruct(taggeditems) | GenericIfData::TaggedUnion(taggeditems) => {
                let mut resultvec = Vec::new();
                if let Some(itemlist) = taggeditems.get(tag) {
                    for item in itemlist {
                        resultvec.push(func(
                            &item.data,
                            item.uid,
                            item.start_offset,
                            item.end_offset,
                        )?);
                    }
                }
                Ok(resultvec)
            }
            _ => Err("structural mismatch: get_multiple_optitems called on unsuitable element"),
        }
    }
}

impl GenericIfData {
    pub(crate) fn write(&self, indent: usize) -> String {
        match self {
            GenericIfData::Struct(_, _, items) | GenericIfData::Block { items, .. } => {
                let mut writer = Writer::new(indent);
                for item in items {
                    item.write_item(&mut writer, indent);
                }
                writer.finish()
            }
            _ => {
                let mut writer = Writer::new(indent);
                self.write_item(&mut writer, indent);
                writer.finish()
            }
        }
    }

    fn write_item(&self, writer: &mut Writer, indent: usize) {
        match self {
            Self::Char(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::Int(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::Long(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::Int64(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::UChar(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::UInt(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::ULong(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::UInt64(offset, value) => {
                writer.add_integer(value.0, value.1, *offset);
            }
            Self::Float(offset, value) => {
                writer.add_float(*value, *offset);
            }
            Self::Double(offset, value) => {
                writer.add_float(*value, *offset);
            }
            Self::String(offset, text) => {
                writer.add_quoted_string(text, *offset);
            }
            Self::EnumItem(offset, enitem) => {
                writer.add_str(enitem, *offset);
            }
            Self::Array(items) | Self::Sequence(items) | Self::Struct(_, _, items) => {
                for item in items {
                    item.write_item(writer, indent);
                }
            }
            Self::TaggedStruct(taggeditems) | Self::TaggedUnion(taggeditems) => {
                let mut tgroup = Vec::new();
                for tgitemlist in taggeditems.values() {
                    for tgitem in tgitemlist {
                        tgroup.push(TaggedItemInfo {
                            tag: &tgitem.tag,
                            incfile: &tgitem.incfile,
                            uid: tgitem.uid,
                            line: tgitem.line,
                            start_offset: tgitem.start_offset,
                            end_offset: tgitem.end_offset,
                            is_block: tgitem.is_block,
                            item_text: tgitem.data.write(indent + 1),
                            position_restriction: None,
                        });
                    }
                }
                writer.add_group(tgroup);
            }
            _ => { /* no need to do anything for Self::None and Self::Block */ }
        }
    }

    // merge_includes()
    // items that originate in an included file will have theit incfile member set to Some("filename")
    // this function strips that information, which will cause these included items to be written with all
    // the elements that were part of this file originally
    pub(crate) fn merge_includes(&mut self) {
        match self {
            Self::Block { incfile, items, .. } | Self::Struct(incfile, _, items) => {
                *incfile = None;
                for item in items {
                    item.merge_includes();
                }
            }
            Self::TaggedStruct(taggeditems) | Self::TaggedUnion(taggeditems) => {
                for tgitemlist in taggeditems.values_mut() {
                    for tgitem in tgitemlist {
                        tgitem.incfile = None;
                        tgitem.data.merge_includes();
                    }
                }
            }
            _ => {}
        }
    }
}

// an implementation of PartialEq that ignores the line numbers stored in the elements.
// Two elements are equal if their contained values match, regardless of the line numbers.
impl PartialEq for GenericIfData {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Self::None => {
                matches!(other, Self::None)
            }
            Self::Char(_, val) => {
                if let Self::Char(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Int(_, val) => {
                if let Self::Int(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Long(_, val) => {
                if let Self::Long(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Int64(_, val) => {
                if let Self::Int64(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::UChar(_, val) => {
                if let Self::UChar(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::UInt(_, val) => {
                if let Self::UInt(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::ULong(_, val) => {
                if let Self::ULong(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::UInt64(_, val) => {
                if let Self::UInt64(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Float(_, val) => {
                if let Self::Float(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Double(_, val) => {
                if let Self::Double(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::String(_, val) => {
                if let Self::String(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Array(arr) => {
                if let Self::Array(otherarr) = other {
                    arr == otherarr
                } else {
                    false
                }
            }
            Self::EnumItem(_, val) => {
                if let Self::EnumItem(_, otherval) = other {
                    val == otherval
                } else {
                    false
                }
            }
            Self::Struct(_, _, items) => {
                if let Self::Struct(_, _, otheritems) = other {
                    items == otheritems
                } else {
                    false
                }
            }
            Self::Sequence(seq) => {
                if let Self::Sequence(otherseq) = other {
                    seq == otherseq
                } else {
                    false
                }
            }
            Self::TaggedStruct(tgitems) => {
                if let Self::TaggedStruct(othertgi) = other {
                    tgitems == othertgi
                } else {
                    false
                }
            }
            Self::TaggedUnion(tgitems) => {
                if let Self::TaggedUnion(othertgi) = other {
                    tgitems == othertgi
                } else {
                    false
                }
            }
            Self::Block { items, .. } => {
                if let Self::Block {
                    items: otheritems, ..
                } = other
                {
                    items == otheritems
                } else {
                    false
                }
            }
        }
    }
}

impl PartialEq for GenericIfDataTaggedItem {
    fn eq(&self, other: &Self) -> bool {
        self.tag == other.tag && self.data == other.data && self.is_block == other.is_block
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn tokenize() {
        let tokenvec = tokenize_a2ml("       ").unwrap();
        assert!(tokenvec.is_empty());

        let tokenvec = tokenize_a2ml("/* // */").unwrap();
        assert!(tokenvec.is_empty());
        let tokenvec = tokenize_a2ml("/*/*/").unwrap();
        assert!(tokenvec.is_empty());
        let tokenvec = tokenize_a2ml("/***/").unwrap();
        assert!(tokenvec.is_empty());
        let tokenvec_err = tokenize_a2ml("/* ");
        assert!(tokenvec_err.is_err());
        let tokenvec = tokenize_a2ml("//*/").unwrap();
        assert!(tokenvec.is_empty());

        let tokenvec = tokenize_a2ml(r#""TAG""#).unwrap();
        assert_eq!(tokenvec.len(), 1);
        assert!(matches!(tokenvec[0], TokenType::Tag("TAG")));

        let tokenvec = tokenize_a2ml(";").unwrap();
        assert!(matches!(tokenvec[0], TokenType::Semicolon));

        let tokenvec = tokenize_a2ml("0").unwrap();
        assert!(matches!(tokenvec[0], TokenType::Constant(0)));

        let tokenvec = tokenize_a2ml("0x03").unwrap();
        assert!(matches!(tokenvec[0], TokenType::Constant(3)));

        let tokenvec = tokenize_a2ml("123456").unwrap();
        assert!(matches!(tokenvec[0], TokenType::Constant(123456)));

        let err_result = tokenize_a2ml(r#" "unclosed "#);
        assert!(err_result.is_err());
    }

    #[test]
    fn parse() {
        static TEST_INPUT: &str = r#"
        /* comment */
        struct SomeStruct {
            uchar; // comment
            uint;
            ulong;
            char;
            int;
            long;
            float;
            double;
            uint[3];
            enum {
                "ANON_ENUM_A" = 0,
                "ANON_ENUM_B" = 1
            };
            enum xyz {
                "SOME_ENUM_A" = 1,
                "SOME_ENUM_B" = 0x2,
                "SOME_EMUM_C" = 3
            };
            taggedunion {
                "FOO" uint;
                "BAR" uchar;
            };
            taggedstruct {
                ("REP_ITEM" uint)*;
                "NORMAL_ITEM" struct {
                    char[128];
                };
                "REP_ITEM_INNER" (struct InnerRepStruct {
                    uint;
                })*;
            };
        };

        block "IF_DATA" struct SomeStruct;
"#;
        let mut enum_hashmap_1 = HashMap::new();
        enum_hashmap_1.insert("ANON_ENUM_B".to_string(), Some(1));
        enum_hashmap_1.insert("ANON_ENUM_A".to_string(), Some(0));
        let mut enum_hashmap_2 = HashMap::new();
        enum_hashmap_2.insert("SOME_ENUM_A".to_string(), Some(1));
        enum_hashmap_2.insert("SOME_ENUM_B".to_string(), Some(2));
        enum_hashmap_2.insert("SOME_EMUM_C".to_string(), Some(3));
        let mut taggedunion_hashmap = HashMap::new();
        taggedunion_hashmap.insert(
            "FOO".to_string(),
            A2mlTaggedTypeSpec {
                tag: "FOO".to_string(),
                item: A2mlTypeSpec::UInt,
                is_block: false,
                repeat: false,
            },
        );
        taggedunion_hashmap.insert(
            "BAR".to_string(),
            A2mlTaggedTypeSpec {
                tag: "BAR".to_string(),
                item: A2mlTypeSpec::UChar,
                is_block: false,
                repeat: false,
            },
        );
        let mut taggedstruct_hashmap = HashMap::new();
        taggedstruct_hashmap.insert(
            "NORMAL_ITEM".to_string(),
            A2mlTaggedTypeSpec {
                tag: "NORMAL_ITEM".to_string(),
                item: A2mlTypeSpec::Struct(vec![A2mlTypeSpec::Array(
                    Box::new(A2mlTypeSpec::Char),
                    128,
                )]),
                is_block: false,
                repeat: false,
            },
        );
        taggedstruct_hashmap.insert(
            "REP_ITEM_INNER".to_string(),
            A2mlTaggedTypeSpec {
                tag: "REP_ITEM_INNER".to_string(),
                item: A2mlTypeSpec::Sequence(Box::new(A2mlTypeSpec::Struct(vec![
                    A2mlTypeSpec::UInt,
                ]))),
                is_block: false,
                repeat: false,
            },
        );
        taggedstruct_hashmap.insert(
            "REP_ITEM".to_string(),
            A2mlTaggedTypeSpec {
                tag: "REP_ITEM".to_string(),
                item: A2mlTypeSpec::UInt,
                is_block: false,
                repeat: true,
            },
        );
        let expected_parse_result = A2mlTypeSpec::Struct(vec![
            A2mlTypeSpec::UChar,
            A2mlTypeSpec::UInt,
            A2mlTypeSpec::ULong,
            A2mlTypeSpec::Char,
            A2mlTypeSpec::Int,
            A2mlTypeSpec::Long,
            A2mlTypeSpec::Float,
            A2mlTypeSpec::Double,
            A2mlTypeSpec::Array(Box::new(A2mlTypeSpec::UInt), 3),
            A2mlTypeSpec::Enum(enum_hashmap_1),
            A2mlTypeSpec::Enum(enum_hashmap_2),
            A2mlTypeSpec::TaggedUnion(taggedunion_hashmap),
            A2mlTypeSpec::TaggedStruct(taggedstruct_hashmap),
        ]);

        let parse_result = parse_a2ml(TEST_INPUT);
        assert!(parse_result.is_ok());
        let a2ml_spec = parse_result.unwrap();
        println!("{:?}", a2ml_spec);
        assert_eq!(a2ml_spec, expected_parse_result);
    }
}