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
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
//! This module contains a CDDL parser.
//!
//! The only public items here are the function [`parse_cddl`] and the error
//! [`ParseError`] and its child enum [`ErrorKind`].
//!
//! # Examples
//! ```
//! use cddl_cat::parse_cddl;
//!
//! let input = "map = { name: tstr }";
//! assert!(parse_cddl(input).is_ok());
//! ```

use crate::ast::*;
use escape8259::unescape;
use nom::{
    branch::alt,
    bytes::complete::{tag, take_while1},
    character::complete::{
        anychar, char as charx, digit0, digit1, hex_digit1, multispace1, not_line_ending, one_of,
    },
    combinator::{all_consuming, map, map_res, opt, recognize, value as valuex},
    error::FromExternalError,
    multi::{many0, many1, separated_list1},
    sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
};
use std::{borrow::Cow, convert::TryFrom};
use thiserror::Error;

//
// A note on the design of the parser:
//
// Parsers built from the "nom" crate look a bit different from most other
// Rust code; for extra readability there is a lot of extra indents that
// rustfmt wouldn't like (and rustfmt::skip is applied extensively.)

// This error type is used everywhere in this parser.  It allows
// me to mix locally-generated custom errors with the errors that
// are automatically generated by the parser.
type JResult<'a, I, O> = nom::IResult<I, O, CowParseError<'a>>;

/// The "kind" of error generated during CDDL parsing.
#[non_exhaustive]
#[derive(Debug, PartialEq)]
pub enum ErrorKind {
    /// An integer didn't parse correctly.
    MalformedInteger,
    /// A floating-point number didn't parse correctly.
    MalformedFloat,
    /// A hex literal didn't parse correctly.
    MalformedHex,
    /// A malformed text string
    MalformedText,
    /// A malformed base64 byte string
    MalformedBase64,
    /// A nonspecific parsing error.
    Unparseable,
}
use ErrorKind::*;

/// An error that occurred during CDDL parsing.
#[derive(Debug, PartialEq, Error)]
// thiserror will generate a Display implementation.
#[error("{kind:?}({ctx})")]
pub struct ParseError {
    /// The "kind" of error generated during CDDL parsing.
    pub kind: ErrorKind,
    /// A snippet of text from the CDDL input that may be the cause of the error.
    pub ctx: String,
}

// Convert a temporary error into an owned 'static error.
impl From<CowParseError<'_>> for ParseError {
    fn from(err: CowParseError<'_>) -> Self {
        ParseError {
            kind: err.kind,
            // Create an owned String from the Cow<'_, str>
            ctx: err.ctx.into(),
        }
    }
}

#[derive(Debug, PartialEq)]
struct CowParseError<'a> {
    /// The "kind" of error generated during CDDL parsing.
    pub kind: ErrorKind,
    /// A snippet of text from the CDDL input that may be the cause of the error.
    ///
    /// This may contain either a borrowed 'str or an owned String. This is useful
    /// because many transient errors are generated during parsing and thrown away,
    /// and there's no point allocating memory for them until we are done parsing.
    pub ctx: Cow<'a, str>,
}

// Convert a bounded lifetime ParseError into a ParseError<'static>.

impl<I, E> FromExternalError<I, E> for CowParseError<'_> {
    fn from_external_error(_input: I, _kind: nom::error::ErrorKind, _e: E) -> Self {
        CowParseError {
            kind: ErrorKind::Unparseable,
            ctx: "nom-error".into(),
        }
    }
}

fn parse_error<'a, S: Into<Cow<'a, str>>>(kind: ErrorKind, ctx: S) -> CowParseError<'a> {
    CowParseError {
        kind,
        ctx: ctx.into(),
    }
}

// Used when calling all_consuming() at the end of the parsing process.
impl From<nom::Err<CowParseError<'_>>> for ParseError {
    fn from(e: nom::Err<CowParseError>) -> ParseError {
        match e {
            nom::Err::Incomplete(_) => parse_error(Unparseable, "Incomplete"),
            nom::Err::Error(pe) => pe,
            nom::Err::Failure(pe) => pe,
        }
        .into()
    }
}

// FIXME: the name collision here makes the code hard to read
impl<'a, I: Into<Cow<'a, str>>> nom::error::ParseError<I> for CowParseError<'a> {
    fn from_error_kind(input: I, _kind: nom::error::ErrorKind) -> Self {
        parse_error(Unparseable, input)
    }

    fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self {
        // FIXME: It's not obvious what I should do here, or
        // when a proper implementation will be necessary...
        other
    }
}

// A workaround for the fact that nom::combinator::map_res discards the returned error type.
// See also https://github.com/Geal/nom/issues/1171
fn map_res_fail<I: Clone, O1, O2, E: nom::error::ParseError<I>, F, G>(
    mut first: F,
    second: G,
) -> impl FnMut(I) -> nom::IResult<I, O2, E>
where
    F: FnMut(I) -> nom::IResult<I, O1, E>,
    G: Fn(O1) -> Result<O2, E>,
{
    move |input: I| {
        let (input, o1) = first(input)?;
        match second(o1) {
            Ok(o2) => Ok((input, o2)),
            Err(e) => Err(nom::Err::Failure(e)),
        }
    }
}

// CDDL whitespace definition:
// Note no support for tabs, or naked linefeed characters.
//
// S = *WS
// WS = SP / NL
// SP = %x20
// NL = COMMENT / CRLF
// COMMENT = ";" *PCHAR CRLF
// PCHAR = %x20-7E / %x80-10FFFD
// CRLF = %x0A / %x0D.0A

// This varies a bit from the RFC, again, with respect to whitespace.
#[rustfmt::skip]
fn comment(input: &str) -> JResult<&str, &str> {
    // semicolon, anything, terminated by CR or CR+LF.
    preceded(
        charx(';'),
        not_line_ending
    )(input)
}

// Any amount of whitespace (including none) (including comments)
// Note: this counts tab characters as whitespace, which differs from RFC8610.
#[rustfmt::skip]
fn ws(input: &str) -> JResult<&str, &str> {
    recognize(
        many0(
            alt((
                // multispace1 includes tabs
                multispace1,
                comment
            ))
        )
    )
    (input)
}

// An optional comma and any whitespace surrounding it.
#[rustfmt::skip]
fn optcom(input: &str) -> JResult<&str, ()> {
    valuex((), pair(
        ws,
        opt(pair(
            tag(","),
            ws
        ))
    ))
    (input)
}

// id = EALPHA *(*("-" / ".") (EALPHA / DIGIT))
// So the first character needs to be EALPHA (alpha plus @_$)
// and then any number of EALPHA or DIGIT or "-" or ".",
// ending with EALPHA or DIGIT.
// we'll take this in steps:
// 1. first EALPHA char
// 2. do the following zero or more times:
//    a. optionally consume one of -.
//    b. consume EALPHA or DIGIT.

fn ealpha_char(c: char) -> bool {
    c.is_ascii_alphabetic() || c == '@' || c == '_' || c == '$'
}

#[rustfmt::skip]
fn ealpha(input: &str) -> JResult<&str, &str> {
    take_while1(ealpha_char)
    (input)
}

// The tail characters of an ident: *("-" / ".") (EALPHA / DIGIT)
#[rustfmt::skip]
fn ident_tail(input: &str) -> JResult<&str, &str> {
    recognize(
        preceded(
            opt(many1(one_of("-."))),
            alt((
                ealpha,
                digit1
            ))
        )
    )
    (input)
}

#[rustfmt::skip]
fn ident(input: &str) -> JResult<&str, &str> {
    recognize(
        preceded(
            ealpha,
            many0(ident_tail)
        )
    )
    (input)
}

// uint = DIGIT1 *DIGIT
//      / "0x" 1*HEXDIG
//      / "0b" 1*BINDIG
//      / "0"
#[rustfmt::skip]
fn uint_hex(input: &str) -> JResult<&str, &str> {
    preceded(
        tag("0x"),
        hex_digit1
    )
    (input)
}

#[rustfmt::skip]
fn uint_binary(input: &str) -> JResult<&str, &str> {
    preceded(
        tag("0b"),
        recognize(many1(one_of("01")))
    )
    (input)
}

#[rustfmt::skip]
fn uint_decimal(input: &str) -> JResult<&str, &str> {
    alt((
        tag("0"),
        recognize(
            pair(
                one_of("123456789"),
                digit0
            )
        )
    ))
    (input)
}

// Parsing of integers, floats, etc. can get pretty complicated.  Because
// different downstream parsers may want the integer in different forms, we
// store its information in a temporary struct that preserves the original
// string slice along with some context that will help us remember what that
// string represents.
struct RawUint<'a> {
    slice: &'a str,
    base: u32,
}

// Parse the string for uint; return the slice and the intended base (radix).
#[rustfmt::skip]
fn uint(input: &str) -> JResult<&str, RawUint> {
    alt((
        map(uint_hex, |slice| RawUint{slice, base: 16}),
        map(uint_binary, |slice| RawUint{slice, base: 2}),
        map(uint_decimal, |slice| {
            RawUint{slice, base: 10}
        }),
    ))
    (input)
}

// Extract an unsigned integer using the correct base (radix).
#[rustfmt::skip]
fn uint_u64(input: &str) -> JResult<&str, u64> {
    map_res_fail(uint, |raw| {
        u64::from_str_radix(raw.slice, raw.base)
        .map_err(|_| {
            parse_error(MalformedInteger, raw.slice)
        })
    })
    (input)
}

// Like RawUint, this is a temporary representation so we can
// preserve both the string slice and some metadata about it.
struct RawInt<'a> {
    slice: &'a str,
    base: u32,
    neg: bool,
}

// A signed integer
#[rustfmt::skip]
fn int(input: &str) -> JResult<&str, RawInt> {
    let f = pair(
        opt(charx('-')),
        uint
    );
    map(f, |(optneg, rawuint)| {
        RawInt {
            slice: rawuint.slice,
            base: rawuint.base,
            neg: optneg.is_some(),
        }
    })
    (input)
}

// "." fraction
// fraction = 1*DIGIT
#[rustfmt::skip]
fn dot_fraction(input: &str) -> JResult<&str, &str> {
    recognize(
        pair(
            charx('.'),
            digit1
        )
    )
    (input)
}

// "e" exponent
// exponent = ["+"/"-"] 1*DIGIT
#[rustfmt::skip]
fn e_exponent(input: &str) -> JResult<&str, &str> {
    recognize(
        tuple((
            charx('e'),
            opt(one_of("+-")),
            digit1
        ))
    )
    (input)
}

// A helper function for converting string -> Value::Float,
// and mapping to the right error type
#[rustfmt::skip]
fn parse_float(s: &str) -> Result<Value, CowParseError> {
    match s.parse::<f64>() {
        Ok(fl) => Ok(Value::Float(fl)),
        Err(_) => Err(parse_error(MalformedFloat, s)),
    }
}

// A helper function for converting RawInt -> Value::Xint,
// and mapping to the right error type
fn parse_int(raw: RawInt) -> Result<Value, CowParseError> {
    // Note: the string slice doesn't contain the '-' character, so we
    // need to handle that ourselves.
    let posint = u64::from_str_radix(raw.slice, raw.base)
        .map_err(|_| parse_error(MalformedInteger, raw.slice))?;

    if raw.neg {
        // i64 has a larger positive range than negative range, so if we
        // survive the conversion to i64 then unary `-` must succeed.
        let negint: i64 = try_into_int(posint, raw.slice)?;
        Ok(Value::Nint(-negint))
    } else {
        Ok(Value::Uint(posint))
    }
}

// A rough approximation of nom's offset() function, allowing us to
// do something like nom's recognize() without losing the inner types.
fn offset(whole: &str, part: &str) -> usize {
    part.as_ptr() as usize - whole.as_ptr() as usize
}

// This is similar to nom's `recognize` function.
// The difference is that it doesn't throw away the inner parser's result;
// it returns a tuple (slice, result) so you can have both.
fn recognizer<'a, O, E, F>(
    mut parser: F,
) -> impl FnMut(&'a str) -> nom::IResult<&'a str, (&'a str, O), E>
where
    E: nom::error::ParseError<&'a str>,
    F: FnMut(&'a str) -> nom::IResult<&'a str, O, E>,
{
    move |input: &'a str| match parser(input) {
        Ok((i, output)) => {
            let index = offset(input, i);
            let output_slice = &input[..index];
            let output_tuple = (output_slice, output);
            Ok((i, output_tuple))
        }
        Err(e) => Err(e),
    }
}

// int ["." fraction] ["e" exponent ]
// (must have at least one of the latter two to be a float)
fn float_or_int(input: &str) -> JResult<&str, Value> {
    let f = recognizer(tuple((int, opt(dot_fraction), opt(e_exponent))));
    map_res_fail(f, |(recognized, output)| {
        let (firstint, frac, exp) = output;

        // If we picked up a '.' or 'e' we are parsing a float; if neither we
        // are parsing an integer.
        let dot_or_e = frac.is_some() || exp.is_some();
        if dot_or_e {
            parse_float(recognized)
        } else {
            parse_int(firstint)
        }
    })(input)
}

// bytes = [bsqual] %x27 *BCHAR %x27
// BCHAR = %x20-26 / %x28-5B / %x5D-10FFFD / SESC / CRLF
// bsqual = "h" / "b64"
//
// Byte strings can come in 3 forms:
// - 'abc\n' <= utf8 string interpreted as a byte string (and escaping is allowed)
// - h'1234' or h'12 34' <= hex (base16) bytes; all whitespace is ignored.
// - 'SGVsbG8gd29ybGQ=' <= base64 encoded byte string.
// Also, byte strings can be concatenated, i.e. 'Hello ' 'world' == 'Hello world'.
// See the RFC for details.

#[rustfmt::skip]
fn is_unescaped_bchar(c: char) -> bool {
    let ranges = [
        (0x20 ..= 0x26),
        (0x28 ..= 0x5B),
        (0x5D ..= 0x7E),
        (0x80 ..= 0x10FFD),
    ];
    let cv = c as u32;

    ranges.iter().any(|range| range.contains(&cv))
}

// One or more unescaped byte-string characters
#[rustfmt::skip]
fn unescaped_bchar(input: &str) -> JResult<&str, &str> {
    take_while1(is_unescaped_bchar)
    (input)
}

// FIXME: should also permit included CRLF
// Zero or more byte-string characters
#[rustfmt::skip]
fn bchar(input: &str) -> JResult<&str, &str> {
    recognize(
        many0(
            alt((
                unescaped_bchar,
                sesc
            ))
        )
    )
    (input)
}

// This is basically identical to `text_literal` except that
// it uses single-quotes.
#[rustfmt::skip]
fn bytestring_utf8(input: &str) -> JResult<&str, String> {
    let f = delimited(
        tag("'"),
        bchar,
        tag("'")
    );

    map_res_fail(f, |s| {
        unescape(s).map_err(|_| parse_error(MalformedText, s) )
    })
    (input)
}

// This doesn't check that only the right characters are used;
// that will be done later during parsing of the string.
#[rustfmt::skip]
fn bytestring_hex(input: &str) -> JResult<&str, &str> {
    delimited(
        tag("h'"),
        bchar,
        tag("\'")
    )(input)
}

// This doesn't check that only the right characters are used;
// that will be done later during parsing of the string.
#[rustfmt::skip]
fn bytestring_base64(input: &str) -> JResult<&str, &str> {
    delimited(
        tag("b64'"),
        bchar,
        charx('\'')
    )(input)
}

// A helper function for parsing hex digits to bytes, while
// ignoring whitespace and mapping to the right error type.
fn parse_hex(s: &str) -> Result<Vec<u8>, CowParseError> {
    // strip whitespace
    // FIXME: this consumes more chars than the RFC says we should.
    let s: String = s.chars().filter(|c| !c.is_ascii_whitespace()).collect();

    hex::decode(&s).map_err(|_| parse_error(MalformedHex, s))
}

#[rustfmt::skip]
fn bytestring(input: &str) -> JResult<&str, Vec<u8>> {
    alt((
        map(bytestring_utf8, |s| s.as_bytes().into()),
        map_res_fail(bytestring_hex, parse_hex),
        map_res_fail(bytestring_base64, |s| {
            base64::decode_config(s, base64::URL_SAFE).map_err(|_| {
                parse_error(MalformedBase64, s)
            })
        }),
    ))
    (input)
}

// text = %x22 *SCHAR %x22
// SCHAR = %x20-21 / %x23-5B / %x5D-7E / %x80-10FFFD / SESC
// SESC = "\" (%x20-7E / %x80-10FFFD)

#[rustfmt::skip]
fn is_unescaped_schar(c: char) -> bool {
    let ranges = [
        (0x20 ..= 0x21),
        (0x23 ..= 0x5B),
        (0x5D ..= 0x7E),
        (0x80 ..= 0x10FFD),
    ];
    let cv = c as u32;

    ranges.iter().any(|range| range.contains(&cv))
}

// One or more unescaped text characters
#[rustfmt::skip]
fn unescaped_schar(input: &str) -> JResult<&str, &str> {
    take_while1(is_unescaped_schar)
    (input)
}

// A single escaped character
#[rustfmt::skip]
fn sesc(input: &str) -> JResult<&str, &str> {
    // FIXME: allow only (%x20-7E / %x80-10FFFD)
    preceded(charx('\\'), recognize(anychar))
    (input)
}

// Zero or more text characters
#[rustfmt::skip]
fn schar(input: &str) -> JResult<&str, &str> {
    recognize(
        many0(
            alt((
                unescaped_schar,
                sesc
            ))
        )
    )
    (input)
}

#[rustfmt::skip]
fn text_literal(input: &str) -> JResult<&str, String> {
    let f = delimited(
        charx('"'),
        schar,
        charx('"')
    );

    map_res_fail(f, |s| {
        unescape(s).map_err(|_| parse_error(MalformedText, s) )
    })
    (input)
}

// value = number / text / bytes
// number = hexfloat / (int ["." fraction] ["e" exponent ])
#[rustfmt::skip]
fn value(input: &str) -> JResult<&str, Value> {
    alt((
        float_or_int,
        map(text_literal, Value::Text),
        map(bytestring, Value::Bytes),
    ))(input)
}

// Match the ": Y" part of an "X : Y" memberkey.
#[rustfmt::skip]
fn grpent_memberkey_tail(input: &str) -> JResult<&str, Type> {
    preceded(
        pair(
            tag(":"),
            ws,
        ),
        ty,
    )(input)
}

// A helper function for grpent_member for assembling the Member
// from the key and value when using the "X:Y" syntax.
//
// The key must be a Type2::Value or Type2::Typename or this function
// will panic.
fn assemble_basic_member(key: Type1, value: Type) -> Result<Member, CowParseError<'static>> {
    let member_key = match key {
        Type1::Simple(Type2::Value(v)) => MemberKeyVal::Value(v),
        Type1::Simple(Type2::Typename(s)) => {
            // Because we used the "key:value" syntax, the typename may
            // only be a plain string, without generic parameters.
            // If generic parameters are desired, the "key=>value" syntax
            // would be required (and we would not have arrived here).

            if !s.generic_args.is_empty() {
                return Err(parse_error(Unparseable, "Bareword with generic arguments"));
            }
            MemberKeyVal::Bareword(s.name)
        }
        _ => panic!("assemble_basic_member wrong key type"),
    };
    Ok(Member {
        key: Some(MemberKey {
            val: member_key,
            cut: true,
        }),
        value,
    })
}

// grpent = [occur S] [memberkey S] type
//        / [occur S] groupname [genericarg]  ; preempted by above
//        / [occur S] "(" S group S ")"
//
// memberkey = type1 S ["^" S] "=>"
//           / bareword S ":"
//           / value S ":"
//
// Parsing the memberkey syntax is tricky, because it's easy to fall into
// 2^N iterations due to backtracking.
//
// The problem arises when we see something like "[[[[ int ]]]]".
// If we first search for a memberkey, we can go on an adventure trying
// many different partial matches of the first memberkey type
// ( type1 S ["^" S] "=>" ) before discarding that work because there is
// no trailing "=>".
//
// The root problem is that "int" will match both the with-memberkey and
// without-memberkey grammars, because it's both a type and a type1.
// if we start wrapping it in N sets of array-brackets or map-brackets,
// there are 2^N possible ways for that type1 to be parsed before we
// discover the missing "=>".
//
// The solution is to change the parser to match this equivalent grammar:
//
// grpent = [occur S] member_type1
//        / [occur S] groupname [genericarg]  ; preempted by above
//        / [occur S] "(" S group S ")"
//
// member_type1 = bareword S ":" type
//              / value S ":" type
//              / type1 S ["^" S] "=>" type
//
// In this grammar, it's easier to see that the bareword and value nodes
// also match the type1 node.  We avoid backtracking by matching the
// type1 node first, and then peeking at which variant it is to see if
// the ":" syntax is allowed.  If it's an id (bareword) or value, then
// we attempt to match that syntax before trying the others.
//
#[rustfmt::skip]
fn grpent_member(input: &str) -> JResult<&str, Member> {

    // The leading Type1 is required for this parser.
    let (input, first_type1) = terminated(type1, ws)(input)?;

    // If the type1 matches is a plain value or typename (aka id), then we may
    // be looking at a memberkey followed by a ":".  That syntax isn't allowed
    // for other Type1 patterns.

    match first_type1 {
        Type1::Simple(Type2::Value(_)) |
        Type1::Simple(Type2::Typename(_)) => {
            // Next, try to match ":" ws ty
            if let Ok((input, tail_type)) = grpent_memberkey_tail(input) {
                let member = assemble_basic_member(first_type1, tail_type);
                match member {
                    Ok(member) => return Ok((input, member)),
                    Err(e) => return Err(nom::Err::Failure(e)),
                }

            }
        }
        // "X:Y" isn't allowed when X is some other Type1 variant.
        _ => {}
    }

    // grpent with memberkey followed by "=>" (with optional cut symbol "^")
    // should return a Member, with key: Some(MemberKey{val, cut})
    // and value: Type

    if let Ok((input, matched_tuple)) = tuple((
        opt(terminated(tag("^"), ws)),
        tag("=>"),
        ws,
        ty,
    ))(input) {
        let (cut, _, _, val) = matched_tuple;
        let member = Member {
            key: Some(MemberKey {
                val: MemberKeyVal::Type1(first_type1),
                cut: cut.is_some(),
            }),
            value: val,
        };
        return Ok((input, member));
    }

    // grpent without memberkey
    // should return a Member, with key: None and value: Type containing Vec<Type1>
    // This is a lot like the fn `ty` but we will concatenate this with first_type1

    if let Ok((input, mut ty1s)) = many0(
        preceded(
            delimited(ws, tag("/"), ws),
            type1
        )
    )(input) {
        // insert the first type1 (from the top of this function)
        ty1s.insert(0, first_type1);
        let member = Member {
            key: None,
            value: Type(ty1s),
        };
        return Ok((input, member));
    }

    Err(nom::Err::Error(parse_error(Unparseable, "grpent_member")))
}

#[rustfmt::skip]
fn grpent_parens(input: &str) -> JResult<&str, Group> {
    delimited(
        charx('('),
        delimited(
            ws,
            group,
            ws,
        ),
        charx(')')
    )(input)
}

#[rustfmt::skip]
fn grpent_val(input: &str) -> JResult<&str, GrpEntVal> {
    alt((
        map(grpent_member, GrpEntVal::Member),
        map(ident, |s| GrpEntVal::Groupname(s.into())),
        map(grpent_parens, GrpEntVal::Parenthesized),
    ))
    (input)
}

// A helper function that does u64->usize conversion, returning
// CowParseError(MalformedInteger) on failure.
fn try_into_int<T, U>(x: T, source: &str) -> Result<U, CowParseError>
where
    U: TryFrom<T>,
{
    <U>::try_from(x).map_err(|_| parse_error(MalformedInteger, source))
}

// occur = [uint] "*" [uint]
//       / "+"
//       / "?"
#[rustfmt::skip]
fn occur_star(input: &str) -> JResult<&str, Occur> {
    let f = tuple((
        opt(uint_u64),
        tag("*"),
        opt(uint_u64),
    ));
    // FIXME: it's really not the parser's business to be inventing an upper
    // limit here.  Plus, the use of usize::MAX is kind of gross.
    // The parser should leave these as Option and leave it to others to
    // decide what to do with that.
    map_res(f, |tup| -> Result<Occur, CowParseError> {
        if tup.0.is_none() && tup.2.is_none() {
            Ok(Occur::ZeroOrMore)
        } else {
            let lower: usize = match tup.0 {
                Some(n) => try_into_int(n, input)?,
                None => 0,
            };
            let upper: usize = match tup.2 {
                Some(n) => try_into_int(n, input)?,
                None => std::usize::MAX,
            };
            Ok(Occur::Numbered(lower, upper))
        }
    })
    (input)
}

#[rustfmt::skip]
fn occur(input: &str) -> JResult<&str, Occur> {
    alt((
        occur_star,
        valuex(Occur::OneOrMore, tag("+")),
        valuex(Occur::Optional, tag("?"))
    ))
    (input)
}

// grpent = [occur S] [memberkey S] type
//        / [occur S] groupname [genericarg]  ; preempted by above
//        / [occur S] "(" S group S ")"

#[rustfmt::skip]
fn grpent(input: &str) -> JResult<&str, GrpEnt> {
    let f = pair(
        opt(terminated(occur, ws)),
        grpent_val
    );
    map(f, |(occur, val)| GrpEnt{ occur, val } )
    (input)
}

// grpchoice = zero-or-more "grpent optional-comma"
#[rustfmt::skip]
fn grpchoice(input: &str) -> JResult<&str, GrpChoice> {
    let f = many0(
        terminated(grpent, optcom)
    );
    map(f, GrpChoice)
    (input)
}

// group = grpchoice *(S "//" S grpchoice)
#[rustfmt::skip]
fn group(input: &str) -> JResult<&str, Group> {

    // It would have been great to write this as
    //  separated_list1(
    //      tag("//"),
    //      grpchoice)
    // but separated_list1 returns an error if the
    // list-item succeeds on "", which grpchoice does.

    let f = pair(
        grpchoice,
        many0(preceded(
            delimited(
                ws,
                tag("//"),
                ws,
            ),
            grpchoice
        ))
    );

    map(f, |(first, mut rest)| {
        // Build a new vector containing all the grpchoice elements.
        let mut gcs = vec![first];
        gcs.append(&mut rest);
        Group(gcs)
    })(input)
}

// "(" S type S ")"
#[rustfmt::skip]
fn type2_parens(input: &str) -> JResult<&str, Type> {
    delimited(
        charx('('),
        delimited(
            ws,
            ty,
            ws,
        ),
        charx(')')
    )(input)
}

// "{" S group S "}"
#[rustfmt::skip]
fn type2_map(input: &str) -> JResult<&str, Group> {
    delimited(
        charx('{'),
        delimited(
            ws,
            group,
            ws,
        ),
        charx('}')
    )(input)
}

// "[" S group S "]"
#[rustfmt::skip]
fn type2_array(input: &str) -> JResult<&str, Group> {
    delimited(
        charx('['),
        delimited(
            ws,
            group,
            ws,
        ),
        charx(']')
    )(input)
}

// "~" S typename [genericarg]
#[rustfmt::skip]
fn type2_unwrap(input: &str) -> JResult<&str, NameGeneric> {
    preceded(
        tag("~"),
        preceded(
            ws,
            name_generic
        )
    )
    (input)
}

// "&" S groupname [genericarg]
// I call the & operator "choice-ify". RFC 8610 (see 2.2.2.2) doesn't say
// what that operator should be called, and "group choice" already means
// something different.
#[rustfmt::skip]
fn type2_choiceify(input: &str) -> JResult<&str, NameGeneric> {
    preceded(
        tag("&"),
        preceded(
            ws,
            name_generic
        )
    )
    (input)
}

// "&" S "(" S group S ")"
#[rustfmt::skip]
fn type2_choiceify_inline(input: &str) -> JResult<&str, Group> {
    preceded(
        tag("&"),
        preceded(
            ws,
            delimited(
                charx('('),
                delimited(
                    ws,
                    group,
                    ws,
                ),
                charx(')')
            )
        )
    )
    (input)
}

// type2 = value
//       / typename [genericarg]
//       / "(" S type S ")"
//       / "{" S group S "}"
//       / "[" S group S "]"
//       / "~" S typename [genericarg]
//       / "&" S "(" S group S ")"
//       / "&" S groupname [genericarg]
//       / "#" "6" ["." uint] "(" S type S ")"
//       / "#" DIGIT ["." uint]
//       / "#"
#[rustfmt::skip]
fn type2(input: &str) -> JResult<&str, Type2> {
    alt((
        map(value, Type2::Value),
        map(name_generic, Type2::Typename),
        map(type2_parens, Type2::Parethesized),
        map(type2_map, Type2::Map),
        map(type2_array, Type2::Array),
        map(type2_unwrap, Type2::Unwrap),
        map(type2_choiceify_inline, Type2::ChoiceifyInline),
        map(type2_choiceify, Type2::Choiceify),
    ))
    (input)
}

// Returns the string containing the control identifier
#[rustfmt::skip]
fn control_op(input: &str) -> JResult<&str, &str> {
    preceded(
        tag("."),
        ident
    )
    (input)
}

// Returns the range or control operator string
// (either ".." or "..." or the control identifier)
#[rustfmt::skip]
fn range_or_control_op(input: &str) -> JResult<&str, (&str, Type2)> {
    pair(
        alt((
            tag("..."),
            tag(".."),
            control_op
        )),
        preceded(
            ws,
            type2
        )
    )
    (input)
}

// type1 = type2 [S (rangeop / ctlop) S type2]
#[rustfmt::skip]
fn type1(input: &str) -> JResult<&str, Type1> {
    let f = pair(
        type2,
        opt(
            preceded(
                ws,
                range_or_control_op
            )
        )
    );
    map(f, |ty1| match ty1 {
        (ty2, None) => Type1::Simple(ty2),
        (start, Some(("..", end))) => Type1::Range(TypeRange {
            start,
            inclusive: true,
            end,
        }),
        (start, Some(("...", end))) => Type1::Range(TypeRange {
            start,
            inclusive: false,
            end,
        }),
        (target, Some((op, arg))) => Type1::Control(TypeControl {
            target,
            op: op.into(),
            arg,
        }),
    })
    (input)
}

// type = type1 [ / type1 ... ]  (skipping over type1 for now)
#[rustfmt::skip]
fn ty(input: &str) -> JResult<&str, Type> {
    let f = separated_list1(
        delimited(ws, tag("/"), ws),
        type1
    );
    map(f, Type)
    (input)
}

// rule = typename [genericparm] S assignt S type
//      / groupname [genericparm] S assigng S grpent
// Note that the first one ends with "type", while
// the second one ends with "group".
// So "foo = (bar)" will be forced down the second path.
//
// The most efficient parsing would be
// 1. name [genericparm] ws
// 2. = type
//    = grpent
//    /= type
//    //= grpent
//

// This is the right side of a rule: one of:
//     assignt S type
//     assigng S grpent
#[rustfmt::skip]
fn rule_val(input: &str) -> JResult<&str, RuleVal> {
    let f = separated_pair(
        tag("="),
        ws,
        alt((
            map(ty, RuleVal::AssignType),
            map(grpent, RuleVal::AssignGroup)
        ))
    );
    // We're just throwing away the operator for now, but we'll need it
    // later when we implement extend operators /= //=
    map(f, |(_op, val)| val )
    (input)
}

// genericparm = "<" S id S *("," S id S ) ">"
#[rustfmt::skip]
fn generic_parm(input: &str) -> JResult<&str, Vec<&str>> {
    delimited(
        pair(tag("<"), ws),
        separated_list1(
            pair(tag(","), ws),
            terminated(ident, ws)),
        tag(">"),
    )(input)
}

// genericarg = "<" S type1 S *("," S type1 S ) ">"
#[rustfmt::skip]
fn generic_arg(input: &str) -> JResult<&str, Vec<Type1>> {
    delimited(
        pair(tag("<"), ws),
        separated_list1(
            pair(tag(","), ws),
            terminated(type1, ws)),
        tag(">"),
    )(input)
}

// A type or group name, followed by optional generic arguments.
#[rustfmt::skip]
fn name_generic(input: &str) -> JResult<&str, NameGeneric> {
    let f = pair(ident, opt(generic_arg));
    map(f, |(name, generic)| {
        // Replace None with empty Vec.
        let generic_args = generic.unwrap_or_default();
        NameGeneric {
            name: name.to_string(),
            generic_args,
        }
    })
    (input)
}

#[rustfmt::skip]
fn rule(input: &str) -> JResult<&str, Rule> {
    let f = separated_pair(
        pair(
            ident,
            opt(generic_parm)
        ),
        ws,
        rule_val
    );
    map(f, |((name, gp), val)| Rule {
        name: name.into(),
        // turn Vec<&str> into Vec<String>
        generic_parms: gp.unwrap_or_default().drain(..).map(|s| s.to_string()).collect(),
        val,
    })(input)
}

// cddl = S 1*(rule S)
#[rustfmt::skip]
fn cddl(input: &str) -> JResult<&str, Cddl> {
    let f = preceded(ws,
        many1(
            terminated(rule, ws)
        )
    );
    map(f, |r| Cddl{rules: r})
    (input)
}

#[rustfmt::skip]
fn cddl_slice(input: &str) -> JResult<&str, CddlSlice> {
    let f = preceded(ws,
        many1(
            terminated(
                map(recognizer(rule), |(s, r)| {
                    (r, s.to_string())
                }),
                ws
            )
        )
    );
    map(f, |r| CddlSlice{rules: r})
    (input)
}

/// The main entry point for parsing CDDL text.
///
/// If successful, it will return a [`Cddl`] instance containing all the rules
/// from the input text.
///
/// # Examples
/// ```
/// use cddl_cat::parse_cddl;
///
/// let input = "map = { name: tstr }";
/// assert!(parse_cddl(input).is_ok());
/// ```
///
pub fn parse_cddl(input: &str) -> Result<Cddl, ParseError> {
    let result = all_consuming(cddl)(input)?;
    Ok(result.1)
}

/// An entry point for parsing CDDL text, preserving rule strings
///
/// This operates exactly like [`parse_cddl`], but stores a copy of the rule's
/// original CDDL text.
pub fn slice_parse_cddl(input: &str) -> Result<CddlSlice, ParseError> {
    let result = all_consuming(cddl_slice)(input)?;
    Ok(result.1)
}

// Useful utilities for testing the parser.
#[cfg(test)]
#[macro_use]
mod test_utils {
    use super::*;

    // Generate a Vec<String> the same way we would generate a Vec<&str>.
    macro_rules! vec_strings {
        ($($str:expr),*) => ({
            vec![$(String::from($str),)*] as Vec<String>
        });
    }

    // Given a string, generate a NameGeneric containing a type name.
    impl From<&str> for NameGeneric {
        fn from(s: &str) -> Self {
            NameGeneric {
                name: s.to_string(),
                generic_args: Vec::new(),
            }
        }
    }

    // Given a string, generate a Type2 containing a type name.
    impl From<&str> for Type2 {
        fn from(s: &str) -> Self {
            Type2::Typename(s.into())
        }
    }

    // Given a string, generate a Type1 containing a type name.
    impl From<&str> for Type1 {
        fn from(s: &str) -> Self {
            Type1::Simple(Type2::Typename(s.into()))
        }
    }

    // Given a Value, generate a Type1.
    impl From<Value> for Type1 {
        fn from(v: Value) -> Self {
            Type1::Simple(Type2::Value(v))
        }
    }

    // Given a string, generate a Member containing a no-key value.
    impl From<&str> for Member {
        fn from(s: &str) -> Self {
            Member {
                key: None,
                value: s.into(),
            }
        }
    }

    // Given a Value, generate a Member containing a no-key value.
    impl From<Value> for Member {
        fn from(v: Value) -> Self {
            Member {
                key: None,
                value: Type1::from(v).into(),
            }
        }
    }

    // Given a string, generate a GrpEnt containing that type name.
    impl From<&str> for GrpEnt {
        fn from(s: &str) -> GrpEnt {
            GrpEnt {
                occur: None,
                val: GrpEntVal::Member(s.into()),
            }
        }
    }

    // A trait for generating literals.
    pub trait CreateLiteral {
        fn literal(self) -> Value;
    }

    // Create a literal string.
    impl CreateLiteral for &str {
        fn literal(self) -> Value {
            Value::Text(self.to_string())
        }
    }

    // Create a literal integer.
    impl CreateLiteral for i64 {
        fn literal(self) -> Value {
            if self >= 0 {
                Value::Uint(self as u64)
            } else {
                Value::Nint(self)
            }
        }
    }

    pub fn bareword(s: &str) -> MemberKeyVal {
        MemberKeyVal::Bareword(s.into())
    }

    impl From<Value> for Type2 {
        fn from(x: Value) -> Type2 {
            Type2::Value(x)
        }
    }

    // Given a Value (a literal), generate a MemberKeyVal.
    impl From<Value> for MemberKeyVal {
        fn from(k: Value) -> MemberKeyVal {
            MemberKeyVal::Value(k)
        }
    }

    // Given a Type1, generate a MemberKeyVal.
    impl From<Type1> for MemberKeyVal {
        fn from(t: Type1) -> MemberKeyVal {
            MemberKeyVal::Type1(t)
        }
    }

    // Given a string, generate a MemberKeyVal (treating it as a type name).
    impl From<&str> for MemberKeyVal {
        fn from(k: &str) -> MemberKeyVal {
            MemberKeyVal::Type1(k.into())
        }
    }

    #[derive(Copy, Clone)]
    pub enum MemberCut {
        Cut,
        NoCut,
    }
    pub use MemberCut::*;

    impl From<MemberCut> for bool {
        fn from(c: MemberCut) -> bool {
            match c {
                Cut => true,
                NoCut => false,
            }
        }
    }

    pub fn kv_member<K, V>(k: K, v: V, cut: MemberCut) -> Member
    where
        K: Into<MemberKeyVal>,
        V: Into<Type1>,
    {
        let v: Type1 = v.into();
        Member {
            key: Some(MemberKey {
                val: k.into(),
                cut: cut.into(),
            }),
            value: v.into(),
        }
    }

    pub fn kv<K, V>(k: K, v: V, cut: MemberCut) -> GrpEnt
    where
        K: Into<MemberKeyVal>,
        V: Into<Type1>,
    {
        GrpEnt {
            occur: None,
            val: GrpEntVal::Member(kv_member(k, v, cut)),
        }
    }

    pub fn gen_group<T: Into<GrpEnt>>(mut members: Vec<T>) -> Group {
        // convert the members into individual GrpEnt structs
        let grpents: Vec<GrpEnt> = members.drain(..).map(|x| x.into()).collect();
        // construct a Group containing one GrpChoice.
        Group(vec![GrpChoice(grpents)])
    }

    pub fn gen_array<T: Into<GrpEnt>>(members: Vec<T>) -> Type1 {
        Type1::Simple(Type2::Array(gen_group(members)))
    }

    pub fn gen_map<T: Into<GrpEnt>>(members: Vec<T>) -> Type1 {
        Type1::Simple(Type2::Map(gen_group(members)))
    }

    // Generate a single-Type1 Type struct.
    impl From<Type1> for Type {
        fn from(x: Type1) -> Self {
            Type(vec![x])
        }
    }

    // Generate a single-Type1 Type struct from a plain string (as a type name).
    impl From<&str> for Type {
        fn from(s: &str) -> Self {
            Type(vec![Type1::from(s)])
        }
    }

    // Create a type name with generic arguments
    pub fn generic<T: Into<Type1>>(name: &str, mut generic_args: Vec<T>) -> Type1 {
        Type1::Simple(Type2::Typename(NameGeneric {
            name: name.to_string(),
            generic_args: generic_args.drain(..).map(|x| x.into()).collect(),
        }))
    }
}

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

    #[test]
    fn test_whitespace() {
        let cddl = "  ; a comment\n        \r\n; another;;;comment\n  ";
        let (remainder, _result) = ws(cddl).unwrap();
        assert_eq!(remainder, "");
    }

    #[test]
    fn test_ident() {
        assert_eq!(ident("a"), Ok(("", "a")));
        assert_eq!(ident("a1"), Ok(("", "a1")));
        assert_eq!(ident("a.1"), Ok(("", "a.1")));
        assert_eq!(ident("a1."), Ok((".", "a1")));
        assert_eq!(ident("@a1"), Ok(("", "@a1")));
        assert_eq!(ident("a..b"), Ok(("", "a..b")));
        assert!(ident("1a").is_err());
    }

    #[test]
    fn test_uint() {
        assert_eq!(uint_u64("999"), Ok(("", 999)));
        assert_eq!(uint_u64("0"), Ok(("", 0)));
        assert_eq!(uint_u64("0x100"), Ok(("", 256)));
        assert_eq!(uint_u64("0b101"), Ok(("", 5)));
        // We're not supposed to parse leading zeros.
        assert_eq!(uint_u64("00"), Ok(("0", 0)));
    }

    #[test]
    fn test_float_or_int() {
        assert_eq!(float_or_int("0.0"), Ok(("", Value::Float(0.0))));
        assert_eq!(float_or_int("1e99"), Ok(("", Value::Float(1e99))));
        assert_eq!(float_or_int("-1e-99"), Ok(("", Value::Float(-1e-99))));
        assert_eq!(float_or_int("123"), Ok(("", Value::Uint(123))));
        assert_eq!(float_or_int("-123"), Ok(("", Value::Nint(-123))));
        assert_eq!(float_or_int("1e"), Ok(("e", Value::Uint(1))));
        assert_eq!(float_or_int("1."), Ok((".", Value::Uint(1))));
        assert!(float_or_int("abc").is_err());

        assert_eq!(float_or_int("0x100"), Ok(("", Value::Uint(256))));
        assert_eq!(float_or_int("0b101"), Ok(("", Value::Uint(5))));
        // We're not supposed to parse leading zeros.
        assert_eq!(float_or_int("00"), Ok(("0", Value::Uint(0))));

        assert_eq!(float_or_int("-0x100"), Ok(("", Value::Nint(-256))));
        assert_eq!(float_or_int("-0b101"), Ok(("", Value::Nint(-5))));

        // While this is allowed in the CDDL grammar, it doesn't make logical sense
        // so we want to return an error.
        assert!(float_or_int("0b1e99").is_err());
        assert!(float_or_int("0b1.1").is_err());
    }

    #[test]
    fn test_bytestring() {
        let result1 = bytestring("'abc'");
        let result = format!("{:?}", result1);
        assert_eq!(result, r#"Ok(("", [97, 98, 99]))"#);

        // Same thing, in hex format
        assert_eq!(result1, bytestring("h'61 62 63'"));
        assert_eq!(result1, bytestring("h' 6 1626 3  '"));

        // Same thing, in base64 format
        assert_eq!(result1, bytestring("b64'YWJj'"));

        // bytestring in UTF-8 with escapes
        assert_eq!(bytestring(r#"'a\nb'"#), Ok(("", "a\nb".into())));
        assert_eq!(bytestring(r#"'\uD834\uDD1E'"#), Ok(("", "𝄞".into())));

        // Non-text bytes
        let result2 = vec![0u8, 0xFF, 1, 0x7F];
        assert_eq!(Ok(("", result2.clone())), bytestring("h'00FF017f'"));
        assert_eq!(Ok(("", result2)), bytestring("b64'AP8Bfw=='"));

        // Empty inputs
        assert_eq!(Ok(("", vec![])), bytestring("h''"));
        assert_eq!(Ok(("", vec![])), bytestring("b64''"));

        fn fail_kind(e: nom::Err<CowParseError>) -> ErrorKind {
            match e {
                nom::Err::Failure(e) => e.kind,
                _ => panic!("expected nom::err::Failure, got {:?}", e),
            }
        }

        // Bad hex character
        assert_eq!(
            fail_kind(bytestring("h'0g1234'").unwrap_err()),
            ErrorKind::MalformedHex
        );

        // Bad base64 character "!"
        assert_eq!(
            fail_kind(bytestring("b64'AP!Bfw=='").unwrap_err()),
            ErrorKind::MalformedBase64
        );

        // wrong flavor of base64: CDDL requires the "base64url" encoding.
        assert_eq!(
            // base64 encoding of FBEF00 using the wrong encoder.
            fail_kind(bytestring("b64'++8A'").unwrap_err()),
            ErrorKind::MalformedBase64
        );
        assert_eq!(
            // base64 encoding of FFFFFF using the wrong encoder.
            fail_kind(bytestring("b64'////'").unwrap_err()),
            ErrorKind::MalformedBase64
        );
    }

    #[test]
    fn test_text() {
        assert!(is_unescaped_schar('A'));
        assert!(is_unescaped_schar('の'));
        assert!(is_unescaped_schar(std::char::from_u32(0x10FF0).unwrap()));
        assert!(!is_unescaped_schar(0x7F as char));

        assert_eq!(unescaped_schar("Aの"), Ok(("", "Aの")));

        assert_eq!(sesc(r#"\n"#), Ok(("", "n")));
        assert_eq!(sesc(r#"\nn"#), Ok(("n", "n")));
        assert_eq!(sesc(r#"\の"#), Ok(("", "の")));

        // FIXME: sesc is allowing characters it shouldn't.
        // assert_eq!(sesc("\\\x7F"), Ok(("\\\x7F", "")));

        assert_eq!(schar(r#"Ab! \c の \\"#), Ok(("", r#"Ab! \c の \\"#)));
        assert_eq!(schar(r#"a\nb"#), Ok(("", r#"a\nb"#)));
        assert_eq!(schar("a\nb"), Ok(("\nb", "a")));

        assert!(text_literal("\"a\nb").is_err());
        assert!(text_literal("abc").is_err());

        assert_eq!(text_literal(r#""""#), Ok(("", "".into())));
        assert_eq!(text_literal(r#""a\nb""#), Ok(("", "a\nb".into())));
        assert_eq!(text_literal(r#""\uD834\uDD1E""#), Ok(("", "𝄞".into())));
        assert_eq!(text_literal(r#""の""#), Ok(("", "の".into())));
    }

    #[test]
    fn test_value() {
        assert_eq!(value("123"), Ok(("", Value::Uint(123))));
        assert_eq!(value(r#""abc""#), Ok(("", Value::Text("abc".into()))));
        assert!(value("abc").is_err());
    }

    #[test]
    fn test_member() {
        let result = grpent_member("a:b");
        assert_eq!(
            result.unwrap().1,
            kv_member(MemberKeyVal::Bareword("a".into()), "b", Cut)
        );

        let result = grpent_member("foo");
        assert_eq!(result.unwrap().1, "foo".into());

        let result = grpent_member("a => b");
        assert_eq!(result.unwrap().1, kv_member("a", "b", NoCut));

        let result = grpent_member("42 ^ => b");
        assert_eq!(
            result.unwrap().1,
            kv_member(Type1::from(42.literal()), "b", Cut)
        );

        let result = grpent_member("abc<T> => def");
        assert_eq!(
            result.unwrap().1,
            kv_member(generic("abc", vec!["T"]), "def", NoCut)
        );

        // Generic arguments not allowed with ":"
        grpent_member("abc<T> : def").unwrap_err();
    }

    #[test]
    fn test_grpent_parens() {
        let result = grpent_parens("()");
        assert_eq!(result.unwrap().1, Group(vec![GrpChoice(vec![])]));
    }

    #[test]
    fn test_grpent_val() {
        let result = grpent_val("foo");
        assert_eq!(result.unwrap().1, GrpEntVal::Member("foo".into()));

        let result = grpent_val("17");
        assert_eq!(result.unwrap().1, GrpEntVal::Member(17.literal().into()));
    }

    #[test]
    fn test_occur() {
        assert_eq!(occur("?"), Ok(("", Occur::Optional)));
        assert_eq!(occur("+"), Ok(("", Occur::OneOrMore)));
        assert_eq!(occur("*"), Ok(("", Occur::ZeroOrMore)));
        assert_eq!(occur("*9"), Ok(("", Occur::Numbered(0, 9))));
        assert_eq!(occur("7*"), Ok(("", Occur::Numbered(7, std::usize::MAX))));
        assert_eq!(occur("7*9"), Ok(("", Occur::Numbered(7, 9))));
        assert_eq!(occur("0b100*0x10"), Ok(("", Occur::Numbered(4, 16))));
    }

    #[test]
    fn test_grpent() {
        let result = grpent("foo").unwrap();
        assert_eq!(result.1, "foo".into());

        let result = grpent("foo: bar").unwrap();
        assert_eq!(
            result.1,
            kv(MemberKeyVal::Bareword("foo".into()), "bar", Cut)
        );
    }

    #[test]
    fn test_grpchoice_empty() {
        let result = grpchoice("").unwrap();
        assert_eq!(result.1, GrpChoice(vec![]));
    }

    #[test]
    fn test_group_empty() {
        let result = group("").unwrap();
        assert_eq!(result.1, Group(vec![GrpChoice(vec![])]));
    }

    #[test]
    fn test_type1() {
        let result = type1("1 .. 9");
        assert_eq!(
            result.unwrap().1,
            Type1::Range(TypeRange {
                start: 1.literal().into(),
                end: 9.literal().into(),
                inclusive: true
            })
        );

        let result = type1("0x10 .. 0x1C");
        assert_eq!(
            result.unwrap().1,
            Type1::Range(TypeRange {
                start: 16.literal().into(),
                end: 28.literal().into(),
                inclusive: true
            })
        );

        let result = type1("1 ... 9");
        assert_eq!(
            result.unwrap().1,
            Type1::Range(TypeRange {
                start: 1.literal().into(),
                end: 9.literal().into(),
                inclusive: false
            })
        );

        let result = type1("uint .size 3");
        assert_eq!(
            result.unwrap().1,
            Type1::Control(TypeControl {
                target: "uint".into(),
                op: "size".to_string(),
                arg: 3.literal().into(),
            })
        );

        // RFC8610 2.2.2.1 points out that "min..max" is not a range, but an identifier
        // (because '.' is a valid ident character).
        let result = type2("min..max");
        assert_eq!(result.unwrap().1, "min..max".into());
    }

    #[test]
    fn test_grpchoice() {
        let result = grpchoice("abc").unwrap();
        assert_eq!(result.1, GrpChoice(vec!["abc".into()]));

        let result = grpchoice("abc, def").unwrap();
        assert_eq!(result.1, GrpChoice(vec!["abc".into(), "def".into(),]));
    }

    #[test]
    fn test_generic_parm() {
        assert!(generic_parm("").is_err());

        assert!(generic_parm("<>").is_err());

        let result = generic_parm("<foo>").unwrap();
        assert_eq!(result.1, vec!["foo"]);

        let result = generic_parm("<foo,bar>").unwrap();
        assert_eq!(result.1, vec!["foo", "bar"]);

        let result = generic_parm("< foo , _bar_ >").unwrap();
        assert_eq!(result.1, vec!["foo", "_bar_"]);
    }

    #[test]
    fn test_generic_arg() {
        assert!(generic_arg("").is_err());

        assert!(generic_arg("<>").is_err());

        let result = generic_arg("<foo>").unwrap();
        assert_eq!(result.1, vec!["foo".into()]);

        let result = generic_arg("<foo,bar>").unwrap();
        assert_eq!(result.1, vec!["foo".into(), "bar".into()]);

        let result = generic_arg("< foo , _bar_ >").unwrap();
        assert_eq!(result.1, vec!["foo".into(), "_bar_".into()]);
    }

    #[test]
    fn choiceify() {
        assert_eq!(
            type2("&foo").unwrap().1,
            Type2::Choiceify(NameGeneric {
                name: "foo".into(),
                generic_args: vec![],
            })
        );
        assert_eq!(
            type2("&(a:1)").unwrap().1,
            Type2::ChoiceifyInline(gen_group(vec![kv(bareword("a"), 1.literal(), Cut),]))
        );
    }

    #[test]
    fn test_rule() {
        let result = rule("foo=bar").unwrap().1;

        assert_eq!(
            result,
            Rule {
                name: "foo".into(),
                generic_parms: vec![],
                val: RuleVal::AssignType("bar".into())
            }
        );

        let result = rule("foo=(bar, baz)").unwrap().1;
        assert_eq!(
            result,
            Rule {
                name: "foo".into(),
                generic_parms: vec![],
                val: RuleVal::AssignGroup(GrpEnt {
                    occur: None,
                    val: GrpEntVal::Parenthesized(gen_group(vec!["bar", "baz"])),
                })
            }
        );

        let result = rule("message<t, v> = [t, v]").unwrap().1;
        assert_eq!(
            result,
            Rule {
                name: "message".into(),
                generic_parms: vec_strings!["t", "v"],
                val: RuleVal::AssignType(gen_array(vec!["t", "v"]).into())
            }
        );
    }

    #[test]
    fn test_cddl() {
        let result = parse_cddl("foo = {\"a\": bar,\n b => baz}");

        assert_eq!(
            result.unwrap(),
            Cddl {
                rules: vec![Rule {
                    name: "foo".into(),
                    generic_parms: vec![],
                    val: RuleVal::AssignType(Type(vec![gen_map(vec![
                        kv("a".literal(), "bar", Cut),
                        kv("b", "baz", NoCut)
                    ])]))
                }]
            }
        );
    }

    #[test]
    fn test_cddl_slice() {
        let result = slice_parse_cddl(" foo = { a: tstr } bar = \n[ int ] ").unwrap();
        assert_eq!(result.rules[0].1, "foo = { a: tstr }");
        assert_eq!(result.rules[1].1, "bar = \n[ int ]");
    }

    // FIXME: these are things I discovered while validating cbor.  Move them to their own tests?
    #[test]
    fn test_stuff() {
        parse_cddl("thing = { foo : tstr }").unwrap();
        parse_cddl("bar = (c: int)").unwrap(); // This is a rule containing a group assignment.
        parse_cddl("thing = {agroup empty} agroup = (age: int, name: tstr) empty = ()").unwrap();
        parse_cddl(
            r#"
            address = { delivery }

            delivery = (
            street: tstr, ? "number": uint, city //
            po_box: uint, city //
            per_pickup: true )

            city = (
            name: tstr, zip_code: uint
            )"#,
        )
        .unwrap();
    }

    #[test]
    fn test_errors() {
        let err = parse_cddl("x=9999999999999999999999999999999").unwrap_err();
        assert_eq!(err.kind, MalformedInteger);

        let err = parse_cddl(r#"x="\ud800""#).unwrap_err();
        assert_eq!(err.kind, MalformedText);

        let err = parse_cddl("x=h'61 62 6'").unwrap_err();
        assert_eq!(err.kind, MalformedHex);
    }
}