]> git.saurik.com Git - bison.git/blob - src/scan-skel.l
Merge remote-tracking branch 'origin/maint'
[bison.git] / src / scan-skel.l
1 /* Scan Bison Skeletons. -*- C -*-
2
3 Copyright (C) 2001-2012 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 %option nodefault noyywrap noinput nounput never-interactive debug
21 %option prefix="skel_" outfile="lex.yy.c"
22
23 %{
24 /* Work around a bug in flex 2.5.31. See Debian bug 333231
25 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
26 #undef skel_wrap
27 #define skel_wrap() 1
28
29 #define FLEX_PREFIX(Id) skel_ ## Id
30 #include <src/flex-scanner.h>
31
32 #include <dirname.h>
33 #include <error.h>
34 #include <quotearg.h>
35
36 #include <src/complain.h>
37 #include <src/getargs.h>
38 #include <src/files.h>
39 #include <src/scan-skel.h>
40
41 #define YY_DECL static int skel_lex (void)
42 YY_DECL;
43
44 #define QPUTS(String) \
45 fputs (quotearg_style (c_quoting_style, String), yyout)
46
47 static void at_directive_perform (int at_directive_argc,
48 char *at_directive_argv[],
49 char **outnamep, int *out_linenop);
50 static void fail_for_at_directive_too_many_args (char const *at_directive_name);
51 static void fail_for_at_directive_too_few_args (char const *at_directive_name);
52 static void fail_for_invalid_at (char const *at);
53 %}
54
55 %x SC_AT_DIRECTIVE_ARGS
56 %x SC_AT_DIRECTIVE_SKIP_WS
57
58 %%
59
60 %{
61 int out_lineno PACIFY_CC (= 0);
62 char *outname = NULL;
63
64 /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and
65 @fatal_at directives take multiple arguments, and the last three already
66 can't take more than 7. at_directive_argv[0] is the directive name. */
67 #define AT_DIRECTIVE_ARGC_MAX 8
68 int at_directive_argc = 0;
69 char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX];
70 %}
71
72 "@@" fputc ('@', yyout);
73 "@{" fputc ('[', yyout);
74 "@}" fputc (']', yyout);
75 "@`" /* Empty. Used by b4_cat in ../data/bison.m4. */
76 @\n /* Likewise. */
77
78 "@oline@" fprintf (yyout, "%d", out_lineno + 1);
79 "@ofile@" QPUTS (outname);
80
81 @[a-z_]+"(" {
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;
87 }
88
89 /* This pattern must not match more than the previous @ patterns. */
90 @[^@{}`(\n]* fail_for_invalid_at (yytext);
91 \n out_lineno++; ECHO;
92 [^@\n]+ ECHO;
93
94 <INITIAL><<EOF>> {
95 if (outname)
96 {
97 free (outname);
98 xfclose (yyout);
99 }
100 return EOF;
101 }
102
103 <SC_AT_DIRECTIVE_ARGS>{
104 [^@]+ { STRING_GROW; }
105
106 "@@" { obstack_1grow (&obstack_for_string, '@'); }
107 "@{" { obstack_1grow (&obstack_for_string, '['); }
108 "@}" { obstack_1grow (&obstack_for_string, ']'); }
109 "@`" /* Empty. Useful for starting an argument
110 that begins with whitespace. */
111 @\n /* Empty. */
112
113 @[,)] {
114 if (at_directive_argc >= AT_DIRECTIVE_ARGC_MAX)
115 fail_for_at_directive_too_many_args (at_directive_argv[0]);
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 (at_directive_argc, at_directive_argv,
127 &outname, &out_lineno);
128 obstack_free (&obstack_for_string, at_directive_argv[0]);
129 at_directive_argc = 0;
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_ARGS; }
140 }
141
142 <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS>{
143 <<EOF>> {
144 fatal (_("unclosed %s directive in skeleton"), at_directive_argv[0]);
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 }
167
168 void
169 skel_scanner_free (void)
170 {
171 obstack_free (&obstack_for_string, 0);
172 /* Reclaim Flex's buffers. */
173 yylex_destroy ();
174 }
175
176 static void
177 at_directive_perform (int at_directive_argc,
178 char *at_directive_argv[],
179 char **outnamep, int *out_linenop)
180 {
181 if (STREQ (at_directive_argv[0], "@basename"))
182 {
183 if (at_directive_argc > 2)
184 fail_for_at_directive_too_many_args (at_directive_argv[0]);
185 fputs (last_component (at_directive_argv[1]), yyout);
186 }
187 else if (STREQ (at_directive_argv[0], "@warn")
188 || STREQ (at_directive_argv[0], "@complain")
189 || STREQ (at_directive_argv[0], "@fatal"))
190 {
191 void (*func)(char const *, ...);
192 switch (at_directive_argv[0][1])
193 {
194 case 'w': func = warn; break;
195 case 'c': func = complain; break;
196 case 'f': func = fatal; break;
197 default: aver (false); break;
198 }
199 switch (at_directive_argc)
200 {
201 case 2:
202 func (_(at_directive_argv[1]));
203 break;
204 case 3:
205 func (_(at_directive_argv[1]), at_directive_argv[2]);
206 break;
207 case 4:
208 func (_(at_directive_argv[1]), at_directive_argv[2],
209 at_directive_argv[3]);
210 break;
211 case 5:
212 func (_(at_directive_argv[1]), at_directive_argv[2],
213 at_directive_argv[3], at_directive_argv[4]);
214 break;
215 case 6:
216 func (_(at_directive_argv[1]), at_directive_argv[2],
217 at_directive_argv[3], at_directive_argv[4],
218 at_directive_argv[5]);
219 break;
220 default:
221 fail_for_at_directive_too_many_args (at_directive_argv[0]);
222 break;
223 }
224 }
225 else if (STREQ (at_directive_argv[0], "@warn_at")
226 || STREQ (at_directive_argv[0], "@complain_at")
227 || STREQ (at_directive_argv[0], "@fatal_at"))
228 {
229 void (*func)(location, char const *, ...);
230 location loc;
231 if (at_directive_argc < 4)
232 fail_for_at_directive_too_few_args (at_directive_argv[0]);
233 switch (at_directive_argv[0][1])
234 {
235 case 'w': func = warn_at; break;
236 case 'c': func = complain_at; break;
237 case 'f': func = fatal_at; break;
238 default: aver (false); break;
239 }
240 boundary_set_from_string (&loc.start, at_directive_argv[1]);
241 boundary_set_from_string (&loc.end, at_directive_argv[2]);
242 switch (at_directive_argc)
243 {
244 case 4:
245 func (loc, _(at_directive_argv[3]));
246 break;
247 case 5:
248 func (loc, _(at_directive_argv[3]), at_directive_argv[4]);
249 break;
250 case 6:
251 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
252 at_directive_argv[5]);
253 break;
254 case 7:
255 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
256 at_directive_argv[5], at_directive_argv[6]);
257 break;
258 case 8:
259 func (loc, _(at_directive_argv[3]), at_directive_argv[4],
260 at_directive_argv[5], at_directive_argv[6],
261 at_directive_argv[7]);
262 break;
263 default:
264 fail_for_at_directive_too_many_args (at_directive_argv[0]);
265 break;
266 }
267 }
268 else if (STREQ (at_directive_argv[0], "@output"))
269 {
270 if (at_directive_argc > 2)
271 fail_for_at_directive_too_many_args (at_directive_argv[0]);
272 if (*outnamep)
273 {
274 free (*outnamep);
275 xfclose (yyout);
276 }
277 *outnamep = xstrdup (at_directive_argv[1]);
278 output_file_name_check (outnamep);
279 yyout = xfopen (*outnamep, "w");
280 *out_linenop = 1;
281 }
282 else
283 fail_for_invalid_at (at_directive_argv[0]);
284 }
285
286 static void
287 fail_for_at_directive_too_few_args (char const *at_directive_name)
288 {
289 fatal (_("too few arguments for %s directive in skeleton"),
290 at_directive_name);
291 }
292
293 static void
294 fail_for_at_directive_too_many_args (char const *at_directive_name)
295 {
296 fatal (_("too many arguments for %s directive in skeleton"),
297 at_directive_name);
298 }
299
300 static void
301 fail_for_invalid_at (char const *at)
302 {
303 fatal ("invalid @ in skeleton: %s", at);
304 }