icu_experimental/transliterate/compile/
pass2.rs

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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use super::*;
use crate::transliterate::provider as ds;
use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;
use alloc::string::ToString;
use core::fmt::{self, Display, Formatter};
use icu_collections::codepointinvlist::CodePointInversionList;
use icu_locale_core::Locale;
use zerovec::{vecs::Index32, VarZeroVec};

type Result<T> = core::result::Result<T, CompileError>;

macro_rules! impl_insert {
    ($fn_name:ident, $field:ident, $elt_type:ty, $($next_field:tt)*) => {
        fn $fn_name(&mut self, elt: $elt_type) -> char {
            // pass 1 is responsible for this
            debug_assert!(self.$field.current < self.$($next_field)*);
            #[allow(clippy::unwrap_used)] // the whole PUP (15) consists of valid chars
            let standin = char::try_from(self.$field.current).unwrap();
            self.$field.vec.push(elt);
            self.$field.current += 1;
            standin
        }
    };
}

struct MutVarTableField<T> {
    vec: Vec<T>,
    base: u32,
    current: u32,
}

// a mutable version of the zero-copy `VarTable` that allows easy construction
struct MutVarTable {
    compounds: MutVarTableField<String>,
    quantifiers_opt: MutVarTableField<String>,
    quantifiers_kleene: MutVarTableField<String>,
    quantifiers_kleene_plus: MutVarTableField<String>,
    segments: MutVarTableField<ds::Segment<'static>>,
    unicode_sets: MutVarTableField<parse::UnicodeSet>,
    function_calls: MutVarTableField<ds::FunctionCall<'static>>,
    left_placeholder_base: u32,
    right_placeholder_base: u32,
    backref_base: u32,
    counts: pass1::SpecialConstructCounts,
}

impl MutVarTable {
    fn try_new_from_counts(counts: pass1::SpecialConstructCounts) -> Result<Self> {
        if counts.num_total() > ds::VarTable::NUM_DYNAMIC {
            return Err(CompileErrorKind::TooManySpecials.into());
        }

        let compounds_base = ds::VarTable::BASE as u32;
        let quantifiers_opt_base = compounds_base + counts.num_compounds as u32;
        let quantifiers_kleene_base = quantifiers_opt_base + counts.num_quantifiers_opt as u32;
        let quantifiers_kleene_plus_base =
            quantifiers_kleene_base + counts.num_quantifiers_kleene as u32;
        let segments_base =
            quantifiers_kleene_plus_base + counts.num_quantifiers_kleene_plus as u32;
        let unicode_sets_base = segments_base + counts.num_segments as u32;
        let function_calls_base = unicode_sets_base + counts.num_unicode_sets as u32;

        let left_placeholder_base = function_calls_base + counts.num_function_calls as u32;

        // if placeholders needed to encode 0, we would need to add 1 to this. they don't, so this
        // is fine.
        let right_placeholder_base = left_placeholder_base + counts.max_left_placeholders;
        // same here
        let backref_base = right_placeholder_base + counts.max_right_placeholders;

        Ok(Self {
            compounds: MutVarTableField {
                vec: Vec::with_capacity(counts.num_compounds),
                base: compounds_base,
                current: compounds_base,
            },
            quantifiers_opt: MutVarTableField {
                vec: Vec::with_capacity(counts.num_quantifiers_opt),
                base: quantifiers_opt_base,
                current: quantifiers_opt_base,
            },
            quantifiers_kleene: MutVarTableField {
                vec: Vec::with_capacity(counts.num_quantifiers_kleene),
                base: quantifiers_kleene_base,
                current: quantifiers_kleene_base,
            },
            quantifiers_kleene_plus: MutVarTableField {
                vec: Vec::with_capacity(counts.num_quantifiers_kleene_plus),
                base: quantifiers_kleene_plus_base,
                current: quantifiers_kleene_plus_base,
            },
            segments: MutVarTableField {
                vec: Vec::with_capacity(counts.num_segments),
                base: segments_base,
                current: segments_base,
            },
            unicode_sets: MutVarTableField {
                vec: Vec::with_capacity(counts.num_unicode_sets),
                base: unicode_sets_base,
                current: unicode_sets_base,
            },
            function_calls: MutVarTableField {
                vec: Vec::with_capacity(counts.num_function_calls),
                base: function_calls_base,
                current: function_calls_base,
            },
            counts,
            left_placeholder_base,
            right_placeholder_base,
            backref_base,
        })
    }

    impl_insert!(insert_compound, compounds, String, quantifiers_opt.base);
    impl_insert!(
        insert_quantifier_opt,
        quantifiers_opt,
        String,
        quantifiers_kleene.base
    );
    impl_insert!(
        insert_quantifier_kleene,
        quantifiers_kleene,
        String,
        quantifiers_kleene_plus.base
    );
    impl_insert!(
        insert_quantifier_kleene_plus,
        quantifiers_kleene_plus,
        String,
        segments.base
    );
    impl_insert!(
        insert_segment,
        segments,
        ds::Segment<'static>,
        unicode_sets.base
    );
    impl_insert!(
        insert_unicode_set,
        unicode_sets,
        parse::UnicodeSet,
        function_calls.base
    );
    impl_insert!(
        insert_function_call,
        function_calls,
        ds::FunctionCall<'static>,
        backref_base
    );

    fn standin_for_cursor(&self, left: u32, right: u32) -> char {
        match (left, right) {
            (0, 0) => ds::VarTable::RESERVED_PURE_CURSOR,
            (left, 0) => {
                debug_assert!(left <= self.counts.max_left_placeholders);
                #[allow(clippy::unwrap_used)] // constructor checks this via num_totals
                char::try_from(self.left_placeholder_base + left - 1).unwrap()
            }
            (0, right) => {
                debug_assert!(right <= self.counts.max_right_placeholders);
                #[allow(clippy::unwrap_used)] // constructor checks this via num_totals
                char::try_from(self.right_placeholder_base + right - 1).unwrap()
            }
            _ => {
                // validation catches (>0, >0) cases
                unreachable!()
            }
        }
    }

    fn standin_for_backref(&self, backref_num: u32) -> char {
        debug_assert!(backref_num > 0);
        // -1 because backrefs are 1-indexed
        let standin = self.backref_base + backref_num - 1;
        debug_assert!(standin <= ds::VarTable::MAX_DYNAMIC as u32);
        #[allow(clippy::unwrap_used)] // constructor checks this via num_totals
        char::try_from(standin).unwrap()
    }

    fn finalize(&self) -> ds::VarTable<'static> {
        // we computed the exact counts, so when we call finalize, we should be full
        debug_assert!(self.is_full());

        ds::VarTable {
            compounds: VarZeroVec::from(&self.compounds.vec),
            quantifiers_opt: VarZeroVec::from(&self.quantifiers_opt.vec),
            quantifiers_kleene: VarZeroVec::from(&self.quantifiers_kleene.vec),
            quantifiers_kleene_plus: VarZeroVec::from(&self.quantifiers_kleene_plus.vec),
            segments: VarZeroVec::from(&self.segments.vec),
            unicode_sets: VarZeroVec::from(&self.unicode_sets.vec),
            function_calls: VarZeroVec::from(&self.function_calls.vec),
            // these casts are safe, because the constructor checks that everything is in range,
            // and the range has a length of less than 2^16
            max_left_placeholder_count: self.counts.max_left_placeholders as u16,
            max_right_placeholder_count: self.counts.max_right_placeholders as u16,
        }
    }

    fn is_full(&self) -> bool {
        self.compounds.vec.len() == self.counts.num_compounds
            && self.quantifiers_opt.vec.len() == self.counts.num_quantifiers_opt
            && self.quantifiers_kleene.vec.len() == self.counts.num_quantifiers_kleene
            && self.quantifiers_kleene_plus.vec.len() == self.counts.num_quantifiers_kleene_plus
            && self.segments.vec.len() == self.counts.num_segments
            && self.unicode_sets.vec.len() == self.counts.num_unicode_sets
            && self.function_calls.vec.len() == self.counts.num_function_calls
    }
}

enum LiteralOrStandin<'a> {
    Literal(&'a str),
    Standin(char),
}

// gives us `to_string` and makes clippy happy
impl Display for LiteralOrStandin<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match *self {
            LiteralOrStandin::Literal(s) => write!(f, "{}", s),
            LiteralOrStandin::Standin(c) => write!(f, "{}", c),
        }
    }
}

pub(super) struct Pass2<'a, 'p> {
    var_table: MutVarTable,
    var_definitions: &'a BTreeMap<String, &'p [parse::Element]>,
    // transliterators available at datagen time exist in this mapping from legacy ID to BCP47 ID
    id_mapping: &'a BTreeMap<String, Locale>,
    // the inverse of VarTable.compounds
    var_to_char: BTreeMap<String, char>,
    // the current segment index (per conversion rule)
    curr_segment: u16,
}

impl<'a, 'p> Pass2<'a, 'p> {
    pub(super) fn run(
        pass1: pass1::DirectedPass1Result<'p>,
        var_definitions: &'a BTreeMap<String, &'p [parse::Element]>,
        id_mapping: &'a BTreeMap<String, Locale>,
    ) -> Result<ds::RuleBasedTransliterator<'static>> {
        let mut pass2 = Pass2 {
            var_table: MutVarTable::try_new_from_counts(pass1.data.counts)?,
            var_definitions,
            id_mapping,
            var_to_char: BTreeMap::new(),
            curr_segment: 0,
        };
        let mut compiled_transform_groups: Vec<VarZeroVec<'static, ds::SimpleIdULE>> = Vec::new();
        let mut compiled_conversion_groups: Vec<VarZeroVec<'static, ds::RuleULE, Index32>> =
            Vec::new();

        for (transform_group, conversion_group) in pass1.groups {
            let compiled_transform_group: Vec<_> = transform_group
                .into_iter()
                .map(|id| pass2.compile_single_id(id.into_owned()))
                .collect();
            compiled_transform_groups.push(VarZeroVec::from(&compiled_transform_group));

            let compiled_conversion_group: Vec<_> = conversion_group
                .into_iter()
                .map(|rule| pass2.compile_conversion_rule(rule))
                .collect();
            compiled_conversion_groups.push(VarZeroVec::from(&compiled_conversion_group));
        }

        Ok(ds::RuleBasedTransliterator {
            visibility: true,
            filter: pass1.filter.unwrap_or(CodePointInversionList::all()),
            id_group_list: VarZeroVec::from(&compiled_transform_groups),
            rule_group_list: VarZeroVec::from(&compiled_conversion_groups),
            variable_table: pass2.var_table.finalize(),
        })
    }

    fn compile_conversion_rule(
        &mut self,
        rule: rule_group_agg::UniConversionRule<'p>,
    ) -> ds::Rule<'static> {
        self.curr_segment = 0;
        let ante = self.compile_section(rule.ante, parse::ElementLocation::Source);
        let key = self.compile_section(rule.key, parse::ElementLocation::Source);
        let post = self.compile_section(rule.post, parse::ElementLocation::Source);
        let replacer = self.compile_section(rule.replacement, parse::ElementLocation::Target);
        ds::Rule {
            ante: ante.into(),
            key: key.into(),
            post: post.into(),
            replacer: replacer.into(),
        }
    }

    // returns the standin char
    fn compile_variable(&mut self, var: &str) -> char {
        if let Some(&c) = self.var_to_char.get(var) {
            return c;
        }
        // the first pass ensures that all variables are defined
        #[allow(clippy::indexing_slicing)]
        let definition = self.var_definitions[var];
        let compiled = self.compile_section(definition, parse::ElementLocation::VariableDefinition);
        let standin = self.var_table.insert_compound(compiled);
        self.var_to_char.insert(var.to_owned(), standin);
        standin
    }

    fn compile_section(
        &mut self,
        section: &'p [parse::Element],
        loc: parse::ElementLocation,
    ) -> String {
        let mut result = String::new();
        for elt in section {
            if elt.kind().skipped_in(loc) {
                continue;
            }
            match self.compile_element(elt, loc) {
                LiteralOrStandin::Literal(s) => result.push_str(s),
                LiteralOrStandin::Standin(c) => result.push(c),
            }
        }
        result
    }

    fn compile_element(
        &mut self,
        elt: &'p parse::Element,
        loc: parse::ElementLocation,
    ) -> LiteralOrStandin<'p> {
        debug_assert!(!elt.kind().skipped_in(loc));
        match elt {
            parse::Element::Literal(s) => LiteralOrStandin::Literal(s),
            parse::Element::VariableRef(v) => LiteralOrStandin::Standin(self.compile_variable(v)),
            parse::Element::Quantifier(kind, inner) => {
                let inner = self.compile_element(inner, loc).to_string();
                let standin = match kind {
                    parse::QuantifierKind::ZeroOrOne => self.var_table.insert_quantifier_opt(inner),
                    parse::QuantifierKind::ZeroOrMore => {
                        self.var_table.insert_quantifier_kleene(inner)
                    }
                    parse::QuantifierKind::OneOrMore => {
                        self.var_table.insert_quantifier_kleene_plus(inner)
                    }
                };
                LiteralOrStandin::Standin(standin)
            }
            &parse::Element::BackRef(num) => {
                LiteralOrStandin::Standin(self.var_table.standin_for_backref(num))
            }
            parse::Element::AnchorEnd => {
                LiteralOrStandin::Standin(ds::VarTable::RESERVED_ANCHOR_END)
            }
            parse::Element::AnchorStart => {
                LiteralOrStandin::Standin(ds::VarTable::RESERVED_ANCHOR_START)
            }
            parse::Element::UnicodeSet(set) => {
                let standin = self.var_table.insert_unicode_set(set.clone());
                LiteralOrStandin::Standin(standin)
            }
            parse::Element::Segment(inner) => {
                // important to store the number before the recursive call
                let idx = self.curr_segment;
                self.curr_segment += 1;
                let inner = self.compile_section(inner, loc);
                let standin = self.var_table.insert_segment(ds::Segment {
                    idx,
                    content: inner.into(),
                });
                LiteralOrStandin::Standin(standin)
            }
            parse::Element::FunctionCall(id, inner) => {
                let inner = self.compile_section(inner, loc);
                let id = self.compile_single_id(id.clone());
                let standin = self.var_table.insert_function_call(ds::FunctionCall {
                    translit: id,
                    arg: inner.into(),
                });
                LiteralOrStandin::Standin(standin)
            }
            &parse::Element::Cursor(pre, post) => {
                LiteralOrStandin::Standin(self.var_table.standin_for_cursor(pre, post))
            }
        }
    }

    fn compile_single_id(&self, id: parse::SingleId) -> ds::SimpleId<'static> {
        let unparsed = id.basic_id.to_string();

        let string = if matches!(
            unparsed.as_str(),
            "any-nfc"
                | "any-nfkc"
                | "any-nfd"
                | "any-nfkd"
                | "any-null"
                | "any-remove"
                | "any-lower"
                | "any-upper"
                | "any-title"
                | "any-hex/unicode"
                | "any-hex/rust"
                | "any-hex/xml"
                | "any-hex/perl"
                | "any-hex/plain"
        ) {
            unparsed
        } else if let Some(bcp47_id) = self.id_mapping.get(&unparsed) {
            bcp47_id.to_string()
        } else {
            icu_provider::log::warn!("Reference to unknown transliterator: {unparsed}");
            format!("x-{unparsed}")
        };
        ds::SimpleId {
            id: string.into(),
            filter: id.filter.unwrap_or(CodePointInversionList::all()),
        }
    }
}