This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #114888] Localise PL_comppad_name in cv_clone
[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
2434f628 76unless ($version =~ /\b(1\.875[a-z]?|2\.[01345])\b/) { die <<EOF; }
0de566d7 77
2080282f 78You have the wrong version of bison in your path; currently 1.875
2434f628
RU
792.0, 2.1, 2.3, 2.4 or 2.5 is required. Try installing
80 http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
0de566d7
DM
81or similar. Your bison identifies itself as:
82
83$version
84EOF
85
86# creates $tmpc_file and $tmph_file
87my_system("$bison -d -o $tmpc_file $y_file");
88
e8fb9efb 89open my $ctmp_fh, '<', $tmpc_file or die "Can't open $tmpc_file: $!\n";
0de566d7 90my $clines;
e8fb9efb 91{ local $/; $clines = <$ctmp_fh>; }
0de566d7
DM
92die "failed to read $tmpc_file: length mismatch\n"
93 unless length $clines == -s $tmpc_file;
e8fb9efb 94close $ctmp_fh;
0de566d7
DM
95
96my ($actlines, $tablines) = extract($clines);
97
6c7ae946 98our %tokens;
d5c6462e 99$tablines .= make_type_tab($y_file, $tablines);
0539ab63 100
cc49830d
NC
101my ($act_fh, $tab_fh, $h_fh) = map {
102 open_new($_, '>', { by => $0, from => $y_file });
103} $act_file, $tab_file, $h_file;
0de566d7 104
cc49830d 105print $act_fh $actlines;
e8fb9efb 106
cc49830d 107print $tab_fh $tablines;
0de566d7
DM
108
109unlink $tmpc_file;
110
111# Wrap PERL_CORE round the symbol definitions. Also, the
96f4e226
SH
112# C<#line 30 "perly.y"> confuses the Win32 resource compiler and the
113# C<#line 188 "perlytmp.h"> gets picked up by make depend, so remove them.
0de566d7 114
e8fb9efb 115open my $tmph_fh, '<', $tmph_file or die "Can't open $tmph_file: $!\n";
e8fb9efb 116
0de566d7 117my $endcore_done = 0;
2434f628
RU
118# Token macros need to be generated manually from bison 2.4 on
119my $gather_tokens = ($version =~ /\b2\.[45]\b/ ? undef : 0);
ce1534ab 120my $tokens;
e8fb9efb
NC
121while (<$tmph_fh>) {
122 print $h_fh "#ifdef PERL_CORE\n" if $. == 1;
0de566d7 123 if (!$endcore_done and /YYSTYPE_IS_DECLARED/) {
6c7ae946
FC
124 print $h_fh <<h;
125#ifdef PERL_IN_TOKE_C
126static bool
127S_is_opval_token(int type) {
128 switch (type) {
129h
130 print $h_fh <<i for sort grep $tokens{$_} eq 'opval', keys %tokens;
131 case $_:
132i
133 print $h_fh <<j;
134 return 1;
135 }
136 return 0;
137}
138#endif /* PERL_IN_TOKE_C */
139#endif /* PERL_CORE */
140j
0de566d7
DM
141 $endcore_done = 1;
142 }
96f4e226 143 next if /^#line \d+ ".*"/;
ce1534ab
VP
144 if (not defined $gather_tokens) {
145 $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x;
146 }
147 elsif ($gather_tokens) {
148 if (/^\# \s* endif/x) { # The #endif just after the end of the token enum
149 $gather_tokens = 0;
150 $_ .= "\n/* Tokens. */\n$tokens";
151 }
152 else {
153 my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x;
154 $tokens .= "#define $tok $val\n" if $tok;
155 }
156 }
e8fb9efb 157 print $h_fh $_;
0de566d7 158}
e8fb9efb 159close $tmph_fh;
0de566d7
DM
160unlink $tmph_file;
161
c24c946d
NC
162foreach ($act_fh, $tab_fh, $h_fh) {
163 read_only_bottom_close_and_rename($_, ['regen_perly.pl', $y_file]);
164}
0de566d7
DM
165
166exit 0;
167
168
169sub extract {
170 my $clines = shift;
171 my $tablines;
172 my $actlines;
173
174 $clines =~ m@
175 (?:
176 ^/* YYFINAL[^\n]+\n #optional comment
177 )?
178 \# \s* define \s* YYFINAL # first #define
179 .*? # other defines + most tables
180 yystos\[\]\s*= # start of last table
181 .*?
182 }\s*; # end of last table
183 @xms
184 or die "Can't extract tables from $tmpc_file\n";
185 $tablines = $&;
186
187
188 $clines =~ m@
189 switch \s* \( \s* \w+ \s* \) \s* { \s*
190 (
ce1534ab
VP
191 case \s* \d+ \s* :
192 \s*
193 (?: \s* /\* .*? \*/ \s* )* # optional C-comments
194 \s*
2ade6388 195 \#line [^\n]+"\Q$y_file\E"
0de566d7
DM
196 .*?
197 )
198 }
199 \s*
ce1534ab 200 (?: \s* /\* .*? \*/ \s* )* # optional C-comments
0de566d7
DM
201 \s*
202 (
203 \#line[^\n]+\.c"
204 |
205 \#line[^\n]+\.simple"
3797f23d
DM
206 |
207 YY_SYMBOL_PRINT
0de566d7
DM
208 )
209 @xms
210 or die "Can't extract actions from $tmpc_file\n";
211 $actlines = $1;
212
ce1534ab
VP
213 # Remove extraneous comments from bison 2.4
214 $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx;
215
0d6f9730
DM
216 # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them.
217 $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm;
218
1654d593
DM
219 # convert yyvsp[nnn] into ps[nnn].val
220
221 $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g
222 or die "Can't convert value stack name\n";
223
0de566d7
DM
224 return $actlines. "\n", $tablines. "\n";
225}
226
d5c6462e
DM
227# Generate a table, yy_type_tab[], that specifies for each token, what
228# type of value it holds.
0539ab63 229#
d5c6462e
DM
230# Read the .y file and extract a list of all the token names and
231# non-terminal names; then scan the string $tablines for the table yytname,
232# which gives the token index of each token/non-terminal; then use this to
233# create yy_type_tab.
0539ab63 234#
d5c6462e
DM
235# ie given (in perly.y),
236#
237# %token <opval> A
238# %token <ival> B
239# %type <pval> C
240# %type <opval> D
241#
242# and (in $tablines),
243#
244# yytname[] = { "A" "B", "C", "D", "E" };
0539ab63
DM
245#
246# then return
d5c6462e
DM
247#
248# typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes;
249#
250# static const toketypes yy_type_tab[]
251# = { toketype_opval, toketype_ival, toketype_pval,
252# toketype_opval, toketype_ival }
253#
254# where "E" has the default type. The default type is determined
255# by the __DEFAULT__ comment next to the appropriate union member in
256# perly.y
0539ab63 257
d5c6462e 258sub make_type_tab {
0539ab63 259 my ($y_file, $tablines) = @_;
6c7ae946 260 my %just_tokens;
0539ab63 261 my %tokens;
d5c6462e
DM
262 my %types;
263 my $default_token;
0539ab63
DM
264 open my $fh, '<', $y_file or die "Can't open $y_file: $!\n";
265 while (<$fh>) {
29522234
DM
266 if (/(\$\d+)\s*=/) {
267 warn "$y_file:$.: dangerous assignment to $1: $_";
268 }
269
d5c6462e
DM
270 if (/__DEFAULT__/) {
271 m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x
272 or die "$y_file: can't parse __DEFAULT__ line: $_";
273 die "$y_file: duplicate __DEFAULT__ line: $_"
274 if defined $default_token;
275 $default_token = $1;
276 next;
277 }
278
279 next unless /^%(token|type)/;
6c7ae946 280 s/^%((token)|type)\s+<(\w+)>\s+//
d5c6462e 281 or die "$y_file: unparseable token/type line: $_";
6c7ae946
FC
282 for (split ' ', $_) {
283 $tokens{$_} = $3;
284 if ($2) {
285 $just_tokens{$_} = $3;
286 }
287 }
288 $types{$3} = 1;
0539ab63 289 }
6c7ae946 290 *tokens = \%just_tokens; # perly.h needs this
d5c6462e
DM
291 die "$y_file: no __DEFAULT__ token defined\n" unless $default_token;
292 $types{$default_token} = 1;
0539ab63
DM
293
294 $tablines =~ /^\Qstatic const char *const yytname[] =\E\n
efcfdf1f 295 \{\n
0539ab63
DM
296 (.*?)
297 ^};
298 /xsm
299 or die "Can't extract yytname[] from table string\n";
300 my $fields = $1;
d5c6462e
DM
301 $fields =~ s{"([^"]+)"}
302 { "toketype_" .
303 (defined $tokens{$1} ? $tokens{$1} : $default_token)
304 }ge;
305 $fields =~ s/, \s* 0 \s* $//x
306 or die "make_type_tab: couldn't delete trailing ',0'\n";
307
0539ab63 308 return
d5c6462e
DM
309 "\ntypedef enum {\n\t"
310 . join(", ", map "toketype_$_", sort keys %types)
311 . "\n} toketypes;\n\n"
312 . "/* type of each token/terminal */\n"
d5c6462e
DM
313 . "static const toketypes yy_type_tab[] =\n{\n"
314 . $fields
315 . "\n};\n";
0539ab63
DM
316}
317
318
0de566d7
DM
319sub my_system {
320 system(@_);
321 if ($? == -1) {
d5c6462e 322 die "failed to execute command '@_': $!\n";
0de566d7
DM
323 }
324 elsif ($? & 127) {
325 die sprintf "command '@_' died with signal %d\n",
326 ($? & 127);
327 }
328 elsif ($? >> 8) {
329 die sprintf "command '@_' exited with value %d\n", $? >> 8;
330 }
331}