1 /* Bison Action Scanner -*- C -*-
3 Copyright (C) 2006, 2007, 2008 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);
52 /* A string to be pushed to obstack after dollar/at has been handled */
53 static char *ref_tail_fields;
55 static location the_location;
56 static location *loc = &the_location;
58 /* A string representing the most recent translation. */
59 static char *last_string;
61 /* True if an untyped $$ or $n was seen. */
62 static bool untyped_var_seen;
65 /* C and C++ comments in code. */
66 %x SC_COMMENT SC_LINE_COMMENT
67 /* Strings and characters in code. */
68 %x SC_STRING SC_CHARACTER
69 /* Whether in a rule or symbol action. Specifies the translation
71 %x SC_RULE_ACTION SC_SYMBOL_ACTION
74 /* POSIX says that a tag must be both an id and a C union member, but
75 historically almost any character is allowed in a tag. We disallow
76 NUL and newline, as this simplifies our implementation. */
79 /* Zero or more instances of backslash-newline. Following GCC, allow
80 white space between the backslash and the newline. */
81 splice (\\[ \f\t\v]*\n)*
83 /* C style identifier. Must start with letter. Will be used for
84 named symbol references. */
85 letter [-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
86 id {letter}({letter}|[0-9])*
87 ref -?[0-9]+|{id}|"["{id}"]"|"$"
92 /* Nesting level of the current code in braces. */
95 /* Whether a semicolon is probably needed.
96 The heuristic is that a semicolon is not needed after `{', `}', `;',
97 or a C preprocessor directive, and that whitespaces and comments
98 do not affect this flag.
99 Note that `{' does not need a semicolon because of `{}'.
100 A semicolon may be needed before a cpp direcive, but don't bother. */
101 bool need_semicolon = false;
103 /* Whether in a C preprocessor directive. Don't use a start condition
104 for this because, at the end of strings and comments, we still need
105 to know whether we're in a directive. */
108 /* This scanner is special: it is invoked only once, henceforth
109 is expected to return only once. This initialization is
110 therefore done once per action to translate. */
111 aver (sc_context == SC_SYMBOL_ACTION
112 || sc_context == SC_RULE_ACTION
113 || sc_context == INITIAL);
117 /*------------------------------------------------------------.
118 | Scanning a C comment. The initial `/ *' is already eaten. |
119 `------------------------------------------------------------*/
123 "*"{splice}"/" STRING_GROW; BEGIN sc_context;
127 /*--------------------------------------------------------------.
128 | Scanning a line comment. The initial `//' is already eaten. |
129 `--------------------------------------------------------------*/
133 "\n" STRING_GROW; BEGIN sc_context;
134 {splice} STRING_GROW;
138 /*--------------------------------------------.
139 | Scanning user-code characters and strings. |
140 `--------------------------------------------*/
142 <SC_CHARACTER,SC_STRING>
144 {splice}|\\{splice}. STRING_GROW;
149 "'" STRING_GROW; BEGIN sc_context;
154 "\"" STRING_GROW; BEGIN sc_context;
158 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
162 need_semicolon = true;
167 need_semicolon = true;
175 BEGIN SC_LINE_COMMENT;
181 "$"("<"{tag}">")?{ref} {
183 handle_action_dollar (self->rule, yytext, *loc);
184 if (ref_tail_fields != NULL) {
185 obstack_sgrow (&obstack_for_string, ref_tail_fields);
187 need_semicolon = true;
191 handle_action_at (self->rule, yytext, *loc);
192 if (ref_tail_fields != NULL) {
193 obstack_sgrow (&obstack_for_string, ref_tail_fields);
195 need_semicolon = true;
198 warn_at (*loc, _("stray `$'"));
199 obstack_sgrow (&obstack_for_string, "$][");
200 need_semicolon = true;
203 warn_at (*loc, _("stray `@'"));
204 obstack_sgrow (&obstack_for_string, "@@");
205 need_semicolon = true;
208 obstack_sgrow (&obstack_for_string, "@{");
209 need_semicolon = true;
212 obstack_sgrow (&obstack_for_string, "@}");
213 need_semicolon = true;
216 ";" STRING_GROW; need_semicolon = false;
217 "{" STRING_GROW; ++braces_level; need_semicolon = false;
219 bool outer_brace = --braces_level == 0;
221 /* As an undocumented Bison extension, append `;' before the last
222 brace in braced code, so that the user code can omit trailing
223 `;'. But do not append `;' if emulating Yacc, since Yacc does
225 if (outer_brace && !yacc_flag && language_prio == default_prio
226 && skeleton_prio == default_prio && need_semicolon && ! in_cpp)
228 warn_at (*loc, _("a `;' might be needed at the end of action code"));
229 warn_at (*loc, _("future versions of Bison will not add the `;'"));
230 obstack_1grow (&obstack_for_string, ';');
234 need_semicolon = false;
237 /* Preprocessing directives should only be recognized at the beginning
238 of lines, allowing whitespace including comments, but in C/C++,
239 `#' can only be the start of preprocessor directives or within
240 `#define' directives anyway, so don't bother with begin of line. */
241 "#" STRING_GROW; in_cpp = true;
243 {splice} STRING_GROW;
244 [\n\r] STRING_GROW; if (in_cpp) in_cpp = need_semicolon = false;
246 . STRING_GROW; need_semicolon = true;
252 obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
253 self->is_value_used = true;
256 obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
257 locations_flag = true;
262 /*-----------------------------------------.
263 | Escape M4 quoting characters in C code. |
264 `-----------------------------------------*/
268 \$ obstack_sgrow (&obstack_for_string, "$][");
269 \@ obstack_sgrow (&obstack_for_string, "@@");
270 \[ obstack_sgrow (&obstack_for_string, "@{");
271 \] obstack_sgrow (&obstack_for_string, "@}");
274 /*-----------------------------------------------------.
275 | By default, grow the string obstack with the input. |
276 `-----------------------------------------------------*/
280 /* End of processing. */
290 symbol_list_null(symbol_list *l)
292 if (l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
299 is_dot_or_dash(char ch)
301 return ch == '.' || ch == '-';
307 return '0' <= ch && ch <= '9';
311 contains_dot_or_dash(const char* str)
313 return strpbrk(str, ".-") != NULL;
316 #define VARIANT_HIDDEN (1 << 0)
317 #define VARIANT_BAD_BRACKETING (1 << 1)
318 #define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2)
322 /* Index in symbol list. */
325 /* Matched symbol id and loc. */
329 /* Hidding named reference. */
330 named_ref* hidden_by;
336 static variant *variant_table = 0;
337 static unsigned variant_table_size = 0;
338 static unsigned variant_count = 0;
344 if (variant_count > variant_table_size)
346 while (variant_count > variant_table_size)
347 variant_table_size = 2 * variant_table_size + 3;
348 variant_table = xnrealloc (variant_table, variant_table_size,
349 sizeof *variant_table);
351 return &variant_table[variant_count - 1];
355 find_prefix_end(const char *prefix, char *begin, char *end)
359 while (*prefix && ptr != end)
373 variant_add(uniqstr id, location loc, long int ind,
374 char *cp, char *cp_end, bool exact_mode)
378 prefix_end = find_prefix_end(id, cp, cp_end);
380 (prefix_end == cp_end ||
381 (!exact_mode && is_dot_or_dash(*prefix_end))))
383 variant *r = variant_table_grow();
395 #define INVALID_REF (INT_MIN)
396 #define LHS_REF (INT_MIN + 1)
399 parse_named_ref(char *cp, symbol_list *rule, int rule_length,
400 int midrule_rhs_index, char *text, location loc,
415 if (is_digit (*cp) || (*cp == '-' && is_digit (* (cp + 1))))
417 long int num = strtol (cp, &cp, 10);
418 if (1 - INT_MAX + rule_length <= num && num <= rule_length)
422 complain_at (loc, _("integer out of range: %s"), quote (text));
431 /* Ignore the brackets. */
433 for (p = cp; *p != ']'; ++p);
440 /* Take all characters of the name. */
441 for (p = cp; *p; ++p)
442 if (is_dot_or_dash(*p))
447 for (p = cp; *p; ++p);
451 /* Add all relevant variants. */
453 for (ind = 0, l = rule; !symbol_list_null(l); ++ind, l = l->next)
455 if (l->content_type != SYMLIST_SYMBOL)
458 variant = variant_add(l->content.sym->tag, l->sym_loc, ind,
459 cp, cp_end, exact_mode);
461 if (variant && l->named_ref)
462 variant->hidden_by = l->named_ref;
465 variant_add(l->named_ref->id, l->named_ref->loc, ind,
466 cp, cp_end, exact_mode);
472 for (i = 0; i < variant_count; ++i)
474 variant = &variant_table[i];
477 /* Check visibility from mid-rule actions. */
478 if (midrule_rhs_index != 0 &&
479 (ind == 0 || ind > midrule_rhs_index))
481 variant->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
485 /* Check correct bracketing. */
486 if (!exact_mode && contains_dot_or_dash(variant->id))
488 variant->err |= VARIANT_BAD_BRACKETING;
492 /* Check using of hidden symbols. */
493 if (variant->hidden_by != NULL)
495 variant->err |= VARIANT_HIDDEN;
503 if (variant_count == 1 && has_valid)
505 /* The only "good" case is here. */
506 ind = variant_table[0].ind;
507 if (ind == midrule_rhs_index)
513 /* Start complaining. */
515 if (variant_count == 0)
516 complain_at (loc, _("reference is invalid: %s, symbol not found"),
518 else if (variant_count > 1 && !has_error)
519 complain_at (loc, _("reference is ambiguous: %s"),
521 else if (variant_count > 1 && has_valid && has_error)
522 complain_at (loc, _("reference is misleading: %s"),
525 complain_at (loc, _("reference is invalid: %s"),
528 for (i = 0; i < variant_count; ++i)
530 static char at_buf[20];
532 variant = &variant_table[i];
534 if (variant->ind == 0)
535 strcpy(at_buf, "$$");
537 snprintf(at_buf, sizeof(at_buf), "$%ld", variant->ind);
539 if (variant->err == 0)
540 complain_at (variant->loc, _(" refers to: %c%s at %s"),
541 dollar_or_at, variant->id, at_buf);
544 static struct obstack msg_buf;
545 const char *tail = "";
550 tail = cp + strlen(variant->id);
552 if (variant->hidden_by)
554 id = variant->hidden_by->id;
555 loc = variant->hidden_by->loc;
563 /* Create the explanation message. */
565 obstack_init (&msg_buf);
567 obstack_fgrow1 (&msg_buf, " possibly meant: %c", dollar_or_at);
568 if (contains_dot_or_dash (id))
569 obstack_fgrow1 (&msg_buf, "[%s]", id);
571 obstack_sgrow (&msg_buf, id);
572 obstack_sgrow (&msg_buf, tail);
574 if (variant->err & VARIANT_HIDDEN)
576 obstack_fgrow1 (&msg_buf, ", hiding %c", dollar_or_at);
577 if (contains_dot_or_dash (variant->id))
578 obstack_fgrow1 (&msg_buf, "[%s]", variant->id);
580 obstack_sgrow (&msg_buf, variant->id);
581 obstack_sgrow (&msg_buf, tail);
584 obstack_fgrow1 (&msg_buf, " at %s", at_buf);
586 if (variant->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
587 obstack_fgrow1 (&msg_buf, ", cannot be accessed from "
588 "mid-rule action at $%d", midrule_rhs_index);
590 obstack_1grow (&msg_buf, '\0');
591 complain_at (loc, _("%s"), (char *) obstack_finish (&msg_buf));
592 obstack_free (&msg_buf, 0);
599 /* Keeps track of the maximum number of semantic values to the left of
600 a handle (those referenced by $0, $-1, etc.) are required by the
601 semantic actions of this grammar. */
602 int max_left_semantic_context = 0;
605 /*------------------------------------------------------------------.
606 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
608 | Possible inputs: $[<TYPENAME>]($|integer) |
610 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
611 `------------------------------------------------------------------*/
614 handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
616 char const *type_name = NULL;
619 symbol_list *effective_rule;
620 int effective_rule_length, n;
622 if (rule->midrule_parent_rule)
624 effective_rule = rule->midrule_parent_rule;
625 effective_rule_length = rule->midrule_parent_rhs_index - 1;
629 effective_rule = rule;
630 effective_rule_length = symbol_list_length (rule->next);
633 /* Get the type name if explicit. */
640 /* The '>' symbol will be later replaced by '\0'. Original
641 'text' is needed for error messages. */
644 if (untyped_var_seen)
645 complain_at (dollar_loc, _("explicit type given in untyped grammar"));
649 n = parse_named_ref (cp, effective_rule, effective_rule_length,
650 rule->midrule_parent_rhs_index, text, dollar_loc, '$');
662 type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0);
666 if (union_seen | tag_seen)
668 if (rule->midrule_parent_rule)
669 complain_at (dollar_loc,
670 _("$$ for the midrule at $%d of `%s'"
671 " has no declared type"),
672 rule->midrule_parent_rhs_index,
673 effective_rule->content.sym->tag);
675 complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
676 rule->content.sym->tag);
679 untyped_var_seen = true;
683 obstack_fgrow1 (&obstack_for_string,
684 "]b4_lhs_value([%s])[", type_name);
685 rule->action_props.is_value_used = true;
689 if (max_left_semantic_context < 1 - n)
690 max_left_semantic_context = 1 - n;
691 if (!type_name && 0 < n)
693 symbol_list_n_type_name_get (effective_rule, dollar_loc, n);
696 if (union_seen | tag_seen)
697 complain_at (dollar_loc, _("$%s of `%s' has no declared type"),
698 cp, effective_rule->content.sym->tag);
700 untyped_var_seen = true;
704 obstack_fgrow3 (&obstack_for_string,
705 "]b4_rhs_value(%d, %d, [%s])[",
706 effective_rule_length, n, type_name);
708 symbol_list_n_get (effective_rule, n)->action_props.is_value_used =
715 /*------------------------------------------------------.
716 | TEXT is a location token (i.e., a `@...'). Output to |
717 | OBSTACK_FOR_STRING a reference to this location. |
718 `------------------------------------------------------*/
721 handle_action_at (symbol_list *rule, char *text, location at_loc)
724 symbol_list *effective_rule;
725 int effective_rule_length, n;
727 if (rule->midrule_parent_rule)
729 effective_rule = rule->midrule_parent_rule;
730 effective_rule_length = rule->midrule_parent_rhs_index - 1;
734 effective_rule = rule;
735 effective_rule_length = symbol_list_length (rule->next);
738 locations_flag = true;
740 n = parse_named_ref (cp, effective_rule, effective_rule_length,
741 rule->midrule_parent_rhs_index, text, at_loc, '@');
748 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
752 obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
753 effective_rule_length, n);
759 /*-------------------------.
760 | Initialize the scanner. |
761 `-------------------------*/
763 /* Translate the dollars and ats in \a self, in the context \a sc_context
764 (SC_RULE_ACTION, SC_SYMBOL_ACTION, INITIAL). */
767 translate_action (code_props *self, int sc_context)
770 static bool initialized = false;
773 obstack_init (&obstack_for_string);
778 loc->start = loc->end = self->location.start;
779 yy_switch_to_buffer (yy_scan_string (self->code));
780 res = code_lex (self, sc_context);
781 yy_delete_buffer (YY_CURRENT_BUFFER);
786 /*------------------------------------------------------------------------.
787 | Implementation of the public interface as documented in "scan-code.h". |
788 `------------------------------------------------------------------------*/
791 code_props_none_init (code_props *self)
793 *self = code_props_none;
796 code_props const code_props_none = CODE_PROPS_NONE_INIT;
799 code_props_plain_init (code_props *self, char const *code, location code_loc)
801 self->kind = CODE_PROPS_PLAIN;
803 self->location = code_loc;
804 self->is_value_used = false;
806 self->named_ref = NULL;
810 code_props_symbol_action_init (code_props *self, char const *code,
813 self->kind = CODE_PROPS_SYMBOL_ACTION;
815 self->location = code_loc;
816 self->is_value_used = false;
818 self->named_ref = NULL;
822 code_props_rule_action_init (code_props *self, char const *code,
823 location code_loc, symbol_list *rule,
824 named_ref *named_ref)
826 self->kind = CODE_PROPS_RULE_ACTION;
828 self->location = code_loc;
829 self->is_value_used = false;
831 self->named_ref = named_ref;
835 code_props_translate_code (code_props *self)
839 case CODE_PROPS_NONE:
841 case CODE_PROPS_PLAIN:
842 self->code = translate_action (self, INITIAL);
844 case CODE_PROPS_SYMBOL_ACTION:
845 self->code = translate_action (self, SC_SYMBOL_ACTION);
847 case CODE_PROPS_RULE_ACTION:
848 self->code = translate_action (self, SC_RULE_ACTION);
854 code_scanner_last_string_free (void)
860 code_scanner_free (void)
862 obstack_free (&obstack_for_string, 0);
863 /* Reclaim Flex's buffers. */