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