]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
17edc790ecb2c257843e4785239b6971d190d01f
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
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 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
22
23 %{
24 /* Work around a bug in flex 2.5.31. See Debian bug 333231
25 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
26 #undef skel_wrap
27 #define skel_wrap() 1
28
29 #define FLEX_PREFIX(Id) skel_ ## Id
30 #include <src/flex-scanner.h>
31
32 #include <dirname.h>
33 #include <error.h>
34 #include <quotearg.h>
35
36 #include <src/complain.h>
37 #include <src/getargs.h>
38 #include <src/files.h>
39 #include <src/scan-skel.h>
40
41 #define YY_DECL static int skel_lex (void)
42 YY_DECL;
43
44 #define QPUTS(String) \
45 fputs (quotearg_style (c_quoting_style, String), yyout)
46
47 static void at_directive_perform (int at_directive_argc,
48 char *at_directive_argv[],
49 char **outnamep, int *out_linenop);
50 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
51 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
52 static void fail_for_invalid_at (char const *at);
53 %}
54
55 %x SC_AT_DIRECTIVE_ARGS
56 %x SC_AT_DIRECTIVE_SKIP_WS
57
58 %%
59
60 %{
61 int out_lineno PACIFY_CC (= 0);
62 char *outname = NULL;
63
64 /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and
65 @fatal_at directives take multiple arguments, and the last three already
66 can't take more than 7. at_directive_argv[0] is the directive name. */
67 #define AT_DIRECTIVE_ARGC_MAX 8
68 int at_directive_argc = 0;
69 char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
70 %}
71
72 "@@" fputc ('@', yyout);
73 "@{" fputc ('[', yyout);
74 "@}" fputc (']', yyout);
75 "@`" continue; /* Used by b4_cat in ../data/bison.m4. */
76 @\n continue;
77
78 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
79 "@ofile@" QPUTS (outname);
80
81 @[a-z_]+"(" {
82 yytext[yyleng-1] = '\0';
83 obstack_grow (&obstack_for_string, yytext, yyleng);
84 at_directive_argv[at_directive_argc++] = obstack_finish (&obstack_for_string);
85 BEGIN SC_AT_DIRECTIVE_ARGS;
86 }
87
88 /* This pattern must not match more than the previous @ patterns. */
89 @[^@{}`(\n]* fail_for_invalid_at (yytext);
90 \n out_lineno++; ECHO;
91 [^@\n]+ ECHO;
92
93 <INITIAL><<EOF>> {
94 if (outname)
95 {
96 free (outname);
97 xfclose (yyout);
98 }
99 return EOF;
100 }
101
102 <SC_AT_DIRECTIVE_ARGS>
103 {
104 [^@]+ STRING_GROW;
105
106 "@@" obstack_1grow (&obstack_for_string, '@');
107 "@{" obstack_1grow (&obstack_for_string, '[');
108 "@}" obstack_1grow (&obstack_for_string, ']');
109 "@`" continue; /* For starting an argument that begins with whitespace. */
110 @\n continue;
111
112 @[,)] {
113 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
114 fail_for_at_directive_too_many_args (at_directive_argv[0]);
115
116 at_directive_argv[at_directive_argc++] =
117 obstack_finish0 (&obstack_for_string);
118
119 /* Like M4, skip whitespace after a comma. */
120 if (yytext[1] == ',')
121 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
122 else
123 {
124 at_directive_perform (at_directive_argc, at_directive_argv,
125 &outname, &out_lineno);
126 obstack_free (&obstack_for_string, at_directive_argv[0]);
127 at_directive_argc = 0;
128 BEGIN INITIAL;
129 }
130 }
131
132 @.? fail_for_invalid_at (yytext);
133 }
134
135 <SC_AT_DIRECTIVE_SKIP_WS>
136 {
137 [ \t\r\n] continue;
138 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
139 }
140
141 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
142 {
143 <<EOF>> {
144 complain (fatal, _("unclosed %s directive in skeleton"),
145 at_directive_argv[0]);
146 }
147 }
148
149 %%
150
151 /*------------------------.
152 | Scan a Bison skeleton. |
153 `------------------------*/
154
155 void
156 scan_skel (FILE *in)
157 {
158 static bool initialized = false;
159 if (!initialized)
160 {
161 initialized = true;
162 obstack_init (&obstack_for_string);
163 }
164 skel_in = in;
165 skel__flex_debug = trace_flag & trace_skeleton;
166 skel_lex ();
167 }
168
169 void
170 skel_scanner_free (void)
171 {
172 obstack_free (&obstack_for_string, 0);
173 /* Reclaim Flex's buffers. */
174 yylex_destroy ();
175 }
176
177 static void
178 at_directive_perform (int at_directive_argc,
179 char *at_directive_argv[],
180 char **outnamep, int *out_linenop)
181 {
182 if (STREQ (at_directive_argv[0], "@basename"))
183 {
184 if (at_directive_argc > 2)
185 fail_for_at_directive_too_many_args (at_directive_argv[0]);
186 fputs (last_component (at_directive_argv[1]), yyout);
187 }
188 else if (STREQ (at_directive_argv[0], "@warn")
189 || STREQ (at_directive_argv[0], "@complain")
190 || STREQ (at_directive_argv[0], "@fatal"))
191 {
192 warnings complaint_flag;
193 switch (at_directive_argv[0][1])
194 {
195 case 'w': complaint_flag = Wother; break;
196 case 'c': complaint_flag = complaint; break;
197 case 'f': complaint_flag = fatal; break;
198 default: aver (false); break;
199 }
200 switch (at_directive_argc)
201 {
202 case 2:
203 complain (complaint_flag, "%s", _(at_directive_argv[1]));
204 break;
205 case 3:
206 complain (complaint_flag, _(at_directive_argv[1]),
207 at_directive_argv[2]);
208 break;
209 case 4:
210 complain (complaint_flag, _(at_directive_argv[1]),
211 at_directive_argv[2], at_directive_argv[3]);
212 break;
213 case 5:
214 complain (complaint_flag, _(at_directive_argv[1]),
215 at_directive_argv[2], at_directive_argv[3],
216 at_directive_argv[4]);
217 break;
218 case 6:
219 complain (complaint_flag, _(at_directive_argv[1]),
220 at_directive_argv[2], at_directive_argv[3],
221 at_directive_argv[4], at_directive_argv[5]);
222 break;
223 default:
224 fail_for_at_directive_too_many_args (at_directive_argv[0]);
225 break;
226 }
227 }
228 else if (STREQ (at_directive_argv[0], "@warn_at")
229 || STREQ (at_directive_argv[0], "@complain_at")
230 || STREQ (at_directive_argv[0], "@fatal_at"))
231 {
232 warnings complaint_flag;
233 location loc;
234 if (at_directive_argc < 4)
235 fail_for_at_directive_too_few_args (at_directive_argv[0]);
236 switch (at_directive_argv[0][1])
237 {
238 case 'w': complaint_flag = Wother; break;
239 case 'c': complaint_flag = complaint; break;
240 case 'f': complaint_flag = fatal; break;
241 default: aver (false); break;
242 }
243 boundary_set_from_string (&loc.start, at_directive_argv[1]);
244 boundary_set_from_string (&loc.end, at_directive_argv[2]);
245 switch (at_directive_argc)
246 {
247 case 4:
248 complain_at (loc, complaint_flag, "%s", _(at_directive_argv[3]));
249 break;
250 case 5:
251 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
252 at_directive_argv[4]);
253 break;
254 case 6:
255 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
256 at_directive_argv[4], at_directive_argv[5]);
257 break;
258 case 7:
259 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
260 at_directive_argv[4], at_directive_argv[5],
261 at_directive_argv[6]);
262 break;
263 case 8:
264 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
265 at_directive_argv[4], at_directive_argv[5],
266 at_directive_argv[6], at_directive_argv[7]);
267 break;
268 default:
269 fail_for_at_directive_too_many_args (at_directive_argv[0]);
270 break;
271 }
272 }
273 else if (STREQ (at_directive_argv[0], "@output"))
274 {
275 if (at_directive_argc > 2)
276 fail_for_at_directive_too_many_args (at_directive_argv[0]);
277 if (*outnamep)
278 {
279 free (*outnamep);
280 xfclose (yyout);
281 }
282 *outnamep = xstrdup (at_directive_argv[1]);
283 output_file_name_check (outnamep);
284 yyout = xfopen (*outnamep, "w");
285 *out_linenop = 1;
286 }
287 else
288 fail_for_invalid_at (at_directive_argv[0]);
289 }
290
291 static void
292 fail_for_at_directive_too_few_args (char const *at_directive_name)
293 {
294 complain (fatal, _("too few arguments for %s directive in skeleton"),
295 at_directive_name);
296 }
297
298 static void
299 fail_for_at_directive_too_many_args (char const *at_directive_name)
300 {
301 complain (fatal, _("too many arguments for %s directive in skeleton"),
302 at_directive_name);
303 }
304
305 static void
306 fail_for_invalid_at (char const *at)
307 {
308 complain (fatal, "invalid @ in skeleton: %s", at);
309 }