This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
correct omission of File::CheckTree and Text::Soundex from deprecations
[perl5.git] / regen_perly.pl
1 #!/usr/bin/perl
2 #
3 # regen_perly.pl, DAPM 12-Feb-04
4 #
5 # Copyright (c) 2004, 2005, 2006, 2009, 2010, 2011 Larry Wall
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
14 #               plus an extra table generated by this script.
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
32 use 5.006;
33 sub usage { die "usage: $0 [ -b bison_executable ] [ file.y ]\n" }
34
35 use warnings;
36 use strict;
37
38 BEGIN { require 'regen/regen_lib.pl'; }
39
40 my $bison = 'bison';
41
42 if (@ARGV >= 2 and $ARGV[0] eq '-b') {
43     shift;
44     $bison = shift;
45 }
46
47 my $y_file = shift || 'perly.y';
48
49 usage 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
59 die "$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.
67 # These last two constraints  may well be met by earlier versions, but
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
71 my $version = `$bison -V`;
72 unless ($version) { die <<EOF; }
73 Could not find a version of bison in your path. Please install bison.
74 EOF
75
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. :-(
78 unless ($version =~ /\b(1\.875[a-z]?|2\.[0134567])\b/) { die <<EOF; }
79
80 You have the wrong version of bison in your path; currently 1.875
81 2.0, 2.1, 2.3, 2.4, 2.5, 2.6 or 2.7 is required.  Try installing
82     http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
83 or similar.  Your bison identifies itself as:
84
85 $version
86 EOF
87
88 # bison's version number, not the entire string, is most useful later on.
89 $version = $1;
90
91 # creates $tmpc_file and $tmph_file
92 my_system("$bison -d -o $tmpc_file $y_file");
93
94 open my $ctmp_fh, '<', $tmpc_file or die "Can't open $tmpc_file: $!\n";
95 my $clines;
96 { local $/; $clines = <$ctmp_fh>; }
97 die "failed to read $tmpc_file: length mismatch\n"
98     unless length $clines == -s $tmpc_file;
99 close $ctmp_fh;
100
101 my ($actlines, $tablines) = extract($clines);
102
103 our %tokens;
104 $tablines .= make_type_tab($y_file, $tablines);
105
106 my ($act_fh, $tab_fh, $h_fh) = map {
107     open_new($_, '>', { by => $0, from => $y_file });
108 } $act_file, $tab_file, $h_file;
109
110 print $act_fh $actlines;
111
112 print $tab_fh $tablines;
113
114 unlink $tmpc_file;
115
116 # Wrap PERL_CORE round the symbol definitions. Also,  the
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.
119
120 open my $tmph_fh, '<', $tmph_file or die "Can't open $tmph_file: $!\n";
121
122 my $endcore_done = 0;
123 # Token macros need to be generated manually from bison 2.4 on
124 my $gather_tokens = $version >= 2.4 ? undef : 0;
125 my $tokens;
126 while (<$tmph_fh>) {
127     # bison 2.6 adds header guards, which break things because of where we
128     # insert #ifdef PERL_CORE, so strip them because they aren't important
129     next if /YY_PERLYTMP_H/;
130
131     print $h_fh "#ifdef PERL_CORE\n" if $. == 1;
132     if (!$endcore_done and /YYSTYPE_IS_DECLARED/) {
133         print $h_fh <<h;
134 #ifdef PERL_IN_TOKE_C
135 static bool
136 S_is_opval_token(int type) {
137     switch (type) {
138 h
139         print $h_fh <<i for sort grep $tokens{$_} eq 'opval', keys %tokens;
140     case $_:
141 i
142         print $h_fh <<j;
143         return 1;
144     }
145     return 0;
146 }
147 #endif /* PERL_IN_TOKE_C */
148 #endif /* PERL_CORE */
149 j
150         $endcore_done = 1;
151     }
152     next if /^#line \d+ ".*"/;
153     if (not defined $gather_tokens) {
154         $gather_tokens = 1 if /^\s* enum \s* yytokentype \s* \{/x;
155     }
156     elsif ($gather_tokens) {
157         if (/^\# \s* endif/x) { # The #endif just after the end of the token enum
158             $gather_tokens = 0;
159             $_ .= "\n/* Tokens.  */\n$tokens";
160         }
161         else {
162             my ($tok, $val) = /(\w+) \s* = \s* (\d+)/x;
163             $tokens .= "#define $tok $val\n" if $tok;
164         }
165     }
166     print $h_fh $_;
167 }
168 close $tmph_fh;
169 unlink $tmph_file;
170
171 foreach ($act_fh, $tab_fh, $h_fh) {
172     read_only_bottom_close_and_rename($_, ['regen_perly.pl', $y_file]);
173 }
174
175 exit 0;
176
177
178 sub extract {
179     my $clines = shift;
180     my $tablines;
181     my $actlines;
182
183     $clines =~ m@
184         (?:
185             ^/* YYFINAL[^\n]+\n         #optional comment
186         )?
187         \# \s* define \s* YYFINAL       # first #define
188         .*?                             # other defines + most tables
189         yystos\[\]\s*=                  # start of last table
190         .*?
191         }\s*;                           # end of last table
192     @xms
193         or die "Can't extract tables from $tmpc_file\n";
194     $tablines = $&;
195
196
197     $clines =~ m@
198         switch \s* \( \s* \w+ \s* \) \s* { \s*
199         (
200             case \s* \d+ \s* :
201             \s*
202             (?: \s* /\* .*? \*/ \s* )*  # optional C-comments
203             \s*
204             \#line [^\n]+"\Q$y_file\E"
205             .*?
206         )
207         }
208         \s*
209         (?: \s* /\* .*? \*/ \s* )*      # optional C-comments
210         \s*
211         (
212             \#line[^\n]+\.c"
213         |
214             \#line[^\n]+\.simple"
215         |
216             YY_SYMBOL_PRINT
217         )
218     @xms
219         or die "Can't extract actions from $tmpc_file\n";
220     $actlines = $1;
221
222     # Remove extraneous comments from bison 2.4
223     $actlines =~ s!\s* /\* \s* Line \s* \d+ \s* of \s* yacc\.c \s* \*/!!gx;
224
225     # C<#line 188 "perlytmp.c"> gets picked up by make depend, so remove them.
226     $actlines =~ s/^#line \d+ "\Q$tmpc_file\E".*$//gm;
227
228     # convert yyvsp[nnn] into ps[nnn].val
229
230     $actlines =~ s/yyvsp\[(.*?)\]/ps[$1].val/g
231         or die "Can't convert value stack name\n";
232
233     return $actlines. "\n", $tablines. "\n";
234 }
235
236 # Generate a table, yy_type_tab[], that specifies for each token, what
237 # type of value it holds.
238 #
239 # Read the .y file and extract a list of all the token names and
240 # non-terminal names; then scan the string $tablines for the table yytname,
241 # which gives the token index of each token/non-terminal; then use this to
242 # create yy_type_tab.
243 #
244 # ie given (in perly.y),
245 #
246 #   %token <opval> A
247 #   %token <ival>  B
248 #   %type  <pval>  C
249 #   %type  <opval> D
250 #
251 # and (in $tablines),
252 #
253 #   yytname[] = { "A" "B", "C", "D", "E" };
254 #
255 # then return
256 #
257 #    typedef enum { toketype_ival, toketype_opval, toketype_pval } toketypes;
258 #
259 #    static const toketypes yy_type_tab[]
260 #          = { toketype_opval, toketype_ival, toketype_pval,
261 #                toketype_opval, toketype_ival }
262 #
263 # where "E" has the default type. The default type is determined
264 # by the __DEFAULT__ comment  next to the appropriate union member in
265 # perly.y
266
267 sub make_type_tab {
268     my ($y_file, $tablines) = @_;
269     my %just_tokens;
270     my %tokens;
271     my %types;
272     my $default_token;
273     open my $fh, '<', $y_file or die "Can't open $y_file: $!\n";
274     while (<$fh>) {
275         if (/(\$\d+)\s*=/) {
276             warn "$y_file:$.: dangerous assignment to $1: $_";
277         }
278
279         if (/__DEFAULT__/) {
280             m{(\w+) \s* ; \s* /\* \s* __DEFAULT__}x
281                 or die "$y_file: can't parse __DEFAULT__ line: $_";
282             die "$y_file: duplicate __DEFAULT__ line: $_"
283                     if defined $default_token;
284             $default_token = $1;
285             next;
286         }
287
288         next unless /^%(token|type)/;
289         s/^%((token)|type)\s+<(\w+)>\s+//
290             or die "$y_file: unparseable token/type line: $_";
291         for (split ' ', $_) {
292             $tokens{$_} = $3;
293             if ($2) {
294                 $just_tokens{$_} = $3;
295             }
296         }
297         $types{$3} = 1;
298     }
299     *tokens = \%just_tokens; # perly.h needs this
300     die "$y_file: no __DEFAULT__ token defined\n" unless $default_token;
301     $types{$default_token} = 1;
302
303     $tablines =~ /^\Qstatic const char *const yytname[] =\E\n
304             \{\n
305             (.*?)
306             ^};
307             /xsm
308         or die "Can't extract yytname[] from table string\n";
309     my $fields = $1;
310     $fields =~ s{"([^"]+)"}
311                 { "toketype_" .
312                     (defined $tokens{$1} ? $tokens{$1} : $default_token)
313                 }ge;
314     $fields =~ s/, \s* (?:0|YY_NULL) \s* $//x
315         or die "make_type_tab: couldn't delete trailing ',0'\n";
316
317     return 
318           "\ntypedef enum {\n\t"
319         . join(", ", map "toketype_$_", sort keys %types)
320         . "\n} toketypes;\n\n"
321         . "/* type of each token/terminal */\n"
322         . "static const toketypes yy_type_tab[] =\n{\n"
323         . $fields
324         . "\n};\n";
325 }
326
327
328 sub my_system {
329     system(@_);
330     if ($? == -1) {
331         die "failed to execute command '@_': $!\n";
332     }
333     elsif ($? & 127) {
334         die sprintf "command '@_' died with signal %d\n",
335             ($? & 127);
336     }
337     elsif ($? >> 8) {
338         die sprintf "command '@_' exited with value %d\n", $? >> 8;
339     }
340 }