]> git.saurik.com Git - bison.git/blob - examples/extexi
doc: merge HACKING and README-hacking.
[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, 2006 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 3 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, see <http://www.gnu.org/licenses/>.
18
19 # This script is for use with any Awk that conforms to POSIX.
20 # It was derived from a similar script tests/generate.awk in GNU m4.
21 #
22 # Usage: extexi input-file.texi ... -- [FILES to extract]
23 BEGIN {
24 if (!output_dir)
25 output_dir = ".";
26 for (argc = 1; argc < ARGC; ++argc)
27 if (ARGV[argc] == "--")
28 break;
29 for (i = argc + 1; i < ARGC; ++i)
30 file_wanted[ARGV[i]] = 1;
31 ARGC = argc;
32 }
33
34 /^@node / {
35 if (seq > 0)
36 print "AT_CLEANUP";
37
38 split ($0, tmp, ",");
39 node = substr(tmp[1], 7);
40 seq = 0;
41 }
42
43 /^@comment file: / {
44 if (!file_wanted[$3])
45 message("ignoring " $3);
46 else
47 {
48 message("extracting " $3);
49 file = $3;
50 }
51 }
52
53 /^@example$/, /^@end example$/ {
54 if (!file)
55 next;
56
57 if ($0 ~ /^@example$/)
58 {
59 input = files_output[file] ? "\n" : "";
60
61 # FNR is starting at 0 instead of 1, and
62 # #line report the line number of the *next* line.
63 # => + 2.
64 # Note that recent Bison support it, but not Flex.
65 if (file ~ /\.[chy]*$/)
66 input = "#line " (FNR + 1) " \"" FILENAME "\"\n";
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 # The parens around the output file seem to be required
79 # by awk on Mac OS X Tiger (darwin 8.4.6).
80 printf ("%s", input) >> (output_dir "/" file);
81 else
82 printf ("%s", input) > (output_dir "/" file);
83 close (output_dir "/" file);
84 files_output[file] = 1;
85
86 file = input = "";
87 next;
88 }
89
90 input = input $0 "\n";
91 }
92
93
94 # We have to handle CONTENTS line per line, since anchors in AWK are
95 # referring to the whole string, not the lines.
96 function normalize(contents, i, lines, n, line, res) {
97 # Remove the Texinfo tags.
98 n = split (contents, lines, "\n");
99 # We don't want the last field which empty: it's behind the last \n.
100 for (i = 1; i < n; ++i)
101 {
102 line = lines[i];
103
104 # Whole line commands.
105 if (line ~ /^@(c |comment|dots|end (ignore|group)|ignore|group)/)
106 # Gperf accepts empty lines as valid input!!!
107 if (file ~ /\.gperf$/)
108 continue;
109 else
110 line = "";
111
112 gsub (/"@value\{VERSION\}"/, "\"" VERSION "\"", line)
113 gsub (/^@result\{\}/, "", line);
114 gsub (/^@error\{\}/, "", line);
115 gsub ("@[{]", "{", line);
116 gsub ("@}", "}", line);
117 gsub ("@@", "@", line);
118 gsub ("@comment.*", "", line);
119
120 res = res line "\n";
121 }
122 return res;
123 }
124
125
126 function message(msg) {
127 if (! message_printed[msg])
128 {
129 print "extexi: " msg > "/dev/stderr";
130 message_printed[msg] = 1;
131 }
132 }
133
134 function fatal(msg) {
135 message(msg);
136 exit 1
137 }