1 /* Scan Bison Skeletons. -*- C -*-
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
20 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
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>. */
29 #define FLEX_PREFIX(Id) skel_ ## Id
30 #include <src/flex-scanner.h>
36 #include <src/complain.h>
37 #include <src/getargs.h>
38 #include <src/files.h>
39 #include <src/scan-skel.h>
41 #define YY_DECL static int skel_lex (void)
44 #define QPUTS(String) \
45 fputs (quotearg_style (c_quoting_style, String), yyout)
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);
55 %x SC_AT_DIRECTIVE_ARGS
56 %x SC_AT_DIRECTIVE_SKIP_WS
61 int out_lineno PACIFY_CC (= 0);
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];
72 "@@" fputc ('@', yyout);
73 "@{" fputc ('[', yyout);
74 "@}" fputc (']', yyout);
75 "@`" continue; /* Used by b4_cat in ../data/bison.m4. */
78 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
79 "@ofile@" QPUTS (outname);
82 yytext[yyleng-1] = '\0';
83 obstack_grow (&obstack_for_string, yytext, yyleng);
84 at_directive_argv[at_directive_argc++] =
85 obstack_finish (&obstack_for_string);
86 BEGIN SC_AT_DIRECTIVE_ARGS;
89 /* This pattern must not match more than the previous @ patterns. */
90 @[^@{}`(\n]* fail_for_invalid_at (yytext);
91 \n out_lineno++; ECHO;
103 <SC_AT_DIRECTIVE_ARGS>
107 "@@" obstack_1grow (&obstack_for_string, '@');
108 "@{" obstack_1grow (&obstack_for_string, '[');
109 "@}" obstack_1grow (&obstack_for_string, ']');
110 "@`" continue; /* For starting an argument that begins with whitespace. */
114 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
115 fail_for_at_directive_too_many_args (at_directive_argv[0]);
117 obstack_1grow (&obstack_for_string, '\0');
118 at_directive_argv[at_directive_argc++] =
119 obstack_finish (&obstack_for_string);
121 /* Like M4, skip whitespace after a comma. */
122 if (yytext[1] == ',')
123 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
126 at_directive_perform (at_directive_argc, at_directive_argv,
127 &outname, &out_lineno);
128 obstack_free (&obstack_for_string, at_directive_argv[0]);
129 at_directive_argc = 0;
134 @.? fail_for_invalid_at (yytext);
137 <SC_AT_DIRECTIVE_SKIP_WS>
140 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
143 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>
146 complain (fatal, _("unclosed %s directive in skeleton"),
147 at_directive_argv[0]);
153 /*------------------------.
154 | Scan a Bison skeleton. |
155 `------------------------*/
160 static bool initialized = false;
164 obstack_init (&obstack_for_string);
167 skel__flex_debug = trace_flag & trace_skeleton;
172 skel_scanner_free (void)
174 obstack_free (&obstack_for_string, 0);
175 /* Reclaim Flex's buffers. */
180 at_directive_perform (int at_directive_argc,
181 char *at_directive_argv[],
182 char **outnamep, int *out_linenop)
184 if (STREQ (at_directive_argv[0], "@basename"))
186 if (at_directive_argc > 2)
187 fail_for_at_directive_too_many_args (at_directive_argv[0]);
188 fputs (last_component (at_directive_argv[1]), yyout);
190 else if (STREQ (at_directive_argv[0], "@warn")
191 || STREQ (at_directive_argv[0], "@complain")
192 || STREQ (at_directive_argv[0], "@fatal"))
194 warnings complaint_flag;
195 switch (at_directive_argv[0][1])
197 case 'w': complaint_flag = Wother; break;
198 case 'c': complaint_flag = complaint; break;
199 case 'f': complaint_flag = fatal; break;
200 default: aver (false); break;
202 switch (at_directive_argc)
205 complain (complaint_flag, "%s", _(at_directive_argv[1]));
208 complain (complaint_flag, _(at_directive_argv[1]),
209 at_directive_argv[2]);
212 complain (complaint_flag, _(at_directive_argv[1]),
213 at_directive_argv[2], at_directive_argv[3]);
216 complain (complaint_flag, _(at_directive_argv[1]),
217 at_directive_argv[2], at_directive_argv[3],
218 at_directive_argv[4]);
221 complain (complaint_flag, _(at_directive_argv[1]),
222 at_directive_argv[2], at_directive_argv[3],
223 at_directive_argv[4], at_directive_argv[5]);
226 fail_for_at_directive_too_many_args (at_directive_argv[0]);
230 else if (STREQ (at_directive_argv[0], "@warn_at")
231 || STREQ (at_directive_argv[0], "@complain_at")
232 || STREQ (at_directive_argv[0], "@fatal_at"))
234 warnings complaint_flag;
236 if (at_directive_argc < 4)
237 fail_for_at_directive_too_few_args (at_directive_argv[0]);
238 switch (at_directive_argv[0][1])
240 case 'w': complaint_flag = Wother; break;
241 case 'c': complaint_flag = complaint; break;
242 case 'f': complaint_flag = fatal; break;
243 default: aver (false); break;
245 boundary_set_from_string (&loc.start, at_directive_argv[1]);
246 boundary_set_from_string (&loc.end, at_directive_argv[2]);
247 switch (at_directive_argc)
250 complain_at (loc, complaint_flag, "%s", _(at_directive_argv[3]));
253 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
254 at_directive_argv[4]);
257 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
258 at_directive_argv[4], at_directive_argv[5]);
261 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
262 at_directive_argv[4], at_directive_argv[5],
263 at_directive_argv[6]);
266 complain_at (loc, complaint_flag, _(at_directive_argv[3]),
267 at_directive_argv[4], at_directive_argv[5],
268 at_directive_argv[6], at_directive_argv[7]);
271 fail_for_at_directive_too_many_args (at_directive_argv[0]);
275 else if (STREQ (at_directive_argv[0], "@output"))
277 if (at_directive_argc > 2)
278 fail_for_at_directive_too_many_args (at_directive_argv[0]);
284 *outnamep = xstrdup (at_directive_argv[1]);
285 output_file_name_check (outnamep);
286 yyout = xfopen (*outnamep, "w");
290 fail_for_invalid_at (at_directive_argv[0]);
294 fail_for_at_directive_too_few_args (char const *at_directive_name)
296 complain (fatal, _("too few arguments for %s directive in skeleton"),
301 fail_for_at_directive_too_many_args (char const *at_directive_name)
303 complain (fatal, _("too many arguments for %s directive in skeleton"),
308 fail_for_invalid_at (char const *at)
310 complain (fatal, "invalid @ in skeleton: %s", at);