]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
2007-02-27 Paolo Bonzini <bonzini@gnu.org>
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Foundation, Inc.
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.
17
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
21 02110-1301, USA. */
22
23 %option nodefault noyywrap nounput never-interactive debug
24 %option prefix="skel_" outfile="lex.yy.c"
25
26 %{
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
32 #define FLEX_PREFIX(Id) skel_ ## Id
33 #include "flex-scanner.h"
34
35 #include <dirname.h>
36 #include <error.h>
37 #include <quotearg.h>
38
39 #include "complain.h"
40 #include "getargs.h"
41 #include "files.h"
42 #include "scan-skel.h"
43
44 #define YY_DECL static int skel_lex (void)
45 YY_DECL;
46
47 #define QPUTS(String) \
48 fputs (quotearg_style (c_quoting_style, String), yyout)
49
50 static void at_directive_perform (int at_directive_argc,
51 char *at_directive_argv[],
52 char **outnamep, int *out_linenop);
53 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
54 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
55 static void fail_for_invalid_at (char const *at);
56 %}
57
58 %x SC_AT_DIRECTIVE_ARGS
59 %x SC_AT_DIRECTIVE_SKIP_WS
60
61 %%
62
63 %{
64 int out_lineno IF_LINT (= 0);
65 char *outname = NULL;
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];
73 %}
74
75 "@@" fputc ('@', yyout);
76 "@{" fputc ('[', yyout);
77 "@}" fputc (']', yyout);
78
79 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
80 "@ofile@" QPUTS (outname);
81 "@dir_prefix@" QPUTS (dir_prefix);
82
83 @[a-z_]+"(" {
84 yytext[yyleng-1] = '\0';
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;
89 }
90
91 /* This pattern must not match more than the previous @ patterns. */
92 @[^@{}(\n]* fail_for_invalid_at (yytext);
93 \n out_lineno++; ECHO;
94 [^@\n]+ ECHO;
95
96 <INITIAL><<EOF>> {
97 if (outname)
98 {
99 free (outname);
100 xfclose (yyout);
101 }
102 return EOF;
103 }
104
105 <SC_AT_DIRECTIVE_ARGS>{
106 [^@]+ { STRING_GROW; }
107
108 "@@" { obstack_1grow (&obstack_for_string, '@'); }
109 "@{" { obstack_1grow (&obstack_for_string, '['); }
110 "@}" { obstack_1grow (&obstack_for_string, ']'); }
111 "@`" /* Emtpy. Useful for starting an argument
112 that begins with whitespace. */
113
114 @[,)] {
115 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
116 fail_for_at_directive_too_many_args (at_directive_argv[0]);
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 {
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;
131 BEGIN INITIAL;
132 }
133 }
134
135 @.? { fail_for_invalid_at (yytext); }
136 }
137
138 <SC_AT_DIRECTIVE_SKIP_WS>{
139 [ \t\r\n]
140 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; }
141 }
142
143 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
144 <<EOF>> {
145 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
146 }
147 }
148
149 %%
150
151 /*------------------------.
152 | Scan a Bison skeleton. |
153 `------------------------*/
154
155 void
156 scan_skel (FILE *in)
157 {
158 static bool initialized = false;
159 if (!initialized)
160 {
161 initialized = true;
162 obstack_init (&obstack_for_string);
163 }
164 skel_in = in;
165 skel__flex_debug = trace_flag & trace_skeleton;
166 skel_lex ();
167 }
168
169 void
170 skel_scanner_free (void)
171 {
172 obstack_free (&obstack_for_string, 0);
173 /* Reclaim Flex's buffers. */
174 yylex_destroy ();
175 }
176
177 static
178 void at_directive_perform (int at_directive_argc,
179 char *at_directive_argv[],
180 char **outnamep, int *out_linenop)
181 {
182 if (0 == strcmp (at_directive_argv[0], "@basename"))
183 {
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);
187 }
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"))
191 {
192 void (*func)(char const *, ...);
193 switch (at_directive_argv[0][1])
194 {
195 case 'w': func = warn; break;
196 case 'c': func = complain; break;
197 case 'f': func = fatal; break;
198 default: aver (false); break;
199 }
200 switch (at_directive_argc)
201 {
202 case 2:
203 func (_(at_directive_argv[1]));
204 break;
205 case 3:
206 func (_(at_directive_argv[1]), at_directive_argv[2]);
207 break;
208 case 4:
209 func (_(at_directive_argv[1]), at_directive_argv[2],
210 at_directive_argv[3]);
211 break;
212 case 5:
213 func (_(at_directive_argv[1]), at_directive_argv[2],
214 at_directive_argv[3], at_directive_argv[4]);
215 break;
216 case 6:
217 func (_(at_directive_argv[1]), at_directive_argv[2],
218 at_directive_argv[3], at_directive_argv[4],
219 at_directive_argv[5]);
220 break;
221 default:
222 fail_for_at_directive_too_many_args (at_directive_argv[0]);
223 break;
224 }
225 }
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"))
229 {
230 void (*func)(location, char const *, ...);
231 location loc;
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])
235 {
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;
240 }
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)
244 {
245 case 4:
246 func (loc, _(at_directive_argv[3]));
247 break;
248 case 5:
249 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
250 break;
251 case 6:
252 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
253 at_directive_argv[5]);
254 break;
255 case 7:
256 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
257 at_directive_argv[5], at_directive_argv[6]);
258 break;
259 case 8:
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]);
263 break;
264 default:
265 fail_for_at_directive_too_many_args (at_directive_argv[0]);
266 break;
267 }
268 }
269 else if (0 == strcmp (at_directive_argv[0], "@output"))
270 {
271 if (at_directive_argc > 2)
272 fail_for_at_directive_too_many_args (at_directive_argv[0]);
273 if (*outnamep)
274 {
275 free (*outnamep);
276 xfclose (yyout);
277 }
278 *outnamep = xstrdup (at_directive_argv[1]);
279 output_file_name_check (*outnamep);
280 yyout = xfopen (*outnamep, "w");
281 *out_linenop = 1;
282 }
283 else
284 fail_for_invalid_at (at_directive_argv[0]);
285 }
286
287 static void
288 fail_for_at_directive_too_few_args (char const *at_directive_name)
289 {
290 fatal (_("too few arguments for %s directive in skeleton"),
291 at_directive_name);
292 }
293
294 static void
295 fail_for_at_directive_too_many_args (char const *at_directive_name)
296 {
297 fatal (_("too many arguments for %s directive in skeleton"),
298 at_directive_name);
299 }
300
301 static void
302 fail_for_invalid_at (char const *at)
303 {
304 fatal ("invalid @ in skeleton: %s", at);
305 }