tiger-lib 1.14.0

Library used by the tools ck3-tiger, vic3-tiger, and imperator-tiger. This library holds the bulk of the code for them. It can be built either for ck3-tiger with the feature ck3, or for vic3-tiger with the feature vic3, or for imperator-tiger with the feature imperator, but not both at the same time.
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
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
//! Validate triggers, which are parts of the script that specify yes or no conditions.

use bitflags::bitflags;

use std::str::FromStr;

use crate::block::{BV, Block, Comparator, Eq::*, Field};
use crate::context::{Reason, ScopeContext};
#[cfg(feature = "jomini")]
use crate::data::genes::Gene;
#[cfg(feature = "jomini")]
use crate::data::trigger_localization::validate_trigger_localization;
use crate::date::Date;
use crate::desc::validate_desc;
use crate::everything::Everything;
use crate::game::Game;
use crate::helpers::is_country_tag;
use crate::helpers::stringify_choices;
#[cfg(feature = "hoi4")]
use crate::hoi4::effect_validation::validate_flag_name;
#[cfg(feature = "hoi4")]
use crate::hoi4::variables::validate_variable;
use crate::item::Item;
use crate::lowercase::Lowercase;
#[cfg(any(feature = "vic3", feature = "imperator"))]
use crate::modif::verify_modif_exists;
use crate::report::{ErrorKey, Severity, err, fatal, tips, warn};
use crate::scopes::{
    ArgumentValue, Scopes, needs_prefix, scope_iterator, scope_prefix, scope_to_scope,
};
#[cfg(feature = "jomini")]
use crate::script_value::validate_script_value;
use crate::token::{Loc, Token};
use crate::tooltipped::Tooltipped;
use crate::validate::{
    ListType, precheck_iterator_fields, validate_identifier, validate_ifelse_sequence,
    validate_inside_iterator, validate_iterator_fields,
};
use crate::validator::Validator;

/// Look up a trigger token that evaluates to a trigger value.
///
/// `name` is the token. `data` is used in special cases to verify the name dynamically,
/// for example, `<lifestyle>_xp` is only a valid trigger if `<lifestyle>` is present in
/// the database.
///
/// Returns the inscopes valid for the trigger and the output trigger value type.
pub fn scope_trigger(name: &Token, data: &Everything) -> Option<(Scopes, Trigger)> {
    let scope_trigger = match Game::game() {
        #[cfg(feature = "ck3")]
        Game::Ck3 => crate::ck3::tables::triggers::scope_trigger,
        #[cfg(feature = "vic3")]
        Game::Vic3 => crate::vic3::tables::triggers::scope_trigger,
        #[cfg(feature = "imperator")]
        Game::Imperator => crate::imperator::tables::triggers::scope_trigger,
        #[cfg(feature = "hoi4")]
        Game::Hoi4 => crate::hoi4::tables::triggers::scope_trigger,
    };
    scope_trigger(name, data)
}

/// The standard interface to trigger validation. Validates a trigger in the given [`ScopeContext`].
///
/// `tooltipped` determines what warnings are emitted related to tooltippability of the triggers
/// inside the block.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
pub fn validate_trigger(
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
) -> bool {
    let vd = Validator::new(block, data);
    validate_trigger_internal(
        Lowercase::empty(),
        ListType::None,
        block,
        data,
        sc,
        vd,
        tooltipped,
        false,
    )
}

/// Like [`validate_trigger`] but specifies a maximum [`Severity`] for the reports emitted by this
/// validation. Used to validate triggers in item definitions that don't warrant the `Error` level.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
#[allow(dead_code)]
pub fn validate_trigger_max_sev(
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
    max_sev: Severity,
) -> bool {
    let mut vd = Validator::new(block, data);
    vd.set_max_severity(max_sev);
    validate_trigger_internal(
        Lowercase::empty(),
        ListType::None,
        block,
        data,
        sc,
        vd,
        tooltipped,
        false,
    )
}

/// The interface to trigger validation when [`validate_trigger`] is too limited.
///
/// `caller` is the key that opened this trigger. It is used to determine which special cases apply.
/// For example, if `caller` is `trigger_if` then a `limit` block is expected.
///
/// `in_list` specifies whether this trigger is directly in an `any_` iterator. It is also used to
/// determine which special cases apply.
///
/// `negated` is true iff this trigger is tested in a negative sense, for example if it is
/// somewhere inside a `NOT = { ... }` block. `negated` is propagated to all sub-blocks and is
/// flipped when another `NOT` or similar is encountered inside this one.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
// TODO: `in_list` could be removed if the code checks directly for the `any_` prefix instead.
#[allow(clippy::too_many_arguments)]
pub fn validate_trigger_internal(
    caller: &Lowercase,
    ltype: ListType,
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    mut tooltipped: Tooltipped,
    negated: bool,
) -> bool {
    let mut side_effects = false;
    let max_sev = vd.max_severity();
    vd.set_case_sensitive(false);

    #[cfg(feature = "hoi4")]
    let caller = if Game::is_hoi4() && tooltipped == Tooltipped::Inner {
        &Lowercase::new_unchecked("custom_override_tooltip")
    } else {
        caller
    };

    // If this condition looks weird, it's because the negation from for example NOR has already
    // been applied to the `negated` value.
    if tooltipped == Tooltipped::FailuresOnly
        && ((negated && (caller == "and" || caller == "nand"))
            || (!negated && (caller == "or" || caller == "nor" || caller == "all_false")))
    {
        let true_negated = if caller == "nor" || caller == "all_false" || caller == "and" {
            "negated "
        } else {
            ""
        };
        let msg = format!(
            "{true_negated}{} is a too complex trigger to be tooltipped in a trigger that shows failures only.",
            caller.to_uppercase()
        );
        let info = "Try adding a custom_description or custom_tooltip, or simplifying the trigger";
        warn(ErrorKey::Tooltip).msg(msg).info(info).loc(block).push();
    }

    if block.num_items() == 1 {
        if caller == "and" {
            let msg = "AND with only one item inside is useless";
            let info = "you can remove the AND and just have the item there";
            tips(ErrorKey::Performance).msg(msg).info(info).loc(block).push();
        } else if caller == "or" {
            let msg = "OR with only one item inside is probably not what you intended";
            warn(ErrorKey::Logic).weak().msg(msg).loc(block).push();
        }
    }

    if caller == "trigger_if"
        || caller == "trigger_else_if"
        || caller == "trigger_else"
        || (Game::is_hoi4() && (caller == "if" || caller == "else_if" || caller == "else"))
    {
        if caller != "trigger_else" && caller != "else" {
            vd.req_field_warn("limit");
        }
        vd.field_validated_key_block("limit", |key, block, data| {
            if caller == "trigger_else" {
                let msg = "`trigger_else` with a `limit` does work, but may indicate a mistake";
                let info = "normally you would use `trigger_else_if` instead.";
                tips(ErrorKey::IfElse).msg(msg).info(info).loc(key).push();
            }
            side_effects |= validate_trigger(block, data, sc, Tooltipped::No);
        });
    } else {
        vd.ban_field("limit", || "`trigger_if`, `trigger_else_if` or `trigger_else`");
    }

    #[cfg(feature = "jomini")]
    if ltype == ListType::None {
        vd.ban_field("filter", || "lists");
    } else {
        vd.field_validated_block("filter", |block, data| {
            side_effects |= validate_trigger(block, data, sc, Tooltipped::No);
        });
    }

    validate_iterator_fields(caller, ltype, data, sc, &mut vd, &mut tooltipped, false);

    if ltype != ListType::None {
        validate_inside_iterator(caller, ltype, block, data, sc, &mut vd, tooltipped);
    }

    // TODO: the custom_description and custom_tooltip logic is duplicated for effects
    #[cfg(feature = "jomini")]
    if Game::is_jomini() {
        if caller == "custom_description" || caller == "custom_tooltip" {
            vd.req_field("text");
            if caller == "custom_tooltip" {
                vd.field_item("text", Item::Localization);
            } else if let Some(token) = vd.field_value("text") {
                validate_trigger_localization(token, data, tooltipped, negated);
            }
            vd.field_target_ok_this("subject", sc, Scopes::non_primitive());
        } else {
            vd.ban_field("text", || "`custom_description` or `custom_tooltip`");
            vd.ban_field("subject", || "`custom_description` or `custom_tooltip`");
        }
    }

    if caller == "custom_description" {
        // object and value are handled in the loop
    } else {
        vd.ban_field("object", || "`custom_description`");
    }

    if caller == "modifier" {
        // desc is handled in the loop
        // mark add and factor as known
        vd.multi_field("add");
        vd.multi_field("factor");
        vd.field_validated_block("trigger", |block, data| {
            side_effects |= validate_trigger(block, data, sc, Tooltipped::No);
        });
    } else {
        vd.ban_field("add", || "`modifier` or script values");
        vd.ban_field("factor", || "`modifier` blocks");
        vd.ban_field("desc", || "`modifier` or script values");
        vd.ban_field("trigger", || "`modifier` blocks");
    }

    if caller == "calc_true_if" {
        vd.req_field("amount");
        // TODO: verify these are integers
        vd.multi_field_any_cmp("amount");
    } else if ltype == ListType::None {
        vd.ban_field("amount", || "`calc_true_if`");
    }

    validate_ifelse_sequence(block, "trigger_if", "trigger_else_if", "trigger_else");

    vd.unknown_fields_any_cmp(|key, cmp, bv| {
        #[cfg(feature = "jomini")]
        if Game::is_jomini() && key.is("value") {
            validate_script_value(bv, data, sc);
            side_effects = true;
            return;
        }

        if key.is("desc") || key.is("DESC") {
            validate_desc(bv, data, sc);
            return;
        }

        if key.is("object") {
            if let Some(token) = bv.expect_value() {
                validate_target_ok_this(token, data, sc, Scopes::non_primitive());
            }
            return;
        }

        if let Some((it_type, it_name)) = key.split_once('_') {
            if let Ok(ltype) = ListType::try_from(it_type.as_str()) {
                if let Some((inscopes, outscope)) = scope_iterator(&it_name, data, sc) {
                    if !ltype.is_for_triggers() {
                        let msg = format!("cannot use `{it_type}_` list in a trigger");
                        err(ErrorKey::Validation).msg(msg).loc(key).push();
                        return;
                    }
                    sc.expect(inscopes, &Reason::Token(key.clone()));
                    if let Some(block) = bv.expect_block() {
                        precheck_iterator_fields(ltype, it_name.as_str(), block, data, sc);
                        sc.open_scope(outscope, key.clone());
                        let mut vd = Validator::new(block, data);
                        vd.set_max_severity(max_sev);
                        side_effects |= validate_trigger_internal(
                            &Lowercase::new(it_name.as_str()),
                            ltype,
                            block,
                            data,
                            sc,
                            vd,
                            tooltipped,
                            negated,
                        );
                        sc.close();
                    }
                    return;
                }
            }
        }

        if caller == "weighted_calc_true_if" && data.get_trigger(key).is_some() {
            let msg =
                "scripted triggers don't work in `weighted_calc_true_if` (confirmed for 1.16)";
            err(ErrorKey::Bugs).msg(msg).loc(key).push();
        }

        side_effects |=
            validate_trigger_key_bv(key, cmp, bv, data, sc, tooltipped, negated, max_sev);
    });

    if caller == "modifier" {
        // check add and factor at the end, accounting for any temporary scope saved
        // elsewhere in the block.
        vd.multi_field_validated("add", |bv, data| {
            if Game::is_jomini() {
                #[cfg(feature = "jomini")]
                validate_script_value(bv, data, sc);
                side_effects = true;
            } else {
                // TODO HOI4
                let _ = &bv;
                let _ = &data;
            }
        });

        vd.multi_field_validated("factor", |bv, data| {
            if Game::is_jomini() {
                #[cfg(feature = "jomini")]
                validate_script_value(bv, data, sc);
                side_effects = true;
            } else {
                // TODO HOI4
                let _ = &bv;
                let _ = &data;
            }
        });
    }

    side_effects
}

/// Validate a trigger given its key and argument. It is like [`validate_trigger_internal`] except
/// that all special cases are assumed to have been handled. This is the interface used for the
/// `switch` effect, where the key and argument are not together in the script.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
#[allow(clippy::too_many_arguments)] // nothing can be cut
pub fn validate_trigger_key_bv(
    key: &Token,
    cmp: Comparator,
    bv: &BV,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
    negated: bool,
    max_sev: Severity,
) -> bool {
    let mut side_effects = false;

    // Scripted trigger?
    if let Some(trigger) = data.get_trigger(key) {
        match bv {
            BV::Value(token) => {
                if !(token.is("yes") || token.is("no") || token.is("YES") || token.is("NO")) {
                    warn(ErrorKey::Validation).msg("expected yes or no").loc(token).push();
                }
                if !trigger.macro_parms().is_empty() {
                    fatal(ErrorKey::Macro).msg("expected macro arguments").loc(token).push();
                    return side_effects;
                }
                let negated = if token.is("no") { !negated } else { negated };
                // TODO: check side_effects
                trigger.validate_call(key, data, sc, tooltipped, negated);
            }
            BV::Block(block) => {
                let parms = trigger.macro_parms();
                if parms.is_empty() {
                    let msg = "this scripted trigger does not need macro arguments";
                    fatal(ErrorKey::Macro).msg(msg).loc(block).push();
                } else {
                    let mut vec = Vec::new();
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    for parm in &parms {
                        if let Some(token) = vd.field_value(parm) {
                            vec.push(token.clone());
                        } else {
                            let msg = format!("this scripted trigger needs parameter {parm}");
                            err(ErrorKey::Macro).msg(msg).loc(block).push();
                            return side_effects;
                        }
                    }
                    vd.unknown_value_fields(|key, _value| {
                        let msg = format!("this scripted trigger does not need parameter {key}");
                        let info = "supplying an unneeded parameter often causes a crash";
                        fatal(ErrorKey::Macro).msg(msg).info(info).loc(key).push();
                    });

                    let args: Vec<_> = parms.into_iter().zip(vec).collect();
                    // TODO: check side_effects
                    trigger.validate_macro_expansion(key, &args, data, sc, tooltipped, negated);
                }
            }
        }
        return side_effects;
    }

    // `10 < script value` is a valid trigger
    if key.is_number() {
        if Game::is_jomini() {
            #[cfg(feature = "jomini")]
            validate_script_value(bv, data, sc);
        } else {
            // TODO HOI4
        }
        return side_effects;
    }

    #[cfg(feature = "hoi4")]
    if Game::is_hoi4() && key.starts_with("var:") {
        validate_variable(key, data, sc, Severity::Error);
        sc.open_builder();
        sc.replace(Scopes::all_but_none(), key.clone());
        side_effects |= validate_trigger_rhs(key, cmp, bv, data, sc, tooltipped, negated, max_sev);
        return side_effects;
    }

    let part_vec = partition(key);
    sc.open_builder();
    for i in 0..part_vec.len() {
        let mut part_flags = PartFlags::empty();
        if i == 0 {
            part_flags |= PartFlags::First;
        }
        if i + 1 == part_vec.len() {
            part_flags |= PartFlags::Last;
        }
        if matches!(cmp, Comparator::Equals(Question)) {
            part_flags |= PartFlags::Question;
        }
        let part = &part_vec[i];

        match part {
            Part::TokenArgument(func, arg) => validate_argument(part_flags, func, arg, data, sc),
            Part::Token(part) => {
                let part_lc = Lowercase::new(part.as_str());
                // prefixed scope transition, e.g. cp:councillor_steward
                if let Some((prefix, mut arg)) = part.split_once(':') {
                    // event_id have multiple parts separated by `.`
                    let is_event_id = prefix.lowercase_is("event_id");
                    if is_event_id {
                        arg = key.split_once(':').unwrap().1;
                    }
                    if !validate_prefix(part_flags, &prefix, &arg, data, sc) {
                        sc.close();
                        return side_effects;
                    }
                    if is_event_id {
                        break; // force last part
                    }
                } else if part_lc == "root" {
                    sc.replace_root();
                } else if part_lc == "prev" {
                    if !part_flags.contains(PartFlags::First) && !Game::is_imperator() {
                        warn_not_first(part);
                    }
                    sc.replace_prev();
                } else if part_lc == "this" {
                    sc.replace_this();
                } else if Game::is_hoi4() && part_lc == "from" {
                    #[cfg(feature = "hoi4")]
                    sc.replace_from();
                } else if data.script_value_exists(part.as_str()) {
                    // TODO: check side_effects
                    #[cfg(feature = "jomini")]
                    data.script_values.validate_call(part, data, sc);
                    sc.replace(Scopes::Value, part.clone());
                } else if let Some((inscopes, outscope)) = scope_to_scope(part, sc.scopes()) {
                    #[cfg(feature = "imperator")]
                    if let Some((inscopes, trigger)) = scope_trigger(part, data) {
                        // If a trigger of the same name exists, and it's compatible with this
                        // location and scope context, then that trigger takes precedence.
                        if part_flags.contains(PartFlags::Last)
                            && (inscopes.contains(Scopes::None) || sc.scopes().intersects(inscopes))
                        {
                            validate_inscopes(part_flags, part, inscopes, sc);
                            sc.close();
                            side_effects |= match_trigger_bv(
                                &trigger,
                                &part.clone(),
                                cmp,
                                bv,
                                data,
                                sc,
                                tooltipped,
                                negated,
                                max_sev,
                            );
                            return side_effects;
                        }
                    }
                    validate_inscopes(part_flags, part, inscopes, sc);
                    sc.replace(outscope, part.clone());
                } else if let Some((inscopes, trigger)) = scope_trigger(part, data) {
                    if !part_flags.contains(PartFlags::Last) {
                        let msg = format!("`{part}` should be the last part");
                        warn(ErrorKey::Validation).msg(msg).loc(part).push();
                        sc.close();
                        return side_effects;
                    }
                    if !part_flags.contains(PartFlags::First)
                        && trigger_comparevalue(part, data).is_none()
                    {
                        let msg = format!("`{part}` should be the only part");
                        warn(ErrorKey::Validation).msg(msg).loc(part).push();
                        sc.close();
                        return side_effects;
                    }
                    validate_inscopes(part_flags, part, inscopes, sc);
                    sc.close();
                    side_effects |= match_trigger_bv(
                        &trigger,
                        &part.clone(),
                        cmp,
                        bv,
                        data,
                        sc,
                        tooltipped,
                        negated,
                        max_sev,
                    );
                    return side_effects;
                } else if Game::is_hoi4() && is_country_tag(part.as_str()) {
                    if !part_flags.contains(PartFlags::First) {
                        warn_not_first(part);
                    }
                    #[cfg(feature = "hoi4")]
                    data.verify_exists(Item::CountryTag, part);
                    #[cfg(feature = "hoi4")]
                    sc.replace(Scopes::Country, part.clone());
                } else if is_character_token(part.as_str(), data) {
                    #[cfg(feature = "hoi4")]
                    sc.replace(Scopes::Character, part.clone());
                } else {
                    // TODO: warn if trying to use iterator here
                    let msg = format!("unknown token `{part}`");
                    err(ErrorKey::UnknownField).msg(msg).loc(part).push();
                    sc.close();
                    return side_effects;
                }
            }
        }
    }

    side_effects |= validate_trigger_rhs(key, cmp, bv, data, sc, tooltipped, negated, max_sev);
    side_effects
}

#[allow(clippy::too_many_arguments)] // nothing can be cut
pub fn validate_trigger_rhs(
    key: &Token,
    cmp: Comparator,
    bv: &BV,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
    negated: bool,
    max_sev: Severity,
) -> bool {
    let mut side_effects = false;

    if !matches!(cmp, Comparator::Equals(Single | Question)) {
        // At this point we don't know whether to expect a script value or a target at the
        // right-hand side. Check for target first; a target can be a script value so that
        // encompasses both if the `bv` is not a block.
        if matches!(cmp, Comparator::NotEquals | Comparator::Equals(Double))
            && bv.get_value().is_some()
        {
            let scopes = sc.scopes();
            sc.close();
            if let Some(token) = bv.expect_value() {
                validate_target_ok_this(token, data, sc, scopes);
            }
        } else if sc.can_be(Scopes::Value) {
            sc.close();
            // TODO: check side_effects
            #[cfg(feature = "jomini")]
            validate_script_value(bv, data, sc);
        } else {
            let msg = format!("unexpected comparator {cmp}");
            warn(ErrorKey::Validation).msg(msg).loc(key).push();
            sc.close();
        }
        return side_effects;
    }

    match bv {
        BV::Value(t) => {
            let scopes = sc.scopes();
            sc.close();
            validate_target_ok_this(t, data, sc, scopes);
        }
        BV::Block(b) => {
            sc.finalize_builder();
            let mut vd = Validator::new(b, data);
            vd.set_max_severity(max_sev);
            side_effects |= validate_trigger_internal(
                Lowercase::empty(),
                ListType::None,
                b,
                data,
                sc,
                vd,
                tooltipped,
                negated,
            );
            sc.close();
        }
    }

    side_effects
}

/// Implementation of the [`Trigger::Block`] variant and its friends. It takes a list of known
/// fields and their own `Trigger` validators, and checks that the given `block` contains only
/// fields from that list and validates them.
///
/// The field names may have a prefix to indicate how they are to be used.
/// * `?` means the field is optional
/// * `*` means the field is optional and may occur multiple times
/// * `+` means the field is required and may occur multiple times
///
/// The default is that the field is required and may occur only once.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
fn match_trigger_fields(
    fields: &[(&str, Trigger)],
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
    negated: bool,
    max_sev: Severity,
) -> bool {
    let mut side_effects = false;
    let mut vd = Validator::new(block, data);
    vd.set_max_severity(max_sev);
    for (field, _) in fields {
        if let Some(opt) = field.strip_prefix('?') {
            vd.field_any_cmp(opt);
        } else if let Some(mlt) = field.strip_prefix('*') {
            vd.multi_field_any_cmp(mlt);
        } else if let Some(mlt) = field.strip_prefix('+') {
            vd.req_field(mlt);
            vd.multi_field_any_cmp(mlt);
        } else {
            vd.req_field(field);
            vd.field_any_cmp(field);
        }
    }

    for Field(key, cmp, bv) in block.iter_fields() {
        for (field, trigger) in fields {
            let fieldname = if let Some(opt) = field.strip_prefix('?') {
                opt
            } else if let Some(mlt) = field.strip_prefix('*') {
                mlt
            } else if let Some(mlt) = field.strip_prefix('+') {
                mlt
            } else {
                field
            };
            if key.is(fieldname) {
                side_effects |= match_trigger_bv(
                    trigger, key, *cmp, bv, data, sc, tooltipped, negated, max_sev,
                );
            }
        }
    }
    side_effects
}

/// Takes a [`Trigger`] and a trigger field, and validates that the constraints
/// specified by the `Trigger` hold.
///
/// Returns true iff the trigger had side effects (such as saving scopes).
#[allow(clippy::too_many_arguments)]
fn match_trigger_bv(
    trigger: &Trigger,
    name: &Token,
    cmp: Comparator,
    bv: &BV,
    data: &Everything,
    sc: &mut ScopeContext,
    tooltipped: Tooltipped,
    negated: bool,
    max_sev: Severity,
) -> bool {
    let mut side_effects = false;
    // True iff the comparator must be Comparator::Equals
    let mut must_be_eq = true;
    // True iff it's probably a mistake if the comparator is Comparator::Equals
    #[cfg(any(feature = "ck3", feature = "hoi4"))]
    let mut warn_if_eq = false;
    #[cfg(not(any(feature = "ck3", feature = "hoi4")))]
    let warn_if_eq = false;

    match trigger {
        Trigger::Boolean => {
            if let Some(token) = bv.expect_value() {
                validate_target(token, data, sc, Scopes::Bool);
            }
        }
        Trigger::CompareValue => {
            must_be_eq = false;
            // TODO: check side_effects
            if Game::is_jomini() {
                #[cfg(feature = "jomini")]
                validate_script_value(bv, data, sc);
            } else {
                #[cfg(feature = "hoi4")]
                if let Some(token) = bv.expect_value() {
                    validate_target(token, data, sc, Scopes::Value);
                }
            }
        }
        #[cfg(any(feature = "ck3", feature = "hoi4"))]
        Trigger::CompareValueWarnEq => {
            must_be_eq = false;
            warn_if_eq = true;
            // TODO: check side_effects
            #[cfg(feature = "jomini")]
            validate_script_value(bv, data, sc);
            // TODO HOI4
        }
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        Trigger::SetValue => {
            // TODO: check side_effects
            validate_script_value(bv, data, sc);
        }
        Trigger::CompareDate => {
            must_be_eq = false;
            if let Some(token) = bv.expect_value() {
                if Date::from_str(token.as_str()).is_err() {
                    let msg = format!("{name} expects a date value");
                    warn(ErrorKey::Validation).msg(msg).loc(token).push();
                }
            }
        }
        #[cfg(feature = "vic3")]
        Trigger::ItemOrCompareValue(i) => {
            if let Some(token) = bv.expect_value() {
                if !data.item_exists(*i, token.as_str()) {
                    must_be_eq = false;
                    validate_target(token, data, sc, Scopes::Value);
                }
            }
        }
        Trigger::Scope(s) => {
            if let Some(token) = bv.get_value() {
                validate_target(token, data, sc, *s);
            } else if s.contains(Scopes::Value) {
                // TODO: check side_effects
                #[cfg(feature = "jomini")]
                validate_script_value(bv, data, sc);
                // TODO HOI4
            } else {
                bv.expect_value();
            }
        }
        Trigger::ScopeOkThis(s) => {
            if let Some(token) = bv.get_value() {
                validate_target_ok_this(token, data, sc, *s);
            } else if s.contains(Scopes::Value) {
                // TODO: check side_effects
                #[cfg(feature = "jomini")]
                validate_script_value(bv, data, sc);
                // TODO HOI4
            } else {
                bv.expect_value();
            }
        }
        Trigger::Item(i) => {
            if let Some(token) = bv.expect_value() {
                data.verify_exists_max_sev(*i, token, max_sev);
            }
        }
        Trigger::ScopeOrItem(s, i) => {
            if let Some(token) = bv.expect_value() {
                if !data.item_exists(*i, token.as_str()) {
                    validate_target(token, data, sc, *s);
                }
            }
        }
        Trigger::Choice(choices) => {
            if let Some(token) = bv.expect_value() {
                if !choices.iter().any(|c| token.is(c)) {
                    let msg = format!("unknown value {token} for {name}");
                    let info = format!("valid values are: {}", stringify_choices(choices));
                    warn(ErrorKey::Validation).msg(msg).info(info).loc(token).push();
                }
            }
        }
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        Trigger::CompareChoice(choices) => {
            must_be_eq = false;
            if let Some(token) = bv.expect_value() {
                if !choices.contains(&token.as_str()) {
                    let msg = format!("{name} expects one of {}", stringify_choices(choices));
                    warn(ErrorKey::Validation).msg(msg).loc(token).push();
                }
            }
        }
        #[cfg(feature = "vic3")]
        Trigger::CompareChoiceOrNumber(choices) => {
            must_be_eq = false;
            if let Some(token) = bv.expect_value() {
                if !token.is_number() && !choices.contains(&token.as_str()) {
                    validate_target(token, data, sc, Scopes::Value);
                }
            }
        }
        Trigger::Block(fields) => {
            if let Some(block) = bv.expect_block() {
                side_effects |=
                    match_trigger_fields(fields, block, data, sc, tooltipped, negated, max_sev);
            }
        }
        #[cfg(feature = "ck3")]
        Trigger::ScopeOrBlock(s, fields) => match bv {
            BV::Value(token) => {
                validate_target(token, data, sc, *s);
            }
            BV::Block(block) => {
                side_effects |=
                    match_trigger_fields(fields, block, data, sc, tooltipped, negated, max_sev);
            }
        },
        #[cfg(feature = "ck3")]
        Trigger::ItemOrBlock(i, fields) => match bv {
            BV::Value(token) => data.verify_exists_max_sev(*i, token, max_sev),
            BV::Block(block) => {
                side_effects |=
                    match_trigger_fields(fields, block, data, sc, tooltipped, negated, max_sev);
            }
        },
        #[cfg(feature = "ck3")]
        Trigger::IdentifierOrBlock(kind, fields) => match bv {
            BV::Value(token) => validate_identifier(token, kind, Severity::Error),
            BV::Block(block) => {
                side_effects |=
                    match_trigger_fields(fields, block, data, sc, tooltipped, negated, max_sev);
            }
        },
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        Trigger::BlockOrCompareValue(fields) => match bv {
            BV::Value(t) => {
                validate_target(t, data, sc, Scopes::Value);
                must_be_eq = false;
            }
            BV::Block(b) => {
                side_effects |=
                    match_trigger_fields(fields, b, data, sc, tooltipped, negated, max_sev);
            }
        },
        #[cfg(feature = "ck3")]
        Trigger::ScopeList(s) => {
            if let Some(block) = bv.expect_block() {
                let mut vd = Validator::new(block, data);
                vd.set_max_severity(max_sev);
                for token in vd.values() {
                    validate_target(token, data, sc, *s);
                }
            }
        }
        #[cfg(feature = "ck3")]
        Trigger::ScopeCompare(s) => {
            if let Some(block) = bv.expect_block() {
                if block.iter_items().count() != 1 {
                    let msg = "unexpected number of items in block";
                    warn(ErrorKey::Validation).msg(msg).loc(block).push();
                }
                for Field(key, _, bv) in block.iter_fields_warn() {
                    validate_target(key, data, sc, *s);
                    if let Some(token) = bv.expect_value() {
                        validate_target(token, data, sc, *s);
                    }
                }
            }
        }
        #[cfg(feature = "ck3")]
        Trigger::CompareToScope(s) => {
            must_be_eq = false;
            if let Some(token) = bv.expect_value() {
                validate_target(token, data, sc, *s);
            }
        }
        Trigger::Control => {
            if let Some(block) = bv.expect_block() {
                let mut negated = negated;
                let name_lc = name.as_str().to_ascii_lowercase();
                if name_lc == "all_false"
                    || name_lc == "not"
                    || name_lc == "nand"
                    || name_lc == "nor"
                {
                    negated = !negated;
                }
                let mut tooltipped = tooltipped;
                if name_lc == "custom_description" {
                    tooltipped = Tooltipped::No;
                }
                let mut vd = Validator::new(block, data);
                vd.set_max_severity(max_sev);
                side_effects |= validate_trigger_internal(
                    &Lowercase::from_string_unchecked(name_lc),
                    ListType::None,
                    block,
                    data,
                    sc,
                    vd,
                    tooltipped,
                    negated,
                );
            }
        }
        Trigger::Special => {
            if name.is("exists") {
                if let Some(token) = bv.expect_value() {
                    if token.is("yes") || token.is("no") {
                        let msg = "`exists = yes/no` does not work";
                        let info = if token.is("yes") {
                            "try `exists = this`"
                        } else {
                            "try `NOT = { exists = this }`"
                        };
                        warn(ErrorKey::Scopes).msg(msg).info(info).loc(token).push();
                    } else if token.starts_with("scope:") && !token.as_str().contains('.') {
                        // exists = scope:name is used to check if that scope name was set
                        if !negated {
                            sc.exists_scope(token.as_str().strip_prefix("scope:").unwrap(), name);
                        }
                    } else if token.starts_with("flag:") {
                        // exists = flag:$REASON$ is used in vanilla just to shut up their error.log,
                        // so accept it silently even though it's a no-op.
                    } else {
                        validate_target_ok_this(token, data, sc, Scopes::non_primitive());

                        if tooltipped.is_tooltipped() {
                            if let Some(firstpart) = token.as_str().strip_suffix(".holder") {
                                let msg = format!(
                                    "could rewrite this as `{firstpart} = {{ is_title_created = yes }}`"
                                );
                                let info = "it gives a nicer tooltip";
                                tips(ErrorKey::Tooltip).msg(msg).info(info).loc(name).push();
                            }
                        }
                    }
                }
            } else if name.is("custom_tooltip") {
                match bv {
                    BV::Value(t) => data.verify_exists_max_sev(Item::Localization, t, max_sev),
                    BV::Block(b) => {
                        let mut vd = Validator::new(b, data);
                        vd.set_max_severity(max_sev);
                        side_effects |= validate_trigger_internal(
                            &Lowercase::new(name.as_str()),
                            ListType::None,
                            b,
                            data,
                            sc,
                            vd,
                            Tooltipped::No,
                            negated,
                        );
                    }
                }
            } else if name.is("has_gene") {
                #[cfg(feature = "jomini")]
                if let Some(block) = bv.expect_block() {
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    vd.field_item("category", Item::GeneCategory);
                    if let Some(category) = block.get_field_value("category") {
                        if let Some(template) = vd.field_value("template") {
                            Gene::verify_has_template(category.as_str(), template, data);
                        }
                    }
                }
            } else if name.is("save_temporary_opinion_value_as") {
                if let Some(block) = bv.expect_block() {
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    vd.req_field("name");
                    vd.req_field("target");
                    vd.field_target("target", sc, Scopes::Character);
                    if let Some(name) = vd.field_value("name") {
                        sc.define_name_token(name.as_str(), Scopes::Value, name);
                        side_effects = true;
                    }
                }
            } else if name.is("save_temporary_scope_value_as") {
                #[cfg(feature = "jomini")]
                if let Some(block) = bv.expect_block() {
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    vd.req_field("name");
                    vd.req_field("value");
                    vd.field_validated("value", |bv, data| match bv {
                        BV::Value(token) => {
                            validate_target(token, data, sc, Scopes::primitive());
                        }
                        BV::Block(_) => validate_script_value(bv, data, sc),
                    });
                    // TODO: figure out the scope type of `value` and use that
                    if let Some(name) = vd.field_value("name") {
                        sc.define_name_token(name.as_str(), Scopes::primitive(), name);
                        side_effects = true;
                    }
                }
            } else if name.is("save_temporary_scope_as") {
                if let Some(name) = bv.expect_value() {
                    sc.save_current_scope(name.as_str());
                    side_effects = true;
                }
            } else if name.is("weighted_calc_true_if") {
                if let Some(block) = bv.expect_block() {
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    if let Some(bv) = vd.field_any_cmp("amount") {
                        if let Some(token) = bv.expect_value() {
                            token.expect_number();
                        }
                    }
                    for (_, block) in vd.integer_blocks() {
                        let vd = Validator::new(block, data);
                        side_effects |= validate_trigger_internal(
                            &Lowercase::new_unchecked("weighted_calc_true_if"),
                            ListType::None,
                            block,
                            data,
                            sc,
                            vd,
                            tooltipped,
                            false,
                        );
                        if block.num_items() > 1 {
                            let msg = "`weighted_calc_true_if` accepts only one trigger per weight (confirmed for 1.16)";
                            let info = "and no scripted triggers";
                            err(ErrorKey::Bugs).msg(msg).info(info).loc(block).push();
                        }
                    }
                }
            } else if name.is("switch") {
                if let Some(block) = bv.expect_block() {
                    let mut vd = Validator::new(block, data);
                    vd.set_max_severity(max_sev);
                    vd.req_field("trigger");
                    if let Some(target) = vd.field_value("trigger") {
                        let target = target.clone();
                        let mut count = 0;
                        vd.set_allow_questionmark_equals(true);
                        vd.unknown_block_fields(|key, block| {
                            count += 1;
                            if !key.is("fallback") {
                                let synthetic_bv = BV::Value(key.clone());
                                validate_trigger_key_bv(
                                    &target,
                                    Comparator::Equals(Single),
                                    &synthetic_bv,
                                    data,
                                    sc,
                                    tooltipped,
                                    negated,
                                    max_sev,
                                );
                            }
                            side_effects |= validate_trigger(block, data, sc, tooltipped);
                        });
                        if count == 0 {
                            let msg = "switch with no branches";
                            err(ErrorKey::Logic).msg(msg).loc(name).push();
                        }
                    }
                }
            } else if name.is("add_to_temporary_list") {
                if let Some(value) = bv.expect_value() {
                    sc.define_or_expect_list(value);
                    side_effects = true;
                }
            } else if name.is("is_in_list") {
                if let Some(value) = bv.expect_value() {
                    sc.expect_list(value);
                }
            } else if name.is("is_researching_technology") {
                #[cfg(feature = "vic3")]
                if let Some(value) = bv.expect_value() {
                    if !value.is("any") {
                        data.verify_exists(Item::Technology, value);
                    }
                }
            }
            // TODO: time_of_year
        }
        #[cfg(feature = "hoi4")]
        Trigger::Iterator(ltype, outscope) => {
            let it_name = name.split_once('_').unwrap().1;
            if let Some(block) = bv.expect_block() {
                precheck_iterator_fields(*ltype, it_name.as_str(), block, data, sc);
                sc.open_scope(*outscope, name.clone());
                let mut vd = Validator::new(block, data);
                vd.set_max_severity(max_sev);
                side_effects |= validate_trigger_internal(
                    &Lowercase::new(it_name.as_str()),
                    *ltype,
                    block,
                    data,
                    sc,
                    vd,
                    tooltipped,
                    negated,
                );
                sc.close();
            }
        }
        Trigger::Identifier(kind) => {
            if let Some(token) = bv.expect_value() {
                validate_identifier(token, kind, Severity::Error);
            }
        }
        #[cfg(feature = "hoi4")]
        Trigger::Flag => {
            if let Some(token) = bv.expect_value() {
                if tooltipped.is_tooltipped() && !token.as_str().contains('@') {
                    data.verify_exists(Item::Localization, token);
                }
                validate_flag_name(token);
            }
        }
        #[cfg(feature = "hoi4")]
        Trigger::FlagOrBlock(fields) => {
            if name.is("has_unit_leader_flag") {
                let msg = "deprecated in favor of has_character_flag";
                warn(ErrorKey::Deprecated).msg(msg).loc(name).push();
            }

            match bv {
                BV::Value(token) => {
                    if tooltipped.is_tooltipped() && !token.as_str().contains('@') {
                        data.verify_exists(Item::Localization, token);
                    }
                    validate_flag_name(token);
                }
                BV::Block(block) => {
                    side_effects |=
                        match_trigger_fields(fields, block, data, sc, tooltipped, negated, max_sev);
                }
            }
        }
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        Trigger::Removed(msg, info) => {
            err(ErrorKey::Removed).msg(*msg).info(*info).loc(name).push();
        }
        Trigger::UncheckedValue => {
            bv.expect_value();
            side_effects = true; // have to assume it's possible
        }
        Trigger::UncheckedTodo => {
            side_effects = true; // have to assume it's possible
        }
    }

    if matches!(cmp, Comparator::Equals(_)) {
        if warn_if_eq {
            let msg = format!(
                "`{name} {cmp}` means exactly equal to that amount, which is usually not what you want"
            );
            warn(ErrorKey::Logic).msg(msg).loc(name).push();
        }
    } else if must_be_eq {
        let msg = format!("unexpected comparator {cmp}");
        warn(ErrorKey::Validation).msg(msg).loc(name).push();
    }
    side_effects
}

/// Validate that `token` is valid as the right-hand side of a field.
///
/// `outscopes` is the set of scope types that this target is allowed to produce.
/// * Example: in `has_claim_on = title:e_byzantium`, the target is `title:e_byzantium` and it
///   should produce a [`Scopes::LandedTitle`] scope in order to be valid for `has_claim_on`.
///
/// Returns the best guess about the scope type that this target resolves to.
pub fn validate_target_ok_this(
    token: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
    outscopes: Scopes,
) -> Scopes {
    if token.is_number() {
        #[allow(unused_mut)] // only needs mut for hoi4
        let mut number_scope = Scopes::Value;
        #[cfg(feature = "hoi4")]
        if Game::is_hoi4() && data.item_exists(Item::State, token.as_str()) {
            number_scope |= Scopes::State;
        }
        if !outscopes.intersects(number_scope | Scopes::None) {
            let msg = format!("expected {outscopes}");
            warn(ErrorKey::Scopes).msg(msg).loc(token).push();
        }
        return number_scope;
    }
    #[cfg(feature = "hoi4")]
    if Game::is_hoi4() {
        if token.starts_with("var:")
            || token.starts_with("global.")
            || token.as_str().contains('^')
            || token.as_str().contains('@')
            || token.as_str().contains('?')
        {
            validate_variable(token, data, sc, Severity::Error);
            return Scopes::all_but_none();
        }
        if data.variables.variable_exists(token.as_str()) {
            return Scopes::all_but_none();
        }
    }
    let part_vec = partition(token);
    sc.open_builder();
    for i in 0..part_vec.len() {
        let mut part_flags = PartFlags::empty();
        if i == 0 {
            part_flags |= PartFlags::First;
        }
        if i + 1 == part_vec.len() {
            part_flags |= PartFlags::Last;
        }
        let part = &part_vec[i];

        match part {
            Part::TokenArgument(func, arg) => validate_argument(part_flags, func, arg, data, sc),
            Part::Token(part) => {
                let part_lc = Lowercase::new(part.as_str());
                // prefixed scope transition, e.g. cp:councillor_steward
                if let Some((prefix, mut arg)) = part.split_once(':') {
                    // event_id have multiple parts separated by `.`
                    let is_event_id = prefix.lowercase_is("event_id");
                    if is_event_id {
                        arg = token.split_once(':').unwrap().1;
                    }
                    if !validate_prefix(part_flags, &prefix, &arg, data, sc) {
                        sc.close();
                        return Scopes::all();
                    }
                    if is_event_id {
                        break; // force last part
                    }
                } else if part_lc == "root" {
                    sc.replace_root();
                } else if part_lc == "prev" {
                    if !part_flags.contains(PartFlags::First) && !Game::is_imperator() {
                        warn_not_first(part);
                    }
                    sc.replace_prev();
                } else if part_lc == "this" {
                    sc.replace_this();
                } else if Game::is_hoi4() && part_lc == "from" {
                    #[cfg(feature = "hoi4")]
                    sc.replace_from();
                } else if data.script_value_exists(part.as_str()) {
                    // TODO: check side_effects
                    #[cfg(feature = "jomini")]
                    data.script_values.validate_call(part, data, sc);
                    sc.replace(Scopes::Value, part.clone());
                } else if let Some((inscopes, outscope)) = scope_to_scope(part, sc.scopes()) {
                    #[cfg(feature = "imperator")]
                    if let Some(inscopes) = trigger_comparevalue(part, data) {
                        // If a trigger of the same name exists, and it's compatible with this
                        // location and scope context, then that trigger takes precedence.
                        if part_flags.contains(PartFlags::Last)
                            && (inscopes.contains(Scopes::None) || sc.scopes().intersects(inscopes))
                        {
                            validate_inscopes(part_flags, part, inscopes, sc);
                            sc.replace(Scopes::Value, part.clone());
                            continue;
                        }
                    }
                    validate_inscopes(part_flags, part, inscopes, sc);
                    sc.replace(outscope, part.clone());
                } else if let Some(inscopes) = trigger_comparevalue(part, data) {
                    if !part_flags.contains(PartFlags::Last) {
                        let msg = format!("`{part}` should be the last part");
                        warn(ErrorKey::Validation).msg(msg).loc(part).push();
                        sc.close();
                        return Scopes::all();
                    }
                    validate_inscopes(part_flags, part, inscopes, sc);
                    sc.replace(Scopes::Value, part.clone());
                } else if Game::is_hoi4() && is_country_tag(part.as_str()) {
                    if !part_flags.contains(PartFlags::First) {
                        warn_not_first(part);
                    }
                    #[cfg(feature = "hoi4")]
                    data.verify_exists(Item::CountryTag, part);
                    #[cfg(feature = "hoi4")]
                    sc.replace(Scopes::Country, part.clone());
                } else if is_character_token(part.as_str(), data) {
                    #[cfg(feature = "hoi4")]
                    sc.replace(Scopes::Character, part.clone());
                } else if Game::is_hoi4() && part.is_integer() {
                    #[cfg(feature = "hoi4")]
                    data.verify_exists(Item::State, part);
                    #[cfg(feature = "hoi4")]
                    sc.replace(Scopes::State, part.clone());
                } else {
                    // See if the user forgot a prefix like `faith:` or `culture:`
                    let mut opt_info = None;
                    if part_flags.contains(PartFlags::First | PartFlags::Last) {
                        if let Some(prefix) = needs_prefix(part.as_str(), data, outscopes) {
                            opt_info = Some(format!("did you mean `{prefix}:{part}` ?"));
                        }
                    }

                    // TODO: warn if trying to use iterator here
                    let msg = format!("unknown token `{part}`");
                    err(ErrorKey::UnknownField).msg(msg).opt_info(opt_info).loc(part).push();
                    sc.close();
                    return Scopes::all();
                }
            }
        }
    }
    let (final_scopes, because) = sc.scopes_reason();
    if !outscopes.intersects(final_scopes | Scopes::None) {
        let part = &part_vec[part_vec.len() - 1];
        let msg = format!("`{part}` produces {final_scopes} but expected {outscopes}");
        // Must not be at the same location to avoid spurious error messages
        let opt_loc = (part.loc() != because.token().loc).then(|| because.token());
        let msg2 = format!("scope was {}", because.msg());
        warn(ErrorKey::Scopes).msg(msg).loc(part).opt_loc_msg(opt_loc, msg2).push();
    }
    sc.close();
    final_scopes
}

/// Just like [`validate_target_ok_this`], but warns if the target is a literal `this` because that
/// is usually a mistake.
pub fn validate_target(
    token: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
    outscopes: Scopes,
) -> Scopes {
    if token.is("this") {
        let msg = "target `this` makes no sense here";
        warn(ErrorKey::UseOfThis).msg(msg).loc(token).push();
    }
    validate_target_ok_this(token, data, sc, outscopes)
}

/// A part in a token chain
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Part {
    /// A simple token
    Token(Token),
    /// Function and argument tokens
    TokenArgument(Token, Token),
}

impl std::fmt::Display for Part {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Part::Token(token) => token.fmt(f),
            Part::TokenArgument(func, arg) => write!(f, "{func}({arg})"),
        }
    }
}

impl Part {
    fn loc(&self) -> Loc {
        match self {
            Part::Token(t) | Part::TokenArgument(t, _) => t.loc,
        }
    }
}

/// This function partitions the input token into parts separated by `.`. Each part may contain either a token
/// or a token-argument pair when using the `()`-syntax, e.g. `"prowess_diff(liege)"`. It does not validate the tokens
/// or arguments, but simply parses and detects any syntactical errors.
pub fn partition(token: &Token) -> Vec<Part> {
    let mut parts = Vec::new();

    let mut has_part_argument = false;
    let mut has_part_argument_erred = false;
    let mut paren_depth = 0;
    let (mut part_idx, mut part_col) = (0, 0);
    let (mut first_paren_idx, mut first_paren_col) = (0, 0);
    let (mut second_paren_idx, mut second_paren_col) = (0, 0);

    for (col, (idx, ch)) in token.as_str().char_indices().enumerate() {
        let col = u32::try_from(col).expect("internal error: 2^32 columns");
        match ch {
            '.' => {
                if paren_depth == 0 {
                    if part_idx == idx {
                        // Empty part; err but skip it since it's likely a typo
                        let mut loc = token.loc;
                        loc.column += col;
                        err(ErrorKey::Validation).msg("empty part").loc(loc).push();
                    } else if !has_part_argument {
                        // The just completed part has no argument
                        let mut part_loc = token.loc;
                        part_loc.column += part_col;
                        #[allow(unused_mut)]
                        let mut part_token = token.subtoken(part_idx..idx, part_loc);
                        #[cfg(feature = "imperator")]
                        // Imperator has a `hidden:` prefix that can go before other prefixes so it
                        // has to be handled specially.
                        if let Some(hidden_arg) = part_token.strip_prefix("hidden:") {
                            part_token = hidden_arg;
                        }
                        parts.push(Part::Token(part_token));
                    }
                    has_part_argument = false;
                    has_part_argument_erred = false;
                    part_col = col + 1;
                    part_idx = idx + 1;
                }
            }
            '(' => {
                if paren_depth == 0 {
                    first_paren_col = col;
                    first_paren_idx = idx;
                } else if paren_depth == 1 {
                    second_paren_col = col;
                    second_paren_idx = idx;
                }

                paren_depth += 1;
            }
            ')' => {
                if paren_depth == 0 {
                    // Missing opening parenthesis `(`
                    let mut loc = token.loc;
                    loc.column += col;
                    err(ErrorKey::Validation)
                        .msg("closing without opening parenthesis `(`")
                        .loc(loc)
                        .push();
                } else if paren_depth == 1 {
                    // Argument between parentheses
                    let mut func_loc = token.loc;
                    func_loc.column += part_col;
                    let func_token = token.subtoken(part_idx..first_paren_idx, func_loc);

                    let mut arg_loc = token.loc;
                    arg_loc.column += first_paren_col + 1;
                    let arg_token = token.subtoken_stripped(first_paren_idx + 1..idx, arg_loc);

                    parts.push(Part::TokenArgument(func_token, arg_token));
                    has_part_argument = true;
                    paren_depth -= 1;
                } else if paren_depth == 2 {
                    // Cannot have nested parentheses
                    let mut loc = token.loc;
                    loc.column += second_paren_col;
                    let nested_paren_token = token.subtoken(second_paren_idx..=idx, loc);
                    err(ErrorKey::Validation)
                        .msg("cannot have nested parentheses")
                        .loc(nested_paren_token)
                        .push();
                    paren_depth -= 1;
                }
            }
            _ => {
                // an argument can only be the last part or followed by dot `.` AND hasn't erred from it yet
                if has_part_argument && !has_part_argument_erred {
                    let mut loc = token.loc;
                    loc.column += col;
                    err(ErrorKey::Validation)
                        .msg("argument can only be the last part or followed by dot `.`")
                        .loc(loc)
                        .push();
                    has_part_argument_erred = true;
                }
            }
        }
    }

    if paren_depth > 0 {
        // Missing closing parenthesis `)`
        let mut loc = token.loc;
        loc.column += first_paren_col;
        let broken_token = token.subtoken(first_paren_idx.., loc);
        err(ErrorKey::Validation)
            .msg("opening without closing parenthesis `)`")
            .loc(broken_token)
            .push();
    }

    if part_idx == token.as_str().len() {
        // Trailing `.`
        let mut loc = token.loc;
        loc.column += part_col;
        err(ErrorKey::Validation).msg("trailing dot `.`").loc(loc).push();
    } else if !has_part_argument {
        // final part (without argument)
        let mut part_loc = token.loc;
        part_loc.column += part_col;
        // SAFETY: part_idx < token.as_str.len()
        #[allow(unused_mut)]
        let mut part_token = token.subtoken(part_idx.., part_loc);
        #[cfg(feature = "imperator")]
        // see above
        if let Some(hidden_arg) = part_token.strip_prefix("hidden:") {
            part_token = hidden_arg;
        }
        parts.push(Part::Token(part_token));
    }
    parts
}

bitflags! {
    // Attributes can be applied to flags types
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct PartFlags: u8 {
        const First = 0b_0000_0001;
        const Last = 0b_0000_0010;
        const Question = 0b_0000_0100;
    }
}

#[inline]
pub fn warn_not_first(name: &Token) {
    let msg = format!("`{name}:` makes no sense except as first part");
    warn(ErrorKey::Validation).msg(msg).loc(name).push();
}

pub fn validate_inscopes(
    part_flags: PartFlags,
    name: &Token,
    inscopes: Scopes,
    sc: &mut ScopeContext,
) {
    // If the part does not use its inscope then any parts that come before it are useless
    // and probably indicate a mistake is being made.
    if inscopes == Scopes::None && !part_flags.contains(PartFlags::First) {
        warn_not_first(name);
    }
    sc.expect(inscopes, &Reason::Token(name.clone()));
}

#[allow(unused_variables)] // imperator does not use sc
fn validate_argument_internal(
    arg: &Token,
    validation: ArgumentValue,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    match validation {
        ArgumentValue::Item(item) => data.verify_exists(item, arg),
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        ArgumentValue::Scope(scope) => {
            let stash = sc.stash_builder();
            validate_target_ok_this(arg, data, sc, scope);
            sc.unstash_builder(stash);
        }
        #[cfg(any(feature = "ck3", feature = "vic3"))]
        ArgumentValue::ScopeOrItem(scope, item) => {
            if !data.item_exists(item, arg.as_str()) {
                let stash = sc.stash_builder();
                validate_target_ok_this(arg, data, sc, scope);
                sc.unstash_builder(stash);
            }
        }
        #[cfg(feature = "ck3")]
        ArgumentValue::TraitTrack => {
            if let Some((traitname, track)) = arg.split_once('|') {
                // TODO: verify that the track belongs to this trait
                data.verify_exists(Item::Trait, &traitname);
                data.verify_exists(Item::TraitTrack, &track);
            } else if !data.item_exists(Item::Trait, arg.as_str()) {
                let stash = sc.stash_builder();
                validate_target(arg, data, sc, Scopes::Trait);
                sc.unstash_builder(stash);
            }
        }
        #[cfg(any(feature = "vic3", feature = "imperator"))]
        ArgumentValue::Modif => {
            // TODO: deduce the ModifKinds from the `this` scope
            match Game::game() {
                #[cfg(feature = "vic3")]
                Game::Vic3 => verify_modif_exists(
                    arg,
                    data,
                    crate::vic3::modif::ModifKinds::all(),
                    Severity::Warning,
                ),
                #[cfg(feature = "imperator")]
                Game::Imperator => verify_modif_exists(
                    arg,
                    data,
                    crate::imperator::modif::ModifKinds::all(),
                    Severity::Warning,
                ),
                #[allow(unreachable_patterns)]
                _ => unreachable!(),
            }
        }
        #[cfg(any(feature = "vic3", feature = "ck3"))]
        ArgumentValue::Identifier(kind) => {
            validate_identifier(arg, kind, Severity::Error);
        }
        ArgumentValue::UncheckedValue => (),
        #[cfg(feature = "ck3")]
        ArgumentValue::Removed(version, info) => {
            let msg = format!("removed in {version}");
            err(ErrorKey::Removed).msg(msg).info(info).loc(arg).push();
        }
    }
}

/// Validate for scope and not trigger arguments
pub fn validate_argument_scope(
    part_flags: PartFlags,
    (inscopes, outscopes, validation): (Scopes, Scopes, ArgumentValue),
    func: &Token,
    arg: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    validate_inscopes(part_flags, func, inscopes, sc);
    validate_argument_internal(arg, validation, data, sc);

    let mut outscopes_token = func.clone();
    outscopes_token.combine(arg, ':');
    if func.lowercase_is("scope") {
        if part_flags.contains(PartFlags::Last | PartFlags::Question) {
            sc.exists_scope(arg.as_str(), outscopes_token.clone());
        }
        sc.replace_named_scope(arg.as_str(), outscopes_token);
    } else {
        sc.replace(outscopes, outscopes_token);
    }
}

/// Validate that the argument passed through is valid, either being of a complex trigger compare value,
/// or a scope prefix.
#[allow(unreachable_code, unused_variables)]
pub fn validate_argument(
    part_flags: PartFlags,
    func: &Token,
    arg: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    #[cfg(feature = "imperator")]
    if Game::is_imperator() {
        // Imperator does not use `()`
        let msg = "imperator does not support the `()` syntax";
        let mut opening_paren_loc = arg.loc;
        opening_paren_loc.column -= 1;
        err(ErrorKey::WrongGame).msg(msg).loc(opening_paren_loc).push();
        return;
    }

    #[cfg(feature = "hoi4")]
    if Game::is_hoi4() {
        let msg = "hoi4 does not support the `()` syntax";
        let mut opening_paren_loc = arg.loc;
        opening_paren_loc.column -= 1;
        err(ErrorKey::WrongGame).msg(msg).loc(opening_paren_loc).push();
        return;
    }

    let scope_trigger_complex: fn(&str) -> Option<(Scopes, ArgumentValue, Scopes)> =
        match Game::game() {
            #[cfg(feature = "ck3")]
            Game::Ck3 => crate::ck3::tables::triggers::scope_trigger_complex,
            #[cfg(feature = "vic3")]
            Game::Vic3 => crate::vic3::tables::triggers::scope_trigger_complex,
            #[cfg(feature = "imperator")]
            Game::Imperator => unreachable!(),
            #[cfg(feature = "hoi4")]
            Game::Hoi4 => unreachable!(),
        };

    let func_lc = func.as_str().to_ascii_lowercase();
    if let Some((inscopes, validation, outscopes)) = scope_trigger_complex(&func_lc) {
        sc.expect(inscopes, &Reason::Token(func.clone()));
        validate_argument_internal(arg, validation, data, sc);
        sc.replace(outscopes, func.clone());
    } else if let Some(entry) = scope_prefix(func) {
        validate_argument_scope(part_flags, entry, func, arg, data, sc);
    } else {
        let msg = format!("unknown token `{func}:`");
        err(ErrorKey::Validation).msg(msg).loc(func).push();
    }
}

/// Validate a `prefix:value` construct in a trigger.
///
/// Very similar to `validate_argument` because the notations are almost interchangeable in script.
#[allow(unreachable_code, unused_variables)]
pub fn validate_prefix(
    part_flags: PartFlags,
    prefix: &Token,
    arg: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
) -> bool {
    fn scope_trigger_complex(prefix: &str) -> Option<(Scopes, ArgumentValue, Scopes)> {
        match Game::game() {
            #[cfg(feature = "ck3")]
            Game::Ck3 => crate::ck3::tables::triggers::scope_trigger_complex(prefix),
            #[cfg(feature = "vic3")]
            Game::Vic3 => crate::vic3::tables::triggers::scope_trigger_complex(prefix),
            #[cfg(feature = "imperator")]
            Game::Imperator => None,
            #[cfg(feature = "hoi4")]
            Game::Hoi4 => None,
        }
    }

    let prefix_lc = prefix.as_str().to_ascii_lowercase();
    if let Some((inscopes, validation, outscopes)) = scope_trigger_complex(&prefix_lc) {
        sc.expect(inscopes, &Reason::Token(prefix.clone()));
        validate_argument_internal(arg, validation, data, sc);
        sc.replace(outscopes, prefix.clone());
        true
    } else if let Some(entry) = scope_prefix(prefix) {
        validate_argument_scope(part_flags, entry, prefix, arg, data, sc);
        true
    } else {
        let msg = format!("unknown prefix `{prefix:}`");
        err(ErrorKey::Validation).msg(msg).loc(prefix).push();
        false
    }
}

/// A description of the constraints on the right-hand side of a given trigger.
/// In other words, how it can be used.
///
/// It is used recursively in variants like [`Trigger::Block`], where each of the sub fields have
/// their own `Trigger`.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[allow(dead_code)] // TODO: remove when hoi4 is complete
pub enum Trigger {
    /// trigger = no or trigger = yes
    Boolean,
    /// can be a script value
    CompareValue,
    /// can be a script value; warn if =
    #[cfg(any(feature = "ck3", feature = "hoi4"))]
    CompareValueWarnEq,
    /// can be a script value; no < or >
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    SetValue,
    /// value must be a valid date
    CompareDate,
    /// trigger is either = item or compared to another trigger
    #[cfg(feature = "vic3")]
    ItemOrCompareValue(Item),
    /// trigger is compared to a scope object
    Scope(Scopes),
    /// trigger is compared to a scope object which may be `this`
    ScopeOkThis(Scopes),
    /// value is chosen from an item type
    Item(Item),
    ScopeOrItem(Scopes, Item),
    /// value is chosen from a list given here
    Choice(&'static [&'static str]),
    /// value is from a list given here that can be compared
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    CompareChoice(&'static [&'static str]),
    /// like `CompareChoice` but value can also be just a number
    #[cfg(feature = "vic3")]
    CompareChoiceOrNumber(&'static [&'static str]),
    /// For Block, if a field name in the array starts with ? it means that field is optional
    /// trigger takes a block with these fields
    Block(&'static [(&'static str, Trigger)]),
    /// trigger takes a block with these fields
    #[cfg(feature = "ck3")]
    ScopeOrBlock(Scopes, &'static [(&'static str, Trigger)]),
    /// trigger takes a block with these fields
    #[cfg(feature = "ck3")]
    ItemOrBlock(Item, &'static [(&'static str, Trigger)]),
    /// trigger takes a single identifier or a block with these fields
    #[cfg(feature = "ck3")]
    IdentifierOrBlock(&'static str, &'static [(&'static str, Trigger)]),
    /// can be part of a scope chain but also a standalone trigger
    #[cfg(any(feature = "ck3", feature = "vic3"))]
    BlockOrCompareValue(&'static [(&'static str, Trigger)]),
    /// trigger takes a block of values of this scope type
    #[cfg(feature = "ck3")]
    ScopeList(Scopes),
    /// trigger takes a block comparing two scope objects
    #[cfg(feature = "ck3")]
    ScopeCompare(Scopes),
    /// this is for inside a Block, where a key is compared to a scope object
    #[cfg(feature = "ck3")]
    CompareToScope(Scopes),
    /// trigger is an iterator that does not follow the regular pattern
    #[cfg(feature = "hoi4")]
    Iterator(ListType, Scopes),
    /// trigger takes a single word
    Identifier(&'static str),
    /// trigger takes a flag name
    #[cfg(feature = "hoi4")]
    Flag,
    /// trigger takes a flag name or a block
    #[cfg(feature = "hoi4")]
    FlagOrBlock(&'static [(&'static str, Trigger)]),

    #[cfg(any(feature = "ck3", feature = "vic3"))]
    Removed(&'static str, &'static str),

    /// this key opens another trigger block
    Control,
    /// this has specific code for validation
    Special,

    UncheckedValue,
    #[allow(dead_code)]
    UncheckedTodo,
}

/// This function checks if the trigger is one that can be used at the end of a scope chain on the
/// right-hand side of a comparator.
pub fn trigger_comparevalue(name: &Token, data: &Everything) -> Option<Scopes> {
    match (Game::game(), scope_trigger(name, data)) {
        #[cfg(feature = "ck3")]
        (
            Game::Ck3,
            Some((
                s,
                Trigger::CompareValue
                | Trigger::CompareValueWarnEq
                | Trigger::CompareDate
                | Trigger::SetValue
                | Trigger::BlockOrCompareValue(_)
                | Trigger::CompareChoice(_),
            )),
        ) => Some(s),
        #[cfg(feature = "vic3")]
        (
            Game::Vic3,
            Some((
                s,
                Trigger::CompareValue
                | Trigger::CompareDate
                | Trigger::BlockOrCompareValue(_)
                | Trigger::ItemOrCompareValue(_)
                | Trigger::CompareChoice(_)
                | Trigger::CompareChoiceOrNumber(_),
            )),
        ) => Some(s),
        #[cfg(feature = "imperator")]
        (Game::Imperator, Some((s, Trigger::CompareValue | Trigger::CompareDate))) => Some(s),
        #[cfg(feature = "hoi4")]
        (
            Game::Hoi4,
            Some((s, Trigger::CompareValue | Trigger::CompareValueWarnEq | Trigger::CompareDate)),
        ) => Some(s),
        _ => std::option::Option::None,
    }
}

// This function works around the problem of needing cfg-specific conditions in an else if
#[inline]
#[allow(unused_variables)]
pub fn is_character_token(part: &str, data: &Everything) -> bool {
    #[cfg(feature = "hoi4")]
    if Game::is_hoi4() {
        return data.item_exists(Item::Character, part);
    }
    false
}