]> git.saurik.com Git - bison.git/blame - examples/extexi
maint: update copyright years
[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
7d6bad19 6# Copyright (C) 1992, 2000-2001, 2005-2006, 2009-2013 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
1c59e0a1 22# Usage: extexi 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
38# normalize($block)
39# -----------------
40# Remove Texinfo mark up.
41sub normalize($)
42{
43 local ($_) = @_;
44
45 s/^\@(c |comment|dots|end (ignore|group)|ignore|group).*//mg;
46 s/\@value\{VERSION\}/$ENV{VERSION}/g;
47 s/^\@(error|result)\{\}//mg;
48 s/\@([{}@])/$1/g;
49 s/\@comment.*//;
50 $_;
1c59e0a1
AD
51}
52
8e15fef5
AD
53# Print messages only once.
54my %msg;
55sub message($)
56{
57 my ($msg) = @_;
58 if (! $msg{$msg})
1c59e0a1 59 {
8e15fef5
AD
60 print STDERR "extexi: $msg\n";
61 $msg{$msg} = 1;
1c59e0a1 62 }
8e15fef5 63}
1c59e0a1 64
8e15fef5
AD
65# basename => full file name for files we should extract.
66my %file_wanted;
8e15fef5
AD
67
68sub process ($)
69{
70 my ($in) = @_;
71 use IO::File;
72 my $f = new IO::File($in)
73 or die "$in: cannot open: $?";
830e7330
AD
74 # FILE-NAME => { BLOCK-NUM => CODE }
75 my %file;
76
77 # The latest "@comment file: FILE [BLOCK-NUM]" arguments.
8e15fef5 78 my $file;
830e7330 79 my $block;
8e15fef5
AD
80 # The @example block currently read.
81 my $input;
82 local $_;
83 while (<$f>)
1c59e0a1 84 {
830e7330 85 if (/^\@comment file: ([^:\n]+)(?::\s*(\d+))?$/)
8e15fef5
AD
86 {
87 my $f = $1;
830e7330 88 $block = $2 || 1;
8e15fef5
AD
89 if ($file_wanted{$f})
90 {
91 $file = $file_wanted{$f};
92 message(" GEN $file");
93 }
94 else
95 {
96 message("SKIP $f");
97 }
98 }
99 elsif ($file && /^\@(small)?example$/ .. /^\@end (small)?example$/)
100 {
101 if (/^\@(small)?example$/)
102 {
8e15fef5
AD
103 # Bison supports synclines, but not Flex.
104 $input .= sprintf ("#line %s \"$in\"\n", $. + 1)
105 if $file =~ /\.[chy]*$/;
106 next;
107 }
108 elsif (/^\@end (small)?example$/)
109 {
110 die "no contents: $file"
111 if $input eq "";
112
830e7330 113 $file{$file}{$block} .= normalize($input);
8e15fef5 114 $file = $input = undef;
830e7330 115 ++$block;
8e15fef5
AD
116 }
117 else
118 {
119 $input .= $_;
120 }
121 }
830e7330 122 }
1c59e0a1 123
830e7330
AD
124 # Output the files.
125 for my $file (keys %file)
126 {
127 # No spurious end of line: use printf.
128 my $o = new IO::File(">$file")
129 or die "$file: cannot create: $?";
130 print $o $file{$file}{$_}
131 for sort keys %{$file{$file}};
1c59e0a1 132 }
1c59e0a1
AD
133}
134
8e15fef5
AD
135my @input;
136my $seen_dash = 0;
137for my $arg (@ARGV)
138{
139 if ($arg eq '--')
1c59e0a1 140 {
8e15fef5 141 $seen_dash = 1;
1c59e0a1 142 }
8e15fef5 143 elsif ($seen_dash)
f938a7e7 144 {
8e15fef5
AD
145 use File::Basename;
146 $file_wanted{basename($arg)} = $arg;
147 }
148 else
149 {
150 push @input, $arg;
f938a7e7 151 }
1c59e0a1 152}
8e15fef5
AD
153process $_
154 foreach @input;
155
156
157### Setup "GNU" style for perl-mode and cperl-mode.
158## Local Variables:
159## perl-indent-level: 2
160## perl-continued-statement-offset: 2
161## perl-continued-brace-offset: 0
162## perl-brace-offset: 0
163## perl-brace-imaginary-offset: 0
164## perl-label-offset: -2
165## cperl-indent-level: 2
166## cperl-brace-offset: 0
167## cperl-continued-brace-offset: 0
168## cperl-label-offset: -2
169## cperl-extra-newline-before-brace: t
170## cperl-merge-trailing-else: nil
171## cperl-continued-statement-offset: 2
172## End: