1 /* Scan Bison Skeletons. -*- C -*-
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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;
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];
66 %x SC_AT_DIRECTIVE_ARG
67 %x SC_AT_DIRECTIVE_SKIP_WS
72 int out_lineno IF_LINT (= 0);
76 "@@" fputc ('@', yyout);
77 "@{" fputc ('[', yyout);
78 "@}" fputc (']', yyout);
80 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
81 "@ofile@" QPUTS (outname);
82 "@dir_prefix@" QPUTS (dir_prefix);
85 yytext[yyleng-1] = '\0';
86 at_directive_name = xstrdup (yytext);
87 BEGIN SC_AT_DIRECTIVE_ARG;
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_ARG>{
105 [^@]+ { STRING_GROW; }
107 "@@" { obstack_1grow (&obstack_for_string, '@'); }
108 "@{" { obstack_1grow (&obstack_for_string, '['); }
109 "@}" { obstack_1grow (&obstack_for_string, ']'); }
110 "@`" /* Emtpy. Useful for starting an argument
111 that begins with whitespace. */
114 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
115 fail_for_at_directive_too_many_args ();
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 (&outname, &out_lineno);
127 obstack_free (&obstack_for_string, at_directive_argv[0]);
128 at_directive_argc = 0;
129 free (at_directive_name);
134 @.? { fail_for_invalid_at (yytext); }
137 <SC_AT_DIRECTIVE_SKIP_WS>{
139 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARG; }
142 <SC_AT_DIRECTIVE_ARG,SC_AT_DIRECTIVE_SKIP_WS>{
144 fatal (_("unclosed %s directive in skeleton"), at_directive_name);
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;
166 /* Reclaim Flex's buffers. */
171 skel_scanner_free (void)
173 obstack_free (&obstack_for_string, 0);
177 void at_directive_perform (char **outnamep, int *out_linenop)
179 if (0 == strcmp (at_directive_name, "@basename"))
181 if (at_directive_argc > 1)
182 fail_for_at_directive_too_many_args ();
183 fputs (last_component (at_directive_argv[0]), yyout);
185 else if (0 == strcmp (at_directive_name, "@warn")
186 || 0 == strcmp (at_directive_name, "@complain")
187 || 0 == strcmp (at_directive_name, "@fatal"))
189 void (*func)(char const *, ...);
190 switch (at_directive_name[1])
192 case 'w': func = warn; break;
193 case 'c': func = complain; break;
194 case 'f': func = fatal; break;
195 default: aver (false); func = NULL; break;
197 switch (at_directive_argc)
200 func (_(at_directive_argv[0]));
203 func (_(at_directive_argv[0]), at_directive_argv[1]);
206 func (_(at_directive_argv[0]), at_directive_argv[1],
207 at_directive_argv[2]);
210 func (_(at_directive_argv[0]), at_directive_argv[1],
211 at_directive_argv[2], at_directive_argv[3]);
214 func (_(at_directive_argv[0]), at_directive_argv[1],
215 at_directive_argv[2], at_directive_argv[3],
216 at_directive_argv[4]);
219 fail_for_at_directive_too_many_args ();
223 else if (0 == strcmp (at_directive_name, "@warn_at")
224 || 0 == strcmp (at_directive_name, "@complain_at")
225 || 0 == strcmp (at_directive_name, "@fatal_at"))
227 void (*func)(location, char const *, ...);
229 if (at_directive_argc < 3)
230 fail_for_at_directive_too_few_args ();
231 switch (at_directive_name[1])
233 case 'w': func = warn_at; break;
234 case 'c': func = complain_at; break;
235 case 'f': func = fatal_at; break;
236 default: aver (false); func = NULL; break;
238 boundary_set_from_string (&loc.start, at_directive_argv[0]);
239 boundary_set_from_string (&loc.end, at_directive_argv[1]);
240 switch (at_directive_argc)
243 func (loc, _(at_directive_argv[2]));
246 func (loc, _(at_directive_argv[2]), at_directive_argv[3]);
249 func (loc, _(at_directive_argv[2]), at_directive_argv[3],
250 at_directive_argv[4]);
253 func (loc, _(at_directive_argv[2]), at_directive_argv[3],
254 at_directive_argv[4], at_directive_argv[5]);
257 func (loc, _(at_directive_argv[2]), at_directive_argv[3],
258 at_directive_argv[4], at_directive_argv[5],
259 at_directive_argv[6]);
262 fail_for_at_directive_too_many_args ();
266 else if (0 == strcmp (at_directive_name, "@output"))
268 if (at_directive_argc > 1)
269 fail_for_at_directive_too_many_args ();
275 *outnamep = xstrdup (at_directive_argv[0]);
276 output_file_name_check (*outnamep);
277 yyout = xfopen (*outnamep, "w");
281 fail_for_invalid_at (at_directive_name);
285 fail_for_at_directive_too_few_args (void)
287 fatal (_("too few arguments for %s directive in skeleton"),
292 fail_for_at_directive_too_many_args (void)
294 fatal (_("too many arguments for %s directive in skeleton"),
299 fail_for_invalid_at (char const *at)
301 fatal ("invalid @ in skeleton: %s", at);