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