flm-core 0.7.0

FLM semantic markup framework — core library, built on techy
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
//! The element declarations a registry was built from, kept as declared:
//! [`Declarations`], and the references into them that its accessors return.
//!
//! A feature declares what an author may write in a document with element
//! declarations ([`ElementDef`]: the names a command is written under, its
//! syntax, its arguments, its parsing modes and its documentation), scoped
//! packages ([`ScopedPackageDef`]), fallbacks ([`FallbackDef`]) and overrides
//! ([`OverrideDef`]). The registry build turns them into the FLM language
//! that documents are parsed with
//! ([`FlmRegistry::lang`](super::FlmRegistry::lang)), which keeps only what a
//! parse needs. The registry also keeps the declarations themselves in a
//! [`Declarations`] value
//! ([`FlmRegistry::declarations`](super::FlmRegistry::declarations)), so
//! that a program can describe what an author may write: every command with
//! its syntax, its arguments and their roles, and its documentation.
//!
//! The declarations are the ones the build used: a [peer element
//! value](super::FeatureBuilder::peer_element_value) whose element type some
//! feature declares is kept where it was declared, and one whose element type
//! no feature declares is not there, since the build dropped it. Each element
//! declaration holds the element value the registry numbered, with the
//! aspects features attached to it.
//!
//! Keeping the declarations costs little: a declaration shares its parsers and
//! its element value with the language through `Arc`s, and only its strings
//! are its own.

use crate::lang::mint::packages::{feature_stack_order, is_weak};
use crate::lang::mint::{
    ArgDef, CallableKind, ElementDef, FallbackDef, OverrideDef, ScopedPackageDef, SyntaxDef,
    TackOnField,
};

/// The element declarations, scoped packages, fallbacks and overrides that
/// the features of a registry declared, as declared.
///
/// The element declarations are in the order in which the language stacks
/// the features' definitions (see [`elements`](Declarations::elements)), so
/// that, in a parsing mode, the last declaration of a command is the one in
/// force. Everything else is in registration order.
///
/// Get it from
/// [`FlmRegistry::declarations`](super::FlmRegistry::declarations). The list
/// accessors ([`elements`](Declarations::elements),
/// [`scoped_packages`](Declarations::scoped_packages),
/// [`fallbacks`](Declarations::fallbacks),
/// [`overrides`](Declarations::overrides)) return everything; the other
/// accessors answer the usual questions of a program that describes the
/// language: the commands of one feature
/// ([`elements_of`](Declarations::elements_of)), the declarations of one
/// command ([`named`](Declarations::named)), a scoped package and the places
/// that make it available ([`scoped_package`](Declarations::scoped_package),
/// [`scoped_package_uses`](Declarations::scoped_package_uses)), and the
/// arguments that hold a given kind of thing
/// ([`arguments`](Declarations::arguments),
/// [`role_uses`](Declarations::role_uses)).
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use flm_core::core::registry::{BuildError, FeatureBuilder, FeatureInfo, FlmFeature, build_registry};
/// use flm_core::lang::mint::{ArgDef, CallableKind, ElementDef, SyntaxDef};
/// use flm_core::model::element::ElementSpec;
///
/// struct Shout;
/// impl FlmFeature for Shout {
///     fn info(&self) -> FeatureInfo { FeatureInfo::new("shout", "Shouting") }
///     fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
///         let element = Arc::new(ElementSpec::new("shout.shout"));
///         let syntax = SyntaxDef::Macro { args: vec![ArgDef::mandatory("text")] };
///         b.element(ElementDef::new("shout", ["shout"], syntax, element).docs("Shouted text."))
///     }
/// }
///
/// let registry = build_registry([Arc::new(Shout) as Arc<dyn FlmFeature>])?;
/// let found = registry.declarations().named(CallableKind::Macro, "shout");
/// assert_eq!(found.len(), 1);
/// # #[cfg(feature = "docs")]
/// assert_eq!(found[0].docs, "Shouted text.");
/// assert_eq!(found[0].syntax.args()[0].name.as_deref(), Some("text"));
/// # Ok::<(), flm_core::core::registry::RegistryBuildError>(())
/// ```
#[derive(Debug, Clone)]
pub struct Declarations {
    elements: Vec<ElementDef>,
    scoped_packages: Vec<ScopedPackageDef>,
    fallbacks: Vec<FallbackDef>,
    overrides: Vec<OverrideDef>,
}

/// One argument declaration among a registry's [`Declarations`], with where
/// it is declared: the element declaration it belongs to, the scoped package
/// that holds that declaration, and the tack-on field whose own argument list
/// holds it.
///
/// [`Declarations::arguments`] and [`Declarations::role_uses`] return it.
#[derive(Debug, Clone, Copy)]
pub struct ArgumentRef<'a> {
    /// The element declaration the argument belongs to. Its
    /// [`owner`](ElementDef::owner) is the feature that declared it.
    pub declaration: &'a ElementDef,
    /// The scoped package that holds the element declaration, or `None` for
    /// a declaration a feature made directly
    /// ([`FeatureBuilder::element`](super::FeatureBuilder::element)).
    pub within: Option<&'a ScopedPackageDef>,
    /// The [tack-on field](TackOnField) whose own argument list holds the
    /// argument (the innermost one, for a field written in another field's
    /// argument), or `None` for an argument of the element declaration
    /// itself.
    pub field: Option<&'a TackOnField>,
    /// The argument's position, counting from zero, in the argument list
    /// that holds it: the element declaration's own, or the field's.
    pub index: usize,
    /// The argument declaration.
    pub arg: &'a ArgDef,
}

/// One place where an element declaration makes a scoped package available:
/// an environment's body ([`BodyDef::scoped`](crate::lang::mint::BodyDef::scoped))
/// or an argument ([`ArgDef::scoped`]).
///
/// [`Declarations::scoped_package_uses`] returns it.
#[derive(Debug, Clone, Copy)]
pub enum ScopedPackageUse<'a> {
    /// The body of an environment makes the package available inside it.
    Body {
        /// The environment's declaration. Its [`owner`](ElementDef::owner)
        /// is the feature that declared it.
        declaration: &'a ElementDef,
        /// The scoped package that holds the declaration, or `None` for a
        /// declaration a feature made directly.
        within: Option<&'a ScopedPackageDef>,
    },
    /// An argument makes the package available inside it.
    Argument(ArgumentRef<'a>),
}

impl<'a> ScopedPackageUse<'a> {
    /// The element declaration whose body or argument makes the package
    /// available.
    pub fn declaration(&self) -> &'a ElementDef {
        match self {
            ScopedPackageUse::Body { declaration, .. } => declaration,
            ScopedPackageUse::Argument(argument) => argument.declaration,
        }
    }

    /// The scoped package that holds that declaration, or `None` for a
    /// declaration a feature made directly.
    pub fn within(&self) -> Option<&'a ScopedPackageDef> {
        match self {
            ScopedPackageUse::Body { within, .. } => *within,
            ScopedPackageUse::Argument(argument) => argument.within,
        }
    }
}

/// Adds to `out` every argument of the element declaration `def`, held in
/// the scoped package `within` if any, in the order written: each argument
/// of the declaration, followed by the arguments of each of its tack-on
/// fields, and so on for a field's own tack-on arguments.
pub(crate) fn arguments_of<'a>(
    def: &'a ElementDef,
    within: Option<&'a ScopedPackageDef>,
    out: &mut Vec<ArgumentRef<'a>>,
) {
    arguments_in(def, within, None, def.syntax.args(), out);
}

/// Adds to `out` the arguments `args` of the declaration `def`, held in the
/// list of the tack-on field `field` (or the declaration's own list), each
/// followed by the arguments of its tack-on fields.
fn arguments_in<'a>(
    def: &'a ElementDef,
    within: Option<&'a ScopedPackageDef>,
    field: Option<&'a TackOnField>,
    args: &'a [ArgDef],
    out: &mut Vec<ArgumentRef<'a>>,
) {
    for (index, arg) in args.iter().enumerate() {
        out.push(ArgumentRef { declaration: def, within, field, index, arg });
        for inner in arg.kind.tack_on_fields().unwrap_or_default() {
            arguments_in(def, within, Some(inner), &inner.args, out);
        }
    }
}

impl Declarations {
    /// The declarations as the build leaves them, moved into the frozen
    /// registry, with the weak element declarations before the ordinary ones,
    /// each part grouped by feature in the order of [`feature_stack_order`].
    pub(crate) fn new(
        mut elements: Vec<ElementDef>,
        scoped_packages: Vec<ScopedPackageDef>,
        fallbacks: Vec<FallbackDef>,
        overrides: Vec<OverrideDef>,
    ) -> Declarations {
        // The build lists a finalizer's declarations after every declaration
        // of the first registration round, while the language stacks each
        // feature's definitions together, at the place of its first
        // declaration, every feature's weak declarations under every
        // feature's ordinary ones. The sort is stable, so each feature's
        // declarations of one weakness keep the order it made them in.
        let order = feature_stack_order(&elements);
        elements.sort_by_key(|def| {
            (!is_weak(def), order.iter().position(|owner| *owner == def.owner))
        });
        Declarations { elements, scoped_packages, fallbacks, overrides }
    }

    /// Every element declaration a feature made directly
    /// ([`FeatureBuilder::element`](super::FeatureBuilder::element)),
    /// grouped by feature. The declarations held by scoped packages are not
    /// among them: see [`scoped_packages`](Declarations::scoped_packages).
    ///
    /// The declarations come in the order in which the language stacks the
    /// features' definitions, so that, in a parsing mode, the last
    /// declaration of a command is the one in force. The weak declarations
    /// ([`ElementDef::weak`]) of every feature come first, then the ordinary
    /// ones. In each of these two parts, the features come in the order of
    /// each feature's first element declaration, a later feature's definition
    /// of a command replacing an earlier one's: the final order of the
    /// features ([`FlmRegistry::features`](super::FlmRegistry::features))
    /// among those that declare elements when they register. A feature whose
    /// only element declarations come from a [`Finalizer`](super::Finalizer)
    /// comes after them. Each feature's declarations of one part are in the
    /// order it made them, its finalizers' last.
    pub fn elements(&self) -> &[ElementDef] {
        &self.elements
    }

    /// Every scoped package, with the element declarations it holds, in
    /// registration order.
    pub fn scoped_packages(&self) -> &[ScopedPackageDef] {
        &self.scoped_packages
    }

    /// Every fallback, in registration order.
    pub fn fallbacks(&self) -> &[FallbackDef] {
        &self.fallbacks
    }

    /// Every override, in registration order.
    pub fn overrides(&self) -> &[OverrideDef] {
        &self.overrides
    }

    /// The element declarations that the feature called `feature` made
    /// directly, in the order of [`elements`](Declarations::elements): its
    /// weak declarations first, then its ordinary ones, each in the order it
    /// made them. The declarations of its scoped
    /// packages are not among them: see
    /// [`scoped_packages_of`](Declarations::scoped_packages_of).
    pub fn elements_of<'a>(&'a self, feature: &'a str) -> impl Iterator<Item = &'a ElementDef> + 'a {
        self.elements.iter().filter(move |def| def.owner == feature)
    }

    /// The scoped packages that the feature called `feature` declared, in the
    /// order it declared them.
    pub fn scoped_packages_of<'a>(
        &'a self,
        feature: &'a str,
    ) -> impl Iterator<Item = &'a ScopedPackageDef> + 'a {
        self.scoped_packages.iter().filter(move |package| package.owner == feature)
    }

    /// The element declarations, across features, that define the command
    /// `name` of the callable kind `kind`, in the order of
    /// [`elements`](Declarations::elements). `name` is
    /// written without the escape character (`section`, `itemize`); for a
    /// special character sequence it is the trigger (`~`). A file-inclusion
    /// macro is a macro.
    ///
    /// Several declarations define one command when their parsing modes
    /// differ ([`ElementDef::modes`]), or when one feature's definition
    /// replaces another's. In a given parsing mode, the definition in force
    /// is the last one in this list that is available in that mode (a
    /// feature declares a command at most once per parsing mode, so this is
    /// the ordinary definition of the feature whose definitions the language
    /// stacks last, or the weak one of that feature when no feature defines
    /// the command ordinarily in that mode). Only the declarations
    /// that features made directly are searched; a scoped package's command
    /// is found through [`scoped_package`](Declarations::scoped_package).
    pub fn named(&self, kind: CallableKind, name: &str) -> Vec<&ElementDef> {
        self.elements.iter().filter(|def| defines(def, kind, name)).collect()
    }

    /// The scoped package called `name`, or `None` when no feature declares
    /// one. A registry holds at most one scoped package of a name.
    pub fn scoped_package(&self, name: &str) -> Option<&ScopedPackageDef> {
        self.scoped_packages.iter().find(|package| package.name == *name)
    }

    /// Every place where an element declaration makes the scoped package
    /// called `name` available: each environment body and each argument
    /// whose declaration lists the package ([`BodyDef::scoped`](crate::lang::mint::BodyDef::scoped),
    /// [`ArgDef::scoped`]). The declarations that features made directly come
    /// first, in the order of [`elements`](Declarations::elements), then
    /// those held by scoped packages, in registration order.
    /// A use by a feature that declared the package as a peer is listed
    /// whether or not some feature declares the package.
    pub fn scoped_package_uses(&self, name: &str) -> Vec<ScopedPackageUse<'_>> {
        let mut uses = Vec::new();
        for (def, within) in self.all_elements() {
            if let SyntaxDef::Environment { body, .. } = &def.syntax {
                if body.scoped.iter().any(|package| package == name) {
                    uses.push(ScopedPackageUse::Body { declaration: def, within });
                }
            }
            let mut arguments = Vec::new();
            arguments_of(def, within, &mut arguments);
            uses.extend(
                arguments
                    .into_iter()
                    .filter(|argument| argument.arg.scoped.iter().any(|package| package == name))
                    .map(ScopedPackageUse::Argument),
            );
        }
        uses
    }

    /// Every argument of every element declaration, including the arguments
    /// of the tack-on fields: the declarations that features made directly
    /// first, in the order of [`elements`](Declarations::elements), then
    /// those held by scoped packages, in registration order; each
    /// declaration's arguments come in the order written.
    pub fn arguments(&self) -> Vec<ArgumentRef<'_>> {
        let mut out = Vec::new();
        for (def, within) in self.all_elements() {
            arguments_of(def, within, &mut out);
        }
        out
    }

    /// Every argument whose [role](ArgDef::role) is `role`, in the order of
    /// [`arguments`](Declarations::arguments).
    pub fn role_uses(&self, role: &str) -> Vec<ArgumentRef<'_>> {
        self.arguments()
            .into_iter()
            .filter(|argument| argument.arg.role.as_ref().is_some_and(|name| name == role))
            .collect()
    }

    /// Every element declaration with the scoped package that holds it: the
    /// declarations that features made directly first, then those of each
    /// scoped package.
    fn all_elements(&self) -> impl Iterator<Item = (&ElementDef, Option<&ScopedPackageDef>)> {
        self.elements.iter().map(|def| (def, None)).chain(
            self.scoped_packages
                .iter()
                .flat_map(|package| package.elements.iter().map(move |def| (def, Some(package)))),
        )
    }
}

/// Whether the declaration `def` defines the command `name` of the callable
/// kind `kind`: one of its names, or its trigger for a special character
/// sequence.
fn defines(def: &ElementDef, kind: CallableKind, name: &str) -> bool {
    if def.syntax.kind() != Some(kind) {
        return false;
    }
    match &def.syntax {
        SyntaxDef::Specials { trigger, .. } => trigger == name,
        _ => def.names.iter().any(|written| written == name),
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use techy::latexlike::Mode;

    use crate::core::registry::{
        build_registry, BuildError, FeatureBuilder, FeatureInfo, Finalizer, FlmFeature,
        FlmRegistry, RegistryBuildError, UnresolvedPeer,
    };
    use crate::lang::mint::{
        ArgDef, ArgStdKind, BodyDef, CallableKind, ElementDef, FallbackDef, FallbackKind,
        ScopedPackageDef, Symbol, SyntaxDef, TackOnField,
    };
    use crate::model::element::ElementSpec;
    use crate::model::names::Name;

    type Register = dyn Fn(&mut FeatureBuilder<'_>) -> Result<(), BuildError> + Send + Sync;

    /// A feature called `name` whose registration is the closure it holds.
    struct Test {
        name: &'static str,
        register: Box<Register>,
    }

    impl FlmFeature for Test {
        fn info(&self) -> FeatureInfo {
            FeatureInfo::new(self.name, self.name)
        }

        fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
            (self.register)(b)
        }
    }

    fn feature(
        name: &'static str,
        register: impl Fn(&mut FeatureBuilder<'_>) -> Result<(), BuildError> + Send + Sync + 'static,
    ) -> Arc<dyn FlmFeature> {
        Arc::new(Test { name, register: Box::new(register) })
    }

    fn built(features: Vec<Arc<dyn FlmFeature>>) -> Arc<FlmRegistry> {
        build_registry(features).unwrap_or_else(|error| panic!("{}", error.render()))
    }

    fn refused(features: Vec<Arc<dyn FlmFeature>>) -> RegistryBuildError {
        match build_registry(features) {
            Ok(_) => panic!("the registry build was expected to fail"),
            Err(error) => error,
        }
    }

    fn element(name: &str) -> Arc<ElementSpec> {
        Arc::new(ElementSpec::new(name))
    }

    /// A macro `\name` of `owner` taking `args`, whose element is `owner.name`.
    fn macro_with(owner: &str, name: &str, args: Vec<ArgDef>) -> ElementDef {
        ElementDef::new(owner, [name], SyntaxDef::Macro { args }, element(&format!("{owner}.{name}")))
    }

    fn arg_names(def: &ElementDef) -> Vec<Option<&str>> {
        def.syntax.args().iter().map(|arg| arg.name.as_deref()).collect()
    }

    fn notes(registry: &FlmRegistry) -> Vec<String> {
        registry.build_diagnostics().iter().map(|diagnostic| diagnostic.identifier().to_string()).collect()
    }

    // --- the retained declarations ---------------------------------------------------------

    #[test]
    fn a_registry_retains_every_declaration_in_registration_order() {
        let registry = built(vec![
            feature("t", |b| {
                b.element(macro_with("t", "a", vec![ArgDef::mandatory("x")]).docs("The a."))?;
                b.element(macro_with("t", "b", Vec::new()))?;
                b.element(ElementDef::new(
                    "t",
                    ["list"],
                    SyntaxDef::Environment { args: Vec::new(), body: BodyDef::new().scoped("t.items") },
                    element("t.list"),
                ))?;
                b.scoped_package(ScopedPackageDef {
                    owner: "t".into(),
                    name: Name::from("t.items"),
                    elements: vec![macro_with("t", "item", Vec::new())],
                    docs: String::from("The items."),
                })?;
                b.fallback(FallbackDef {
                    owner: "t".into(),
                    modes: vec![Mode::Math],
                    kind: FallbackKind::Macro,
                    element: element("t.any"),
                })
            }),
            feature("u", |b| {
                b.element(macro_with("u", "a", Vec::new()).modes([Mode::Math]))?;
                b.override_callable(CallableKind::Macro, "b")
            }),
        ]);
        let declarations = registry.declarations();
        let written: Vec<(&str, &str)> =
            declarations.elements().iter().map(|def| (def.owner.as_str(), def.names[0].as_str())).collect();
        assert_eq!(written, [("t", "a"), ("t", "b"), ("t", "list"), ("u", "a")]);
        assert_eq!(declarations.scoped_packages().len(), 1);
        assert_eq!(declarations.fallbacks().len(), 1);
        assert_eq!(declarations.overrides().len(), 1);
        assert_eq!(declarations.elements_of("t").count(), 3);
        assert_eq!(declarations.elements_of("u").count(), 1);
        assert_eq!(declarations.scoped_packages_of("t").count(), 1);

        let a = declarations.named(CallableKind::Macro, "a");
        let owners: Vec<&str> = a.iter().map(|def| def.owner.as_str()).collect();
        assert_eq!(owners, ["t", "u"]);
        #[cfg(feature = "docs")]
        assert_eq!(a[0].docs, "The a.");
        assert_eq!(arg_names(a[0]), [Some("x")]);
        assert!(declarations.named(CallableKind::Environment, "a").is_empty());
        assert!(declarations.named(CallableKind::Macro, "item").is_empty());

        let items = declarations.scoped_package("t.items").expect("declared");
        assert_eq!(items.elements[0].names[0], "item");
        let uses = declarations.scoped_package_uses("t.items");
        assert_eq!(uses.len(), 1);
        assert!(matches!(uses[0], super::ScopedPackageUse::Body { within: None, .. }));
        assert_eq!(uses[0].declaration().names[0], "list");
    }

    /// A finalizer that declares the macro `\x` for its feature.
    struct LateX;

    impl Finalizer for LateX {
        fn finalize(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
            b.element(macro_with("a", "x", Vec::new()))
        }
    }

    #[test]
    fn a_finalizer_s_declarations_join_its_feature_s_and_the_last_one_named_is_in_force() {
        // `a` declares `\x` from its finalizer, after `b` declared `\x` (with an
        // override) in the first round; the language stacks `b`'s definitions
        // above all of `a`'s, so `b`'s `\x` is in force.
        let registry = built(vec![
            feature("a", |b| {
                b.element(macro_with("a", "y", Vec::new()))?;
                b.finalizer(Arc::new(LateX))
            }),
            feature("b", |b| {
                b.element(macro_with("b", "x", Vec::new()))?;
                b.override_callable(CallableKind::Macro, "x")
            }),
        ]);
        let declarations = registry.declarations();
        let written: Vec<(&str, &str)> =
            declarations.elements().iter().map(|def| (def.owner.as_str(), def.names[0].as_str())).collect();
        assert_eq!(written, [("a", "y"), ("a", "x"), ("b", "x")]);
        let x = declarations.named(CallableKind::Macro, "x");
        let owners: Vec<&str> = x.iter().map(|def| def.owner.as_str()).collect();
        assert_eq!(owners, ["a", "b"]);
        let symbol = Symbol { kind: CallableKind::Macro, name: String::from("x") };
        let in_force = registry.lang().symbols().get(&symbol).expect("`\\x` is defined");
        assert_eq!(x.last().map(|def| &def.owner), Some(in_force));
    }

    #[test]
    fn in_a_mode_the_last_declaration_named_is_in_force_whatever_the_weakness() {
        // `b`, registered after `a`, declares `\x` weakly in both modes; `a`
        // declares it ordinarily in math mode. In text mode `b`'s is in force,
        // in math mode `a`'s.
        let registry = built(vec![
            feature("a", |b| b.element(macro_with("a", "x", Vec::new()).modes([Mode::Math]))),
            feature("b", |b| {
                b.element(macro_with("b", "x", Vec::new()).modes([Mode::Text, Mode::Math]).weak())
            }),
        ]);
        let declarations = registry.declarations();
        let x = declarations.named(CallableKind::Macro, "x");
        let in_force = |mode: Mode| {
            x.iter().rev().find(|def| def.modes.contains(&mode)).map(|def| def.owner.as_str())
        };
        assert_eq!(in_force(Mode::Text), Some("b"));
        assert_eq!(in_force(Mode::Math), Some("a"));
    }

    #[test]
    fn the_mark_enters_the_digest() {
        let with = |weak: bool| {
            built(vec![feature("a", move |b| {
                let def = macro_with("a", "x", Vec::new());
                b.element(if weak { def.weak() } else { def })
            })])
            .digest()
        };
        assert_eq!(with(true), with(true));
        assert_ne!(with(true), with(false));
    }

    #[test]
    fn a_resolved_peer_element_value_is_kept_and_an_unresolved_one_is_not() {
        let registry = built(vec![
            feature("t", |b| b.element(macro_with("t", "own", Vec::new()))),
            feature("u", |b| {
                b.peer_element_value(ElementDef::new("u", ["also"], SyntaxDef::Macro { args: Vec::new() }, element("t.own")))?;
                b.peer_element_value(ElementDef::new("u", ["none"], SyntaxDef::Macro { args: Vec::new() }, element("v.none")))
            }),
        ]);
        let names: Vec<&str> =
            registry.declarations().elements().iter().map(|def| def.names[0].as_str()).collect();
        assert_eq!(names, ["own", "also"]);
    }

    // --- argument roles ---------------------------------------------------------------------

    #[test]
    fn a_declared_role_is_listed_and_found_on_its_arguments() {
        let registry = built(vec![feature("t", |b| {
            b.argument_role("t.key", "A key.")?;
            b.element(macro_with("t", "k", vec![ArgDef::mandatory("key").role("t.key")]))
        })]);
        let role = registry.argument_role_named("t.key").expect("declared");
        assert_eq!(role.owner, "t");
        #[cfg(feature = "docs")]
        assert_eq!(role.docs, "A key.");
        assert_eq!(registry.argument_roles().len(), 1);
        let uses = registry.declarations().role_uses("t.key");
        assert_eq!(uses.len(), 1);
        assert_eq!(uses[0].arg.name.as_deref(), Some("key"));
        assert_eq!(uses[0].declaration.names[0], "k");
        assert!(registry.argument_role_named("t.other").is_none());
    }

    #[test]
    fn a_role_no_feature_declares_is_an_error_naming_the_feature_command_argument_and_role() {
        let error = refused(vec![feature("t", |b| {
            b.argument_role("t.key", "A key.")?;
            let field = TackOnField::new("tag", vec![ArgDef::mandatory("name").role("t.tag")]);
            b.element(macro_with(
                "t",
                "k",
                vec![ArgDef::new(ArgStdKind::Mandatory).role("t.kye"), ArgDef::new(ArgStdKind::TackOn { fields: vec![field] })],
            ))
        })]);
        let roles: Vec<(&str, Option<&str>, Option<&str>)> = error
            .errors
            .iter()
            .filter_map(|error| match error {
                BuildError::UndeclaredArgumentRole { role, place, .. } => {
                    Some((role.as_str(), place.argument.as_deref(), place.field.as_deref()))
                }
                _ => None,
            })
            .collect();
        assert_eq!(roles, [("t.kye", None, None), ("t.tag", Some("name"), Some("tag"))]);
        match &error.errors[0] {
            BuildError::UndeclaredArgumentRole { feature, place, role, roster } => {
                assert_eq!((feature.as_str(), place.command.as_str(), role.as_str()), ("t", "\\k", "t.kye"));
                assert_eq!(roster, &[Name::from("t.key")]);
                assert_eq!((place.argument.as_deref(), place.index, place.field.as_deref()), (None, 0, None));
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn another_feature_s_role_needs_no_peer_declaration_when_that_feature_is_present() {
        let registry = built(vec![
            feature("t", |b| b.argument_role("t.key", "A key.")),
            feature("u", |b| b.element(macro_with("u", "k", vec![ArgDef::mandatory("key").role("t.key")]))),
        ]);
        assert_eq!(registry.declarations().role_uses("t.key").len(), 1);
        assert!(registry.peer_argument_roles().is_empty());
    }

    #[test]
    fn a_peer_declared_role_resolves_and_an_unresolved_one_is_a_note() {
        let user = || {
            feature("u", |b| {
                b.peer_argument_role("t.key")?;
                b.peer_argument_role("t.key")?;
                b.element(macro_with("u", "k", vec![ArgDef::mandatory("key").role("t.key")]))
            })
        };
        let resolved = built(vec![feature("t", |b| b.argument_role("t.key", "A key.")), user()]);
        assert_eq!(resolved.peer_argument_roles().len(), 1);
        assert!(resolved.peer_argument_roles()[0].resolved);
        assert!(notes(&resolved).is_empty(), "{:?}", notes(&resolved));

        let alone = built(vec![user()]);
        assert_eq!(alone.peer_argument_roles().len(), 1);
        assert!(!alone.peer_argument_roles()[0].resolved);
        assert_eq!(notes(&alone), ["flm.registry.unresolved-peer"]);
        let note = alone.build_diagnostics().iter().next().expect("one note");
        let data = note.data().downcast_ref::<UnresolvedPeer>().expect("an unresolved peer");
        assert_eq!(data.kind, "argument role");
        assert_eq!((data.feature.as_str(), data.name.as_str()), ("u", "t.key"));
        assert_eq!(alone.declarations().role_uses("t.key").len(), 1);
    }

    #[test]
    fn two_declarations_of_one_role_are_an_error_naming_both_features() {
        let error = refused(vec![
            feature("t", |b| b.argument_role("x.key", "A key.")),
            feature("u", |b| b.argument_role("x.key", "Another key.")),
        ]);
        assert!(
            matches!(
                &error.errors[..],
                [BuildError::Duplicate { kind: "argument role", name, first, second }]
                    if name.as_str() == "x.key" && first.as_str() == "t" && second.as_str() == "u"
            ),
            "{error:?}"
        );
    }

    #[test]
    fn a_role_is_not_an_input_of_the_digest() {
        let with = |role: bool| {
            built(vec![feature("t", move |b| {
                b.argument_role("t.key", "A key.")?;
                let mut arg = ArgDef::mandatory("key");
                if role {
                    arg = arg.role("t.key");
                }
                b.element(macro_with("t", "k", vec![arg]))
            })])
            .digest()
        };
        assert_eq!(with(false), with(true));
        let without_declaration =
            built(vec![feature("t", |b| b.element(macro_with("t", "k", vec![ArgDef::mandatory("key")])))]).digest();
        assert_eq!(with(false), without_declaration);
    }
}