UCD.pm: if at first you don't succeed, croak?
[p5sagit/p5-mst-13.2.git] / lib / unicode / mktables.PL
1 #!../../miniperl
2
3 use bytes;
4
5 $UnicodeData = "Unicode.txt";
6 $SyllableData = "syllables.txt";
7 $PropData = "PropList.txt";
8
9
10 # Note: we try to keep filenames unique within first 8 chars.  Using
11 # subdirectories for the following helps.
12 mkdir "In", 0755;
13 mkdir "Is", 0755;
14 mkdir "To", 0755;
15
16 @todo = (
17 # typical
18
19     # 005F: SPACING UNDERSCROE
20     ['IsWord',   '$cat =~ /^[LMN]/ or $code eq "005F"', ''],
21     ['IsAlnum',  '$cat =~ /^[LMN]/',    ''],
22     ['IsAlpha',  '$cat =~ /^[LM]/',     ''],
23     # 0009: HORIZONTAL TABULATION
24     # 000A: LINE FEED
25     # 000B: VERTICAL TABULATION
26     # 000C: FORM FEED
27     # 000D: CARRIAGE RETURN
28     # 0020: SPACE
29     ['IsSpace',  '$cat  =~ /^Z/ ||
30                   $code =~ /^(0009|000A|000B|000C|000D)$/',     ''],
31     ['IsSpacePerl',
32                  '$cat  =~ /^Z/ ||
33                   $code =~ /^(0009|000A|000C|000D)$/',          ''],
34     ['IsBlank',  '$code =~ /^(0020|0009)$/ ||
35                   $cat  =~ /^Z[^lp]$/', ''],
36     ['IsDigit',  '$cat =~ /^Nd$/',      ''],
37     ['IsUpper',  '$cat =~ /^L[ut]$/',   ''],
38     ['IsLower',  '$cat =~ /^Ll$/',      ''],
39     ['IsASCII',  '$code le "007f"',     ''],
40     ['IsCntrl',  '$cat =~ /^C/',        ''],
41     ['IsGraph',  '$cat =~ /^([LMNPS]|Co)/',     ''],
42     ['IsPrint',  '$cat =~ /^([LMNPS]|Co|Zs)/',  ''],
43     ['IsPunct',  '$cat =~ /^P/',        ''],
44     # 003[0-9]: DIGIT ZERO..NINE, 00[46][1-6]: A..F, a..f
45     ['IsXDigit', '$code =~ /^00(3[0-9]|[46][1-6])$/',   ''],
46     ['ToUpper',  '$up',                 '$up'],
47     ['ToLower',  '$down',               '$down'],
48     ['ToTitle',  '$title',              '$title'],
49     ['ToDigit',  '$dec ne ""',          '$dec'],
50
51 # Name
52
53     ['Name',    '$name',                '$name'],
54
55 # Category
56
57     ['Category', '$cat',                '$cat'],
58
59 # Normative
60
61     ['IsM',     '$cat =~ /^M/',         ''],    # Mark
62     ['IsMn',    '$cat eq "Mn"',         ''],    # Mark, Non-Spacing 
63     ['IsMc',    '$cat eq "Mc"',         ''],    # Mark, Combining
64     ['IsMe',    '$cat eq "Me"',         ''],    # Mark, Enclosing
65
66     ['IsN',     '$cat =~ /^N/',         ''],    # Number
67     ['IsNd',    '$cat eq "Nd"',         ''],    # Number, Decimal Digit
68     ['IsNo',    '$cat eq "No"',         ''],    # Number, Other
69     ['IsNl',    '$cat eq "Nl"',         ''],    # Number, Letter
70
71     ['IsZ',     '$cat =~ /^Z/',         ''],    # Separator
72     ['IsZs',    '$cat eq "Zs"',         ''],    # Separator, Space
73     ['IsZl',    '$cat eq "Zl"',         ''],    # Separator, Line
74     ['IsZp',    '$cat eq "Zp"',         ''],    # Separator, Paragraph
75
76     ['IsC',     '$cat =~ /^C/',         ''],    # Crazy
77     ['IsCc',    '$cat eq "Cc"',         ''],    # Other, Control or Format
78     ['IsCo',    '$cat eq "Co"',         ''],    # Other, Private Use
79     ['IsCn',    '$cat eq "Cn"',         ''],    # Other, Not Assigned
80     ['IsCf',    '$cat eq "Cf"',         ''],    # Other, Format
81     ['IsCs',    '$cat eq "Cs"',         ''],    # Other, Surrogate
82     ['IsCn',    'Unassigned Code Value',$PropData],     # Other, Not Assigned
83  
84 # Informative
85
86     ['IsL',     '$cat =~ /^L/',         ''],    # Letter
87     ['IsLu',    '$cat eq "Lu"',         ''],    # Letter, Uppercase
88     ['IsLl',    '$cat eq "Ll"',         ''],    # Letter, Lowercase
89     ['IsLt',    '$cat eq "Lt"',         ''],    # Letter, Titlecase 
90     ['IsLm',    '$cat eq "Lm"',         ''],    # Letter, Modifier
91     ['IsLo',    '$cat eq "Lo"',         ''],    # Letter, Other 
92
93     ['IsP',     '$cat =~ /^P/',         ''],    # Punctuation
94     ['IsPd',    '$cat eq "Pd"',         ''],    # Punctuation, Dash
95     ['IsPs',    '$cat eq "Ps"',         ''],    # Punctuation, Open
96     ['IsPe',    '$cat eq "Pe"',         ''],    # Punctuation, Close
97     ['IsPo',    '$cat eq "Po"',         ''],    # Punctuation, Other
98     ['IsPc',    '$cat eq "Pc"',         ''],    # Punctuation, Connector
99     ['IsPi',    '$cat eq "Pi"',         ''],    # Punctuation, Initial quote
100     ['IsPf',    '$cat eq "Pf"',         ''],    # Punctuation, Final quote
101
102     ['IsS',     '$cat =~ /^S/',         ''],    # Symbol
103     ['IsSm',    '$cat eq "Sm"',         ''],    # Symbol, Math
104     ['IsSk',    '$cat eq "Sk"',         ''],    # Symbol, Modifier
105     ['IsSc',    '$cat eq "Sc"',         ''],    # Symbol, Currency
106     ['IsSo',    '$cat eq "So"',         ''],    # Symbol, Other
107
108 # Combining class
109     ['CombiningClass', '$comb',         '$comb'],
110
111 # BIDIRECTIONAL PROPERTIES
112  
113     ['Bidirectional', '$bid',           '$bid'],
114
115 # Strong types:
116
117     ['IsBidiL', '$bid eq "L"',          ''],    # Left-Right; Most alphabetic,
118                                                 # syllabic, and logographic
119                                                 # characters (e.g., CJK
120                                                 # ideographs)
121     ['IsBidiR', '$bid eq "R"',          ''],    # Right-Left; Arabic, Hebrew,
122                                                 # and punctuation specific to
123                                                 # those scripts
124
125     ['IsBidiLRE', '$bid eq "LRE"',       ''],    # Left-to-Right Embedding
126     ['IsBidiLRO', '$bid eq "LRO"',       ''],    # Left-to-Right Override
127     ['IsBidiAL', '$bid eq "AL"',         ''],    # Right-to-Left Arabic
128     ['IsBidiRLE', '$bid eq "RLE"',       ''],    # Right-to-Left Embedding
129     ['IsBidiRLO', '$bid eq "RLO"',       ''],    # Right-to-Left Override
130     ['IsBidiPDF', '$bid eq "PDF"',       ''],    # Pop Directional Format
131     ['IsBidiNSM', '$bid eq "NSM"',       ''],    # Non-Spacing Mark
132     ['IsBidiBN', '$bid eq "BN"',         ''],    # Boundary Neutral
133
134 # Weak types:
135
136     ['IsBidiEN','$bid eq "EN"',         ''],    # European Number
137     ['IsBidiES','$bid eq "ES"',         ''],    # European Number Separator
138     ['IsBidiET','$bid eq "ET"',         ''],    # European Number Terminator
139     ['IsBidiAN','$bid eq "AN"',         ''],    # Arabic Number
140     ['IsBidiCS','$bid eq "CS"',         ''],    # Common Number Separator
141
142 # Separators:
143
144     ['IsBidiB', '$bid eq "B"',          ''],    # Block Separator
145     ['IsBidiS', '$bid eq "S"',          ''],    # Segment Separator
146
147 # Neutrals:
148
149     ['IsBidiWS','$bid eq "WS"',         ''],    # Whitespace
150     ['IsBidiON','$bid eq "ON"',         ''],    # Other Neutrals ; All other
151                                                 # characters: punctuation,
152                                                 # symbols
153
154 # Decomposition
155
156     ['Decomposition',   '$decomp',      '$decomp'],
157     ['IsDecoCanon',     '$decomp && $decomp !~ /^</',   ''],
158     ['IsDecoCompat',    '$decomp =~ /^</',              ''],
159     ['IsDCfont',        '$decomp =~ /^<font>/',         ''],
160     ['IsDCnoBreak',     '$decomp =~ /^<noBreak>/',      ''],
161     ['IsDCinitial',     '$decomp =~ /^<initial>/',      ''],
162     ['IsDCmedial',      '$decomp =~ /^<medial>/',       ''],
163     ['IsDCfinal',       '$decomp =~ /^<final>/',        ''],
164     ['IsDCisolated',    '$decomp =~ /^<isolated>/',     ''],
165     ['IsDCcircle',      '$decomp =~ /^<circle>/',       ''],
166     ['IsDCsuper',       '$decomp =~ /^<super>/',        ''],
167     ['IsDCsub',         '$decomp =~ /^<sub>/',          ''],
168     ['IsDCvertical',    '$decomp =~ /^<vertical>/',     ''],
169     ['IsDCwide',        '$decomp =~ /^<wide>/',         ''],
170     ['IsDCnarrow',      '$decomp =~ /^<narrow>/',       ''],
171     ['IsDCsmall',       '$decomp =~ /^<small>/',        ''],
172     ['IsDCsquare',      '$decomp =~ /^<square>/',       ''],
173     ['IsDCfraction',    '$decomp =~ /^<fraction>/',     ''],
174     ['IsDCcompat',      '$decomp =~ /^<compat>/',       ''],
175
176 # Number
177
178     ['Number',  '$num ne ""',           '$num'],
179
180 # Mirrored
181
182     ['IsMirrored', '$mir eq "Y"',       ''],
183
184 # Arabic
185
186     ['ArabLink',        '1',            '$link'],
187     ['ArabLnkGrp',      '1',            '$linkgroup'],
188
189 # Jamo
190
191     ['JamoShort',       '1',            '$short'],
192
193 # Syllables
194
195     syllable_defs(),
196
197 # Line break properties - Normative
198
199     ['IsLbrkBK','$brk eq "BK"',         ''],    # Mandatory Break
200     ['IsLbrkCR','$brk eq "CR"',         ''],    # Carriage Return
201     ['IsLbrkLF','$brk eq "LF"',         ''],    # Line Feed
202     ['IsLbrkCM','$brk eq "CM"',         ''],    # Attached Characters and Combining Marks
203     ['IsLbrkSG','$brk eq "SG"',         ''],    # Surrogates
204     ['IsLbrkGL','$brk eq "GL"',         ''],    # Non-breaking (Glue)
205     ['IsLbrkCB','$brk eq "CB"',         ''],    # Contingent Break Opportunity
206     ['IsLbrkSP','$brk eq "SP"',         ''],    # Space
207     ['IsLbrkZW','$brk eq "ZW"',         ''],    # Zero Width Space
208
209 # Line break properties - Informative
210     ['IsLbrkXX','$brk eq "XX"',         ''],    # Unknown
211     ['IsLbrkOP','$brk eq "OP"',         ''],    # Opening Punctuation
212     ['IsLbrkCL','$brk eq "CL"',         ''],    # Closing Punctuation
213     ['IsLbrkQU','$brk eq "QU"',         ''],    # Ambiguous Quotation
214     ['IsLbrkNS','$brk eq "NS"',         ''],    # Non Starter
215     ['IsLbrkEX','$brk eq "EX"',         ''],    # Exclamation/Interrogation
216     ['IsLbrkSY','$brk eq "SY"',         ''],    # Symbols Allowing Breaks
217     ['IsLbrkIS','$brk eq "IS"',         ''],    # Infix Separator (Numeric)
218     ['IsLbrkPR','$brk eq "PR"',         ''],    # Prefix (Numeric)
219     ['IsLbrkPO','$brk eq "PO"',         ''],    # Postfix (Numeric)
220     ['IsLbrkNU','$brk eq "NU"',         ''],    # Numeric
221     ['IsLbrkAL','$brk eq "AL"',         ''],    # Ordinary Alphabetic and Symbol Characters
222     ['IsLbrkID','$brk eq "ID"',         ''],    # Ideographic
223     ['IsLbrkIN','$brk eq "IN"',         ''],    # Inseparable
224     ['IsLbrkHY','$brk eq "HY"',         ''],    # Hyphen
225     ['IsLbrkBB','$brk eq "BB"',         ''],    # Break Opportunity Before
226     ['IsLbrkBA','$brk eq "BA"',         ''],    # Break Opportunity After
227     ['IsLbrkSA','$brk eq "SA"',         ''],    # Complex Context (South East Asian)
228     ['IsLbrkAI','$brk eq "AI"',         ''],    # Ambiguous (Alphabetic or Ideographic)
229     ['IsLbrkB2','$brk eq "B2"',         ''],    # Break Opportunity Before and After
230 );
231
232 # This is not written for speed...
233
234 my %InId;
235 my $InId = 0;
236
237 foreach $file (@todo) {
238     my ($table, $wanted, $val) = @$file;
239     next if @ARGV and not grep { $_ eq $table } @ARGV;
240     print $table, "\n";
241     $table =~ s/\W+//g;
242     if ($table =~ /^In(.+)/) {
243         my $id;
244         unless (exists $InId{$1}) {
245             $InId{$1} = $InId++;
246         }
247         $id = $InId{$1};
248         open(OUT, ">In/$id.pl") or die "Can't create In/$id.pl: $!\n";
249         print OUT "# In/$id.pl $1\n";
250     }
251     elsif ($table =~ /^(Is|To)(.+)/) {
252         open(OUT, ">$1/$2.pl") or die "Can't create $1/$2.pl: $!\n";
253     }
254     else {
255         open(OUT, ">$table.pl") or die "Can't create $table.pl: $!\n";
256     }
257     print OUT <<EOH;
258 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
259 # This file is built by $0 from e.g. $UnicodeData.
260 # Any changes made here will be lost!
261 EOH
262     print OUT <<"END";
263 return <<'END';
264 END
265     print OUT proplist($table, $wanted, $val);
266     print OUT "END\n";
267     close OUT;
268 }
269
270 # Must treat blocks specially.
271
272 exit if @ARGV and not grep { $_ eq Block } @ARGV;
273 print "Blocks\n";
274 open(UD, 'Blocks.txt') or die "Can't open Blocks.txt: $!\n";
275 open(OUT, ">Blocks.pl") or die "Can't create Blocks.pl: $!\n";
276 print OUT <<EOH;
277 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
278 # This file is built by $0 from e.g. $UnicodeData.
279 # Any changes made here will be lost!
280 EOH
281 print OUT <<"END";
282 return <<'END';
283 END
284
285 while (<UD>) {
286     next if /^#/;
287     next if /^$/;
288     chomp;
289     ($code, $last, $name) = /^([0-9a-f]+)\.\.([0-9a-f]+); (.+)/i;
290     if ($name) {
291         print OUT "$code        $last   $name\n";
292         $name =~ s/\W+//g;
293         my $id;
294         unless (exists $InId{$name}) {
295             $InId{$name} = $InId++;
296         }
297         $id = $InId{$name};
298         open(BLOCK, ">In/$id.pl");
299         print OUT "# In/$id.pl $name\n";
300         print BLOCK <<EOH;
301 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
302 # This file is built by $0 from e.g. $UnicodeData.
303 # Any changes made here will be lost!
304 EOH
305         print BLOCK <<"END2";
306 return <<'END';
307 $code   $last
308 END
309 END2
310         close BLOCK;
311     }
312 }
313
314 print OUT "END\n";
315 close OUT;
316
317 open(INID, ">In.pl");
318
319 print INID <<EOH;
320 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
321 # This file is built by $0 from e.g. $UnicodeData.
322 # Any changes made here will be lost!
323 %utf8::In = (
324 EOH
325
326 # Order doesn't matter but let's prettyprint anyway.
327 foreach my $in (sort { $InId{$a} <=> $InId{$b} } keys %InId) {
328     printf INID "%-40s => %3d,\n", "'$in'", $InId{$in};
329 }
330
331 print INID ");\n";
332
333 close(INID);
334
335 ##################################################
336
337 sub proplist {
338     my ($table, $wanted, $val) = @_;
339     my @wanted;
340     my $out;
341     my $split;
342
343     return listFromPropFile($wanted) if $val eq $PropData;
344
345     if ($table =~ /^Arab/) {
346         open(UD, "ArabShap.txt") or warn "Can't open $table: $!";
347
348         $split = '($code, $name, $link, $linkgroup) = split(/; */);';
349     }
350     elsif ($table =~ /^Jamo/) {
351         open(UD, "Jamo.txt") or warn "Can't open $table: $!";
352
353         $split = '($code, $short, $name) = split(/; */); $code =~ s/^U\+//;';
354     }
355     elsif ($table =~ /^IsSyl/) {
356         open(UD, $SyllableData) or warn "Can't open $table: $!";
357
358         $split = '($code, $short, $syl) = split(/; */); $code =~ s/^U\+//;';
359     }
360     elsif ($table =~ /^IsLbrk/) {
361         open(UD, "LineBrk.txt") or warn "Can't open $table: $!";
362
363         $split = '($code, $brk, $name) = /^([0-9a-f]+);(\w+) # (.+)/i;';
364     }
365     else {
366         open(UD, $UnicodeData) or warn "Can't open $UnicodeData: $!";
367
368         $split = '($code, $name, $cat, $comb, $bid, $decomp, $dec, $dig, $num, $mir, $uni1,
369                 $comment, $up, $down, $title) = split(/;/);';
370     }
371
372     if ($table =~ /^(?:To|Is)[A-Z]/) {
373         eval <<"END";
374             while (<UD>) {
375                 next if /^#/;
376                 next if /^\\s/;
377                 s/\\s+\$//;
378                 $split
379                 if ($wanted) {
380                     push(\@wanted, [hex \$code, hex $val, \$name =~ /, First>\$/]);
381                 }
382             }
383 END
384         die $@ if $@;
385
386         while (@wanted) {
387             $beg = shift @wanted;
388             $last = $beg;
389             while (@wanted and $wanted[0]->[0] == $last->[0] + 1 and
390                 (not $val or $wanted[0]->[1] == $last->[1] + 1)) {
391                     $last = shift @wanted;
392             }
393             $out .= sprintf "%04x", $beg->[0];
394             if ($beg->[2]) {
395                 $last = shift @wanted;
396             }
397             if ($beg == $last) {
398                 $out .= "\t";
399             }
400             else {
401                 $out .= sprintf "\t%04x", $last->[0];
402             }
403             $out .= sprintf "\t%04x", $beg->[1] if $val;
404             $out .= "\n";
405         }
406     }
407     else {
408         eval <<"END";
409             while (<UD>) {
410                 next if /^#/;
411                 next if /^\\s*\$/;
412                 chop;
413                 $split
414                 if ($wanted) {
415                     push(\@wanted, [hex \$code, $val, \$name =~ /, First>\$/]);
416                 }
417             }
418 END
419         die $@ if $@;
420
421         while (@wanted) {
422             $beg = shift @wanted;
423             $last = $beg;
424             while (@wanted and $wanted[0]->[0] == $last->[0] + 1 and
425                 ($wanted[0]->[1] eq $last->[1])) {
426                     $last = shift @wanted;
427             }
428             $out .= sprintf "%04x", $beg->[0];
429             if ($beg->[2]) {
430                 $last = shift @wanted;
431             }
432             if ($beg == $last) {
433                 $out .= "\t";
434             }
435             else {
436                 $out .= sprintf "\t%04x", $last->[0];
437             }
438             $out .= sprintf "\t%s\n", $beg->[1];
439         }
440     }
441     $out;
442 }
443
444 sub listFromPropFile {
445     my ($wanted) = @_;
446     my $out;
447
448     open (UD, $PropData) or die "Can't open $PropData: $!\n";
449     local($/) = "\n" . '*' x 43 . "\n\nProperty dump for:";   # not 42?
450
451     <UD>;
452     while (<UD>) {
453         chomp;
454         if (s/0x[\d\w]+\s+\((.*?)\)// and $wanted eq $1) {
455             s/\(\d+ chars\)//g;
456             s/^\s+//mg;
457             s/\s+$//mg;
458             s/\.\./\t/g;
459             $out = lc $_;
460             last;
461         }
462     }
463     close (UD);
464     "$out\n";
465 }
466
467 sub syllable_defs {
468     my @defs;
469     my %seen;
470
471     open (SD, $SyllableData) or die "Can't open $SyllableData: $!\n";
472     while (<SD>) {
473         next if /^\s*(#|$)/;
474         s/\s+$//;
475         ($code, $name, $syl) = split /; */;
476         next unless $syl;
477         push (@defs, ["IsSyl$syl", qq{\$syl eq "$syl"}, ''])
478                                                      unless $seen{$syl}++;
479     }
480     close (SD);
481     return (@defs);
482 }
483
484 # eof