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
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
36 # Use @ignore for code to extract that must not be part of the
37 # documentation. For instance:
40 # @comment file: calc++/scanner.ll
42 # // Work around an incompatibility in Flex.
50 use File
::Basename
qw(dirname);
51 use File
::Path
qw(make_path);
53 # Whether we generate synclines.
58 # Remove Texinfo mark up.
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;
75 # Print messages only once.
82 print STDERR
"extexi: $msg\n";
87 # The list of files we should extract.
90 # Whether we should extract that file, and then under which path.
94 for my $file (@file_wanted)
96 # No endswith in Perl 5...
97 return $file if $f eq substr($file, -length($f));
104 # Read input file $in, and generate the outputs.
109 my $f = new IO
::File
($in)
110 or die "$in: cannot open: $?";
111 # FILE-NAME => { BLOCK-NUM => CODE }
114 # The latest "@comment file: FILE [BLOCK-NUM]" arguments.
117 # The @example block currently read.
122 if (/^\@comment file: ([^:\n]+)(?::\s*(\d+))?$/)
128 $file = file_wanted
($f);
129 message
(" GEN $file");
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"
149 $file{$file}{$block} .= "\n" if defined $file{$file}{$block};
150 $file{$file}{$block} .= normalize
($input);
151 $file = $input = undef;
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}};
178 push @file_wanted, $arg;
184 elsif ($arg eq '--synclines')
197 ### Setup "GNU" style for perl-mode and cperl-mode.
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