debian-copyright 0.1.56

A parser for Debian copyright files
Documentation
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
//! License expression parsing for DEP-5 copyright files.
//!
//! License expressions combine license names with `or`, `and`, and `with` operators.
//! `and` binds tighter than `or`. A comma before an operator lowers its precedence,
//! e.g. `A or B, and C` means `(A or B) and C`.
//!
//! The `with` keyword attaches an exception to the preceding license name
//! (e.g. `GPL-2+ with OpenSSL-exception`).
//!
//! License names between operators are taken verbatim, including any spaces,
//! so malformed names such as `Apache 2.0` survive a parse/display round trip
//! rather than being truncated at the first space.

/// An error from the strict expression parsers.
///
/// [`LicenseExpr::parse`] never fails (malformed input degrades to verbatim
/// names); [`LicenseExpr::parse_strict`] and [`LicenseExpr::parse_spdx`]
/// report malformed input with this error instead, so callers can fall back
/// to treating the field as an opaque literal.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ExprParseError {
    message: String,
    input: String,
}

impl ExprParseError {
    fn new(message: impl Into<String>, input: &str) -> Self {
        ExprParseError {
            message: message.into(),
            input: input.to_string(),
        }
    }

    /// The expression that failed to parse.
    pub fn input(&self) -> &str {
        &self.input
    }

    /// What was wrong with it, without the expression itself.
    pub fn message(&self) -> &str {
        &self.message
    }
}

impl std::fmt::Display for ExprParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} in license expression {:?}", self.message, self.input)
    }
}

impl std::error::Error for ExprParseError {}

/// A parsed license expression from a DEP-5 copyright file.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LicenseExpr {
    /// A single license name, e.g. `MIT`.
    Name(String),

    /// A license with an exception, e.g. `GPL-2+ with OpenSSL-exception`.
    WithException(String, String),

    /// All of these licenses apply simultaneously.
    And(Vec<LicenseExpr>),

    /// Any one of these licenses may be chosen.
    Or(Vec<LicenseExpr>),
}

impl LicenseExpr {
    /// Parse a license expression string.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr = LicenseExpr::parse("GPL-2+ or MIT");
    /// assert_eq!(expr, LicenseExpr::Or(vec![
    ///     LicenseExpr::Name("GPL-2+".to_string()),
    ///     LicenseExpr::Name("MIT".to_string()),
    /// ]));
    ///
    /// let expr = LicenseExpr::parse("GPL-2+ with OpenSSL-exception");
    /// assert_eq!(expr, LicenseExpr::WithException(
    ///     "GPL-2+".to_string(),
    ///     "OpenSSL-exception".to_string(),
    /// ));
    /// ```
    pub fn parse(input: &str) -> Self {
        let tokens = tokenize(input);
        if tokens.is_empty() {
            return LicenseExpr::Name(String::new());
        }
        parse_expr(input, &tokens)
    }

    /// Parse a license expression string, rejecting malformed input.
    ///
    /// Unlike [`parse`](Self::parse), which accepts anything (unparseable
    /// constructs survive verbatim inside names), this enforces the DEP-5
    /// short-name grammar: a license name is a single token, operators may
    /// not dangle, and a comma must introduce a lowered `and`/`or`. Callers
    /// use it to detect fields that are not really expressions and treat
    /// them as opaque literals instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// assert!(LicenseExpr::parse_strict("GPL-2+ or MIT").is_ok());
    /// assert!(LicenseExpr::parse_strict("Apache 2.0").is_err());
    /// assert!(LicenseExpr::parse_strict("MIT or").is_err());
    /// ```
    pub fn parse_strict(input: &str) -> Result<Self, ExprParseError> {
        let tokens = tokenize(input);
        StrictParser {
            input,
            tokens: &tokens,
            pos: 0,
        }
        .parse()
    }

    /// Parse an SPDX license expression.
    ///
    /// SPDX expressions use uppercase `AND`/`OR`/`WITH` operators (accepted
    /// case-insensitively here, as found in the wild) and parentheses for
    /// grouping; `WITH` attaches an exception identifier to a single license
    /// id. The resulting tree uses the same [`LicenseExpr`] vocabulary as the
    /// DEP-5 parsers, with leaf names kept verbatim; use
    /// [`map_names`](Self::map_names) to convert them to DEP-5 names.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr = LicenseExpr::parse_spdx("(MIT AND ISC) OR GPL-2.0-only").unwrap();
    /// assert_eq!(expr, LicenseExpr::Or(vec![
    ///     LicenseExpr::And(vec![
    ///         LicenseExpr::Name("MIT".to_string()),
    ///         LicenseExpr::Name("ISC".to_string()),
    ///     ]),
    ///     LicenseExpr::Name("GPL-2.0-only".to_string()),
    /// ]));
    /// ```
    pub fn parse_spdx(input: &str) -> Result<Self, ExprParseError> {
        let tokens = spdx_tokenize(input);
        SpdxParser {
            input,
            tokens: &tokens,
            pos: 0,
        }
        .parse()
    }

    /// Render the expression as an unambiguous SPDX string.
    ///
    /// Compound operands are parenthesised, so the result round-trips
    /// through [`parse_spdx`](Self::parse_spdx) even for nestings DEP-5
    /// cannot express. Leaf names and exceptions are emitted verbatim; use
    /// [`map_names`](Self::map_names) first to convert DEP-5 names to SPDX
    /// identifiers.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr = LicenseExpr::parse_spdx("(MIT AND ISC) OR GPL-2.0-only").unwrap();
    /// assert_eq!(expr.to_spdx_string(), "(MIT AND ISC) OR GPL-2.0-only");
    /// ```
    pub fn to_spdx_string(&self) -> String {
        fn operand(expr: &LicenseExpr) -> String {
            match expr {
                LicenseExpr::And(_) | LicenseExpr::Or(_) => {
                    format!("({})", expr.to_spdx_string())
                }
                _ => expr.to_spdx_string(),
            }
        }
        match self {
            LicenseExpr::Name(n) => n.clone(),
            LicenseExpr::WithException(n, e) => format!("{} WITH {}", n, e),
            LicenseExpr::And(exprs) => exprs.iter().map(operand).collect::<Vec<_>>().join(" AND "),
            LicenseExpr::Or(exprs) => exprs.iter().map(operand).collect::<Vec<_>>().join(" OR "),
        }
    }

    /// Rebuild the expression with every leaf name and exception converted.
    ///
    /// Superseded by [`map_leaves`](Self::map_leaves), which takes one closure
    /// over a whole leaf rather than two interchangeable ones. The two
    /// converters here have the same signature, so passing them in the wrong
    /// order compiles and silently swaps names for exceptions.
    #[deprecated(since = "0.1.55", note = "use LicenseExpr::map_leaves instead")]
    pub fn map_names(
        &self,
        convert_name: &dyn Fn(&str) -> String,
        convert_exception: &dyn Fn(&str) -> String,
    ) -> LicenseExpr {
        self.map_leaves(|leaf| match leaf {
            LicenseExpr::Name(n) => LicenseExpr::Name(convert_name(n)),
            LicenseExpr::WithException(n, e) => {
                LicenseExpr::WithException(convert_name(n), convert_exception(e))
            }
            other => other.clone(),
        })
    }

    /// Rebuild the expression with every leaf converted.
    ///
    /// Structural: the and/or/with shape is untouched, and `convert` is called
    /// on each [`Name`](LicenseExpr::Name) and
    /// [`WithException`](LicenseExpr::WithException) leaf in turn. This is how
    /// an expression moves between vocabularies, e.g. an SPDX-parsed tree into
    /// DEP-5 short names. Passing the leaf whole (rather than a name and an
    /// exception separately) means a conversion sees the license and its
    /// exception together, which matters when the pair maps to a single name.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr = LicenseExpr::parse_spdx("MIT OR GPL-2.0-only").unwrap();
    /// let mapped = expr.map_leaves(|leaf| match leaf {
    ///     LicenseExpr::Name(name) if name == "MIT" => LicenseExpr::Name("Expat".to_string()),
    ///     other => other.clone(),
    /// });
    /// assert_eq!(mapped.to_string(), "Expat or GPL-2.0-only");
    /// ```
    pub fn map_leaves(&self, convert: impl Fn(&LicenseExpr) -> LicenseExpr) -> LicenseExpr {
        fn walk(expr: &LicenseExpr, convert: &impl Fn(&LicenseExpr) -> LicenseExpr) -> LicenseExpr {
            match expr {
                LicenseExpr::Name(_) | LicenseExpr::WithException(..) => convert(expr),
                LicenseExpr::And(exprs) => {
                    LicenseExpr::And(exprs.iter().map(|e| walk(e, convert)).collect())
                }
                LicenseExpr::Or(exprs) => {
                    LicenseExpr::Or(exprs.iter().map(|e| walk(e, convert)).collect())
                }
            }
        }
        walk(self, &convert)
    }

    /// The expression's leaves ([`Name`](LicenseExpr::Name) and
    /// [`WithException`](LicenseExpr::WithException) nodes), in order.
    ///
    /// Unlike [`license_names`](Self::license_names) this keeps the
    /// exception attached to its license, so a caller grouping by "license
    /// as licensed" (where `GPL-2 with an exception` is not `GPL-2`) can key
    /// off each leaf as a whole.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr = LicenseExpr::parse("GPL-2+ with OpenSSL-exception or MIT");
    /// let leaves = expr.leaves();
    /// assert_eq!(leaves.len(), 2);
    /// assert_eq!(leaves[0].to_string(), "GPL-2+ with OpenSSL-exception");
    /// assert_eq!(leaves[1].to_string(), "MIT");
    /// ```
    pub fn leaves(&self) -> Vec<&LicenseExpr> {
        let mut out = Vec::new();
        fn collect<'a>(expr: &'a LicenseExpr, out: &mut Vec<&'a LicenseExpr>) {
            match expr {
                LicenseExpr::Name(_) | LicenseExpr::WithException(..) => out.push(expr),
                LicenseExpr::And(exprs) | LicenseExpr::Or(exprs) => {
                    for expr in exprs {
                        collect(expr, out);
                    }
                }
            }
        }
        collect(self, &mut out);
        out
    }

    /// Returns the individual license names contained in this expression.
    ///
    /// For `WithException` variants, only the license name is returned,
    /// not the exception name.
    pub fn license_names(&self) -> Vec<&str> {
        let mut names = Vec::new();
        self.collect_names(&mut names);
        names
    }

    /// Locate each license name in `input` along with its byte range.
    ///
    /// Returns each license name paired with the half-open byte range it
    /// occupies in `input`. Exception words after `with` are skipped, matching
    /// [`license_names`](Self::license_names). Unlike `license_names`, no
    /// entry is emitted for expressions that contain no name token, since
    /// there is no meaningful range to report.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let input = "GPL-2+ or MIT";
    /// assert_eq!(
    ///     LicenseExpr::name_ranges(input),
    ///     vec![("GPL-2+", 0..6), ("MIT", 10..13)],
    /// );
    ///
    /// let input = "GPL-2+ with OpenSSL-exception or MIT";
    /// assert_eq!(
    ///     LicenseExpr::name_ranges(input),
    ///     vec![("GPL-2+", 0..6), ("MIT", 33..36)],
    /// );
    /// ```
    pub fn name_ranges(input: &str) -> Vec<(&str, std::ops::Range<usize>)> {
        let tokens = tokenize(input);
        let mut out = Vec::new();
        let mut i = 0;
        while i < tokens.len() {
            match &tokens[i].kind {
                TokenKind::Word => {
                    let last = word_run_end(&tokens, i);
                    let range = tokens[i].range.start..tokens[last].range.end;
                    out.push((&input[range.clone()], range));
                    i = last + 1;
                    if matches!(tokens.get(i).map(|t| &t.kind), Some(TokenKind::With)) {
                        i += 1;
                        if matches!(tokens.get(i).map(|t| &t.kind), Some(TokenKind::Word)) {
                            i = word_run_end(&tokens, i) + 1;
                        }
                    }
                }
                _ => {
                    i += 1;
                }
            }
        }
        out
    }

    /// Whether this expression contains a top-level `or`, in which case a
    /// parent operator must be written in its comma-lowered form (`, and` /
    /// `, or`) to keep the intended grouping.
    fn needs_comma_join(&self) -> bool {
        match self {
            LicenseExpr::Or(_) => true,
            LicenseExpr::And(exprs) => exprs.iter().any(|e| e.needs_comma_join()),
            LicenseExpr::Name(_) | LicenseExpr::WithException(..) => false,
        }
    }

    fn collect_names<'a>(&'a self, names: &mut Vec<&'a str>) {
        match self {
            LicenseExpr::Name(n) => names.push(n),
            LicenseExpr::WithException(n, _) => names.push(n),
            LicenseExpr::And(exprs) | LicenseExpr::Or(exprs) => {
                for expr in exprs {
                    expr.collect_names(names);
                }
            }
        }
    }
}

impl std::str::FromStr for LicenseExpr {
    type Err = ExprParseError;

    /// Parse a DEP-5 license expression, rejecting malformed input.
    ///
    /// This is [`LicenseExpr::parse_strict`], not the lenient
    /// [`LicenseExpr::parse`]: a `FromStr` that never fails would make the
    /// `Result` a lie. Reading arbitrary copyright fields, where prose in a
    /// `License` field must survive rather than fail, wants `parse` instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use debian_copyright::LicenseExpr;
    ///
    /// let expr: LicenseExpr = "GPL-2+ or MIT".parse().unwrap();
    /// assert_eq!(expr.license_names(), vec!["GPL-2+", "MIT"]);
    /// assert!("Apache 2.0".parse::<LicenseExpr>().is_err());
    /// ```
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        LicenseExpr::parse_strict(s)
    }
}

impl std::fmt::Display for LicenseExpr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LicenseExpr::Name(n) => f.write_str(n),
            LicenseExpr::WithException(n, e) => write!(f, "{} with {}", n, e),
            LicenseExpr::And(exprs) => {
                // `and` normally binds tighter than `or`, so an operand with
                // a top-level `or` forces the comma-lowered form: `A or B, and C`.
                let sep = if exprs.iter().any(|e| e.needs_comma_join()) {
                    ", and "
                } else {
                    " and "
                };
                for (i, expr) in exprs.iter().enumerate() {
                    if i > 0 {
                        f.write_str(sep)?;
                    }
                    write!(f, "{}", expr)?;
                }
                Ok(())
            }
            LicenseExpr::Or(exprs) => {
                // An operand that itself uses the comma-lowered form needs
                // this `or` comma-lowered too: `A, and B or C, or D`.
                let sep = if exprs.iter().any(|e| e.needs_comma_join()) {
                    ", or "
                } else {
                    " or "
                };
                for (i, expr) in exprs.iter().enumerate() {
                    if i > 0 {
                        f.write_str(sep)?;
                    }
                    write!(f, "{}", expr)?;
                }
                Ok(())
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
enum TokenKind {
    Word,
    Or,
    And,
    With,
    Comma,
}

#[derive(Debug, Clone)]
struct Token {
    kind: TokenKind,
    range: std::ops::Range<usize>,
}

/// Given that `tokens[start]` is a `Word`, return the index of the last token
/// of the name run it begins: consecutive `Word` tokens, where a comma joins
/// the run only when another word follows it. This mirrors how `parse` groups
/// words into names (a comma directly before `and`/`or` is an operator,
/// anywhere else it is preserved as part of the surrounding name).
fn word_run_end(tokens: &[Token], start: usize) -> usize {
    let mut i = start;
    loop {
        let next = i + 1;
        match tokens.get(next).map(|t| &t.kind) {
            Some(TokenKind::Word) => i = next,
            Some(TokenKind::Comma)
                if matches!(tokens.get(next + 1).map(|t| &t.kind), Some(TokenKind::Word)) =>
            {
                i = next + 1;
            }
            _ => break,
        }
    }
    i
}

fn tokenize(input: &str) -> Vec<Token> {
    let mut tokens = Vec::new();
    let mut pos = 0;
    while pos < input.len() {
        let Some(offset) = input[pos..].find(|c: char| !c.is_whitespace()) else {
            break;
        };
        let start = pos + offset;
        pos = match input[start..].find(char::is_whitespace) {
            Some(offset) => start + offset,
            None => input.len(),
        };
        let mut end = pos;
        let mut trailing_comma = false;
        if end > start && input.as_bytes()[end - 1] == b',' {
            trailing_comma = true;
            end -= 1;
        }
        if end > start {
            let word = &input[start..end];
            let kind = if word.eq_ignore_ascii_case("or") {
                TokenKind::Or
            } else if word.eq_ignore_ascii_case("and") {
                TokenKind::And
            } else if word.eq_ignore_ascii_case("with") {
                TokenKind::With
            } else {
                TokenKind::Word
            };
            tokens.push(Token {
                kind,
                range: start..end,
            });
        }
        if trailing_comma {
            tokens.push(Token {
                kind: TokenKind::Comma,
                range: end..end + 1,
            });
        }
    }
    tokens
}

/// Consume consecutive `Word` tokens starting at `*pos` and return the range
/// of `input` they span, or `None` if the token at `*pos` is not a `Word`.
/// The span preserves whatever separated the words in the input.
fn take_words(tokens: &[Token], pos: &mut usize) -> Option<std::ops::Range<usize>> {
    let start = match tokens.get(*pos) {
        Some(Token {
            kind: TokenKind::Word,
            range,
        }) => range.start,
        _ => return None,
    };
    let mut end = tokens[*pos].range.end;
    *pos += 1;
    while let Some(Token {
        kind: TokenKind::Word,
        range,
    }) = tokens.get(*pos)
    {
        end = range.end;
        *pos += 1;
    }
    Some(start..end)
}

/// Parse a single license term: a name optionally followed by `with <exception>`.
/// A name consumes all words until the next `or`, `and`, `with`, comma, or end,
/// so names containing spaces (as found in malformed files) survive parsing.
fn parse_term(input: &str, tokens: &[Token], pos: &mut usize) -> LicenseExpr {
    let name = match take_words(tokens, pos) {
        Some(range) => input[range].to_string(),
        None => return LicenseExpr::Name(String::new()),
    };

    if matches!(tokens.get(*pos).map(|t| &t.kind), Some(TokenKind::With)) {
        *pos += 1;
        let exception = match take_words(tokens, pos) {
            Some(range) => input[range].to_string(),
            None => String::new(),
        };
        LicenseExpr::WithException(name, exception)
    } else {
        LicenseExpr::Name(name)
    }
}

/// Parse a token stream into a `LicenseExpr`.
///
/// Handles comma-lowered precedence by splitting on `, and` / `, or` first,
/// then parsing each segment with normal precedence (`and` > `or`).
fn parse_expr(input: &str, tokens: &[Token]) -> LicenseExpr {
    // Split into segments at comma boundaries (comma + operator = low precedence).
    let mut segments: Vec<(Vec<Token>, Option<TokenKind>)> = Vec::new();
    let mut current: Vec<Token> = Vec::new();

    let mut i = 0;
    while i < tokens.len() {
        if tokens[i].kind == TokenKind::Comma {
            if i + 1 < tokens.len() && matches!(tokens[i + 1].kind, TokenKind::Or | TokenKind::And)
            {
                let op = tokens[i + 1].kind.clone();
                segments.push((std::mem::take(&mut current), Some(op)));
                i += 2;
            } else {
                i += 1;
            }
        } else {
            current.push(tokens[i].clone());
            i += 1;
        }
    }
    if !current.is_empty() {
        segments.push((current, None));
    }

    // A lone comma produces tokens but no segments.
    if segments.is_empty() {
        return LicenseExpr::Name(String::new());
    }

    if segments.len() == 1 {
        return parse_segment(input, &segments[0].0);
    }

    // Group segments by their joining low-precedence operator.
    // Low-precedence `and` binds tighter than low-precedence `or`.
    // Nested same-operator expressions are spliced into their parent so
    // `A, and B and C` and `A and B and C` parse to the same flat tree.
    fn push_and_operand(group: &mut Vec<LicenseExpr>, expr: LicenseExpr) {
        match expr {
            LicenseExpr::And(exprs) => group.extend(exprs),
            other => group.push(other),
        }
    }

    let mut and_groups: Vec<Vec<LicenseExpr>> = vec![Vec::new()];
    push_and_operand(&mut and_groups[0], parse_segment(input, &segments[0].0));

    for i in 1..segments.len() {
        let preceding_op = segments[i - 1].1.as_ref().unwrap_or(&TokenKind::Or);
        if !matches!(preceding_op, TokenKind::And) {
            and_groups.push(Vec::new());
        }
        push_and_operand(
            and_groups.last_mut().unwrap(),
            parse_segment(input, &segments[i].0),
        );
    }

    let mut or_operands = Vec::new();
    for group in and_groups {
        let expr = if group.len() == 1 {
            group.into_iter().next().unwrap()
        } else {
            LicenseExpr::And(group)
        };
        match expr {
            LicenseExpr::Or(exprs) => or_operands.extend(exprs),
            other => or_operands.push(other),
        }
    }

    if or_operands.len() == 1 {
        or_operands.into_iter().next().unwrap()
    } else {
        LicenseExpr::Or(or_operands)
    }
}

/// Parse a segment (no comma-lowered operators) with normal precedence: `and` > `or`.
fn parse_segment(input: &str, tokens: &[Token]) -> LicenseExpr {
    // Split on `or` (lower precedence), then each part on `and`.
    let mut or_groups: Vec<Vec<Token>> = vec![Vec::new()];
    for tok in tokens {
        if tok.kind == TokenKind::Or {
            or_groups.push(Vec::new());
        } else {
            or_groups.last_mut().unwrap().push(tok.clone());
        }
    }

    let or_exprs: Vec<LicenseExpr> = or_groups
        .into_iter()
        .map(|group| {
            let mut and_groups: Vec<Vec<Token>> = vec![Vec::new()];
            for tok in &group {
                if tok.kind == TokenKind::And {
                    and_groups.push(Vec::new());
                } else {
                    and_groups.last_mut().unwrap().push(tok.clone());
                }
            }

            let and_exprs: Vec<LicenseExpr> = and_groups
                .into_iter()
                .map(|toks| {
                    let mut pos = 0;
                    parse_term(input, &toks, &mut pos)
                })
                .collect();

            if and_exprs.len() == 1 {
                and_exprs.into_iter().next().unwrap()
            } else {
                LicenseExpr::And(and_exprs)
            }
        })
        .collect();

    if or_exprs.len() == 1 {
        or_exprs.into_iter().next().unwrap()
    } else {
        LicenseExpr::Or(or_exprs)
    }
}

/// Strict recursive-descent parser for the DEP-5 short-name grammar
/// (copyright-format 1.0 sec 7.2). The comma is a precedence level looser
/// than `or`, applied as a left-associative fold:
///
/// ```text
/// comma-expr := or-expr ( "," ("and"|"or") or-expr )*
/// or-expr    := and-expr ( "or" and-expr )*
/// and-expr   := with-expr ( "and" with-expr )*
/// with-expr  := NAME ( "with" NAME+ )?
/// ```
struct StrictParser<'a> {
    input: &'a str,
    tokens: &'a [Token],
    pos: usize,
}

impl<'a> StrictParser<'a> {
    fn err<T>(&self, message: &str) -> Result<T, ExprParseError> {
        Err(ExprParseError::new(message, self.input))
    }

    fn peek(&self) -> Option<&TokenKind> {
        self.tokens.get(self.pos).map(|t| &t.kind)
    }

    fn parse(mut self) -> Result<LicenseExpr, ExprParseError> {
        if self.tokens.is_empty() {
            return self.err("empty expression");
        }
        let expr = self.comma_expr()?;
        if self.pos != self.tokens.len() {
            return self.err("trailing tokens");
        }
        Ok(expr)
    }

    fn comma_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let mut acc = self.or_expr()?;
        while self.peek() == Some(&TokenKind::Comma) {
            self.pos += 1;
            let op = match self.peek() {
                Some(TokenKind::And) => TokenKind::And,
                Some(TokenKind::Or) => TokenKind::Or,
                _ => return self.err("expected 'and' or 'or' after comma"),
            };
            self.pos += 1;
            let rhs = self.or_expr()?;
            // Splice same-operator operands so the tree matches what the
            // lenient parser builds for the equivalent expression.
            acc = match (op, acc) {
                (TokenKind::And, LicenseExpr::And(mut exprs)) => {
                    exprs.push(rhs);
                    LicenseExpr::And(exprs)
                }
                (TokenKind::And, acc) => LicenseExpr::And(vec![acc, rhs]),
                (_, LicenseExpr::Or(mut exprs)) => {
                    exprs.push(rhs);
                    LicenseExpr::Or(exprs)
                }
                (_, acc) => LicenseExpr::Or(vec![acc, rhs]),
            };
        }
        Ok(acc)
    }

    fn or_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let mut terms = vec![self.and_expr()?];
        while self.peek() == Some(&TokenKind::Or) {
            self.pos += 1;
            terms.push(self.and_expr()?);
        }
        Ok(if terms.len() == 1 {
            terms.pop().unwrap()
        } else {
            LicenseExpr::Or(terms)
        })
    }

    fn and_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let mut terms = vec![self.with_expr()?];
        while self.peek() == Some(&TokenKind::And) {
            self.pos += 1;
            terms.push(self.with_expr()?);
        }
        Ok(if terms.len() == 1 {
            terms.pop().unwrap()
        } else {
            LicenseExpr::And(terms)
        })
    }

    fn with_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let name = self.name()?;
        if self.peek() != Some(&TokenKind::With) {
            return Ok(LicenseExpr::Name(name));
        }
        self.pos += 1;
        // The exception phrase may span several words ("Autoconf exception").
        let start = match self.tokens.get(self.pos) {
            Some(Token {
                kind: TokenKind::Word,
                range,
            }) => range.start,
            _ => return self.err("expected exception after 'with'"),
        };
        let mut end = self.tokens[self.pos].range.end;
        self.pos += 1;
        while let Some(Token {
            kind: TokenKind::Word,
            range,
        }) = self.tokens.get(self.pos)
        {
            end = range.end;
            self.pos += 1;
        }
        Ok(LicenseExpr::WithException(
            name,
            self.input[start..end].to_string(),
        ))
    }

    fn name(&mut self) -> Result<String, ExprParseError> {
        match self.tokens.get(self.pos) {
            Some(Token {
                kind: TokenKind::Word,
                range,
            }) => {
                self.pos += 1;
                // A second consecutive word would be a multi-word name, which
                // the short-name grammar does not allow.
                if let Some(TokenKind::Word) = self.peek() {
                    return self.err("expected operator between license names");
                }
                Ok(self.input[range.clone()].to_string())
            }
            Some(_) => self.err("expected license name"),
            None => self.err("unexpected end of expression"),
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
enum SpdxToken<'a> {
    Id(&'a str),
    And,
    Or,
    With,
    Open,
    Close,
}

fn spdx_tokenize(input: &str) -> Vec<SpdxToken<'_>> {
    fn classify(word: &str) -> SpdxToken<'_> {
        if word.eq_ignore_ascii_case("and") {
            SpdxToken::And
        } else if word.eq_ignore_ascii_case("or") {
            SpdxToken::Or
        } else if word.eq_ignore_ascii_case("with") {
            SpdxToken::With
        } else {
            SpdxToken::Id(word)
        }
    }
    let mut tokens = Vec::new();
    let mut start: Option<usize> = None;
    for (i, c) in input.char_indices() {
        match c {
            '(' | ')' => {
                if let Some(s) = start.take() {
                    tokens.push(classify(&input[s..i]));
                }
                tokens.push(if c == '(' {
                    SpdxToken::Open
                } else {
                    SpdxToken::Close
                });
            }
            c if c.is_whitespace() => {
                if let Some(s) = start.take() {
                    tokens.push(classify(&input[s..i]));
                }
            }
            _ => {
                if start.is_none() {
                    start = Some(i);
                }
            }
        }
    }
    if let Some(s) = start {
        tokens.push(classify(&input[s..]));
    }
    tokens
}

/// Recursive-descent parser for SPDX license expressions:
///
/// ```text
/// or-expr   := and-expr  ( "OR"  and-expr )*
/// and-expr  := with-expr ( "AND" with-expr )*
/// with-expr := atom ( "WITH" exception-id )?
/// atom      := license-id | "(" or-expr ")"
/// ```
struct SpdxParser<'a> {
    input: &'a str,
    tokens: &'a [SpdxToken<'a>],
    pos: usize,
}

impl<'a> SpdxParser<'a> {
    fn err<T>(&self, message: &str) -> Result<T, ExprParseError> {
        Err(ExprParseError::new(message, self.input))
    }

    fn parse(mut self) -> Result<LicenseExpr, ExprParseError> {
        if self.tokens.is_empty() {
            return self.err("empty expression");
        }
        let expr = self.or_expr()?;
        if self.pos != self.tokens.len() {
            return self.err("trailing tokens");
        }
        Ok(expr)
    }

    fn or_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let mut terms = vec![self.and_expr()?];
        while self.tokens.get(self.pos) == Some(&SpdxToken::Or) {
            self.pos += 1;
            terms.push(self.and_expr()?);
        }
        Ok(if terms.len() == 1 {
            terms.pop().unwrap()
        } else {
            LicenseExpr::Or(terms)
        })
    }

    fn and_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let mut terms = vec![self.with_expr()?];
        while self.tokens.get(self.pos) == Some(&SpdxToken::And) {
            self.pos += 1;
            terms.push(self.with_expr()?);
        }
        Ok(if terms.len() == 1 {
            terms.pop().unwrap()
        } else {
            LicenseExpr::And(terms)
        })
    }

    fn with_expr(&mut self) -> Result<LicenseExpr, ExprParseError> {
        let atom = self.atom()?;
        if self.tokens.get(self.pos) != Some(&SpdxToken::With) {
            return Ok(atom);
        }
        self.pos += 1;
        // SPDX: WITH applies to a license id only, not a parenthesised group.
        let LicenseExpr::Name(name) = atom else {
            return self.err("WITH must follow a license id");
        };
        match self.tokens.get(self.pos) {
            Some(SpdxToken::Id(exception)) => {
                self.pos += 1;
                Ok(LicenseExpr::WithException(name, exception.to_string()))
            }
            _ => self.err("expected exception id after WITH"),
        }
    }

    fn atom(&mut self) -> Result<LicenseExpr, ExprParseError> {
        match self.tokens.get(self.pos) {
            Some(SpdxToken::Id(id)) => {
                self.pos += 1;
                Ok(LicenseExpr::Name(id.to_string()))
            }
            Some(SpdxToken::Open) => {
                self.pos += 1;
                let expr = self.or_expr()?;
                if self.tokens.get(self.pos) != Some(&SpdxToken::Close) {
                    return self.err("expected ')'");
                }
                self.pos += 1;
                Ok(expr)
            }
            Some(_) => self.err("expected license id"),
            None => self.err("unexpected end of expression"),
        }
    }
}

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

    #[test]
    fn test_single_name() {
        assert_eq!(LicenseExpr::parse("MIT"), LicenseExpr::Name("MIT".into()));
    }

    #[test]
    fn test_or() {
        assert_eq!(
            LicenseExpr::parse("GPL-2+ or MIT"),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("GPL-2+".into()),
                LicenseExpr::Name("MIT".into()),
            ])
        );
    }

    #[test]
    fn test_and() {
        assert_eq!(
            LicenseExpr::parse("Apache-2.0 and BSD-3-clause"),
            LicenseExpr::And(vec![
                LicenseExpr::Name("Apache-2.0".into()),
                LicenseExpr::Name("BSD-3-clause".into()),
            ])
        );
    }

    #[test]
    fn test_with_exception() {
        assert_eq!(
            LicenseExpr::parse("GPL-2+ with OpenSSL-exception"),
            LicenseExpr::WithException("GPL-2+".into(), "OpenSSL-exception".into())
        );
    }

    #[test]
    fn test_with_multi_word_exception() {
        assert_eq!(
            LicenseExpr::parse("GPL-2+ with Autoconf exception"),
            LicenseExpr::WithException("GPL-2+".into(), "Autoconf exception".into())
        );
    }

    #[test]
    fn test_with_exception_then_or() {
        assert_eq!(
            LicenseExpr::parse("GPL-2+ with OpenSSL-exception or MIT"),
            LicenseExpr::Or(vec![
                LicenseExpr::WithException("GPL-2+".into(), "OpenSSL-exception".into()),
                LicenseExpr::Name("MIT".into()),
            ])
        );
    }

    #[test]
    fn test_and_binds_tighter_than_or() {
        // A or B and C → A or (B and C)
        assert_eq!(
            LicenseExpr::parse("A or B and C"),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("A".into()),
                LicenseExpr::And(vec![
                    LicenseExpr::Name("B".into()),
                    LicenseExpr::Name("C".into()),
                ]),
            ])
        );
    }

    #[test]
    fn test_comma_lowers_precedence() {
        // A or B, and C → (A or B) and C
        assert_eq!(
            LicenseExpr::parse("A or B, and C"),
            LicenseExpr::And(vec![
                LicenseExpr::Or(vec![
                    LicenseExpr::Name("A".into()),
                    LicenseExpr::Name("B".into()),
                ]),
                LicenseExpr::Name("C".into()),
            ])
        );
    }

    #[test]
    fn test_case_insensitive_operators() {
        assert_eq!(
            LicenseExpr::parse("GPL-2+ OR MIT"),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("GPL-2+".into()),
                LicenseExpr::Name("MIT".into()),
            ])
        );
    }

    #[test]
    fn test_license_names() {
        let expr = LicenseExpr::parse("GPL-2+ or MIT and BSD-3-clause");
        assert_eq!(expr.license_names(), vec!["GPL-2+", "MIT", "BSD-3-clause"]);
    }

    #[test]
    fn test_license_names_with_exception() {
        let expr = LicenseExpr::parse("GPL-2+ with OpenSSL-exception or MIT");
        assert_eq!(expr.license_names(), vec!["GPL-2+", "MIT"]);
    }

    #[test]
    fn test_display_round_trip_simple() {
        let input = "GPL-2+ or MIT";
        let expr = LicenseExpr::parse(input);
        assert_eq!(expr.to_string(), input);
    }

    #[test]
    fn test_display_with_exception() {
        let input = "GPL-2+ with OpenSSL-exception";
        let expr = LicenseExpr::parse(input);
        assert_eq!(expr.to_string(), input);
    }

    #[test]
    fn test_name_ranges_simple() {
        let input = "MIT";
        assert_eq!(LicenseExpr::name_ranges(input), vec![("MIT", 0..3)]);
    }

    #[test]
    fn test_name_ranges_or() {
        let input = "GPL-2+ or MIT";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("GPL-2+", 0..6), ("MIT", 10..13)],
        );
    }

    #[test]
    fn test_name_ranges_and() {
        let input = "Apache-2.0 and BSD-3-clause";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("Apache-2.0", 0..10), ("BSD-3-clause", 15..27)],
        );
    }

    #[test]
    fn test_name_ranges_with_exception() {
        let input = "GPL-2+ with OpenSSL-exception or MIT";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("GPL-2+", 0..6), ("MIT", 33..36)],
        );
    }

    #[test]
    fn test_name_ranges_multi_word_exception() {
        let input = "GPL-2+ with Autoconf exception or MIT";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("GPL-2+", 0..6), ("MIT", 34..37)],
        );
    }

    #[test]
    fn test_name_ranges_comma_lowered() {
        let input = "A or B, and C";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("A", 0..1), ("B", 5..6), ("C", 12..13)],
        );
    }

    #[test]
    fn test_name_ranges_empty() {
        assert_eq!(LicenseExpr::name_ranges(""), Vec::<(&str, _)>::new());
        assert_eq!(LicenseExpr::name_ranges("   "), Vec::<(&str, _)>::new());
    }

    #[test]
    fn test_name_ranges_matches_license_names() {
        let cases = [
            "GPL-2+ or MIT",
            "Apache-2.0 and BSD-3-clause",
            "GPL-2+ with OpenSSL-exception or MIT",
            "A or B, and C",
            "GPL-1+ or Artistic or Perl",
        ];
        for input in cases {
            let from_expr: Vec<String> = LicenseExpr::parse(input)
                .license_names()
                .into_iter()
                .map(str::to_owned)
                .collect();
            let from_ranges: Vec<String> = LicenseExpr::name_ranges(input)
                .into_iter()
                .map(|(n, _)| n.to_owned())
                .collect();
            assert_eq!(from_ranges, from_expr, "mismatch for input {input:?}");
        }
    }

    #[test]
    fn test_multi_word_name() {
        assert_eq!(
            LicenseExpr::parse("Apache 2.0"),
            LicenseExpr::Name("Apache 2.0".into())
        );
    }

    #[test]
    fn test_multi_word_name_in_or() {
        assert_eq!(
            LicenseExpr::parse("Creative Commons Attribution 3.0 or MIT"),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("Creative Commons Attribution 3.0".into()),
                LicenseExpr::Name("MIT".into()),
            ])
        );
    }

    #[test]
    fn test_multi_word_name_with_exception() {
        assert_eq!(
            LicenseExpr::parse("Apache 2.0 with LLVM exception"),
            LicenseExpr::WithException("Apache 2.0".into(), "LLVM exception".into())
        );
    }

    #[test]
    fn test_name_ranges_multi_word_name() {
        let input = "Apache 2.0 or MIT";
        assert_eq!(
            LicenseExpr::name_ranges(input),
            vec![("Apache 2.0", 0..10), ("MIT", 14..17)],
        );
    }

    #[test]
    fn test_non_ascii_name() {
        assert_eq!(
            LicenseExpr::parse("Ràndom"),
            LicenseExpr::Name("Ràndom".into())
        );
    }

    #[test]
    fn test_display_comma_lowered_round_trip() {
        let input = "A or B, and C";
        let expr = LicenseExpr::parse(input);
        assert_eq!(expr.to_string(), input);
        assert_eq!(LicenseExpr::parse(&expr.to_string()), expr);
    }

    #[test]
    fn test_display_comma_lowered_or_round_trip() {
        let input = "A, and B or C, or D";
        let expr = LicenseExpr::parse(input);
        assert_eq!(expr.to_string(), input);
        assert_eq!(LicenseExpr::parse(&expr.to_string()), expr);
    }

    #[test]
    fn test_stray_comma_preserved_in_name() {
        assert_eq!(
            LicenseExpr::parse("A, B, and C"),
            LicenseExpr::And(vec![
                LicenseExpr::Name("A, B".into()),
                LicenseExpr::Name("C".into()),
            ])
        );
    }

    #[test]
    fn test_lone_comma() {
        assert_eq!(LicenseExpr::parse(","), LicenseExpr::Name(String::new()));
    }

    #[test]
    fn test_parse_strict_valid() {
        for input in [
            "MIT",
            "GPL-2+ or MIT",
            "Apache-2.0 and BSD-3-clause",
            "GPL-2+ with OpenSSL-exception",
            "GPL-2+ with Autoconf exception",
            "A or B, and C",
            "A, and B or C, or D",
            "GPL-1+ or Artistic or Perl",
        ] {
            let strict = LicenseExpr::parse_strict(input)
                .unwrap_or_else(|e| panic!("strict parse failed for {input:?}: {e}"));
            assert_eq!(
                strict,
                LicenseExpr::parse(input),
                "tree mismatch for {input:?}"
            );
        }
    }

    #[test]
    fn test_parse_strict_rejects_malformed() {
        for input in [
            "",
            "   ",
            "Apache 2.0",
            "MIT or",
            "or MIT",
            "MIT or or GPL-2",
            "MIT with",
            "MIT, GPL-2",
            "A, B, and C",
            ",",
        ] {
            assert!(
                LicenseExpr::parse_strict(input).is_err(),
                "strict parse accepted {input:?}"
            );
        }
    }

    #[test]
    fn test_parse_spdx_simple() {
        assert_eq!(
            LicenseExpr::parse_spdx("MIT OR Apache-2.0").unwrap(),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("MIT".into()),
                LicenseExpr::Name("Apache-2.0".into()),
            ])
        );
    }

    #[test]
    fn test_parse_spdx_case_insensitive_operators() {
        assert_eq!(
            LicenseExpr::parse_spdx("MIT or Apache-2.0").unwrap(),
            LicenseExpr::parse_spdx("MIT OR Apache-2.0").unwrap(),
        );
    }

    #[test]
    fn test_parse_spdx_with() {
        assert_eq!(
            LicenseExpr::parse_spdx("GPL-2.0-only WITH Classpath-exception-2.0").unwrap(),
            LicenseExpr::WithException("GPL-2.0-only".into(), "Classpath-exception-2.0".into())
        );
    }

    #[test]
    fn test_parse_spdx_parens_and_precedence() {
        // Parens override the AND-binds-tighter default.
        assert_eq!(
            LicenseExpr::parse_spdx("MIT AND (ISC OR GPL-2.0-only)").unwrap(),
            LicenseExpr::And(vec![
                LicenseExpr::Name("MIT".into()),
                LicenseExpr::Or(vec![
                    LicenseExpr::Name("ISC".into()),
                    LicenseExpr::Name("GPL-2.0-only".into()),
                ]),
            ])
        );
        assert_eq!(
            LicenseExpr::parse_spdx("MIT AND ISC OR GPL-2.0-only").unwrap(),
            LicenseExpr::Or(vec![
                LicenseExpr::And(vec![
                    LicenseExpr::Name("MIT".into()),
                    LicenseExpr::Name("ISC".into()),
                ]),
                LicenseExpr::Name("GPL-2.0-only".into()),
            ])
        );
    }

    #[test]
    fn test_parse_spdx_rejects_malformed() {
        for input in [
            "",
            "MIT OR",
            "(MIT",
            "MIT)",
            "MIT WITH",
            "(MIT AND ISC) WITH X",
        ] {
            assert!(
                LicenseExpr::parse_spdx(input).is_err(),
                "SPDX parse accepted {input:?}"
            );
        }
    }

    #[test]
    fn test_to_spdx_string_round_trip() {
        for input in [
            "MIT",
            "MIT OR Apache-2.0",
            "(MIT AND ISC) OR GPL-2.0-only",
            "GPL-2.0-only WITH Classpath-exception-2.0",
            "MIT AND (ISC OR GPL-2.0-only)",
        ] {
            let expr = LicenseExpr::parse_spdx(input).unwrap();
            assert_eq!(
                LicenseExpr::parse_spdx(&expr.to_spdx_string()).unwrap(),
                expr,
                "round trip failed for {input:?}"
            );
        }
    }

    #[allow(deprecated)]
    #[test]
    fn test_map_names() {
        let expr =
            LicenseExpr::parse_spdx("MIT OR GPL-2.0-only WITH Classpath-exception-2.0").unwrap();
        let mapped = expr.map_names(
            &|name| match name {
                "MIT" => "Expat".to_string(),
                "GPL-2.0-only" => "GPL-2".to_string(),
                other => other.to_string(),
            },
            &|exception| match exception {
                "Classpath-exception-2.0" => "ClassPath exception".to_string(),
                other => other.to_string(),
            },
        );
        assert_eq!(
            mapped.to_string(),
            "Expat or GPL-2 with ClassPath exception"
        );
    }

    #[test]
    fn test_map_leaves() {
        let expr =
            LicenseExpr::parse_spdx("MIT OR GPL-2.0-only WITH Classpath-exception-2.0").unwrap();
        let mapped = expr.map_leaves(|leaf| match leaf {
            LicenseExpr::Name(name) if name == "MIT" => LicenseExpr::Name("Expat".to_string()),
            LicenseExpr::WithException(name, exception)
                if name == "GPL-2.0-only" && exception == "Classpath-exception-2.0" =>
            {
                LicenseExpr::WithException("GPL-2".to_string(), "ClassPath exception".to_string())
            }
            other => other.clone(),
        });
        assert_eq!(
            mapped.to_string(),
            "Expat or GPL-2 with ClassPath exception"
        );
    }

    #[test]
    fn test_map_leaves_preserves_structure() {
        let expr = LicenseExpr::parse("A or B, and C");
        let mapped = expr.map_leaves(|leaf| leaf.clone());
        assert_eq!(mapped, expr);
    }

    #[test]
    fn test_map_leaves_sees_license_and_exception_together() {
        // A leaf whose license and exception together map to one DEP-5 name;
        // the deprecated map_names could not express this.
        let expr = LicenseExpr::parse_spdx("GPL-2.0-only WITH Font-exception-2.0").unwrap();
        let mapped = expr.map_leaves(|leaf| match leaf {
            LicenseExpr::WithException(name, exception)
                if name == "GPL-2.0-only" && exception == "Font-exception-2.0" =>
            {
                LicenseExpr::Name("GPL-2-with-font-exception".to_string())
            }
            other => other.clone(),
        });
        assert_eq!(
            mapped,
            LicenseExpr::Name("GPL-2-with-font-exception".into())
        );
    }

    #[test]
    fn test_from_str() {
        let expr: LicenseExpr = "GPL-2+ or MIT".parse().unwrap();
        assert_eq!(expr, LicenseExpr::parse_strict("GPL-2+ or MIT").unwrap());
        assert!("Apache 2.0".parse::<LicenseExpr>().is_err());
    }

    #[test]
    fn test_expr_parse_error_accessors() {
        let err = LicenseExpr::parse_strict("MIT or").unwrap_err();
        assert_eq!(err.input(), "MIT or");
        assert_eq!(err.message(), "unexpected end of expression");
        assert_eq!(
            err.to_string(),
            r#"unexpected end of expression in license expression "MIT or""#
        );
    }

    #[test]
    fn test_leaves() {
        let expr = LicenseExpr::parse("A and B with C exception or D");
        let leaves: Vec<String> = expr.leaves().iter().map(|l| l.to_string()).collect();
        assert_eq!(leaves, vec!["A", "B with C exception", "D"]);
    }

    #[test]
    fn test_three_way_or() {
        assert_eq!(
            LicenseExpr::parse("GPL-1+ or Artistic or Perl"),
            LicenseExpr::Or(vec![
                LicenseExpr::Name("GPL-1+".into()),
                LicenseExpr::Name("Artistic".into()),
                LicenseExpr::Name("Perl".into()),
            ])
        );
    }
}