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.
110 # Read in the Unicode.txt, the main Unicode database.
117 if (open(my $Unicode, "Unicode.txt")) {
129 next unless /^[0-9A-Fa-f]+;/;
132 my ($code, $name, $cat, $comb, $bidi, $deco,
133 $decimal, $digit, $number,
134 $mirrored, $unicode10, $comment,
135 $upper, $lower, $title) = split(/\s*;\s*/);
137 if ($name =~ /^<(.+), (First|Last)>$/) {
140 append($General{$name} ||= [], $code, $name);
142 extend($General{$name} , $code);
144 unless (defined $In{$name}) {
145 $In{$name} = $InId++;
146 $InIn{$name} = $General{$name};
148 append($Cat{$cat} ||= [], $code);
149 append($Cat{substr($cat, 0, 1)}
152 append(\@Name, $code, $name);
154 append(\@General, $code, $cat);
156 append($Cat{$cat} ||= [], $code);
157 append($Cat{substr($cat, 0, 1)}
159 # 005F: SPACING UNDERSCORE
160 append($Cat{Word} ||= [], $code)
161 if $cat =~ /^[LMN]/ or $code eq "005F";
162 append($Cat{Alnum} ||= [], $code)
164 append($Cat{Alpha} ||= [], $code)
166 # 0009: HORIZONTAL TABULATION
168 # 000B: VERTICAL TABULATION
170 # 000D: CARRIAGE RETURN
172 append($Cat{Space} ||= [], $code)
174 $code =~ /^(0009|000A|000B|000C|000D)$/;
175 append($Cat{SpacePerl} ||= [], $code)
177 $code =~ /^(0009|000A|000C|000D)$/;
178 append($Cat{Blank} ||= [], $code)
179 if $code =~ /^(0020|0009)$/ ||
181 append($Cat{Digit} ||= [], $code) if $cat eq "Nd";
182 append($Cat{Upper} ||= [], $code) if $cat eq "Lu";
183 append($Cat{Lower} ||= [], $code) if $cat eq "Ll";
184 append($Cat{Title} ||= [], $code) if $cat eq "Lt";
185 append($Cat{ASCII} ||= [], $code) if $code le "007F";
186 append($Cat{Cntrl} ||= [], $code) if $cat =~ /^C/;
187 append($Cat{Graph} ||= [], $code) if $cat =~ /^([LMNPS]|Co)/;
188 append($Cat{Print} ||= [], $code) if $cat =~ /^([LMNPS]|Co|Zs)/;
189 append($Cat{Punct} ||= [], $code) if $cat =~ /^P/;
190 # 003[0-9]: DIGIT ZERO..NINE, 00[46][1-6]: A..F, a..f
191 append($Cat{XDigit} ||= [], $code)
192 if $code =~ /^00(3[0-9]|[46][1-6])$/;
194 append($To{Upper} ||= [], $code, $upper) if $upper;
195 append($To{Lower} ||= [], $code, $lower) if $lower;
196 append($To{Title} ||= [], $code, $title) if $title;
197 append($To{Digit} ||= [], $code, $decimal) if $decimal;
199 append(\@Bidi, $code, $bidi);
200 append($Bidi{$bidi} ||= [], $code);
202 append(\@Comb, $code, $comb) if $comb;
205 append(\@Deco, $code, $deco);
206 if ($deco =~/^<(\w+)>/) {
207 append($Deco{Compat} ||= [], $code);
208 append($DC{$1} ||= [], $code);
210 append($Deco{Canon} ||= [], $code);
214 append(\@Number, $code, $number) if $number;
216 append(\@Mirrored, $code) if $mirrored eq "Y";
220 flush(\@Name, "Name.pl");
222 foreach my $cat (sort keys %Cat) {
223 flush($Cat{$cat}, "Is/$cat.pl");
226 foreach my $to (sort keys %To) {
227 flush($To{$to}, "To/$to.pl");
230 flush(\@Bidi, "Bidirectional.pl");
231 foreach my $bidi (sort keys %Bidi) {
232 flush($Bidi{$bidi}, "Is/Bidi$bidi.pl");
235 flush(\@Comb, "CombiningClass.pl");
237 flush(\@Deco, "Decomposition.pl");
238 foreach my $deco (sort keys %Deco) {
239 flush($Deco{$deco}, "Is/Deco$deco.pl");
241 foreach my $dc (sort keys %DC) {
242 flush($DC{$dc}, "Is/DC$dc.pl");
245 flush(\@Number, "Number.pl");
247 flush(\@Mirrored, "Is/Mirrored.pl");
249 die "$0: Unicode.txt: $!\n";
252 # The general cateory can be written out already now.
254 flush(\@General, "Category.pl");
257 # Read in the LineBrk.txt.
260 if (open(my $LineBrk, "LineBrk.txt")) {
265 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/;
267 my ($first, $last, $lbrk) = ($1, $2, $3);
269 append(\@Lbrk, $first, $lbrk);
270 append($Lbrk{$lbrk} ||= [], $first);
272 extend(\@Lbrk, $last);
273 extend($Lbrk{$lbrk}, $last);
277 flush(\@Lbrk, "Lbrk.pl");
278 foreach my $lbrk (sort keys %Lbrk) {
279 flush($Lbrk{$lbrk}, "Is/Lbrk$lbrk.pl");
282 die "$0: LineBrk.txt: $!\n";
286 # Read in the ArabShap.txt.
289 if (open(my $ArabShap, "ArabShap.txt")) {
293 while (<$ArabShap>) {
294 next unless /^[0-9A-Fa-f]+;/;
297 my ($code, $name, $link, $linkgroup) = split(/\s*;\s*/);
299 append(\@ArabLink, $code, $link);
300 append(\@ArabLinkGroup, $code, $linkgroup);
303 flush(\@ArabLink, "ArabLink.pl");
304 flush(\@ArabLinkGroup, "ArabLnkGrp.pl");
306 die "$0: ArabShap.txt: $!\n";
310 # Read in the Jamo.txt.
313 if (open(my $Jamo, "Jamo.txt")) {
317 next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/;
319 my ($code, $short) = ($1, $2);
321 append(\@Short, $code, $short);
324 flush(\@Short, "JamoShort.pl");
326 die "$0: Jamo.txt: $!\n";
330 # Read in the Scripts.txt.
335 if (open(my $Scripts, "Scripts.txt")) {
337 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
339 # Wait until all the scripts have been read since
340 # they are not listed in numeric order.
341 push @Scripts, [ hex($1), $1, $2, $3 ];
344 die "$0: Scripts.txt: $!\n";
347 # Now append the scripts properties in their code point order.
352 for my $script (sort { $a->[0] <=> $b->[0] } @Scripts) {
353 my ($code, $first, $last, $name) = @$script;
354 append($Scripts, $first, $name);
355 append($Script{$name} ||= [], $first, $name);
357 extend($Scripts, $last);
358 extend($Script{$name}, $last);
360 unless (defined $In{$name}) {
361 $InScript{$InId} = $name;
362 $In{$name} = $InId++;
363 $InIn{$name} = $Script{$name};
367 # Scripts.pl can be written out already now.
369 flush(\@Scripts, "Scripts.pl");
371 # Common is everything not explicitly assigned to a Script
373 $In{Common} = $InId++;
374 my $Common = inverse($Scripts);
375 $InIn{Common} = $Common;
378 # Read in the Blocks.txt.
384 if (open(my $Blocks, "Blocks.txt")) {
386 next unless /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/;
388 my ($first, $last, $name) = ($1, $2, $3);
389 my $origname = $name;
391 # If there's a naming conflict (the script names are
392 # in uppercase), the name of the block has " Block"
395 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
396 for my $i (values %InScript) {
397 if ($i =~ /^$pat$/i) {
403 append(\@Blocks, $first, $name);
404 append($Blocks{$name} ||= [], $first, $name);
406 extend(\@Blocks, $last);
407 extend($Blocks{$name}, $last);
409 unless (defined $In{$name}) {
410 $InBlock{$InId} = $origname;
411 $In{$name} = $InId++;
412 $InIn{$name} = $Blocks{$name};
416 die "$0: Blocks.txt: $!\n";
419 # Blocks.pl can be written out already now.
421 flush(\@Blocks, "Blocks.pl");
424 # Read in the PropList.txt. It contains extended properties not
425 # listed in the Unicode.txt, such as 'Other_Alphabetic':
426 # alphabetic but not of the general category L; many modifiers
427 # belong to this extended property category: while they are not
428 # alphabets, they are alphabetic in nature.
433 if (open(my $Props, "PropList.txt")) {
435 next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/;
437 # Wait until all the extended properties have been read since
438 # they are not listed in numeric order.
439 push @Props, [ hex($1), $1, $2, $3 ];
442 die "$0: PropList.txt: $!\n";
445 # Now append the extended properties in their code point order.
450 for my $prop (sort { $a->[0] <=> $b->[0] } @Props) {
451 my ($code, $first, $last, $name) = @$prop;
452 append($Props, $first, $name);
453 append($Prop{$name} ||= [], $first, $name);
455 extend($Props, $last);
456 extend($Prop{$name}, $last);
458 unless (defined $In{$name}) {
459 $In{$name} = $InId++;
460 $InIn{$name} = $Prop{$name};
464 # Assigned is everything not Cn aka Noncharacter_Code_Point
466 $In{Assigned} = $InId++;
467 my $Assigned = inverse($Prop{Noncharacter_Code_Point});
468 $InIn{Assigned} = $Assigned;
470 sub merge_general_and_extended {
471 my ($name, $general, $extended) = @_;
475 map { pop @{$_}; $_ }
476 sort { $a->[2] <=> $b->[2] }
477 map { [ $_->[0], $_->[1], hex($_->[0]) ] }
479 map { ref $_ ? @$_ : $_ }
480 @Cat {ref $general ? @$general : $general } :
483 map { ref $_ ? @$_ : $_ }
484 @Prop{ref $extended ? @$extended : $extended} :
487 $In{$name} = $InId++;
488 $InIn{$name} = $merged;
493 # Alphabetic is L and Other_Alphabetic.
496 merge_general_and_extended('Alphabetic', 'L', 'Other_Alphabetic');
498 # Lowercase is Ll and Other_Lowercase.
501 merge_general_and_extended('Lowercase', 'Ll', 'Other_Lowercase');
503 # Uppercase is Lu and Other_Uppercase.
506 merge_general_and_extended('Uppercase', 'Lu', 'Other_Uppercase');
508 # Math is Sm and Other_Math.
511 merge_general_and_extended('Math', 'Sm', 'Other_Math');
513 # Lampersand is Ll, Lu, and Lt.
516 merge_general_and_extended('Lampersand', [ qw(Ll Lu Lt) ]);
518 # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl.
521 merge_general_and_extended('ID_Start', [ qw(Ll Lu Lt Lm Lo Nl) ]);
523 # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc.
526 merge_general_and_extended('ID_Continue', [ qw(Ll Lu Lt Lm Lo Nl
534 my $Any = [ [ 0, sprintf("%04X", $LastUnicodeCodepoint) ] ];
538 # mapping() will be used to write out the In and Is virtual mappings.
542 my ($map, $name) = @_;
544 if (open(my $fh, ">$name.pl")) {
548 # The %pat will hold a hash that maps the first two
549 # lowercased letters of a class to a 'fuzzified' regular
550 # expression that points to the real mapping.
554 # But first write out the offical name to real name
555 # (the filename) mapping.
561 for my $i (sort { lc $a cmp lc $b } keys %$map) {
563 # Here is the 'fuzzification': accept any space,
564 # dash, or underbar where in the official name
565 # there is space or a dash (or underbar, but
567 $pat =~ s/([- _])/(?:[-_]|\\s+)?/g;
568 # The prefix length of 2 is enough spread,
569 # and besides, we have 'Yi' as an In category.
570 push @{$pat{lc(substr($i, 0, 2))}}, [ $i, $pat ];
571 printf $fh "%-45s => '$map->{$i}',\n", "'$i'";
577 # Now write out the %pat mapping.
583 foreach my $prefix (sort keys %pat) {
584 print $fh "'$prefix' => {\n";
585 foreach my $ipat (@{$pat{$prefix}}) {
586 my ($i, $pat) = @$ipat;
587 print $fh "\t'$pat' => '$map->{$i}',\n";
597 die "$0: $name.pl: $!\n";
602 # Write out the virtual In mappings.
608 # Append the InScript and InBlock mappings.
609 # These are needed only if Script= and Block= syntaxes are used.
612 if (open(my $In, ">>In.pl")) {
618 for my $i (sort { $a <=> $b } keys %InScript) {
619 printf $In "%4d => '$InScript{$i}',\n", $i;
630 for my $i (sort { $a <=> $b } keys %InBlock) {
631 printf $In "%4d => '$InBlock{$i}',\n", $i;
637 die "$0: In.pl: $!\n";
640 # Easy low-calorie cheat.
642 copy("In/$In{Noncharacter_Code_Point}.pl", "Is/Cn.pl");
645 # Write out the real In mappings
646 # (the In.pl written out just above has the virtual In mappings)
649 foreach my $in (sort { $In{$a} <=> $In{$b} } keys %In) {
650 flush($InIn{$in}, "In/$In{$in}.pl");
654 # The mapping from General Category long forms to short forms is
655 # currently hardwired here since no simple data file in the UCD
661 'Uppercase Letter' => 'Lu',
662 'Lowercase Letter' => 'Ll',
663 'Titlecase Letter' => 'Lt',
664 'Modifier Letter' => 'Lm',
665 'Other Letter' => 'Lo',
668 'Non-Spacing Mark' => 'Mn',
669 'Spacing Combining Mark' => 'Mc',
670 'Enclosing Mark' => 'Me',
673 'Space Separator' => 'Zs',
674 'Line Separator' => 'Zl',
675 'Paragraph Separator' => 'Zp',
678 'Decimal Digit Number' => 'Nd',
679 'Letter Number' => 'Nl',
680 'Other Number' => 'No',
682 'Punctuation' => 'P',
683 'Connector Punctuation' => 'Pc',
684 'Dash Punctuation' => 'Pd',
685 'Open Punctuation' => 'Ps',
686 'Close Punctuation' => 'Pe',
687 'Initial Punctuation' => 'Pi',
688 'Final Punctuation' => 'Pf',
689 'Other Punctuation' => 'Po',
692 'Math Symbol' => 'Sm',
693 'Currency Symbol' => 'Sc',
694 'Modifier Symbol' => 'Sk',
695 'Other Symbol' => 'So',
701 'Private Use' => 'Co',
702 'Not Assigned' => 'Cn',
704 'Other Control' => 'Cc',
705 'Other Format' => 'Cf',
706 'Other Surrogate' => 'Cs',
707 'Other Private Use' => 'Co',
708 'Other Not Assigned' => 'Cn',
712 # Write out the virtual Is mappings.
718 # Read in the special cases.
723 if (open(my $SpecCase, "SpecCase.txt")) {
724 while (<$SpecCase>) {
725 next unless /^[0-9A-Fa-f]+;/;
729 my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/);
731 if ($condition) { # not implemented yet
732 print "# SKIPPING $_\n";
736 # Wait until all the special cases have been read since
737 # they are not listed in numeric order.
739 push @{$Case{Lower}}, [ $ix, $code, $lower ];
740 push @{$Case{Title}}, [ $ix, $code, $title ];
741 push @{$Case{Upper}}, [ $ix, $code, $upper ];
744 die "$0: SpecCase.txt: $!\n";
747 # Now write out the special cases properties in their code point order.
748 # Prepend them to the To/{Upper,Lower,Title}.pl.
750 for my $case (qw(Lower Title Upper)) {
751 my $NormalCase = do "To/$case.pl";
752 if (open(my $Case, ">To/$case.pl")) {
756 %utf8::ToSpec$case = (
758 for my $prop (sort { $a->[0] <=> $b->[0] } @{$Case{$case}}) {
759 my ($ix, $code, $to) = @$prop;
761 join "", map { sprintf "\\x{%s}", $_ } split ' ', $to;
762 print $Case qq['$ix' => "$tostr",\n];
769 print $Case $NormalCase;
772 die "$0: To/$case.txt: $!\n";