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