1 /* Scan Bison Skeletons. -*- C -*-
3 Copyright (C) 2001-2007, 2009-2010 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 "flex-scanner.h"
39 #include "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 IF_LINT (= 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 "@`" /* Emtpy. Used by b4_cat in ../data/bison.m4. */
78 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
79 "@ofile@" QPUTS (outname);
80 "@dir_prefix@" QPUTS (dir_prefix);
83 yytext[yyleng-1] = '\0';
84 obstack_grow (&obstack_for_string, yytext, yyleng);
85 at_directive_argv[at_directive_argc++] =
86 obstack_finish (&obstack_for_string);
87 BEGIN SC_AT_DIRECTIVE_ARGS;
90 /* This pattern must not match more than the previous @ patterns. */
91 @[^@{}`(\n]* fail_for_invalid_at (yytext);
92 \n out_lineno++; ECHO;
104 <SC_AT_DIRECTIVE_ARGS>{
105 [^@]+ { STRING_GROW; }
107 "@@" { obstack_1grow (&obstack_for_string, '@'); }
108 "@{" { obstack_1grow (&obstack_for_string, '['); }
109 "@}" { obstack_1grow (&obstack_for_string, ']'); }
110 "@`" /* Empty. Useful for starting an argument
111 that begins with whitespace. */
115 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
116 fail_for_at_directive_too_many_args (at_directive_argv[0]);
118 obstack_1grow (&obstack_for_string, '\0');
119 at_directive_argv[at_directive_argc++] =
120 obstack_finish (&obstack_for_string);
122 /* Like M4, skip whitespace after a comma. */
123 if (yytext[1] == ',')
124 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
127 at_directive_perform (at_directive_argc, at_directive_argv,
128 &outname, &out_lineno);
129 obstack_free (&obstack_for_string, at_directive_argv[0]);
130 at_directive_argc = 0;
135 @.? { fail_for_invalid_at (yytext); }
138 <SC_AT_DIRECTIVE_SKIP_WS>{
140 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
143 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
145 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
151 /*------------------------.
152 | Scan a Bison skeleton. |
153 `------------------------*/
158 static bool initialized = false;
162 obstack_init (&obstack_for_string);
165 skel__flex_debug = trace_flag & trace_skeleton;
170 skel_scanner_free (void)
172 obstack_free (&obstack_for_string, 0);
173 /* Reclaim Flex's buffers. */
178 at_directive_perform (int at_directive_argc,
179 char *at_directive_argv[],
180 char **outnamep, int *out_linenop)
182 if (0 == strcmp (at_directive_argv[0], "@basename"))
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);
188 else if (0 == strcmp (at_directive_argv[0], "@warn")
189 || 0 == strcmp (at_directive_argv[0], "@complain")
190 || 0 == strcmp (at_directive_argv[0], "@fatal"))
192 void (*func)(char const *, ...);
193 switch (at_directive_argv[0][1])
195 case 'w': func = warn; break;
196 case 'c': func = complain; break;
197 case 'f': func = fatal; break;
198 default: aver (false); break;
200 switch (at_directive_argc)
203 func (_(at_directive_argv[1]));
206 func (_(at_directive_argv[1]), at_directive_argv[2]);
209 func (_(at_directive_argv[1]), at_directive_argv[2],
210 at_directive_argv[3]);
213 func (_(at_directive_argv[1]), at_directive_argv[2],
214 at_directive_argv[3], at_directive_argv[4]);
217 func (_(at_directive_argv[1]), at_directive_argv[2],
218 at_directive_argv[3], at_directive_argv[4],
219 at_directive_argv[5]);
222 fail_for_at_directive_too_many_args (at_directive_argv[0]);
226 else if (0 == strcmp (at_directive_argv[0], "@warn_at")
227 || 0 == strcmp (at_directive_argv[0], "@complain_at")
228 || 0 == strcmp (at_directive_argv[0], "@fatal_at"))
230 void (*func)(location, char const *, ...);
232 if (at_directive_argc < 4)
233 fail_for_at_directive_too_few_args (at_directive_argv[0]);
234 switch (at_directive_argv[0][1])
236 case 'w': func = warn_at; break;
237 case 'c': func = complain_at; break;
238 case 'f': func = fatal_at; break;
239 default: aver (false); break;
241 boundary_set_from_string (&loc.start, at_directive_argv[1]);
242 boundary_set_from_string (&loc.end, at_directive_argv[2]);
243 switch (at_directive_argc)
246 func (loc, _(at_directive_argv[3]));
249 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
252 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
253 at_directive_argv[5]);
256 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
257 at_directive_argv[5], at_directive_argv[6]);
260 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
261 at_directive_argv[5], at_directive_argv[6],
262 at_directive_argv[7]);
265 fail_for_at_directive_too_many_args (at_directive_argv[0]);
269 else if (0 == strcmp (at_directive_argv[0], "@output"))
271 if (at_directive_argc > 2)
272 fail_for_at_directive_too_many_args (at_directive_argv[0]);
278 *outnamep = xstrdup (at_directive_argv[1]);
279 output_file_name_check (outnamep);
280 yyout = xfopen (*outnamep, "w");
284 fail_for_invalid_at (at_directive_argv[0]);
288 fail_for_at_directive_too_few_args (char const *at_directive_name)
290 fatal (_("too few arguments for %s directive in skeleton"),
295 fail_for_at_directive_too_many_args (char const *at_directive_name)
297 fatal (_("too many arguments for %s directive in skeleton"),
302 fail_for_invalid_at (char const *at)
304 fatal ("invalid @ in skeleton: %s", at);