Commit | Line | Data |
d73e5302 |
1 | #!/usr/bin/perl -w |
4392c1cd |
2 | require 5.008; # Needs pack "U". Probably safest to run on 5.8.x |
d73e5302 |
3 | use strict; |
cf25bb62 |
4 | use Carp; |
d07a55ed |
5 | use File::Spec; |
5beb625e |
6 | |
cf25bb62 |
7 | ## |
8 | ## mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl) |
9 | ## from the Unicode database files (lib/unicore/*.txt). |
10 | ## |
d2d499f5 |
11 | |
cb3d361a |
12 | ## "Fuzzy" means this section in Unicode TR18: |
13 | ## |
14 | ## The recommended names for UCD properties and property values are in |
15 | ## PropertyAliases.txt [Prop] and PropertyValueAliases.txt |
16 | ## [PropValue]. There are both abbreviated names and longer, more |
17 | ## descriptive names. It is strongly recommended that both names be |
18 | ## recognized, and that loose matching of property names be used, |
19 | ## whereby the case distinctions, whitespace, hyphens, and underbar |
20 | ## are ignored. |
21 | |
d07a55ed |
22 | foreach my $lib ('To', 'lib', |
23 | map {File::Spec->catdir("lib",$_)} |
24 | qw(gc_sc dt bc hst ea jt lb nt ccc)) { |
25 | next if -d $lib; |
26 | mkdir $lib, 0755 or die "mkdir '$lib': $!"; |
27 | } |
d73e5302 |
28 | |
8e4b7420 |
29 | ## Base names already used in lib/gc_sc (for avoiding 8.3 conflicts) |
30 | my %BaseNames; |
31 | |
32 | |
cf25bb62 |
33 | ## |
34 | ## Process any args. |
35 | ## |
5beb625e |
36 | my $Verbose = 0; |
37 | my $MakeTestScript = 0; |
cf25bb62 |
38 | |
39 | while (@ARGV) |
40 | { |
41 | my $arg = shift @ARGV; |
42 | if ($arg eq '-v') { |
43 | $Verbose = 1; |
44 | } elsif ($arg eq '-q') { |
45 | $Verbose = 0; |
5beb625e |
46 | } elsif ($arg eq '-maketest') { |
47 | $MakeTestScript = 1; |
d73e5302 |
48 | } else { |
5beb625e |
49 | die "usage: $0 [-v|-q] [-maketest]"; |
d73e5302 |
50 | } |
51 | } |
52 | |
cf25bb62 |
53 | my $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1. |
d73e5302 |
54 | |
cf25bb62 |
55 | my $HEADER=<<"EOF"; |
d73e5302 |
56 | # !!!!!!! DO NOT EDIT THIS FILE !!!!!!! |
551b6b6f |
57 | # This file is built by $0 from e.g. UnicodeData.txt. |
d73e5302 |
58 | # Any changes made here will be lost! |
cf25bb62 |
59 | |
60 | EOF |
61 | |
d07a55ed |
62 | sub force_unlink { |
63 | my $filename = shift; |
64 | return unless -e $filename; |
65 | return if CORE::unlink($filename); |
66 | # We might need write permission |
67 | chmod 0777, $filename; |
68 | CORE::unlink($filename) or die "Couldn't unlink $filename: $!\n"; |
69 | } |
5beb625e |
70 | |
71 | ## |
72 | ## Given a filename and a reference to an array of lines, |
73 | ## write the lines to the file only if the contents have not changed. |
d07a55ed |
74 | ## Filename can be given as an arrayref of directory names |
5beb625e |
75 | ## |
76 | sub WriteIfChanged($\@) |
77 | { |
78 | my $file = shift; |
79 | my $lines = shift; |
80 | |
d07a55ed |
81 | $file = File::Spec->catfile(@$file) if ref $file; |
82 | |
5beb625e |
83 | my $TextToWrite = join '', @$lines; |
84 | if (open IN, $file) { |
85 | local($/) = undef; |
86 | my $PreviousText = <IN>; |
87 | close IN; |
88 | if ($PreviousText eq $TextToWrite) { |
89 | print "$file unchanged.\n" if $Verbose; |
90 | return; |
91 | } |
92 | } |
d07a55ed |
93 | force_unlink ($file); |
5beb625e |
94 | if (not open OUT, ">$file") { |
95 | die "$0: can't open $file for output: $!\n"; |
96 | } |
97 | print "$file written.\n" if $Verbose; |
98 | |
99 | print OUT $TextToWrite; |
100 | close OUT; |
101 | } |
102 | |
cf25bb62 |
103 | ## |
104 | ## The main datastructure (a "Table") represents a set of code points that |
105 | ## are part of a particular quality (that are part of \pL, \p{InGreek}, |
106 | ## etc.). They are kept as ranges of code points (starting and ending of |
107 | ## each range). |
108 | ## |
109 | ## For example, a range ASCII LETTERS would be represented as: |
110 | ## [ [ 0x41 => 0x5A, 'UPPER' ], |
111 | ## [ 0x61 => 0x7A, 'LOWER, ] ] |
112 | ## |
113 | sub RANGE_START() { 0 } ## index into range element |
114 | sub RANGE_END() { 1 } ## index into range element |
115 | sub RANGE_NAME() { 2 } ## index into range element |
116 | |
99598c8c |
117 | ## Conceptually, these should really be folded into the 'Table' objects |
cf25bb62 |
118 | my %TableInfo; |
99598c8c |
119 | my %TableDesc; |
cf25bb62 |
120 | my %FuzzyNames; |
121 | my %AliasInfo; |
5beb625e |
122 | my %CanonicalToOrig; |
cf25bb62 |
123 | |
124 | ## |
99598c8c |
125 | ## Turn something like |
126 | ## OLD-ITALIC |
5beb625e |
127 | ## into |
99598c8c |
128 | ## OldItalic |
129 | ## |
130 | sub CanonicalName($) |
131 | { |
5beb625e |
132 | my $orig = shift; |
133 | my $name = lc $orig; |
99598c8c |
134 | $name =~ s/(?<![a-z])(\w)/\u$1/g; |
5beb625e |
135 | $name =~ s/[-_\s]+//g; |
99598c8c |
136 | |
5beb625e |
137 | $CanonicalToOrig{$name} = $orig if not $CanonicalToOrig{$name}; |
99598c8c |
138 | return $name; |
139 | } |
140 | |
12ac2576 |
141 | |
142 | ## |
143 | ## Store the alias definitions for later use. |
144 | ## |
145 | my %PropertyAlias; |
146 | my %PropValueAlias; |
147 | |
148 | my %PA_reverse; |
149 | my %PVA_reverse; |
150 | |
151 | sub Build_Aliases() |
152 | { |
153 | ## |
154 | ## Most of the work with aliases doesn't occur here, |
155 | ## but rather in utf8_heavy.pl, which uses utf8_pva.pl, |
156 | ## which contains just this function. However, this one |
157 | ## |
158 | ## -- japhy (2004/04/13) |
159 | |
160 | open PA, "< PropertyAliases.txt" |
161 | or confess "Can't open PropertyAliases.txt: $!"; |
162 | while (<PA>) { |
163 | s/#.*//; |
164 | s/\s+$//; |
165 | next if /^$/; |
166 | |
167 | my ($abbrev, $name) = split /\s*;\s*/; |
168 | next if $abbrev eq "n/a"; |
169 | $PropertyAlias{$abbrev} = $name; |
170 | $PA_reverse{$name} = $abbrev; |
171 | } |
172 | close PA; |
173 | |
174 | open PVA, "< PropValueAliases.txt" |
175 | or confess "Can't open PropValueAliases.txt: $!"; |
176 | while (<PVA>) { |
177 | s/#.*//; |
178 | s/\s+$//; |
179 | next if /^$/; |
180 | |
181 | my ($prop, @data) = split /\s*;\s*/; |
182 | |
183 | if ($prop eq 'ccc') { |
184 | $PropValueAlias{$prop}{$data[1]} = [ @data[0,2] ]; |
185 | $PVA_reverse{$prop}{$data[2]} = [ @data[0,1] ]; |
186 | } |
187 | else { |
188 | next if $data[0] eq "n/a"; |
189 | $PropValueAlias{$prop}{$data[0]} = $data[1]; |
190 | $PVA_reverse{$prop}{$data[1]} = $data[0]; |
191 | } |
192 | } |
193 | close PVA; |
194 | } |
195 | |
196 | |
99598c8c |
197 | ## |
cf25bb62 |
198 | ## Associates a property ("Greek", "Lu", "Assigned",...) with a Table. |
199 | ## |
200 | ## Called like: |
99598c8c |
201 | ## New_Prop(In => 'Greek', $Table, Desc => 'Greek Block', Fuzzy => 1); |
cf25bb62 |
202 | ## |
203 | ## Normally, these parameters are set when the Table is created (when the |
204 | ## Table->New constructor is called), but there are times when it needs to |
205 | ## be done after-the-fact...) |
206 | ## |
207 | sub New_Prop($$$@) |
208 | { |
209 | my $Type = shift; ## "Is" or "In"; |
210 | my $Name = shift; |
211 | my $Table = shift; |
212 | |
213 | ## remaining args are optional key/val |
214 | my %Args = @_; |
215 | |
99598c8c |
216 | my $Fuzzy = delete $Args{Fuzzy}; |
217 | my $Desc = delete $Args{Desc}; # description |
218 | |
5beb625e |
219 | $Name = CanonicalName($Name) if $Fuzzy; |
cf25bb62 |
220 | |
221 | ## sanity check a few args |
222 | if (%Args or ($Type ne 'Is' and $Type ne 'In') or not ref $Table) { |
223 | confess "$0: bad args to New_Prop" |
224 | } |
225 | |
226 | if (not $TableInfo{$Type}->{$Name}) |
227 | { |
228 | $TableInfo{$Type}->{$Name} = $Table; |
99598c8c |
229 | $TableDesc{$Type}->{$Name} = $Desc; |
230 | if ($Fuzzy) { |
cf25bb62 |
231 | $FuzzyNames{$Type}->{$Name} = $Name; |
232 | } |
233 | } |
d73e5302 |
234 | } |
235 | |
d73e5302 |
236 | |
cf25bb62 |
237 | ## |
238 | ## Creates a new Table object. |
239 | ## |
240 | ## Args are key/value pairs: |
99598c8c |
241 | ## In => Name -- Name of "In" property to be associated with |
242 | ## Is => Name -- Name of "Is" property to be associated with |
243 | ## Fuzzy => Boolean -- True if name can be accessed "fuzzily" |
244 | ## Desc => String -- Description of the property |
cf25bb62 |
245 | ## |
246 | ## No args are required. |
247 | ## |
248 | sub Table::New |
249 | { |
250 | my $class = shift; |
251 | my %Args = @_; |
252 | |
253 | my $Table = bless [], $class; |
254 | |
99598c8c |
255 | my $Fuzzy = delete $Args{Fuzzy}; |
256 | my $Desc = delete $Args{Desc}; |
cf25bb62 |
257 | |
258 | for my $Type ('Is', 'In') |
259 | { |
260 | if (my $Name = delete $Args{$Type}) { |
99598c8c |
261 | New_Prop($Type => $Name, $Table, Desc => $Desc, Fuzzy => $Fuzzy); |
cf25bb62 |
262 | } |
263 | } |
264 | |
265 | ## shouldn't have any left over |
266 | if (%Args) { |
267 | confess "$0: bad args to Table->New" |
268 | } |
269 | |
270 | return $Table; |
d73e5302 |
271 | } |
272 | |
cf25bb62 |
273 | ## |
274 | ## Returns true if the Table has no code points |
275 | ## |
276 | sub Table::IsEmpty |
277 | { |
278 | my $Table = shift; #self |
279 | return not @$Table; |
d73e5302 |
280 | } |
281 | |
cf25bb62 |
282 | ## |
283 | ## Returns true if the Table has code points |
284 | ## |
285 | sub Table::NotEmpty |
286 | { |
287 | my $Table = shift; #self |
288 | return @$Table; |
d73e5302 |
289 | } |
290 | |
cf25bb62 |
291 | ## |
292 | ## Returns the maximum code point currently in the table. |
293 | ## |
294 | sub Table::Max |
295 | { |
296 | my $Table = shift; #self |
297 | confess "oops" if $Table->IsEmpty; ## must have code points to have a max |
298 | return $Table->[-1]->[RANGE_END]; |
299 | } |
d73e5302 |
300 | |
cf25bb62 |
301 | ## |
302 | ## Replaces the codepoints in the Table with those in the Table given |
303 | ## as an arg. (NOTE: this is not a "deep copy"). |
304 | ## |
305 | sub Table::Replace($$) |
306 | { |
307 | my $Table = shift; #self |
308 | my $New = shift; |
d73e5302 |
309 | |
cf25bb62 |
310 | @$Table = @$New; |
311 | } |
71d929cb |
312 | |
cf25bb62 |
313 | ## |
314 | ## Given a new code point, make the last range of the Table extend to |
315 | ## include the new (and all intervening) code points. |
316 | ## |
317 | sub Table::Extend |
318 | { |
319 | my $Table = shift; #self |
320 | my $codepoint = shift; |
d73e5302 |
321 | |
cf25bb62 |
322 | my $PrevMax = $Table->Max; |
e904f995 |
323 | |
cf25bb62 |
324 | confess "oops ($codepoint <= $PrevMax)" if $codepoint <= $PrevMax; |
e904f995 |
325 | |
cf25bb62 |
326 | $Table->[-1]->[RANGE_END] = $codepoint; |
327 | } |
c3a8a2b8 |
328 | |
cf25bb62 |
329 | ## |
330 | ## Given a code point range start and end (and optional name), blindly |
331 | ## append them to the list of ranges for the Table. |
332 | ## |
333 | ## NOTE: Code points must be added in strictly ascending numeric order. |
334 | ## |
335 | sub Table::RawAppendRange |
336 | { |
337 | my $Table = shift; #self |
338 | my $start = shift; |
339 | my $end = shift; |
340 | my $name = shift; |
341 | $name = "" if not defined $name; ## warning: $name can be "0" |
342 | |
343 | push @$Table, [ $start, # RANGE_START |
344 | $end, # RANGE_END |
345 | $name ]; # RANGE_NAME |
346 | } |
c3a8a2b8 |
347 | |
cf25bb62 |
348 | ## |
349 | ## Given a code point (and optional name), add it to the Table. |
350 | ## |
351 | ## NOTE: Code points must be added in strictly ascending numeric order. |
352 | ## |
353 | sub Table::Append |
354 | { |
355 | my $Table = shift; #self |
356 | my $codepoint = shift; |
357 | my $name = shift; |
358 | $name = "" if not defined $name; ## warning: $name can be "0" |
359 | |
360 | ## |
361 | ## If we've already got a range working, and this code point is the next |
362 | ## one in line, and if the name is the same, just extend the current range. |
363 | ## |
364 | if ($Table->NotEmpty |
365 | and |
366 | $Table->Max == $codepoint - 1 |
367 | and |
368 | $Table->[-1]->[RANGE_NAME] eq $name) |
369 | { |
370 | $Table->Extend($codepoint); |
c3a8a2b8 |
371 | } |
cf25bb62 |
372 | else |
373 | { |
374 | $Table->RawAppendRange($codepoint, $codepoint, $name); |
d73e5302 |
375 | } |
cf25bb62 |
376 | } |
d73e5302 |
377 | |
cf25bb62 |
378 | ## |
379 | ## Given a code point range starting value and ending value (and name), |
380 | ## Add the range to teh Table. |
381 | ## |
382 | ## NOTE: Code points must be added in strictly ascending numeric order. |
383 | ## |
384 | sub Table::AppendRange |
385 | { |
386 | my $Table = shift; #self |
387 | my $start = shift; |
388 | my $end = shift; |
389 | my $name = shift; |
390 | $name = "" if not defined $name; ## warning: $name can be "0" |
391 | |
392 | $Table->Append($start, $name); |
393 | $Table->Extend($end) if $end > $start; |
394 | } |
d73e5302 |
395 | |
cf25bb62 |
396 | ## |
397 | ## Return a new Table that represents all code points not in the Table. |
398 | ## |
399 | sub Table::Invert |
400 | { |
401 | my $Table = shift; #self |
402 | |
403 | my $New = Table->New(); |
404 | my $max = -1; |
405 | for my $range (@$Table) |
406 | { |
407 | my $start = $range->[RANGE_START]; |
408 | my $end = $range->[RANGE_END]; |
409 | if ($start-1 >= $max+1) { |
410 | $New->AppendRange($max+1, $start-1, ""); |
411 | } |
412 | $max = $end; |
d73e5302 |
413 | } |
cf25bb62 |
414 | if ($max+1 < $LastUnicodeCodepoint) { |
415 | $New->AppendRange($max+1, $LastUnicodeCodepoint); |
d73e5302 |
416 | } |
cf25bb62 |
417 | return $New; |
418 | } |
d73e5302 |
419 | |
cf25bb62 |
420 | ## |
421 | ## Merges any number of other tables with $self, returning the new table. |
422 | ## (existing tables are not modified) |
423 | ## |
a3a8c5f0 |
424 | ## |
425 | ## Args may be Tables, or individual code points (as integers). |
426 | ## |
cf25bb62 |
427 | ## Can be called as either a constructor or a method. |
428 | ## |
429 | sub Table::Merge |
430 | { |
431 | shift(@_) if not ref $_[0]; ## if called as a constructor, lose the class |
432 | my @Tables = @_; |
433 | |
434 | ## Accumulate all records from all tables |
435 | my @Records; |
a3a8c5f0 |
436 | for my $Arg (@Tables) |
437 | { |
438 | if (ref $Arg) { |
439 | ## arg is a table -- get its ranges |
440 | push @Records, @$Arg; |
441 | } else { |
442 | ## arg is a codepoint, make a range |
443 | push @Records, [ $Arg, $Arg ] |
444 | } |
d73e5302 |
445 | } |
446 | |
cf25bb62 |
447 | ## sort by range start, with longer ranges coming first. |
448 | my ($first, @Rest) = sort { |
449 | ($a->[RANGE_START] <=> $b->[RANGE_START]) |
450 | or |
451 | ($b->[RANGE_END] <=> $b->[RANGE_END]) |
452 | } @Records; |
453 | |
454 | my $New = Table->New(); |
455 | |
456 | ## Ensuring the first range is there makes the subsequent loop easier |
457 | $New->AppendRange($first->[RANGE_START], |
458 | $first->[RANGE_END]); |
459 | |
460 | ## Fold in records so long as they add new information. |
461 | for my $set (@Rest) |
462 | { |
463 | my $start = $set->[RANGE_START]; |
464 | my $end = $set->[RANGE_END]; |
465 | if ($start > $New->Max) { |
466 | $New->AppendRange($start, $end); |
467 | } elsif ($end > $New->Max) { |
468 | $New->Extend($end); |
469 | } |
d73e5302 |
470 | } |
d73e5302 |
471 | |
cf25bb62 |
472 | return $New; |
d73e5302 |
473 | } |
474 | |
cf25bb62 |
475 | ## |
476 | ## Given a filename, write a representation of the Table to a file. |
99598c8c |
477 | ## May have an optional comment as a 2nd arg. |
d07a55ed |
478 | ## Filename may actually be an arrayref of directories |
cf25bb62 |
479 | ## |
480 | sub Table::Write |
481 | { |
99598c8c |
482 | my $Table = shift; #self |
cf25bb62 |
483 | my $filename = shift; |
99598c8c |
484 | my $comment = shift; |
d73e5302 |
485 | |
5beb625e |
486 | my @OUT = $HEADER; |
99598c8c |
487 | if (defined $comment) { |
488 | $comment =~ s/\s+\Z//; |
489 | $comment =~ s/^/# /gm; |
5beb625e |
490 | push @OUT, "#\n$comment\n#\n"; |
99598c8c |
491 | } |
5beb625e |
492 | push @OUT, "return <<'END';\n"; |
d73e5302 |
493 | |
cf25bb62 |
494 | for my $set (@$Table) |
495 | { |
496 | my $start = $set->[RANGE_START]; |
497 | my $end = $set->[RANGE_END]; |
498 | my $name = $set->[RANGE_NAME]; |
d73e5302 |
499 | |
cf25bb62 |
500 | if ($start == $end) { |
5beb625e |
501 | push @OUT, sprintf "%04X\t\t%s\n", $start, $name; |
cf25bb62 |
502 | } else { |
5beb625e |
503 | push @OUT, sprintf "%04X\t%04X\t%s\n", $start, $end, $name; |
cf25bb62 |
504 | } |
505 | } |
d73e5302 |
506 | |
5beb625e |
507 | push @OUT, "END\n"; |
508 | |
509 | WriteIfChanged($filename, @OUT); |
510 | } |
511 | |
512 | ## This used only for making the test script. |
513 | ## helper function |
514 | sub IsUsable($) |
515 | { |
516 | my $code = shift; |
517 | return 0 if $code <= 0x0000; ## don't use null |
518 | return 0 if $code >= $LastUnicodeCodepoint; ## keep in range |
519 | return 0 if ($code >= 0xD800 and $code <= 0xDFFF); ## no surrogates |
520 | return 0 if ($code >= 0xFDD0 and $code <= 0xFDEF); ## utf8.c says no good |
521 | return 0 if (($code & 0xFFFF) == 0xFFFE); ## utf8.c says no good |
522 | return 0 if (($code & 0xFFFF) == 0xFFFF); ## utf8.c says no good |
523 | return 1; |
524 | } |
525 | |
526 | ## Return a code point that's part of the table. |
527 | ## Returns nothing if the table is empty (or covers only surrogates). |
528 | ## This used only for making the test script. |
529 | sub Table::ValidCode |
530 | { |
531 | my $Table = shift; #self |
532 | for my $set (@$Table) { |
533 | return $set->[RANGE_END] if IsUsable($set->[RANGE_END]); |
534 | } |
535 | return (); |
536 | } |
537 | |
538 | ## Return a code point that's not part of the table |
539 | ## Returns nothing if the table covers all code points. |
540 | ## This used only for making the test script. |
541 | sub Table::InvalidCode |
542 | { |
543 | my $Table = shift; #self |
544 | |
545 | return 0x1234 if $Table->IsEmpty(); |
546 | |
547 | for my $set (@$Table) |
548 | { |
549 | if (IsUsable($set->[RANGE_END] + 1)) |
550 | { |
551 | return $set->[RANGE_END] + 1; |
552 | } |
553 | |
554 | if (IsUsable($set->[RANGE_START] - 1)) |
555 | { |
556 | return $set->[RANGE_START] - 1; |
557 | } |
558 | } |
559 | return (); |
cf25bb62 |
560 | } |
561 | |
562 | ########################################################################### |
563 | ########################################################################### |
564 | ########################################################################### |
565 | |
566 | |
567 | ## |
568 | ## Called like: |
99598c8c |
569 | ## New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 1); |
cf25bb62 |
570 | ## |
99598c8c |
571 | ## The args must be in that order, although the Fuzzy pair may be omitted. |
cf25bb62 |
572 | ## |
573 | ## This creates 'IsAll' as an alias for 'IsAny' |
574 | ## |
575 | sub New_Alias($$$@) |
576 | { |
577 | my $Type = shift; ## "Is" or "In" |
578 | my $Alias = shift; |
99598c8c |
579 | my $SameAs = shift; # expecting "SameAs" -- just ignored |
cf25bb62 |
580 | my $Name = shift; |
581 | |
582 | ## remaining args are optional key/val |
583 | my %Args = @_; |
584 | |
99598c8c |
585 | my $Fuzzy = delete $Args{Fuzzy}; |
cf25bb62 |
586 | |
587 | ## sanity check a few args |
588 | if (%Args or ($Type ne 'Is' and $Type ne 'In') or $SameAs ne 'SameAs') { |
589 | confess "$0: bad args to New_Alias" |
d73e5302 |
590 | } |
591 | |
5beb625e |
592 | $Alias = CanonicalName($Alias) if $Fuzzy; |
593 | |
594 | if (not $TableInfo{$Type}->{$Name}) |
595 | { |
596 | my $CName = CanonicalName($Name); |
597 | if ($TableInfo{$Type}->{$CName}) { |
598 | confess "$0: Use canonical form '$CName' instead of '$Name' for alias."; |
599 | } else { |
12ac2576 |
600 | confess "$0: don't have original $Type => $Name to make alias\n"; |
5beb625e |
601 | } |
cf25bb62 |
602 | } |
603 | if ($TableInfo{$Alias}) { |
604 | confess "$0: already have original $Type => $Alias; can't make alias"; |
d73e5302 |
605 | } |
cf25bb62 |
606 | $AliasInfo{$Type}->{$Name} = $Alias; |
99598c8c |
607 | if ($Fuzzy) { |
cf25bb62 |
608 | $FuzzyNames{$Type}->{$Alias} = $Name; |
609 | } |
610 | |
d73e5302 |
611 | } |
612 | |
d73e5302 |
613 | |
cf25bb62 |
614 | ## All assigned code points |
99598c8c |
615 | my $Assigned = Table->New(Is => 'Assigned', |
616 | Desc => "All assigned code points", |
5beb625e |
617 | Fuzzy => 0); |
d2d499f5 |
618 | |
cf25bb62 |
619 | my $Name = Table->New(); ## all characters, individually by name |
620 | my $General = Table->New(); ## all characters, grouped by category |
621 | my %General; |
622 | my %Cat; |
d73e5302 |
623 | |
cf25bb62 |
624 | ## |
551b6b6f |
625 | ## Process UnicodeData.txt (Categories, etc.) |
cf25bb62 |
626 | ## |
44da8cae |
627 | sub UnicodeData_Txt() |
cf25bb62 |
628 | { |
629 | my $Bidi = Table->New(); |
630 | my $Deco = Table->New(); |
631 | my $Comb = Table->New(); |
632 | my $Number = Table->New(); |
12ac2576 |
633 | my $Mirrored = Table->New();#Is => 'Mirrored', |
634 | #Desc => "Mirrored in bidirectional text", |
635 | #Fuzzy => 0); |
d73e5302 |
636 | |
cf25bb62 |
637 | my %DC; |
638 | my %Bidi; |
12ac2576 |
639 | my %Number; |
640 | $DC{can} = Table->New(); |
641 | $DC{com} = Table->New(); |
cf25bb62 |
642 | |
643 | ## Initialize Perl-generated categories |
551b6b6f |
644 | ## (Categories from UnicodeData.txt are auto-initialized in gencat) |
44da8cae |
645 | $Cat{Alnum} = |
646 | Table->New(Is => 'Alnum', Desc => "[[:Alnum:]]", Fuzzy => 0); |
647 | $Cat{Alpha} = |
648 | Table->New(Is => 'Alpha', Desc => "[[:Alpha:]]", Fuzzy => 0); |
649 | $Cat{ASCII} = |
650 | Table->New(Is => 'ASCII', Desc => "[[:ASCII:]]", Fuzzy => 0); |
651 | $Cat{Blank} = |
652 | Table->New(Is => 'Blank', Desc => "[[:Blank:]]", Fuzzy => 0); |
653 | $Cat{Cntrl} = |
654 | Table->New(Is => 'Cntrl', Desc => "[[:Cntrl:]]", Fuzzy => 0); |
655 | $Cat{Digit} = |
656 | Table->New(Is => 'Digit', Desc => "[[:Digit:]]", Fuzzy => 0); |
657 | $Cat{Graph} = |
658 | Table->New(Is => 'Graph', Desc => "[[:Graph:]]", Fuzzy => 0); |
659 | $Cat{Lower} = |
660 | Table->New(Is => 'Lower', Desc => "[[:Lower:]]", Fuzzy => 0); |
661 | $Cat{Print} = |
662 | Table->New(Is => 'Print', Desc => "[[:Print:]]", Fuzzy => 0); |
663 | $Cat{Punct} = |
664 | Table->New(Is => 'Punct', Desc => "[[:Punct:]]", Fuzzy => 0); |
665 | $Cat{Space} = |
666 | Table->New(Is => 'Space', Desc => "[[:Space:]]", Fuzzy => 0); |
667 | $Cat{Title} = |
668 | Table->New(Is => 'Title', Desc => "[[:Title:]]", Fuzzy => 0); |
669 | $Cat{Upper} = |
670 | Table->New(Is => 'Upper', Desc => "[[:Upper:]]", Fuzzy => 0); |
671 | $Cat{XDigit} = |
672 | Table->New(Is => 'XDigit', Desc => "[[:XDigit:]]", Fuzzy => 0); |
673 | $Cat{Word} = |
674 | Table->New(Is => 'Word', Desc => "[[:Word:]]", Fuzzy => 0); |
675 | $Cat{SpacePerl} = |
676 | Table->New(Is => 'SpacePerl', Desc => '\s', Fuzzy => 0); |
d73e5302 |
677 | |
cf25bb62 |
678 | my %To; |
679 | $To{Upper} = Table->New(); |
680 | $To{Lower} = Table->New(); |
681 | $To{Title} = Table->New(); |
682 | $To{Digit} = Table->New(); |
683 | |
684 | sub gencat($$$$) |
685 | { |
686 | my ($name, ## Name ("LATIN CAPITAL LETTER A") |
687 | $cat, ## Category ("Lu", "Zp", "Nd", etc.) |
688 | $code, ## Code point (as an integer) |
689 | $op) = @_; |
690 | |
691 | my $MajorCat = substr($cat, 0, 1); ## L, M, Z, S, etc |
692 | |
693 | $Assigned->$op($code); |
694 | $Name->$op($code, $name); |
695 | $General->$op($code, $cat); |
696 | |
697 | ## add to the sub category (e.g. "Lu", "Nd", "Cf", ..) |
99598c8c |
698 | $Cat{$cat} ||= Table->New(Is => $cat, |
699 | Desc => "General Category '$cat'", |
700 | Fuzzy => 0); |
cf25bb62 |
701 | $Cat{$cat}->$op($code); |
702 | |
703 | ## add to the major category (e.g. "L", "N", "C", ...) |
99598c8c |
704 | $Cat{$MajorCat} ||= Table->New(Is => $MajorCat, |
705 | Desc => "Major Category '$MajorCat'", |
706 | Fuzzy => 0); |
cf25bb62 |
707 | $Cat{$MajorCat}->$op($code); |
708 | |
709 | ($General{$name} ||= Table->New)->$op($code, $name); |
710 | |
711 | # 005F: SPACING UNDERSCORE |
d75d706f |
712 | $Cat{Word}->$op($code) if $cat =~ /^[LMN]|Pc/; |
c65e4d19 |
713 | $Cat{Alnum}->$op($code) if $cat =~ /^[LM]|Nd/; |
cf25bb62 |
714 | $Cat{Alpha}->$op($code) if $cat =~ /^[LM]/; |
715 | |
d75d706f |
716 | my $isspace = |
717 | ($cat =~ /Zs|Zl|Zp/ && |
718 | $code != 0x200B) # 200B is ZWSP which is for line break control |
719 | # and therefore it is not part of "space" even while it is "Zs". |
cf25bb62 |
720 | || $code == 0x0009 # 0009: HORIZONTAL TAB |
721 | || $code == 0x000A # 000A: LINE FEED |
722 | || $code == 0x000B # 000B: VERTICAL TAB |
723 | || $code == 0x000C # 000C: FORM FEED |
e6f58734 |
724 | || $code == 0x000D # 000D: CARRIAGE RETURN |
d75d706f |
725 | || $code == 0x0085 # 0085: NEL |
726 | |
727 | ; |
cf25bb62 |
728 | |
d75d706f |
729 | $Cat{Space}->$op($code) if $isspace; |
cf25bb62 |
730 | |
d75d706f |
731 | $Cat{SpacePerl}->$op($code) if $isspace |
732 | && $code != 0x000B; # Backward compat. |
cf25bb62 |
733 | |
d75d706f |
734 | $Cat{Blank}->$op($code) if $isspace |
735 | && !($code == 0x000A || |
736 | $code == 0x000B || |
737 | $code == 0x000C || |
738 | $code == 0x000D || |
739 | $code == 0x0085 || |
740 | $cat =~ /^Z[lp]/); |
cf25bb62 |
741 | |
742 | $Cat{Digit}->$op($code) if $cat eq "Nd"; |
743 | $Cat{Upper}->$op($code) if $cat eq "Lu"; |
744 | $Cat{Lower}->$op($code) if $cat eq "Ll"; |
745 | $Cat{Title}->$op($code) if $cat eq "Lt"; |
746 | $Cat{ASCII}->$op($code) if $code <= 0x007F; |
747 | $Cat{Cntrl}->$op($code) if $cat =~ /^C/; |
d75d706f |
748 | my $isgraph = !$isspace && $cat !~ /Cc|Cs|Cn/; |
749 | $Cat{Graph}->$op($code) if $isgraph; |
750 | $Cat{Print}->$op($code) if $isgraph || $isspace; |
cf25bb62 |
751 | $Cat{Punct}->$op($code) if $cat =~ /^P/; |
752 | |
753 | $Cat{XDigit}->$op($code) if ($code >= 0x30 && $code <= 0x39) ## 0..9 |
754 | || ($code >= 0x41 && $code <= 0x46) ## A..F |
755 | || ($code >= 0x61 && $code <= 0x66); ## a..f |
756 | } |
d73e5302 |
757 | |
cf25bb62 |
758 | ## open ane read file..... |
551b6b6f |
759 | if (not open IN, "UnicodeData.txt") { |
760 | die "$0: UnicodeData.txt: $!\n"; |
cf25bb62 |
761 | } |
d73e5302 |
762 | |
a3a8c5f0 |
763 | ## |
764 | ## For building \p{_CombAbove} and \p{_CanonDCIJ} |
765 | ## |
766 | my %_Above_HexCodes; ## Hexcodes for chars with $comb == 230 ("ABOVE") |
767 | |
768 | my %CodeToDeco; ## Maps code to decomp. list for chars with first |
769 | ## decomp. char an "i" or "j" (for \p{_CanonDCIJ}) |
770 | |
771 | ## This is filled in as we go.... |
99598c8c |
772 | my $CombAbove = Table->New(Is => '_CombAbove', |
773 | Desc => '(for internal casefolding use)', |
774 | Fuzzy => 0); |
a3a8c5f0 |
775 | |
cf25bb62 |
776 | while (<IN>) |
777 | { |
778 | next unless /^[0-9A-Fa-f]+;/; |
779 | s/\s+$//; |
780 | |
781 | my ($hexcode, ## code point in hex (e.g. "0041") |
782 | $name, ## character name (e.g. "LATIN CAPITAL LETTER A") |
783 | $cat, ## category (e.g. "Lu") |
784 | $comb, ## Canonical combining class (e.t. "230") |
785 | $bidi, ## directional category (e.g. "L") |
786 | $deco, ## decomposition mapping |
787 | $decimal, ## decimal digit value |
788 | $digit, ## digit value |
789 | $number, ## numeric value |
790 | $mirrored, ## mirrored |
791 | $unicode10, ## name in Unicode 1.0 |
792 | $comment, ## comment field |
793 | $upper, ## uppercase mapping |
794 | $lower, ## lowercase mapping |
795 | $title, ## titlecase mapping |
796 | ) = split(/\s*;\s*/); |
797 | |
2eb5892f |
798 | # Note that in Unicode 3.2 there will be names like |
799 | # LINE FEED (LF), which probably means that \N{} needs |
800 | # to cope also with LINE FEED and LF. |
801 | $name = $unicode10 if $name eq '<control>' && $unicode10 ne ''; |
802 | |
cf25bb62 |
803 | my $code = hex($hexcode); |
804 | |
a3a8c5f0 |
805 | if ($comb and $comb == 230) { |
806 | $CombAbove->Append($code); |
807 | $_Above_HexCodes{$hexcode} = 1; |
808 | } |
809 | |
810 | ## Used in building \p{_CanonDCIJ} |
811 | if ($deco and $deco =~ m/^006[9A]\b/) { |
812 | $CodeToDeco{$code} = $deco; |
813 | } |
814 | |
cf25bb62 |
815 | ## |
816 | ## There are a few pairs of lines like: |
817 | ## AC00;<Hangul Syllable, First>;Lo;0;L;;;;;N;;;;; |
818 | ## D7A3;<Hangul Syllable, Last>;Lo;0;L;;;;;N;;;;; |
819 | ## that define ranges. |
820 | ## |
821 | if ($name =~ /^<(.+), (First|Last)>$/) |
822 | { |
823 | $name = $1; |
824 | gencat($name, $cat, $code, $2 eq 'First' ? 'Append' : 'Extend'); |
99598c8c |
825 | #New_Prop(In => $name, $General{$name}, Fuzzy => 1); |
cf25bb62 |
826 | } |
827 | else |
828 | { |
829 | ## normal (single-character) lines |
830 | gencat($name, $cat, $code, 'Append'); |
831 | |
832 | # No Append() here since since several codes may map into one. |
833 | $To{Upper}->RawAppendRange($code, $code, $upper) if $upper; |
834 | $To{Lower}->RawAppendRange($code, $code, $lower) if $lower; |
835 | $To{Title}->RawAppendRange($code, $code, $title) if $title; |
836 | $To{Digit}->Append($code, $decimal) if length $decimal; |
837 | |
838 | $Bidi->Append($code, $bidi); |
839 | $Comb->Append($code, $comb) if $comb; |
840 | $Number->Append($code, $number) if length $number; |
841 | |
12ac2576 |
842 | length($decimal) and ($Number{De} ||= Table->New())->Append($code) |
843 | or |
844 | length($digit) and ($Number{Di} ||= Table->New())->Append($code) |
845 | or |
846 | length($number) and ($Number{Nu} ||= Table->New())->Append($code); |
847 | |
cf25bb62 |
848 | $Mirrored->Append($code) if $mirrored eq "Y"; |
849 | |
12ac2576 |
850 | $Bidi{$bidi} ||= Table->New();#Is => "bt/$bidi", |
851 | #Desc => "Bi-directional category '$bidi'", |
852 | #Fuzzy => 0); |
cf25bb62 |
853 | $Bidi{$bidi}->Append($code); |
854 | |
855 | if ($deco) |
856 | { |
857 | $Deco->Append($code, $deco); |
858 | if ($deco =~/^<(\w+)>/) |
859 | { |
12ac2576 |
860 | my $dshort = $PVA_reverse{dt}{ucfirst lc $1}; |
861 | $DC{com}->Append($code); |
cf25bb62 |
862 | |
12ac2576 |
863 | $DC{$dshort} ||= Table->New(); |
864 | $DC{$dshort}->Append($code); |
cf25bb62 |
865 | } |
866 | else |
867 | { |
12ac2576 |
868 | $DC{can}->Append($code); |
cf25bb62 |
869 | } |
870 | } |
871 | } |
872 | } |
873 | close IN; |
d2d499f5 |
874 | |
cf25bb62 |
875 | ## |
876 | ## Tidy up a few special cases.... |
877 | ## |
d73e5302 |
878 | |
cf25bb62 |
879 | $Cat{Cn} = $Assigned->Invert; ## Cn is everything that doesn't exist |
99598c8c |
880 | New_Prop(Is => 'Cn', |
881 | $Cat{Cn}, |
882 | Desc => "General Category 'Cn' [not functional in Perl]", |
883 | Fuzzy => 0); |
d73e5302 |
884 | |
cf25bb62 |
885 | ## Unassigned is the same as 'Cn' |
5beb625e |
886 | New_Alias(Is => 'Unassigned', SameAs => 'Cn', Fuzzy => 0); |
d73e5302 |
887 | |
cf25bb62 |
888 | $Cat{C}->Replace($Cat{C}->Merge($Cat{Cn})); ## Now merge in Cn into C |
d73e5302 |
889 | |
d73e5302 |
890 | |
12ac2576 |
891 | # LC is Ll, Lu, and Lt. |
892 | # (used to be L& or L_, but PropValueAliases.txt defines it as LC) |
893 | New_Prop(Is => 'LC', |
cf25bb62 |
894 | Table->Merge(@Cat{qw[Ll Lu Lt]}), |
99598c8c |
895 | Desc => '[\p{Ll}\p{Lu}\p{Lt}]', |
896 | Fuzzy => 0); |
d73e5302 |
897 | |
cf25bb62 |
898 | ## Any and All are all code points. |
99598c8c |
899 | my $Any = Table->New(Is => 'Any', |
900 | Desc => sprintf("[\\x{0000}-\\x{%X}]", |
901 | $LastUnicodeCodepoint), |
5beb625e |
902 | Fuzzy => 0); |
cf25bb62 |
903 | $Any->RawAppendRange(0, $LastUnicodeCodepoint); |
d73e5302 |
904 | |
5beb625e |
905 | New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 0); |
d73e5302 |
906 | |
a3a8c5f0 |
907 | ## |
908 | ## Build special properties for Perl's internal case-folding needs: |
909 | ## \p{_CaseIgnorable} |
910 | ## \p{_CanonDCIJ} |
911 | ## \p{_CombAbove} |
912 | ## _CombAbove was built above. Others are built here.... |
913 | ## |
914 | |
915 | ## \p{_CaseIgnorable} is [\p{Mn}\0x00AD\x2010] |
916 | New_Prop(Is => '_CaseIgnorable', |
917 | Table->Merge($Cat{Mn}, |
918 | 0x00AD, #SOFT HYPHEN |
919 | 0x2010), #HYPHEN |
99598c8c |
920 | Desc => '(for internal casefolding use)', |
921 | Fuzzy => 0); |
a3a8c5f0 |
922 | |
923 | |
924 | ## \p{_CanonDCIJ} is fairly complex... |
99598c8c |
925 | my $CanonCDIJ = Table->New(Is => '_CanonDCIJ', |
926 | Desc => '(for internal casefolding use)', |
927 | Fuzzy => 0); |
a3a8c5f0 |
928 | ## It contains the ASCII 'i' and 'j'.... |
929 | $CanonCDIJ->Append(0x0069); # ASCII ord("i") |
930 | $CanonCDIJ->Append(0x006A); # ASCII ord("j") |
931 | ## ...and any character with a decomposition that starts with either of |
932 | ## those code points, but only if the decomposition does not have any |
933 | ## combining character with the "ABOVE" canonical combining class. |
934 | for my $code (sort { $a <=> $b} keys %CodeToDeco) |
935 | { |
936 | ## Need to ensure that all decomposition characters do not have |
937 | ## a %HexCodeToComb in %AboveCombClasses. |
938 | my $want = 1; |
939 | for my $deco_hexcode (split / /, $CodeToDeco{$code}) |
940 | { |
941 | if (exists $_Above_HexCodes{$deco_hexcode}) { |
942 | ## one of the decmposition chars has an ABOVE combination |
943 | ## class, so we're not interested in this one |
944 | $want = 0; |
945 | last; |
946 | } |
947 | } |
948 | if ($want) { |
949 | $CanonCDIJ->Append($code); |
950 | } |
951 | } |
952 | |
953 | |
d73e5302 |
954 | |
cf25bb62 |
955 | ## |
956 | ## Now dump the files. |
957 | ## |
958 | $Name->Write("Name.pl"); |
12ac2576 |
959 | |
960 | # $Bidi->Write("Bidirectional.pl"); |
12ac2576 |
961 | for (keys %Bidi) { |
962 | $Bidi{$_}->Write( |
d07a55ed |
963 | ["lib","bc","$_.pl"], |
12ac2576 |
964 | "BidiClass category '$PropValueAlias{bc}{$_}'" |
965 | ); |
966 | } |
967 | |
cf25bb62 |
968 | $Comb->Write("CombiningClass.pl"); |
12ac2576 |
969 | for (keys %{ $PropValueAlias{ccc} }) { |
970 | my ($code, $name) = @{ $PropValueAlias{ccc}{$_} }; |
971 | (my $c = Table->New())->Append($code); |
972 | $c->Write( |
d07a55ed |
973 | ["lib","ccc","$_.pl"], |
12ac2576 |
974 | "CombiningClass category '$name'" |
975 | ); |
976 | } |
977 | |
cf25bb62 |
978 | $Deco->Write("Decomposition.pl"); |
12ac2576 |
979 | for (keys %DC) { |
980 | $DC{$_}->Write( |
d07a55ed |
981 | ["lib","dt","$_.pl"], |
12ac2576 |
982 | "DecompositionType category '$PropValueAlias{dt}{$_}'" |
983 | ); |
984 | } |
985 | |
986 | # $Number->Write("Number.pl"); |
12ac2576 |
987 | for (keys %Number) { |
988 | $Number{$_}->Write( |
d07a55ed |
989 | ["lib","nt","$_.pl"], |
12ac2576 |
990 | "NumericType category '$PropValueAlias{nt}{$_}'" |
991 | ); |
992 | } |
993 | |
994 | # $General->Write("Category.pl"); |
cf25bb62 |
995 | |
996 | for my $to (sort keys %To) { |
d07a55ed |
997 | $To{$to}->Write(["To","$to.pl"]); |
d73e5302 |
998 | } |
12ac2576 |
999 | |
1000 | for (keys %{ $PropValueAlias{gc} }) { |
1001 | New_Alias(Is => $PropValueAlias{gc}{$_}, SameAs => $_, Fuzzy => 1); |
1002 | } |
d73e5302 |
1003 | } |
1004 | |
cf25bb62 |
1005 | ## |
551b6b6f |
1006 | ## Process LineBreak.txt |
cf25bb62 |
1007 | ## |
551b6b6f |
1008 | sub LineBreak_Txt() |
cf25bb62 |
1009 | { |
551b6b6f |
1010 | if (not open IN, "LineBreak.txt") { |
1011 | die "$0: LineBreak.txt: $!\n"; |
cf25bb62 |
1012 | } |
d73e5302 |
1013 | |
cf25bb62 |
1014 | my $Lbrk = Table->New(); |
1015 | my %Lbrk; |
d73e5302 |
1016 | |
cf25bb62 |
1017 | while (<IN>) |
1018 | { |
1019 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/; |
d73e5302 |
1020 | |
cf25bb62 |
1021 | my ($first, $last, $lbrk) = (hex($1), hex($2||""), $3); |
d73e5302 |
1022 | |
cf25bb62 |
1023 | $Lbrk->Append($first, $lbrk); |
d73e5302 |
1024 | |
12ac2576 |
1025 | $Lbrk{$lbrk} ||= Table->New(); |
cf25bb62 |
1026 | $Lbrk{$lbrk}->Append($first); |
d73e5302 |
1027 | |
cf25bb62 |
1028 | if ($last) { |
1029 | $Lbrk->Extend($last); |
1030 | $Lbrk{$lbrk}->Extend($last); |
d73e5302 |
1031 | } |
1032 | } |
cf25bb62 |
1033 | close IN; |
d73e5302 |
1034 | |
12ac2576 |
1035 | # $Lbrk->Write("Lbrk.pl"); |
1036 | |
12ac2576 |
1037 | |
1038 | for (keys %Lbrk) { |
1039 | $Lbrk{$_}->Write( |
d07a55ed |
1040 | ["lib","lb","$_.pl"], |
12ac2576 |
1041 | "Linebreak category '$PropValueAlias{lb}{$_}'" |
1042 | ); |
1043 | } |
cf25bb62 |
1044 | } |
d73e5302 |
1045 | |
cf25bb62 |
1046 | ## |
551b6b6f |
1047 | ## Process ArabicShaping.txt. |
cf25bb62 |
1048 | ## |
551b6b6f |
1049 | sub ArabicShaping_txt() |
cf25bb62 |
1050 | { |
551b6b6f |
1051 | if (not open IN, "ArabicShaping.txt") { |
1052 | die "$0: ArabicShaping.txt: $!\n"; |
cf25bb62 |
1053 | } |
d73e5302 |
1054 | |
cf25bb62 |
1055 | my $ArabLink = Table->New(); |
1056 | my $ArabLinkGroup = Table->New(); |
d73e5302 |
1057 | |
12ac2576 |
1058 | my %JoinType; |
1059 | |
cf25bb62 |
1060 | while (<IN>) |
1061 | { |
1062 | next unless /^[0-9A-Fa-f]+;/; |
1063 | s/\s+$//; |
d73e5302 |
1064 | |
cf25bb62 |
1065 | my ($hexcode, $name, $link, $linkgroup) = split(/\s*;\s*/); |
1066 | my $code = hex($hexcode); |
1067 | $ArabLink->Append($code, $link); |
1068 | $ArabLinkGroup->Append($code, $linkgroup); |
12ac2576 |
1069 | |
1070 | $JoinType{$link} ||= Table->New(Is => "JoinType$link"); |
1071 | $JoinType{$link}->Append($code); |
d73e5302 |
1072 | } |
cf25bb62 |
1073 | close IN; |
1074 | |
12ac2576 |
1075 | # $ArabLink->Write("ArabLink.pl"); |
1076 | # $ArabLinkGroup->Write("ArabLnkGrp.pl"); |
1077 | |
12ac2576 |
1078 | |
1079 | for (keys %JoinType) { |
1080 | $JoinType{$_}->Write( |
d07a55ed |
1081 | ["lib","jt","$_.pl"], |
12ac2576 |
1082 | "JoiningType category '$PropValueAlias{jt}{$_}'" |
1083 | ); |
1084 | } |
1085 | } |
1086 | |
1087 | ## |
1088 | ## Process EastAsianWidth.txt. |
1089 | ## |
1090 | sub EastAsianWidth_txt() |
1091 | { |
1092 | if (not open IN, "EastAsianWidth.txt") { |
1093 | die "$0: EastAsianWidth.txt: $!\n"; |
1094 | } |
1095 | |
1096 | my %EAW; |
1097 | |
1098 | while (<IN>) |
1099 | { |
1100 | next unless /^[0-9A-Fa-f]+;/; |
1101 | s/#.*//; |
1102 | s/\s+$//; |
1103 | |
1104 | my ($hexcode, $pv) = split(/\s*;\s*/); |
1105 | my $code = hex($hexcode); |
1106 | $EAW{$pv} ||= Table->New(Is => "EastAsianWidth$pv"); |
1107 | $EAW{$pv}->Append($code); |
1108 | } |
1109 | close IN; |
1110 | |
12ac2576 |
1111 | |
1112 | for (keys %EAW) { |
1113 | $EAW{$_}->Write( |
d07a55ed |
1114 | ["lib","ea","$_.pl"], |
12ac2576 |
1115 | "EastAsianWidth category '$PropValueAlias{ea}{$_}'" |
1116 | ); |
1117 | } |
1118 | } |
1119 | |
1120 | ## |
1121 | ## Process HangulSyllableType.txt. |
1122 | ## |
1123 | sub HangulSyllableType_txt() |
1124 | { |
1125 | if (not open IN, "HangulSyllableType.txt") { |
1126 | die "$0: HangulSyllableType.txt: $!\n"; |
1127 | } |
1128 | |
1129 | my %HST; |
1130 | |
1131 | while (<IN>) |
1132 | { |
1133 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/; |
1134 | my ($first, $last, $pv) = (hex($1), hex($2||""), $3); |
1135 | |
1136 | $HST{$pv} ||= Table->New(Is => "HangulSyllableType$pv"); |
1137 | $HST{$pv}->Append($first); |
1138 | |
1139 | if ($last) { $HST{$pv}->Extend($last) } |
1140 | } |
1141 | close IN; |
1142 | |
12ac2576 |
1143 | for (keys %HST) { |
1144 | $HST{$_}->Write( |
d07a55ed |
1145 | ["lib","hst","$_.pl"], |
12ac2576 |
1146 | "HangulSyllableType category '$PropValueAlias{hst}{$_}'" |
1147 | ); |
1148 | } |
d73e5302 |
1149 | } |
1150 | |
cf25bb62 |
1151 | ## |
1152 | ## Process Jamo.txt. |
1153 | ## |
1154 | sub Jamo_txt() |
1155 | { |
1156 | if (not open IN, "Jamo.txt") { |
1157 | die "$0: Jamo.txt: $!\n"; |
1158 | } |
1159 | my $Short = Table->New(); |
d73e5302 |
1160 | |
cf25bb62 |
1161 | while (<IN>) |
1162 | { |
1163 | next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/; |
1164 | my ($code, $short) = (hex($1), $2); |
d73e5302 |
1165 | |
cf25bb62 |
1166 | $Short->Append($code, $short); |
d73e5302 |
1167 | } |
cf25bb62 |
1168 | close IN; |
12ac2576 |
1169 | # $Short->Write("JamoShort.pl"); |
d73e5302 |
1170 | } |
1171 | |
cf25bb62 |
1172 | ## |
1173 | ## Process Scripts.txt. |
1174 | ## |
1175 | sub Scripts_txt() |
1176 | { |
1177 | my @ScriptInfo; |
d73e5302 |
1178 | |
cf25bb62 |
1179 | if (not open(IN, "Scripts.txt")) { |
1180 | die "$0: Scripts.txt: $!\n"; |
1181 | } |
1182 | while (<IN>) { |
1183 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/; |
d73e5302 |
1184 | |
cf25bb62 |
1185 | # Wait until all the scripts have been read since |
1186 | # they are not listed in numeric order. |
1187 | push @ScriptInfo, [ hex($1), hex($2||""), $3 ]; |
1188 | } |
1189 | close IN; |
d73e5302 |
1190 | |
cf25bb62 |
1191 | # Now append the scripts properties in their code point order. |
d73e5302 |
1192 | |
cf25bb62 |
1193 | my %Script; |
1194 | my $Scripts = Table->New(); |
d73e5302 |
1195 | |
cf25bb62 |
1196 | for my $script (sort { $a->[0] <=> $b->[0] } @ScriptInfo) |
1197 | { |
1198 | my ($first, $last, $name) = @$script; |
1199 | $Scripts->Append($first, $name); |
d73e5302 |
1200 | |
99598c8c |
1201 | $Script{$name} ||= Table->New(Is => $name, |
1202 | Desc => "Script '$name'", |
1203 | Fuzzy => 1); |
cf25bb62 |
1204 | $Script{$name}->Append($first, $name); |
d73e5302 |
1205 | |
cf25bb62 |
1206 | if ($last) { |
1207 | $Scripts->Extend($last); |
1208 | $Script{$name}->Extend($last); |
1209 | } |
1210 | } |
d73e5302 |
1211 | |
12ac2576 |
1212 | # $Scripts->Write("Scripts.pl"); |
d73e5302 |
1213 | |
cf25bb62 |
1214 | ## Common is everything not explicitly assigned to a Script |
1215 | ## |
1216 | ## ***shouldn't this be intersected with \p{Assigned}? ****** |
1217 | ## |
99598c8c |
1218 | New_Prop(Is => 'Common', |
1219 | $Scripts->Invert, |
1220 | Desc => 'Pseudo-Script of codepoints not in other Unicode scripts', |
1221 | Fuzzy => 1); |
cf25bb62 |
1222 | } |
d73e5302 |
1223 | |
cf25bb62 |
1224 | ## |
1225 | ## Given a name like "Close Punctuation", return a regex (that when applied |
1226 | ## with /i) matches any valid form of that name (e.g. "ClosePunctuation", |
1227 | ## "Close-Punctuation", etc.) |
1228 | ## |
1229 | ## Accept any space, dash, or underbar where in the official name there is |
1230 | ## space or a dash (or underbar, but there never is). |
1231 | ## |
1232 | ## |
1233 | sub NameToRegex($) |
1234 | { |
1235 | my $Name = shift; |
1236 | $Name =~ s/[- _]/(?:[-_]|\\s+)?/g; |
1237 | return $Name; |
1238 | } |
d73e5302 |
1239 | |
cf25bb62 |
1240 | ## |
1241 | ## Process Blocks.txt. |
1242 | ## |
1243 | sub Blocks_txt() |
1244 | { |
1245 | my $Blocks = Table->New(); |
1246 | my %Blocks; |
d73e5302 |
1247 | |
cf25bb62 |
1248 | if (not open IN, "Blocks.txt") { |
1249 | die "$0: Blocks.txt: $!\n"; |
1250 | } |
d73e5302 |
1251 | |
cf25bb62 |
1252 | while (<IN>) |
1253 | { |
1254 | #next if not /Private Use$/; |
1255 | next if not /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/; |
d73e5302 |
1256 | |
cf25bb62 |
1257 | my ($first, $last, $name) = (hex($1), hex($2), $3); |
d73e5302 |
1258 | |
cf25bb62 |
1259 | $Blocks->Append($first, $name); |
76ccdbe2 |
1260 | |
99598c8c |
1261 | $Blocks{$name} ||= Table->New(In => $name, |
1262 | Desc => "Block '$name'", |
1263 | Fuzzy => 1); |
cf25bb62 |
1264 | $Blocks{$name}->Append($first, $name); |
76ccdbe2 |
1265 | |
cf25bb62 |
1266 | if ($last and $last != $first) { |
1267 | $Blocks->Extend($last); |
1268 | $Blocks{$name}->Extend($last); |
d73e5302 |
1269 | } |
d73e5302 |
1270 | } |
cf25bb62 |
1271 | close IN; |
1272 | |
12ac2576 |
1273 | # $Blocks->Write("Blocks.pl"); |
d73e5302 |
1274 | } |
1275 | |
cf25bb62 |
1276 | ## |
1277 | ## Read in the PropList.txt. It contains extended properties not |
551b6b6f |
1278 | ## listed in the UnicodeData.txt, such as 'Other_Alphabetic': |
cf25bb62 |
1279 | ## alphabetic but not of the general category L; many modifiers |
1280 | ## belong to this extended property category: while they are not |
1281 | ## alphabets, they are alphabetic in nature. |
1282 | ## |
1283 | sub PropList_txt() |
1284 | { |
1285 | my @PropInfo; |
1286 | |
1287 | if (not open IN, "PropList.txt") { |
1288 | die "$0: PropList.txt: $!\n"; |
1289 | } |
d73e5302 |
1290 | |
cf25bb62 |
1291 | while (<IN>) |
1292 | { |
1293 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/; |
d73e5302 |
1294 | |
cf25bb62 |
1295 | # Wait until all the extended properties have been read since |
1296 | # they are not listed in numeric order. |
1297 | push @PropInfo, [ hex($1), hex($2||""), $3 ]; |
1298 | } |
1299 | close IN; |
71d929cb |
1300 | |
cf25bb62 |
1301 | # Now append the extended properties in their code point order. |
1302 | my $Props = Table->New(); |
1303 | my %Prop; |
71d929cb |
1304 | |
cf25bb62 |
1305 | for my $prop (sort { $a->[0] <=> $b->[0] } @PropInfo) |
1306 | { |
1307 | my ($first, $last, $name) = @$prop; |
1308 | $Props->Append($first, $name); |
71d929cb |
1309 | |
99598c8c |
1310 | $Prop{$name} ||= Table->New(Is => $name, |
1311 | Desc => "Extended property '$name'", |
1312 | Fuzzy => 1); |
cf25bb62 |
1313 | $Prop{$name}->Append($first, $name); |
71d929cb |
1314 | |
cf25bb62 |
1315 | if ($last) { |
1316 | $Props->Extend($last); |
1317 | $Prop{$name}->Extend($last); |
1318 | } |
71d929cb |
1319 | } |
d73e5302 |
1320 | |
12ac2576 |
1321 | for (keys %Prop) { |
1322 | (my $file = $PA_reverse{$_}) =~ tr/_//d; |
8e4b7420 |
1323 | # XXX I'm assuming that the names from %Prop don't suffer 8.3 clashes. |
1324 | $BaseNames{lc $file}++; |
12ac2576 |
1325 | $Prop{$_}->Write( |
d07a55ed |
1326 | ["lib","gc_sc","$file.pl"], |
12ac2576 |
1327 | "Binary property '$_'" |
1328 | ); |
1329 | } |
1330 | |
cf25bb62 |
1331 | # Alphabetic is L and Other_Alphabetic. |
99598c8c |
1332 | New_Prop(Is => 'Alphabetic', |
cf25bb62 |
1333 | Table->Merge($Cat{L}, $Prop{Other_Alphabetic}), |
99598c8c |
1334 | Desc => '[\p{L}\p{OtherAlphabetic}]', # use canonical names here |
1335 | Fuzzy => 1); |
cf25bb62 |
1336 | |
1337 | # Lowercase is Ll and Other_Lowercase. |
99598c8c |
1338 | New_Prop(Is => 'Lowercase', |
cf25bb62 |
1339 | Table->Merge($Cat{Ll}, $Prop{Other_Lowercase}), |
99598c8c |
1340 | Desc => '[\p{Ll}\p{OtherLowercase}]', # use canonical names here |
1341 | Fuzzy => 1); |
cf25bb62 |
1342 | |
1343 | # Uppercase is Lu and Other_Uppercase. |
1344 | New_Prop(Is => 'Uppercase', |
1345 | Table->Merge($Cat{Lu}, $Prop{Other_Uppercase}), |
99598c8c |
1346 | Desc => '[\p{Lu}\p{Other_Uppercase}]', # use canonical names here |
1347 | Fuzzy => 1); |
cf25bb62 |
1348 | |
1349 | # Math is Sm and Other_Math. |
1350 | New_Prop(Is => 'Math', |
1351 | Table->Merge($Cat{Sm}, $Prop{Other_Math}), |
99598c8c |
1352 | Desc => '[\p{Sm}\p{OtherMath}]', # use canonical names here |
1353 | Fuzzy => 1); |
cf25bb62 |
1354 | |
1355 | # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl. |
1356 | New_Prop(Is => 'ID_Start', |
1357 | Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl]}), |
99598c8c |
1358 | Desc => '[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]', |
1359 | Fuzzy => 1); |
cf25bb62 |
1360 | |
1361 | # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc. |
1362 | New_Prop(Is => 'ID_Continue', |
1363 | Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl Mn Mc Nd Pc ]}), |
99598c8c |
1364 | Desc => '[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]', |
1365 | Fuzzy => 1); |
d73e5302 |
1366 | } |
1367 | |
5beb625e |
1368 | |
1369 | ## |
1370 | ## These are used in: |
1371 | ## MakePropTestScript() |
1372 | ## WriteAllMappings() |
1373 | ## for making the test script. |
1374 | ## |
1375 | my %FuzzyNameToTest; |
1376 | my %ExactNameToTest; |
1377 | |
1378 | |
1379 | ## This used only for making the test script |
1380 | sub GenTests($$$$) |
1381 | { |
1382 | my $FH = shift; |
1383 | my $Prop = shift; |
1384 | my $MatchCode = shift; |
1385 | my $FailCode = shift; |
1386 | |
1387 | if (defined $MatchCode) { |
1388 | printf $FH qq/Expect(1, "\\x{%04X}", '\\p{$Prop}' );\n/, $MatchCode; |
1389 | printf $FH qq/Expect(0, "\\x{%04X}", '\\p{^$Prop}');\n/, $MatchCode; |
1390 | printf $FH qq/Expect(0, "\\x{%04X}", '\\P{$Prop}' );\n/, $MatchCode; |
1391 | printf $FH qq/Expect(1, "\\x{%04X}", '\\P{^$Prop}');\n/, $MatchCode; |
1392 | } |
1393 | if (defined $FailCode) { |
1394 | printf $FH qq/Expect(0, "\\x{%04X}", '\\p{$Prop}' );\n/, $FailCode; |
1395 | printf $FH qq/Expect(1, "\\x{%04X}", '\\p{^$Prop}');\n/, $FailCode; |
1396 | printf $FH qq/Expect(1, "\\x{%04X}", '\\P{$Prop}' );\n/, $FailCode; |
1397 | printf $FH qq/Expect(0, "\\x{%04X}", '\\P{^$Prop}');\n/, $FailCode; |
1398 | } |
1399 | } |
1400 | |
1401 | ## This used only for making the test script |
1402 | sub ExpectError($$) |
1403 | { |
1404 | my $FH = shift; |
1405 | my $prop = shift; |
1406 | |
1407 | print $FH qq/Error('\\p{$prop}');\n/; |
1408 | print $FH qq/Error('\\P{$prop}');\n/; |
1409 | } |
1410 | |
1411 | ## This used only for making the test script |
1412 | my @GoodSeps = ( |
1413 | " ", |
1414 | "-", |
1415 | " \t ", |
1416 | "", |
1417 | "", |
1418 | "_", |
1419 | ); |
1420 | my @BadSeps = ( |
1421 | "--", |
1422 | "__", |
1423 | " _", |
1424 | "/" |
1425 | ); |
1426 | |
1427 | ## This used only for making the test script |
1428 | sub RandomlyFuzzifyName($;$) |
1429 | { |
1430 | my $Name = shift; |
1431 | my $WantError = shift; ## if true, make an error |
1432 | |
1433 | my @parts; |
1434 | for my $part (split /[-\s_]+/, $Name) |
1435 | { |
1436 | if (@parts) { |
1437 | if ($WantError and rand() < 0.3) { |
1438 | push @parts, $BadSeps[rand(@BadSeps)]; |
1439 | $WantError = 0; |
1440 | } else { |
1441 | push @parts, $GoodSeps[rand(@GoodSeps)]; |
1442 | } |
1443 | } |
1444 | my $switch = int rand(4); |
1445 | if ($switch == 0) { |
1446 | push @parts, uc $part; |
1447 | } elsif ($switch == 1) { |
1448 | push @parts, lc $part; |
1449 | } elsif ($switch == 2) { |
1450 | push @parts, ucfirst $part; |
1451 | } else { |
1452 | push @parts, $part; |
1453 | } |
1454 | } |
1455 | my $new = join('', @parts); |
1456 | |
1457 | if ($WantError) { |
1458 | if (rand() >= 0.5) { |
1459 | $new .= $BadSeps[rand(@BadSeps)]; |
1460 | } else { |
1461 | $new = $BadSeps[rand(@BadSeps)] . $new; |
1462 | } |
1463 | } |
1464 | return $new; |
1465 | } |
1466 | |
1467 | ## This used only for making the test script |
1468 | sub MakePropTestScript() |
1469 | { |
1470 | ## this written directly -- it's huge. |
d07a55ed |
1471 | force_unlink ("TestProp.pl"); |
5beb625e |
1472 | if (not open OUT, ">TestProp.pl") { |
1473 | die "$0: TestProp.pl: $!\n"; |
1474 | } |
1475 | print OUT <DATA>; |
1476 | |
1477 | while (my ($Name, $Table) = each %ExactNameToTest) |
1478 | { |
1479 | GenTests(*OUT, $Name, $Table->ValidCode, $Table->InvalidCode); |
1480 | ExpectError(*OUT, uc $Name) if uc $Name ne $Name; |
1481 | ExpectError(*OUT, lc $Name) if lc $Name ne $Name; |
1482 | } |
1483 | |
1484 | |
1485 | while (my ($Name, $Table) = each %FuzzyNameToTest) |
1486 | { |
1487 | my $Orig = $CanonicalToOrig{$Name}; |
1488 | my %Names = ( |
1489 | $Name => 1, |
1490 | $Orig => 1, |
1491 | RandomlyFuzzifyName($Orig) => 1 |
1492 | ); |
1493 | |
1494 | for my $N (keys %Names) { |
1495 | GenTests(*OUT, $N, $Table->ValidCode, $Table->InvalidCode); |
1496 | } |
1497 | |
1498 | ExpectError(*OUT, RandomlyFuzzifyName($Orig, 'ERROR')); |
1499 | } |
1500 | |
1501 | print OUT "Finished();\n"; |
1502 | close OUT; |
1503 | } |
1504 | |
1505 | |
1506 | ## |
1507 | ## These are used only in: |
1508 | ## RegisterFileForName() |
1509 | ## WriteAllMappings() |
1510 | ## |
1511 | my %Exact; ## will become %utf8::Exact; |
1512 | my %Canonical; ## will become %utf8::Canonical; |
1513 | my %CaComment; ## Comment for %Canonical entry of same key |
1514 | |
1515 | ## |
1516 | ## Given info about a name and a datafile that it should be associated with, |
1517 | ## register that assocation in %Exact and %Canonical. |
1518 | sub RegisterFileForName($$$$) |
1519 | { |
1520 | my $Type = shift; |
1521 | my $Name = shift; |
1522 | my $IsFuzzy = shift; |
1523 | my $filename = shift; |
1524 | |
1525 | ## |
1526 | ## Now in details for the mapping. $Type eq 'Is' has the |
1527 | ## Is removed, as it will be removed in utf8_heavy when this |
1528 | ## data is being checked. In keeps its "In", but a second |
1529 | ## sans-In record is written if it doesn't conflict with |
1530 | ## anything already there. |
1531 | ## |
1532 | if (not $IsFuzzy) |
1533 | { |
1534 | if ($Type eq 'Is') { |
1535 | die "oops[$Name]" if $Exact{$Name}; |
1536 | $Exact{$Name} = $filename; |
1537 | } else { |
1538 | die "oops[$Type$Name]" if $Exact{"$Type$Name"}; |
1539 | $Exact{"$Type$Name"} = $filename; |
1540 | $Exact{$Name} = $filename if not $Exact{$Name}; |
1541 | } |
1542 | } |
1543 | else |
1544 | { |
1545 | my $CName = lc $Name; |
1546 | if ($Type eq 'Is') { |
1547 | die "oops[$CName]" if $Canonical{$CName}; |
1548 | $Canonical{$CName} = $filename; |
1549 | $CaComment{$CName} = $Name if $Name =~ tr/A-Z// >= 2; |
1550 | } else { |
1551 | die "oops[$Type$CName]" if $Canonical{lc "$Type$CName"}; |
1552 | $Canonical{lc "$Type$CName"} = $filename; |
1553 | $CaComment{lc "$Type$CName"} = "$Type$Name"; |
1554 | if (not $Canonical{$CName}) { |
1555 | $Canonical{$CName} = $filename; |
1556 | $CaComment{$CName} = "$Type$Name"; |
1557 | } |
1558 | } |
1559 | } |
1560 | } |
1561 | |
cf25bb62 |
1562 | ## |
1563 | ## Writes the info accumulated in |
1564 | ## |
1565 | ## %TableInfo; |
1566 | ## %FuzzyNames; |
1567 | ## %AliasInfo; |
1568 | ## |
1569 | ## |
1570 | sub WriteAllMappings() |
1571 | { |
99598c8c |
1572 | my @MAP; |
1573 | |
5beb625e |
1574 | ## 'Is' *MUST* come first, so its names have precidence over 'In's |
1575 | for my $Type ('Is', 'In') |
1576 | { |
1577 | my %RawNameToFile; ## a per-$Type cache |
cf25bb62 |
1578 | |
5beb625e |
1579 | for my $Name (sort {length $a <=> length $b} keys %{$TableInfo{$Type}}) |
cf25bb62 |
1580 | { |
5beb625e |
1581 | ## Note: $Name is already canonical |
99598c8c |
1582 | my $Table = $TableInfo{$Type}->{$Name}; |
5beb625e |
1583 | my $IsFuzzy = $FuzzyNames{$Type}->{$Name}; |
99598c8c |
1584 | |
1585 | ## Need an 8.3 safe filename (which means "an 8 safe" $filename) |
5beb625e |
1586 | my $filename; |
cf25bb62 |
1587 | { |
5beb625e |
1588 | ## 'Is' items lose 'Is' from the basename. |
12ac2576 |
1589 | $filename = $Type eq 'Is' ? |
1590 | ($PVA_reverse{sc}{$Name} || $Name) : |
1591 | "$Type$Name"; |
5beb625e |
1592 | |
1593 | $filename =~ s/[^\w_]+/_/g; # "L&" -> "L_" |
1594 | substr($filename, 8) = '' if length($filename) > 8; |
1595 | |
1596 | ## |
1597 | ## Make sure the basename doesn't conflict with something we |
1598 | ## might have already written. If we have, say, |
1599 | ## InGreekExtended1 |
1600 | ## InGreekExtended2 |
1601 | ## they become |
1602 | ## InGreekE |
1603 | ## InGreek2 |
1604 | ## |
1605 | while (my $num = $BaseNames{lc $filename}++) |
1606 | { |
1607 | $num++; ## so basenames with numbers start with '2', which |
1608 | ## just looks more natural. |
1609 | ## Want to append $num, but if it'll make the basename longer |
1610 | ## than 8 characters, pre-truncate $filename so that the result |
1611 | ## is acceptable. |
1612 | my $delta = length($filename) + length($num) - 8; |
1613 | if ($delta > 0) { |
1614 | substr($filename, -$delta) = $num; |
1615 | } else { |
1616 | $filename .= $num; |
1617 | } |
99598c8c |
1618 | } |
5beb625e |
1619 | }; |
99598c8c |
1620 | |
1621 | ## |
1622 | ## Construct a nice comment to add to the file, and build data |
1623 | ## for the "./Properties" file along the way. |
1624 | ## |
1625 | my $Comment; |
1626 | { |
1627 | my $Desc = $TableDesc{$Type}->{$Name} || ""; |
1628 | ## get list of names this table is reference by |
1629 | my @Supported = $Name; |
1630 | while (my ($Orig, $Alias) = each %{ $AliasInfo{$Type} }) |
1631 | { |
1632 | if ($Orig eq $Name) { |
1633 | push @Supported, $Alias; |
1634 | } |
1635 | } |
1636 | |
1637 | my $TypeToShow = $Type eq 'Is' ? "" : $Type; |
1638 | my $OrigProp; |
1639 | |
1640 | $Comment = "This file supports:\n"; |
1641 | for my $N (@Supported) |
1642 | { |
1643 | my $IsFuzzy = $FuzzyNames{$Type}->{$N}; |
5beb625e |
1644 | my $Prop = "\\p{$TypeToShow$Name}"; |
99598c8c |
1645 | $OrigProp = $Prop if not $OrigProp; #cache for aliases |
1646 | if ($IsFuzzy) { |
1647 | $Comment .= "\t$Prop (and fuzzy permutations)\n"; |
1648 | } else { |
1649 | $Comment .= "\t$Prop\n"; |
1650 | } |
1651 | my $MyDesc = ($N eq $Name) ? $Desc : "Alias for $OrigProp ($Desc)"; |
1652 | |
1653 | push @MAP, sprintf("%s %-42s %s\n", |
1654 | $IsFuzzy ? '*' : ' ', $Prop, $MyDesc); |
1655 | } |
1656 | if ($Desc) { |
1657 | $Comment .= "\nMeaning: $Desc\n"; |
1658 | } |
1659 | |
1660 | } |
cf25bb62 |
1661 | ## |
1662 | ## Okay, write the file... |
1663 | ## |
d07a55ed |
1664 | $Table->Write(["lib","gc_sc","$filename.pl"], $Comment); |
99598c8c |
1665 | |
5beb625e |
1666 | ## and register it |
1667 | $RawNameToFile{$Name} = $filename; |
1668 | RegisterFileForName($Type => $Name, $IsFuzzy, $filename); |
cf25bb62 |
1669 | |
5beb625e |
1670 | if ($IsFuzzy) |
1671 | { |
1672 | my $CName = CanonicalName($Type . '_'. $Name); |
1673 | $FuzzyNameToTest{$Name} = $Table if !$FuzzyNameToTest{$Name}; |
1674 | $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName}; |
1675 | } else { |
1676 | $ExactNameToTest{$Name} = $Table; |
cf25bb62 |
1677 | } |
1678 | |
cf25bb62 |
1679 | } |
1680 | |
5beb625e |
1681 | ## Register aliase info |
1682 | for my $Name (sort {length $a <=> length $b} keys %{$AliasInfo{$Type}}) |
cf25bb62 |
1683 | { |
5beb625e |
1684 | my $Alias = $AliasInfo{$Type}->{$Name}; |
1685 | my $IsFuzzy = $FuzzyNames{$Type}->{$Alias}; |
1686 | my $filename = $RawNameToFile{$Name}; |
1687 | die "oops [$Alias]->[$Name]" if not $filename; |
1688 | RegisterFileForName($Type => $Alias, $IsFuzzy, $filename); |
1689 | |
1690 | my $Table = $TableInfo{$Type}->{$Name}; |
1691 | die "oops" if not $Table; |
1692 | if ($IsFuzzy) |
1693 | { |
1694 | my $CName = CanonicalName($Type .'_'. $Alias); |
1695 | $FuzzyNameToTest{$Alias} = $Table if !$FuzzyNameToTest{$Alias}; |
1696 | $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName}; |
1697 | } else { |
1698 | $ExactNameToTest{$Alias} = $Table; |
1699 | } |
cf25bb62 |
1700 | } |
5beb625e |
1701 | } |
cf25bb62 |
1702 | |
5beb625e |
1703 | ## |
1704 | ## Write out the property list |
1705 | ## |
1706 | { |
1707 | my @OUT = ( |
1708 | "##\n", |
1709 | "## This file created by $0\n", |
1710 | "## List of built-in \\p{...}/\\P{...} properties.\n", |
1711 | "##\n", |
1712 | "## '*' means name may be 'fuzzy'\n", |
1713 | "##\n\n", |
1714 | sort { substr($a,2) cmp substr($b, 2) } @MAP, |
1715 | ); |
1716 | WriteIfChanged('Properties', @OUT); |
1717 | } |
cf25bb62 |
1718 | |
5beb625e |
1719 | use Text::Tabs (); ## using this makes the files about half the size |
1720 | |
1721 | ## Write Exact.pl |
1722 | { |
1723 | my @OUT = ( |
1724 | $HEADER, |
1725 | "##\n", |
1726 | "## Data in this file used by ../utf8_heavy.pl\n", |
1727 | "##\n\n", |
12ac2576 |
1728 | "## Mapping from name to filename in ./lib/gc_sc\n", |
5beb625e |
1729 | "%utf8::Exact = (\n", |
1730 | ); |
cf25bb62 |
1731 | |
12ac2576 |
1732 | $Exact{InGreek} = 'InGreekA'; # this is evil kludge |
cf25bb62 |
1733 | for my $Name (sort keys %Exact) |
1734 | { |
1735 | my $File = $Exact{$Name}; |
5beb625e |
1736 | $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name "; |
1737 | my $Text = sprintf("%-15s => %s,\n", $Name, qq/'$File'/); |
1738 | push @OUT, Text::Tabs::unexpand($Text); |
cf25bb62 |
1739 | } |
5beb625e |
1740 | push @OUT, ");\n1;\n"; |
1741 | |
1742 | WriteIfChanged('Exact.pl', @OUT); |
1743 | } |
cf25bb62 |
1744 | |
5beb625e |
1745 | ## Write Canonical.pl |
1746 | { |
1747 | my @OUT = ( |
1748 | $HEADER, |
1749 | "##\n", |
1750 | "## Data in this file used by ../utf8_heavy.pl\n", |
1751 | "##\n\n", |
1752 | "## Mapping from lc(canonical name) to filename in ./lib\n", |
1753 | "%utf8::Canonical = (\n", |
1754 | ); |
1755 | my $Trail = ""; ## used just to keep the spacing pretty |
1756 | for my $Name (sort keys %Canonical) |
cf25bb62 |
1757 | { |
5beb625e |
1758 | my $File = $Canonical{$Name}; |
1759 | if ($CaComment{$Name}) { |
1760 | push @OUT, "\n" if not $Trail; |
1761 | push @OUT, " # $CaComment{$Name}\n"; |
1762 | $Trail = "\n"; |
1763 | } else { |
1764 | $Trail = ""; |
cf25bb62 |
1765 | } |
5beb625e |
1766 | $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name "; |
1767 | my $Text = sprintf(" %-41s => %s,\n$Trail", $Name, qq/'$File'/); |
1768 | push @OUT, Text::Tabs::unexpand($Text); |
cf25bb62 |
1769 | } |
5beb625e |
1770 | push @OUT, ");\n1\n"; |
1771 | WriteIfChanged('Canonical.pl', @OUT); |
d2d499f5 |
1772 | } |
5beb625e |
1773 | |
1774 | MakePropTestScript() if $MakeTestScript; |
d2d499f5 |
1775 | } |
1776 | |
5beb625e |
1777 | |
551b6b6f |
1778 | sub SpecialCasing_txt() |
cf25bb62 |
1779 | { |
1780 | # |
1781 | # Read in the special cases. |
1782 | # |
983ffd37 |
1783 | |
cf25bb62 |
1784 | my %CaseInfo; |
1785 | |
551b6b6f |
1786 | if (not open IN, "SpecialCasing.txt") { |
1787 | die "$0: SpecialCasing.txt: $!\n"; |
cf25bb62 |
1788 | } |
1789 | while (<IN>) { |
1790 | next unless /^[0-9A-Fa-f]+;/; |
1791 | s/\#.*//; |
1792 | s/\s+$//; |
1793 | |
1794 | my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/); |
1795 | |
1796 | if ($condition) { # not implemented yet |
1797 | print "# SKIPPING $_\n" if $Verbose; |
1798 | next; |
1799 | } |
1800 | |
1801 | # Wait until all the special cases have been read since |
1802 | # they are not listed in numeric order. |
1803 | my $ix = hex($code); |
a6da3000 |
1804 | push @{$CaseInfo{Lower}}, [ $ix, $code, $lower ] |
1805 | unless $code eq $lower; |
1806 | push @{$CaseInfo{Title}}, [ $ix, $code, $title ] |
1807 | unless $code eq $title; |
1808 | push @{$CaseInfo{Upper}}, [ $ix, $code, $upper ] |
1809 | unless $code eq $upper; |
cf25bb62 |
1810 | } |
1811 | close IN; |
1812 | |
1813 | # Now write out the special cases properties in their code point order. |
1814 | # Prepend them to the To/{Upper,Lower,Title}.pl. |
1815 | |
1816 | for my $case (qw(Lower Title Upper)) |
1817 | { |
1818 | my $NormalCase = do "To/$case.pl" || die "$0: $@\n"; |
cf25bb62 |
1819 | |
b08cf34e |
1820 | my @OUT = |
1821 | ( |
1822 | $HEADER, "\n", |
1823 | "# The key UTF-8 _bytes_, the value UTF-8 (speed hack)\n", |
1824 | "%utf8::ToSpec$case =\n(\n", |
1825 | ); |
cf25bb62 |
1826 | |
1827 | for my $prop (sort { $a->[0] <=> $b->[0] } @{$CaseInfo{$case}}) { |
1828 | my ($ix, $code, $to) = @$prop; |
1829 | my $tostr = |
1830 | join "", map { sprintf "\\x{%s}", $_ } split ' ', $to; |
b08cf34e |
1831 | push @OUT, sprintf qq["%s" => "$tostr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $ix))); |
5cb851a6 |
1832 | # Remove any single-character mappings for |
1833 | # the same character since we are going for |
1834 | # the special casing rules. |
1835 | $NormalCase =~ s/^$code\t\t\w+\n//m; |
cf25bb62 |
1836 | } |
5beb625e |
1837 | push @OUT, ( |
1838 | ");\n\n", |
1839 | "return <<'END';\n", |
1840 | $NormalCase, |
1841 | "END\n" |
1842 | ); |
d07a55ed |
1843 | WriteIfChanged(["To","$case.pl"], @OUT); |
d2d499f5 |
1844 | } |
d2d499f5 |
1845 | } |
1846 | |
c4051cc5 |
1847 | # |
1848 | # Read in the case foldings. |
1849 | # |
551b6b6f |
1850 | # We will do full case folding, C + F + I (see CaseFolding.txt). |
c4051cc5 |
1851 | # |
551b6b6f |
1852 | sub CaseFolding_txt() |
cf25bb62 |
1853 | { |
551b6b6f |
1854 | if (not open IN, "CaseFolding.txt") { |
1855 | die "$0: CaseFolding.txt: $!\n"; |
cf25bb62 |
1856 | } |
c4051cc5 |
1857 | |
cf25bb62 |
1858 | my $Fold = Table->New(); |
c4051cc5 |
1859 | my %Fold; |
1860 | |
cf25bb62 |
1861 | while (<IN>) { |
254ba52a |
1862 | # Skip status 'S', simple case folding |
c4051cc5 |
1863 | next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/; |
1864 | |
cf25bb62 |
1865 | my ($code, $status, $fold) = (hex($1), $2, $3); |
c4051cc5 |
1866 | |
1867 | if ($status eq 'C') { # Common: one-to-one folding |
254ba52a |
1868 | # No append() since several codes may fold into one. |
cf25bb62 |
1869 | $Fold->RawAppendRange($code, $code, $fold); |
c4051cc5 |
1870 | } else { # F: full, or I: dotted uppercase I -> dotless lowercase I |
cf25bb62 |
1871 | $Fold{$code} = $fold; |
c4051cc5 |
1872 | } |
1873 | } |
cf25bb62 |
1874 | close IN; |
c4051cc5 |
1875 | |
cf25bb62 |
1876 | $Fold->Write("To/Fold.pl"); |
c4051cc5 |
1877 | |
1878 | # |
1879 | # Prepend the special foldings to the common foldings. |
1880 | # |
c4051cc5 |
1881 | my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n"; |
5beb625e |
1882 | |
b08cf34e |
1883 | my @OUT = |
1884 | ( |
1885 | $HEADER, "\n", |
1886 | "# The ke UTF-8 _bytes_, the value UTF-8 (speed hack)\n", |
1887 | "%utf8::ToSpecFold =\n(\n", |
1888 | ); |
cf25bb62 |
1889 | for my $code (sort { $a <=> $b } keys %Fold) { |
1890 | my $foldstr = |
1891 | join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code}; |
b08cf34e |
1892 | push @OUT, sprintf qq["%s" => "$foldstr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $code))); |
c4051cc5 |
1893 | } |
5beb625e |
1894 | push @OUT, ( |
1895 | ");\n\n", |
1896 | "return <<'END';\n", |
1897 | $CommonFold, |
1898 | "END\n", |
1899 | ); |
1900 | |
d07a55ed |
1901 | WriteIfChanged(["To","Fold.pl"], @OUT); |
c4051cc5 |
1902 | } |
1903 | |
cf25bb62 |
1904 | ## Do it.... |
1905 | |
12ac2576 |
1906 | Build_Aliases(); |
44da8cae |
1907 | UnicodeData_Txt(); |
cf25bb62 |
1908 | PropList_txt(); |
1909 | |
1910 | Scripts_txt(); |
1911 | Blocks_txt(); |
1912 | |
5beb625e |
1913 | WriteAllMappings(); |
1914 | |
551b6b6f |
1915 | LineBreak_Txt(); |
1916 | ArabicShaping_txt(); |
12ac2576 |
1917 | EastAsianWidth_txt(); |
1918 | HangulSyllableType_txt(); |
cf25bb62 |
1919 | Jamo_txt(); |
551b6b6f |
1920 | SpecialCasing_txt(); |
1921 | CaseFolding_txt(); |
cf25bb62 |
1922 | |
5beb625e |
1923 | exit(0); |
cf25bb62 |
1924 | |
5beb625e |
1925 | ## TRAILING CODE IS USED BY MakePropTestScript() |
1926 | __DATA__ |
1927 | use strict; |
1928 | use warnings; |
1929 | |
1930 | my $Tests = 0; |
1931 | my $Fails = 0; |
cf25bb62 |
1932 | |
5beb625e |
1933 | sub Expect($$$) |
1934 | { |
1935 | my $Expect = shift; |
1936 | my $String = shift; |
1937 | my $Regex = shift; |
1938 | my $Line = (caller)[2]; |
1939 | |
1940 | $Tests++; |
1941 | my $RegObj; |
1942 | my $result = eval { |
1943 | $RegObj = qr/$Regex/; |
1944 | $String =~ $RegObj ? 1 : 0 |
1945 | }; |
1946 | |
1947 | if (not defined $result) { |
1948 | print "couldn't compile /$Regex/ on $0 line $Line: $@\n"; |
1949 | $Fails++; |
1950 | } elsif ($result ^ $Expect) { |
1951 | print "bad result (expected $Expect) on $0 line $Line: $@\n"; |
1952 | $Fails++; |
1953 | } |
1954 | } |
d73e5302 |
1955 | |
5beb625e |
1956 | sub Error($) |
1957 | { |
1958 | my $Regex = shift; |
1959 | $Tests++; |
1960 | if (eval { 'x' =~ qr/$Regex/; 1 }) { |
1961 | $Fails++; |
1962 | my $Line = (caller)[2]; |
1963 | print "expected error for /$Regex/ on $0 line $Line: $@\n"; |
1964 | } |
1965 | } |
1966 | |
1967 | sub Finished() |
1968 | { |
1969 | if ($Fails == 0) { |
1970 | print "All $Tests tests passed.\n"; |
1971 | exit(0); |
1972 | } else { |
1973 | print "$Tests tests, $Fails failed!\n"; |
1974 | exit(-1); |
1975 | } |
1976 | } |