forked from jerryscript-project/jerryscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-unicode.py
executable file
·803 lines (605 loc) · 30.7 KB
/
gen-unicode.py
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
#!/usr/bin/env python
# Copyright JS Foundation and other contributors, http://js.foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import argparse
import csv
import itertools
import os
import re
import warnings
from gen_c_source import LICENSE, format_code
from settings import PROJECT_DIR
UNICODE_DATA_FILE = 'UnicodeData.txt'
SPECIAL_CASING_FILE = 'SpecialCasing.txt'
DERIVED_PROPS_FILE = 'DerivedCoreProperties.txt'
PROP_LIST_FILE = 'PropList.txt'
CASE_FOLDING_FILE = 'CaseFolding.txt'
RANGES_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-ranges.inc.h')
RANGES_SUP_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-ranges-sup.inc.h')
CONVERSIONS_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-conversions.inc.h')
CONVERSIONS_SUP_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-conversions-sup.inc.h')
FOLDING_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-folding.inc.h')
FOLDING_SUP_C_SOURCE = os.path.join(PROJECT_DIR, 'jerry-core/lit/lit-unicode-folding-sup.inc.h')
UNICODE_PLANE_TYPE_BASIC = 0
UNICODE_PLANE_TYPE_SUPPLEMENTARY = 1
# common code generation
class UnicodeBasicSource(object):
# pylint: disable=too-many-instance-attributes
def __init__(self, filepath, character_type="uint16_t", length_type="uint8_t"):
self._filepath = filepath
self._header = [LICENSE, ""]
self._data = []
self._table_name_suffix = ""
self.character_type = character_type
self.length_type = length_type
self._range_table_types = [self.character_type,
self.length_type,
self.character_type]
self._range_table_names = ["interval_starts",
"interval_lengths",
"chars"]
self._range_table_descriptions = ["Character interval starting points for",
"Character interval lengths for",
"Non-interval characters for"]
self._conversion_range_types = [self.character_type,
self.length_type]
self._conversion_range_names = ["ranges",
"range_lengths"]
def complete_header(self, completion):
self._header.append(completion)
self._header.append("") # for an extra empty line
def add_whitepace_range(self, category, categorizer, units):
self.add_range(category, categorizer.create_tables(units))
def add_range(self, category, tables):
idx = 0
for table in tables:
self.add_table(table,
"/**\n * %s %s.\n */" % (self._range_table_descriptions[idx], category),
self._range_table_types[idx],
category,
self._range_table_names[idx])
idx += 1
def add_conversion_range(self, category, tables, descriptions):
self.add_named_conversion_range(category, tables, self._conversion_range_names, descriptions)
def add_named_conversion_range(self, category, tables, table_names, descriptions):
idx = 0
for table in tables:
self.add_table(table,
descriptions[idx],
self._conversion_range_types[idx],
category,
table_names[idx])
idx += 1
def add_table(self, table, description, table_type, category, table_name):
if table and sum(table) != 0:
self._data.append(description)
self._data.append("static const %s lit_unicode_%s%s%s[] JERRY_ATTR_CONST_DATA ="
% (table_type,
category.lower(),
"_" + table_name if table_name else "",
self._table_name_suffix))
self._data.append("{")
self._data.append(format_code(table, 1, 6 if self._table_name_suffix else 4))
self._data.append("};")
self._data.append("") # for an extra empty line
def generate(self):
with open(self._filepath, 'w') as generated_source:
generated_source.write("\n".join(self._header))
generated_source.write("\n".join(self._data))
class UnicodeSupplementarySource(UnicodeBasicSource):
def __init__(self, filepath):
UnicodeBasicSource.__init__(self, filepath, "uint32_t", "uint16_t")
self._table_name_suffix = "_sup"
def add_whitepace_range(self, category, categorizer, units):
self.add_range(category, categorizer.create_tables(units))
class UnicodeBasicCategorizer(object):
def __init__(self):
self._length_limit = 0xff
self.extra_id_continue_units = set([0x200C, 0x200D])
#pylint: disable=no-self-use
def in_range(self, i):
return i >= 0x80 and i < 0x10000
def _group_ranges(self, units):
"""
Convert an increasing list of integers into a range list
:return: List of ranges.
"""
for _, group in itertools.groupby(enumerate(units), lambda q: (q[1] - q[0])):
group = list(group)
yield group[0][1], group[-1][1]
def create_tables(self, units):
"""
Split list of ranges into intervals and single char lists.
:return: A tuple containing the following info:
- list of interval starting points
- list of interval lengths
- list of single chars
"""
interval_sps = []
interval_lengths = []
chars = []
for element in self._group_ranges(units):
interval_length = element[1] - element[0]
if interval_length == 0:
chars.append(element[0])
elif interval_length > self._length_limit:
for i in range(element[0], element[1], self._length_limit + 1):
length = min(self._length_limit, element[1] - i)
interval_sps.append(i)
interval_lengths.append(length)
else:
interval_sps.append(element[0])
interval_lengths.append(interval_length)
return interval_sps, interval_lengths, chars
def read_units(self, file_path, categories, subcategories=None):
"""
Read the Unicode Derived Core Properties file and extract the ranges
for the given categories.
:param file_path: Path to the Unicode "DerivedCoreProperties.txt" file.
:param categories: A list of category strings to extract from the Unicode file.
:param subcategories: A list of subcategory strings to restrict categories.
:return: A dictionary each string from the :param categories: is a key and for each
key list of code points are stored.
"""
# Create a dictionary in the format: { category[0]: [ ], ..., category[N]: [ ] }
units = {}
for category in categories:
units[category] = []
# Formats to match:
# <HEX> ; <category> #
# <HEX>..<HEX> ; <category> # <subcategory>
matcher = r"(?P<start>[\dA-F]+)(?:\.\.(?P<end>[\dA-F]+))?\s+; (?P<category>[\w]+) # (?P<subcategory>[\w&]{2})"
with open(file_path, "r") as src_file:
for line in src_file:
match = re.match(matcher, line)
if (match
and match.group("category") in categories
and (not subcategories or match.group("subcategory") in subcategories)):
start = int(match.group("start"), 16)
# if no "end" found use the "start"
end = int(match.group("end") or match.group("start"), 16)
matching_code_points = [
code_point for code_point in range(start, end + 1) if self.in_range(code_point)
]
units[match.group("category")].extend(matching_code_points)
return units
def read_case_mappings(self, unicode_data_file, special_casing_file):
"""
Read the corresponding unicode values of lower and upper case letters and store these in tables.
:param unicode_data_file: Contains the default case mappings (one-to-one mappings).
:param special_casing_file: Contains additional informative case mappings that are either not one-to-one
or which are context-sensitive.
:return: Upper and lower case mappings.
"""
lower_case_mapping = {}
upper_case_mapping = {}
# Add one-to-one mappings
with open(unicode_data_file) as unicode_data:
reader = csv.reader(unicode_data, delimiter=';')
for line in reader:
letter_id = int(line[0], 16)
if not self.in_range(letter_id):
continue
capital_letter = line[12]
small_letter = line[13]
if capital_letter:
upper_case_mapping[letter_id] = parse_unicode_sequence(capital_letter)
if small_letter:
lower_case_mapping[letter_id] = parse_unicode_sequence(small_letter)
# Update the conversion tables with the special cases
with open(special_casing_file) as special_casing:
reader = csv.reader(special_casing, delimiter=';')
for line in reader:
# Skip comment sections and empty lines
if not line or line[0].startswith('#'):
continue
# Replace '#' character with empty string
for idx, fragment in enumerate(line):
if fragment.find('#') >= 0:
line[idx] = ''
letter_id = int(line[0], 16)
condition_list = line[4]
if not self.in_range(letter_id) or condition_list:
continue
original_letter = parse_unicode_sequence(line[0])
small_letter = parse_unicode_sequence(line[1])
capital_letter = parse_unicode_sequence(line[3])
if small_letter != original_letter:
lower_case_mapping[letter_id] = small_letter
if capital_letter != original_letter:
upper_case_mapping[letter_id] = capital_letter
return lower_case_mapping, upper_case_mapping
class UnicodeSupplementaryCategorizer(UnicodeBasicCategorizer):
def __init__(self):
UnicodeBasicCategorizer.__init__(self)
self._length_limit = 0xffff
self.extra_id_continue_units = set()
def in_range(self, i):
return i >= 0x10000
def generate_ranges(script_args, plane_type):
if plane_type == UNICODE_PLANE_TYPE_SUPPLEMENTARY:
c_source = UnicodeSupplementarySource(RANGES_SUP_C_SOURCE)
categorizer = UnicodeSupplementaryCategorizer()
else:
c_source = UnicodeBasicSource(RANGES_C_SOURCE)
categorizer = UnicodeBasicCategorizer()
header_completion = ["/* This file is automatically generated by the %s script" % os.path.basename(__file__),
" * from %s. Do not edit! */" % (DERIVED_PROPS_FILE),
""]
c_source.complete_header("\n".join(header_completion))
derived_props_path = os.path.join(script_args.unicode_dir, DERIVED_PROPS_FILE)
units = categorizer.read_units(derived_props_path, ["ID_Start", "ID_Continue"])
units["ID_Continue"] = sorted(set(units["ID_Continue"]).union(categorizer.extra_id_continue_units)
- set(units["ID_Start"]))
for category, unit in units.items():
c_source.add_range(category, categorizer.create_tables(unit))
prop_list_path = os.path.join(script_args.unicode_dir, PROP_LIST_FILE)
white_space_units = categorizer.read_units(prop_list_path, ["White_Space"], ["Zs"])["White_Space"]
c_source.add_whitepace_range("White_Space", categorizer, white_space_units)
c_source.generate()
# functions for unicode conversions
def make_char(hex_val):
"""
Create a unicode character from a hex value
:param hex_val: Hex value of the character.
:return: Unicode character corresponding to the value.
"""
try:
return unichr(hex_val)
except NameError:
return chr(hex_val)
def parse_unicode_sequence(raw_data):
"""
Parse unicode sequence from raw data.
:param raw_data: Contains the unicode sequence which needs to parse.
:return: The parsed unicode sequence.
"""
result = ''
for unicode_char in raw_data.split(' '):
if unicode_char == '':
continue
# Convert it to unicode code point (from hex value without 0x prefix)
hex_val = int(unicode_char, 16)
result += make_char(hex_val)
return result
def extract_ranges(letter_case, reverse_letter_case=None):
"""
Extract ranges from case mappings
(the second param is optional, if it's not empty, a range will contains bidirectional conversions only).
:param letter_id: An integer, representing the unicode code point of the character.
:param letter_case: case mappings dictionary which contains the conversions.
:param reverse_letter_case: Comparable case mapping table which contains the return direction of the conversion.
:return: A table with the start points and their mapped value, and another table with the lengths of the ranges.
"""
in_range = False
range_position = -1
ranges = []
range_lengths = []
for letter_id in sorted(letter_case.keys()):
prev_letter_id = letter_id - 1
# One-way conversions
if reverse_letter_case is None:
if len(letter_case[letter_id]) > 1:
in_range = False
continue
if prev_letter_id not in letter_case or len(letter_case[prev_letter_id]) > 1:
in_range = False
continue
# Two way conversions
else:
if not is_bidirectional_conversion(letter_id, letter_case, reverse_letter_case):
in_range = False
continue
if not is_bidirectional_conversion(prev_letter_id, letter_case, reverse_letter_case):
in_range = False
continue
conv_distance = calculate_conversion_distance(letter_case, letter_id)
prev_conv_distance = calculate_conversion_distance(letter_case, prev_letter_id)
if conv_distance != prev_conv_distance:
in_range = False
continue
if in_range:
range_lengths[range_position] += 1
else:
in_range = True
range_position += 1
# Add the start point of the range and its mapped value
ranges.extend([prev_letter_id, ord(letter_case[prev_letter_id])])
range_lengths.append(2)
# Remove all ranges from the case mapping table.
for idx in range(0, len(ranges), 2):
range_length = range_lengths[idx // 2]
for incr in range(range_length):
del letter_case[ranges[idx] + incr]
if reverse_letter_case is not None:
del reverse_letter_case[ranges[idx + 1] + incr]
return ranges, range_lengths
def extract_character_pair_ranges(letter_case, reverse_letter_case):
"""
Extract two or more character pairs from the case mapping tables.
:param letter_case: case mappings dictionary which contains the conversions.
:param reverse_letter_case: Comparable case mapping table which contains the return direction of the conversion.
:return: A table with the start points, and another table with the lengths of the ranges.
"""
start_points = []
lengths = []
in_range = False
element_counter = -1
for letter_id in sorted(letter_case.keys()):
# Only extract character pairs
if not is_bidirectional_conversion(letter_id, letter_case, reverse_letter_case):
in_range = False
continue
if ord(letter_case[letter_id]) == letter_id + 1:
prev_letter_id = letter_id - 2
if not is_bidirectional_conversion(prev_letter_id, letter_case, reverse_letter_case):
in_range = False
if in_range:
lengths[element_counter] += 2
else:
element_counter += 1
start_points.append(letter_id)
lengths.append(2)
in_range = True
else:
in_range = False
# Remove all found case mapping from the conversion tables after the scanning method
for idx, letter_id in enumerate(start_points):
conv_length = lengths[idx]
for incr in range(0, conv_length, 2):
del letter_case[letter_id + incr]
del reverse_letter_case[letter_id + 1 + incr]
return start_points, lengths
def extract_character_pairs(letter_case, reverse_letter_case):
"""
Extract character pairs. Check that two unicode value are also a mapping value of each other.
:param letter_case: case mappings dictionary which contains the conversions.
:param reverse_letter_case: Comparable case mapping table which contains the return direction of the conversion.
:return: A table with character pairs.
"""
character_pairs = []
for letter_id in sorted(letter_case.keys()):
if is_bidirectional_conversion(letter_id, letter_case, reverse_letter_case):
mapped_value = letter_case[letter_id]
character_pairs.extend([letter_id, ord(mapped_value)])
# Remove character pairs from case mapping tables
del letter_case[letter_id]
del reverse_letter_case[ord(mapped_value)]
return character_pairs
def extract_special_ranges(letter_case):
"""
Extract special ranges. It contains start points of one-to-two letter case ranges
where the second character is always the same.
:param letter_case: case mappings dictionary which contains the conversions.
:return: A table with the start points and their mapped values, and a table with the lengths of the ranges.
"""
special_ranges = []
special_range_lengths = []
range_position = -1
for letter_id in sorted(letter_case.keys()):
mapped_value = letter_case[letter_id]
if len(mapped_value) != 2:
continue
prev_letter_id = letter_id - 1
if prev_letter_id not in letter_case:
in_range = False
continue
prev_mapped_value = letter_case[prev_letter_id]
if len(prev_mapped_value) != 2:
continue
if prev_mapped_value[1] != mapped_value[1]:
continue
if (ord(prev_mapped_value[0]) - prev_letter_id) != (ord(mapped_value[0]) - letter_id):
in_range = False
continue
if in_range:
special_range_lengths[range_position] += 1
else:
range_position += 1
in_range = True
special_ranges.extend([prev_letter_id, ord(prev_mapped_value[0]), ord(prev_mapped_value[1])])
special_range_lengths.append(1)
# Remove special ranges from the conversion table
for idx in range(0, len(special_ranges), 3):
range_length = special_range_lengths[idx // 3]
letter_id = special_ranges[idx]
for incr in range(range_length):
del letter_case[special_ranges[idx] + incr]
return special_ranges, special_range_lengths
def extract_conversions(letter_case):
"""
Extract conversions. It provide the full (or remained) case mappings from the table.
The counter table contains the information of how much one-to-one, one-to-two or one-to-three mappings
exists successively in the conversion table.
:return: A table with conversions, and a table with counters.
"""
unicodes = [[], [], []]
unicode_lengths = [0, 0, 0]
# 1 to 1 byte
for letter_id in sorted(letter_case.keys()):
mapped_value = letter_case[letter_id]
if len(mapped_value) != 1:
continue
unicodes[0].extend([letter_id, ord(mapped_value)])
del letter_case[letter_id]
# 1 to 2 bytes
for letter_id in sorted(letter_case.keys()):
mapped_value = letter_case[letter_id]
if len(mapped_value) != 2:
continue
unicodes[1].extend([letter_id, ord(mapped_value[0]), ord(mapped_value[1])])
del letter_case[letter_id]
# 1 to 3 bytes
for letter_id in sorted(letter_case.keys()):
mapped_value = letter_case[letter_id]
if len(mapped_value) != 3:
continue
unicodes[2].extend([letter_id, ord(mapped_value[0]), ord(mapped_value[1]), ord(mapped_value[2])])
del letter_case[letter_id]
unicode_lengths = [int(len(unicodes[0]) / 2), int(len(unicodes[1]) / 3), int(len(unicodes[2]) / 4)]
return list(itertools.chain.from_iterable(unicodes)), unicode_lengths
def is_bidirectional_conversion(letter_id, letter_case, reverse_letter_case):
"""
Check that two unicode value are also a mapping value of each other.
:param letter_id: An integer, representing the unicode code point of the character.
:param other_case_mapping: Comparable case mapping table which possible contains
the return direction of the conversion.
:return: True, if it's a reverible conversion, false otherwise.
"""
if letter_id not in letter_case:
return False
# Check one-to-one mapping
mapped_value = letter_case[letter_id]
if len(mapped_value) > 1:
return False
# Check two way conversions
mapped_value_id = ord(mapped_value)
if mapped_value_id not in reverse_letter_case or len(reverse_letter_case[mapped_value_id]) > 1:
return False
if ord(reverse_letter_case[mapped_value_id]) != letter_id:
return False
return True
def calculate_conversion_distance(letter_case, letter_id):
"""
Calculate the distance between the unicode character and its mapped value
(only needs and works with one-to-one mappings).
:param letter_case: case mappings dictionary which contains the conversions.
:param letter_id: An integer, representing the unicode code point of the character.
:return: The conversion distance.
"""
if letter_id not in letter_case or len(letter_case[letter_id]) > 1:
return None
return ord(letter_case[letter_id]) - letter_id
def generate_conversions(script_args, plane_type):
if plane_type == UNICODE_PLANE_TYPE_SUPPLEMENTARY:
c_source = UnicodeSupplementarySource(CONVERSIONS_SUP_C_SOURCE)
categorizer = UnicodeSupplementaryCategorizer()
else:
c_source = UnicodeBasicSource(CONVERSIONS_C_SOURCE)
categorizer = UnicodeBasicCategorizer()
header_completion = ["/* This file is automatically generated by the %s script" % os.path.basename(__file__),
" * from %s and %s files. Do not edit! */" % (UNICODE_DATA_FILE, SPECIAL_CASING_FILE),
""]
c_source.complete_header("\n".join(header_completion))
unicode_data_path = os.path.join(script_args.unicode_dir, UNICODE_DATA_FILE)
special_casing_path = os.path.join(script_args.unicode_dir, SPECIAL_CASING_FILE)
# Read the corresponding unicode values of lower and upper case letters and store these in tables
lower_case, upper_case = categorizer.read_case_mappings(unicode_data_path, special_casing_path)
c_source.add_conversion_range("character_case",
extract_ranges(lower_case, upper_case),
[("/* Contains start points of character case ranges "
"(these are bidirectional conversions). */"),
"/* Interval lengths of start points in `character_case_ranges` table. */"])
c_source.add_conversion_range("character_pair",
extract_character_pair_ranges(lower_case, upper_case),
["/* Contains the start points of bidirectional conversion ranges. */",
"/* Interval lengths of start points in `character_pair_ranges` table. */"])
c_source.add_table(extract_character_pairs(lower_case, upper_case),
"/* Contains lower/upper case bidirectional conversion pairs. */",
c_source.character_type,
"character_pairs",
"")
c_source.add_conversion_range("upper_case_special",
extract_special_ranges(upper_case),
[("/* Contains start points of one-to-two uppercase ranges where the "
"second character\n"
" * is always the same.\n"
" */"),
"/* Interval lengths for start points in `upper_case_special_ranges` table. */"])
c_source.add_conversion_range("lower_case",
extract_ranges(lower_case),
["/* Contains start points of lowercase ranges. */",
"/* Interval lengths for start points in `lower_case_ranges` table. */"])
c_source.add_named_conversion_range("lower_case",
extract_conversions(lower_case),
["conversions", "conversion_counters"],
[("/* The remaining lowercase conversions. The lowercase variant can "
"be one-to-three character long. */"),
("/* Number of one-to-one, one-to-two, and one-to-three lowercase "
"conversions. */")])
c_source.add_named_conversion_range("upper_case",
extract_conversions(upper_case),
["conversions", "conversion_counters"],
[("/* The remaining uppercase conversions. The uppercase variant can "
"be one-to-three character long. */"),
("/* Number of one-to-one, one-to-two, and one-to-three uppercase "
"conversions. */")])
if lower_case:
warnings.warn('Not all elements extracted from the lowercase table!')
if upper_case:
warnings.warn('Not all elements extracted from the uppercase table!')
c_source.generate()
def generate_folding(script_args, plane_type):
if plane_type == UNICODE_PLANE_TYPE_SUPPLEMENTARY:
c_source = UnicodeSupplementarySource(FOLDING_SUP_C_SOURCE)
categorizer = UnicodeSupplementaryCategorizer()
else:
c_source = UnicodeBasicSource(FOLDING_C_SOURCE)
categorizer = UnicodeBasicCategorizer()
header_completion = ["/* This file is automatically generated by the %s script" % os.path.basename(__file__),
" * from the %s file. Do not edit! */" % (CASE_FOLDING_FILE),
""]
c_source.complete_header("\n".join(header_completion))
unicode_data_path = os.path.join(script_args.unicode_dir, UNICODE_DATA_FILE)
special_casing_path = os.path.join(script_args.unicode_dir, SPECIAL_CASING_FILE)
case_folding_path = os.path.join(script_args.unicode_dir, CASE_FOLDING_FILE)
# Read the corresponding unicode values of lower and upper case letters and store these in tables
lower_case, upper_case = categorizer.read_case_mappings(unicode_data_path, special_casing_path)
folding = {}
with open(case_folding_path, 'r') as case_folding:
case_folding_re = re.compile(r'(?P<code_point>[^;]*);\s*(?P<type>[^;]*);\s*(?P<folding>[^;]*);')
for line in case_folding:
match = case_folding_re.match(line)
if match and match.group('type') in ('S', 'C'):
code_point = int(match.group('code_point'), 16)
if categorizer.in_range(code_point):
folding[code_point] = parse_unicode_sequence(match.group('folding'))
should_to_upper = []
should_skip_to_lower = []
for code_point in lower_case:
if code_point not in folding:
should_skip_to_lower.append(code_point)
for code_point, folded in folding.items():
if lower_case.get(code_point, make_char(code_point)) != folded:
should_to_upper.append(code_point)
if upper_case.get(code_point, '') == folded:
should_skip_to_lower.append(code_point)
c_source.add_range('folding_skip_to_lower', categorizer.create_tables(should_skip_to_lower))
c_source.add_range('folding_to_upper', categorizer.create_tables(should_to_upper))
c_source.generate()
# entry point
def main():
parser = argparse.ArgumentParser(description='lit-unicode-{conversions,ranges}-{sup}.inc.h generator',
epilog='''
The input data must be retrieved from
http://www.unicode.org/Public/<VERSION>/ucd/UCD.zip.
The last known good version is 13.0.0.
''')
def check_dir(path):
if not os.path.isdir(path) or not os.access(path, os.R_OK):
raise argparse.ArgumentTypeError('The %s directory does not exist or is not readable!' % path)
return path
parser.add_argument('--unicode-dir', metavar='DIR', action='store', required=True,
type=check_dir, help='specify the unicode data directory')
script_args = parser.parse_args()
generate_ranges(script_args, UNICODE_PLANE_TYPE_BASIC)
generate_ranges(script_args, UNICODE_PLANE_TYPE_SUPPLEMENTARY)
generate_conversions(script_args, UNICODE_PLANE_TYPE_BASIC)
generate_conversions(script_args, UNICODE_PLANE_TYPE_SUPPLEMENTARY)
generate_folding(script_args, UNICODE_PLANE_TYPE_BASIC)
# There are currently no code points in the supplementary planes that require special folding
# generate_folding(script_args, UNICODE_PLANE_TYPE_SUPPLEMENTARY)
if __name__ == "__main__":
main()