meta-language 1.0.0

A self-describing links-network core for lossless language representation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! The grammar feature union forms (`docs/grammar/feature-union.md`).
//!
//! One table describes every expression kind, every operation of the scanner
//! and action language and every grammar declaration the feature union adds,
//! and the native listing, links and JSON codecs read and write them from
//! that table, so the serializations stay in step with each other and with
//! `js/src/grammar-feature-forms.js`.

use std::collections::BTreeSet;

use super::{CharClassItem, GrammarExpr};

/// The type of one field of a feature form or an operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FieldType {
    /// One word out of a fixed list.
    Choice(&'static [&'static str]),
    /// A signed integer of at most 15 digits.
    Integer,
    /// A name: a rule, mode, token, stack, variable, field or attribute.
    Name,
    /// A text value.
    Text,
    /// One grammar expression.
    Expression,
    /// The remaining grammar expressions.
    Expressions,
    /// One condition operation.
    Condition,
    /// One value operation.
    Value,
    /// The remaining condition operations.
    Conditions,
    /// A statement list written as `WORD(...)`; the flag makes it optional.
    Block(&'static str, bool),
}

/// The fields of one form, as `(key, type)` pairs in listing order.
pub type FormFields = &'static [(&'static str, FieldType)];

/// The expression kinds the feature union adds, with their fields in listing order.
pub const FEATURE_EXPRESSION_FORMS: &[(&str, FormFields)] = &[
    (
        "precedence",
        &[
            ("level", FieldType::Integer),
            (
                "associativity",
                FieldType::Choice(&["left", "right", "none"]),
            ),
            ("item", FieldType::Expression),
        ],
    ),
    (
        "namedPrecedence",
        &[
            ("name", FieldType::Name),
            (
                "associativity",
                FieldType::Choice(&["left", "right", "none"]),
            ),
            ("item", FieldType::Expression),
        ],
    ),
    (
        "dynamicPrecedence",
        &[
            ("level", FieldType::Integer),
            ("item", FieldType::Expression),
        ],
    ),
    (
        "lexicalPrecedence",
        &[
            ("level", FieldType::Integer),
            ("item", FieldType::Expression),
        ],
    ),
    ("longest", &[("items", FieldType::Expressions)]),
    ("token", &[("item", FieldType::Expression)]),
    ("immediateToken", &[("item", FieldType::Expression)]),
    (
        "alias",
        &[("name", FieldType::Name), ("item", FieldType::Expression)],
    ),
    ("parameter", &[("name", FieldType::Name)]),
    (
        "predicate",
        &[
            ("item", FieldType::Expression),
            ("condition", FieldType::Condition),
        ],
    ),
    (
        "recover",
        &[
            ("item", FieldType::Expression),
            ("synchronize", FieldType::Expression),
        ],
    ),
    ("missing", &[("item", FieldType::Expression)]),
    (
        "embed",
        &[
            ("language", FieldType::Name),
            ("item", FieldType::Expression),
        ],
    ),
    (
        "expand",
        &[
            ("name", FieldType::Name),
            ("arguments", FieldType::Expressions),
        ],
    ),
];

/// Whether an operation is a statement, a condition or a value.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OperationCategory {
    /// An operation run for its effect.
    Statement,
    /// An operation that holds or does not.
    Condition,
    /// An operation that computes an integer or a text.
    Value,
}

impl OperationCategory {
    /// The category's word in error messages.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Statement => "statement",
            Self::Condition => "condition",
            Self::Value => "value",
        }
    }
}

const TWO_VALUES: FormFields = &[("left", FieldType::Value), ("right", FieldType::Value)];
const STATEMENT: OperationCategory = OperationCategory::Statement;
const CONDITION: OperationCategory = OperationCategory::Condition;
const VALUE: OperationCategory = OperationCategory::Value;

/// The operation language of external scanners, semantic actions and
/// predicates, with each operation's category and fields in listing order.
pub const OPERATION_FORMS: &[(&str, OperationCategory, FormFields)] = &[
    ("advance", STATEMENT, &[]),
    ("consume", STATEMENT, &[("item", FieldType::Expression)]),
    ("skip", STATEMENT, &[("item", FieldType::Expression)]),
    ("mark", STATEMENT, &[]),
    ("emit", STATEMENT, &[("token", FieldType::Name)]),
    ("fail", STATEMENT, &[]),
    (
        "if",
        STATEMENT,
        &[
            ("condition", FieldType::Condition),
            ("consequent", FieldType::Block("then", false)),
            ("alternative", FieldType::Block("else", true)),
        ],
    ),
    (
        "while",
        STATEMENT,
        &[
            ("condition", FieldType::Condition),
            ("body", FieldType::Block("do", false)),
        ],
    ),
    (
        "push",
        STATEMENT,
        &[("stack", FieldType::Name), ("value", FieldType::Value)],
    ),
    ("pop", STATEMENT, &[("stack", FieldType::Name)]),
    (
        "set",
        STATEMENT,
        &[("variable", FieldType::Name), ("value", FieldType::Value)],
    ),
    ("pushMode", STATEMENT, &[("mode", FieldType::Name)]),
    ("popMode", STATEMENT, &[]),
    ("setMode", STATEMENT, &[("mode", FieldType::Name)]),
    (
        "setAttribute",
        STATEMENT,
        &[("attribute", FieldType::Name), ("value", FieldType::Value)],
    ),
    ("buildNode", STATEMENT, &[("kind", FieldType::Name)]),
    ("valid", CONDITION, &[("token", FieldType::Name)]),
    ("expected", CONDITION, &[("item", FieldType::Expression)]),
    ("next", CONDITION, &[("item", FieldType::Expression)]),
    ("atEnd", CONDITION, &[]),
    ("equal", CONDITION, TWO_VALUES),
    ("less", CONDITION, TWO_VALUES),
    ("greater", CONDITION, TWO_VALUES),
    ("all", CONDITION, &[("conditions", FieldType::Conditions)]),
    ("some", CONDITION, &[("conditions", FieldType::Conditions)]),
    ("not", CONDITION, &[("condition", FieldType::Condition)]),
    ("integer", VALUE, &[("value", FieldType::Integer)]),
    ("text", VALUE, &[("value", FieldType::Text)]),
    ("variable", VALUE, &[("name", FieldType::Name)]),
    ("top", VALUE, &[("stack", FieldType::Name)]),
    ("depth", VALUE, &[("stack", FieldType::Name)]),
    ("column", VALUE, &[]),
    ("matched", VALUE, &[]),
    ("mode", VALUE, &[]),
    (
        "attribute",
        VALUE,
        &[("field", FieldType::Name), ("attribute", FieldType::Name)],
    ),
    (
        "sumOf",
        VALUE,
        &[("field", FieldType::Name), ("attribute", FieldType::Name)],
    ),
    ("fieldText", VALUE, &[("field", FieldType::Name)]),
    ("length", VALUE, &[("value", FieldType::Value)]),
    ("number", VALUE, &[("value", FieldType::Value)]),
    ("add", VALUE, TWO_VALUES),
    ("subtract", VALUE, TWO_VALUES),
    ("multiply", VALUE, TWO_VALUES),
];

/// The ways a grammar may match: generalized (every alternative), PEG (first
/// and greedy) or longest (generalized, ties going to the longer token).
pub const MATCHING_MODES: &[&str] = &["generalized", "peg", "longest"];

/// The steps that settle two parses of one text that end alike, as a
/// grammar's `(settling STEP ...)` declares them.
///
/// In order (the lower repair cost always decides first): `tokens`, the tokens a lexer prefers (higher
/// lexical precedence, longer, a literal over a pattern, earlier), with the
/// lexing that goes with it (keywords, separators, immediate tokens);
/// `precedence`, the shift or reduction an LR parser keeps by precedence and
/// associativity, forking where a declared conflict asks; `dynamic`, the
/// higher dynamic precedence. The last step settles a tie: `first` keeps the
/// parse reached first, `ambiguity` keeps it and reports the ambiguity.
pub const SETTLING_STEPS: &[&str] = &["tokens", "precedence", "dynamic", "first", "ambiguity"];

/// The settling of a grammar that declares none, by its matching.
#[must_use]
pub fn default_settling(matching: &str) -> &'static [&'static str] {
    match matching {
        "peg" => &["first"],
        "longest" => &["tokens", "precedence", "dynamic", "ambiguity"],
        _ => &["dynamic", "ambiguity"],
    }
}

/// Why `steps` is no settling, or `None`: every step is known and given
/// once, and exactly the last one settles a tie.
#[must_use]
pub fn settling_problem<S: AsRef<str>>(steps: &[S]) -> Option<String> {
    if steps.is_empty() {
        return Some("the settling names no step".to_owned());
    }
    for (index, step) in steps.iter().enumerate() {
        let step = step.as_ref();
        if !SETTLING_STEPS.contains(&step) {
            return Some(format!("unknown settling step {step}"));
        }
        if steps.iter().position(|other| other.as_ref() == step) != Some(index) {
            return Some(format!("the settling step {step} is given twice"));
        }
        let tie = step == "first" || step == "ambiguity";
        if tie != (index == steps.len() - 1) {
            return Some("the settling ends with exactly one of first and ambiguity".to_owned());
        }
    }
    None
}

/// The longest integer a form may spell, in decimal digits.
pub const MAX_INTEGER_DIGITS: usize = 15;

/// The fields of the feature expression form `head`, when it is one.
#[must_use]
pub fn feature_expression_fields(head: &str) -> Option<FormFields> {
    FEATURE_EXPRESSION_FORMS
        .iter()
        .find(|(name, _)| *name == head)
        .map(|(_, fields)| *fields)
}

/// The category and fields of the operation `head`, when it is one.
#[must_use]
pub fn operation_form(head: &str) -> Option<(OperationCategory, FormFields)> {
    OPERATION_FORMS
        .iter()
        .find(|(name, _, _)| *name == head)
        .map(|(_, category, fields)| (*category, *fields))
}

/// Checks the text of an integer field: an optional `-` and at most 15 digits.
///
/// # Errors
///
/// Returns the detail of the failure.
pub fn check_integer(text: &str) -> Result<i64, String> {
    let digits = text.strip_prefix('-').unwrap_or(text);
    if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
        return Err(format!("expected an integer, not {text}"));
    }
    if digits.len() > MAX_INTEGER_DIGITS {
        return Err(format!("integer {text} is too large"));
    }
    text.parse()
        .map_err(|_| format!("expected an integer, not {text}"))
}

/// The value of one field of a feature form or an operation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FieldValue {
    /// One word of a [`FieldType::Choice`].
    Word(String),
    /// An integer.
    Integer(i64),
    /// A name.
    Name(String),
    /// A text.
    Text(String),
    /// One expression.
    Expression(GrammarExpr),
    /// A list of expressions.
    Expressions(Vec<GrammarExpr>),
    /// One condition or value.
    Operation(FeatureForm),
    /// A list of conditions.
    Operations(Vec<FeatureForm>),
    /// A statement block; `None` for an absent optional block.
    Block(Option<Vec<FeatureForm>>),
}

/// One feature expression form or one operation: a head word and its field
/// values, in the order of [`FEATURE_EXPRESSION_FORMS`] or [`OPERATION_FORMS`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FeatureForm {
    /// The form's head word, such as `precedence` or `pushMode`.
    pub head: String,
    /// The field values in table order.
    pub fields: Vec<FieldValue>,
}

/// One operation of the scanner and action language.
pub type Operation = FeatureForm;

impl FeatureForm {
    /// Builds a form from its head and field values.
    #[must_use]
    pub fn new(head: impl Into<String>, fields: Vec<FieldValue>) -> Self {
        Self {
            head: head.into(),
            fields,
        }
    }

    /// The table fields of this form, as an expression form or an operation.
    #[must_use]
    pub fn spec(&self) -> FormFields {
        feature_expression_fields(&self.head)
            .or_else(|| operation_form(&self.head).map(|(_, fields)| fields))
            .unwrap_or(&[])
    }

    /// The value of the field `key`.
    #[must_use]
    pub fn field(&self, key: &str) -> Option<&FieldValue> {
        let index = self.spec().iter().position(|(name, _)| *name == key)?;
        self.fields.get(index)
    }

    /// The expression of the field `key`.
    #[must_use]
    pub fn expression(&self, key: &str) -> Option<&GrammarExpr> {
        match self.field(key)? {
            FieldValue::Expression(expr) => Some(expr),
            _ => None,
        }
    }

    /// The expression list of the field `key`.
    #[must_use]
    pub fn expressions(&self, key: &str) -> &[GrammarExpr] {
        match self.field(key) {
            Some(FieldValue::Expressions(items)) => items,
            _ => &[],
        }
    }

    /// The word, name or text of the field `key`.
    #[must_use]
    pub fn text(&self, key: &str) -> Option<&str> {
        match self.field(key)? {
            FieldValue::Word(value) | FieldValue::Name(value) | FieldValue::Text(value) => {
                Some(value)
            }
            _ => None,
        }
    }

    /// The integer of the field `key`.
    #[must_use]
    pub fn integer(&self, key: &str) -> Option<i64> {
        match self.field(key)? {
            FieldValue::Integer(value) => Some(*value),
            _ => None,
        }
    }

    /// The single operation of the field `key`.
    #[must_use]
    pub fn operation(&self, key: &str) -> Option<&Self> {
        match self.field(key)? {
            FieldValue::Operation(operation) => Some(operation),
            _ => None,
        }
    }

    /// The operation list or block of the field `key`; `None` for an absent block.
    #[must_use]
    pub fn operations(&self, key: &str) -> Option<&[Self]> {
        match self.field(key)? {
            FieldValue::Operations(items) | FieldValue::Block(Some(items)) => Some(items),
            _ => None,
        }
    }

    /// Every grammar expression this form holds directly, operations included.
    pub fn for_each_expression<'a>(&'a self, visit: &mut impl FnMut(&'a GrammarExpr)) {
        for value in &self.fields {
            match value {
                FieldValue::Expression(expr) => visit(expr),
                FieldValue::Expressions(items) => items.iter().for_each(&mut *visit),
                FieldValue::Operation(operation) => operation.for_each_expression(visit),
                FieldValue::Operations(items) | FieldValue::Block(Some(items)) => {
                    for operation in items {
                        operation.for_each_expression(visit);
                    }
                }
                _ => {}
            }
        }
    }

    /// Rewrites every grammar expression this form holds, operations included.
    pub fn map_expressions(&mut self, map: &mut impl FnMut(&mut GrammarExpr)) {
        for value in &mut self.fields {
            match value {
                FieldValue::Expression(expr) => map(expr),
                FieldValue::Expressions(items) => items.iter_mut().for_each(&mut *map),
                FieldValue::Operation(operation) => operation.map_expressions(map),
                FieldValue::Operations(items) | FieldValue::Block(Some(items)) => {
                    for operation in items {
                        operation.map_expressions(map);
                    }
                }
                _ => {}
            }
        }
    }
}

/// One item of a byte class.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ByteClassItem {
    /// One byte.
    Byte(u8),
    /// An inclusive byte range.
    Range(u8, u8),
}

/// One item of a character class that names Unicode properties.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum UnicodeClassItem {
    /// A single character.
    Char(char),
    /// An inclusive character range.
    Range(char, char),
    /// A Unicode general category such as `Lu` or `L`.
    Category(String),
    /// A Unicode script such as `Greek` or `Han`.
    Script(String),
}

/// A character class: a plain [`GrammarExpr::CharClass`] when every item is a
/// character or a range, otherwise a [`FeatureExpr::UnicodeClass`].
#[must_use]
pub fn class_expression(negated: bool, items: Vec<UnicodeClassItem>) -> GrammarExpr {
    let plain = items
        .iter()
        .map(|item| match item {
            UnicodeClassItem::Char(value) => Some(CharClassItem::Char(*value)),
            UnicodeClassItem::Range(start, end) => Some(CharClassItem::Range(*start, *end)),
            UnicodeClassItem::Category(_) | UnicodeClassItem::Script(_) => None,
        })
        .collect::<Option<Vec<_>>>();
    plain.map_or_else(
        || GrammarExpr::feature(FeatureExpr::UnicodeClass { negated, items }),
        |items| GrammarExpr::CharClass { negated, items },
    )
}

/// An expression of the grammar feature union.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FeatureExpr {
    /// A form of [`FEATURE_EXPRESSION_FORMS`].
    Form(FeatureForm),
    /// A class of bytes, `byteClass` or `notByteClass`.
    ByteClass {
        /// Whether the class is negated.
        negated: bool,
        /// Bytes and byte ranges of the class.
        items: Vec<ByteClassItem>,
    },
    /// A character class with at least one Unicode `category` or `script` item.
    UnicodeClass {
        /// Whether the class is negated.
        negated: bool,
        /// Characters, ranges, categories and scripts of the class.
        items: Vec<UnicodeClassItem>,
    },
    /// A call of a parameterized rule, `ref(NAME, ARGUMENT, ...)`.
    Call {
        /// The called rule.
        name: String,
        /// The arguments, at least one.
        arguments: Vec<GrammarExpr>,
    },
}

impl FeatureExpr {
    /// The head word of this expression in the native listing.
    #[must_use]
    pub fn head(&self) -> &str {
        match self {
            Self::Form(form) => &form.head,
            Self::ByteClass { .. } => "byteClass",
            Self::UnicodeClass { .. } => "charClass",
            Self::Call { .. } => "ref",
        }
    }

    /// Every grammar expression this one holds directly, operations included.
    pub fn for_each_expression<'a>(&'a self, visit: &mut impl FnMut(&'a GrammarExpr)) {
        match self {
            Self::Form(form) => form.for_each_expression(visit),
            Self::Call { arguments, .. } => arguments.iter().for_each(visit),
            Self::ByteClass { .. } | Self::UnicodeClass { .. } => {}
        }
    }

    /// Rewrites every grammar expression this one holds directly.
    pub fn map_expressions(&mut self, map: &mut impl FnMut(&mut GrammarExpr)) {
        match self {
            Self::Form(form) => form.map_expressions(map),
            Self::Call { arguments, .. } => arguments.iter_mut().for_each(map),
            Self::ByteClass { .. } | Self::UnicodeClass { .. } => {}
        }
    }

    /// Adds the rule names this expression references, as the JavaScript
    /// `collectReferences` does: calls and the expressions of `item`,
    /// `items`, `arguments` and `synchronize` fields.
    pub(crate) fn collect_references(&self, names: &mut BTreeSet<String>) {
        match self {
            Self::Call { name, arguments } => {
                names.insert(name.clone());
                for argument in arguments {
                    argument.collect_nonterminals(names);
                }
            }
            Self::Form(form) => {
                for value in &form.fields {
                    match value {
                        FieldValue::Expression(expr) => expr.collect_nonterminals(names),
                        FieldValue::Expressions(items) => {
                            for item in items {
                                item.collect_nonterminals(names);
                            }
                        }
                        _ => {}
                    }
                }
            }
            Self::ByteClass { .. } | Self::UnicodeClass { .. } => {}
        }
    }
}

/// The optional parts of a rule the feature union adds.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RuleAttributes {
    /// The rule's parameters; empty for a rule without.
    pub parameters: Vec<String>,
    /// The channel; a channel other than `default` makes the rule trivia.
    pub channel: Option<String>,
    /// The lexer modes the rule may match in.
    pub modes: Option<Vec<String>>,
    /// The semantic action, a statement list.
    pub action: Option<Vec<Operation>>,
}

impl RuleAttributes {
    /// Whether no attribute is set.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self == &Self::default()
    }
}

/// A macro declaration: `macro NAME(P, ...) = EXPRESSION`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GrammarMacro {
    /// The macro name.
    pub name: String,
    /// The macro parameters.
    pub parameters: Vec<String>,
    /// The macro body.
    pub expression: GrammarExpr,
}

/// An external scanner declaration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GrammarScanner {
    /// The scanner name.
    pub name: String,
    /// The tokens the scanner produces.
    pub tokens: Vec<String>,
    /// The scanner's statements.
    pub operations: Vec<Operation>,
}

/// One entry of a precedence order: a named precedence or a rule, whose
/// productions it ranks.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PrecedenceEntry {
    /// The precedence `namedPrecedence` gives this name.
    Name(String),
    /// Every precedence of this rule's productions.
    Rule(String),
}

impl PrecedenceEntry {
    /// Builds an entry from its kind word, `name` or `rule`, and its value.
    #[must_use]
    pub fn from_kind(kind: &str, value: String) -> Option<Self> {
        match kind {
            "name" => Some(Self::Name(value)),
            "rule" => Some(Self::Rule(value)),
            _ => None,
        }
    }

    /// The entry's kind word, `name` or `rule`.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        match self {
            Self::Name(_) => "name",
            Self::Rule(_) => "rule",
        }
    }

    /// The precedence or rule name.
    #[must_use]
    pub fn value(&self) -> &str {
        match self {
            Self::Name(value) | Self::Rule(value) => value,
        }
    }
}

/// The grammar-level declarations of the feature union.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GrammarDeclarations {
    /// `generalized`, `peg` or `longest`, when given.
    pub matching: Option<String>,
    /// The settling steps (see [`SETTLING_STEPS`]), when given.
    pub settling: Option<Vec<String>>,
    /// Names of grammars this one inherits from.
    pub imports: Vec<String>,
    /// The lexer modes beyond `default`.
    pub modes: Vec<String>,
    /// Trivia expressions allowed between any two tokens.
    pub extras: Vec<GrammarExpr>,
    /// Groups of rule names whose ambiguity is expected.
    pub conflicts: Vec<Vec<String>>,
    /// Orders of named precedences and rules, the earlier entry higher.
    pub precedences: Vec<Vec<PrecedenceEntry>>,
    /// Macro declarations.
    pub macros: Vec<GrammarMacro>,
    /// External scanner declarations.
    pub scanners: Vec<GrammarScanner>,
}

impl GrammarDeclarations {
    /// Whether no declaration is given.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self == &Self::default()
    }

    /// The token names the external scanners produce.
    #[must_use]
    pub fn external_tokens(&self) -> Vec<&str> {
        self.scanners
            .iter()
            .flat_map(|scanner| scanner.tokens.iter().map(String::as_str))
            .collect()
    }

    /// The names of the declaration kinds that are given, in listing order.
    #[must_use]
    pub fn present(&self) -> Vec<&'static str> {
        let mut present = Vec::new();
        if self.matching.is_some() {
            present.push("matching");
        }
        if self.settling.is_some() {
            present.push("settling");
        }
        for (name, empty) in [
            ("imports", self.imports.is_empty()),
            ("modes", self.modes.is_empty()),
            ("extras", self.extras.is_empty()),
            ("conflicts", self.conflicts.is_empty()),
            ("precedences", self.precedences.is_empty()),
            ("macros", self.macros.is_empty()),
            ("scanners", self.scanners.is_empty()),
        ] {
            if !empty {
                present.push(name);
            }
        }
        present
    }
}