tiger_lib/
validate.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
//! Validation functions that are useful for more than one data module.

use std::fmt::{Display, Formatter};

use crate::block::{Block, BV};
#[cfg(feature = "ck3")]
use crate::ck3::validate::{
    validate_activity_modifier, validate_ai_value_modifier, validate_compare_modifier,
    validate_compatibility_modifier, validate_opinion_modifier, validate_scheme_modifier,
};
use crate::context::ScopeContext;
use crate::data::scripted_modifiers::ScriptedModifier;
use crate::everything::Everything;
use crate::game::Game;
use crate::item::Item;
use crate::lowercase::Lowercase;
use crate::report::{err, fatal, report, warn, Confidence, ErrorKey, Severity};
#[cfg(feature = "ck3")]
use crate::scopes::Scopes;
use crate::scopes::{scope_prefix, scope_to_scope};
use crate::script_value::{validate_non_dynamic_script_value, validate_script_value};
use crate::token::Token;
use crate::tooltipped::Tooltipped;
#[cfg(feature = "ck3")]
use crate::trigger::validate_target_ok_this;
use crate::trigger::{
    partition, validate_argument, validate_argument_scope, validate_inscopes, validate_trigger,
    validate_trigger_internal, warn_not_first, Part, PartFlags,
};
use crate::validator::Validator;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ListType {
    None,
    Any,
    Every,
    Ordered,
    Random,
}

impl Display for ListType {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        match self {
            ListType::None => write!(f, ""),
            ListType::Any => write!(f, "any"),
            ListType::Every => write!(f, "every"),
            ListType::Ordered => write!(f, "ordered"),
            ListType::Random => write!(f, "random"),
        }
    }
}

impl TryFrom<&str> for ListType {
    type Error = std::fmt::Error;

    fn try_from(from: &str) -> Result<Self, Self::Error> {
        match from {
            "" => Ok(ListType::None),
            "any" => Ok(ListType::Any),
            "every" => Ok(ListType::Every),
            "ordered" => Ok(ListType::Ordered),
            "random" => Ok(ListType::Random),
            _ => Err(std::fmt::Error),
        }
    }
}

#[cfg(not(feature = "imperator"))]
pub fn validate_compare_duration(block: &Block, data: &Everything, sc: &mut ScopeContext) {
    let mut vd = Validator::new(block, data);
    let mut count = 0;

    for field in &["days", "weeks", "months", "years"] {
        if let Some(bv) = vd.field_any_cmp(field) {
            validate_script_value(bv, data, sc);
            count += 1;
        }
    }

    if count != 1 {
        let msg = "must have 1 of days, weeks, months, or years";
        let key = if count == 0 { ErrorKey::FieldMissing } else { ErrorKey::Validation };
        err(key).msg(msg).loc(block).push();
    }
}

// Very similar to validate_days_weeks_months_years, but requires = instead of allowing comparators
// "weeks" is not documented but is used all over vanilla TODO: verify
pub fn validate_mandatory_duration(block: &Block, vd: &mut Validator, sc: &mut ScopeContext) {
    let mut count = 0;

    for field in &["days", "weeks", "months", "years"] {
        if vd.field_script_value(field, sc) {
            count += 1;
        }
    }

    if count != 1 {
        let msg = "must have 1 of days, weeks, months, or years";
        let key = if count == 0 { ErrorKey::FieldMissing } else { ErrorKey::Validation };
        err(key).msg(msg).loc(block).push();
    }
}

pub fn validate_duration(block: &Block, data: &Everything, sc: &mut ScopeContext) {
    let mut vd = Validator::new(block, data);
    validate_mandatory_duration(block, &mut vd, sc);
}

// Very similar to validate_duration, but validates part of a block that may contain a duration
// Also does not accept script values (per the documentation)
pub fn validate_optional_duration_int(vd: &mut Validator) {
    let mut count = 0;

    for field in &["days", "weeks", "months", "years"] {
        vd.field_validated_value(field, |key, mut vd| {
            vd.integer();
            count += 1;
            if count > 1 {
                let msg = "must have at most 1 of days, weeks, months, or years";
                err(ErrorKey::Validation).msg(msg).loc(key).push();
            }
        });
    }
}

// Very similar to validate_days_weeks_months_years, but requires = instead of allowing comparators
pub fn validate_optional_duration(vd: &mut Validator, sc: &mut ScopeContext) {
    let mut count = 0;

    #[cfg(not(feature = "imperator"))]
    let options = &["days", "weeks", "months", "years"];

    // Imperator does not allow a "weeks" field and does allow a "duration" field for modifiers.
    #[cfg(feature = "imperator")]
    let options = &["days", "months", "years", "duration"];

    for field in options {
        vd.field_validated_key(field, |key, bv, data| {
            validate_script_value(bv, data, sc);
            count += 1;
            if count > 1 {
                let msg = "must have at most 1 of days, weeks, months, or years";
                err(ErrorKey::Validation).msg(msg).loc(key).push();
            }
        });
    }
}

// Does not accept script values (per the documentation)
pub fn validate_color(block: &Block, _data: &Everything) {
    // Reports in this function are `warn` level because a bad color is just an UI issue,
    // and normal confidence level because I'm not 100% sure of the color formats.
    let mut count = 0;
    // Get the color tag, as in color = hsv { 0.5 1.0 1.0 }
    let tag = block.tag.as_deref().map_or("rgb", Token::as_str);
    for item in block.iter_items() {
        if let Some(t) = item.get_value() {
            if tag == "hsv" {
                t.check_number();
                if let Some(f) = t.get_number() {
                    if !(0.0..=1.0).contains(&f) {
                        // TODO: check if integer color values actually work in hsv,
                        // then adjust the report.
                        let msg = "hsv values should be between 0.0 and 1.0";
                        let mut info = "";
                        if t.is_integer() {
                            info = "did you mean `hsv360`?";
                        }
                        warn(ErrorKey::Colors).weak().msg(msg).info(info).loc(t).push();
                    }
                } else {
                    warn(ErrorKey::Colors).msg("expected hsv value").loc(t).push();
                }
            } else if tag == "hsv360" {
                if let Some(i) = t.get_integer() {
                    if count == 0 && !(0..=360).contains(&i) {
                        let msg = "hsv360 h values should be between 0 and 360";
                        warn(ErrorKey::Colors).msg(msg).loc(t).push();
                    } else if count > 0 && !(0..=100).contains(&i) {
                        let msg = "hsv360 s and v values should be between 0 and 100";
                        warn(ErrorKey::Colors).msg(msg).loc(t).push();
                    }
                } else {
                    warn(ErrorKey::Colors).msg("expected hsv360 value").loc(t).push();
                }
            } else if let Some(i) = t.get_integer() {
                if !(0..=255).contains(&i) {
                    let msg = "color values should be between 0 and 255";
                    warn(ErrorKey::Colors).msg(msg).loc(t).push();
                }
            } else if let Some(f) = t.get_number() {
                t.check_number();
                if !(0.0..=1.0).contains(&f) {
                    let msg = "color values should be between 0.0 and 1.0";
                    warn(ErrorKey::Colors).msg(msg).loc(t).push();
                }
            } else {
                warn(ErrorKey::Colors).msg("expected color value").loc(t).push();
            }
            count += 1;
        }
    }
    if count != 3 && count != 4 {
        warn(ErrorKey::Colors).msg("expected 3 or 4 color values").loc(block).push();
    }
}

pub fn validate_possibly_named_color(bv: &BV, data: &Everything) {
    match bv {
        BV::Value(token) => data.verify_exists(Item::NamedColor, token),
        BV::Block(block) => validate_color(block, data),
    }
}

/// Check some iterator fields *before* the list scope has opened.
#[allow(unused_variables)] // `name` is only used for ck3
pub fn precheck_iterator_fields(
    ltype: ListType,
    name: &str,
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    match ltype {
        ListType::Any => {
            if let Some(bv) = block.get_field("percent") {
                if let Some(token) = bv.get_value() {
                    if let Some(num) = token.get_number() {
                        token.check_number();
                        if num > 1.0 {
                            let msg = "'percent' here needs to be between 0 and 1";
                            warn(ErrorKey::Range).msg(msg).loc(token).push();
                        }
                    }
                }
                validate_script_value(bv, data, sc);
            }
            if let Some(bv) = block.get_field("count") {
                match bv {
                    BV::Value(token) if token.is("all") => (),
                    bv => validate_script_value(bv, data, sc),
                }
            };
        }
        ListType::Ordered => {
            for field in &["min", "max"] {
                if let Some(bv) = block.get_field(field) {
                    validate_script_value(bv, data, sc);
                }
            }
            if let Some(bv) = block.get_field("position") {
                if let Some(token) = bv.get_value() {
                    if !token.is("end") {
                        validate_script_value(bv, data, sc);
                    }
                } else {
                    validate_script_value(bv, data, sc);
                }
            }
        }
        ListType::Random | ListType::Every | ListType::None => (),
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() && name == "county_in_region" {
        for region in block.get_field_values("region") {
            if !data.item_exists(Item::Region, region.as_str()) {
                validate_target_ok_this(region, data, sc, Scopes::GeographicalRegion);
            }
        }
    }
    #[cfg(feature = "ck3")]
    if Game::is_ck3() && name == "succession_appointment_investors" {
        if let Some(candidate) = block.get_field_value("candidate") {
            validate_target_ok_this(candidate, data, sc, Scopes::Character);
        }
        if let Some(value) = block.get_field("value") {
            validate_script_value(value, data, sc);
        }
    }
}

/// This checks the fields that are only used in iterators.
/// It does not check "limit" because that is shared with the if/else blocks.
/// Returns true iff the iterator took care of its own tooltips
pub fn validate_iterator_fields(
    caller: &Lowercase,
    list_type: ListType,
    _data: &Everything,
    sc: &mut ScopeContext,
    vd: &mut Validator,
    tooltipped: &mut Tooltipped,
    is_svalue: bool,
) {
    // undocumented
    if list_type == ListType::None {
        vd.ban_field("custom", || "lists");
    } else if vd.field_item("custom", Item::Localization) {
        *tooltipped = Tooltipped::No;
    }

    // undocumented
    if list_type != ListType::None && list_type != ListType::Any {
        vd.multi_field_validated_block("alternative_limit", |b, data| {
            validate_trigger(b, data, sc, *tooltipped);
        });
    } else {
        vd.ban_field("alternative_limit", || "`every_`, `ordered_`, and `random_` lists");
    }

    if list_type == ListType::Any {
        vd.field_any_cmp("percent"); // prechecked
        vd.field_any_cmp("count"); // prechecked
    } else {
        vd.ban_field("percent", || "`any_` lists");
        if caller != "while" {
            vd.ban_field("count", || "`while` and `any_` lists");
        }
    }

    if list_type == ListType::Ordered {
        vd.field_script_value("order_by", sc);
        vd.field("position"); // prechecked
        vd.field("min"); // prechecked
        vd.field("max"); // prechecked
        vd.field_bool("check_range_bounds");
    } else {
        vd.ban_field("order_by", || "`ordered_` lists");
        vd.ban_field("position", || "`ordered_` lists");
        if caller != "random_list" && caller != "duel" && !is_svalue {
            vd.ban_field("min", || "`ordered_` lists, `random_list`, and `duel`");
            vd.ban_field("max", || "`ordered_` lists, `random_list`, and `duel`");
        }
        vd.ban_field("check_range_bounds", || "`ordered_` lists");
    }

    if list_type == ListType::Random {
        vd.field_validated_block_sc("weight", sc, validate_modifiers_with_base);
    } else {
        vd.ban_field("weight", || "`random_` lists");
    }
}

/// This checks the special fields for certain iterators, like `type =` in `every_relation`.
/// It doesn't check the generic ones like `limit` or the ordering ones for `ordered_*`.
#[allow(unused_variables)] // vic3 does not use `tooltipped`
pub fn validate_inside_iterator(
    name: &Lowercase,
    listtype: ListType,
    block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    vd: &mut Validator,
    tooltipped: Tooltipped,
) {
    // Docs say that all three can take either list or variable, but global and local lists must be variable lists.
    if name == "in_list" {
        vd.req_field_one_of(&["list", "variable"]);
        if let Some(token) = vd.field_value("list") {
            sc.expect_list(token);
            sc.replace_list_entry(token.as_str(), token);
        }
    } else if name == "in_local_list" || name == "in_global_list" {
        vd.req_field("variable");
        vd.ban_field("list", || format!("{listtype}_in_list"));
    } else {
        vd.ban_field("list", || format!("{listtype}_in_list"));
        vd.ban_field("variable", || {
            format!(
                "`{listtype}_in_list`, `{listtype}_in_local_list`, or `{listtype}_in_global_list`"
            )
        });
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "in_de_facto_hierarchy" || name == "in_de_jure_hierarchy" {
            vd.field_validated_block("filter", |block, data| {
                validate_trigger(block, data, sc, tooltipped);
            });
            vd.field_validated_block("continue", |block, data| {
                validate_trigger(block, data, sc, tooltipped);
            });
        } else {
            let only_for = || {
                format!("`{listtype}_in_de_facto_hierarchy` or `{listtype}_in_de_jure_hierarchy`")
            };
            vd.ban_field("filter", only_for);
            vd.ban_field("continue", only_for);
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "county_in_region" {
            vd.req_field("region");
            vd.multi_field_value("region"); // prechecked
        } else {
            vd.ban_field("region", || format!("`{listtype}_county_in_region`"));
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "court_position_holder" {
            vd.field_item("type", Item::CourtPosition);
        } else if name == "relation" {
            if !block.has_key("type") {
                let msg = "required field `type` missing";
                let info = format!(
                    "Verified for 1.9.2: with no type, {listtype}_relation will do nothing."
                );
                err(ErrorKey::FieldMissing).strong().msg(msg).info(info).loc(block).push();
            }
            vd.multi_field_item("type", Item::Relation);
        } else {
            vd.ban_field("type", || {
                format!("`{listtype}_court_position_holder` or `{listtype}_relation`")
            });
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "claim" {
            vd.field_choice("explicit", &["yes", "no", "all"]);
            vd.field_choice("pressed", &["yes", "no", "all"]);
        } else {
            vd.ban_field("explicit", || format!("`{listtype}_claim`"));
            vd.ban_field("pressed", || format!("`{listtype}_claim`"));
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "pool_character" {
            vd.req_field("province");
            if let Some(token) = vd.field_value("province") {
                validate_target_ok_this(token, data, sc, Scopes::Province);
            }
        } else {
            vd.ban_field("province", || format!("`{listtype}_pool_character`"));
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if sc.can_be(Scopes::Character) {
            vd.field_bool("only_if_dead");
            vd.field_bool("even_if_dead");
        } else {
            vd.ban_field("only_if_dead", || "lists of characters");
            vd.ban_field("even_if_dead", || "lists of characters");
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "character_struggle" {
            vd.field_choice("involvement", &["involved", "interloper"]);
        } else {
            vd.ban_field("involvement", || format!("`{listtype}_character_struggle`"));
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "connected_county" {
            // Undocumented
            vd.field_bool("invert");
            vd.field_numeric("max_naval_distance");
            vd.field_bool("allow_one_county_land_gap");
        } else {
            let only_for = || format!("`{listtype}_connected_county`");
            vd.ban_field("invert", only_for);
            vd.ban_field("max_naval_distance", only_for);
            vd.ban_field("allow_one_county_land_gap", only_for);
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "activity_phase_location"
            || name == "activity_phase_location_future"
            || name == "activity_phase_location_past"
        {
            vd.field_bool("unique");
        } else {
            let only_for =
                || format!("the `{listtype}_activity_phase_location` family of iterators");
            vd.ban_field("unique", only_for);
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "guest_subset" || name == "guest_subset_current_phase" {
            vd.field_item("name", Item::GuestSubset);
        } else {
            vd.ban_field("name", || {
                format!("`{listtype}_guest_subset` and `{listtype}_guest_subset_current_phase`")
            });
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "guest_subset" {
            vd.field_value("phase"); // TODO
        } else {
            vd.ban_field("phase", || format!("`{listtype}_guest_subset`"));
        }
    }

    if Game::is_ck3() {
        #[cfg(feature = "ck3")]
        if name == "trait_in_category" {
            vd.field_value("category"); // TODO
        } else {
            // Don't ban, because it's a valid trigger
        }
    }

    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        if name == "succession_appointment_investors" {
            vd.req_field("candidate");
            vd.field_value("candidate"); // prechecked
            vd.field_any_cmp("value"); // prechecked
        } else {
            vd.ban_field("candidate", || format!("`{listtype}_succession_appointment_investors`"));
        }
    }
}

pub fn validate_modifiers_with_base(block: &Block, data: &Everything, sc: &mut ScopeContext) {
    let mut vd = Validator::new(block, data);
    vd.field_validated("base", validate_non_dynamic_script_value);
    vd.fields_script_value("add", sc);
    vd.fields_script_value("factor", sc);
    vd.fields_script_value("min", sc);
    vd.fields_script_value("max", sc);
    validate_modifiers(&mut vd, sc);
    validate_scripted_modifier_calls(vd, data, sc);
}

pub fn validate_modifiers(vd: &mut Validator, sc: &mut ScopeContext) {
    vd.multi_field_validated_block("first_valid", |b, data| {
        let mut vd = Validator::new(b, data);
        validate_modifiers(&mut vd, sc);
    });
    vd.multi_field_validated_block("modifier", |b, data| {
        validate_trigger_internal(
            &Lowercase::new_unchecked("modifier"),
            false,
            b,
            data,
            sc,
            Tooltipped::No,
            false,
            Severity::Error,
        );
    });
    #[cfg(feature = "ck3")]
    if Game::is_ck3() {
        vd.multi_field_validated_block_sc("compare_modifier", sc, validate_compare_modifier);
        vd.multi_field_validated_block_sc("opinion_modifier", sc, validate_opinion_modifier);
        vd.multi_field_validated_block_sc("ai_value_modifier", sc, validate_ai_value_modifier);
        vd.multi_field_validated_block_sc(
            "compatibility_modifier",
            sc,
            validate_compatibility_modifier,
        );

        // These are special single-use modifiers
        vd.multi_field_validated_block_sc("scheme_modifier", sc, validate_scheme_modifier);
        vd.multi_field_validated_block_sc("activity_modifier", sc, validate_activity_modifier);
    }

    vd.fields_script_value("min", sc);
    vd.fields_script_value("max", sc);
}

#[cfg(feature = "vic3")]
pub fn validate_vic3_modifiers(vd: &mut Validator, sc: &mut ScopeContext) {
    vd.multi_field_validated("modifier", |bv, data| {
        validate_script_value(bv, data, sc);
    });
}

pub fn validate_scripted_modifier_call(
    key: &Token,
    bv: &BV,
    modifier: &ScriptedModifier,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    match bv {
        BV::Value(token) => {
            if !modifier.macro_parms().is_empty() {
                fatal(ErrorKey::Macro).msg("expected macro arguments").loc(token).push();
            } else if !token.is("yes") {
                warn(ErrorKey::Validation).msg("expected just modifier = yes").loc(token).push();
            }
            modifier.validate_call(key, data, sc);
        }
        BV::Block(block) => {
            let parms = modifier.macro_parms();
            if parms.is_empty() {
                fatal(ErrorKey::Macro)
                    .msg("this scripted modifier does not need macro arguments")
                    .info("you can just use it as modifier = yes")
                    .loc(block)
                    .push();
            } else {
                let mut vec = Vec::new();
                let mut vd = Validator::new(block, data);
                for parm in &parms {
                    if let Some(token) = vd.field_value(parm) {
                        vec.push(token.clone());
                    } else {
                        let msg = format!("this scripted modifier needs parameter {parm}");
                        err(ErrorKey::Macro).msg(msg).loc(block).push();
                        return;
                    }
                }
                vd.unknown_value_fields(|key, _value| {
                    let msg = format!("this scripted modifier 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();
                modifier.validate_macro_expansion(key, &args, data, sc);
            }
        }
    }
}

pub fn validate_scripted_modifier_calls(
    mut vd: Validator,
    data: &Everything,
    sc: &mut ScopeContext,
) {
    vd.unknown_fields(|key, bv| {
        if let Some(modifier) = data.scripted_modifiers.get(key.as_str()) {
            validate_scripted_modifier_call(key, bv, modifier, data, sc);
        } else {
            let msg = format!("unknown field `{key}`");
            warn(ErrorKey::UnknownField).msg(msg).loc(key).push();
        }
    });
}

pub fn validate_ai_chance(bv: &BV, data: &Everything, sc: &mut ScopeContext) {
    match bv {
        BV::Value(t) => _ = t.expect_number(),
        BV::Block(b) => validate_modifiers_with_base(b, data, sc),
    }
}

/// Validate the left-hand part of a `target = { target_scope }` block.
///
/// The caller is expected to have done `sc.open_builder()` before calling and then do `sc.close()` after calling.
/// Returns true iff validation was complete.
/// `qeq` is true if the scope chain is to the left of a ?= operator.
pub fn validate_scope_chain(
    token: &Token,
    data: &Everything,
    sc: &mut ScopeContext,
    qeq: bool,
) -> bool {
    let part_vec = partition(token);
    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 qeq {
            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, arg)) = part.split_once(':') {
                    // known prefix
                    if let Some(entry) = scope_prefix(&prefix) {
                        validate_argument_scope(part_flags, entry, &prefix, &arg, data, sc);
                    } else {
                        let msg = format!("unknown prefix `{prefix}:`");
                        err(ErrorKey::Validation).msg(msg).loc(prefix).push();
                        return false;
                    }
                } 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 let Some((inscopes, outscope)) = scope_to_scope(part, sc.scopes()) {
                    validate_inscopes(part_flags, part, inscopes, sc);
                    sc.replace(outscope, part.clone());
                } else {
                    let msg = format!("unknown token `{part}`");
                    err(ErrorKey::UnknownField).msg(msg).loc(part).push();
                    return false;
                }
            }
        }
    }
    true
}

pub fn validate_ifelse_sequence(block: &Block, key_if: &str, key_elseif: &str, key_else: &str) {
    let mut seen_if = false;
    for (key, block) in block.iter_definitions() {
        if key.is(key_if) {
            seen_if = true;
            continue;
        } else if key.is(key_elseif) {
            if !seen_if {
                let msg = format!("`{key_elseif} without preceding `{key_if}`");
                warn(ErrorKey::IfElse).msg(msg).loc(key).push();
            }
            seen_if = true;
            continue;
        } else if key.is(key_else) {
            if !seen_if {
                let msg = format!("`{key_else} without preceding `{key_if}`");
                warn(ErrorKey::IfElse).msg(msg).loc(key).push();
            }
            if block.has_key("limit") {
                // `else` with a `limit`, followed by another `else`, does work.
                seen_if = true;
                continue;
            }
        }
        seen_if = false;
    }
}

pub fn validate_numeric_range(
    block: &Block,
    data: &Everything,
    min: f64,
    max: f64,
    sev: Severity,
    conf: Confidence,
) {
    let mut vd = Validator::new(block, data);
    let mut count = 0;
    let mut prev = 0.0;

    for token in vd.values() {
        if let Some(n) = token.expect_number() {
            count += 1;
            if !(min..=max).contains(&n) {
                let msg = format!("expected number between {min} and {max}");
                report(ErrorKey::Range, sev).conf(conf).msg(msg).loc(token).push();
            }
            if count == 1 {
                prev = n;
            } else if count == 2 && n < prev {
                let msg = "expected second number to be bigger than first number";
                report(ErrorKey::Range, sev).conf(conf).msg(msg).loc(token).push();
            } else if count == 3 {
                let msg = "expected exactly 2 numbers";
                report(ErrorKey::Range, sev).strong().msg(msg).loc(block).push();
            }
        }
    }
}