characterminator 0.1.2

find and eliminate characters outside an allowed set, per file type and per file, so text costs fewer tokens
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
//! The transliteration map: what a disallowed character is rewritten to.
//!
//! One line is `<from> <to>`, where `to` may be absent and an absent `to` is
//! an explicit delete -- the ONLY way this crate ever removes a character
//! (V4). Both sides are tokens in the sense of `codepoint`, so a replacement
//! that is itself a space is written `U+0020`: the line is split on
//! whitespace, and a trailing blank nobody can see would be a poor way to
//! declare a space.
//!
//! Sources are held longest first, because a declared sequence must win over
//! a declared prefix of it (`src/fix/emoji:V31`).
//!
//! Two more line kinds serve `src/fix/words:V51`. `word <from> <to>` is an
//! entry whose replacement is a WORD, which `walk` keeps apart from a
//! neighbouring letter. `use <name>` reads a named builtin map at that line, so
//! an opt-in map is one line of the grammar every source already speaks -- a
//! `.ctrm-map` line or a `--map` flag alike (`src/rules:V18`).

use crate::fix::class;
use crate::fix::codepoint::decode;
use crate::fix::family::{ROOT, Tree};
use crate::fix::words::NAMED;
use crate::fix::{Class, Error, Family, MapEntry};
use crate::rules::Origin;
use std::cmp::Reverse;
use std::collections::{BTreeMap, HashSet};

/// The builtin map, in the `.ctrm-map` grammar (`src/charset:V22`).
///
/// Public as TEXT for the reason the sets file is: the builtin is the
/// lowest source of the precedence chain (`src/rules:V19`) and arrives
/// there the same way a `.ctrm-map` or a `--map` flag does. One grammar,
/// one parser, and an entry a user overrides by declaring it again.
///
/// Two files joined at compile time, as the sets are: the hand-written
/// typography and modifier map (V26, `src/fix/emoji:V60`), then the GENERATED
/// emoji sequence map (`src/fix/emoji:V62`), which a regeneration rewrites
/// whole. A builtin line number counts from the top of the joined text.
pub(crate) const BUILTIN: &str = concat!(
    include_str!("map.ctrm-map"),
    include_str!("emoji/emoji-seq.ctrm-map")
);

/// A declared source matched at the current position. `word` says the
/// replacement is a word, which `walk` keeps off a neighbouring letter.
pub(crate) struct Match {
    pub(crate) len: usize,
    pub(crate) to: String,
    pub(crate) word: bool,
}

/// What a matched source rewrites to: a literal replacement from a map
/// entry (a word or not), or a class whose answer depends on the fidelity
/// family (V28).
#[derive(Debug, Clone, PartialEq, Eq)]
enum Target {
    To { to: String, word: bool },
    Class(usize),
}

impl Target {
    const fn word(&self) -> bool {
        matches!(self, Self::To { word: true, .. })
    }
}

/// One rewritable source, ready for longest-first matching.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Candidate {
    source: String,
    target: Target,
}

/// The effective map: the entries, classes and families these lines
/// declared, plus the candidate list `resolve_at` walks.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Map {
    entries: Vec<MapEntry>,
    classes: Vec<Class>,
    tree: Tree,
    fidelity: Option<String>,
    candidates: Vec<Candidate>,
    /// Each first character, and the candidates opening with it, in candidate
    /// order: the scan asks at EVERY position, and two thousand emoji sequences
    /// (`src/fix/emoji:V62`) tried in turn cost seconds on a large file.
    starts: BTreeMap<char, Vec<usize>>,
}

impl Map {
    /// The builtin map (V26) at the default fidelity: what `ctrm fix` runs
    /// with no `.ctrm-map`. INFALLIBLE because the text is compiled in; a
    /// defect in it is this crate's, and `map_test` keeps it from shipping.
    #[must_use]
    pub fn builtin() -> Self {
        Self::parse(BUILTIN, &|line| Origin::Builtin { line })
            .unwrap_or_default()
    }

    /// Parse map lines. `origin` labels a 1-based line number, so the same
    /// parser serves a file, a builtin and a `--map` flag (`src/rules:V18`).
    pub(crate) fn parse(
        source: &str,
        origin: &dyn Fn(usize) -> Origin,
    ) -> Result<Self, Error> {
        Self::default().layer(source, origin)
    }

    /// Read another source OVER this map, later winning per source and
    /// per class name (`src/rules:V19`).
    ///
    /// The precedence chain hands its sources in order, and each one is a
    /// WHOLE text rather than a line: a `family` line declares a tree a
    /// later line may use, so a map cannot be assembled entry by entry
    /// the way rules and sets can. Validation runs after every layer, so
    /// a source that breaks the tree is refused where it was added rather
    /// than blamed on whatever came last.
    ///
    /// # Errors
    ///
    /// As [`Map::parse`]: a malformed line, a cyclic family tree, or a
    /// class naming a family nothing declared.
    pub(crate) fn layer(
        mut self,
        source: &str,
        origin: &dyn Fn(usize) -> Origin,
    ) -> Result<Self, Error> {
        for (index, line) in source.lines().enumerate() {
            self.read(line, index.saturating_add(1), origin)?;
        }
        self.dedupe();
        self.entries.sort_by_key(|entry| Reverse(entry.from.len()));
        self.tree.validate()?;
        self.validate_classes()?;
        self.index();
        Ok(self)
    }

    /// The family tree these lines declared, on top of the builtin one.
    #[must_use]
    pub(crate) fn tree(&self) -> &Tree {
        &self.tree
    }

    /// The equivalence classes these lines declared.
    #[must_use]
    pub(crate) fn classes(&self) -> &[Class] {
        &self.classes
    }

    /// The family classes resolve for. `ascii` unless the caller says
    /// otherwise, which is the strict default `src/rules:V1` asks for; the
    /// rules node supplies `text` and any `@<family>` per `src/rules:V29`.
    #[must_use]
    pub(crate) fn fidelity(&self) -> &str {
        self.fidelity.as_deref().unwrap_or(ROOT)
    }

    /// Resolve classes for `family` instead. The family has to be in the
    /// tree, so a typo is a config error rather than a silent fallback.
    pub fn with_fidelity(mut self, family: &str) -> Result<Self, Error> {
        self.tree.path(family)?;
        self.fidelity = Some(family.to_owned());
        Ok(self)
    }

    /// The effective entries, longest source first. Each carries its origin,
    /// which is how `explain` answers "why" (`src/rules:V20`).
    #[must_use]
    pub(crate) fn entries(&self) -> &[MapEntry] {
        &self.entries
    }

    /// The longest declared source -- map entry or class member -- that
    /// matches here, covers at least one disallowed character, and resolves
    /// to something. A span of allowed characters is never a violation, so
    /// it is never rewritten (V6).
    pub(crate) fn resolve_at(
        &self,
        rest: &str,
        allowed: &dyn Fn(char) -> bool,
    ) -> Result<Option<Match>, Error> {
        let first = rest.chars().next().and_then(|c| self.starts.get(&c));
        let held = first.into_iter().flatten();
        for candidate in held.filter_map(|at| self.candidates.get(*at)) {
            let Some(to) = self.try_at(candidate, rest, allowed)? else {
                continue;
            };
            return Ok(Some(Match {
                len: candidate.source.len(),
                to,
                word: candidate.target.word(),
            }));
        }
        Ok(None)
    }

    /// How many times a replacement may be rewritten again before the map is
    /// called cyclic. An acyclic chain visits each source at most once, so
    /// anything longer has come back to a source it already used.
    pub(crate) fn budget(&self) -> usize {
        self.candidates.len().saturating_add(1)
    }

    fn try_at(
        &self,
        candidate: &Candidate,
        rest: &str,
        allowed: &dyn Fn(char) -> bool,
    ) -> Result<Option<String>, Error> {
        if !rest.starts_with(&candidate.source)
            || !violates(&candidate.source, allowed)
        {
            return Ok(None);
        }
        self.target_of(&candidate.target, allowed)
    }

    fn target_of(
        &self,
        target: &Target,
        allowed: &dyn Fn(char) -> bool,
    ) -> Result<Option<String>, Error> {
        match target {
            Target::To { to, .. } => Ok(Some(to.clone())),
            Target::Class(index) => match self.classes.get(*index) {
                Some(class) => {
                    class::resolve(class, &self.tree, self.fidelity(), allowed)
                }
                None => Ok(None),
            },
        }
    }

    /// One line, dispatched by its first word.
    ///
    /// The keywords cost no source anyone could map: a `<from>` is only
    /// ever rewritten where it is DISALLOWED (V6), and `family`, `use` and
    /// `word` are ASCII, which every set holds.
    fn read(
        &mut self,
        line: &str,
        number: usize,
        origin: &dyn Fn(usize) -> Origin,
    ) -> Result<(), Error> {
        let body = line.trim();
        if body.is_empty() || body.starts_with('#') {
            return Ok(());
        }
        match body.split_once(' ') {
            Some(("family", rest)) => self.declare(rest, number),
            Some(("=", rest)) => self.classify(rest, number),
            Some(("use", rest)) => self.pull(rest.trim(), &origin(number)),
            Some(("word", rest)) => {
                self.add(word(rest, origin(number)), number)
            }
            _ => self.add(entry(body, origin(number), false), number),
        }
    }

    /// `use <name>`: read a named builtin map AT this line, so it sits in
    /// the precedence chain exactly where it was asked for and a later
    /// line still overrides one of its entries (`src/rules:V19`).
    ///
    /// Every entry it brings carries the ORIGIN of the `use` line rather
    /// than a line of the named file: the answer to "why was this
    /// rewritten" is the line somebody wrote to opt in (`src/rules:V20`),
    /// and it needs no new kind of origin to say so.
    fn pull(&mut self, name: &str, origin: &Origin) -> Result<(), Error> {
        let text = NAMED
            .iter()
            .find(|(held, _)| *held == name)
            .map(|(_, text)| *text)
            .ok_or_else(|| Error::UnknownMap {
                name: name.to_owned(),
            })?;
        for (index, line) in text.lines().enumerate() {
            self.read(line, index.saturating_add(1), &|_| origin.clone())?;
        }
        Ok(())
    }

    fn declare(&mut self, rest: &str, number: usize) -> Result<(), Error> {
        let declared = family(rest).ok_or(Error::Syntax { line: number })?;
        self.tree.declare(declared)
    }

    /// A later line wins over an earlier one for the same class name, as it
    /// does for a map entry (`src/rules:V19`).
    fn classify(&mut self, rest: &str, number: usize) -> Result<(), Error> {
        let declared =
            class::parse_line(rest).ok_or(Error::Syntax { line: number })?;
        self.classes.retain(|held| held.name != declared.name);
        self.classes.push(declared);
        Ok(())
    }

    /// A later line wins over an earlier one for the same source, which is
    /// the precedence the config chain expects (`src/rules:V19`).
    fn add(
        &mut self,
        entry: Option<MapEntry>,
        number: usize,
    ) -> Result<(), Error> {
        let entry = entry.ok_or(Error::Syntax { line: number })?;
        if never_withheld(&entry.from) {
            return Err(Error::NeverRewritten { line: number });
        }
        self.entries.push(entry);
        Ok(())
    }

    /// Keep the LAST entry for each source, where its last push left it:
    /// what dropping the earlier one on every push kept, in one walk from
    /// the end rather than one walk of every entry per line -- quadratic
    /// over the two thousand builtin lines.
    fn dedupe(&mut self) {
        let mut seen = HashSet::new();
        let mut kept: Vec<MapEntry> = std::mem::take(&mut self.entries)
            .into_iter()
            .rev()
            .filter(|entry| seen.insert(entry.from.clone()))
            .collect();
        kept.reverse();
        self.entries = kept;
    }

    /// Every family a class member names has to be in the tree, or that
    /// member could never resolve and the line is a typo nothing reports.
    fn validate_classes(&self) -> Result<(), Error> {
        for class in &self.classes {
            for member in &class.members {
                self.tree.path(&member.family)?;
            }
        }
        Ok(())
    }

    /// Build the longest-first candidate list. A map entry beats a class
    /// member of the same length, because a line naming one source is more
    /// specific than a class listing it among alternatives; the sort is
    /// stable, so entries pushed first keep that priority.
    fn index(&mut self) {
        self.candidates = Vec::new();
        for entry in &self.entries {
            self.candidates.push(Candidate {
                source: entry.from.clone(),
                target: Target::To {
                    to: entry.to.clone(),
                    word: entry.word,
                },
            });
        }
        self.index_classes();
        self.candidates
            .sort_by_key(|held| Reverse(held.source.len()));
        self.index_starts();
    }

    /// Group the sorted candidates by first character, keeping their order.
    fn index_starts(&mut self) {
        self.starts = BTreeMap::new();
        for (at, held) in self.candidates.iter().enumerate() {
            if let Some(first) = held.source.chars().next() {
                self.starts.entry(first).or_default().push(at);
            }
        }
    }

    fn index_classes(&mut self) {
        let mut found = Vec::new();
        for (index, class) in self.classes.iter().enumerate() {
            for member in &class.members {
                found.push(Candidate {
                    source: member.text.clone(),
                    target: Target::Class(index),
                });
            }
        }
        self.candidates.append(&mut found);
    }
}

/// One `family <name> <parent>` line. A name carrying `:` or `,` is
/// refused, because those two characters are what a class line is made of.
fn family(rest: &str) -> Option<Family> {
    let mut words = rest.split_whitespace();
    let name = named(words.next()?)?;
    let parent = named(words.next()?)?;
    if words.next().is_some() {
        return None;
    }
    Some(Family {
        name,
        parent: Some(parent),
    })
}

pub(crate) fn named(word: &str) -> Option<String> {
    if word.contains([':', ',']) {
        return None;
    }
    Some(word.to_owned())
}

/// Whether every character of a source is in `ascii`, which every rule
/// grants (`src/rules:V24`) and no hazard touches: such a source is never
/// a violation, so it is never rewritten (V6). `foo bar` parsed as a map
/// line that did nothing (`src/judge:V116`, B68).
fn never_withheld(source: &str) -> bool {
    let ascii = crate::charset::builtin::ascii();
    source.chars().all(|ch| ascii.contains(ch))
}

/// Whether any character of `text` is one the caller disallows.
pub(crate) fn violates(text: &str, allowed: &dyn Fn(char) -> bool) -> bool {
    text.chars().any(|ch| !allowed(ch))
}

/// One `<from> <to>` line. An empty source is rejected: it would match at
/// every position and rewrite nothing.
fn entry(body: &str, origin: Origin, word: bool) -> Option<MapEntry> {
    let mut words = body.split_whitespace();
    let from = decode(words.next()?)?;
    let to = match words.next() {
        Some(word) => decode(word)?,
        None => String::new(),
    };
    if from.is_empty() || words.next().is_some() {
        return None;
    }
    Some(MapEntry {
        from,
        to,
        word,
        origin,
    })
}

/// One `word <from> <to>` line (`src/fix/words:V51`). The replacement is
/// required: a word that deletes is a delete, and the plain line already says
/// that.
fn word(rest: &str, origin: Origin) -> Option<MapEntry> {
    entry(rest, origin, true).filter(|held| !held.to.is_empty())
}

#[cfg(test)]
#[path = "map_test.rs"]
mod tests;