This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables: Move method to different class
[perl5.git] / regen_perly.pl
CommitLineData
0de566d7
DM
1#!/usr/bin/perl
2#
3# regen_perly.pl, DAPM 12-Feb-04
4#
1d325971 5# Copyright (c) 2004, 2005 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
32sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" }
33
34use warnings;
35use strict;
36
a9718e07
Z
37BEGIN { require 'regen_lib.pl'; }
38
0de566d7
DM
39my $bison = 'bison';
40
41if (@ARGV >= 2 and $ARGV[0] eq '-b') {
42 shift;
43 $bison = shift;
44}
45
46my $y_file = shift || 'perly.y';
47
48usage unless @ARGV==0 && $y_file =~ /\.y$/;
49
50(my $h_file = $y_file) =~ s/\.y$/.h/;
51(my $act_file = $y_file) =~ s/\.y$/.act/;
52(my $tab_file = $y_file) =~ s/\.y$/.tab/;
53(my $tmpc_file = $y_file) =~ s/\.y$/tmp.c/;
54(my $tmph_file = $y_file) =~ s/\.y$/tmp.h/;
a9718e07
Z
55my $new_h_file = "$h_file-new";
56my $new_act_file = "$act_file-new";
57my $new_tab_file = "$tab_file-new";
0de566d7
DM
58
59# the yytranslate[] table generated by bison is ASCII/EBCDIC sensitive
60
61die "$0: must be run on an ASCII system\n" unless ord 'A' == 65;
62
63# check for correct version number. The constraints are:
64# * must be >= 1.24 to avoid licensing issues.
65# * it must generate the yystos[] table. Version 1.28 doesn't generate
66# this; 1.35+ does
67# * Must produce output which is extractable by the regexes below
68# * Must produce the right values.
69# These last two contstraints may well be met by earlier versions, but
70# I simply haven't tested them yet. If it works for you, then modify
71# the test below to allow that version too. DAPM Feb 04.
72
73my $version = `$bison -V`;
7b931d50
LB
74unless ($version) { die <<EOF; }
75Could not find a version of bison in your path. Please install bison.
76EOF
77
ce1534ab 78unless ($version =~ /\b(1\.875[a-z]?|2\.[0134])\b/) { die <<EOF; }
0de566d7 79
2080282f 80You have the wrong version of bison in your path; currently 1.875
aae7e7b3
VP
812.0, 2.1, 2.3 or 2.4 is required. Try installing
82 http://ftp.gnu.org/gnu/bison/bison-2.4.1.tar.gz
0de566d7
DM
83or similar. Your bison identifies itself as:
84
85$version
86EOF
87
88# creates $tmpc_file and $tmph_file
89my_system("$bison -d -o $tmpc_file $y_file");
90
91open CTMPFILE, $tmpc_file or die "Can't open $tmpc_file: $!\n";
92my $clines;
93{ local $/; $clines = <CTMPFILE>; }
94die "failed to read $tmpc_file: length mismatch\n"
95 unless length $clines == -s $tmpc_file;
96close CTMPFILE;
97
98my ($actlines, $tablines) = extract($clines);
99
d5c6462e 100$tablines .= make_type_tab($y_file, $tablines);
0539ab63 101
a9718e07
Z
102chmod 0644, $new_act_file;
103open ACTFILE, ">$new_act_file" or die "can't open $new_act_file: $!\n";
0de566d7
DM
104print ACTFILE $actlines;
105close ACTFILE;
a9718e07 106chmod 0444, $new_act_file;
0de566d7 107
a9718e07
Z
108chmod 0644, $new_tab_file;
109open TABFILE, ">$new_tab_file" or die "can't open $new_tab_file: $!\n";
0de566d7
DM
110print TABFILE $tablines;
111close TABFILE;
a9718e07 112chmod 0444, $new_tab_file;
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
DM
119
120open TMPH_FILE, $tmph_file or die "Can't open $tmph_file: $!\n";
a9718e07
Z
121chmod 0644, $new_h_file;
122open H_FILE, ">$new_h_file" or die "Can't open $new_h_file: $!\n";
0de566d7 123my $endcore_done = 0;
ce1534ab
VP
124# Token macros need to be generated manually on bison 2.4
125my $gather_tokens = ($version =~ /\b2\.4\b/ ? undef : 0);
126my $tokens;
0de566d7
DM
127while (<TMPH_FILE>) {
128 print H_FILE "#ifdef PERL_CORE\n" if $. == 1;
129 if (!$endcore_done and /YYSTYPE_IS_DECLARED/) {
130 print H_FILE "#endif /* PERL_CORE */\n";
131 $endcore_done = 1;
132 }
96f4e226 133 next if /^#line \d+ ".*"/;
ce1534ab
VP
134 if (not defined $gather_tokens) {
135 $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x;
136 }
137 elsif ($gather_tokens) {
138 if (/^\# \s* endif/x) { # The #endif just after the end of the token enum
139 $gather_tokens = 0;
140 $_ .= "\n/* Tokens. */\n$tokens";
141 }
142 else {
143 my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x;
144 $tokens .= "#define $tok $val\n" if $tok;
145 }
146 }
0de566d7
DM
147 print H_FILE $_;
148}
149close TMPH_FILE;
150close H_FILE;
a9718e07 151chmod 0444, $new_h_file;
0de566d7
DM
152unlink $tmph_file;
153
a9718e07
Z
154rename_if_different($new_h_file, $h_file);
155rename_if_different($new_tab_file, $tab_file);
156rename_if_different($new_act_file, $act_file);
0de566d7
DM
157
158exit 0;
159
160
161sub extract {
162 my $clines = shift;
163 my $tablines;
164 my $actlines;
165
166 $clines =~ m@
167 (?:
168 ^/* YYFINAL[^\n]+\n #optional comment
169 )?
170 \# \s* define \s* YYFINAL # first #define
171 .*? # other defines + most tables
172 yystos\[\]\s*= # start of last table
173 .*?
174 }\s*; # end of last table
175 @xms
176 or die "Can't extract tables from $tmpc_file\n";
177 $tablines = $&;
178
179
180 $clines =~ m@
181 switch \s* \( \s* \w+ \s* \) \s* { \s*
182 (
ce1534ab
VP
183 case \s* \d+ \s* :
184 \s*
185 (?: \s* /\* .*? \*/ \s* )* # optional C-comments
186 \s*
2ade6388 187 \#line [^\n]+"\Q$y_file\E"
0de566d7
DM
188 .*?
189 )
190 }
191 \s*
ce1534ab 192 (?: \s* /\* .*? \*/ \s* )* # optional C-comments
0de566d7
DM
193 \s*
194 (
195 \#line[^\n]+\.c"
196 |
197 \#line[^\n]+\.simple"
3797f23d
DM
198 |
199 YY_SYMBOL_PRINT
0de566d7
DM
200 )
201 @xms
202 or die "Can't extract actions from $tmpc_file\n";
203 $actlines = $1;
204
ce1534ab
VP
205 # Remove extraneous comments from bison 2.4
206 $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx;
207
0d6f9730
DM
208 # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them.
209 $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm;
210
1654d593
DM
211 # convert yyvsp[nnn] into ps[nnn].val
212
213 $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g
214 or die "Can't convert value stack name\n";
215
0de566d7
DM
216 return $actlines. "\n", $tablines. "\n";
217}
218
d5c6462e
DM
219# Generate a table, yy_type_tab[], that specifies for each token, what
220# type of value it holds.
0539ab63 221#
d5c6462e
DM
222# Read the .y file and extract a list of all the token names and
223# non-terminal names; then scan the string $tablines for the table yytname,
224# which gives the token index of each token/non-terminal; then use this to
225# create yy_type_tab.
0539ab63 226#
d5c6462e
DM
227# ie given (in perly.y),
228#
229# %token <opval> A
230# %token <ival> B
231# %type <pval> C
232# %type <opval> D
233#
234# and (in $tablines),
235#
236# yytname[] = { "A" "B", "C", "D", "E" };
0539ab63
DM
237#
238# then return
d5c6462e
DM
239#
240# typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes;
241#
242# static const toketypes yy_type_tab[]
243# = { toketype_opval, toketype_ival, toketype_pval,
244# toketype_opval, toketype_ival }
245#
246# where "E" has the default type. The default type is determined
247# by the __DEFAULT__ comment next to the appropriate union member in
248# perly.y
0539ab63 249
d5c6462e 250sub make_type_tab {
0539ab63
DM
251 my ($y_file, $tablines) = @_;
252 my %tokens;
d5c6462e
DM
253 my %types;
254 my $default_token;
0539ab63
DM
255 open my $fh, '<', $y_file or die "Can't open $y_file: $!\n";
256 while (<$fh>) {
29522234
DM
257 if (/(\$\d+)\s*=/) {
258 warn "$y_file:$.: dangerous assignment to $1: $_";
259 }
260
d5c6462e
DM
261 if (/__DEFAULT__/) {
262 m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x
263 or die "$y_file: can't parse __DEFAULT__ line: $_";
264 die "$y_file: duplicate __DEFAULT__ line: $_"
265 if defined $default_token;
266 $default_token = $1;
267 next;
268 }
269
270 next unless /^%(token|type)/;
271 s/^%(token|type)\s+<(\w+)>\s+//
272 or die "$y_file: unparseable token/type line: $_";
273 $tokens{$_} = $2 for (split ' ', $_);
274 $types{$2} = 1;
0539ab63 275 }
d5c6462e
DM
276 die "$y_file: no __DEFAULT__ token defined\n" unless $default_token;
277 $types{$default_token} = 1;
0539ab63
DM
278
279 $tablines =~ /^\Qstatic const char *const yytname[] =\E\n
280 {\n
281 (.*?)
282 ^};
283 /xsm
284 or die "Can't extract yytname[] from table string\n";
285 my $fields = $1;
d5c6462e
DM
286 $fields =~ s{"([^"]+)"}
287 { "toketype_" .
288 (defined $tokens{$1} ? $tokens{$1} : $default_token)
289 }ge;
290 $fields =~ s/, \s* 0 \s* $//x
291 or die "make_type_tab: couldn't delete trailing ',0'\n";
292
0539ab63 293 return
d5c6462e
DM
294 "\ntypedef enum {\n\t"
295 . join(", ", map "toketype_$_", sort keys %types)
296 . "\n} toketypes;\n\n"
297 . "/* type of each token/terminal */\n"
d5c6462e
DM
298 . "static const toketypes yy_type_tab[] =\n{\n"
299 . $fields
300 . "\n};\n";
0539ab63
DM
301}
302
303
0de566d7
DM
304sub my_system {
305 system(@_);
306 if ($? == -1) {
d5c6462e 307 die "failed to execute command '@_': $!\n";
0de566d7
DM
308 }
309 elsif ($? & 127) {
310 die sprintf "command '@_' died with signal %d\n",
311 ($? & 127);
312 }
313 elsif ($? >> 8) {
314 die sprintf "command '@_' exited with value %d\n", $? >> 8;
315 }
316}