mln-rs-expression 0.0.12

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
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
// The compound expressions: the operators the style specification defines by
// their signatures. Each signature states its parameter and result types and
// names its operator ([`Op`]), which typed evaluation matches on. The
// registry keeps the overloads of a name in the order parsing tries them.

use std::borrow::Cow;
use std::sync::LazyLock;

use feature::{Identifier as FeatureIdentifier, Value as MbglValue};
use mln_conversion::Convertible;
use mln_math::{fmax, fmin};
use mln_util::geometry::FeatureType;
use mln_util::hash::HashMap;
use mln_util::{Color, i18n};

use crate::check_subtype::check_subtype;
use crate::expression::{
    Dependency, EvalResult, EvaluationContext, EvaluationError, EvaluationResult, Expr, Node, Output,
    mbgl_to_expression_value,
};
use crate::parsing_context::{ParseResult, ParsingContext};
use crate::type_::{self, Type};
use crate::utf8_op_helpers::get_unicode_character_offset_on_validated_utf8;
use crate::util::rgba;
use crate::value::{ObjectType, Value, from_mbgl_value_owned, to_string, type_of};

/// The parameter list of a signature: fixed types or `VarargsType`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Params {
    Fixed(Vec<Type>),
    Varargs(Type),
}

/// The operator of a compound expression signature.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, mln_wire::Wire)]
pub(crate) enum Op {
    E,
    Pi,
    Ln2,
    Typeof,
    ToRgba,
    Rgba,
    Rgb,
    Zoom,
    HeatmapDensity,
    Elevation,
    LineProgress,
    Accumulated,
    Has,
    HasObject,
    Get,
    GetObject,
    Properties,
    GeometryType,
    Id,
    Add,
    Sub,
    Neg,
    Mul,
    Div,
    Rem,
    Pow,
    Sqrt,
    Log10,
    Ln,
    Log2,
    Sin,
    Cos,
    Tan,
    Asin,
    Acos,
    Atan,
    Min,
    Max,
    Round,
    Floor,
    Ceil,
    Abs,
    Not,
    IsSupportedScript,
    Upcase,
    Downcase,
    Concat,
    Split,
    Join,
    ResolvedLocale,
    Error,
    FeatureState,
    GlobalState,
}

pub(crate) struct SignatureBase {
    pub(crate) result: Type,
    pub(crate) params: Params,
    pub(crate) name: &'static str,
    pub(crate) dependencies: Dependency,
    pub(crate) op: Op,
}

fn sig(name: &'static str, result: Type, params: Params, dependencies: Dependency, op: Op) -> SignatureBase {
    SignatureBase { result, params, name, dependencies, op }
}

fn fixed(types: &[Type]) -> Params {
    Params::Fixed(types.to_vec())
}

static REGISTRY: LazyLock<Vec<SignatureBase>> = LazyLock::new(|| {
    use Dependency as D;
    use Type as T;
    let s = T::String;
    let n = T::Number;
    let b = T::Boolean;
    let none = || fixed(&[]);
    let num1 = || fixed(std::slice::from_ref(&T::Number));
    let num2 = || fixed(&[T::Number, T::Number]);
    let str1 = || fixed(std::slice::from_ref(&T::String));
    let mut registry = vec![
        sig("e", n.clone(), none(), D::NONE, Op::E),
        sig("pi", n.clone(), none(), D::NONE, Op::Pi),
        sig("ln2", n.clone(), none(), D::NONE, Op::Ln2),
        sig("typeof", s.clone(), fixed(&[T::Value]), D::NONE, Op::Typeof),
        sig("to-rgba", T::array_n(T::Number, 4), fixed(&[T::Color]), D::NONE, Op::ToRgba),
        sig("rgba", T::Color, fixed(&[n.clone(), n.clone(), n.clone(), n.clone()]), D::NONE, Op::Rgba),
        sig("rgb", T::Color, fixed(&[n.clone(), n.clone(), n.clone()]), D::NONE, Op::Rgb),
        sig("zoom", n.clone(), none(), D::ZOOM, Op::Zoom),
        sig("heatmap-density", n.clone(), none(), D::PROPERTY_INPUT, Op::HeatmapDensity),
        sig("elevation", n.clone(), none(), D::ELEVATION, Op::Elevation),
        sig("line-progress", n.clone(), none(), D::PROPERTY_INPUT, Op::LineProgress),
        sig("accumulated", T::Value, none(), D::PROPERTY_INPUT, Op::Accumulated),
        sig("has", b.clone(), str1(), D::FEATURE, Op::Has),
        sig("has", b.clone(), fixed(&[s.clone(), T::Object]), D::NONE, Op::HasObject),
        sig("get", T::Value, str1(), D::FEATURE, Op::Get),
        sig("get", T::Value, fixed(&[s.clone(), T::Object]), D::NONE, Op::GetObject),
        sig("properties", T::Object, none(), D::FEATURE, Op::Properties),
        sig("geometry-type", s.clone(), none(), D::FEATURE, Op::GeometryType),
        sig("id", T::Value, none(), D::FEATURE, Op::Id),
        sig("+", n.clone(), Params::Varargs(n.clone()), D::NONE, Op::Add),
        sig("-", n.clone(), num2(), D::NONE, Op::Sub),
        sig("-", n.clone(), num1(), D::NONE, Op::Neg),
        sig("*", n.clone(), Params::Varargs(n.clone()), D::NONE, Op::Mul),
        sig("/", n.clone(), num2(), D::NONE, Op::Div),
        sig("%", n.clone(), num2(), D::NONE, Op::Rem),
        sig("^", n.clone(), num2(), D::NONE, Op::Pow),
    ];
    for (name, op) in [
        ("sqrt", Op::Sqrt),
        ("log10", Op::Log10),
        ("ln", Op::Ln),
        ("log2", Op::Log2),
        ("sin", Op::Sin),
        ("cos", Op::Cos),
        ("tan", Op::Tan),
        ("asin", Op::Asin),
        ("acos", Op::Acos),
        ("atan", Op::Atan),
    ] {
        registry.push(sig(name, n.clone(), num1(), D::NONE, op));
    }
    registry.extend([
        sig("min", n.clone(), Params::Varargs(n.clone()), D::NONE, Op::Min),
        sig("max", n.clone(), Params::Varargs(n.clone()), D::NONE, Op::Max),
        sig("round", n.clone(), num1(), D::NONE, Op::Round),
        sig("floor", n.clone(), num1(), D::NONE, Op::Floor),
        sig("ceil", n.clone(), num1(), D::NONE, Op::Ceil),
        sig("abs", n.clone(), num1(), D::NONE, Op::Abs),
        sig("!", b.clone(), fixed(&[T::Boolean]), D::NONE, Op::Not),
        sig("is-supported-script", b.clone(), str1(), D::NONE, Op::IsSupportedScript),
        sig("upcase", s.clone(), str1(), D::NONE, Op::Upcase),
        sig("downcase", s.clone(), str1(), D::NONE, Op::Downcase),
        sig("concat", s.clone(), Params::Varargs(T::Value), D::NONE, Op::Concat),
        sig("split", T::array(s.clone()), fixed(&[s.clone(), s.clone()]), D::NONE, Op::Split),
        sig("join", s.clone(), fixed(&[T::array(s.clone()), s.clone()]), D::NONE, Op::Join),
        sig("resolved-locale", s.clone(), fixed(&[T::Collator]), D::NONE, Op::ResolvedLocale),
        sig("error", T::Error, str1(), D::NONE, Op::Error),
        sig("feature-state", T::Value, str1(), D::FEATURE | D::FEATURE_STATE, Op::FeatureState),
        sig("global-state", T::Value, str1(), D::GLOBAL_STATE, Op::GlobalState),
    ]);
    registry
});

/// The signatures of each name, in definition order.
static BY_NAME: LazyLock<HashMap<&'static str, Vec<&'static SignatureBase>>> = LazyLock::new(|| {
    let mut by_name: HashMap<&'static str, Vec<&'static SignatureBase>> = HashMap::default();
    for signature in REGISTRY.iter() {
        by_name.entry(signature.name).or_default().push(signature);
    }
    by_name
});

/// The signature of each op.
static BY_OP: LazyLock<HashMap<Op, &'static SignatureBase>> = LazyLock::new(|| {
    let by_op: HashMap<Op, &'static SignatureBase> =
        REGISTRY.iter().map(|signature| (signature.op, signature)).collect();
    debug_assert_eq!(by_op.len(), REGISTRY.len(), "two signatures share an op");
    by_op
});

/// The signatures registered under `name`, in definition order.
fn definitions(name: &str) -> &'static [&'static SignatureBase] {
    BY_NAME.get(name).map_or(&[], Vec::as_slice)
}

pub(crate) fn feature_unavailable() -> EvaluationError {
    EvaluationError::new("Feature data is unavailable in the current evaluation context.")
}

/// A feature id as a feature value: `None` for no id.
pub(crate) fn identifier_to_mbgl(id: FeatureIdentifier) -> Option<MbglValue> {
    match id {
        FeatureIdentifier::Null => None,
        FeatureIdentifier::Uint(v) => Some(MbglValue::Uint(v)),
        FeatureIdentifier::Int(v) => Some(MbglValue::Int(v)),
        FeatureIdentifier::Double(v) => Some(MbglValue::Double(v)),
        FeatureIdentifier::String(v) => Some(MbglValue::String(v)),
    }
}

fn identifier_to_expression_value(id: FeatureIdentifier) -> Value {
    match id {
        FeatureIdentifier::Null => Value::Null,
        FeatureIdentifier::Uint(v) => Value::Number(v as f64),
        FeatureIdentifier::Int(v) => Value::Number(v as f64),
        FeatureIdentifier::Double(v) => Value::Number(v),
        FeatureIdentifier::String(v) => Value::String(v),
    }
}

fn feature_type_as_string(t: FeatureType) -> &'static str {
    match t {
        FeatureType::Point => "Point",
        FeatureType::LineString => "LineString",
        FeatureType::Polygon => "Polygon",
        FeatureType::Unknown => "Unknown",
    }
}

/// An operator with a name and typed arguments.
#[derive(Clone, mln_wire::Wire)]
pub(crate) struct CompoundExpression {
    #[wire(with = "signature_by_op")]
    signature: &'static SignatureBase,
    args: Box<[Expr]>,
}

/// A signature crosses as its op, which only it has.
mod signature_by_op {

    use super::{BY_OP, Op, SignatureBase};

    #[expect(clippy::trivially_copy_pass_by_ref, reason = "`#[wire(with)]` passes the field by reference")]
    pub(super) fn write(signature: &&'static SignatureBase, w: &mut mln_wire::Writer) {
        mln_wire::Wire::write(&signature.op, w);
    }

    pub(super) fn read(r: &mut mln_wire::Reader<'_>) -> Option<&'static SignatureBase> {
        let op = <Op as mln_wire::Wire>::read(r)?;
        BY_OP.get(&op).copied().or_else(|| r.fail("no signature for an op"))
    }
}

impl PartialEq for CompoundExpression {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(self.signature, other.signature) && self.args == other.args
    }
}

impl CompoundExpression {
    /// The signature the arguments matched.
    #[must_use]
    pub(crate) fn signature(&self) -> &'static SignatureBase {
        self.signature
    }

    #[must_use]
    pub(crate) fn args(&self) -> &[Expr] {
        &self.args
    }

    pub(crate) fn exists(name: &str) -> bool {
        BY_NAME.contains_key(name)
    }

    pub(crate) fn serialize(&self) -> MbglValue {
        let mut serialized = vec![MbglValue::String(self.signature.name.to_owned())];
        serialized.extend(self.args.iter().map(Expr::serialize));
        MbglValue::Array(serialized)
    }

    /// The error of the "error" operator.
    fn error(&self, params: &EvaluationContext<'_>) -> EvaluationError {
        match self.args[0].eval_str(params) {
            Ok(message) => EvaluationError::new(message.into_owned()),
            Err(e) => e,
        }
    }

    fn feature<'p>(
        params: &EvaluationContext<'p>,
    ) -> EvalResult<&'p dyn mln_tile_id::geometry_tile_data::GeometryTileFeature> {
        params.feature.ok_or_else(feature_unavailable)
    }

    pub(crate) fn eval_num(&self, params: &EvaluationContext<'_>) -> EvalResult<f64> {
        let a = &self.args;
        let num = |i: usize| a[i].eval_num(params);
        Ok(match self.signature.op {
            Op::E => std::f64::consts::E,
            Op::Pi => std::f64::consts::PI,
            Op::Ln2 => std::f64::consts::LN_2,
            Op::Zoom => match params.zoom {
                Some(zoom) => f64::from(zoom),
                None => {
                    return Err(EvaluationError::new(
                        "The 'zoom' expression is unavailable in the current evaluation context.",
                    ));
                }
            },
            Op::HeatmapDensity => params.color_ramp_parameter.ok_or_else(|| {
                EvaluationError::new(
                    "The 'heatmap-density' expression is unavailable in the current evaluation context.",
                )
            })?,
            // For color-relief, elevation comes as the color ramp parameter;
            // for 3D terrain, as the elevation. While parsing, 0 lets parsing
            // succeed: the elevation dependency keeps the expression from
            // folding into a constant.
            Op::Elevation => params.color_ramp_parameter.or(params.elevation.map(f64::from)).unwrap_or(0.0),
            Op::LineProgress => params.color_ramp_parameter.ok_or_else(|| {
                EvaluationError::new("The 'line-progress' expression is unavailable in the current evaluation context.")
            })?,
            Op::Add => {
                let mut sum = 0.0;
                for arg in a {
                    sum += arg.eval_num(params)?;
                }
                sum
            }
            Op::Mul => {
                let mut product = 1.0;
                for arg in a {
                    product *= arg.eval_num(params)?;
                }
                product
            }
            Op::Min => {
                let mut result = f64::INFINITY;
                for arg in a {
                    result = fmin(arg.eval_num(params)?, result);
                }
                result
            }
            Op::Max => {
                let mut result = f64::NEG_INFINITY;
                for arg in a {
                    result = fmax(arg.eval_num(params)?, result);
                }
                result
            }
            Op::Sub => num(0)? - num(1)?,
            Op::Neg => -num(0)?,
            Op::Div => num(0)? / num(1)?,
            // Rust's float `%` is C fmod.
            Op::Rem => num(0)? % num(1)?,
            Op::Pow => num(0)?.powf(num(1)?),
            Op::Sqrt => num(0)?.sqrt(),
            Op::Log10 => num(0)?.log10(),
            Op::Ln => num(0)?.ln(),
            Op::Log2 => num(0)?.log2(),
            Op::Sin => num(0)?.sin(),
            Op::Cos => num(0)?.cos(),
            Op::Tan => num(0)?.tan(),
            Op::Asin => num(0)?.asin(),
            Op::Acos => num(0)?.acos(),
            Op::Atan => num(0)?.atan(),
            // C `round` rounds half away from zero, as f64::round does.
            Op::Round => num(0)?.round(),
            Op::Floor => num(0)?.floor(),
            Op::Ceil => num(0)?.ceil(),
            Op::Abs => num(0)?.abs(),
            Op::Error => return Err(self.error(params)),
            _ => return f64::from_value(self.evaluate(params)?),
        })
    }

    pub(crate) fn eval_bool(&self, params: &EvaluationContext<'_>) -> EvalResult<bool> {
        let a = &self.args;
        let op = self.signature.op;
        Ok(match op {
            Op::Not => !a[0].eval_bool(params)?,
            Op::Has => {
                let key = a[0].eval_str(params)?;
                Self::feature(params)?.get_value(&key).is_some()
            }
            Op::HasObject => {
                let key = a[0].eval_str(params)?;
                let object = a[1].evaluate_ref(params)?;
                let Value::Object(object) = &*object else { unreachable!("object argument") };
                object.contains_key(&*key)
            }
            Op::IsSupportedScript => is_supported_script(params, &a[0].eval_str(params)?),
            Op::Error => return Err(self.error(params)),
            _ => return bool::from_value(self.evaluate(params)?),
        })
    }

    pub(crate) fn eval_str<'e>(&'e self, params: &EvaluationContext<'_>) -> EvalResult<Cow<'e, str>> {
        let a = &self.args;
        Ok(Cow::Owned(match self.signature.op {
            Op::Typeof => type_::to_string(&type_of(&*a[0].evaluate_ref(params)?)),
            Op::GeometryType => return Ok(Cow::Borrowed(feature_type_as_string(Self::feature(params)?.get_type()))),
            Op::Upcase => a[0].eval_str(params)?.to_uppercase(),
            Op::Downcase => a[0].eval_str(params)?.to_lowercase(),
            Op::Concat => {
                let mut s = String::new();
                for arg in a {
                    match &*arg.evaluate_ref(params)? {
                        Value::String(part) => s += part,
                        other => s += &to_string(other),
                    }
                }
                s
            }
            Op::Join => {
                let input = a[0].evaluate_ref(params)?;
                let Value::Array(input) = &*input else { unreachable!("array argument") };
                let input: Vec<&str> = input.iter().map(|v| v.as_str().expect("string item")).collect();
                input.join(&a[1].eval_str(params)?)
            }
            Op::ResolvedLocale => {
                let collator = a[0].evaluate_ref(params)?;
                let Value::Collator(collator) = &*collator else { unreachable!("collator argument") };
                collator.resolved_locale().to_owned()
            }
            Op::Error => return Err(self.error(params)),
            _ => return Cow::from_value(self.evaluate(params)?),
        }))
    }

    pub(crate) fn eval_color(&self, params: &EvaluationContext<'_>) -> EvalResult<Color> {
        let a = &self.args;
        let num = |i: usize| a[i].eval_num(params);
        match self.signature.op {
            Op::Rgba => rgba(num(0)?, num(1)?, num(2)?, num(3)?),
            Op::Rgb => rgba(num(0)?, num(1)?, num(2)?, 1.0),
            Op::Error => Err(self.error(params)),
            _ => Color::from_value(self.evaluate(params)?),
        }
    }

    pub(crate) fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
        match self.signature.result {
            Type::Number => return self.eval_num(params).map(Value::Number),
            Type::Boolean => return self.eval_bool(params).map(Value::Bool),
            Type::String => return self.eval_str(params).map(|s| Value::String(s.into_owned())),
            Type::Color => return self.eval_color(params).map(Value::Color),
            _ => {}
        }
        let a = &self.args;
        match self.signature.op {
            Op::ToRgba => {
                let color = a[0].eval_color(params)?;
                Ok(Value::Array(color.to_array().iter().map(|v| Value::Number(*v)).collect()))
            }
            Op::Accumulated => match params.accumulated {
                None => Err(EvaluationError::new(
                    "The 'accumulated' expression is unavailable in the current evaluation context.",
                )),
                Some(v) => Ok(mbgl_to_expression_value(v)),
            },
            Op::Get => {
                let key = a[0].eval_str(params)?;
                Ok(Self::feature(params)?.get_value(&key).map_or(Value::Null, from_mbgl_value_owned))
            }
            Op::GetObject => {
                let key = a[0].eval_str(params)?;
                let object = a[1].evaluate_ref(params)?;
                let Value::Object(object) = &*object else { unreachable!("object argument") };
                Ok(object.get(&*key).cloned().unwrap_or(Value::Null))
            }
            Op::Properties => {
                let mut result = ObjectType::new();
                for (k, v) in Self::feature(params)?.get_properties().iter() {
                    result.insert(k.clone(), mbgl_to_expression_value(v));
                }
                Ok(Value::Object(result))
            }
            Op::Id => Ok(identifier_to_expression_value(Self::feature(params)?.get_id())),
            Op::Split => {
                let input = a[0].eval_str(params)?;
                let delimiter = a[1].eval_str(params)?;
                Ok(Value::Array(split(&input, &delimiter)))
            }
            Op::FeatureState => {
                let key = a[0].eval_str(params)?;
                let state = params.feature_state.and_then(|state| state.get(&*key));
                Ok(state.map_or(Value::Null, mbgl_to_expression_value))
            }
            Op::GlobalState => {
                let key = a[0].eval_str(params)?;
                let state = params.global_state.and_then(|state| state.get(&*key));
                Ok(state.map_or(Value::Null, mbgl_to_expression_value))
            }
            Op::Error => Err(self.error(params)),
            op => unreachable!("{op:?} has a typed result"),
        }
    }
}

fn split(input: &str, delimiter: &str) -> Vec<Value> {
    let mut result = Vec::new();
    if delimiter.is_empty() {
        let mut remaining = input;
        while !remaining.is_empty() {
            let character_length = get_unicode_character_offset_on_validated_utf8(remaining, 1);
            result.push(Value::String(remaining[..character_length].to_owned()));
            remaining = &remaining[character_length..];
        }
        return result;
    }
    let mut start = 0;
    loop {
        match input[start..].find(delimiter) {
            None => {
                result.push(Value::String(input[start..].to_owned()));
                break;
            }
            Some(offset) => {
                let position = start + offset;
                result.push(Value::String(input[start..position].to_owned()));
                start = position + delimiter.len();
            }
        }
    }
    result
}

fn is_supported_script(params: &EvaluationContext<'_>, x: &str) -> bool {
    let Some(font_faces) = params.font_faces.filter(|faces| !faces.is_empty()) else {
        return i18n::is_string_in_supported_script(x);
    };
    x.encode_utf16().all(|chr| {
        i18n::char_in_supported_script(chr)
            || font_faces
                .iter()
                .any(|face| face.ranges.iter().any(|range| u32::from(chr) >= range.0 && u32::from(chr) <= range.1))
    })
}

fn expected_types_error(definitions: &[&'static SignatureBase], args: &[Expr]) -> String {
    // Only used if there are no overloads with matching number of args
    let mut available_overloads: Vec<String> = Vec::new();
    let mut overloads: Vec<String> = Vec::new();
    for signature in definitions {
        match &signature.params {
            Params::Varargs(t) => {
                overloads.push(format!("({})", type_::to_string(t)));
            }
            Params::Fixed(params) => {
                let overload = format!("({})", params.iter().map(type_::to_string).collect::<Vec<_>>().join(", "));
                if params.len() == args.len() {
                    overloads.push(overload);
                } else {
                    available_overloads.push(overload);
                }
            }
        }
    }

    let signatures = if overloads.is_empty() { available_overloads.join(" | ") } else { overloads.join(" | ") };

    let actual_types = args.iter().map(|arg| type_::to_string(&arg.get_type())).collect::<Vec<_>>().join(", ");

    format!("Expected arguments of type {signatures}, but found ({actual_types}) instead.")
}

fn create_compound_expression_from(
    definitions: &[&'static SignatureBase],
    args: Vec<Expr>,
    ctx: &mut ParsingContext,
) -> ParseResult {
    let mut signature_context = ParsingContext::with_key(ctx.get_key());

    for signature in definitions {
        signature_context.clear_errors();

        match &signature.params {
            Params::Fixed(params) => {
                if params.len() != args.len() {
                    signature_context.error(format!(
                        "Expected {} arguments, but found {} instead.",
                        params.len(),
                        args.len()
                    ));
                    continue;
                }

                for (i, arg) in args.iter().enumerate() {
                    if let Some(err) = check_subtype(&params[i], &arg.get_type()) {
                        signature_context.error_at(err, i + 1);
                    }
                }
            }
            Params::Varargs(param_type) => {
                for (i, arg) in args.iter().enumerate() {
                    if let Some(err) = check_subtype(param_type, &arg.get_type()) {
                        signature_context.error_at(err, i + 1);
                    }
                }
            }
        }

        if !signature_context.has_errors() {
            return Some(Expr::new(Node::Compound(Box::new(CompoundExpression { signature, args: args.into() }))));
        }
    }

    if definitions.len() == 1 {
        ctx.append_errors(&signature_context);
    } else {
        ctx.error(expected_types_error(definitions, &args));
    }

    None
}

pub(crate) fn parse_compound_expression(name: &str, value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
    debug_assert!(value.is_array() && value.array_length() > 0);

    let definitions = definitions(name);
    if definitions.is_empty() {
        ctx.error_at(
            format!(r#"Unknown expression "{name}". If you wanted a literal array, use ["literal", [...]]."#),
            0,
        );
        return None;
    }

    let length = value.array_length();

    // Per the style specification, the "global-state" property name must be
    // a string literal, not the result of a sub-expression.
    if name == "global-state" && length == 2 && value.array_member(1).to_string().is_none() {
        ctx.error_at("Global state property must be a string literal.".to_owned(), 1);
        return None;
    }

    // Each failed overload parses its arguments, so two overloads of one arity
    // would make parsing a nested chain of them exponential in its depth.
    let mut overload_matched_arity = false;
    for signature in definitions {
        let matches = match &signature.params {
            Params::Varargs(_) => true,
            Params::Fixed(p) => p.len() == length - 1,
        };
        if matches {
            debug_assert!(!overload_matched_arity);
            overload_matched_arity = true;

            // First parse all the args, potentially coercing to the types
            // expected by this overload.
            ctx.clear_errors();
            let mut arg_parse_failed = false;
            let mut args = Vec::with_capacity(length - 1);

            for i in 1..length {
                let expected = match &signature.params {
                    Params::Varargs(t) => t.clone(),
                    Params::Fixed(params) => params[i - 1].clone(),
                };

                let Some(parsed) = ctx.parse(&value.array_member(i), i, Some(expected), None) else {
                    arg_parse_failed = true;
                    break;
                };
                args.push(parsed);
            }
            // When the args couldn't be coerced to this overload's expected
            // types, move on to the next one.
            if !arg_parse_failed {
                let parse_with_args = create_compound_expression_from(definitions, args, ctx);
                if parse_with_args.is_some() {
                    return parse_with_args;
                }
            }
        }
    }
    // The overload that takes this many arguments failed, and its errors say
    // why. Parsing the arguments again would double the work at every level.
    if overload_matched_arity {
        return None;
    }

    // No overload takes this many arguments. Parse the arguments without
    // expected types just for the error message
    ctx.clear_errors();
    let mut args = Vec::with_capacity(length - 1);

    for i in 1..length {
        let parsed = ctx.parse(&value.array_member(i), i, None, None)?;
        args.push(parsed);
    }

    ctx.error(expected_types_error(definitions, &args));

    None
}

pub fn create_compound_expression(name: &str, args: Vec<Expr>, ctx: &mut ParsingContext) -> ParseResult {
    create_compound_expression_from(definitions(name), args, ctx)
}