]>
Commit | Line | Data |
---|---|---|
be2a1a68 | 1 | /* Scan Bison Skeletons. -*- C -*- |
353d3eb6 | 2 | |
139c548a | 3 | Copyright (C) 2001-2012 Free Software Foundation, Inc. |
1239777d AD |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
f16b0819 PE |
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. | |
1239777d | 11 | |
f16b0819 PE |
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. | |
9b3add5b | 16 | |
1239777d | 17 | You should have received a copy of the GNU General Public License |
f16b0819 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9b3add5b | 19 | |
8d90395d | 20 | %option nodefault noyywrap noinput nounput never-interactive debug |
aed7fd9b | 21 | %option prefix="skel_" outfile="lex.yy.c" |
9b3add5b | 22 | |
aed7fd9b | 23 | %{ |
4f6e011e PE |
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 | ||
f9bfc42a | 29 | #define FLEX_PREFIX(Id) skel_ ## Id |
0305d25e | 30 | #include <src/flex-scanner.h> |
353d3eb6 | 31 | |
cb48f191 | 32 | #include <dirname.h> |
353d3eb6 PE |
33 | #include <error.h> |
34 | #include <quotearg.h> | |
35 | ||
0305d25e AD |
36 | #include <src/complain.h> |
37 | #include <src/getargs.h> | |
38 | #include <src/files.h> | |
39 | #include <src/scan-skel.h> | |
dc9701e8 | 40 | |
08af01c2 JD |
41 | #define YY_DECL static int skel_lex (void) |
42 | YY_DECL; | |
06f01bc4 | 43 | |
b9c3c10c | 44 | static void at_directive_perform (int argc, char *argv[], |
3eb82471 JD |
45 | char **outnamep, int *out_linenop); |
46 | static void fail_for_at_directive_too_many_args (char const *at_directive_name); | |
47 | static void fail_for_at_directive_too_few_args (char const *at_directive_name); | |
08af01c2 | 48 | static void fail_for_invalid_at (char const *at); |
aed7fd9b | 49 | %} |
08af01c2 | 50 | |
3eb82471 | 51 | %x SC_AT_DIRECTIVE_ARGS |
08af01c2 JD |
52 | %x SC_AT_DIRECTIVE_SKIP_WS |
53 | ||
9b3add5b | 54 | %% |
e9683cfd PE |
55 | |
56 | %{ | |
84f6a6ca | 57 | int out_lineno PACIFY_CC (= 0); |
e9683cfd | 58 | char *outname = NULL; |
3eb82471 JD |
59 | |
60 | /* Currently, only the @warn, @complain, @fatal, @warn_at, @complain_at, and | |
61 | @fatal_at directives take multiple arguments, and the last three already | |
b9c3c10c AD |
62 | can't take more than 7. argv[0] is the directive name. */ |
63 | #define ARGC_MAX 8 | |
64 | int argc = 0; | |
65 | char *argv[ARGC_MAX]; | |
e9683cfd PE |
66 | %} |
67 | ||
7dc4a694 JD |
68 | "@@" fputc ('@', yyout); |
69 | "@{" fputc ('[', yyout); | |
70 | "@}" fputc (']', yyout); | |
4d24ffb7 AD |
71 | "@`" continue; /* Used by b4_cat in ../data/bison.m4. */ |
72 | @\n continue; | |
e9683cfd | 73 | |
3fc65ead | 74 | "@oline@" fprintf (yyout, "%d", out_lineno + 1); |
b9c3c10c | 75 | "@ofile@" fputs (quotearg_style (c_quoting_style, outname), yyout); |
bd9d212b | 76 | |
08af01c2 JD |
77 | @[a-z_]+"(" { |
78 | yytext[yyleng-1] = '\0'; | |
3eb82471 | 79 | obstack_grow (&obstack_for_string, yytext, yyleng); |
b9c3c10c | 80 | argv[argc++] = obstack_finish (&obstack_for_string); |
3eb82471 | 81 | BEGIN SC_AT_DIRECTIVE_ARGS; |
bd9d212b | 82 | } |
e9683cfd | 83 | |
3fc16193 | 84 | /* This pattern must not match more than the previous @ patterns. */ |
7dc4a694 | 85 | @[^@{}`(\n]* fail_for_invalid_at (yytext); |
e9690142 JD |
86 | \n out_lineno++; ECHO; |
87 | [^@\n]+ ECHO; | |
e9683cfd | 88 | |
dbfcf7a8 | 89 | <INITIAL><<EOF>> { |
7ec5ab2e PE |
90 | if (outname) |
91 | { | |
92 | free (outname); | |
93 | xfclose (yyout); | |
94 | } | |
95 | return EOF; | |
96 | } | |
08af01c2 | 97 | |
4d24ffb7 AD |
98 | <SC_AT_DIRECTIVE_ARGS> |
99 | { | |
100 | [^@]+ STRING_GROW; | |
08af01c2 | 101 | |
4d24ffb7 AD |
102 | "@@" obstack_1grow (&obstack_for_string, '@'); |
103 | "@{" obstack_1grow (&obstack_for_string, '['); | |
104 | "@}" obstack_1grow (&obstack_for_string, ']'); | |
105 | "@`" continue; /* For starting an argument that begins with whitespace. */ | |
106 | @\n continue; | |
08af01c2 JD |
107 | |
108 | @[,)] { | |
b9c3c10c AD |
109 | if (argc >= ARGC_MAX) |
110 | fail_for_at_directive_too_many_args (argv[0]); | |
08af01c2 | 111 | |
b9c3c10c | 112 | argv[argc++] = obstack_finish0 (&obstack_for_string); |
08af01c2 JD |
113 | |
114 | /* Like M4, skip whitespace after a comma. */ | |
115 | if (yytext[1] == ',') | |
116 | BEGIN SC_AT_DIRECTIVE_SKIP_WS; | |
117 | else | |
118 | { | |
b9c3c10c AD |
119 | at_directive_perform (argc, argv, &outname, &out_lineno); |
120 | obstack_free (&obstack_for_string, argv[0]); | |
121 | argc = 0; | |
08af01c2 JD |
122 | BEGIN INITIAL; |
123 | } | |
124 | } | |
125 | ||
4d24ffb7 | 126 | @.? fail_for_invalid_at (yytext); |
08af01c2 JD |
127 | } |
128 | ||
4d24ffb7 AD |
129 | <SC_AT_DIRECTIVE_SKIP_WS> |
130 | { | |
131 | [ \t\r\n] continue; | |
b9c3c10c | 132 | . yyless (0); BEGIN SC_AT_DIRECTIVE_ARGS; |
08af01c2 JD |
133 | } |
134 | ||
4d24ffb7 AD |
135 | <SC_AT_DIRECTIVE_ARGS,SC_AT_DIRECTIVE_SKIP_WS> |
136 | { | |
bb8e56ff | 137 | <<EOF>> complain (NULL, fatal, _("unclosed %s directive in skeleton"), argv[0]); |
08af01c2 JD |
138 | } |
139 | ||
9b3add5b | 140 | %% |
536545f3 | 141 | |
2cdb2a7b PE |
142 | /*------------------------. |
143 | | Scan a Bison skeleton. | | |
144 | `------------------------*/ | |
536545f3 | 145 | |
536545f3 | 146 | void |
2cdb2a7b | 147 | scan_skel (FILE *in) |
536545f3 | 148 | { |
08af01c2 JD |
149 | static bool initialized = false; |
150 | if (!initialized) | |
151 | { | |
152 | initialized = true; | |
153 | obstack_init (&obstack_for_string); | |
154 | } | |
2cdb2a7b | 155 | skel_in = in; |
c5e3e510 | 156 | skel__flex_debug = trace_flag & trace_skeleton; |
536545f3 | 157 | skel_lex (); |
536545f3 | 158 | } |
bd9d212b | 159 | |
3fc65ead JD |
160 | void |
161 | skel_scanner_free (void) | |
162 | { | |
163 | obstack_free (&obstack_for_string, 0); | |
3eb82471 JD |
164 | /* Reclaim Flex's buffers. */ |
165 | yylex_destroy (); | |
3fc65ead JD |
166 | } |
167 | ||
b9c3c10c AD |
168 | static inline warnings |
169 | flag (const char *arg) | |
170 | { | |
171 | switch (arg[1]) | |
172 | { | |
173 | case 'w': return Wother; | |
174 | case 'c': return complaint; | |
175 | case 'f': return fatal; | |
176 | default: aver (false); break; | |
177 | } | |
178 | } | |
179 | ||
f39ab286 | 180 | static void |
b9c3c10c | 181 | at_directive_perform (int argc, char *argv[], char **outnamep, int *out_linenop) |
3fc65ead | 182 | { |
b9c3c10c | 183 | if (STREQ (argv[0], "@basename")) |
3fc65ead | 184 | { |
b9c3c10c AD |
185 | if (argc > 2) |
186 | fail_for_at_directive_too_many_args (argv[0]); | |
187 | fputs (last_component (argv[1]), yyout); | |
3fc65ead | 188 | } |
b9c3c10c AD |
189 | else if (STREQ (argv[0], "@warn") |
190 | || STREQ (argv[0], "@complain") | |
191 | || STREQ (argv[0], "@fatal")) | |
3fc65ead | 192 | { |
b9c3c10c AD |
193 | warnings w = flag (argv[0]); |
194 | switch (argc) | |
3fc65ead | 195 | { |
87b0a375 | 196 | case 2: |
bb8e56ff | 197 | complain (NULL, w, "%s", _(argv[1])); |
3fc65ead | 198 | break; |
87b0a375 | 199 | case 3: |
bb8e56ff | 200 | complain (NULL, w, _(argv[1]), argv[2]); |
3fc65ead | 201 | break; |
87b0a375 | 202 | case 4: |
bb8e56ff | 203 | complain (NULL, w, _(argv[1]), argv[2], argv[3]); |
3fc65ead | 204 | break; |
87b0a375 | 205 | case 5: |
bb8e56ff | 206 | complain (NULL, w, _(argv[1]), argv[2], argv[3], argv[4]); |
3fc65ead | 207 | break; |
87b0a375 | 208 | case 6: |
bb8e56ff | 209 | complain (NULL, w, _(argv[1]), argv[2], argv[3], argv[4], argv[5]); |
3fc65ead JD |
210 | break; |
211 | default: | |
b9c3c10c | 212 | fail_for_at_directive_too_many_args (argv[0]); |
3fc65ead JD |
213 | break; |
214 | } | |
215 | } | |
b9c3c10c AD |
216 | else if (STREQ (argv[0], "@warn_at") |
217 | || STREQ (argv[0], "@complain_at") | |
218 | || STREQ (argv[0], "@fatal_at")) | |
3fc65ead | 219 | { |
b9c3c10c | 220 | warnings w = flag (argv[0]); |
3fc65ead | 221 | location loc; |
b9c3c10c AD |
222 | if (argc < 4) |
223 | fail_for_at_directive_too_few_args (argv[0]); | |
224 | boundary_set_from_string (&loc.start, argv[1]); | |
225 | boundary_set_from_string (&loc.end, argv[2]); | |
226 | switch (argc) | |
3fc65ead | 227 | { |
87b0a375 | 228 | case 4: |
bb8e56ff | 229 | complain (&loc, w, "%s", _(argv[3])); |
3fc65ead | 230 | break; |
87b0a375 | 231 | case 5: |
bb8e56ff | 232 | complain (&loc, w, _(argv[3]), argv[4]); |
3fc65ead | 233 | break; |
87b0a375 | 234 | case 6: |
bb8e56ff | 235 | complain (&loc, w, _(argv[3]), argv[4], argv[5]); |
3fc65ead | 236 | break; |
87b0a375 | 237 | case 7: |
bb8e56ff | 238 | complain (&loc, w, _(argv[3]), argv[4], argv[5], argv[6]); |
3fc65ead | 239 | break; |
87b0a375 | 240 | case 8: |
bb8e56ff | 241 | complain (&loc, w, _(argv[3]), argv[4], argv[5], argv[6], |
b9c3c10c | 242 | argv[7]); |
3fc65ead JD |
243 | break; |
244 | default: | |
b9c3c10c | 245 | fail_for_at_directive_too_many_args (argv[0]); |
3fc65ead JD |
246 | break; |
247 | } | |
248 | } | |
b9c3c10c | 249 | else if (STREQ (argv[0], "@output")) |
3fc65ead | 250 | { |
b9c3c10c AD |
251 | if (argc > 2) |
252 | fail_for_at_directive_too_many_args (argv[0]); | |
3fc65ead JD |
253 | if (*outnamep) |
254 | { | |
255 | free (*outnamep); | |
256 | xfclose (yyout); | |
257 | } | |
b9c3c10c | 258 | *outnamep = xstrdup (argv[1]); |
f39ab286 | 259 | output_file_name_check (outnamep); |
3fc65ead JD |
260 | yyout = xfopen (*outnamep, "w"); |
261 | *out_linenop = 1; | |
262 | } | |
263 | else | |
b9c3c10c | 264 | fail_for_invalid_at (argv[0]); |
3fc65ead JD |
265 | } |
266 | ||
267 | static void | |
3eb82471 | 268 | fail_for_at_directive_too_few_args (char const *at_directive_name) |
3fc65ead | 269 | { |
bb8e56ff | 270 | complain (NULL, fatal, _("too few arguments for %s directive in skeleton"), |
b9c3c10c | 271 | at_directive_name); |
3fc65ead JD |
272 | } |
273 | ||
08af01c2 | 274 | static void |
3eb82471 | 275 | fail_for_at_directive_too_many_args (char const *at_directive_name) |
bd9d212b | 276 | { |
bb8e56ff | 277 | complain (NULL, fatal, _("too many arguments for %s directive in skeleton"), |
6fb8b256 | 278 | at_directive_name); |
08af01c2 JD |
279 | } |
280 | ||
281 | static void | |
282 | fail_for_invalid_at (char const *at) | |
283 | { | |
bb8e56ff | 284 | complain (NULL, fatal, "invalid @ in skeleton: %s", at); |
08af01c2 | 285 | } |