Update URLs to prefer https: to http:
[bison.git] / examples / extexi
blob56cfeb5387cb764839a4f6dc278cff51dbc43b86
1 #! /usr/bin/perl -w
2 # Extract all examples from the manual source.
4 # This file is part of GNU Bison
6 # Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2015, 2018-2021 Free
7 # Software Foundation, Inc.
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 # Usage: extexi [OPTION...] input-file.texi ... -- [FILES to extract]
24 # Look for @example environments preceded with lines such as:
26 # @comment file calc.y
27 # or
28 # @comment file calc.y: 3
30 # and output their content in that file (calc.y). When numbers are
31 # provided, use them to decide the output order (block numbered 1 is
32 # output before block 2, even if the latter appears before). The same
33 # number may be used several time, in which case the order of
34 # appearance is used.
36 # Use @ignore for code to extract that must not be part of the
37 # documentation. For instance:
39 # @ignore
40 # @comment file: calc++/scanner.ll
41 # @example
42 # // Work around an incompatibility in Flex.
43 # # undef yywrap
44 # # define yywrap() 1
45 # @end example
46 # @end ignore
48 use strict;
50 use File::Basename qw(dirname);
51 use File::Path qw(make_path);
53 # Whether we generate synclines.
54 my $synclines = 0;
56 # normalize($block)
57 # -----------------
58 # Remove Texinfo mark up.
59 sub normalize($)
61 local ($_) = @_;
63 # If we just remove this lines, then the compiler's tracking of
64 # #lines is broken. Leave lines that that accepted by all our tools
65 # (including flex, hence the leading space), and that will be easy
66 # to remove (see the Make examples-unline recipe).
67 s{^\@(c |comment|dots|end (ignore|group)|ignore|group).*}{ /**/}mg;
68 s/\@value\{VERSION\}/$ENV{VERSION}/g;
69 s/^\@(error|result)\{\}//mg;
70 s/\@([{}@])/$1/g;
71 s/\@comment.*//;
72 $_;
75 # Print messages only once.
76 my %msg;
77 sub message($)
79 my ($msg) = @_;
80 if (! $msg{$msg})
82 print STDERR "extexi: $msg\n";
83 $msg{$msg} = 1;
87 # The list of files we should extract.
88 my @file_wanted;
90 # Whether we should extract that file, and then under which path.
91 sub file_wanted ($)
93 my ($f) = @_;
94 for my $file (@file_wanted)
96 # No endswith in Perl 5...
97 return $file if $f eq substr($file, -length($f));
99 undef
102 # process ($in)
103 # -------------
104 # Read input file $in, and generate the outputs.
105 sub process ($)
107 my ($in) = @_;
108 use IO::File;
109 my $f = new IO::File($in)
110 or die "$in: cannot open: $?";
111 # FILE-NAME => { BLOCK-NUM => CODE }
112 my %file;
114 # The latest "@comment file: FILE [BLOCK-NUM]" arguments.
115 my $file;
116 my $block;
117 # The @example block currently read.
118 my $input;
119 local $_;
120 while (<$f>)
122 if (/^\@comment file: ([^:\n]+)(?::\s*(\d+))?$/)
124 my $f = $1;
125 $block = $2 || 1;
126 if (file_wanted($f))
128 $file = file_wanted($f);
129 message(" GEN $file");
131 else
133 message("SKIP $f");
136 elsif ($file && /^\@(small)?example$/ .. /^\@end (small)?example$/)
138 if (/^\@(small)?example$/)
140 # Bison supports synclines, but not Flex.
141 $input .= sprintf ("#line %s \"$in\"\n", $. + 1)
142 if $synclines && $file =~ /\.[chy]*$/;
144 elsif (/^\@end (small)?example$/)
146 die "no contents: $file"
147 if $input eq "";
149 $file{$file}{$block} .= "\n" if defined $file{$file}{$block};
150 $file{$file}{$block} .= normalize($input);
151 $file = $input = undef;
152 ++$block;
154 else
156 $input .= $_;
161 # Output the files.
162 for my $file (keys %file)
164 make_path (dirname ($file));
165 my $o = new IO::File(">$file")
166 or die "$file: cannot create: $?";
167 print $o $file{$file}{$_}
168 for sort keys %{$file{$file}};
172 my @input;
173 my $seen_dash = 0;
174 for my $arg (@ARGV)
176 if ($seen_dash)
178 push @file_wanted, $arg;
180 elsif ($arg eq '--')
182 $seen_dash = 1;
184 elsif ($arg eq '--synclines')
186 $synclines = 1;
188 else
190 push @input, $arg;
193 process $_
194 foreach @input;
197 ### Setup "GNU" style for perl-mode and cperl-mode.
198 ## Local Variables:
199 ## perl-indent-level: 2
200 ## perl-continued-statement-offset: 2
201 ## perl-continued-brace-offset: 0
202 ## perl-brace-offset: 0
203 ## perl-brace-imaginary-offset: 0
204 ## perl-label-offset: -2
205 ## cperl-indent-level: 2
206 ## cperl-brace-offset: 0
207 ## cperl-continued-brace-offset: 0
208 ## cperl-label-offset: -2
209 ## cperl-extra-newline-before-brace: t
210 ## cperl-merge-trailing-else: nil
211 ## cperl-continued-statement-offset: 2
212 ## End: