]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
ed9d6d0dffc82addc96a772c52bfc6c9621818d8
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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 (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);
54
55 /* In SC_AT_DIRECTIVE_ARG context, the name of the directive. */
56 static char *at_directive_name;
57
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];
64 %}
65
66 %x SC_AT_DIRECTIVE_ARG
67 %x SC_AT_DIRECTIVE_SKIP_WS
68
69 %%
70
71 %{
72 int out_lineno IF_LINT (= 0);
73 char *outname = NULL;
74 %}
75
76 "@@" fputc ('@', yyout);
77 "@{" fputc ('[', yyout);
78 "@}" fputc (']', yyout);
79
80 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
81 "@ofile@" QPUTS (outname);
82 "@dir_prefix@" QPUTS (dir_prefix);
83
84 @[a-z_]+"(" {
85 yytext[yyleng-1] = '\0';
86 at_directive_name = xstrdup (yytext);
87 BEGIN SC_AT_DIRECTIVE_ARG;
88 }
89
90 /* This pattern must not match more than the previous @ patterns. */
91 @[^@{}(\n]* fail_for_invalid_at (yytext);
92 \n out_lineno++; ECHO;
93 [^@\n]+ ECHO;
94
95 <INITIAL><<EOF>> {
96 if (outname)
97 {
98 free (outname);
99 xfclose (yyout);
100 }
101 return EOF;
102 }
103
104 <SC_AT_DIRECTIVE_ARG>{
105 [^@]+ { STRING_GROW; }
106
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. */
112
113 @[,)] {
114 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
115 fail_for_at_directive_too_many_args ();
116
117 obstack_1grow (&obstack_for_string, '\0');
118 at_directive_argv[at_directive_argc++] =
119 obstack_finish (&obstack_for_string);
120
121 /* Like M4, skip whitespace after a comma. */
122 if (yytext[1] == ',')
123 BEGIN SC_AT_DIRECTIVE_SKIP_WS;
124 else
125 {
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);
130 BEGIN INITIAL;
131 }
132 }
133
134 @.? { fail_for_invalid_at (yytext); }
135 }
136
137 <SC_AT_DIRECTIVE_SKIP_WS>{
138 [ \t\r\n]
139 . { yyless (0); BEGIN SC_AT_DIRECTIVE_ARG; }
140 }
141
142 <SC_AT_DIRECTIVE_ARG,SC_AT_DIRECTIVE_SKIP_WS>{
143 <<EOF>> {
144 fatal (_("unclosed %s directive in skeleton"), at_directive_name);
145 }
146 }
147
148 %%
149
150 /*------------------------.
151 | Scan a Bison skeleton. |
152 `------------------------*/
153
154 void
155 scan_skel (FILE *in)
156 {
157 static bool initialized = false;
158 if (!initialized)
159 {
160 initialized = true;
161 obstack_init (&obstack_for_string);
162 }
163 skel_in = in;
164 skel__flex_debug = trace_flag & trace_skeleton;
165 skel_lex ();
166 /* Reclaim Flex's buffers. */
167 yylex_destroy ();
168 }
169
170 void
171 skel_scanner_free (void)
172 {
173 obstack_free (&obstack_for_string, 0);
174 }
175
176 static
177 void at_directive_perform (char **outnamep, int *out_linenop)
178 {
179 if (0 == strcmp (at_directive_name, "@basename"))
180 {
181 if (at_directive_argc > 1)
182 fail_for_at_directive_too_many_args ();
183 fputs (last_component (at_directive_argv[0]), yyout);
184 }
185 else if (0 == strcmp (at_directive_name, "@warn")
186 || 0 == strcmp (at_directive_name, "@complain")
187 || 0 == strcmp (at_directive_name, "@fatal"))
188 {
189 void (*func)(char const *, ...);
190 switch (at_directive_name[1])
191 {
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;
196 }
197 switch (at_directive_argc)
198 {
199 case 1:
200 func (_(at_directive_argv[0]));
201 break;
202 case 2:
203 func (_(at_directive_argv[0]), at_directive_argv[1]);
204 break;
205 case 3:
206 func (_(at_directive_argv[0]), at_directive_argv[1],
207 at_directive_argv[2]);
208 break;
209 case 4:
210 func (_(at_directive_argv[0]), at_directive_argv[1],
211 at_directive_argv[2], at_directive_argv[3]);
212 break;
213 case 5:
214 func (_(at_directive_argv[0]), at_directive_argv[1],
215 at_directive_argv[2], at_directive_argv[3],
216 at_directive_argv[4]);
217 break;
218 default:
219 fail_for_at_directive_too_many_args ();
220 break;
221 }
222 }
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"))
226 {
227 void (*func)(location, char const *, ...);
228 location loc;
229 if (at_directive_argc < 3)
230 fail_for_at_directive_too_few_args ();
231 switch (at_directive_name[1])
232 {
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;
237 }
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)
241 {
242 case 3:
243 func (loc, _(at_directive_argv[2]));
244 break;
245 case 4:
246 func (loc, _(at_directive_argv[2]), at_directive_argv[3]);
247 break;
248 case 5:
249 func (loc, _(at_directive_argv[2]), at_directive_argv[3],
250 at_directive_argv[4]);
251 break;
252 case 6:
253 func (loc, _(at_directive_argv[2]), at_directive_argv[3],
254 at_directive_argv[4], at_directive_argv[5]);
255 break;
256 case 7:
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]);
260 break;
261 default:
262 fail_for_at_directive_too_many_args ();
263 break;
264 }
265 }
266 else if (0 == strcmp (at_directive_name, "@output"))
267 {
268 if (at_directive_argc > 1)
269 fail_for_at_directive_too_many_args ();
270 if (*outnamep)
271 {
272 free (*outnamep);
273 xfclose (yyout);
274 }
275 *outnamep = xstrdup (at_directive_argv[0]);
276 output_file_name_check (*outnamep);
277 yyout = xfopen (*outnamep, "w");
278 *out_linenop = 1;
279 }
280 else
281 fail_for_invalid_at (at_directive_name);
282 }
283
284 static void
285 fail_for_at_directive_too_few_args (void)
286 {
287 fatal (_("too few arguments for %s directive in skeleton"),
288 at_directive_name);
289 }
290
291 static void
292 fail_for_at_directive_too_many_args (void)
293 {
294 fatal (_("too many arguments for %s directive in skeleton"),
295 at_directive_name);
296 }
297
298 static void
299 fail_for_invalid_at (char const *at)
300 {
301 fatal ("invalid @ in skeleton: %s", at);
302 }