]> git.saurik.com Git - bison.git/blob - examples/extexi
Alexandre Duret-Lutz <adl@gnu.org>
[bison.git] / examples / extexi
1 # Extract all examples from the manual source. -*- AWK -*-
2
3 # This file is part of GNU Bison
4 # Copyright 1992, 2000, 2001, 2005 Free Software Foundation, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # 02111-1307 USA
20
21 # This script is for use with any Awk that conforms to POSIX.
22 # It was derived from a similar script tests/generate.awk in GNU m4.
23 #
24 # Usage: extexi input-file.texi ... -- [FILES to extract]
25 BEGIN {
26 if (!output_dir)
27 output_dir = ".";
28 for (argc = 1; argc < ARGC; ++argc)
29 if (ARGV[argc] == "--")
30 break;
31 for (i = argc + 1; i < ARGC; ++i)
32 file_wanted[ARGV[i]] = 1;
33 ARGC = argc;
34 }
35
36 /^@node / {
37 if (seq > 0)
38 print "AT_CLEANUP";
39
40 split ($0, tmp, ",");
41 node = substr(tmp[1], 7);
42 seq = 0;
43 }
44
45 /^@comment file: / {
46 if (!file_wanted[$3])
47 message("ignoring " $3);
48 else
49 {
50 message("extracting " $3);
51 file = $3;
52 }
53 }
54
55 /^@example$/, /^@end example$/ {
56 if (!file)
57 next;
58
59 if ($0 ~ /^@example$/)
60 {
61 input = files_output[file] ? "\n" : "";
62
63 # FNR is starting at 0 instead of 1, and
64 # #line report the line number of the *next* line.
65 # => + 2.
66 # Note that recent Bison support it, but not Flex.
67 next;
68 }
69
70 if ($0 ~ /^@end example$/)
71 {
72 if (input == "")
73 fatal("no contents: " file);
74
75 input = normalize(input);
76 # No spurious end of line: use printf.
77 if (files_output[file])
78 printf ("%s", input) >> output_dir "/" file;
79 else
80 printf ("%s", input) > output_dir "/" file;
81 close (output_dir "/" file);
82 files_output[file] = 1;
83
84 file = input = "";
85 next;
86 }
87
88 input = input $0 "\n";
89 }
90
91
92 # We have to handle CONTENTS line per line, since anchors in AWK are
93 # referring to the whole string, not the lines.
94 function normalize(contents, i, lines, n, line, res) {
95 # Remove the Texinfo tags.
96 n = split (contents, lines, "\n");
97 # We don't want the last field which empty: it's behind the last \n.
98 for (i = 1; i < n; ++i)
99 {
100 line = lines[i];
101
102 # Whole line commands.
103 if (line ~ /^@(c |comment|dots|end (ignore|group)|ignore|group)/)
104 # Gperf accepts empty lines as valid input!!!
105 if (file ~ /\.gperf$/)
106 continue;
107 else
108 line = "";
109
110 gsub (/^@result\{\}/, "", line);
111 gsub (/^@error\{\}/, "", line);
112 gsub ("@[{]", "{", line);
113 gsub ("@}", "}", line);
114 gsub ("@@", "@", line);
115 gsub ("@comment.*", "", line);
116
117 res = res line "\n";
118 }
119 return res;
120 }
121
122
123 function message(msg) {
124 if (! message_printed[msg])
125 {
126 print "extexi: " msg > "/dev/stderr";
127 message_printed[msg] = 1;
128 }
129 }
130
131 function fatal(msg) {
132 message(msg);
133 exit 1
134 }