]> git.saurik.com Git - bison.git/blame - examples/extexi
bison: avoid warnings from static code analysis
[bison.git] / examples / extexi
CommitLineData
8e15fef5
AD
1#! /usr/bin/perl -w
2# Extract all examples from the manual source.
1c59e0a1 3
b34d96c1 4# This file is part of GNU Bison
7d424de1 5
3209eb1c 6# Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2015 Free Software
35905f2b 7# Foundation, Inc.
1c59e0a1 8#
f16b0819 9# This program is free software: you can redistribute it and/or modify
1c59e0a1 10# it under the terms of the GNU General Public License as published by
f16b0819 11# the Free Software Foundation, either version 3 of the License, or
1c59e0a1
AD
12# (at your option) any later version.
13#
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.
18#
19# You should have received a copy of the GNU General Public License
f16b0819 20# along with this program. If not, see <http://www.gnu.org/licenses/>.
1c59e0a1 21
b167e7ba 22# Usage: extexi [OPTION...] input-file.texi ... -- [FILES to extract]
1c59e0a1 23
830e7330
AD
24# Look for @example environments preceded with lines such as:
25#
26# @comment file calc.y
27# or
28# @comment file calc.y: 3
29#
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.
35
8e15fef5
AD
36use strict;
37
b167e7ba
AD
38# Whether we generate synclines.
39my $synclines = 0;
40
8e15fef5
AD
41# normalize($block)
42# -----------------
43# Remove Texinfo mark up.
44sub normalize($)
45{
46 local ($_) = @_;
47
48 s/^\@(c |comment|dots|end (ignore|group)|ignore|group).*//mg;
49 s/\@value\{VERSION\}/$ENV{VERSION}/g;
50 s/^\@(error|result)\{\}//mg;
51 s/\@([{}@])/$1/g;
52 s/\@comment.*//;
53 $_;
1c59e0a1
AD
54}
55
8e15fef5
AD
56# Print messages only once.
57my %msg;
58sub message($)
59{
60 my ($msg) = @_;
61 if (! $msg{$msg})
1c59e0a1 62 {
8e15fef5
AD
63 print STDERR "extexi: $msg\n";
64 $msg{$msg} = 1;
1c59e0a1 65 }
8e15fef5 66}
1c59e0a1 67
8e15fef5
AD
68# basename => full file name for files we should extract.
69my %file_wanted;
8e15fef5
AD
70
71sub process ($)
72{
73 my ($in) = @_;
74 use IO::File;
75 my $f = new IO::File($in)
76 or die "$in: cannot open: $?";
830e7330
AD
77 # FILE-NAME => { BLOCK-NUM => CODE }
78 my %file;
79
80 # The latest "@comment file: FILE [BLOCK-NUM]" arguments.
8e15fef5 81 my $file;
830e7330 82 my $block;
8e15fef5
AD
83 # The @example block currently read.
84 my $input;
85 local $_;
86 while (<$f>)
1c59e0a1 87 {
830e7330 88 if (/^\@comment file: ([^:\n]+)(?::\s*(\d+))?$/)
8e15fef5
AD
89 {
90 my $f = $1;
830e7330 91 $block = $2 || 1;
8e15fef5
AD
92 if ($file_wanted{$f})
93 {
94 $file = $file_wanted{$f};
95 message(" GEN $file");
96 }
97 else
98 {
99 message("SKIP $f");
100 }
101 }
102 elsif ($file && /^\@(small)?example$/ .. /^\@end (small)?example$/)
103 {
104 if (/^\@(small)?example$/)
105 {
8e15fef5
AD
106 # Bison supports synclines, but not Flex.
107 $input .= sprintf ("#line %s \"$in\"\n", $. + 1)
b167e7ba 108 if $synclines && $file =~ /\.[chy]*$/;
8e15fef5
AD
109 next;
110 }
111 elsif (/^\@end (small)?example$/)
112 {
113 die "no contents: $file"
114 if $input eq "";
115
830e7330 116 $file{$file}{$block} .= normalize($input);
8e15fef5 117 $file = $input = undef;
830e7330 118 ++$block;
8e15fef5
AD
119 }
120 else
121 {
122 $input .= $_;
123 }
124 }
830e7330 125 }
1c59e0a1 126
830e7330
AD
127 # Output the files.
128 for my $file (keys %file)
129 {
130 # No spurious end of line: use printf.
131 my $o = new IO::File(">$file")
132 or die "$file: cannot create: $?";
133 print $o $file{$file}{$_}
134 for sort keys %{$file{$file}};
1c59e0a1 135 }
1c59e0a1
AD
136}
137
8e15fef5
AD
138my @input;
139my $seen_dash = 0;
140for my $arg (@ARGV)
141{
b167e7ba
AD
142 if ($seen_dash)
143 {
144 use File::Basename;
145 $file_wanted{basename($arg)} = $arg;
146 }
147 elsif ($arg eq '--')
1c59e0a1 148 {
8e15fef5 149 $seen_dash = 1;
1c59e0a1 150 }
b167e7ba 151 elsif ($arg eq '--synclines')
f938a7e7 152 {
b167e7ba 153 $synclines = 1;
8e15fef5
AD
154 }
155 else
156 {
157 push @input, $arg;
f938a7e7 158 }
1c59e0a1 159}
8e15fef5
AD
160process $_
161 foreach @input;
162
163
164### Setup "GNU" style for perl-mode and cperl-mode.
165## Local Variables:
166## perl-indent-level: 2
167## perl-continued-statement-offset: 2
168## perl-continued-brace-offset: 0
169## perl-brace-offset: 0
170## perl-brace-imaginary-offset: 0
171## perl-label-offset: -2
172## cperl-indent-level: 2
173## cperl-brace-offset: 0
174## cperl-continued-brace-offset: 0
175## cperl-label-offset: -2
176## cperl-extra-newline-before-brace: t
177## cperl-merge-trailing-else: nil
178## cperl-continued-statement-offset: 2
179## End: