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
use crate::common;
use crate::error::Error;
use crate::file_writer;
use crate::generators::CodeGen;
use crate::ucd_parsers;
use std::collections::HashSet;
use std::fs::File;
use std::path::{Path, PathBuf};
use ucd_parse::Codepoints;
use ucd_parse::CoreProperty;
use ucd_parse::Property;
use ucd_parse::Script;
use ucd_parse::UnicodeDataDecompositionTag;
use ucd_parsers::DerivedJoiningType;
use ucd_parsers::HangulSyllableType;

fn parse_unicode_file<U: ucd_parse::UcdFile, F>(path: &Path, mut f: F) -> Result<(), Error>
where
    F: FnMut(&U) -> Result<(), Error>,
{
    let lines: Vec<U> = ucd_parse::parse(path)?;
    for line in lines.iter() {
        f(line)?;
    }
    Ok(())
}

/// Generator that aggregates other [`UcdCodeGen`] elements.
pub struct UcdFileGen {
    ucd_path: PathBuf,
    generators: Vec<Box<dyn UcdCodeGen>>,
}

impl UcdFileGen {
    /// Creates a new `UcdFileGen` element.
    /// # Arguments:
    /// `path` - path where `UCD` files are stored
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        let path = path.as_ref();
        Self {
            ucd_path: path.to_path_buf(),
            generators: Vec::new(),
        }
    }

    /// Adds a [`UcdCodeGen`] element.
    pub fn add(&mut self, gen: Box<dyn UcdCodeGen>) {
        self.generators.push(gen);
    }
}

impl CodeGen for UcdFileGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        let it = self.generators.iter_mut();
        for gen in it {
            gen.parse_unicode_file(&self.ucd_path)?;
            gen.generate_code(file)?;
        }
        Ok(())
    }
}

/// Trait implemented by all elements that are able to parse `UCD` files.
pub trait UcdCodeGen: CodeGen {
    /// Parses a `UCD` file.
    /// # Arguments:
    /// `ucd_path` - Path where `UCD` file is stored.
    fn parse_unicode_file(&mut self, ucd_path: &Path) -> Result<(), Error>;
}

/// Generic trait used by parsers to generate code.
pub trait UcdLineParser<U>: CodeGen {
    /// Process an entry in the `UCD` file.
    /// # Argument:
    /// `line` - Represents a line in the `UCD` file.
    fn process_entry(&mut self, line: &U) -> Result<(), Error>;
}

/// Generator that crates tables of Unicode code points as a result
/// of parsing properties in the `UCD` files.
pub struct UcdTableGen {
    name: String,
    table_name: String,
    cps: HashSet<u32>,
}

impl UcdTableGen {
    /// Creates a new [`UcdTableGen`]
    pub fn new(name: &str, table_name: &str) -> Self {
        Self {
            name: String::from(name),
            table_name: String::from(table_name),
            cps: HashSet::new(),
        }
    }
}

impl CodeGen for UcdTableGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        file_writer::generate_code_from_hashset(file, &self.table_name, &self.cps)
    }
}

impl UcdLineParser<ucd_parsers::UnicodeData> for UcdTableGen {
    fn process_entry(&mut self, udata: &ucd_parsers::UnicodeData) -> Result<(), Error> {
        if self.name == udata.general_category {
            match udata.codepoints {
                Codepoints::Single(ref cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(ref r) => common::insert_codepoint_range(r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

impl UcdLineParser<HangulSyllableType> for UcdTableGen {
    fn process_entry(&mut self, line: &HangulSyllableType) -> Result<(), Error> {
        if self.name == line.prop.property {
            match line.prop.codepoints {
                Codepoints::Single(cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(r) => common::insert_codepoint_range(&r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

impl UcdLineParser<Property> for UcdTableGen {
    fn process_entry(&mut self, line: &Property) -> Result<(), Error> {
        if self.name == line.property {
            match line.codepoints {
                Codepoints::Single(cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(r) => common::insert_codepoint_range(&r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

impl UcdLineParser<CoreProperty> for UcdTableGen {
    fn process_entry(&mut self, line: &CoreProperty) -> Result<(), Error> {
        if self.name == line.property {
            match line.codepoints {
                Codepoints::Single(cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(r) => common::insert_codepoint_range(&r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

impl UcdLineParser<Script> for UcdTableGen {
    fn process_entry(&mut self, line: &Script) -> Result<(), Error> {
        if self.name == line.script {
            match line.codepoints {
                Codepoints::Single(ref cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(ref r) => common::insert_codepoint_range(r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

impl UcdLineParser<DerivedJoiningType> for UcdTableGen {
    fn process_entry(&mut self, line: &DerivedJoiningType) -> Result<(), Error> {
        if self.name == line.prop.property {
            match line.prop.codepoints {
                Codepoints::Single(ref cp) => common::insert_codepoint(cp.value(), &mut self.cps)?,
                Codepoints::Range(ref r) => common::insert_codepoint_range(r, &mut self.cps)?,
            }
        }
        Ok(())
    }
}

/// Aggregator of elements that implement the [`UcdLineParser`] trait.
pub struct UnicodeGen<T: ucd_parse::UcdFile> {
    generators: Vec<Box<dyn UcdLineParser<T>>>,
}

impl<T: ucd_parse::UcdFile> UnicodeGen<T> {
    /// Creates a new Generator for `UCD` files
    pub fn new() -> Self {
        Self {
            generators: Vec::new(),
        }
    }

    /// Add a new `UCD` line parser
    pub fn add(&mut self, gen: Box<dyn UcdLineParser<T>>) {
        self.generators.push(gen);
    }
}

impl<T: ucd_parse::UcdFile> Default for UnicodeGen<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: ucd_parse::UcdFile> UcdCodeGen for UnicodeGen<T> {
    fn parse_unicode_file(&mut self, ucd_path: &Path) -> Result<(), Error> {
        parse_unicode_file(ucd_path, |line: &T| {
            let it = self.generators.iter_mut();
            for gen in it {
                gen.process_entry(line)?;
            }
            Ok(())
        })
    }
}

impl<T: ucd_parse::UcdFile> CodeGen for UnicodeGen<T> {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        let it = self.generators.iter_mut();
        for gen in it {
            gen.generate_code(file)?;
        }
        Ok(())
    }
}

/// Generator that aggregates elements that are able to generate tables
/// from the [`UnicodeData`](http://www.unicode.org/reports/tr44/#UnicodeData.txt) file
pub struct GeneralCategoryGen {
    generators: Vec<Box<dyn UcdLineParser<ucd_parsers::UnicodeData>>>,
}

impl GeneralCategoryGen {
    /// Creates a new `GeneralCategoryGen` element.
    pub fn new() -> Self {
        Self {
            generators: Vec::new(),
        }
    }

    /// Add a new `UCD` line parser
    pub fn add(&mut self, gen: Box<dyn UcdLineParser<ucd_parsers::UnicodeData>>) {
        self.generators.push(gen);
    }
}

impl Default for GeneralCategoryGen {
    fn default() -> Self {
        Self::new()
    }
}

impl UcdCodeGen for GeneralCategoryGen {
    fn parse_unicode_file(&mut self, ucd_path: &Path) -> Result<(), Error> {
        let cps: Vec<ucd_parsers::UnicodeData> = ucd_parsers::UnicodeData::parse(ucd_path)?;
        for udata in cps.iter() {
            let it = self.generators.iter_mut();
            for gen in it {
                gen.process_entry(udata)?;
            }
        }
        Ok(())
    }
}

impl CodeGen for GeneralCategoryGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        let it = self.generators.iter_mut();
        for gen in it {
            gen.generate_code(file)?;
        }
        Ok(())
    }
}

const CANONICAL_COMBINING_CLASS_VIRAMA: u8 = 9;

/// Generator that creates a table of Unicode code points
/// with the `Virama` canonical combining class.
pub struct ViramaTableGen {
    table_name: String,
    cps: HashSet<u32>,
}

impl ViramaTableGen {
    /// Creates a new table generator for code points with the `Virama` combining class
    pub fn new(table_name: &str) -> Self {
        Self {
            table_name: String::from(table_name),
            cps: HashSet::new(),
        }
    }
}

impl CodeGen for ViramaTableGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        file_writer::generate_code_from_hashset(file, &self.table_name, &self.cps)
    }
}

impl UcdLineParser<ucd_parsers::UnicodeData> for ViramaTableGen {
    fn process_entry(&mut self, udata: &ucd_parsers::UnicodeData) -> Result<(), Error> {
        match udata.codepoints {
            Codepoints::Range(ref r) => {
                if udata.canonical_combining_class == CANONICAL_COMBINING_CLASS_VIRAMA {
                    common::insert_codepoint_range(r, &mut self.cps)?;
                }
            }
            Codepoints::Single(ref cp) => {
                if udata.canonical_combining_class == CANONICAL_COMBINING_CLASS_VIRAMA {
                    common::insert_codepoint(cp.value(), &mut self.cps)?;
                }
            }
        }
        Ok(())
    }
}

/// Generator that creates a table of Unicode code points
/// and their decomposition mappings.
pub struct WidthMappingTableGen {
    name: String,
    vec: Vec<(Codepoints, ucd_parse::Codepoint)>,
}

impl WidthMappingTableGen {
    /// Creates a new width mapping table generator
    pub fn new(name: &str) -> Self {
        Self {
            name: String::from(name),
            vec: Vec::new(),
        }
    }
}

impl UcdLineParser<ucd_parsers::UnicodeData> for WidthMappingTableGen {
    fn process_entry(&mut self, udata: &ucd_parsers::UnicodeData) -> Result<(), Error> {
        if udata.decomposition.len == 0 {
            return err!("No decomposition mappings");
        }

        if let Some(tag) = &udata.decomposition.tag {
            if *tag == UnicodeDataDecompositionTag::Wide
                || *tag == UnicodeDataDecompositionTag::Narrow
            {
                self.vec
                    .push((udata.codepoints, udata.decomposition.mapping[0]));
            }
        }
        Ok(())
    }
}

impl CodeGen for WidthMappingTableGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        file_writer::generate_width_mapping_vector(file, &self.name, &self.vec)
    }
}

/// Generator that creates a table of unassigned Unicode code points
pub struct UnassignedTableGen {
    name: String,
    range: ucd_parse::CodepointRange,
    vec: Vec<Codepoints>,
}

impl UnassignedTableGen {
    /// Creates a new table generator for unassigned code points
    pub fn new(table_name: &str) -> Self {
        Self {
            name: String::from(table_name),
            range: ucd_parse::CodepointRange {
                start: ucd_parse::Codepoint::from_u32(0).unwrap(),
                end: ucd_parse::Codepoint::from_u32(0).unwrap(),
            },
            vec: Vec::new(),
        }
    }
}

impl UcdLineParser<ucd_parsers::UnicodeData> for UnassignedTableGen {
    fn process_entry(&mut self, udata: &ucd_parsers::UnicodeData) -> Result<(), Error> {
        match udata.codepoints {
            Codepoints::Range(ref r) => {
                if r.start.value() - self.range.end.value() > 0 {
                    self.range.end = ucd_parse::Codepoint::from_u32(r.start.value() - 1)?;
                    common::add_codepoints(&self.range, &mut self.vec);
                }
                self.range.start = ucd_parse::Codepoint::from_u32(r.end.value() + 1)?;
                self.range.end = r.start;
            }
            Codepoints::Single(ref cp) => {
                let next_cp = ucd_parse::Codepoint::from_u32(cp.value() + 1)?;
                if cp.value() - self.range.end.value() != 0 {
                    self.range.end = ucd_parse::Codepoint::from_u32(cp.value() - 1)?;
                    common::add_codepoints(&self.range, &mut self.vec);
                }

                self.range.start = next_cp;
                self.range.end = next_cp;
            }
        }
        Ok(())
    }
}

impl CodeGen for UnassignedTableGen {
    fn generate_code(&mut self, file: &mut File) -> Result<(), Error> {
        file_writer::generate_code_from_vec(file, &self.name, &self.vec)
    }
}