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 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 %option nodefault noyywrap nounput never-interactive debug
22 %option prefix="skel_" outfile="lex.yy.c"
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
30 #define FLEX_PREFIX(Id) skel_ ## Id
31 #include "flex-scanner.h"
40 #include "scan-skel.h"
42 #define YY_DECL static int skel_lex (void)
45 #define QPUTS(String) \
46 fputs (quotearg_style (c_quoting_style, String), yyout)
48 static void at_directive_perform (int at_directive_argc,
49 char *at_directive_argv[],
50 char **outnamep, int *out_linenop);
51 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
52 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
53 static void fail_for_invalid_at (char const *at);
56 %x SC_AT_DIRECTIVE_ARGS
57 %x SC_AT_DIRECTIVE_SKIP_WS
62 int out_lineno IF_LINT (= 0);
65 /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and
66 @fatal_at directives take multiple arguments, and the last three already
67 can't take more than 7. at_directive_argv[0] is the directive name. */
68 #define AT_DIRECTIVE_ARGC_MAX 8
69 int at_directive_argc = 0;
70 char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
73 "@@" fputc ('@', yyout);
74 "@{" fputc ('[', yyout);
75 "@}" fputc (']', yyout);
77 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
78 "@ofile@" QPUTS (outname);
79 "@dir_prefix@" QPUTS (dir_prefix);
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 "@`" /* Emtpy. Useful for starting an argument
110 that begins with whitespace. */
113 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
114 fail_for_at_directive_too_many_args (at_directive_argv[0]);
116 obstack_1grow (&obstack_for_string, '\0');
117 at_directive_argv[at_directive_argc++] =
118 obstack_finish (&obstack_for_string);
120 /* Like M4, skip whitespace after a comma. */
121 if (yytext[1] == ',')
122 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
125 at_directive_perform (at_directive_argc, at_directive_argv,
126 &outname, &out_lineno);
127 obstack_free (&obstack_for_string, at_directive_argv[0]);
128 at_directive_argc = 0;
133 @.? { fail_for_invalid_at (yytext); }
136 <SC_AT_DIRECTIVE_SKIP_WS>{
138 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
141 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
143 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
149 /*------------------------.
150 | Scan a Bison skeleton. |
151 `------------------------*/
156 static bool initialized = false;
160 obstack_init (&obstack_for_string);
163 skel__flex_debug = trace_flag & trace_skeleton;
168 skel_scanner_free (void)
170 obstack_free (&obstack_for_string, 0);
171 /* Reclaim Flex's buffers. */
176 void at_directive_perform (int at_directive_argc,
177 char *at_directive_argv[],
178 char **outnamep, int *out_linenop)
180 if (0 == strcmp (at_directive_argv[0], "@basename"))
182 if (at_directive_argc > 2)
183 fail_for_at_directive_too_many_args (at_directive_argv[0]);
184 fputs (last_component (at_directive_argv[1]), yyout);
186 else if (0 == strcmp (at_directive_argv[0], "@warn")
187 || 0 == strcmp (at_directive_argv[0], "@complain")
188 || 0 == strcmp (at_directive_argv[0], "@fatal"))
190 void (*func)(char const *, ...);
191 switch (at_directive_argv[0][1])
193 case 'w': func = warn; break;
194 case 'c': func = complain; break;
195 case 'f': func = fatal; break;
196 default: aver (false); break;
198 switch (at_directive_argc)
201 func (_(at_directive_argv[1]));
204 func (_(at_directive_argv[1]), at_directive_argv[2]);
207 func (_(at_directive_argv[1]), at_directive_argv[2],
208 at_directive_argv[3]);
211 func (_(at_directive_argv[1]), at_directive_argv[2],
212 at_directive_argv[3], at_directive_argv[4]);
215 func (_(at_directive_argv[1]), at_directive_argv[2],
216 at_directive_argv[3], at_directive_argv[4],
217 at_directive_argv[5]);
220 fail_for_at_directive_too_many_args (at_directive_argv[0]);
224 else if (0 == strcmp (at_directive_argv[0], "@warn_at")
225 || 0 == strcmp (at_directive_argv[0], "@complain_at")
226 || 0 == strcmp (at_directive_argv[0], "@fatal_at"))
228 void (*func)(location, char const *, ...);
230 if (at_directive_argc < 4)
231 fail_for_at_directive_too_few_args (at_directive_argv[0]);
232 switch (at_directive_argv[0][1])
234 case 'w': func = warn_at; break;
235 case 'c': func = complain_at; break;
236 case 'f': func = fatal_at; break;
237 default: aver (false); break;
239 boundary_set_from_string (&loc.start, at_directive_argv[1]);
240 boundary_set_from_string (&loc.end, at_directive_argv[2]);
241 switch (at_directive_argc)
244 func (loc, _(at_directive_argv[3]));
247 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
250 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
251 at_directive_argv[5]);
254 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
255 at_directive_argv[5], at_directive_argv[6]);
258 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
259 at_directive_argv[5], at_directive_argv[6],
260 at_directive_argv[7]);
263 fail_for_at_directive_too_many_args (at_directive_argv[0]);
267 else if (0 == strcmp (at_directive_argv[0], "@output"))
269 if (at_directive_argc > 2)
270 fail_for_at_directive_too_many_args (at_directive_argv[0]);
276 *outnamep = xstrdup (at_directive_argv[1]);
277 output_file_name_check (*outnamep);
278 yyout = xfopen (*outnamep, "w");
282 fail_for_invalid_at (at_directive_argv[0]);
286 fail_for_at_directive_too_few_args (char const *at_directive_name)
288 fatal (_("too few arguments for %s directive in skeleton"),
293 fail_for_at_directive_too_many_args (char const *at_directive_name)
295 fatal (_("too many arguments for %s directive in skeleton"),
300 fail_for_invalid_at (char const *at)
302 fatal ("invalid @ in skeleton: %s", at);