This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regen/mk_PL_charclass.pl: Update to use EBCDIC utilities
[perl5.git] / regen / mk_PL_charclass.pl
1 #!perl -w
2 use v5.15.8;
3 use strict;
4 use warnings;
5 require 'regen/regen_lib.pl';
6 require 'regen/charset_translations.pl';
7
8 # This program outputs l1_charclass_tab.h, which defines the guts of the
9 # PL_charclass table.  Each line is a bit map of properties that the Unicode
10 # code point at the corresponding position in the table array has.  The first
11 # line corresponds to code point U+0000, NULL, the last line to U+00FF.  For
12 # an application to see if the code point "i" has a particular property, it
13 # just does
14 #    'PL_charclass[i] & BIT'
15 # The bit names are of the form '_CC_property_suffix', where 'CC' stands for
16 # character class, and 'property' is the corresponding property, and 'suffix'
17 # is one of '_A' to mean the property is true only if the corresponding code
18 # point is ASCII, and '_L1' means that the range includes any Latin1
19 # character (ISO-8859-1 including the C0 and C1 controls).  A property without
20 # these suffixes does not have different forms for both ranges.
21
22 # This program need be run only when adding new properties to it, or upon a
23 # new Unicode release, to make sure things haven't been changed by it.
24
25 my @properties = qw(
26     NONLATIN1_FOLD
27     ALPHANUMERIC
28     ALPHA
29     ASCII
30     BLANK
31     CASED
32     CHARNAME_CONT
33     CNTRL
34     DIGIT
35     GRAPH
36     IDFIRST
37     LOWER
38     NON_FINAL_FOLD
39     PRINT
40     PSXSPC
41     PUNCT
42     QUOTEMETA
43     SPACE
44     UPPER
45     WORDCHAR
46     XDIGIT
47     VERTSPACE
48     IS_IN_SOME_FOLD
49     BACKSLASH_FOO_LBRACE_IS_META
50 );
51
52 # Read in the case fold mappings.
53 my %folded_closure;
54 my @hex_non_final_folds;
55 my @folds;
56 use Unicode::UCD;
57
58 BEGIN { # Have to do this at compile time because using user-defined \p{property}
59
60     # Use the Unicode data file if we are on an ASCII platform (which its data
61     # is for), and it is in the modern format (starting in Unicode 3.1.0) and
62     # it is available.  This avoids being affected by potential bugs
63     # introduced by other layers of Perl
64     my $file="lib/unicore/CaseFolding.txt";
65
66     if (ord('A') == 65
67         && pack("C*", split /\./, Unicode::UCD::UnicodeVersion()) ge v3.1.0
68         && open my $fh, "<", $file)
69     {
70         @folds = <$fh>;
71     }
72     else {
73         my ($invlist_ref, $invmap_ref, undef, $default)
74                                     = Unicode::UCD::prop_invmap('Case_Folding');
75         for my $i (0 .. @$invlist_ref - 1 - 1) {
76             next if $invmap_ref->[$i] == $default;
77             my $adjust = -1;
78             for my $j ($invlist_ref->[$i] .. $invlist_ref->[$i+1] -1) {
79                 $adjust++;
80
81                 # Single-code point maps go to a 'C' type
82                 if (! ref $invmap_ref->[$i]) {
83                     push @folds, sprintf("%04X; C; %04X\n",
84                                         $j,
85                                         $invmap_ref->[$i] + $adjust);
86                 }
87                 else {  # Multi-code point maps go to 'F'.  prop_invmap()
88                         # guarantees that no adjustment is needed for these,
89                         # as the range will contain just one element
90                     push @folds, sprintf("%04X; F; %s\n",
91                                         $j,
92                                         join " ", map { sprintf "%04X", $_ }
93                                                         @{$invmap_ref->[$i]});
94                 }
95             }
96         }
97     }
98
99     for (@folds) {
100         chomp;
101
102         # Lines look like (without the initial '#'
103         #0130; F; 0069 0307; # LATIN CAPITAL LETTER I WITH DOT ABOVE
104         # Get rid of comments, ignore blank or comment-only lines
105         my $line = $_ =~ s/ (?: \s* \# .* )? $ //rx;
106         next unless length $line;
107         my ($hex_from, $fold_type, @folded) = split /[\s;]+/, $line;
108
109         my $from = hex $hex_from;
110
111         # Perl only deals with C and F folds
112         next if $fold_type ne 'C' and $fold_type ne 'F';
113
114         # Get each code point in the range that participates in this line's fold.
115         # The hash has keys of each code point in the range, and values of what it
116         # folds to and what folds to it
117         for my $i (0 .. @folded - 1) {
118             my $hex_fold = $folded[$i];
119             my $fold = hex $hex_fold;
120             push @{$folded_closure{$fold}}, $from if $fold < 256;
121             push @{$folded_closure{$from}}, $fold if $from < 256;
122
123             if ($i < @folded-1
124                 && $fold < 256
125                 && ! grep { $_ eq $hex_fold } @hex_non_final_folds)
126             {
127                 push @hex_non_final_folds, $hex_fold;
128
129                 # Also add the upper case, which in the latin1 range folds to
130                 # $fold
131                 push @hex_non_final_folds, sprintf "%04X", ord uc chr $fold;
132             }
133         }
134     }
135
136     # Now having read all the lines, combine them into the full closure of each
137     # code point in the range by adding lists together that share a common
138     # element
139     foreach my $folded (keys %folded_closure) {
140         foreach my $from (grep { $_ < 256 } @{$folded_closure{$folded}}) {
141             push @{$folded_closure{$from}}, @{$folded_closure{$folded}};
142         }
143     }
144 }
145
146 sub Is_Non_Latin1_Fold {
147     my @return;
148
149     foreach my $folded (keys %folded_closure) {
150         push @return, sprintf("%X", $folded), if grep { $_ > 255 }
151                                                      @{$folded_closure{$folded}};
152     }
153     return join("\n", @return) . "\n";
154 }
155
156 sub Is_Non_Final_Fold {
157     return join("\n", @hex_non_final_folds) . "\n";
158 }
159
160 my @bits;   # Bit map for each code point
161
162 # For each character, calculate which properties it matches.
163 for my $ord (0..255) {
164     my $char = chr($ord);
165     utf8::upgrade($char);   # Important to use Unicode rules!
166
167     # Look at all the properties we care about here.
168     for my $property (@properties) {
169         my $name = $property;
170
171         # Remove the suffix to get the actual property name.
172         # Currently the suffixes are '_L1', '_A', and none.
173         # If is a latin1 version, no further checking is needed.
174         if (! ($name =~ s/_L1$//)) {
175
176             # Here, isn't an _L1.  If its _A, it's automatically false for
177             # non-ascii.  The only current ones (besides ASCII) without a
178             # suffix are valid over the whole range.
179             next if $name =~ s/_A$// && $char !~ /\p{ASCII}/;
180         }
181         my $re;
182         if ($name eq 'PUNCT') {;
183
184             # Sadly, this is inconsistent: \pP and \pS for the ascii range,
185             # just \pP outside it.
186             $re = qr/\p{Punct}|[^\P{Symbol}\P{ASCII}]/;
187         } elsif ($name eq 'CHARNAME_CONT') {;
188             $re = qr/\p{_Perl_Charname_Continue}/,
189         } elsif ($name eq 'SPACE') {;
190             $re = qr/\p{XPerlSpace}/;
191         } elsif ($name eq 'IDFIRST') {
192             $re = qr/[_\p{Alpha}]/;
193         } elsif ($name eq 'PSXSPC') {
194             $re = qr/[\v\p{Space}]/;
195         } elsif ($name eq 'WORDCHAR') {
196             $re = qr/\p{XPosixWord}/;
197         } elsif ($name eq 'ALPHANUMERIC') {
198             # Like \w, but no underscore
199             $re = qr/\p{Alnum}/;
200         } elsif ($name eq 'QUOTEMETA') {
201             $re = qr/\p{_Perl_Quotemeta}/;
202         } elsif ($name eq 'NONLATIN1_FOLD') {
203             $re = qr/\p{Is_Non_Latin1_Fold}/;
204         } elsif ($name eq 'NON_FINAL_FOLD') {
205             $re = qr/\p{Is_Non_Final_Fold}/;
206         } elsif ($name eq 'IS_IN_SOME_FOLD') {
207             $re = qr/\p{_Perl_Any_Folds}/;
208         } elsif ($name eq 'BACKSLASH_FOO_LBRACE_IS_META') {
209
210             # This is true for FOO where FOO is the varying character in:
211             # \a{, \b{, \c{, ...
212             # and the sequence has non-literal meaning to Perl; so it is true
213             # for 'x' because \x{ is special, but not 'a' because \a{ isn't.
214             $re = qr/[gkNopPx]/;
215         } else {    # The remainder have the same name and values as Unicode
216             $re = eval "qr/\\p{$name}/";
217             use Carp;
218             carp $@ if ! defined $re;
219         }
220         #print "$ord, $name $property, $re\n";
221         if ($char =~ $re) {  # Add this property if matches
222             $bits[$ord] .= '|' if $bits[$ord];
223             $bits[$ord] .= "(1U<<_CC_$property)";
224         }
225     }
226     #print __LINE__, " $ord $char $bits[$ord]\n";
227 }
228
229 my $out_fh = open_new('l1_char_class_tab.h', '>',
230                       {style => '*', by => $0,
231                       from => "property definitions"});
232
233 print $out_fh <<END;
234 /* For code points whose position is not the same as Unicode,  both are shown
235  * in the comment*/
236 END
237
238 # Output the table using fairly short names for each char.
239 foreach my $charset (get_supported_code_pages()) {
240     my @a2n = get_a2n($charset);
241     my @out;
242
243     print $out_fh "\n" . get_conditional_compile_line_start($charset);
244 for my $ord (0..255) {
245     my $name;
246     my $char = chr $ord;
247     if ($char =~ /\p{PosixGraph}/) {
248         my $quote = $char eq "'" ? '"' : "'";
249         $name = $quote . chr($ord) . $quote;
250     }
251     elsif ($char =~ /\p{XPosixGraph}/) {
252         use charnames();
253         $name = charnames::viacode($ord);
254         $name =~ s/LATIN CAPITAL LETTER //
255                 or $name =~ s/LATIN SMALL LETTER (.*)/\L$1/
256                 or $name =~ s/ SIGN\b//
257                 or $name =~ s/EXCLAMATION MARK/'!'/
258                 or $name =~ s/QUESTION MARK/'?'/
259                 or $name =~ s/QUOTATION MARK/QUOTE/
260                 or $name =~ s/ INDICATOR//;
261         $name =~ s/\bWITH\b/\L$&/;
262         $name =~ s/\bONE\b/1/;
263         $name =~ s/\b(TWO|HALF)\b/2/;
264         $name =~ s/\bTHREE\b/3/;
265         $name =~ s/\b QUARTER S? \b/4/x;
266         $name =~ s/VULGAR FRACTION (.) (.)/$1\/$2/;
267         $name =~ s/\bTILDE\b/'~'/i
268                 or $name =~ s/\bCIRCUMFLEX\b/'^'/i
269                 or $name =~ s/\bSTROKE\b/'\/'/i
270                 or $name =~ s/ ABOVE\b//i;
271     }
272     else {
273         use Unicode::UCD qw(prop_invmap);
274         my ($list_ref, $map_ref, $format) = prop_invmap("Name_Alias");
275         if ($format !~ /^s/) {
276             use Carp;
277             carp "Unexpected format '$format' for 'Name_Alias";
278             last;
279         }
280         my $which = Unicode::UCD::search_invlist($list_ref, $ord);
281         if (! defined $which) {
282             use Carp;
283             carp "No name found for code pont $ord";
284         }
285         else {
286             my $map = $map_ref->[$which];
287             if (! ref $map) {
288                 $name = $map;
289             }
290             else {
291                 # Just pick the first abbreviation if more than one
292                 my @names = grep { $_ =~ /abbreviation/ } @$map;
293                 $name = $names[0];
294             }
295             $name =~ s/:.*//;
296         }
297     }
298     my $index = $a2n[$ord];
299     $out[$index] = ($ord == $index)
300                    ? sprintf "/* U+%02X %s */ %s,\n", $ord, $name, $bits[$ord]
301                    : sprintf "/* 0x%02X U+%02X %s */ %s,\n", $index, $ord, $name, $bits[$ord];
302 }
303     print $out_fh join "", @out;
304     print $out_fh "\n" . get_conditional_compile_line_end();
305 }
306
307 read_only_bottom_close_and_rename($out_fh)