]> git.saurik.com Git - bison.git/blob - src/scan-code.l
Get Bison to build again when configured with --enable-gcc-warnings.
[bison.git] / src / scan-code.l
1 /* Bison Action Scanner -*- C -*-
2
3 Copyright (C) 2006 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA
21 */
22
23 %option debug nodefault nounput noyywrap never-interactive
24 %option prefix="code_" 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 code_wrap
30 #define code_wrap() 1
31
32 #define FLEX_PREFIX(Id) code_ ## Id
33 #include "complain.h"
34 #include "flex-scanner.h"
35 #include "reader.h"
36 #include "getargs.h"
37 #include <assert.h>
38 #include <get-errno.h>
39 #include <quote.h>
40
41 #include "scan-code.h"
42
43 /* The current calling start condition: SC_RULE_ACTION or
44 SC_SYMBOL_ACTION. */
45 # define YY_DECL const char *code_lex (int sc_context)
46 YY_DECL;
47
48 #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng);
49
50 static void handle_action_dollar (char *cp, location dollar_loc);
51 static void handle_action_at (char *cp, location at_loc);
52 static location the_location;
53 static location *loc = &the_location;
54
55 /* The rule being processed. */
56 symbol_list *current_rule;
57 %}
58 /* C and C++ comments in code. */
59 %x SC_COMMENT SC_LINE_COMMENT
60 /* Strings and characters in code. */
61 %x SC_STRING SC_CHARACTER
62 /* Whether in a rule or symbol action. Specifies the translation
63 of $ and @. */
64 %x SC_RULE_ACTION SC_SYMBOL_ACTION
65
66
67 /* POSIX says that a tag must be both an id and a C union member, but
68 historically almost any character is allowed in a tag. We disallow
69 NUL and newline, as this simplifies our implementation. */
70 tag [^\0\n>]+
71
72 /* Zero or more instances of backslash-newline. Following GCC, allow
73 white space between the backslash and the newline. */
74 splice (\\[ \f\t\v]*\n)*
75
76 %%
77
78 %{
79 /* Nesting level of the current code in braces. */
80 int braces_level IF_LINT (= 0);
81
82 /* This scanner is special: it is invoked only once, henceforth
83 is expected to return only once. This initialization is
84 therefore done once per action to translate. */
85 assert (sc_context == SC_SYMBOL_ACTION
86 || sc_context == SC_RULE_ACTION
87 || sc_context == INITIAL);
88 BEGIN sc_context;
89 %}
90
91 /*------------------------------------------------------------.
92 | Scanning a C comment. The initial `/ *' is already eaten. |
93 `------------------------------------------------------------*/
94
95 <SC_COMMENT>
96 {
97 "*"{splice}"/" STRING_GROW; BEGIN sc_context;
98 }
99
100
101 /*--------------------------------------------------------------.
102 | Scanning a line comment. The initial `//' is already eaten. |
103 `--------------------------------------------------------------*/
104
105 <SC_LINE_COMMENT>
106 {
107 "\n" STRING_GROW; BEGIN sc_context;
108 {splice} STRING_GROW;
109 }
110
111
112 /*--------------------------------------------.
113 | Scanning user-code characters and strings. |
114 `--------------------------------------------*/
115
116 <SC_CHARACTER,SC_STRING>
117 {
118 {splice}|\\{splice}. STRING_GROW;
119 }
120
121 <SC_CHARACTER>
122 {
123 "'" STRING_GROW; BEGIN sc_context;
124 }
125
126 <SC_STRING>
127 {
128 "\"" STRING_GROW; BEGIN sc_context;
129 }
130
131
132 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
133 "'" {
134 STRING_GROW;
135 BEGIN SC_CHARACTER;
136 }
137 "\"" {
138 STRING_GROW;
139 BEGIN SC_STRING;
140 }
141 "/"{splice}"*" {
142 STRING_GROW;
143 BEGIN SC_COMMENT;
144 }
145 "/"{splice}"/" {
146 STRING_GROW;
147 BEGIN SC_LINE_COMMENT;
148 }
149 }
150
151 <SC_RULE_ACTION>
152 {
153 "$"("<"{tag}">")?(-?[0-9]+|"$") handle_action_dollar (yytext, *loc);
154 "@"(-?[0-9]+|"$") handle_action_at (yytext, *loc);
155
156 "$" {
157 warn_at (*loc, _("stray `$'"));
158 obstack_sgrow (&obstack_for_string, "$][");
159 }
160 "@" {
161 warn_at (*loc, _("stray `@'"));
162 obstack_sgrow (&obstack_for_string, "@@");
163 }
164
165 "{" STRING_GROW; ++braces_level;
166 "}" {
167 bool outer_brace = --braces_level < 0;
168
169 /* As an undocumented Bison extension, append `;' before the last
170 brace in braced code, so that the user code can omit trailing
171 `;'. But do not append `;' if emulating Yacc, since Yacc does
172 not append one.
173
174 FIXME: Bison should warn if a semicolon seems to be necessary
175 here, and should omit the semicolon if it seems unnecessary
176 (e.g., after ';', '{', or '}', each followed by comments or
177 white space). Such a warning shouldn't depend on --yacc; it
178 should depend on a new --pedantic option, which would cause
179 Bison to warn if it detects an extension to POSIX. --pedantic
180 should also diagnose other Bison extensions like %yacc.
181 Perhaps there should also be a GCC-style --pedantic-errors
182 option, so that such warnings are diagnosed as errors. */
183 if (outer_brace && ! yacc_flag)
184 obstack_1grow (&obstack_for_string, ';');
185
186 STRING_GROW;
187 }
188 }
189
190 <SC_SYMBOL_ACTION>
191 {
192 "$$" obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
193 "@$" obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
194 }
195
196
197 /*-----------------------------------------.
198 | Escape M4 quoting characters in C code. |
199 `-----------------------------------------*/
200
201 <*>
202 {
203 \$ obstack_sgrow (&obstack_for_string, "$][");
204 \@ obstack_sgrow (&obstack_for_string, "@@");
205 \[ obstack_sgrow (&obstack_for_string, "@{");
206 \] obstack_sgrow (&obstack_for_string, "@}");
207 }
208
209 /*-----------------------------------------------------.
210 | By default, grow the string obstack with the input. |
211 `-----------------------------------------------------*/
212
213 <*>.|\n STRING_GROW;
214
215 /* End of processing. */
216 <*><<EOF>> {
217 obstack_1grow (&obstack_for_string, '\0');
218 return obstack_finish (&obstack_for_string);
219 }
220
221 %%
222
223 /* Keeps track of the maximum number of semantic values to the left of
224 a handle (those referenced by $0, $-1, etc.) are required by the
225 semantic actions of this grammar. */
226 int max_left_semantic_context = 0;
227
228
229 /*------------------------------------------------------------------.
230 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
231 | |
232 | Possible inputs: $[<TYPENAME>]($|integer) |
233 | |
234 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
235 `------------------------------------------------------------------*/
236
237 static void
238 handle_action_dollar (char *text, location dollar_loc)
239 {
240 const char *type_name = NULL;
241 char *cp = text + 1;
242 int rule_length = symbol_list_length (current_rule->next);
243
244 /* Get the type name if explicit. */
245 if (*cp == '<')
246 {
247 type_name = ++cp;
248 while (*cp != '>')
249 ++cp;
250 *cp = '\0';
251 ++cp;
252 }
253
254 if (*cp == '$')
255 {
256 if (!type_name)
257 type_name = symbol_list_n_type_name_get (current_rule, dollar_loc, 0);
258 if (!type_name && typed)
259 complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
260 current_rule->sym->tag);
261 if (!type_name)
262 type_name = "";
263 obstack_fgrow1 (&obstack_for_string,
264 "]b4_lhs_value([%s])[", type_name);
265 current_rule->used = true;
266 }
267 else
268 {
269 long int num;
270 set_errno (0);
271 num = strtol (cp, 0, 10);
272 if (INT_MIN <= num && num <= rule_length && ! get_errno ())
273 {
274 int n = num;
275 if (1-n > max_left_semantic_context)
276 max_left_semantic_context = 1-n;
277 if (!type_name && n > 0)
278 type_name =
279 symbol_list_n_type_name_get (current_rule, dollar_loc, n);
280 if (!type_name && typed)
281 complain_at (dollar_loc, _("$%d of `%s' has no declared type"),
282 n, current_rule->sym->tag);
283 if (!type_name)
284 type_name = "";
285 obstack_fgrow3 (&obstack_for_string,
286 "]b4_rhs_value(%d, %d, [%s])[",
287 rule_length, n, type_name);
288 symbol_list_n_used_set (current_rule, n, true);
289 }
290 else
291 complain_at (dollar_loc, _("integer out of range: %s"), quote (text));
292 }
293 }
294
295
296 /*------------------------------------------------------.
297 | TEXT is a location token (i.e., a `@...'). Output to |
298 | OBSTACK_FOR_STRING a reference to this location. |
299 `------------------------------------------------------*/
300
301 static void
302 handle_action_at (char *text, location at_loc)
303 {
304 char *cp = text + 1;
305 int rule_length = symbol_list_length (current_rule->next);
306 locations_flag = true;
307
308 if (*cp == '$')
309 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
310 else
311 {
312 long int num;
313 set_errno (0);
314 num = strtol (cp, 0, 10);
315
316 if (INT_MIN <= num && num <= rule_length && ! get_errno ())
317 {
318 int n = num;
319 obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
320 rule_length, n);
321 }
322 else
323 complain_at (at_loc, _("integer out of range: %s"), quote (text));
324 }
325 }
326
327
328 /*-------------------------.
329 | Initialize the scanner. |
330 `-------------------------*/
331
332 /* Translate the dollars and ats in \a a, whose location is l.
333 Depending on the \a sc_context (SC_RULE_ACTION, SC_SYMBOL_ACTION,
334 INITIAL), the processing is different. */
335
336 static const char *
337 translate_action (int sc_context, const char *a, location l)
338 {
339 const char *res;
340 static bool initialized = false;
341 if (!initialized)
342 {
343 obstack_init (&obstack_for_string);
344 /* The initial buffer, never used. */
345 yy_delete_buffer (YY_CURRENT_BUFFER);
346 yy_flex_debug = 0;
347 initialized = true;
348 }
349
350 loc->start = loc->end = l.start;
351 yy_switch_to_buffer (yy_scan_string (a));
352 res = code_lex (sc_context);
353 yy_delete_buffer (YY_CURRENT_BUFFER);
354
355 return res;
356 }
357
358 const char *
359 translate_rule_action (symbol_list *r, const char *a, location l)
360 {
361 current_rule = r;
362 return translate_action (SC_RULE_ACTION, a, l);
363 }
364
365 const char *
366 translate_symbol_action (const char *a, location l)
367 {
368 return translate_action (SC_SYMBOL_ACTION, a, l);
369 }
370
371 const char *
372 translate_code (const char *a, location l)
373 {
374 return translate_action (INITIAL, a, l);
375 }
376
377 /*-----------------------------------------------.
378 | Free all the memory allocated to the scanner. |
379 `-----------------------------------------------*/
380
381 void
382 code_scanner_free (void)
383 {
384 obstack_free (&obstack_for_string, 0);
385 /* Reclaim Flex's buffers. */
386 yy_delete_buffer (YY_CURRENT_BUFFER);
387 }