]>
Commit | Line | Data |
---|---|---|
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) |
45 | YY_DECL; | |
06f01bc4 | 46 | |
c5e3e510 | 47 | #define QPUTS(String) \ |
68cae94e | 48 | fputs (quotearg_style (c_quoting_style, String), yyout) |
c5e3e510 | 49 | |
3fc65ead | 50 | static void at_directive_perform (char **outnamep, int *out_linenop); |
08af01c2 | 51 | static void fail_for_at_directive_too_many_args (void); |
3fc65ead | 52 | static void fail_for_at_directive_too_few_args (void); |
08af01c2 JD |
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 | ||
3fc65ead JD |
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 | |
08af01c2 JD |
62 | static int at_directive_argc = 0; |
63 | static char *at_directive_argv[AT_DIRECTIVE_ARGC_MAX]; | |
aed7fd9b | 64 | %} |
08af01c2 JD |
65 | |
66 | %x SC_AT_DIRECTIVE_ARG | |
67 | %x SC_AT_DIRECTIVE_SKIP_WS | |
68 | ||
9b3add5b | 69 | %% |
e9683cfd PE |
70 | |
71 | %{ | |
3fc65ead | 72 | int out_lineno IF_LINT (= 0); |
e9683cfd PE |
73 | char *outname = NULL; |
74 | %} | |
75 | ||
e9683cfd PE |
76 | "@@" fputc ('@', yyout); |
77 | "@{" fputc ('[', yyout); | |
78 | "@}" fputc (']', yyout); | |
79 | ||
3fc65ead | 80 | "@oline@" fprintf (yyout, "%d", out_lineno + 1); |
c5e3e510 | 81 | "@ofile@" QPUTS (outname); |
2b81e969 | 82 | "@dir_prefix@" QPUTS (dir_prefix); |
bd9d212b | 83 | |
08af01c2 JD |
84 | @[a-z_]+"(" { |
85 | yytext[yyleng-1] = '\0'; | |
86 | at_directive_name = xstrdup (yytext); | |
87 | BEGIN SC_AT_DIRECTIVE_ARG; | |
bd9d212b | 88 | } |
e9683cfd | 89 | |
3fc16193 | 90 | /* This pattern must not match more than the previous @ patterns. */ |
08af01c2 | 91 | @[^@{}(\n]* fail_for_invalid_at (yytext); |
3fc65ead | 92 | \n out_lineno++; ECHO; |
68e93ad5 | 93 | [^@\n]+ ECHO; |
e9683cfd | 94 | |
08af01c2 | 95 | <INITIAL><<EOF>> { |
7ec5ab2e PE |
96 | if (outname) |
97 | { | |
98 | free (outname); | |
99 | xfclose (yyout); | |
100 | } | |
101 | return EOF; | |
102 | } | |
08af01c2 JD |
103 | |
104 | <SC_AT_DIRECTIVE_ARG>{ | |
3fc65ead | 105 | [^@]+ { STRING_GROW; } |
08af01c2 JD |
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 | { | |
3fc65ead | 126 | at_directive_perform (&outname, &out_lineno); |
08af01c2 JD |
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>{ | |
3fc65ead | 138 | [ \t\r\n] |
08af01c2 JD |
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 | ||
9b3add5b | 148 | %% |
536545f3 | 149 | |
2cdb2a7b PE |
150 | /*------------------------. |
151 | | Scan a Bison skeleton. | | |
152 | `------------------------*/ | |
536545f3 | 153 | |
536545f3 | 154 | void |
2cdb2a7b | 155 | scan_skel (FILE *in) |
536545f3 | 156 | { |
08af01c2 JD |
157 | static bool initialized = false; |
158 | if (!initialized) | |
159 | { | |
160 | initialized = true; | |
161 | obstack_init (&obstack_for_string); | |
162 | } | |
2cdb2a7b | 163 | skel_in = in; |
c5e3e510 | 164 | skel__flex_debug = trace_flag & trace_skeleton; |
536545f3 | 165 | skel_lex (); |
536545f3 | 166 | /* Reclaim Flex's buffers. */ |
580b8926 | 167 | yylex_destroy (); |
536545f3 | 168 | } |
bd9d212b | 169 | |
3fc65ead JD |
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 | ||
08af01c2 JD |
291 | static void |
292 | fail_for_at_directive_too_many_args (void) | |
bd9d212b | 293 | { |
08af01c2 JD |
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 | } |