This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlapi: sortsv_flags is an SV function
[perl5.git] / regen_perly.pl
CommitLineData
0de566d7
DM
1#!/usr/bin/perl
2#
3# regen_perly.pl, DAPM 12-Feb-04
4#
2eee27d7 5# Copyright (c) 2004, 2005, 2006, 2009, 2010, 2011 Larry Wall
0de566d7
DM
6#
7# Given an input file perly.y, run bison on it and produce
8# the following output files:
9#
10# perly.h standard bison header file with minor doctoring of
11# #line directives plus adding a #ifdef PERL_CORE
12#
13# perly.tab the parser table C definitions extracted from the bison output
0539ab63 14# plus an extra table generated by this script.
0de566d7
DM
15#
16# perly.act the action case statements extracted from the bison output
17#
18# Note that perly.c is *not* regenerated - this is now a static file which
19# is not dependent on perly.y any more.
20#
21# If a filename of the form foo.y is given on the command line, then
22# this is used instead as the basename for all the files mentioned
23# above.
24#
25# Note that temporary files of the form perlytmp.h and perlytmp.c are
26# created and then deleted during this process
27#
28# Note also that this script is intended to be run on a UNIX system;
29# it may work elsewhere but no specific attempt has been made to make it
30# portable.
31
e8fb9efb 32use 5.006;
0de566d7
DM
33sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" }
34
35use warnings;
36use strict;
37
e64a0c47 38our $Verbose;
3d7c117d 39BEGIN { require './regen/regen_lib.pl'; }
a9718e07 40
0de566d7
DM
41my $bison = 'bison';
42
43if (@ARGV >= 2 and $ARGV[0] eq '-b') {
44 shift;
45 $bison = shift;
46}
47
48my $y_file = shift || 'perly.y';
49
50usage unless @ARGV==0 && $y_file =~ /\.y$/;
51
52(my $h_file = $y_file) =~ s/\.y$/.h/;
53(my $act_file = $y_file) =~ s/\.y$/.act/;
54(my $tab_file = $y_file) =~ s/\.y$/.tab/;
55(my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/;
56(my $tmph_file = $y_file) =~ s/\.y$/tmp.h/;
57
58# the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive
59
60die "$0: must be run on an ASCII system\n" unless ord 'A' == 65;
61
62# check for correct version number. The constraints are:
63# * must be >= 1.24 to avoid licensing issues.
64# * it must generate the yystos[] table. Version 1.28 doesn't generate
65# this; 1.35+ does
66# * Must produce output which is extractable by the regexes below
67# * Must produce the right values.
8abc1060 68# These last two constraints may well be met by earlier versions, but
0de566d7
DM
69# I simply haven't tested them yet. If it works for you, then modify
70# the test below to allow that version too. DAPM Feb 04.
71
72my $version = `$bison -V`;
7b931d50
LB
73unless ($version) { die <<EOF; }
74Could not find a version of bison in your path. Please install bison.
75EOF
76
9c221ee4
NC
77# Don't change this to add new bison versions without testing that the generated
78# files actually work :-) Win32 in particular may not like them. :-(
86b50d93 79unless ($version =~ /\b(1\.875[a-z]?|2\.[0134567]|3\.[0-4])\b/) { die <<EOF; }
0de566d7 80
f39ff1f3 81You have the wrong version of bison in your path; currently versions
86b50d93 821.875, 2.0-2.7 or 3.0-3.4 are known to work. Try installing
9c221ee4 83 http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
0de566d7
DM
84or similar. Your bison identifies itself as:
85
86$version
87EOF
88
9c221ee4
NC
89# bison's version number, not the entire string, is most useful later on.
90$version = $1;
91
0de566d7
DM
92# creates $tmpc_file and $tmph_file
93my_system("$bison -d -o $tmpc_file $y_file");
94
e8fb9efb 95open my $ctmp_fh, '<', $tmpc_file or die "Can't open $tmpc_file: $!\n";
0de566d7 96my $clines;
e8fb9efb 97{ local $/; $clines = <$ctmp_fh>; }
0de566d7
DM
98die "failed to read $tmpc_file: length mismatch\n"
99 unless length $clines == -s $tmpc_file;
e8fb9efb 100close $ctmp_fh;
0de566d7
DM
101
102my ($actlines, $tablines) = extract($clines);
103
6c7ae946 104our %tokens;
d5c6462e 105$tablines .= make_type_tab($y_file, $tablines);
0539ab63 106
cc49830d
NC
107my ($act_fh, $tab_fh, $h_fh) = map {
108 open_new($_, '>', { by => $0, from => $y_file });
109} $act_file, $tab_file, $h_file;
0de566d7 110
cc49830d 111print $act_fh $actlines;
e8fb9efb 112
cc49830d 113print $tab_fh $tablines;
0de566d7
DM
114
115unlink $tmpc_file;
116
117# Wrap PERL_CORE round the symbol definitions. Also, the
96f4e226
SH
118# C<#line 30 "perly.y"> confuses the Win32 resource compiler and the
119# C<#line 188 "perlytmp.h"> gets picked up by make depend, so remove them.
0de566d7 120
e8fb9efb 121open my $tmph_fh, '<', $tmph_file or die "Can't open $tmph_file: $!\n";
e8fb9efb 122
f39ff1f3
DM
123# add integer-encoded #def of the bison version
124
125{
126 $version =~ /^(\d+)\.(\d+)/
127 or die "Can't handle bison version format: '$version'";
128 my ($v1,$v2) = ($1,$2);
129 die "Unexpectedly large bison version '$v1'" if $v1 > 99;
130 die "Unexpectedly large bison subversion '$v2'" if $v2 > 9999;
131
132 printf $h_fh "#define PERL_BISON_VERSION %2d%04d\n\n", $v1, $v2;
133}
134
0de566d7 135my $endcore_done = 0;
2434f628 136# Token macros need to be generated manually from bison 2.4 on
9c221ee4 137my $gather_tokens = $version >= 2.4 ? undef : 0;
ce1534ab 138my $tokens;
e8fb9efb 139while (<$tmph_fh>) {
04ff073f
JL
140 # bison 2.6 adds header guards, which break things because of where we
141 # insert #ifdef PERL_CORE, so strip them because they aren't important
142 next if /YY_PERLYTMP_H/;
143
e8fb9efb 144 print $h_fh "#ifdef PERL_CORE\n" if $. == 1;
0de566d7 145 if (!$endcore_done and /YYSTYPE_IS_DECLARED/) {
6c7ae946
FC
146 print $h_fh <<h;
147#ifdef PERL_IN_TOKE_C
148static bool
149S_is_opval_token(int type) {
150 switch (type) {
151h
152 print $h_fh <<i for sort grep $tokens{$_} eq 'opval', keys %tokens;
153 case $_:
154i
155 print $h_fh <<j;
156 return 1;
157 }
158 return 0;
159}
160#endif /* PERL_IN_TOKE_C */
161#endif /* PERL_CORE */
162j
0de566d7
DM
163 $endcore_done = 1;
164 }
96f4e226 165 next if /^#line \d+ ".*"/;
ce1534ab
VP
166 if (not defined $gather_tokens) {
167 $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x;
168 }
169 elsif ($gather_tokens) {
170 if (/^\# \s* endif/x) { # The #endif just after the end of the token enum
171 $gather_tokens = 0;
172 $_ .= "\n/* Tokens. */\n$tokens";
173 }
174 else {
175 my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x;
176 $tokens .= "#define $tok $val\n" if $tok;
177 }
178 }
e8fb9efb 179 print $h_fh $_;
0de566d7 180}
e8fb9efb 181close $tmph_fh;
0de566d7
DM
182unlink $tmph_file;
183
c24c946d
NC
184foreach ($act_fh, $tab_fh, $h_fh) {
185 read_only_bottom_close_and_rename($_, ['regen_perly.pl', $y_file]);
186}
0de566d7
DM
187
188exit 0;
189
190
59966791
DM
191# extract the tables and actions from the generated .c file
192
0de566d7
DM
193sub extract {
194 my $clines = shift;
195 my $tablines;
196 my $actlines;
197
f39ff1f3 198 my $last_table = $version >= 3 ? 'yyr2' : 'yystos';
0de566d7
DM
199 $clines =~ m@
200 (?:
201 ^/* YYFINAL[^\n]+\n #optional comment
202 )?
203 \# \s* define \s* YYFINAL # first #define
204 .*? # other defines + most tables
f39ff1f3 205 $last_table\[\]\s*= # start of last table
0de566d7
DM
206 .*?
207 }\s*; # end of last table
208 @xms
209 or die "Can't extract tables from $tmpc_file\n";
210 $tablines = $&;
211
212
59966791
DM
213 # extract all the cases in the big action switch statement
214
0de566d7 215 $clines =~ m@
59966791
DM
216 switch \s* \( \s* yyn \s* \) \s* { \s*
217 ( .*? default: \s* break; \s* )
218 }
0de566d7
DM
219 @xms
220 or die "Can't extract actions from $tmpc_file\n";
221 $actlines = $1;
222
ce1534ab
VP
223 # Remove extraneous comments from bison 2.4
224 $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx;
225
0d6f9730
DM
226 # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them.
227 $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm;
228
1654d593
DM
229 # convert yyvsp[nnn] into ps[nnn].val
230
231 $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g
232 or die "Can't convert value stack name\n";
233
0de566d7
DM
234 return $actlines. "\n", $tablines. "\n";
235}
236
d5c6462e
DM
237# Generate a table, yy_type_tab[], that specifies for each token, what
238# type of value it holds.
0539ab63 239#
d5c6462e
DM
240# Read the .y file and extract a list of all the token names and
241# non-terminal names; then scan the string $tablines for the table yytname,
242# which gives the token index of each token/non-terminal; then use this to
243# create yy_type_tab.
0539ab63 244#
d5c6462e
DM
245# ie given (in perly.y),
246#
247# %token <opval> A
248# %token <ival> B
249# %type <pval> C
250# %type <opval> D
251#
252# and (in $tablines),
253#
254# yytname[] = { "A" "B", "C", "D", "E" };
0539ab63
DM
255#
256# then return
d5c6462e
DM
257#
258# typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes;
259#
260# static const toketypes yy_type_tab[]
261# = { toketype_opval, toketype_ival, toketype_pval,
262# toketype_opval, toketype_ival }
263#
264# where "E" has the default type. The default type is determined
265# by the __DEFAULT__ comment next to the appropriate union member in
266# perly.y
0539ab63 267
d5c6462e 268sub make_type_tab {
0539ab63 269 my ($y_file, $tablines) = @_;
6c7ae946 270 my %just_tokens;
0539ab63 271 my %tokens;
d5c6462e
DM
272 my %types;
273 my $default_token;
0539ab63
DM
274 open my $fh, '<', $y_file or die "Can't open $y_file: $!\n";
275 while (<$fh>) {
b5bbe64a 276 if (/(\$\d+)\s*=[^=]/) {
29522234
DM
277 warn "$y_file:$.: dangerous assignment to $1: $_";
278 }
279
d5c6462e
DM
280 if (/__DEFAULT__/) {
281 m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x
282 or die "$y_file: can't parse __DEFAULT__ line: $_";
283 die "$y_file: duplicate __DEFAULT__ line: $_"
284 if defined $default_token;
285 $default_token = $1;
286 next;
287 }
288
289 next unless /^%(token|type)/;
6c7ae946 290 s/^%((token)|type)\s+<(\w+)>\s+//
d5c6462e 291 or die "$y_file: unparseable token/type line: $_";
6c7ae946
FC
292 for (split ' ', $_) {
293 $tokens{$_} = $3;
294 if ($2) {
295 $just_tokens{$_} = $3;
296 }
297 }
298 $types{$3} = 1;
0539ab63 299 }
6c7ae946 300 *tokens = \%just_tokens; # perly.h needs this
d5c6462e
DM
301 die "$y_file: no __DEFAULT__ token defined\n" unless $default_token;
302 $types{$default_token} = 1;
0539ab63
DM
303
304 $tablines =~ /^\Qstatic const char *const yytname[] =\E\n
efcfdf1f 305 \{\n
0539ab63
DM
306 (.*?)
307 ^};
308 /xsm
309 or die "Can't extract yytname[] from table string\n";
310 my $fields = $1;
d5c6462e
DM
311 $fields =~ s{"([^"]+)"}
312 { "toketype_" .
313 (defined $tokens{$1} ? $tokens{$1} : $default_token)
314 }ge;
f39ff1f3 315 $fields =~ s/, \s* (?:0|YY_NULL|YY_NULLPTR) \s* $//x
d5c6462e
DM
316 or die "make_type_tab: couldn't delete trailing ',0'\n";
317
0539ab63 318 return
d5c6462e
DM
319 "\ntypedef enum {\n\t"
320 . join(", ", map "toketype_$_", sort keys %types)
321 . "\n} toketypes;\n\n"
322 . "/* type of each token/terminal */\n"
d5c6462e
DM
323 . "static const toketypes yy_type_tab[] =\n{\n"
324 . $fields
325 . "\n};\n";
0539ab63
DM
326}
327
328
0de566d7 329sub my_system {
95a1c520
DM
330 if ($Verbose) {
331 print "executing: @_\n";
332 }
0de566d7
DM
333 system(@_);
334 if ($? == -1) {
d5c6462e 335 die "failed to execute command '@_': $!\n";
0de566d7
DM
336 }
337 elsif ($? & 127) {
338 die sprintf "command '@_' died with signal %d\n",
339 ($? & 127);
340 }
341 elsif ($? >> 8) {
342 die sprintf "command '@_' exited with value %d\n", $? >> 8;
343 }
344}