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