]> git.saurik.com Git - bison.git/blame - src/scan-skel.l
Fix push parsing memory leak reported by Brandon Lucia at
[bison.git] / src / scan-skel.l
CommitLineData
be2a1a68 1/* Scan Bison Skeletons. -*- C -*-
353d3eb6 2
279cabb6 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
68cae94e 4 Foundation, Inc.
1239777d
AD
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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)
11 any later version.
12
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.
9b3add5b 17
1239777d
AD
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
0fb669f9
PE
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
9b3add5b 22
c5e3e510 23%option nodefault noyywrap nounput never-interactive debug
aed7fd9b 24%option prefix="skel_" outfile="lex.yy.c"
9b3add5b 25
aed7fd9b 26%{
4f6e011e
PE
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>. */
29#undef skel_wrap
30#define skel_wrap() 1
31
f9bfc42a
JD
32#define FLEX_PREFIX(Id) skel_ ## Id
33#include "flex-scanner.h"
353d3eb6 34
cb48f191 35#include <dirname.h>
353d3eb6
PE
36#include <error.h>
37#include <quotearg.h>
38
db5c43f7 39#include "complain.h"
536545f3 40#include "getargs.h"
be2a1a68 41#include "files.h"
04098407 42#include "scan-skel.h"
dc9701e8 43
08af01c2
JD
44#define YY_DECL static int skel_lex (void)
45YY_DECL;
06f01bc4 46
c5e3e510 47#define QPUTS(String) \
68cae94e 48 fputs (quotearg_style (c_quoting_style, String), yyout)
c5e3e510 49
3eb82471
JD
50static void at_directive_perform (int at_directive_argc,
51 char *at_directive_argv[],
52 char **outnamep, int *out_linenop);
53static void fail_for_at_directive_too_many_args (char const *at_directive_name);
54static void fail_for_at_directive_too_few_args (char const *at_directive_name);
08af01c2 55static void fail_for_invalid_at (char const *at);
aed7fd9b 56%}
08af01c2 57
3eb82471 58%x SC_AT_DIRECTIVE_ARGS
08af01c2
JD
59%x SC_AT_DIRECTIVE_SKIP_WS
60
9b3add5b 61%%
e9683cfd
PE
62
63%{
3fc65ead 64 int out_lineno IF_LINT (= 0);
e9683cfd 65 char *outname = NULL;
3eb82471
JD
66
67 /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and
68 @fatal_at directives take multiple arguments, and the last three already
69 can't take more than 7. at_directive_argv[0] is the directive name. */
70 #define AT_DIRECTIVE_ARGC_MAX 8
71 int at_directive_argc = 0;
72 char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
e9683cfd
PE
73%}
74
e9683cfd
PE
75"@@" fputc ('@', yyout);
76"@{" fputc ('[', yyout);
77"@}" fputc (']', yyout);
78
3fc65ead 79"@oline@" fprintf (yyout, "%d", out_lineno + 1);
c5e3e510 80"@ofile@" QPUTS (outname);
2b81e969 81"@dir_prefix@" QPUTS (dir_prefix);
bd9d212b 82
08af01c2
JD
83@[a-z_]+"(" {
84 yytext[yyleng-1] = '\0';
3eb82471
JD
85 obstack_grow (&obstack_for_string, yytext, yyleng);
86 at_directive_argv[at_directive_argc++] =
87 obstack_finish (&obstack_for_string);
88 BEGIN SC_AT_DIRECTIVE_ARGS;
bd9d212b 89}
e9683cfd 90
3fc16193 91 /* This pattern must not match more than the previous @ patterns. */
5f340b48 92@[^@{}(\n]* fail_for_invalid_at (yytext);
3fc65ead 93\n out_lineno++; ECHO;
68e93ad5 94[^@\n]+ ECHO;
e9683cfd 95
08af01c2 96<INITIAL><<EOF>> {
7ec5ab2e
PE
97 if (outname)
98 {
99 free (outname);
100 xfclose (yyout);
101 }
102 return EOF;
103}
08af01c2 104
3eb82471 105<SC_AT_DIRECTIVE_ARGS>{
5f340b48 106 [^@]+ { STRING_GROW; }
08af01c2 107
5f340b48
JD
108 "@@" { obstack_1grow (&obstack_for_string, '@'); }
109 "@{" { obstack_1grow (&obstack_for_string, '['); }
110 "@}" { obstack_1grow (&obstack_for_string, ']'); }
08af01c2
JD
111 "@`" /* Emtpy. Useful for starting an argument
112 that begins with whitespace. */
113
114 @[,)] {
115 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
3eb82471 116 fail_for_at_directive_too_many_args (at_directive_argv[0]);
08af01c2
JD
117
118 obstack_1grow (&obstack_for_string, '\0');
119 at_directive_argv[at_directive_argc++] =
120 obstack_finish (&obstack_for_string);
121
122 /* Like M4, skip whitespace after a comma. */
123 if (yytext[1] == ',')
124 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
125 else
126 {
3eb82471
JD
127 at_directive_perform (at_directive_argc, at_directive_argv,
128 &outname, &out_lineno);
08af01c2
JD
129 obstack_free (&obstack_for_string, at_directive_argv[0]);
130 at_directive_argc = 0;
08af01c2
JD
131 BEGIN INITIAL;
132 }
133 }
134
135 @.? { fail_for_invalid_at (yytext); }
136}
137
138<SC_AT_DIRECTIVE_SKIP_WS>{
3fc65ead 139 [ \t\r\n]
3eb82471 140 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
08af01c2
JD
141}
142
3eb82471 143<SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
08af01c2 144 <<EOF>> {
3eb82471 145 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
08af01c2
JD
146 }
147}
148
9b3add5b 149%%
536545f3 150
2cdb2a7b
PE
151/*------------------------.
152| Scan a Bison skeleton. |
153`------------------------*/
536545f3 154
536545f3 155void
2cdb2a7b 156scan_skel (FILE *in)
536545f3 157{
08af01c2
JD
158 static bool initialized = false;
159 if (!initialized)
160 {
161 initialized = true;
162 obstack_init (&obstack_for_string);
163 }
2cdb2a7b 164 skel_in = in;
c5e3e510 165 skel__flex_debug = trace_flag & trace_skeleton;
536545f3 166 skel_lex ();
536545f3 167}
bd9d212b 168
3fc65ead
JD
169void
170skel_scanner_free (void)
171{
172 obstack_free (&obstack_for_string, 0);
3eb82471
JD
173 /* Reclaim Flex's buffers. */
174 yylex_destroy ();
3fc65ead
JD
175}
176
177static
3eb82471
JD
178void at_directive_perform (int at_directive_argc,
179 char *at_directive_argv[],
180 char **outnamep, int *out_linenop)
3fc65ead 181{
3eb82471 182 if (0 == strcmp (at_directive_argv[0], "@basename"))
3fc65ead 183 {
3eb82471
JD
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);
3fc65ead 187 }
3eb82471
JD
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"))
3fc65ead
JD
191 {
192 void (*func)(char const *, ...);
3eb82471 193 switch (at_directive_argv[0][1])
3fc65ead
JD
194 {
195 case 'w': func = warn; break;
196 case 'c': func = complain; break;
197 case 'f': func = fatal; break;
3eb82471 198 default: aver (false); break;
3fc65ead
JD
199 }
200 switch (at_directive_argc)
201 {
87b0a375 202 case 2:
3eb82471 203 func (_(at_directive_argv[1]));
3fc65ead 204 break;
87b0a375 205 case 3:
3eb82471 206 func (_(at_directive_argv[1]), at_directive_argv[2]);
3fc65ead 207 break;
87b0a375 208 case 4:
3eb82471
JD
209 func (_(at_directive_argv[1]), at_directive_argv[2],
210 at_directive_argv[3]);
3fc65ead 211 break;
87b0a375 212 case 5:
3eb82471
JD
213 func (_(at_directive_argv[1]), at_directive_argv[2],
214 at_directive_argv[3], at_directive_argv[4]);
3fc65ead 215 break;
87b0a375 216 case 6:
3eb82471
JD
217 func (_(at_directive_argv[1]), at_directive_argv[2],
218 at_directive_argv[3], at_directive_argv[4],
219 at_directive_argv[5]);
3fc65ead
JD
220 break;
221 default:
3eb82471 222 fail_for_at_directive_too_many_args (at_directive_argv[0]);
3fc65ead
JD
223 break;
224 }
225 }
3eb82471
JD
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"))
3fc65ead
JD
229 {
230 void (*func)(location, char const *, ...);
231 location loc;
3eb82471
JD
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])
3fc65ead
JD
235 {
236 case 'w': func = warn_at; break;
237 case 'c': func = complain_at; break;
238 case 'f': func = fatal_at; break;
3eb82471 239 default: aver (false); break;
3fc65ead 240 }
3eb82471
JD
241 boundary_set_from_string (&loc.start, at_directive_argv[1]);
242 boundary_set_from_string (&loc.end, at_directive_argv[2]);
3fc65ead
JD
243 switch (at_directive_argc)
244 {
87b0a375 245 case 4:
3eb82471 246 func (loc, _(at_directive_argv[3]));
3fc65ead 247 break;
87b0a375 248 case 5:
3eb82471 249 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
3fc65ead 250 break;
87b0a375 251 case 6:
3eb82471
JD
252 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
253 at_directive_argv[5]);
3fc65ead 254 break;
87b0a375 255 case 7:
3eb82471
JD
256 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
257 at_directive_argv[5], at_directive_argv[6]);
3fc65ead 258 break;
87b0a375 259 case 8:
3eb82471
JD
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]);
3fc65ead
JD
263 break;
264 default:
3eb82471 265 fail_for_at_directive_too_many_args (at_directive_argv[0]);
3fc65ead
JD
266 break;
267 }
268 }
3eb82471 269 else if (0 == strcmp (at_directive_argv[0], "@output"))
3fc65ead 270 {
3eb82471
JD
271 if (at_directive_argc > 2)
272 fail_for_at_directive_too_many_args (at_directive_argv[0]);
3fc65ead
JD
273 if (*outnamep)
274 {
275 free (*outnamep);
276 xfclose (yyout);
277 }
3eb82471 278 *outnamep = xstrdup (at_directive_argv[1]);
3fc65ead
JD
279 output_file_name_check (*outnamep);
280 yyout = xfopen (*outnamep, "w");
281 *out_linenop = 1;
282 }
283 else
3eb82471 284 fail_for_invalid_at (at_directive_argv[0]);
3fc65ead
JD
285}
286
287static void
3eb82471 288fail_for_at_directive_too_few_args (char const *at_directive_name)
3fc65ead
JD
289{
290 fatal (_("too few arguments for %s directive in skeleton"),
291 at_directive_name);
292}
293
08af01c2 294static void
3eb82471 295fail_for_at_directive_too_many_args (char const *at_directive_name)
bd9d212b 296{
08af01c2
JD
297 fatal (_("too many arguments for %s directive in skeleton"),
298 at_directive_name);
299}
300
301static void
302fail_for_invalid_at (char const *at)
303{
304 fatal ("invalid @ in skeleton: %s", at);
305}