1 /* Bison Action Scanner -*- C -*-
3 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 %option debug nodefault nounput noyywrap never-interactive
21 %option prefix="code_" outfile="lex.yy.c"
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>. */
29 #define FLEX_PREFIX(Id) code_ ## Id
30 #include "flex-scanner.h"
35 #include <get-errno.h>
38 #include "scan-code.h"
41 /* The current calling start condition: SC_RULE_ACTION or
43 # define YY_DECL static char *code_lex (code_props *self, int sc_context)
46 #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng);
48 static void handle_action_dollar (symbol_list *rule, char *cp,
50 static void handle_action_at (symbol_list *rule, char *cp, location at_loc);
51 static location the_location;
52 static location *loc = &the_location;
54 /* A string representing the most recent translation. */
55 static char *last_string;
57 /* True if an untyped $$ or $n was seen. */
58 static bool untyped_var_seen;
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
66 %x SC_RULE_ACTION SC_SYMBOL_ACTION
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. */
74 /* Zero or more instances of backslash-newline. Following GCC, allow
75 white space between the backslash and the newline. */
76 splice (\\[ \f\t\v]*\n)*
81 /* Nesting level of the current code in braces. */
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 aver (sc_context == SC_SYMBOL_ACTION
88 || sc_context == SC_RULE_ACTION
89 || sc_context == INITIAL);
93 /*------------------------------------------------------------.
94 | Scanning a C comment. The initial `/ *' is already eaten. |
95 `------------------------------------------------------------*/
99 "*"{splice}"/" STRING_GROW; BEGIN sc_context;
103 /*--------------------------------------------------------------.
104 | Scanning a line comment. The initial `//' is already eaten. |
105 `--------------------------------------------------------------*/
109 "\n" STRING_GROW; BEGIN sc_context;
110 {splice} STRING_GROW;
114 /*--------------------------------------------.
115 | Scanning user-code characters and strings. |
116 `--------------------------------------------*/
118 <SC_CHARACTER,SC_STRING>
120 {splice}|\\{splice}. STRING_GROW;
125 "'" STRING_GROW; BEGIN sc_context;
130 "\"" STRING_GROW; BEGIN sc_context;
134 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
149 BEGIN SC_LINE_COMMENT;
155 "$"("<"{tag}">")?(-?[0-9]+|"$") {
156 handle_action_dollar (self->rule, yytext, *loc);
159 handle_action_at (self->rule, yytext, *loc);
163 warn_at (*loc, _("stray `$'"));
164 obstack_sgrow (&obstack_for_string, "$][");
167 warn_at (*loc, _("stray `@'"));
168 obstack_sgrow (&obstack_for_string, "@@");
171 "{" STRING_GROW; ++braces_level;
173 bool outer_brace = --braces_level < 0;
175 /* As an undocumented Bison extension, append `;' before the last
176 brace in braced code, so that the user code can omit trailing
177 `;'. But do not append `;' if emulating Yacc, since Yacc does
180 FIXME: Bison should warn if a semicolon seems to be necessary
181 here, and should omit the semicolon if it seems unnecessary
182 (e.g., after ';', '{', or '}', each followed by comments or
183 white space). Such a warning shouldn't depend on --yacc; it
184 should depend on a new --pedantic option, which would cause
185 Bison to warn if it detects an extension to POSIX. --pedantic
186 should also diagnose other Bison extensions like %yacc.
187 Perhaps there should also be a GCC-style --pedantic-errors
188 option, so that such warnings are diagnosed as errors. */
189 if (outer_brace && ! yacc_flag)
190 obstack_1grow (&obstack_for_string, ';');
199 obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
200 self->is_value_used = true;
203 obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
204 locations_flag = true;
209 /*-----------------------------------------.
210 | Escape M4 quoting characters in C code. |
211 `-----------------------------------------*/
215 \$ obstack_sgrow (&obstack_for_string, "$][");
216 \@ obstack_sgrow (&obstack_for_string, "@@");
217 \[ obstack_sgrow (&obstack_for_string, "@{");
218 \] obstack_sgrow (&obstack_for_string, "@}");
221 /*-----------------------------------------------------.
222 | By default, grow the string obstack with the input. |
223 `-----------------------------------------------------*/
227 /* End of processing. */
235 /* Keeps track of the maximum number of semantic values to the left of
236 a handle (those referenced by $0, $-1, etc.) are required by the
237 semantic actions of this grammar. */
238 int max_left_semantic_context = 0;
241 /*------------------------------------------------------------------.
242 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
244 | Possible inputs: $[<TYPENAME>]($|integer) |
246 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
247 `------------------------------------------------------------------*/
250 handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
252 char const *type_name = NULL;
254 symbol_list *effective_rule;
255 int effective_rule_length;
257 if (rule->midrule_parent_rule)
259 effective_rule = rule->midrule_parent_rule;
260 effective_rule_length = rule->midrule_parent_rhs_index - 1;
264 effective_rule = rule;
265 effective_rule_length = symbol_list_length (rule->next);
268 /* Get the type name if explicit. */
276 if (untyped_var_seen)
277 complain_at (dollar_loc, _("explicit type given in untyped grammar"));
284 type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0);
288 if (union_seen | tag_seen)
290 if (rule->midrule_parent_rule)
291 complain_at (dollar_loc,
292 _("$$ for the midrule at $%d of `%s'"
293 " has no declared type"),
294 rule->midrule_parent_rhs_index,
295 effective_rule->content.sym->tag);
297 complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
298 rule->content.sym->tag);
301 untyped_var_seen = true;
305 obstack_fgrow1 (&obstack_for_string,
306 "]b4_lhs_value([%s])[", type_name);
307 rule->action_props.is_value_used = true;
311 long int num = strtol (cp, NULL, 10);
313 if (1 - INT_MAX + effective_rule_length <= num
314 && num <= effective_rule_length)
317 if (max_left_semantic_context < 1 - n)
318 max_left_semantic_context = 1 - n;
319 if (!type_name && 0 < n)
321 symbol_list_n_type_name_get (effective_rule, dollar_loc, n);
324 if (union_seen | tag_seen)
325 complain_at (dollar_loc, _("$%d of `%s' has no declared type"),
326 n, effective_rule->content.sym->tag);
328 untyped_var_seen = true;
332 obstack_fgrow3 (&obstack_for_string,
333 "]b4_rhs_value(%d, %d, [%s])[",
334 effective_rule_length, n, type_name);
336 symbol_list_n_get (effective_rule, n)->action_props.is_value_used =
340 complain_at (dollar_loc, _("integer out of range: %s"), quote (text));
345 /*------------------------------------------------------.
346 | TEXT is a location token (i.e., a `@...'). Output to |
347 | OBSTACK_FOR_STRING a reference to this location. |
348 `------------------------------------------------------*/
351 handle_action_at (symbol_list *rule, char *text, location at_loc)
354 int effective_rule_length =
355 (rule->midrule_parent_rule
356 ? rule->midrule_parent_rhs_index - 1
357 : symbol_list_length (rule->next));
359 locations_flag = true;
362 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
365 long int num = strtol (cp, NULL, 10);
367 if (1 - INT_MAX + effective_rule_length <= num
368 && num <= effective_rule_length)
371 obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
372 effective_rule_length, n);
375 complain_at (at_loc, _("integer out of range: %s"), quote (text));
380 /*-------------------------.
381 | Initialize the scanner. |
382 `-------------------------*/
384 /* Translate the dollars and ats in \a self, in the context \a sc_context
385 (SC_RULE_ACTION, SC_SYMBOL_ACTION, INITIAL). */
388 translate_action (code_props *self, int sc_context)
391 static bool initialized = false;
394 obstack_init (&obstack_for_string);
399 loc->start = loc->end = self->location.start;
400 yy_switch_to_buffer (yy_scan_string (self->code));
401 res = code_lex (self, sc_context);
402 yy_delete_buffer (YY_CURRENT_BUFFER);
407 /*------------------------------------------------------------------------.
408 | Implementation of the public interface as documented in "scan-code.h". |
409 `------------------------------------------------------------------------*/
412 code_props_none_init (code_props *self)
414 *self = code_props_none;
417 code_props const code_props_none = CODE_PROPS_NONE_INIT;
420 code_props_plain_init (code_props *self, char const *code, location code_loc)
422 self->kind = CODE_PROPS_PLAIN;
424 self->location = code_loc;
425 self->is_value_used = false;
430 code_props_symbol_action_init (code_props *self, char const *code,
433 self->kind = CODE_PROPS_SYMBOL_ACTION;
435 self->location = code_loc;
436 self->is_value_used = false;
441 code_props_rule_action_init (code_props *self, char const *code,
442 location code_loc, symbol_list *rule)
444 self->kind = CODE_PROPS_RULE_ACTION;
446 self->location = code_loc;
447 self->is_value_used = false;
452 code_props_translate_code (code_props *self)
456 case CODE_PROPS_NONE:
458 case CODE_PROPS_PLAIN:
459 self->code = translate_action (self, INITIAL);
461 case CODE_PROPS_SYMBOL_ACTION:
462 self->code = translate_action (self, SC_SYMBOL_ACTION);
464 case CODE_PROPS_RULE_ACTION:
465 self->code = translate_action (self, SC_RULE_ACTION);
471 code_scanner_last_string_free (void)
477 code_scanner_free (void)
479 obstack_free (&obstack_for_string, 0);
480 /* Reclaim Flex's buffers. */