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
//! Decorators: declared, removable changes to the items of a pipeline level.
//!
//! Each level shows a decorator its items as records, maps of string fields,
//! and reads the decorated records back (docs/decorators.md lists the fields
//! and what `drop` means at each level). A [`DecoratorSet`] is stored as
//! Links Notation, one decorator per line, in the same canonical form the
//! JavaScript runtime writes, so both runtimes share one corpus.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use links_notation::{LiNo, ParserConfig, parse_lino_to_links_with_config};

use crate::grammar::{percent_decode_links_text, percent_encode_links_text};

/// A record a decorator sees: field names and their texts.
pub type DecoratorRecord = BTreeMap<String, String>;

/// The pipeline levels a decorator extends.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DecoratorLevel {
    /// The rules of an imported grammar.
    Importer,
    /// The rules of a grammar a parser is compiled from.
    GrammarRule,
    /// The decisions of a grammar merge.
    MergeDecision,
    /// The translation of a construct between native grammars.
    ConceptMapping,
    /// The nodes and tokens of a syntax tree.
    Executor,
    /// The ERROR and MISSING nodes of a syntax tree.
    Recovery,
    /// The syntax facts a program model is built from.
    CstToAst,
    /// The replacements of a transformation.
    Transformation,
    /// The lines of an emitted grammar.
    Emitter,
    /// The templates of a translation rule set.
    TranslationRule,
}

/// The pipeline levels a decorator extends, in pipeline order.
pub const DECORATOR_LEVELS: [DecoratorLevel; 10] = [
    DecoratorLevel::Importer,
    DecoratorLevel::GrammarRule,
    DecoratorLevel::MergeDecision,
    DecoratorLevel::ConceptMapping,
    DecoratorLevel::Executor,
    DecoratorLevel::Recovery,
    DecoratorLevel::CstToAst,
    DecoratorLevel::Transformation,
    DecoratorLevel::Emitter,
    DecoratorLevel::TranslationRule,
];

impl DecoratorLevel {
    /// The name the JavaScript runtime and the Links Notation use.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Importer => "importer",
            Self::GrammarRule => "grammar-rule",
            Self::MergeDecision => "merge-decision",
            Self::ConceptMapping => "concept-mapping",
            Self::Executor => "executor",
            Self::Recovery => "recovery",
            Self::CstToAst => "cst-to-ast",
            Self::Transformation => "transformation",
            Self::Emitter => "emitter",
            Self::TranslationRule => "translation-rule",
        }
    }

    /// The level named `name`.
    #[must_use]
    pub fn parse(name: &str) -> Option<Self> {
        DECORATOR_LEVELS
            .into_iter()
            .find(|level| level.as_str() == name)
    }
}

impl fmt::Display for DecoratorLevel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Why a decorator, a set or a decoration is refused.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DecoratorError {
    /// What is wrong.
    pub message: String,
}

impl DecoratorError {
    /// An error with `message`.
    #[must_use]
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
}

impl fmt::Display for DecoratorError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for DecoratorError {}

/// One action of a decorator.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DecoratorAction {
    /// Sets `field` to `value`.
    Set {
        /// The field.
        field: String,
        /// The text it is set to.
        value: String,
    },
    /// Replaces every occurrence of the non-empty text `from` in `field`.
    Replace {
        /// The field.
        field: String,
        /// The text replaced.
        from: String,
        /// The text written instead.
        to: String,
    },
    /// Removes the record.
    Drop,
}

/// One decorator: the records of `level` that have every field of `when`
/// get `actions`, in order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Decorator {
    id: String,
    level: DecoratorLevel,
    order: i64,
    when: Vec<(String, String)>,
    actions: Vec<DecoratorAction>,
}

fn is_name(value: &str) -> bool {
    !value.is_empty()
        && value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
}

impl Decorator {
    /// Checks one decorator.
    ///
    /// # Errors
    ///
    /// A [`DecoratorError`] for an invalid id or field name, no action or a
    /// replacement of empty text.
    pub fn new(
        id: impl Into<String>,
        level: DecoratorLevel,
        order: i64,
        when: Vec<(String, String)>,
        actions: Vec<DecoratorAction>,
    ) -> Result<Self, DecoratorError> {
        let id = id.into();
        if !is_name(&id) {
            return Err(DecoratorError::new(format!("invalid decorator id {id:?}")));
        }
        if when.iter().any(|(field, _)| !is_name(field)) {
            return Err(DecoratorError::new(format!(
                "decorator {id} has an invalid condition"
            )));
        }
        if actions.is_empty() {
            return Err(DecoratorError::new(format!("decorator {id} has no action")));
        }
        for action in &actions {
            match action {
                DecoratorAction::Drop => {}
                DecoratorAction::Set { field, .. } if is_name(field) => {}
                DecoratorAction::Replace { field, from, .. } if is_name(field) => {
                    if from.is_empty() {
                        return Err(DecoratorError::new(format!(
                            "decorator {id} replaces an empty or non-string text in {field}"
                        )));
                    }
                }
                _ => {
                    return Err(DecoratorError::new(format!(
                        "decorator {id} has an action without a field"
                    )));
                }
            }
        }
        Ok(Self {
            id,
            level,
            order,
            when,
            actions,
        })
    }

    /// The id, unique in a set.
    #[must_use]
    pub fn id(&self) -> &str {
        &self.id
    }

    /// The level the decorator extends.
    #[must_use]
    pub const fn level(&self) -> DecoratorLevel {
        self.level
    }

    /// The position among the decorators of the level; ties go by id.
    #[must_use]
    pub const fn order(&self) -> i64 {
        self.order
    }

    /// The fields a record must have for the decorator to apply.
    #[must_use]
    pub fn when(&self) -> &[(String, String)] {
        &self.when
    }

    /// The actions, applied in order.
    #[must_use]
    pub fn actions(&self) -> &[DecoratorAction] {
        &self.actions
    }

    fn render(&self) -> String {
        let mut parts = vec![
            format!("decorator {}", self.id),
            format!("(level {})", self.level),
            format!("(order {})", self.order),
        ];
        if !self.when.is_empty() {
            let pairs: Vec<String> = self
                .when
                .iter()
                .map(|(field, value)| format!("({field} {})", percent_encode_links_text(value)))
                .collect();
            parts.push(format!("(when {})", pairs.join(" ")));
        }
        for action in &self.actions {
            parts.push(match action {
                DecoratorAction::Drop => "(drop)".to_owned(),
                DecoratorAction::Set { field, value } => {
                    format!("(set {field} {})", percent_encode_links_text(value))
                }
                DecoratorAction::Replace { field, from, to } => format!(
                    "(replace {field} {} {})",
                    percent_encode_links_text(from),
                    percent_encode_links_text(to)
                ),
            });
        }
        format!("({})", parts.join(" "))
    }
}

/// An immutable set of decorators.
///
/// The decorators of a level compose in a defined order, by `order` and then
/// by id, each one decorating what the one before it produced;
/// [`DecoratorSet::add`] and [`DecoratorSet::remove`] return new sets, so
/// removing a decorator gives exactly the output of the set without it.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DecoratorSet {
    decorators: Vec<Decorator>,
}

impl DecoratorSet {
    /// A set of `decorators`, sorted by order and then by id.
    ///
    /// # Errors
    ///
    /// A [`DecoratorError`] when two decorators share an id.
    pub fn new(mut decorators: Vec<Decorator>) -> Result<Self, DecoratorError> {
        let mut ids = BTreeSet::new();
        for decorator in &decorators {
            if !ids.insert(decorator.id.as_str()) {
                return Err(DecoratorError::new(format!(
                    "duplicate decorator id {}",
                    decorator.id
                )));
            }
        }
        decorators.sort_by(|left, right| {
            left.order
                .cmp(&right.order)
                .then_with(|| left.id.cmp(&right.id))
        });
        Ok(Self { decorators })
    }

    /// The set without decorators.
    #[must_use]
    pub const fn empty() -> Self {
        Self {
            decorators: Vec::new(),
        }
    }

    /// Reads the decorators of a Links Notation document (see
    /// [`DecoratorSet::to_lino`]).
    ///
    /// # Errors
    ///
    /// A [`DecoratorError`] for text that is not Links Notation or a
    /// statement that is not a valid decorator.
    pub fn from_lino(text: &str) -> Result<Self, DecoratorError> {
        let statements = parse_lino_to_links_with_config(text, &ParserConfig::without_comments())
            .map_err(|error| DecoratorError::new(error.to_string()))?;
        Self::new(
            statements
                .iter()
                .map(read_decorator)
                .collect::<Result<_, _>>()?,
        )
    }

    /// The decorators, in the order they compose.
    #[must_use]
    pub fn decorators(&self) -> &[Decorator] {
        &self.decorators
    }

    /// The number of decorators.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.decorators.len()
    }

    /// Whether the set has no decorator.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.decorators.is_empty()
    }

    /// The ids, in the order the decorators compose.
    #[must_use]
    pub fn ids(&self) -> Vec<&str> {
        self.decorators.iter().map(Decorator::id).collect()
    }

    /// The set with `decorators` added.
    ///
    /// # Errors
    ///
    /// A [`DecoratorError`] when an id is taken.
    pub fn add(
        &self,
        decorators: impl IntoIterator<Item = Decorator>,
    ) -> Result<Self, DecoratorError> {
        Self::new(self.decorators.iter().cloned().chain(decorators).collect())
    }

    /// The set without the decorator `id`.
    ///
    /// # Errors
    ///
    /// A [`DecoratorError`] when the set has no decorator `id`.
    pub fn remove(&self, id: &str) -> Result<Self, DecoratorError> {
        if !self.decorators.iter().any(|decorator| decorator.id == id) {
            return Err(DecoratorError::new(format!("no decorator {id} to remove")));
        }
        Ok(Self {
            decorators: self
                .decorators
                .iter()
                .filter(|decorator| decorator.id != id)
                .cloned()
                .collect(),
        })
    }

    /// The decorators of `level`, in the order they compose.
    pub fn for_level(&self, level: DecoratorLevel) -> impl Iterator<Item = &Decorator> {
        self.decorators
            .iter()
            .filter(move |decorator| decorator.level == level)
    }

    /// Whether the set has a decorator of `level`.
    #[must_use]
    pub fn has(&self, level: DecoratorLevel) -> bool {
        self.for_level(level).next().is_some()
    }

    /// The record the decorators of `level` make of `record`, or `None` when
    /// one of them drops it. A missing field reads as empty text.
    #[must_use]
    pub fn decorate(
        &self,
        level: DecoratorLevel,
        record: &DecoratorRecord,
    ) -> Option<DecoratorRecord> {
        let mut current = record.clone();
        for decorator in self.for_level(level) {
            let applies = decorator
                .when
                .iter()
                .all(|(field, value)| current.get(field).map_or("", String::as_str) == value);
            if !applies {
                continue;
            }
            for action in &decorator.actions {
                match action {
                    DecoratorAction::Drop => return None,
                    DecoratorAction::Set { field, value } => {
                        current.insert(field.clone(), value.clone());
                    }
                    DecoratorAction::Replace { field, from, to } => {
                        let text = current
                            .get(field)
                            .map_or("", String::as_str)
                            .replace(from, to);
                        current.insert(field.clone(), text);
                    }
                }
            }
        }
        Some(current)
    }

    /// Decorates every record of `records`, leaving out the dropped ones.
    #[must_use]
    pub fn decorate_all(
        &self,
        level: DecoratorLevel,
        records: &[DecoratorRecord],
    ) -> Vec<DecoratorRecord> {
        records
            .iter()
            .filter_map(|record| self.decorate(level, record))
            .collect()
    }

    /// The canonical Links Notation of the set, one decorator per line.
    #[must_use]
    pub fn to_lino(&self) -> String {
        self.decorators
            .iter()
            .fold(String::new(), |mut text, decorator| {
                text.push_str(&decorator.render());
                text.push('\n');
                text
            })
    }
}

/// Builds a record from `(field, text)` pairs.
#[must_use]
pub fn decorator_record<const N: usize>(fields: [(&str, &str); N]) -> DecoratorRecord {
    fields
        .into_iter()
        .map(|(field, text)| (field.to_owned(), text.to_owned()))
        .collect()
}

/// The text of `field` in a decorated record, empty when absent.
#[must_use]
pub fn record_field<'a>(record: &'a DecoratorRecord, field: &str) -> &'a str {
    record.get(field).map_or("", String::as_str)
}

type Node = LiNo<String>;

fn word_of(node: &Node) -> Option<&str> {
    match node {
        LiNo::Ref(word) => Some(word),
        LiNo::Link {
            id: Some(word),
            values,
        } if values.is_empty() => Some(word),
        LiNo::Link { .. } => None,
    }
}

fn values_of(node: &Node) -> &[Node] {
    match node {
        LiNo::Link { values, .. } => values,
        LiNo::Ref(_) => &[],
    }
}

fn words(values: &[Node]) -> Result<Vec<&str>, DecoratorError> {
    values
        .iter()
        .map(|value| {
            word_of(value).ok_or_else(|| {
                DecoratorError::new("a decorator field holds a nested link where a word belongs")
            })
        })
        .collect()
}

fn unquote(text: &str, id: &str) -> Result<String, DecoratorError> {
    percent_decode_links_text(text).map_err(|_| {
        DecoratorError::new(format!(
            "decorator {id} has a malformed percent-encoded value {text}"
        ))
    })
}

fn read_decorator(statement: &Node) -> Result<Decorator, DecoratorError> {
    let shape = || {
        DecoratorError::new("a decorator statement must read (decorator <id> (level <level>) …)")
    };
    let LiNo::Link { id: None, values } = statement else {
        return Err(shape());
    };
    let [head, name, rest @ ..] = values.as_slice() else {
        return Err(shape());
    };
    if word_of(head) != Some("decorator") {
        return Err(shape());
    }
    let id = word_of(name).ok_or_else(shape)?.to_owned();
    let mut level = None;
    let mut order = 0;
    let mut when = Vec::new();
    let mut actions = Vec::new();
    for part in rest {
        let parts = values_of(part);
        let keyword = parts.first().and_then(word_of);
        let args = parts.get(1..).unwrap_or_default();
        match keyword {
            Some("level") => {
                let name = words(args)?.first().copied().unwrap_or_default();
                level = Some(DecoratorLevel::parse(name).ok_or_else(|| {
                    DecoratorError::new(format!("decorator {id} names unknown level {name:?}"))
                })?);
            }
            Some("order") => {
                let text = words(args)?.first().copied().unwrap_or_default();
                order = text
                    .parse()
                    .ok()
                    .filter(|_| {
                        let digits = text.strip_prefix('-').unwrap_or(text);
                        !digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit())
                    })
                    .ok_or_else(|| {
                        DecoratorError::new(format!("decorator {id} has a non-integer order"))
                    })?;
            }
            Some("when") => {
                for pair in args {
                    let [field, value] = words(values_of(pair))?[..] else {
                        return Err(DecoratorError::new(format!(
                            "decorator {id} has an invalid condition"
                        )));
                    };
                    when.push((field.to_owned(), unquote(value, &id)?));
                }
            }
            Some("set") => {
                let [field, value] = words(args)?[..] else {
                    return Err(DecoratorError::new(format!(
                        "decorator {id} has an action without a field"
                    )));
                };
                actions.push(DecoratorAction::Set {
                    field: field.to_owned(),
                    value: unquote(value, &id)?,
                });
            }
            Some("replace") => {
                let [field, from, to] = words(args)?[..] else {
                    return Err(DecoratorError::new(format!(
                        "decorator {id} replaces an empty or non-string text"
                    )));
                };
                actions.push(DecoratorAction::Replace {
                    field: field.to_owned(),
                    from: unquote(from, &id)?,
                    to: unquote(to, &id)?,
                });
            }
            Some("drop") => actions.push(DecoratorAction::Drop),
            other => {
                return Err(DecoratorError::new(format!(
                    "decorator {id} has an unknown part {other:?}"
                )));
            }
        }
    }
    let level =
        level.ok_or_else(|| DecoratorError::new(format!("decorator {id} names no level")))?;
    Decorator::new(id, level, order, when, actions)
}