1 /* Bison Action Scanner -*- C -*-
3 Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 %option debug nodefault noinput nounput noyywrap never-interactive
22 %option prefix="code_" outfile="lex.yy.c"
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
30 #define FLEX_PREFIX(Id) code_ ## Id
31 #include <src/flex-scanner.h>
33 #include <src/complain.h>
34 #include <src/reader.h>
35 #include <src/getargs.h>
36 #include <src/muscle-tab.h>
37 #include <src/scan-code.h>
38 #include <src/symlist.h>
41 #include <get-errno.h>
44 /* The current calling start condition: SC_RULE_ACTION or
46 # define YY_DECL static char *code_lex (code_props *self, int sc_context)
49 #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng);
51 static void handle_action_dollar (symbol_list *rule, char *cp,
53 static void handle_action_at (symbol_list *rule, char *cp, location at_loc);
55 /* A string to be pushed to obstack after dollar/at has been handled. */
56 static char *ref_tail_fields;
58 static location the_location;
59 static location *loc = &the_location;
61 /* A string representing the most recent translation. */
62 static char *last_string;
64 /* True if an untyped $$ or $n was seen. */
65 static bool untyped_var_seen;
68 /* C and C++ comments in code. */
69 %x SC_COMMENT SC_LINE_COMMENT
70 /* Strings and characters in code. */
71 %x SC_STRING SC_CHARACTER
72 /* Whether in a rule or symbol action. Specifies the translation
74 %x SC_RULE_ACTION SC_SYMBOL_ACTION
77 /* POSIX says that a tag must be both an id and a C union member, but
78 historically almost any character is allowed in a tag. We disallow
79 NUL and newline, as this simplifies our implementation. */
82 /* Zero or more instances of backslash-newline. Following GCC, allow
83 white space between the backslash and the newline. */
84 splice (\\[ \f\t\v]*\n)*
86 /* C style identifier. Must start with letter. Will be used for
87 named symbol references. Shall be kept synchronized with
88 scan-gram.l "letter" and "id". */
89 letter [-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
90 id {letter}({letter}|[0-9])*
91 ref -?[0-9]+|{id}|"["{id}"]"|"$"
96 /* Nesting level of the current code in braces. */
99 /* Whether a semicolon is probably needed.
100 The heuristic is that a semicolon is not needed after `{', `}', `;',
101 or a C preprocessor directive, and that whitespaces and comments
102 do not affect this flag.
103 Note that `{' does not need a semicolon because of `{}'.
104 A semicolon may be needed before a cpp direcive, but don't bother. */
105 bool need_semicolon = false;
107 /* Whether in a C preprocessor directive. Don't use a start condition
108 for this because, at the end of strings and comments, we still need
109 to know whether we're in a directive. */
112 /* This scanner is special: it is invoked only once, henceforth
113 is expected to return only once. This initialization is
114 therefore done once per action to translate. */
115 aver (sc_context == SC_SYMBOL_ACTION
116 || sc_context == SC_RULE_ACTION
117 || sc_context == INITIAL);
121 /*------------------------------------------------------------.
122 | Scanning a C comment. The initial `/ *' is already eaten. |
123 `------------------------------------------------------------*/
127 "*"{splice}"/" STRING_GROW; BEGIN sc_context;
131 /*--------------------------------------------------------------.
132 | Scanning a line comment. The initial `//' is already eaten. |
133 `--------------------------------------------------------------*/
137 "\n" STRING_GROW; BEGIN sc_context;
138 {splice} STRING_GROW;
142 /*--------------------------------------------.
143 | Scanning user-code characters and strings. |
144 `--------------------------------------------*/
146 <SC_CHARACTER,SC_STRING>
148 {splice}|\\{splice}. STRING_GROW;
153 "'" STRING_GROW; BEGIN sc_context;
158 "\"" STRING_GROW; BEGIN sc_context;
162 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
166 need_semicolon = true;
171 need_semicolon = true;
179 BEGIN SC_LINE_COMMENT;
185 "$"("<"{tag}">")?{ref} {
187 handle_action_dollar (self->rule, yytext, *loc);
188 if (ref_tail_fields) {
189 obstack_sgrow (&obstack_for_string, ref_tail_fields);
191 need_semicolon = true;
195 handle_action_at (self->rule, yytext, *loc);
196 if (ref_tail_fields) {
197 obstack_sgrow (&obstack_for_string, ref_tail_fields);
199 need_semicolon = true;
202 warn_at (*loc, _("stray `$'"));
203 obstack_sgrow (&obstack_for_string, "$][");
204 need_semicolon = true;
207 warn_at (*loc, _("stray `@'"));
208 obstack_sgrow (&obstack_for_string, "@@");
209 need_semicolon = true;
212 obstack_sgrow (&obstack_for_string, "@{");
213 need_semicolon = true;
216 obstack_sgrow (&obstack_for_string, "@}");
217 need_semicolon = true;
220 ";" STRING_GROW; need_semicolon = false;
221 "{" STRING_GROW; ++braces_level; need_semicolon = false;
223 bool outer_brace = --braces_level == 0;
225 /* As an undocumented Bison extension, append `;' before the last
226 brace in braced code, so that the user code can omit trailing
227 `;'. But do not append `;' if emulating Yacc, since Yacc does
229 if (outer_brace && !yacc_flag && language_prio == default_prio
230 && skeleton_prio == default_prio && need_semicolon && ! in_cpp)
232 warn_at (*loc, _("a `;' might be needed at the end of action code"));
233 warn_at (*loc, _("future versions of Bison will not add the `;'"));
234 obstack_1grow (&obstack_for_string, ';');
238 need_semicolon = false;
241 /* Preprocessing directives should only be recognized at the beginning
242 of lines, allowing whitespace including comments, but in C/C++,
243 `#' can only be the start of preprocessor directives or within
244 `#define' directives anyway, so don't bother with begin of line. */
245 "#" STRING_GROW; in_cpp = true;
247 {splice} STRING_GROW;
248 [\n\r] STRING_GROW; if (in_cpp) in_cpp = need_semicolon = false;
251 /* YYFAIL is undocumented and was formally deprecated in Bison
254 STRING_GROW; need_semicolon = true;
255 warn_at (*loc, _("use of YYFAIL, which is deprecated and will be"
259 /* The sole purpose of this is to make sure identifiers that merely
260 contain YYFAIL don't produce the above warning. */
261 [A-Za-z_][0-9A-Za-z_]* STRING_GROW; need_semicolon = true;
263 . STRING_GROW; need_semicolon = true;
269 obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
270 self->is_value_used = true;
273 obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
274 muscle_percent_define_ensure("locations", the_location, true);
279 /*-----------------------------------------.
280 | Escape M4 quoting characters in C code. |
281 `-----------------------------------------*/
285 \$ obstack_sgrow (&obstack_for_string, "$][");
286 \@ obstack_sgrow (&obstack_for_string, "@@");
287 \[ obstack_sgrow (&obstack_for_string, "@{");
288 \] obstack_sgrow (&obstack_for_string, "@}");
291 /*-----------------------------------------------------.
292 | By default, grow the string obstack with the input. |
293 `-----------------------------------------------------*/
297 /* End of processing. */
306 is_dot_or_dash (char ch)
308 return ch == '.' || ch == '-';
312 contains_dot_or_dash (const char* p)
315 if (is_dot_or_dash (*p))
320 /* Defines a variant of a symbolic name resolution. */
323 /* Index in symbol list. */
324 unsigned symbol_index;
326 /* Matched symbol id and loc. */
330 /* Hiding named reference. */
331 named_ref* hidden_by;
333 /* Error flags. May contain zero (no errors) or
334 a combination of VARIANT_* values. */
338 /* Set when the variant refers to a symbol hidden
339 by an explicit symbol reference. */
340 #define VARIANT_HIDDEN (1 << 0)
342 /* Set when the variant refers to a symbol containing
343 dots or dashes. Will require explicit bracketing. */
344 #define VARIANT_BAD_BRACKETING (1 << 1)
346 /* Set when the variant refers to a symbol which is
347 not visible from current midrule. */
348 #define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2)
350 static variant *variant_table = 0;
351 static unsigned variant_table_size = 0;
352 static unsigned variant_count = 0;
355 variant_table_grow (void)
358 if (variant_count > variant_table_size)
360 while (variant_count > variant_table_size)
361 variant_table_size = 2 * variant_table_size + 3;
362 variant_table = xnrealloc (variant_table, variant_table_size,
363 sizeof *variant_table);
365 return &variant_table[variant_count - 1];
369 variant_table_free (void)
371 free (variant_table);
373 variant_table_size = variant_count = 0;
377 find_prefix_end (const char *prefix, char *begin, char *end)
381 for (; *prefix && ptr != end; ++prefix, ++ptr)
392 variant_add (uniqstr id, location id_loc, unsigned symbol_index,
393 char *cp, char *cp_end, bool explicit_bracketing)
397 prefix_end = find_prefix_end (id, cp, cp_end);
399 (prefix_end == cp_end ||
400 (!explicit_bracketing && is_dot_or_dash (*prefix_end))))
402 variant *r = variant_table_grow ();
403 r->symbol_index = symbol_index;
415 get_at_spec(unsigned symbol_index)
417 static char at_buf[20];
418 if (symbol_index == 0)
419 strcpy (at_buf, "$$");
421 snprintf (at_buf, sizeof at_buf, "$%u", symbol_index);
426 show_sub_messages (const char* cp, bool explicit_bracketing,
427 int midrule_rhs_index, char dollar_or_at,
428 bool is_warning, unsigned indent)
432 for (i = 0; i < variant_count; ++i)
434 const variant *var = &variant_table[i];
435 const char *at_spec = get_at_spec (var->symbol_index);
440 warn_at_indent (var->loc, &indent, _("refers to: %c%s at %s"),
441 dollar_or_at, var->id, at_spec);
443 complain_at_indent (var->loc, &indent, _("refers to: %c%s at %s"),
444 dollar_or_at, var->id, at_spec);
448 static struct obstack msg_buf;
449 const char *tail = explicit_bracketing ? "" :
450 cp + strlen (var->id);
451 const char *id = var->hidden_by ? var->hidden_by->id :
453 location id_loc = var->hidden_by ? var->hidden_by->loc :
456 /* Create the explanation message. */
457 obstack_init (&msg_buf);
459 obstack_fgrow1 (&msg_buf, _("possibly meant: %c"), dollar_or_at);
460 if (contains_dot_or_dash (id))
461 obstack_fgrow1 (&msg_buf, "[%s]", id);
463 obstack_sgrow (&msg_buf, id);
464 obstack_sgrow (&msg_buf, tail);
466 if (var->err & VARIANT_HIDDEN)
468 obstack_fgrow1 (&msg_buf, _(", hiding %c"), dollar_or_at);
469 if (contains_dot_or_dash (var->id))
470 obstack_fgrow1 (&msg_buf, "[%s]", var->id);
472 obstack_sgrow (&msg_buf, var->id);
473 obstack_sgrow (&msg_buf, tail);
476 obstack_fgrow1 (&msg_buf, _(" at %s"), at_spec);
478 if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
481 _(", cannot be accessed from mid-rule action at $%d");
482 obstack_fgrow1 (&msg_buf, format, midrule_rhs_index);
485 obstack_1grow (&msg_buf, '\0');
487 warn_at_indent (id_loc, &indent, "%s",
488 (char *) obstack_finish (&msg_buf));
490 complain_at_indent (id_loc, &indent, "%s",
491 (char *) obstack_finish (&msg_buf));
492 obstack_free (&msg_buf, 0);
497 /* Returned from "parse_ref" when the reference
499 #define INVALID_REF (INT_MIN)
501 /* Returned from "parse_ref" when the reference
502 points to LHS ($$) of the current rule or midrule. */
503 #define LHS_REF (INT_MIN + 1)
505 /* Sub-messages indent. */
506 #define SUB_INDENT (4)
508 /* Parse named or positional reference. In case of positional
509 references, can return negative values for $-n "deep" stack
512 parse_ref (char *cp, symbol_list *rule, int rule_length,
513 int midrule_rhs_index, char *text, location text_loc,
518 bool explicit_bracketing;
520 unsigned valid_variants = 0;
521 unsigned valid_variant_index = 0;
526 if (c_isdigit (*cp) || (*cp == '-' && c_isdigit (* (cp + 1))))
528 long int num = strtol (cp, &cp, 10);
529 if (1 - INT_MAX + rule_length <= num && num <= rule_length)
533 complain_at (text_loc, _("integer out of range: %s"),
541 /* Ignore the brackets. */
543 for (p = ++cp; *p != ']'; ++p)
547 explicit_bracketing = true;
551 /* Take all characters of the name. */
553 for (p = cp; *p; ++p)
554 if (is_dot_or_dash (*p))
559 for (p = cp; *p; ++p)
563 explicit_bracketing = false;
566 /* Add all relevant variants. */
568 unsigned symbol_index;
570 for (symbol_index = 0, l = rule; !symbol_list_null (l);
571 ++symbol_index, l = l->next)
574 if (l->content_type != SYMLIST_SYMBOL)
577 var = variant_add (l->content.sym->tag, l->sym_loc,
578 symbol_index, cp, cp_end, explicit_bracketing);
579 if (var && l->named_ref)
580 var->hidden_by = l->named_ref;
583 variant_add (l->named_ref->id, l->named_ref->loc,
584 symbol_index, cp, cp_end, explicit_bracketing);
589 for (i = 0; i < variant_count; ++i)
591 variant *var = &variant_table[i];
592 unsigned symbol_index = var->symbol_index;
594 /* Check visibility from mid-rule actions. */
595 if (midrule_rhs_index != 0
596 && (symbol_index == 0 || midrule_rhs_index < symbol_index))
597 var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
599 /* Check correct bracketing. */
600 if (!explicit_bracketing && contains_dot_or_dash (var->id))
601 var->err |= VARIANT_BAD_BRACKETING;
603 /* Check using of hidden symbols. */
605 var->err |= VARIANT_HIDDEN;
609 valid_variant_index = i;
614 switch (valid_variants)
618 unsigned len = (explicit_bracketing || !ref_tail_fields) ?
619 cp_end - cp : ref_tail_fields - cp;
622 complain_at_indent (text_loc, &indent, _("invalid reference: %s"),
624 indent += SUB_INDENT;
625 if (midrule_rhs_index)
628 _("symbol not found in production before $%d: %.*s");
629 complain_at_indent (rule->location, &indent, format,
630 midrule_rhs_index, len, cp);
635 _("symbol not found in production: %.*s");
636 complain_at_indent (rule->location, &indent, format,
640 if (variant_count > 0)
641 show_sub_messages (cp, explicit_bracketing, midrule_rhs_index,
642 dollar_or_at, false, indent);
648 if (variant_count > 1)
650 warn_at_indent (text_loc, &indent, _("misleading reference: %s"),
652 show_sub_messages (cp, explicit_bracketing, midrule_rhs_index,
653 dollar_or_at, true, indent + SUB_INDENT);
656 unsigned symbol_index =
657 variant_table[valid_variant_index].symbol_index;
658 return (symbol_index == midrule_rhs_index) ? LHS_REF : symbol_index;
665 complain_at_indent (text_loc, &indent, _("ambiguous reference: %s"),
667 show_sub_messages (cp, explicit_bracketing, midrule_rhs_index,
668 dollar_or_at, false, indent + SUB_INDENT);
677 /* Keeps track of the maximum number of semantic values to the left of
678 a handle (those referenced by $0, $-1, etc.) are required by the
679 semantic actions of this grammar. */
680 int max_left_semantic_context = 0;
683 /*------------------------------------------------------------------.
684 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
686 | Possible inputs: $[<TYPENAME>]($|integer) |
688 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
689 `------------------------------------------------------------------*/
692 handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
694 char const *type_name = NULL;
697 symbol_list *effective_rule;
698 int effective_rule_length;
701 if (rule->midrule_parent_rule)
703 effective_rule = rule->midrule_parent_rule;
704 effective_rule_length = rule->midrule_parent_rhs_index - 1;
708 effective_rule = rule;
709 effective_rule_length = symbol_list_length (rule->next);
712 /* Get the type name if explicit. */
719 /* The '>' symbol will be later replaced by '\0'. Original
720 'text' is needed for error messages. */
723 if (untyped_var_seen)
724 complain_at (dollar_loc, _("explicit type given in untyped grammar"));
728 n = parse_ref (cp, effective_rule, effective_rule_length,
729 rule->midrule_parent_rhs_index, text, dollar_loc, '$');
741 type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0);
745 if (union_seen | tag_seen)
747 if (rule->midrule_parent_rule)
748 complain_at (dollar_loc,
749 _("$$ for the midrule at $%d of `%s'"
750 " has no declared type"),
751 rule->midrule_parent_rhs_index,
752 effective_rule->content.sym->tag);
754 complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
755 rule->content.sym->tag);
758 untyped_var_seen = true;
762 obstack_fgrow1 (&obstack_for_string,
763 "]b4_lhs_value([%s])[", type_name);
764 rule->action_props.is_value_used = true;
768 if (max_left_semantic_context < 1 - n)
769 max_left_semantic_context = 1 - n;
770 if (!type_name && 0 < n)
772 symbol_list_n_type_name_get (effective_rule, dollar_loc, n);
775 if (union_seen | tag_seen)
776 complain_at (dollar_loc, _("$%s of `%s' has no declared type"),
777 cp, effective_rule->content.sym->tag);
779 untyped_var_seen = true;
783 obstack_fgrow3 (&obstack_for_string,
784 "]b4_rhs_value(%d, %d, [%s])[",
785 effective_rule_length, n, type_name);
787 symbol_list_n_get (effective_rule, n)->action_props.is_value_used =
794 /*------------------------------------------------------.
795 | TEXT is a location token (i.e., a `@...'). Output to |
796 | OBSTACK_FOR_STRING a reference to this location. |
797 `------------------------------------------------------*/
800 handle_action_at (symbol_list *rule, char *text, location at_loc)
803 symbol_list *effective_rule;
804 int effective_rule_length;
807 if (rule->midrule_parent_rule)
809 effective_rule = rule->midrule_parent_rule;
810 effective_rule_length = rule->midrule_parent_rhs_index - 1;
814 effective_rule = rule;
815 effective_rule_length = symbol_list_length (rule->next);
818 muscle_percent_define_ensure("locations", at_loc, true);
820 n = parse_ref (cp, effective_rule, effective_rule_length,
821 rule->midrule_parent_rhs_index, text, at_loc, '@');
828 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
832 obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
833 effective_rule_length, n);
839 /*-------------------------.
840 | Initialize the scanner. |
841 `-------------------------*/
843 /* Translate the dollars and ats in \a self, in the context \a sc_context
844 (SC_RULE_ACTION, SC_SYMBOL_ACTION, INITIAL). */
847 translate_action (code_props *self, int sc_context)
850 static bool initialized = false;
853 obstack_init (&obstack_for_string);
858 loc->start = loc->end = self->location.start;
859 yy_switch_to_buffer (yy_scan_string (self->code));
860 res = code_lex (self, sc_context);
861 yy_delete_buffer (YY_CURRENT_BUFFER);
866 /*------------------------------------------------------------------------.
867 | Implementation of the public interface as documented in "scan-code.h". |
868 `------------------------------------------------------------------------*/
871 code_props_none_init (code_props *self)
873 *self = code_props_none;
876 code_props const code_props_none = CODE_PROPS_NONE_INIT;
879 code_props_plain_init (code_props *self, char const *code,
882 self->kind = CODE_PROPS_PLAIN;
884 self->location = code_loc;
885 self->is_value_used = false;
887 self->named_ref = NULL;
891 code_props_symbol_action_init (code_props *self, char const *code,
894 self->kind = CODE_PROPS_SYMBOL_ACTION;
896 self->location = code_loc;
897 self->is_value_used = false;
899 self->named_ref = NULL;
903 code_props_rule_action_init (code_props *self, char const *code,
904 location code_loc, symbol_list *rule,
905 named_ref *name, bool is_predicate)
907 self->kind = CODE_PROPS_RULE_ACTION;
909 self->location = code_loc;
910 self->is_value_used = false;
912 self->named_ref = name;
913 self->is_predicate = is_predicate;
917 code_props_translate_code (code_props *self)
921 case CODE_PROPS_NONE:
923 case CODE_PROPS_PLAIN:
924 self->code = translate_action (self, INITIAL);
926 case CODE_PROPS_SYMBOL_ACTION:
927 self->code = translate_action (self, SC_SYMBOL_ACTION);
929 case CODE_PROPS_RULE_ACTION:
930 self->code = translate_action (self, SC_RULE_ACTION);
936 code_scanner_last_string_free (void)
942 code_scanner_free (void)
944 obstack_free (&obstack_for_string, 0);
945 variant_table_free ();
947 /* Reclaim Flex's buffers. */