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 "@`" /* Empty. 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>{
104 [^@]+ { STRING_GROW; }
106 "@@" { obstack_1grow (&obstack_for_string, '@'); }
107 "@{" { obstack_1grow (&obstack_for_string, '['); }
108 "@}" { obstack_1grow (&obstack_for_string, ']'); }
109 "@`" /* Empty. Useful for starting an argument
110 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>{
139 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
142 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
144 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
150 /*------------------------.
151 | Scan a Bison skeleton. |
152 `------------------------*/
157 static bool initialized = false;
161 obstack_init (&obstack_for_string);
164 skel__flex_debug = trace_flag & trace_skeleton;
169 skel_scanner_free (void)
171 obstack_free (&obstack_for_string, 0);
172 /* Reclaim Flex's buffers. */
177 at_directive_perform (int at_directive_argc,
178 char *at_directive_argv[],
179 char **outnamep, int *out_linenop)
181 if (STREQ (at_directive_argv[0], "@basename"))
183 if (at_directive_argc > 2)
184 fail_for_at_directive_too_many_args (at_directive_argv[0]);
185 fputs (last_component (at_directive_argv[1]), yyout);
187 else if (STREQ (at_directive_argv[0], "@warn")
188 || STREQ (at_directive_argv[0], "@complain")
189 || STREQ (at_directive_argv[0], "@fatal"))
191 void (*func)(char const *, ...);
192 switch (at_directive_argv[0][1])
194 case 'w': func = warn; break;
195 case 'c': func = complain; break;
196 case 'f': func = fatal; break;
197 default: aver (false); break;
199 switch (at_directive_argc)
202 func (_(at_directive_argv[1]));
205 func (_(at_directive_argv[1]), at_directive_argv[2]);
208 func (_(at_directive_argv[1]), at_directive_argv[2],
209 at_directive_argv[3]);
212 func (_(at_directive_argv[1]), at_directive_argv[2],
213 at_directive_argv[3], at_directive_argv[4]);
216 func (_(at_directive_argv[1]), at_directive_argv[2],
217 at_directive_argv[3], at_directive_argv[4],
218 at_directive_argv[5]);
221 fail_for_at_directive_too_many_args (at_directive_argv[0]);
225 else if (STREQ (at_directive_argv[0], "@warn_at")
226 || STREQ (at_directive_argv[0], "@complain_at")
227 || STREQ (at_directive_argv[0], "@fatal_at"))
229 void (*func)(location, char const *, ...);
231 if (at_directive_argc < 4)
232 fail_for_at_directive_too_few_args (at_directive_argv[0]);
233 switch (at_directive_argv[0][1])
235 case 'w': func = warn_at; break;
236 case 'c': func = complain_at; break;
237 case 'f': func = fatal_at; break;
238 default: aver (false); break;
240 boundary_set_from_string (&loc.start, at_directive_argv[1]);
241 boundary_set_from_string (&loc.end, at_directive_argv[2]);
242 switch (at_directive_argc)
245 func (loc, _(at_directive_argv[3]));
248 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
251 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
252 at_directive_argv[5]);
255 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
256 at_directive_argv[5], at_directive_argv[6]);
259 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
260 at_directive_argv[5], at_directive_argv[6],
261 at_directive_argv[7]);
264 fail_for_at_directive_too_many_args (at_directive_argv[0]);
268 else if (STREQ (at_directive_argv[0], "@output"))
270 if (at_directive_argc > 2)
271 fail_for_at_directive_too_many_args (at_directive_argv[0]);
277 *outnamep = xstrdup (at_directive_argv[1]);
278 output_file_name_check (outnamep);
279 yyout = xfopen (*outnamep, "w");
283 fail_for_invalid_at (at_directive_argv[0]);
287 fail_for_at_directive_too_few_args (char const *at_directive_name)
289 fatal (_("too few arguments for %s directive in skeleton"),
294 fail_for_at_directive_too_many_args (char const *at_directive_name)
296 fatal (_("too many arguments for %s directive in skeleton"),
301 fail_for_invalid_at (char const *at)
303 fatal ("invalid @ in skeleton: %s", at);