tiger_lib/
validator.rs

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
use std::borrow::Borrow;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Bound, RangeBounds};
use std::str::FromStr;

use crate::block::{Block, BlockItem, Comparator, Eq::*, Field, BV};
use crate::context::ScopeContext;
use crate::date::Date;
use crate::effect::validate_effect_internal;
use crate::everything::Everything;
use crate::helpers::{dup_assign_error, TigerHashSet};
use crate::item::Item;
use crate::lowercase::Lowercase;
#[cfg(feature = "ck3")]
use crate::report::fatal;
use crate::report::{report, ErrorKey, Severity};
use crate::scopes::Scopes;
#[cfg(any(feature = "ck3", feature = "vic3"))]
use crate::script_value::validate_script_value_no_breakdown;
use crate::script_value::{validate_bv, validate_script_value};
use crate::token::Token;
use crate::tooltipped::Tooltipped;
use crate::trigger::{validate_target, validate_target_ok_this, validate_trigger_internal};
use crate::validate::ListType;

pub use self::value_validator::ValueValidator;

mod value_validator;

pub type Builder = dyn Fn(&Token) -> ScopeContext;

/// A helper enum for providing scope contexts to field validation functions.
pub enum FieldScopeContext<'a> {
    Full(&'a mut ScopeContext),
    Rooted(Scopes),
    Builder(&'a Builder),
}

impl<'a> From<&'a mut ScopeContext> for FieldScopeContext<'a> {
    fn from(sc: &'a mut ScopeContext) -> Self {
        Self::Full(sc)
    }
}

impl From<Scopes> for FieldScopeContext<'_> {
    fn from(scopes: Scopes) -> Self {
        Self::Rooted(scopes)
    }
}

impl<'a> From<&'a Builder> for FieldScopeContext<'a> {
    fn from(builder: &'a Builder) -> Self {
        Self::Builder(builder)
    }
}

impl FieldScopeContext<'_> {
    fn validate<R, F>(&mut self, key: &Token, validate_fn: F) -> R
    where
        F: FnOnce(&mut ScopeContext) -> R,
    {
        let mut temp;
        let sc = match self {
            FieldScopeContext::Full(sc) => sc,
            FieldScopeContext::Rooted(scopes) => {
                temp = ScopeContext::new(*scopes, key);
                &mut temp
            }
            FieldScopeContext::Builder(builder) => {
                temp = builder(key);
                &mut temp
            }
        };
        validate_fn(sc)
    }
}

/// A validator for one `Block`.
/// The intended usage is that you wrap the `Block` in a validator, then call validation functions on it
/// until you've validated all the possible legitimate contents of the `Block`, and then the `Validator`
/// will warn the user about anything left over when it goes out of scope. This way you don't have to worry
/// about checking for unknown fields yourself.
///
/// The validator is mostly for checking "fields" (`key = value` and `key = { block }` items in the block),
/// but it can validate loose blocks and loose values and comparisons as well.
pub struct Validator<'a> {
    /// The block being validated
    block: &'a Block,
    /// A link to all the loaded and processed CK3 and mod files
    data: &'a Everything,
    /// Fields that have been requested so far
    known_fields: Vec<&'a str>,
    /// Whether loose tokens are expected
    accepted_tokens: bool,
    /// Whether subblocks are expected
    accepted_blocks: bool,
    /// Whether unknown block fields are expected
    accepted_block_fields: bool,
    /// Whether unknown value fields are expected
    accepted_value_fields: bool,
    /// Whether key comparisons should be done case-sensitively
    case_sensitive: bool,
    /// Whether this block can have ?= operators
    allow_questionmark_equals: bool,
    /// Maximum severity of problems reported by this `Validator`. Defaults to `Error`.
    /// This is intended to be set lower by validators for less-important items.
    /// As an exception, `Fatal` severity reports will still always be logged as `Fatal`.
    /// TODO: pass this down to all the helper functions
    max_severity: Severity,
}

impl Debug for Validator<'_> {
    /// Roll our own `Debug` implementation in order to leave out the `data` field.
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        f.debug_struct("Validator")
            .field("block", &self.block)
            .field("known_fields", &self.known_fields)
            .field("accepted_tokens", &self.accepted_tokens)
            .field("accepted_blocks", &self.accepted_blocks)
            .field("accepted_block_fields", &self.accepted_block_fields)
            .field("accepted_value_fields", &self.accepted_value_fields)
            .field("case_sensitive", &self.case_sensitive)
            .field("allow_questionmark_equals", &self.allow_questionmark_equals)
            .field("max_severity", &self.max_severity)
            .finish()
    }
}

impl<'a> Validator<'a> {
    /// Construct a new `Validator` for a [`Block`]. The `data` reference is there to help out some of the convenience
    /// functions, and also to pass along to closures so that you can easily pass independent functions as the closures.
    pub fn new(block: &'a Block, data: &'a Everything) -> Self {
        Validator {
            block,
            data,
            known_fields: Vec::new(),
            accepted_tokens: false,
            accepted_blocks: false,
            accepted_block_fields: false,
            accepted_value_fields: false,
            case_sensitive: true,
            allow_questionmark_equals: false,
            max_severity: Severity::Fatal,
        }
    }

    /// Control whether the fields in this `Block` will be matched case-sensitively or not.
    /// Whether this should be on or off depends on what the game engine allows, which is not always known.
    pub fn set_case_sensitive(&mut self, cs: bool) {
        self.case_sensitive = cs;
    }

    /// Whether this block can contain `?=` as well as `=` for assignments and definitions.
    /// Blocks that allow `?=` are mostly specialized ones such as triggers and effects.
    pub fn set_allow_questionmark_equals(&mut self, allow_questionmark_equals: bool) {
        self.allow_questionmark_equals = allow_questionmark_equals;
    }

    pub fn set_max_severity(&mut self, max_severity: Severity) {
        self.max_severity = max_severity;
    }

    /// Require field `name` to be present in the block, and warn if it isn't there.
    /// Returns true iff the field is present.
    pub fn req_field(&mut self, name: &str) -> bool {
        let found = self.check_key(name);
        if !found {
            let msg = format!("required field `{name}` missing");
            let sev = Severity::Error.at_most(self.max_severity);
            report(ErrorKey::FieldMissing, sev).msg(msg).loc(self.block).push();
        }
        found
    }

    /// Require exactly one of the fields in `names` to be present in the block,
    /// and warn if they are missing or there is more than one.
    /// Returns true iff it found exactly one.
    pub fn req_field_one_of(&mut self, names: &[&str]) -> bool {
        let mut count = 0;
        for name in names {
            if self.check_key(name) {
                count += 1;
            }
        }
        if count != 1 {
            let msg = format!("expected exactly 1 of {}", names.join(", "));
            let key = if count == 0 { ErrorKey::FieldMissing } else { ErrorKey::Validation };
            let sev = Severity::Error.at_most(self.max_severity);
            report(key, sev).msg(msg).loc(self.block).push();
        }
        count == 1
    }

    /// Require field `name` to be present in the block, and warn if it isn't there.
    /// Returns true iff the field is present. Warns at a lower severity than `req_field`.
    pub fn req_field_warn(&mut self, name: &str) -> bool {
        let found = self.check_key(name);
        if !found {
            let msg = format!("required field `{name}` missing");
            let sev = Severity::Warning.at_most(self.max_severity);
            report(ErrorKey::FieldMissing, sev).msg(msg).loc(self.block).push();
        }
        found
    }

    /// Require field `name` to be present in the block, and warn if it isn't there.
    /// Returns true iff the field is present. Warns at [`Severity::Fatal`] level.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn req_field_fatal(&mut self, name: &str) -> bool {
        let found = self.check_key(name);
        if !found {
            let msg = format!("required field `{name}` missing");
            fatal(ErrorKey::FieldMissing).msg(msg).loc(self.block).push();
        }
        found
    }

    /// Require field `name` to not be in the block, and warn if it is found.
    /// The warning will include the output from the `only_for` closure,
    /// which describes where the field *is* expected.
    /// TODO: make lower-severity versions of this function.
    pub fn ban_field<F, S>(&mut self, name: &str, only_for: F)
    where
        F: Fn() -> S,
        S: Borrow<str> + Display,
    {
        let sev = Severity::Error.at_most(self.max_severity);
        self.multi_field_check(name, |key, _| {
            let msg = format!("`{name} = ` is only for {}", only_for());
            report(ErrorKey::Validation, sev).msg(msg).loc(key).push();
        });
    }

    /// Require field `name` to not be in the block. If it is found, warn that it has been replaced by `replaced_by`.
    /// This is used to adapt to and warn about changes in the game engine.
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    pub fn replaced_field(&mut self, name: &str, replaced_by: &str) {
        let sev = Severity::Error.at_most(self.max_severity);
        self.multi_field_check(name, |key, _| {
            let msg = format!("`{name}` has been replaced by {replaced_by}");
            report(ErrorKey::Validation, sev).msg(msg).loc(key).push();
        });
    }

    fn check_key(&mut self, name: &str) -> bool {
        for Field(key, _, _) in self.block.iter_fields() {
            if (self.case_sensitive && key.is(name))
                || (!self.case_sensitive && key.lowercase_is(name))
            {
                self.known_fields.push(key.as_str());
                return true;
            }
        }
        false
    }

    fn field_check<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &BV),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if (self.case_sensitive && key.is(name))
                || (!self.case_sensitive && key.lowercase_is(name))
            {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                f(key, bv);
                found = Some(key);
            }
        }
        found.is_some()
    }

    fn multi_field_check<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &BV),
    {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if (self.case_sensitive && key.is(name))
                || (!self.case_sensitive && key.lowercase_is(name))
            {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                f(key, bv);
                found = true;
            }
        }
        found
    }

    /// Expect field `name`, if present, to be either an assignment (`= value`) or a definition (`= { block }`).
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field(&mut self, name: &str) -> bool {
        self.field_check(name, |_, _| ())
    }

    /// Just like [`Validator::field`], but expects any number of `name` fields in the block.
    pub fn multi_field(&mut self, name: &str) -> bool {
        self.multi_field_check(name, |_, _| ())
    }

    /// Expect field `name`, if present, to be... present.
    /// Expect no more than one `name` field in the block.
    /// Returns the field's `BV` (block or value) if the field is present.
    /// TODO: replace this with a `field_validated` variant.
    pub fn field_any_cmp(&mut self, name: &str) -> Option<&BV> {
        let mut found = None;
        for Field(key, _, bv) in self.block.iter_fields() {
            if (self.case_sensitive && key.is(name))
                || (!self.case_sensitive && key.lowercase_is(name))
            {
                self.known_fields.push(key.as_str());
                if let Some((other, _)) = found {
                    dup_assign_error(key, other);
                }
                found = Some((key, bv));
            }
        }
        if let Some((_, bv)) = found {
            Some(bv)
        } else {
            None
        }
    }

    /// Expect field `name`, if present, to be an assignment (`name = value`).
    /// Expect no more than one `name` field in the block.
    /// Returns the field's value if the field is present.
    pub fn field_value(&mut self, name: &str) -> Option<&Token> {
        let mut found = None;
        let mut result = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if (self.case_sensitive && key.is(name))
                || (!self.case_sensitive && key.lowercase_is(name))
            {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                if let Some(token) = bv.expect_value() {
                    result = Some(token);
                }
                found = Some(key);
            }
        }
        result
    }

    /// Expect field `name`, if present, to be an assignment (`name = value`).
    /// Expect no more than one `name` field in the block.
    /// Runs the validation closure `f(key, vd)` for every matching field.
    /// Returns true iff the field is present.
    pub fn field_validated_value<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, ValueValidator),
    {
        let max_sev = self.max_severity;
        self.field_check(name, |k, bv| {
            if let Some(value) = bv.expect_value() {
                let mut vd = ValueValidator::new(value, self.data);
                vd.set_max_severity(max_sev);
                f(k, vd);
            }
        })
    }

    /// Just like [`Validator::field_validated_value`], but expect any number of `name` fields in the block.
    pub fn multi_field_validated_value<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, ValueValidator),
    {
        let max_sev = self.max_severity;
        self.multi_field_check(name, |k, bv| {
            if let Some(value) = bv.expect_value() {
                let mut vd = ValueValidator::new(value, self.data);
                vd.set_max_severity(max_sev);
                f(k, vd);
            }
        })
    }

    /// Expect field `name`, if present, to be set to the key of an `itype` item the game database.
    /// The item is looked up and must exist.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_item(&mut self, name: &str, itype: Item) -> bool {
        let sev = self.max_severity;
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                self.data.verify_exists_max_sev(itype, token, sev);
            }
        })
    }

    /// Expect field `name`, if present, to be set to the key of an `on_action`.
    /// The action is looked up and must exist.
    /// If it would be useful, validate the action with the given `ScopeContext`.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_action(&mut self, name: &str, sc: &ScopeContext) -> bool {
        let sev = self.max_severity;
        let data = &self.data;
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                self.data.verify_exists_max_sev(Item::OnAction, token, sev);
                if let Some(mut action_sc) = sc.root_for_action(token) {
                    self.data.on_actions.validate_call(token, data, &mut action_sc);
                }
            }
        })
    }

    /// Expect field `name`, if present, to be set to an event id.
    /// The event is looked up and must exist.
    /// If it would be useful, validate the event with the given `ScopeContext`.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_event(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        let sev = self.max_severity;
        let data = &self.data;
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                self.data.verify_exists_max_sev(Item::Event, token, sev);
                self.data.events.check_scope(token, sc);
                if let Some(mut event_sc) = sc.root_for_event(token) {
                    self.data.events.validate_call(token, data, &mut event_sc);
                }
            }
        })
    }
    /// Expect field `name`, if present, to be set to the key of an `itype` item the game database,
    /// or be the empty string.
    /// The item is looked up and must exist.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_item_or_empty(&mut self, name: &str, itype: Item) -> bool {
        let sev = self.max_severity;
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !token.is("") {
                    self.data.verify_exists_max_sev(itype, token, sev);
                }
            }
        })
    }

    /// Expect field `name`, if present, to be a localization key.
    /// The key is looked up and must exist.
    /// The key's localization entry is validated using the given `ScopeContext`.
    ///
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    #[allow(dead_code)]
    pub fn field_localization(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        let sev = self.max_severity;
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                self.data.verify_exists_max_sev(Item::Localization, token, sev);
                self.data.localization.validate_use(token.as_str(), self.data, sc);
            }
        })
    }

    /// Expect field `name`, if present, to be an assignment where the value evaluates to a scope type in `outscopes`.
    ///
    /// The value is evaluated in the scope context `sc`, so for example if the value does `scope:actor` but there is
    /// no named scope "actor" in the scope context, then a warning is emitted.
    /// Also emits a warning if the value is simply "`this`", because that is almost never correct.
    ///
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_target(&mut self, name: &str, sc: &mut ScopeContext, outscopes: Scopes) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                // TODO: pass max_severity here
                validate_target(token, self.data, sc, outscopes);
            }
        })
    }

    /// Returns true iff the field is present.
    /// Just like [`Validator::field_target`], but allows multiple fields.
    #[cfg(feature = "vic3")]
    pub fn multi_field_target(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        outscopes: Scopes,
    ) -> bool {
        self.multi_field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                // TODO: pass max_severity here
                validate_target(token, self.data, sc, outscopes);
            }
        })
    }

    /// Just like [`Validator::field_target`], but allows the value to be simply "`this`".
    /// It is expected to be used judiciously in cases where "`this`" can be correct.
    pub fn field_target_ok_this(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        outscopes: Scopes,
    ) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                // TODO: pass max_severity here
                validate_target_ok_this(token, self.data, sc, outscopes);
            }
        })
    }

    /// This is a combination of [`Validator::field_item`] and [`Validator::field_target`]. If the field is present
    /// and is not a known `itype` item, then it is evaluated as a target.
    /// Returns true iff the field is present.
    pub fn field_item_or_target(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        itype: Item,
        outscopes: Scopes,
    ) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !self.data.item_exists(itype, token.as_str()) {
                    // TODO: pass max_severity here
                    validate_target(token, self.data, sc, outscopes);
                }
            }
        })
    }

    /// This is a combination of [`Validator::field_item`] and [`Validator::field_target_ok_this`].
    /// If the field is present and is not a known `itype` item, then it is evaluated as a target.
    /// Returns true iff the field is present.
    #[allow(dead_code)]
    pub fn field_item_or_target_ok_this(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        itype: Item,
        outscopes: Scopes,
    ) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !self.data.item_exists(itype, token.as_str()) {
                    // TODO: pass max_severity here
                    validate_target_ok_this(token, self.data, sc, outscopes);
                }
            }
        })
    }

    /// Expect field `name`, if present, to be a definition `name = { block }`.
    /// Expect no more than one `name` field.
    /// No other validation is done.
    /// Returns true iff the field is present.
    pub fn field_block(&mut self, name: &str) -> bool {
        self.field_check(name, |_, bv| _ = bv.expect_block())
    }

    /// Expect field `name`, if present, to be `name = yes` or `name = no`.
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_bool(&mut self, name: &str) -> bool {
        let sev = Severity::Error.at_most(self.max_severity);
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !token.is("yes") && !token.is("no") && !token.is("YES") && !token.is("NO") {
                    report(ErrorKey::Validation, sev).msg("expected yes or no").loc(token).push();
                }
            }
        })
    }

    /// Expect field `name`, if present, to be set to an integer.
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_integer(&mut self, name: &str) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                // TODO: pass max_severity here
                token.expect_integer();
            }
        })
    }

    /// Expect field `name`, if present, to be set to an integer within the `range` provided.
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_integer_range<R: RangeBounds<i64>>(&mut self, name: &str, range: R) {
        let sev = Severity::Error.at_most(self.max_severity);
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                // TODO: pass max_severity here
                if let Some(i) = token.expect_integer() {
                    if !range.contains(&i) {
                        let low = match range.start_bound() {
                            Bound::Unbounded => None,
                            Bound::Included(&n) => Some(n),
                            Bound::Excluded(&n) => Some(n + 1),
                        };
                        let high = match range.end_bound() {
                            Bound::Unbounded => None,
                            Bound::Included(&n) => Some(n),
                            Bound::Excluded(&n) => Some(n - 1),
                        };
                        let msg;
                        if low.is_some() && high.is_some() {
                            msg = format!(
                                "should be between {} and {} (inclusive)",
                                low.unwrap(),
                                high.unwrap()
                            );
                        } else if low.is_some() {
                            msg = format!("should be at least {}", low.unwrap());
                        } else if high.is_some() {
                            msg = format!("should be at most {}", high.unwrap());
                        } else {
                            unreachable!(); // could not have failed the contains check
                        }
                        report(ErrorKey::Range, sev).msg(msg).loc(token).push();
                    }
                }
            }
        });
    }

    /// Expect field `name`, if present, to be set to a number with up to 5 decimals.
    /// (5 decimals is the limit accepted by the game engine in most contexts).
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_numeric(&mut self, name: &str) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                token.expect_number();
            }
        })
    }

    /// Expect field `name`, if present, to be set to a number with any number of decimals.
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_precise_numeric(&mut self, name: &str) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                token.expect_precise_number();
            }
        })
    }

    #[cfg(any(feature = "ck3", feature = "vic3"))]
    pub fn field_numeric_range_internal<R: RangeBounds<f64>>(
        &mut self,
        name: &str,
        range: R,
        precise: bool,
    ) {
        let sev = Severity::Error.at_most(self.max_severity);
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                let numeric =
                    if precise { token.expect_precise_number() } else { token.expect_number() };
                if let Some(f) = numeric {
                    if !range.contains(&f) {
                        let low = match range.start_bound() {
                            Bound::Unbounded => None,
                            Bound::Included(f) => Some(format!("{f} (inclusive)")),
                            Bound::Excluded(f) => Some(format!("{f}")),
                        };
                        let high = match range.end_bound() {
                            Bound::Unbounded => None,
                            Bound::Included(f) => Some(format!("{f} (inclusive)")),
                            Bound::Excluded(f) => Some(format!("{f}")),
                        };
                        let msg;
                        if low.is_some() && high.is_some() {
                            msg =
                                format!("should be between {} and {}", low.unwrap(), high.unwrap());
                        } else if low.is_some() {
                            msg = format!("should be at least {}", low.unwrap());
                        } else if high.is_some() {
                            msg = format!("should be at most {}", high.unwrap());
                        } else {
                            unreachable!(); // could not have failed the contains check
                        }
                        report(ErrorKey::Range, sev).msg(msg).loc(token).push();
                    }
                }
            }
        });
    }

    /// Expect field `name`, if present, to be set to a number within the `range` provided.
    /// Accept at most 5 decimals. (5 decimals is the limit accepted by the game engine in most contexts).
    /// Expect no more than one `name` field.
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    pub fn field_numeric_range<R: RangeBounds<f64>>(&mut self, name: &str, range: R) {
        self.field_numeric_range_internal(name, range, false);
    }

    /// Expect field `name`, if present, to be set to a number within the `range` provided.
    /// Expect no more than one `name` field.
    #[cfg(feature = "ck3")]
    pub fn field_precise_numeric_range<R: RangeBounds<f64>>(&mut self, name: &str, range: R) {
        self.field_numeric_range_internal(name, range, true);
    }

    /// Expect field `name`, if present, to be set to a date.
    /// The format of dates is very flexible, from a single number (the year), to a year.month or year.month.day.
    /// No checking is done on the validity of the date as a date (so January 42nd is okay).
    /// Expect no more than one `name` field.
    /// Returns true iff the field is present.
    pub fn field_date(&mut self, name: &str) -> bool {
        let sev = Severity::Error.at_most(self.max_severity);
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if Date::from_str(token.as_str()).is_err() {
                    let msg = "expected date value";
                    report(ErrorKey::Validation, sev).msg(msg).loc(token).push();
                }
            }
        })
    }

    /// Expect field `name`, if present, to be set to a trigger block.
    ///
    /// The scope context may be a full `ScopeContext`, a rooted `Scopes` or a closure that builds
    /// one from the field key token.
    #[allow(dead_code)]
    pub fn field_trigger_full<'b, T>(&mut self, name: &str, fsc: T, tooltipped: Tooltipped) -> bool
    where
        T: Into<FieldScopeContext<'b>>,
    {
        let mut fsc = fsc.into();
        self.field_validated_key_block(name, |key, block, data| {
            fsc.validate(key, |sc| {
                validate_trigger_internal(
                    Lowercase::empty(),
                    false,
                    block,
                    data,
                    sc,
                    tooltipped,
                    false,
                    Severity::Error,
                )
            });
        })
    }

    /// Expect field `name`, if present, to be set to an effect block.
    ///
    /// The scope context may be a full `ScopeContext`, a rooted `Scopes` or a closure that builds
    /// one from the field key token.
    #[allow(dead_code)]
    pub fn field_effect_full<'b, T>(&mut self, name: &str, fsc: T, tooltipped: Tooltipped) -> bool
    where
        T: Into<FieldScopeContext<'b>>,
    {
        let mut fsc = fsc.into();
        self.field_validated_key_block(name, |key, block, data| {
            fsc.validate(key, |sc| {
                let mut vd = Validator::new(block, data);
                validate_effect_internal(
                    Lowercase::empty(),
                    ListType::None,
                    block,
                    data,
                    sc,
                    &mut vd,
                    tooltipped,
                );
            });
        })
    }

    /// Epexect field `name`, if present, to be set to a script value.
    ///
    /// The scope context may be a full `ScopeContext`, a rooted `Scopes` or a closure that builds
    /// one from the field key token.
    ///
    /// If `breakdown` is true, it does not warn if it is an inline script value and the `desc`
    /// fields in it do not contain valid localizations. This is generally used for script values
    /// that will never be shown to the user except in debugging contexts, such as `ai_will_do`.
    #[allow(dead_code)]
    pub fn field_script_value_full<'b, T>(&mut self, name: &str, fsc: T, breakdown: bool) -> bool
    where
        T: Into<FieldScopeContext<'b>>,
    {
        let mut fsc = fsc.into();
        self.field_check(name, |key, bv| {
            fsc.validate(key, |sc| {
                validate_bv(bv, self.data, sc, breakdown);
            });
        })
    }

    /// Expect field `name`, if present, to be set to a script value, either a named one (simply `name = scriptvaluename`)
    /// or an inline one (can be a simple number, or a range `{ min max }`, or a full script value definition with limits
    /// and math).
    ///
    /// The script value is evaluated in the scope context `sc`, so for example if the script value does `scope:actor` but
    /// there is no named scope "actor" in the scope context, then a warning is emitted.
    ///
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_script_value(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        self.field_check(name, |_, bv| {
            // TODO: pass max_severity value down
            validate_script_value(bv, self.data, sc);
        })
    }

    /// Just like [`Validator::field_script_value`], but does not warn if it is an inline script value and the `desc` fields
    /// in it do not contain valid localizations. This is generally used for script values that will never be shown to
    /// the user except in debugging contexts, such as `ai_will_do`.
    #[cfg(not(feature = "imperator"))] // imperator happens not to use; silence dead code warning
    pub fn field_script_value_no_breakdown(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        self.field_check(name, |_, bv| {
            // TODO: pass max_severity value down
            validate_script_value_no_breakdown(bv, self.data, sc);
        })
    }

    /// Just like [`Validator::field_script_value`], but instead of a full [`ScopeContext`] it just gets the scope type
    /// to be used for the `root` of a `ScopeContext` that is made on the spot. This is a convenient way to associate the
    /// `root` type with the key of this field, for clearer warnings. A passed-in `ScopeContext` would have to be associated
    /// with a key that is further away.
    #[cfg(not(feature = "imperator"))]
    pub fn field_script_value_rooted(&mut self, name: &str, scopes: Scopes) -> bool {
        self.field_check(name, |key, bv| {
            let mut sc = ScopeContext::new(scopes, key);
            // TODO: pass max_severity value down
            validate_script_value(bv, self.data, &mut sc);
        })
    }

    /// Just like [`Validator::field_script_value`], but it takes a closure that uses the field key token
    /// as the input to build and output a [`ScopeContext`]. This is a convenient way to associate the `root` type with the key
    /// of this field, for clearer warnings. A passed-in `ScopeContext` would have to be associated with a key that is further away.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn field_script_value_build_sc<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token) -> ScopeContext,
    {
        self.field_check(name, |key, bv| {
            let mut sc = f(key);
            // TODO: pass max_severity value down
            validate_script_value(bv, self.data, &mut sc);
        })
    }

    /// Just like [`Validator::field_script_value`], but it takes a closure that uses the field key token
    /// as the input to build and output a [`ScopeContext`]. This is a convenient way to associate the `root` type with the key
    /// of this field, for clearer warnings. A passed-in `ScopeContext` would have to be associated with a key that is further away.
    ///
    /// Does not warn if it is an inline script value and the `desc` fields in it do not contain valid localizations.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn field_script_value_no_breakdown_build_sc<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token) -> ScopeContext,
    {
        self.field_check(name, |key, bv| {
            let mut sc = f(key);
            // TODO: pass max_severity value down
            validate_script_value_no_breakdown(bv, self.data, &mut sc);
        })
    }

    /// Just like [`Validator::field_script_value`], but it can accept a literal `flag:something` value as well as a script value.
    #[cfg(not(feature = "imperator"))]
    pub fn field_script_value_or_flag(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        self.field_check(name, |_, bv| {
            // TODO: pass max_severity value down
            if let Some(token) = bv.get_value() {
                validate_target(token, self.data, sc, Scopes::Value | Scopes::Bool | Scopes::Flag);
            } else {
                validate_script_value(bv, self.data, sc);
            }
        })
    }

    /// Just like [`Validator::field_script_value`], but it it expects any number of `name` fields.
    pub fn fields_script_value(&mut self, name: &str, sc: &mut ScopeContext) -> bool {
        self.multi_field_check(name, |_, bv| {
            // TODO: pass max_severity value down
            validate_script_value(bv, self.data, sc);
        })
    }

    /// Expect field `name`, if present, to be set to one of the listed strings in `choices`.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_choice(&mut self, name: &str, choices: &[&str]) -> bool {
        let sev = Severity::Error.at_most(self.max_severity);
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !choices.contains(&token.as_str()) {
                    let msg = format!("expected one of {}", choices.join(", "));
                    report(ErrorKey::Choice, sev).msg(msg).loc(token).push();
                }
            }
        })
    }

    /// Just like [`Validator::field_choice`], but expect any number of `name` fields in the block.
    #[allow(dead_code)] // not currently used
    pub fn multi_field_choice(&mut self, name: &str, choices: &[&str]) -> bool {
        let sev = Severity::Error.at_most(self.max_severity);
        self.multi_field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                if !choices.contains(&token.as_str()) {
                    let msg = format!("expected one of {}", choices.join(", "));
                    report(ErrorKey::Choice, sev).msg(msg).loc(token).push();
                }
            }
        })
    }

    /// Expect field `name`, if present, to be of the form `name = { value value value ... }` with any number of values.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_list(&mut self, name: &str) -> bool {
        self.field_validated_list(name, |_, _| ())
    }

    /// Expect field `name`, if present, to be of the form `name = { value value value ... }` with any number of values.
    /// Expect no more than one `name` field in the block.
    /// Calls the closure `f(value, data)` for every value in the list.
    /// Returns true iff the field is present.
    pub fn field_validated_list<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &Everything),
    {
        self.field_check(name, |_, bv| {
            if let Some(block) = bv.expect_block() {
                for token in block.iter_values_warn() {
                    f(token, self.data);
                }
            }
        })
    }

    /// Expect field `name`, if present, to be of the form `name = { value value value ... }` with any number of values.
    /// Expect every value to be an `itype` item in the game database.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    pub fn field_list_items(&mut self, name: &str, item: Item) -> bool {
        let sev = self.max_severity;
        self.field_validated_list(name, |token, data| {
            data.verify_exists_max_sev(item, token, sev);
        })
    }

    /// Expect field `name`, if present, to be of the form `name = { value value value ... }` with any number of values.
    /// Expect every value to be an entry from the choices array.
    /// Expect no more than one `name` field in the block.
    /// Returns true iff the field is present.
    #[allow(dead_code)]
    pub fn field_list_choice(&mut self, name: &str, choices: &[&str]) -> bool {
        let sev = self.max_severity;
        self.field_validated_list(name, |token, _| {
            if !choices.contains(&token.as_str()) {
                let msg = format!("expected one of {}", choices.join(", "));
                report(ErrorKey::Choice, sev).msg(msg).loc(token).push();
            }
        })
    }

    #[cfg(feature = "ck3")]
    pub fn field_icon(&mut self, name: &str, define: &str, suffix: &str) -> bool {
        self.field_check(name, |_, bv| {
            if let Some(token) = bv.expect_value() {
                self.data.verify_icon(define, token, suffix);
            }
        })
    }

    /// Just like [`Validator::field_validated_list`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_validated_list<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &Everything),
    {
        self.multi_field_check(name, |_, bv| {
            if let Some(block) = bv.expect_block() {
                for token in block.iter_values_warn() {
                    f(token, self.data);
                }
            }
        })
    }

    /// Just like [`Validator::field_list_items`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_list_items(&mut self, name: &str, item: Item) -> bool {
        let sev = self.max_severity;
        self.multi_field_validated_list(name, |token, data| {
            data.verify_exists_max_sev(item, token, sev);
        })
    }

    /// Just like [`Validator::field_value`], but expect any number of `name` fields in the block.
    pub fn multi_field_value(&mut self, name: &str) -> Vec<&Token> {
        let mut vec = Vec::new();
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(token) = bv.expect_value() {
                    vec.push(token);
                }
            }
        }
        vec
    }

    /// Just like [`Validator::field_item`], but expect any number of `name` fields in the block.
    pub fn multi_field_item(&mut self, name: &str, itype: Item) {
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(token) = bv.expect_value() {
                    self.data.verify_exists_max_sev(itype, token, self.max_severity);
                }
            }
        }
    }

    /// Just like [`Validator::field_any_cmp`], but expect any number of `name` fields in the block.
    pub fn multi_field_any_cmp(&mut self, name: &str) -> bool {
        let mut found = false;
        for Field(key, _, _) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                found = true;
            }
        }
        found
    }

    /// Expect field `name`, if present, to be either an assignment (`= value`) or a definition (`= { block }`).
    /// Expect no more than one `name` field in the block.
    /// Calls the closure `f(bv, data)` for every matching field.
    /// Returns true iff the field is present.
    pub fn field_validated<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&BV, &Everything),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                f(bv, self.data);
                found = Some(key);
            }
        }
        found.is_some()
    }

    /// Just like [`Validator::field_validated`], but the closure is `f(key, bv, data)`.
    pub fn field_validated_key<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &BV, &Everything),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                f(key, bv, self.data);
                found = Some(key);
            }
        }
        found.is_some()
    }

    /// Just like [`Validator::field_validated`], but the closure is `f(bv, data, sc)` where `sc` is
    /// the passed-in [`ScopeContext`].
    ///
    /// This method is useful for delegating to [`validate_desc`](crate::desc::validate_desc) which takes a bv and a sc.
    pub fn field_validated_sc<F>(&mut self, name: &str, sc: &mut ScopeContext, mut f: F) -> bool
    where
        F: FnMut(&BV, &Everything, &mut ScopeContext),
    {
        self.field_validated(name, |bv, data| f(bv, data, sc))
    }

    /// Just like [`Validator::field_validated_sc`], but instead of a full [`ScopeContext`] it just gets the scope type
    /// to be used for the `root` of a [`ScopeContext`] that is made on the spot. This is a convenient way to associate the
    /// `root` type with the key of this field, for clearer warnings. A passed-in [`ScopeContext`] would have to be associated
    /// with a key that is further away.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn field_validated_rooted<F>(&mut self, name: &str, scopes: Scopes, f: F) -> bool
    where
        F: FnMut(&BV, &Everything, &mut ScopeContext),
    {
        self.field_validated_build_sc(name, |key| ScopeContext::new(scopes, key), f)
    }

    #[cfg(feature = "ck3")]
    pub fn field_validated_build_sc<B, F>(&mut self, name: &str, mut b: B, mut f: F) -> bool
    where
        B: FnMut(&Token) -> ScopeContext,
        F: FnMut(&BV, &Everything, &mut ScopeContext),
    {
        self.field_validated_key(name, |key, bv, data| {
            let mut sc = b(key);
            f(bv, data, &mut sc);
        })
    }

    /// Just like [`Validator::field_validated`], but expect any number of `name` fields in the block.
    pub fn multi_field_validated<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&BV, &Everything),
    {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                f(bv, self.data);
                found = true;
            }
        }
        found
    }

    /// Just like [`Validator::field_validated_key`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_validated_key<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &BV, &Everything),
    {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                f(key, bv, self.data);
                found = true;
            }
        }
        found
    }

    /// Just like [`Validator::field_validated_sc`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_validated_sc<F>(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        mut f: F,
    ) -> bool
    where
        F: FnMut(&BV, &Everything, &mut ScopeContext),
    {
        self.multi_field_validated(name, |b, data| f(b, data, sc))
    }

    /// Just like [`Validator::field_validated_block`], but expect any number of `name` fields in the block.
    pub fn multi_field_validated_block<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Block, &Everything),
    {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    f(block, self.data);
                }
                found = true;
            }
        }
        found
    }

    /// Just like [`Validator::field_validated_block_sc`], but expect any number of `name` fields in the block.
    pub fn multi_field_validated_block_sc<F>(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        mut f: F,
    ) -> bool
    where
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        self.multi_field_validated_block(name, |b, data| f(b, data, sc))
    }

    /// Expect field `name`, if present, to be a definition `name = { block }`.
    /// Expect no more than one `name` field in the block.
    /// Calls the closure `f(block, data)` for every matching field.
    /// Returns true iff the field is present.
    pub fn field_validated_block<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Block, &Everything),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    f(block, self.data);
                }
                found = Some(key);
            }
        }
        found.is_some()
    }

    /// Just like [`Validator::field_validated_block`], but the closure is `f(key, block, data)`.
    pub fn field_validated_key_block<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &Block, &Everything),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    f(key, block, self.data);
                }
                found = Some(key);
            }
        }
        found.is_some()
    }

    pub fn field_validated_block_build_sc<B, F>(&mut self, name: &str, mut b: B, mut f: F) -> bool
    where
        B: FnMut(&Token) -> ScopeContext,
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    let mut sc = b(key);
                    f(block, self.data, &mut sc);
                }
                found = Some(key);
            }
        }
        found.is_some()
    }

    /// Just like [`Validator::field_validated_key_block`], but expect any number of `name` fields in the block.
    pub fn multi_field_validated_key_block<F>(&mut self, name: &str, mut f: F) -> bool
    where
        F: FnMut(&Token, &Block, &Everything),
    {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    f(key, block, self.data);
                }
                found = true;
            }
        }
        found
    }

    /// Just like [`Validator::field_validated_block`], but the closure is `f(block, data, sc)` where sc is the passed-in `ScopeContext`.
    pub fn field_validated_block_sc<F>(
        &mut self,
        name: &str,
        sc: &mut ScopeContext,
        mut f: F,
    ) -> bool
    where
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        self.field_validated_block(name, |b, data| f(b, data, sc))
    }

    /// Just like [`Validator::field_validated_block_sc`], but instead of a full [`ScopeContext`] it just gets the scope type
    /// to be used for the `root` of a [`ScopeContext`] that is made on the spot. This is a convenient way to associate the
    /// `root` type with the key of this field, for clearer warnings. A passed-in [`ScopeContext`] would have to be associated
    /// with a key that is further away.
    pub fn field_validated_block_rooted<F>(&mut self, name: &str, scopes: Scopes, f: F) -> bool
    where
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        self.field_validated_block_build_sc(name, |key| ScopeContext::new(scopes, key), f)
    }

    /// Just like [`Validator::field_validated_block_rooted`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_validated_block_rooted<F>(&mut self, name: &str, scopes: Scopes, mut f: F)
    where
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    let mut sc = ScopeContext::new(scopes, key);
                    f(block, self.data, &mut sc);
                }
            }
        }
    }

    /// Just like [`Validator::field_validated_block_rooted`], but it takes the passed-in `ScopeContext` and associates its
    /// root with this field's key instead of whatever it was associated with before. This is purely to get better warnings.
    ///
    /// TODO: get rid of this in favor of making proper `ScopeContext` to begin with.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn field_validated_block_rerooted<F>(
        &mut self,
        name: &str,
        sc: &ScopeContext,
        scopes: Scopes,
        mut f: F,
    ) -> bool
    where
        F: FnMut(&Block, &Everything, &mut ScopeContext),
    {
        let mut found = None;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                if let Some(other) = found {
                    dup_assign_error(key, other);
                }
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    let mut sc = sc.clone();
                    sc.change_root(scopes, key);
                    f(block, self.data, &mut sc);
                }
                found = Some(key);
            }
        }
        found.is_some()
    }

    /// Just like [`Validator::field_block`], but expect any number of `name` fields in the block.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn multi_field_block(&mut self, name: &str) -> bool {
        let mut found = false;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is(name) {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                bv.expect_block();
                found = true;
            }
        }
        found
    }

    /// Expect this [`Block`] to be a block with exactly `expect` loose values that are integers.
    /// So it's of the form `{ 1 2 3 }`.
    pub fn req_tokens_integers_exactly(&mut self, expect: usize) {
        self.accepted_tokens = true;
        let mut found = 0;
        for token in self.block.iter_values() {
            if token.expect_integer().is_some() {
                found += 1;
            }
        }
        if found != expect {
            let msg = format!("expected {expect} integers");
            let sev = Severity::Error.at_most(self.max_severity);
            report(ErrorKey::Validation, sev).msg(msg).loc(self.block).push();
        }
    }

    /// Expect this [`Block`] to be a block with exactly `expect` loose values that are numeric with up to 5 decimals.
    /// So it's of the form `{ 0.0 0.5 1.0 }`
    pub fn req_tokens_numbers_exactly(&mut self, expect: usize) {
        self.accepted_tokens = true;
        let mut found = 0;
        for token in self.block.iter_values() {
            if token.expect_number().is_some() {
                found += 1;
            }
        }
        if found != expect {
            let msg = format!("expected {expect} numbers");
            let sev = Severity::Error.at_most(self.max_severity);
            report(ErrorKey::Validation, sev).msg(msg).loc(self.block).push();
        }
    }

    /// Expect field `name`, if present, to be of the form `name = { value value value ... }` with exactly `expect` values.
    /// Expect every value to be a number with up to 5 decimals.
    /// Expect no more than one `name` field in the block.
    pub fn field_list_numeric_exactly(&mut self, name: &str, expect: usize) {
        self.field_validated_block(name, |block, data| {
            let mut vd = Validator::new(block, data);
            vd.req_tokens_numbers_exactly(expect);
        });
    }

    /// Like [`Validator::req_tokens_numbers_exactly`] but the numbers can have any number of decimals.
    pub fn req_tokens_precise_numbers_exactly(&mut self, expect: usize) {
        self.accepted_tokens = true;
        let mut found = 0;
        for token in self.block.iter_values() {
            if token.expect_precise_number().is_some() {
                found += 1;
            }
        }
        if found != expect {
            let msg = format!("expected {expect} numbers");
            let sev = Severity::Error.at_most(self.max_severity);
            report(ErrorKey::Validation, sev).msg(msg).loc(self.block).push();
        }
    }

    /// Like [`Validator::field_list_numeric_exactly`] but the numbers can have any number of decimals.
    pub fn field_list_precise_numeric_exactly(&mut self, name: &str, expect: usize) {
        self.field_validated_block(name, |block, data| {
            let mut vd = Validator::new(block, data);
            vd.req_tokens_precise_numbers_exactly(expect);
        });
    }

    /// Like [`Validator::field_list_numeric_exactly`] but the numbers have to be integers.
    pub fn field_list_integers_exactly(&mut self, name: &str, expect: usize) {
        self.field_validated_block(name, |block, data| {
            let mut vd = Validator::new(block, data);
            vd.req_tokens_integers_exactly(expect);
        });
    }

    /// If `name` is present in the block, emit a low-severity warning together with the helpful message `msg`.
    /// This is for harmless but unneeded fields.
    #[cfg(not(feature = "imperator"))]
    pub fn advice_field(&mut self, name: &str, msg: &str) {
        if let Some(key) = self.block.get_key(name) {
            self.known_fields.push(key.as_str());
            let sev = Severity::Untidy.at_most(self.max_severity);
            report(ErrorKey::Unneeded, sev).msg(msg).loc(key).push();
        }
    }

    /// Expect the block to contain any number of loose values (possibly in addition to other things).
    /// Return a vector of those values.
    /// TODO: make this take a closure or make it an iterator.
    pub fn values(&mut self) -> Vec<&Token> {
        self.accepted_tokens = true;
        self.block.iter_values().collect()
    }

    /// Expect the block to contain any number of loose sub-blocks (possibly in addition to other things).
    /// Return a vector of those blocks.
    /// TODO: make callers use `validated_blocks` instead.
    pub fn blocks(&mut self) -> Vec<&Block> {
        self.accepted_blocks = true;
        self.block.iter_blocks().collect()
    }

    /// Expect the block to contain any number of loose sub-blocks (possibly in addition to other things).
    /// Run the closure `f(block, data)` for every sub-block.
    #[cfg(any(feature = "vic3", feature = "imperator"))] // ck3 happens not to use; silence dead code warning
    pub fn validated_blocks<F>(&mut self, mut f: F)
    where
        F: FnMut(&Block, &Everything),
    {
        self.accepted_blocks = true;
        for block in self.block.iter_blocks() {
            f(block, self.data);
        }
    }

    /// Expect the block to contain any number of `key = { block }` fields where the key is an integer.
    /// Return them as a vector of (key, block) pairs.
    /// TODO: make this take a closure.
    pub fn integer_blocks(&mut self) -> Vec<(&Token, &Block)> {
        let mut vec = Vec::new();
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is_integer() {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    vec.push((key, block));
                }
            }
        }
        vec
    }

    /// Expect the block to contain any number of `key = value` fields where the key is an integer.
    /// Return them as a vector of (key, value) pairs.
    /// TODO: make this take a closure.
    pub fn integer_values(&mut self) -> Vec<(&Token, &Token)> {
        let mut vec = Vec::new();
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is_integer() {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(token) = bv.expect_value() {
                    vec.push((key, token));
                }
            }
        }
        vec
    }

    /// Expect the block to contain any number of `key = value` or `key = { block }` fields where the key is an integer.
    /// Return them as a vector of (key, bv) pairs.
    /// TODO: make this take a closure.
    pub fn integer_keys<F: FnMut(&Token, &BV)>(&mut self, mut f: F) {
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is_integer() {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                f(key, bv);
            }
        }
    }

    /// Expect the block to contain any number of `key = value` or `key = { block }` fields where the key is a number with up to 5 decimals.
    /// Return them as a vector of (key, bv) pairs.
    /// TODO: make this take a closure.
    #[cfg(feature = "vic3")] // ck3 happens not to use; silence dead code warning
    pub fn numeric_keys<F: FnMut(&Token, &BV)>(&mut self, mut f: F) {
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if key.is_number() {
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                f(key, bv);
            }
        }
    }

    /// Expect the block to contain any number of `key = { block }` fields where the key is a date.
    /// Run the closure `f(date, block, data)` for every matching field.
    #[cfg(feature = "ck3")] // vic3 happens not to use; silence dead code warning
    pub fn validate_history_blocks<F>(&mut self, mut f: F)
    where
        F: FnMut(Date, &Token, &Block, &Everything),
    {
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if let Ok(date) = Date::try_from(key) {
                key.expect_date(); // warn about unusual date formats
                self.known_fields.push(key.as_str());
                self.expect_eq_qeq(key, *cmp);
                if let Some(block) = bv.expect_block() {
                    f(date, key, block, self.data);
                }
            }
        }
    }

    /// Expect the block to contain any number of `key = value` or `key = { block }`fields
    /// where each key is a unique Item of type itype.
    /// Run the closure `f(key, bv, data)` for every matching block.
    #[allow(dead_code)]
    fn validate_item_key_fields<F>(&mut self, itype: Item, mut f: F)
    where
        F: FnMut(&Token, &BV, &Everything),
    {
        let mut visited_fields = TigerHashSet::default();
        for Field(key, _, bv) in self.block.iter_fields() {
            self.data.verify_exists(itype, key);

            match visited_fields.get(key.as_str()) {
                Some(&duplicate) => dup_assign_error(key, duplicate),
                None => {
                    visited_fields.insert(key);
                }
            }

            self.known_fields.push(key.as_str());

            f(key, bv, self.data);
        }
    }

    /// Expect the block to contain any number of `key = value` fields
    /// where each key is a unique Item of type itype.
    /// Run the closure `f(key, vd)` for every matching block.
    #[allow(dead_code)]
    pub fn validate_item_key_values<F>(&mut self, itype: Item, mut f: F)
    where
        F: FnMut(&Token, ValueValidator),
    {
        let sev = self.max_severity;
        self.validate_item_key_fields(itype, |key, bv, data| {
            if let Some(value) = bv.expect_value() {
                let mut vd = ValueValidator::new(value, data);
                vd.set_max_severity(sev);
                f(key, vd);
            }
        });
    }

    /// Expect the block to contain any number of `key = { block }` fields
    /// where each key is a unique Item of type itype.
    /// Run the closure `f(key, block, data)` for every matching block.
    #[allow(dead_code)]
    pub fn validate_item_key_blocks<F>(&mut self, itype: Item, mut f: F)
    where
        F: FnMut(&Token, &Block, &Everything),
    {
        self.validate_item_key_fields(itype, |key, bv, data| {
            if let Some(block) = bv.expect_block() {
                f(key, block, data);
            }
        });
    }

    /// Expect the block to contain any number of unknown fields (so don't warn about unknown fields anymore).
    /// Loose values and loose sub-blocks will still be warned about.
    /// Run the closure `f(key, bv)` on every matching *unknown* field. Previously-validated fields will be skipped.
    pub fn unknown_fields<F: FnMut(&Token, &BV)>(&mut self, mut f: F) {
        self.accepted_block_fields = true;
        self.accepted_value_fields = true;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            self.expect_eq_qeq(key, *cmp);
            if !self.known_fields.contains(&key.as_str()) {
                f(key, bv);
            }
        }
    }

    /// Expect the block to contain any number of unknown `key = { block }` fields.
    /// Run the closure `f(key, block)` on every matching *unknown* field. Previously-validated fields will be skipped.
    pub fn unknown_block_fields<F: FnMut(&Token, &Block)>(&mut self, mut f: F) {
        self.accepted_block_fields = true;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if let Some(block) = bv.get_block() {
                self.expect_eq_qeq(key, *cmp);
                if !self.known_fields.contains(&key.as_str()) {
                    f(key, block);
                }
            }
        }
    }

    /// Expect the block to contain any number of unknown `key = value` fields.
    /// Run the closure `f(key, value)` on every matching *unknown* field. Previously-validated fields will be skipped.
    pub fn unknown_value_fields<F: FnMut(&Token, &Token)>(&mut self, mut f: F) {
        self.accepted_value_fields = true;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if let Some(value) = bv.get_value() {
                self.expect_eq_qeq(key, *cmp);
                if !self.known_fields.contains(&key.as_str()) {
                    f(key, value);
                }
            }
        }
    }

    /// Like [`Validator::unknown_fields`] but passes the comparator, so that `f` can determine whether it is `=` or `?=`
    /// It still expects the comparator to be one of those two.
    pub fn unknown_fields_cmp<F: FnMut(&Token, Comparator, &BV)>(&mut self, mut f: F) {
        self.accepted_block_fields = true;
        self.accepted_value_fields = true;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if !self.known_fields.contains(&key.as_str()) {
                self.expect_eq_qeq(key, *cmp);
                f(key, *cmp, bv);
            }
        }
    }

    /// Like [`Validator::unknown_fields_cmp`] but accepts and passes any comparator.
    pub fn unknown_fields_any_cmp<F: FnMut(&Token, Comparator, &BV)>(&mut self, mut f: F) {
        self.accepted_block_fields = true;
        self.accepted_value_fields = true;
        for Field(key, cmp, bv) in self.block.iter_fields() {
            if !self.known_fields.contains(&key.as_str()) {
                f(key, *cmp, bv);
            }
        }
    }

    /// Tells the Validator to not warn about any unknown block contents when it goes out of scope.
    /// (The default is to warn.)
    pub fn no_warn_remaining(&mut self) {
        self.accepted_block_fields = true;
        self.accepted_value_fields = true;
        self.accepted_tokens = true;
        self.accepted_blocks = true;
    }

    /// Tells the Validator to warn about any unknown block contents right now, before it goes out of scope.
    /// It will not warn again when it does go out of scope.
    /// Returns true iff any warnings were emitted.
    pub fn warn_remaining(&mut self) -> bool {
        let mut warned = false;
        for item in self.block.iter_items() {
            match item {
                BlockItem::Field(Field(key, _, bv)) => match bv {
                    BV::Value(_) => {
                        if !self.accepted_value_fields && !self.known_fields.contains(&key.as_str())
                        {
                            let msg = format!("unknown field `{key}`");
                            let sev = Severity::Error.at_most(self.max_severity);
                            report(ErrorKey::UnknownField, sev).weak().msg(msg).loc(key).push();
                            warned = true;
                        }
                    }
                    BV::Block(_) => {
                        if !self.accepted_block_fields && !self.known_fields.contains(&key.as_str())
                        {
                            let msg = format!("unknown field `{key}`");
                            let sev = Severity::Error.at_most(self.max_severity);
                            report(ErrorKey::UnknownField, sev).weak().msg(msg).loc(key).push();
                            warned = true;
                        }
                    }
                },
                BlockItem::Value(t) => {
                    if !self.accepted_tokens {
                        let msg = format!("found loose value {t}, expected only `key =`");
                        let sev = Severity::Error.at_most(self.max_severity);
                        report(ErrorKey::Structure, sev).msg(msg).loc(t).push();
                        warned = true;
                    }
                }
                BlockItem::Block(b) => {
                    if !self.accepted_blocks {
                        let msg = "found sub-block, expected only `key =`";
                        let sev = Severity::Error.at_most(self.max_severity);
                        report(ErrorKey::Structure, sev).msg(msg).loc(b).push();
                        warned = true;
                    }
                }
            }
        }
        self.no_warn_remaining();
        warned
    }

    fn expect_eq_qeq(&self, key: &Token, cmp: Comparator) {
        #[allow(clippy::collapsible_else_if)]
        if self.allow_questionmark_equals {
            if !matches!(cmp, Comparator::Equals(Single | Question)) {
                let msg = format!("expected `{key} =` or `?=`, found `{cmp}`");
                let sev = Severity::Error.at_most(self.max_severity);
                report(ErrorKey::Validation, sev).msg(msg).loc(key).push();
            }
        } else {
            if !matches!(cmp, Comparator::Equals(Single)) {
                let msg = format!("expected `{key} =`, found `{cmp}`");
                let sev = Severity::Error.at_most(self.max_severity);
                report(ErrorKey::Validation, sev).msg(msg).loc(key).push();
            }
        }
    }
}

impl Drop for Validator<'_> {
    fn drop(&mut self) {
        self.warn_remaining();
    }
}