4 # mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl)
5 # from the Unicode database files (lib/unicore/*.txt).
10 my $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1.
17 my ($table, $last) = @_;
19 $table->[-1]->[1] = $last;
23 my ($table, $code, $name) = @_;
25 hex($table->[-1]->[1]) == hex($code) - 1 &&
26 (!defined $name || $table->[-1]->[2] eq $name)) {
27 extend($table, $code);
29 push @$table, [$code, $code, $name];
37 if ($table->[0]->[0]) {
38 $last = hex($table->[0]->[0]);
39 push @$inverse, [ "0000",
40 sprintf("%04X", $last - 1) ];
42 for my $i (0..$#$table-1) {
43 $first = defined $table->[$i ]->[1] ?
44 hex($table->[$i ]->[1]) : 0;
45 $last = defined $table->[$i + 1]->[0] ?
46 hex($table->[$i + 1]->[0]) : $first;
47 push @$inverse, [ sprintf("%04X", $first + 1),
48 sprintf("%04X", $last - 1) ]
49 unless $first + 1 == $last;
58 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
59 # This file is built by $0 from e.g. Unicode.txt.
60 # Any changes made here will be lost!
81 my ($table, $file) = @_;
83 if (open(my $fh, ">$file")) {
87 print $fh $i->[0], "\t",
88 $i->[1] ne $i->[0] ? $i->[1] : "", "\t",
89 defined $i->[2] ? $i->[2] : "", "\n";
94 die "$0: $file: $!\n";
99 # The %In contains the mapping of the script/block name into a number.
107 # Read in the Unicode.txt, the main Unicode database.
114 if (open(my $Unicode, "Unicode.txt")) {
126 next unless /^[0-9A-Fa-f]+;/;
129 my ($code, $name, $cat, $comb, $bidi, $deco,
130 $decimal, $digit, $number,
131 $mirrored, $unicode10, $comment,
132 $upper, $lower, $title) = split(/\s*;\s*/);
134 if ($name =~ /^<(.+), (First|Last)>$/) {
137 append($General{$name} ||= [], $code, $name);
139 extend($General{$name} , $code);
141 unless (defined $In{$name}) {
142 $In{$name} = $InId++;
143 $InIn{$name} = $General{$name};
145 append($Cat{$cat} ||= [], $code);
146 append($Cat{substr($cat, 0, 1)}
149 append(\@Name, $code, $name);
151 append(\@General, $code, $cat);
153 append($Cat{$cat} ||= [], $code);
154 append($Cat{substr($cat, 0, 1)}
156 # 005F: SPACING UNDERSCORE
157 append($Cat{Word} ||= [], $code)
158 if $cat =~ /^[LMN]/ or $code eq "005F";
159 append($Cat{Alnum} ||= [], $code)
161 append($Cat{Alpha} ||= [], $code)
163 # 0009: HORIZONTAL TABULATION
165 # 000B: VERTICAL TABULATION
167 # 000D: CARRIAGE RETURN
169 append($Cat{Space} ||= [], $code)
171 $code =~ /^(0009|000A|000B|000C|000D)$/;
172 append($Cat{SpacePerl} ||= [], $code)
174 $code =~ /^(0009|000A|000C|000D)$/;
175 append($Cat{Blank} ||= [], $code)
176 if $code =~ /^(0020|0009)$/ ||
178 append($Cat{Digit} ||= [], $code) if $cat eq "Nd";
179 append($Cat{Upper} ||= [], $code) if $cat eq "Lu";
180 append($Cat{Lower} ||= [], $code) if $cat eq "Ll";
181 append($Cat{Title} ||= [], $code) if $cat eq "Lt";
182 append($Cat{ASCII} ||= [], $code) if $code le "007F";
183 append($Cat{Cntrl} ||= [], $code) if $cat =~ /^C/;
184 append($Cat{Graph} ||= [], $code) if $cat =~ /^([LMNPS]|Co)/;
185 append($Cat{Print} ||= [], $code) if $cat =~ /^([LMNPS]|Co|Zs)/;
186 append($Cat{Punct} ||= [], $code) if $cat =~ /^P/;
187 # 003[0-9]: DIGIT ZERO..NINE, 00[46][1-6]: A..F, a..f
188 append($Cat{XDigit} ||= [], $code)
189 if $code =~ /^00(3[0-9]|[46][1-6])$/;
191 append($To{Upper} ||= [], $code, $upper) if $upper;
192 append($To{Lower} ||= [], $code, $lower) if $lower;
193 append($To{Title} ||= [], $code, $title) if $title;
194 append($To{Digit} ||= [], $code, $decimal) if $decimal;
196 append(\@Bidi, $code, $bidi);
197 append($Bidi{$bidi} ||= [], $code);
199 append(\@Comb, $code, $comb) if $comb;
202 append(\@Deco, $code, $deco);
203 if ($deco =~/^<(\w+)>/) {
204 append($Deco{Compat} ||= [], $code);
205 append($DC{$1} ||= [], $code);
207 append($Deco{Canon} ||= [], $code);
211 append(\@Number, $code, $number) if $number;
213 append(\@Mirrored, $code) if $mirrored eq "Y";
217 flush(\@Name, "Name.pl");
219 foreach my $cat (sort keys %Cat) {
220 flush($Cat{$cat}, "Is/$cat.pl");
223 foreach my $to (sort keys %To) {
224 flush($To{$to}, "To/$to.pl");
227 flush(\@Bidi, "Bidirectional.pl");
228 foreach my $bidi (sort keys %Bidi) {
229 flush($Bidi{$bidi}, "Is/Bidi$bidi.pl");
232 flush(\@Comb, "CombiningClass.pl");
234 flush(\@Deco, "Decomposition.pl");
235 foreach my $deco (sort keys %Deco) {
236 flush($Deco{$deco}, "Is/Deco$deco.pl");
238 foreach my $dc (sort keys %DC) {
239 flush($DC{$dc}, "Is/DC$dc.pl");
242 flush(\@Number, "Number.pl");
244 flush(\@Mirrored, "Is/Mirrored.pl");
246 die "$0: Unicode.txt: $!\n";
249 # The general cateory can be written out already now.
251 flush(\@General, "Category.pl");
254 # Read in the LineBrk.txt.
257 if (open(my $LineBrk, "LineBrk.txt")) {
262 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
264 my ($first, $last, $lbrk) = ($1, $2, $3);
266 append(\@Lbrk, $first, $lbrk);
267 append($Lbrk{$lbrk} ||= [], $first);
269 extend(\@Lbrk, $last);
270 extend($Lbrk{$lbrk}, $last);
274 flush(\@Lbrk, "Lbrk.pl");
275 foreach my $lbrk (sort keys %Lbrk) {
276 flush($Lbrk{$lbrk}, "Is/Lbrk$lbrk.pl");
279 die "$0: LineBrk.txt: $!\n";
283 # Read in the ArabShap.txt.
286 if (open(my $ArabShap, "ArabShap.txt")) {
290 while (<$ArabShap>) {
291 next unless /^[0-9A-Fa-f]+;/;
294 my ($code, $name, $link, $linkgroup) = split(/\s*;\s*/);
296 append(\@ArabLink, $code, $link);
297 append(\@ArabLinkGroup, $code, $linkgroup);
300 flush(\@ArabLink, "ArabLink.pl");
301 flush(\@ArabLinkGroup, "ArabLnkGrp.pl");
303 die "$0: ArabShap.txt: $!\n";
307 # Read in the Jamo.txt.
310 if (open(my $Jamo, "Jamo.txt")) {
314 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
316 my ($code, $short) = ($1, $2);
318 append(\@Short, $code, $short);
321 flush(\@Short, "JamoShort.pl");
323 die "$0: Jamo.txt: $!\n";
327 # Read in the Scripts.txt.
332 if (open(my $Scripts, "Scripts.txt")) {
334 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
336 # Wait until all the scripts have been read since
337 # they are not listed in numeric order.
338 push @Scripts, [ hex($1), $1, $2, $3 ];
341 die "$0: Scripts.txt: $!\n";
344 # Now append the scripts properties in their code point order.
349 for my $script (sort { $a->[0] <=> $b->[0] } @Scripts) {
350 my ($code, $first, $last, $name) = @$script;
351 append($Scripts, $first, $name);
352 append($Script{$name} ||= [], $first, $name);
354 extend($Scripts, $last);
355 extend($Script{$name}, $last);
357 unless (defined $In{$name}) {
358 $In{$name} = $InId++;
359 $InIn{$name} = $Script{$name};
363 # Scripts.pl can be written out already now.
365 flush(\@Scripts, "Scripts.pl");
367 # Common is everything not explicitly assigned to a Script
369 $In{Common} = $InId++;
370 my $Common = inverse($Scripts);
371 $InIn{Common} = $Common;
374 # Read in the Blocks.txt.
380 if (open(my $Blocks, "Blocks.txt")) {
382 next unless /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
384 my ($first, $last, $name) = ($1, $2, $3);
386 # If there's a naming conflict (the script names are
387 # in uppercase), the name of the block has " Block"
389 $name = "$name Block" if defined $In{"\U$name"};
391 append(\@Blocks, $first, $name);
392 append($Blocks{$name} ||= [], $first, $name);
394 extend(\@Blocks, $last);
395 extend($Blocks{$name}, $last);
397 unless (defined $In{$name}) {
398 $In{$name} = $InId++;
399 $InIn{$name} = $Blocks{$name};
403 die "$0: Blocks.txt: $!\n";
406 # Blocks.pl can be written out already now.
408 flush(\@Blocks, "Blocks.pl");
411 # Read in the PropList.txt. It contains extended properties not
412 # listed in the Unicode.txt, such as 'Other_Alphabetic':
413 # alphabetic but not of the general category L; many modifiers
414 # belong to this extended property category: while they are not
415 # alphabets, they are alphabetic in nature.
420 if (open(my $Props, "PropList.txt")) {
422 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
424 # Wait until all the extended properties have been read since
425 # they are not listed in numeric order.
426 push @Props, [ hex($1), $1, $2, $3 ];
429 die "$0: PropList.txt: $!\n";
432 # Now append the extended properties in their code point order.
437 for my $prop (sort { $a->[0] <=> $b->[0] } @Props) {
438 my ($code, $first, $last, $name) = @$prop;
439 append($Props, $first, $name);
440 append($Prop{$name} ||= [], $first, $name);
442 extend($Props, $last);
443 extend($Prop{$name}, $last);
445 unless (defined $In{$name}) {
446 $In{$name} = $InId++;
447 $InIn{$name} = $Prop{$name};
451 # Assigned is everything not Cn aka Noncharacter_Code_Point
453 $In{Assigned} = $InId++;
454 my $Assigned = inverse($Prop{Noncharacter_Code_Point});
455 $InIn{Assigned} = $Assigned;
457 sub merge_general_and_extended {
458 my ($name, $general, $extended) = @_;
462 map { pop @{$_}; $_ }
463 sort { $a->[2] <=> $b->[2] }
464 map { [ $_->[0], $_->[1], hex($_->[0]) ] }
466 map { ref $_ ? @$_ : $_ }
467 @Cat {ref $general ? @$general : $general } :
470 map { ref $_ ? @$_ : $_ }
471 @Prop{ref $extended ? @$extended : $extended} :
474 $In{$name} = $InId++;
475 $InIn{$name} = $merged;
480 # Alphabetic is L and Other_Alphabetic.
483 merge_general_and_extended('Alphabetic', 'L', 'Other_Alphabetic');
485 # Lowercase is Ll and Other_Lowercase.
488 merge_general_and_extended('Lowercase', 'Ll', 'Other_Lowercase');
490 # Uppercase is Lu and Other_Uppercase.
493 merge_general_and_extended('Uppercase', 'Lu', 'Other_Uppercase');
495 # Math is Sm and Other_Math.
498 merge_general_and_extended('Math', 'Sm', 'Other_Math');
500 # Lampersand is Ll, Lu, and Lt.
503 merge_general_and_extended('Lampersand', [ qw(Ll Lu Lt) ]);
505 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
508 merge_general_and_extended('ID_Start', [ qw(Ll Lu Lt Lm Lo Nl) ]);
510 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
513 merge_general_and_extended('ID_Continue', [ qw(Ll Lu Lt Lm Lo Nl
521 my $Any = [ [ 0, sprintf("%04X", $LastUnicodeCodepoint) ] ];
525 # mapping() will be used to write out the In and Is virtual mappings.
529 my ($map, $name) = @_;
531 if (open(my $fh, ">$name.pl")) {
535 # The %pat will hold a hash that maps the first two
536 # lowercased letters of a class to a 'fuzzified' regular
537 # expression that points to the real mapping.
541 # But first write out the offical name to real name
542 # (the filename) mapping.
548 for my $i (sort keys %$map) {
550 # Here is the 'fuzzification': accept any space,
551 # dash, or underbar where in the official name
552 # there is space or a dash (or underbar, but
554 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
555 # The prefix length of 2 is enough spread,
556 # and besides, we have 'Yi' as an In category.
557 push @{$pat{lc(substr($i, 0, 2))}}, [ $i, $pat ];
558 print $fh "'$i' => '$map->{$i}',\n";
564 # Now write out the %pat mapping.
570 foreach my $prefix (sort keys %pat) {
571 print $fh "'$prefix' => {\n";
572 foreach my $ipat (@{$pat{$prefix}}) {
573 my ($i, $pat) = @$ipat;
574 print $fh "\t'$pat' => '$map->{$i}',\n";
584 die "$0: $name.pl: $!\n";
589 # Write out the virtual In mappings.
594 # Easy low-calorie cheat.
596 copy("In/$In{Noncharacter_Code_Point}.pl", "Is/Cn.pl");
599 # Write out the real In mappings
600 # (the In.pl written out just above has the virtual In mappings)
603 foreach my $in (sort { $In{$a} <=> $In{$b} } keys %In) {
604 flush($InIn{$in}, "In/$In{$in}.pl");
608 # The mapping from General Category long forms to short forms is
609 # currently hardwired here since no simple data file in the UCD
615 'Uppercase Letter' => 'Lu',
616 'Lowercase Letter' => 'Ll',
617 'Titlecase Letter' => 'Lt',
618 'Modifier Letter' => 'Lm',
619 'Other Letter' => 'Lo',
622 'Non-Spacing Mark' => 'Mn',
623 'Spacing Combining Mark' => 'Mc',
624 'Enclosing Mark' => 'Me',
627 'Space Separator' => 'Zs',
628 'Line Separator' => 'Zl',
629 'Paragraph Separator' => 'Zp',
632 'Decimal Digit Number' => 'Nd',
633 'Letter Number' => 'Nl',
634 'Other Number' => 'No',
636 'Punctuation' => 'P',
637 'Connector Punctuation' => 'Pc',
638 'Dash Punctuation' => 'Pd',
639 'Open Punctuation' => 'Ps',
640 'Close Punctuation' => 'Pe',
641 'Initial Punctuation' => 'Pi',
642 'Final Punctuation' => 'Pf',
643 'Other Punctuation' => 'Po',
646 'Math Symbol' => 'Sm',
647 'Currency Symbol' => 'Sc',
648 'Modifier Symbol' => 'Sk',
649 'Other Symbol' => 'So',
655 'Private Use' => 'Co',
656 'Not Assigned' => 'Cn',
658 'Other Control' => 'Cc',
659 'Other Format' => 'Cf',
660 'Other Surrogate' => 'Cs',
661 'Other Private Use' => 'Co',
662 'Other Not Assigned' => 'Cn',
666 # Write out the virtual Is mappings.
672 # Read in the special cases.
677 if (open(my $SpecCase, "SpecCase.txt")) {
678 while (<$SpecCase>) {
679 next unless /^[0-9A-Fa-f]+;/;
683 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
685 if ($condition) { # not implemented yet
686 print "# SKIPPING $_\n";
690 # Wait until all the special cases have been read since
691 # they are not listed in numeric order.
693 push @{$Case{Lower}}, [ $ix, $code, $lower ];
694 push @{$Case{Title}}, [ $ix, $code, $title ];
695 push @{$Case{Upper}}, [ $ix, $code, $upper ];
698 die "$0: SpecCase.txt: $!\n";
701 # Now write out the special cases properties in their code point order.
702 # The To/Spec{Lower,Title,Upper}.pl are unused for now since the swash
703 # routines do not do returning multiple characters.
705 for my $case (qw(Lower Title Upper)) {
707 for my $prop (sort { $a->[0] <=> $b->[0] } @{$Case{$case}}) {
708 my ($ix, $code, $to) = @$prop;
709 append(\@case, $code, $to);
711 flush(\@case, "To/Spec$case.pl");