1 /* Scan Bison Skeletons. -*- C -*-
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to the Free
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 %option nodefault noyywrap nounput never-interactive debug
24 %option prefix="skel_" outfile="lex.yy.c"
27 /* Work around a bug in flex 2.5.31. See Debian bug 333231
28 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
32 #define FLEX_PREFIX(Id) skel_ ## Id
33 #include "flex-scanner.h"
42 #include "scan-skel.h"
44 #define YY_DECL static int skel_lex (void)
47 #define QPUTS(String) \
48 fputs (quotearg_style (c_quoting_style, String), yyout)
50 static void at_directive_perform (char **outnamep, int *out_linenop);
51 static void fail_for_at_directive_too_many_args (void);
52 static void fail_for_at_directive_too_few_args (void);
53 static void fail_for_invalid_at (char const *at);
55 /* In SC_AT_DIRECTIVE_ARG context, the name of the directive. */
56 static char *at_directive_name = NULL;
58 /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and
59 @fatal_at directives take multiple arguments, and the last three already
60 can't take more than 7. */
61 #define AT_DIRECTIVE_ARGC_MAX 7
62 static int at_directive_argc = 0;
63 static char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
67 %x SC_AT_DIRECTIVE_ARGS
68 %x SC_AT_DIRECTIVE_SKIP_WS
73 int out_lineno IF_LINT (= 0);
77 "@@" fputc ('@', yyout);
78 "@{" fputc ('[', yyout);
79 "@}" fputc (']', yyout);
81 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
82 "@ofile@" QPUTS (outname);
83 "@dir_prefix@" QPUTS (dir_prefix);
85 "@gettext<" BEGIN SC_AT_GETTEXT_ARG;
88 yytext[yyleng-1] = '\0';
89 at_directive_name = xstrdup (yytext);
90 BEGIN SC_AT_DIRECTIVE_ARGS;
93 /* This pattern must not match more than the previous @ patterns. */
94 @[^<@{}(\n]* fail_for_invalid_at (yytext);
95 \n out_lineno++; ECHO;
110 obstack_1grow (&obstack_for_string, '\0');
111 arg = obstack_finish (&obstack_for_string);
112 if (!at_directive_name)
114 fprintf (yyout, "%s", _(arg));
115 obstack_free (&obstack_for_string, arg);
120 char const *translated = _(arg);
121 size_t parent_size = strlen (at_directive_argv[at_directive_argc]);
122 size_t translated_size = strlen (translated);
123 char *copy = xmalloc (parent_size + translated_size + 1);
124 strcpy (copy, at_directive_argv[at_directive_argc]);
125 strcpy (copy + parent_size, translated);
126 obstack_free (&obstack_for_string,
127 at_directive_argv[at_directive_argc]);
128 obstack_grow (&obstack_for_string, copy,
129 parent_size + translated_size);
131 BEGIN SC_AT_DIRECTIVE_ARGS;
136 <SC_AT_DIRECTIVE_ARGS>{
137 "@`" /* Emtpy. Useful for starting an argument
138 that begins with whitespace. */
141 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
142 fail_for_at_directive_too_many_args ();
143 obstack_1grow (&obstack_for_string, '\0');
144 at_directive_argv[at_directive_argc] =
145 obstack_finish (&obstack_for_string);
146 BEGIN SC_AT_GETTEXT_ARG;
150 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
151 fail_for_at_directive_too_many_args ();
153 obstack_1grow (&obstack_for_string, '\0');
154 at_directive_argv[at_directive_argc++] =
155 obstack_finish (&obstack_for_string);
157 /* Like M4, skip whitespace after a comma. */
158 if (yytext[1] == ',')
159 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
162 at_directive_perform (&outname, &out_lineno);
163 obstack_free (&obstack_for_string, at_directive_argv[0]);
164 at_directive_argc = 0;
165 free (at_directive_name);
166 at_directive_name = NULL;
172 <SC_AT_GETTEXT_ARG,SC_AT_DIRECTIVE_ARGS>{
173 [^@]+ { STRING_GROW; }
174 "@@" { obstack_1grow (&obstack_for_string, '@'); }
175 "@{" { obstack_1grow (&obstack_for_string, '['); }
176 "@}" { obstack_1grow (&obstack_for_string, ']'); }
177 @.? { fail_for_invalid_at (yytext); }
180 <SC_AT_DIRECTIVE_SKIP_WS>{
182 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
185 <SC_AT_GETTEXT_ARG,SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
187 fatal (_("unclosed %s directive in skeleton"),
188 at_directive_name ? at_directive_name : "@gettext");
194 /*------------------------.
195 | Scan a Bison skeleton. |
196 `------------------------*/
201 static bool initialized = false;
205 obstack_init (&obstack_for_string);
208 skel__flex_debug = trace_flag & trace_skeleton;
210 /* Reclaim Flex's buffers. */
215 skel_scanner_free (void)
217 obstack_free (&obstack_for_string, 0);
221 void at_directive_perform (char **outnamep, int *out_linenop)
223 if (0 == strcmp (at_directive_name, "@basename"))
225 if (at_directive_argc > 1)
226 fail_for_at_directive_too_many_args ();
227 fputs (last_component (at_directive_argv[0]), yyout);
229 else if (0 == strcmp (at_directive_name, "@warn")
230 || 0 == strcmp (at_directive_name, "@complain")
231 || 0 == strcmp (at_directive_name, "@fatal"))
233 void (*func)(char const *, ...);
234 switch (at_directive_name[1])
236 case 'w': func = warn; break;
237 case 'c': func = complain; break;
238 case 'f': func = fatal; break;
239 default: aver (false); func = NULL; break;
241 switch (at_directive_argc)
244 func (at_directive_argv[0]);
247 func (at_directive_argv[0], at_directive_argv[1]);
250 func (at_directive_argv[0], at_directive_argv[1],
251 at_directive_argv[2]);
254 func (at_directive_argv[0], at_directive_argv[1],
255 at_directive_argv[2], at_directive_argv[3]);
258 func (at_directive_argv[0], at_directive_argv[1],
259 at_directive_argv[2], at_directive_argv[3],
260 at_directive_argv[4]);
263 fail_for_at_directive_too_many_args ();
267 else if (0 == strcmp (at_directive_name, "@warn_at")
268 || 0 == strcmp (at_directive_name, "@complain_at")
269 || 0 == strcmp (at_directive_name, "@fatal_at"))
271 void (*func)(location, char const *, ...);
273 if (at_directive_argc < 3)
274 fail_for_at_directive_too_few_args ();
275 switch (at_directive_name[1])
277 case 'w': func = warn_at; break;
278 case 'c': func = complain_at; break;
279 case 'f': func = fatal_at; break;
280 default: aver (false); func = NULL; break;
282 boundary_set_from_string (&loc.start, at_directive_argv[0]);
283 boundary_set_from_string (&loc.end, at_directive_argv[1]);
284 switch (at_directive_argc)
287 func (loc, at_directive_argv[2]);
290 func (loc, at_directive_argv[2], at_directive_argv[3]);
293 func (loc, at_directive_argv[2], at_directive_argv[3],
294 at_directive_argv[4]);
297 func (loc, at_directive_argv[2], at_directive_argv[3],
298 at_directive_argv[4], at_directive_argv[5]);
301 func (loc, at_directive_argv[2], at_directive_argv[3],
302 at_directive_argv[4], at_directive_argv[5],
303 at_directive_argv[6]);
306 fail_for_at_directive_too_many_args ();
310 else if (0 == strcmp (at_directive_name, "@output"))
312 if (at_directive_argc > 1)
313 fail_for_at_directive_too_many_args ();
319 *outnamep = xstrdup (at_directive_argv[0]);
320 output_file_name_check (*outnamep);
321 yyout = xfopen (*outnamep, "w");
325 fail_for_invalid_at (at_directive_name);
329 fail_for_at_directive_too_few_args (void)
331 fatal (_("too few arguments for %s directive in skeleton"),
336 fail_for_at_directive_too_many_args (void)
338 fatal (_("too many arguments for %s directive in skeleton"),
343 fail_for_invalid_at (char const *at)
345 fatal ("invalid @ in skeleton: %s", at);