This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Give blessed() the same TRUEBOOL optimisation that ref() has in boolean contexts
[perl5.git] / regen / keywords.pl
1 #!/usr/bin/perl -w
2 #
3 # Regenerate (overwriting only if changed):
4 #
5 #    keywords.h keywords.c
6 #
7 # from information stored in the DATA section of this file.
8 #
9 # Accepts the standard regen_lib -q and -v args.
10
11 use strict;
12 use Devel::Tokenizer::C 0.05;
13
14 require './regen/regen_lib.pl';
15
16 my $h = open_new('keywords.h', '>',
17                  { by => 'regen/keywords.pl', from => 'its data',
18                    file => 'keywords.h', style => '*',
19                    copyright => [1994 .. 1997, 1999 .. 2002, 2005 .. 2007]});
20 my $c = open_new('keywords.c', '>',
21                  { by => 'regen/keywords.pl', from => 'its data', style => '*'});
22
23 my %by_strength;
24
25 my $keynum = 0;
26 while (<DATA>) {
27     chop;
28     next unless $_;
29     next if /^#/;
30     my ($strength, $keyword) = /^([- +])([A-Z_a-z2]+)/;
31     die "Bad line '$_'" unless defined $strength;
32     print $h tab(5, "#define KEY_$keyword"), $keynum++, "\n";
33     push @{$by_strength{$strength}}, $keyword;
34 }
35
36 # If this hash changes, make sure the equivalent hash in
37 # lib/B/Deparse.pm (%feature_keywords) is also updated.
38 my %feature_kw = (
39     state     => 'state',
40     say       => 'say',
41     given     => 'switch',
42     when      => 'switch',
43     default   => 'switch',
44     # continue is already a keyword
45     break     => 'switch',
46     evalbytes => 'evalbytes',
47     __SUB__   => '__SUB__',
48     fc        => 'fc',
49     isa       => 'isa',
50     try       => 'try',
51     catch     => 'try',
52     defer     => 'defer',
53 );
54
55 my %pos = map { ($_ => 1) } @{$by_strength{'+'}};
56
57 my $t = Devel::Tokenizer::C->new(TokenFunc     => \&perl_keyword,
58                                  TokenString   => 'name',
59                                  StringLength  => 'len',
60                                  MergeSwitches => 1,
61                                 );
62
63 $t->add_tokens(@{$by_strength{'+'}}, @{$by_strength{'-'}}, 'elseif');
64
65 my $switch = $t->generate(Indent => '  ');
66
67 print $c <<"END";
68 #include "EXTERN.h"
69 #define PERL_IN_KEYWORDS_C
70 #include "perl.h"
71 #include "keywords.h"
72 #include "feature.h"
73
74 I32
75 Perl_keyword (pTHX_ const char *name, I32 len, bool all_keywords)
76 {
77   PERL_ARGS_ASSERT_KEYWORD;
78
79 $switch
80 unknown:
81   return 0;
82 }
83 END
84
85 sub perl_keyword
86 {
87   my $k = shift;
88   my $sign = $pos{$k} ? '' : '-';
89
90   if ($k eq 'elseif') {
91     return <<END;
92 Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX), "elseif should be elsif");
93 END
94   }
95   elsif (my $feature = $feature_kw{$k}) {
96     $feature =~ s/([\\"])/\\$1/g;
97     return <<END;
98 return (all_keywords || FEATURE_\U$feature\E_IS_ENABLED ? ${sign}KEY_$k : 0);
99 END
100   }
101   return <<END;
102 return ${sign}KEY_$k;
103 END
104 }
105
106 read_only_bottom_close_and_rename($_, [$0]) foreach $c, $h;
107
108
109 # coresub_op in op.c expects __FILE__, __LINE__ and __PACKAGE__ to be the
110 # first three.
111
112 __END__
113
114  NULL
115 -__FILE__
116 -__LINE__
117 -__PACKAGE__
118 +__DATA__
119 +__END__
120 -__SUB__
121 +AUTOLOAD
122 +BEGIN
123 +UNITCHECK
124 +DESTROY
125 +END
126 +INIT
127 +CHECK
128 -abs
129 -accept
130 -alarm
131 -and
132 -atan2
133 -bind
134 -binmode
135 -bless
136 -break
137 -caller
138 +catch
139 -chdir
140 -chmod
141 -chomp
142 -chop
143 -chown
144 -chr
145 -chroot
146 -close
147 -closedir
148 -cmp
149 -connect
150 -continue
151 -cos
152 -crypt
153 -dbmclose
154 -dbmopen
155 +default
156 +defer
157 +defined
158 +delete
159 -die
160 +do
161 -dump
162 -each
163 +else
164 +elsif
165 -endgrent
166 -endhostent
167 -endnetent
168 -endprotoent
169 -endpwent
170 -endservent
171 -eof
172 -eq
173 +eval
174 -evalbytes
175 -exec
176 +exists
177 -exit
178 -exp
179 -fc
180 -fcntl
181 -fileno
182 -flock
183 +for
184 +foreach
185 -fork
186 +format
187 -formline
188 -ge
189 -getc
190 -getgrent
191 -getgrgid
192 -getgrnam
193 -gethostbyaddr
194 -gethostbyname
195 -gethostent
196 -getlogin
197 -getnetbyaddr
198 -getnetbyname
199 -getnetent
200 -getpeername
201 -getpgrp
202 -getppid
203 -getpriority
204 -getprotobyname
205 -getprotobynumber
206 -getprotoent
207 -getpwent
208 -getpwnam
209 -getpwuid
210 -getservbyname
211 -getservbyport
212 -getservent
213 -getsockname
214 -getsockopt
215 +given
216 +glob
217 -gmtime
218 +goto
219 +grep
220 -gt
221 -hex
222 +if
223 -index
224 -int
225 -ioctl
226 -isa
227 -join
228 -keys
229 -kill
230 +last
231 -lc
232 -lcfirst
233 -le
234 -length
235 -link
236 -listen
237 +local
238 -localtime
239 -lock
240 -log
241 -lstat
242 -lt
243 +m
244 +map
245 -mkdir
246 -msgctl
247 -msgget
248 -msgrcv
249 -msgsnd
250 +my
251 -ne
252 +next
253 +no
254 -not
255 -oct
256 -open
257 -opendir
258 -or
259 -ord
260 +our
261 -pack
262 +package
263 -pipe
264 -pop
265 +pos
266 +print
267 +printf
268 +prototype
269 -push
270 +q
271 +qq
272 +qr
273 -quotemeta
274 +qw
275 +qx
276 -rand
277 -read
278 -readdir
279 -readline
280 -readlink
281 -readpipe
282 -recv
283 +redo
284 -ref
285 -rename
286 +require
287 -reset
288 +return
289 -reverse
290 -rewinddir
291 -rindex
292 -rmdir
293 +s
294 +say
295 +scalar
296 -seek
297 -seekdir
298 -select
299 -semctl
300 -semget
301 -semop
302 -send
303 -setgrent
304 -sethostent
305 -setnetent
306 -setpgrp
307 -setpriority
308 -setprotoent
309 -setpwent
310 -setservent
311 -setsockopt
312 -shift
313 -shmctl
314 -shmget
315 -shmread
316 -shmwrite
317 -shutdown
318 -sin
319 -sleep
320 -socket
321 -socketpair
322 +sort
323 -splice
324 +split
325 -sprintf
326 -sqrt
327 -srand
328 -stat
329 +state
330 +study
331 +sub
332 -substr
333 -symlink
334 -syscall
335 -sysopen
336 -sysread
337 -sysseek
338 -system
339 -syswrite
340 -tell
341 -telldir
342 -tie
343 -tied
344 -time
345 -times
346 +tr
347 +try
348 -truncate
349 -uc
350 -ucfirst
351 -umask
352 +undef
353 +unless
354 -unlink
355 -unpack
356 -unshift
357 -untie
358 +until
359 +use
360 -utime
361 -values
362 -vec
363 -wait
364 -waitpid
365 -wantarray
366 -warn
367 +when
368 +while
369 -write
370 -x
371 -xor
372 +y