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