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