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