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