1 /* Bison Action Scanner -*- C -*-
3 Copyright (C) 2006-2015, 2018-2021 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 <https://www.gnu.org/licenses/>. */
20 %option debug nodefault noinput nounput noyywrap never-interactive
21 %option prefix="code_" outfile="lex.yy.c"
25 #include <get-errno.h>
28 #include "src/complain.h"
29 #include "src/getargs.h"
30 #include "src/muscle-tab.h"
31 #include "src/reader.h"
32 #include "src/scan-code.h"
33 #include "src/symlist.h"
35 #define FLEX_PREFIX(Id) code_ ## Id
36 #include "src/flex-scanner.h"
38 /* Work around a bug in flex 2.5.31. See Debian bug 333231
39 <https://bugs.debian.org/333231>. */
43 struct obstack *obstack_for_actions = &obstack_for_string;
45 /* The current calling start condition: SC_RULE_ACTION or
47 # define YY_DECL static char *code_lex (code_props *self, int sc_context)
50 #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng);
52 static char *fetch_type_name (char *cp, char const **type_name,
53 const location *dollar_loc);
55 static void handle_action_dollar (symbol_list *rule, char *cp,
56 const location *dollar_loc);
57 static void handle_action_at (symbol_list *rule, char *cp,
58 const location *at_loc);
60 /* A string to be pushed to obstack after dollar/at has been handled. */
61 static char *ref_tail_fields;
63 static location current_loc;
64 static location *loc = ¤t_loc;
66 /* A string representing the most recent translation. */
67 static char *last_string;
69 /* True if an untyped $$ or $n was seen. */
70 static bool untyped_var_seen;
73 /* C and C++ comments in code. */
74 %x SC_COMMENT SC_LINE_COMMENT
75 /* Strings and characters in code. */
76 %x SC_STRING SC_CHARACTER
77 /* Whether in a rule or symbol action. Specifies the translation
79 %x SC_RULE_ACTION SC_SYMBOL_ACTION
82 /* POSIX says that a tag must be both an id and a C union member, but
83 historically almost any character is allowed in a tag. We disallow
84 NUL and newline, as this simplifies our implementation. We allow
85 "->" as a means to dereference a pointer. */
86 tag ([^\0\n>]|->)*[^-]
88 /* Zero or more instances of backslash-newline. Following GCC, allow
89 white space between the backslash and the newline. */
90 splice (\\[ \f\t\v]*\n)*
92 /* C style identifier. Must start with letter. Will be used for
93 named symbol references. Shall be kept synchronized with
94 scan-gram.l "letter" and "id". */
95 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
96 id {letter}({letter}|[-0-9])*
97 ref -?[0-9]+|{id}|"["{id}"]"|"$"
102 /* This scanner is special: it is invoked only once, henceforth
103 is expected to return only once. This initialization is
104 therefore done once per action to translate. */
105 aver (sc_context == SC_SYMBOL_ACTION
106 || sc_context == SC_RULE_ACTION
107 || sc_context == INITIAL);
111 /*------------------------------------------------------------.
112 | Scanning a C comment. The initial '/ *' is already eaten. |
113 `------------------------------------------------------------*/
117 "*"{splice}"/" STRING_GROW (); BEGIN sc_context;
121 /*--------------------------------------------------------------.
122 | Scanning a line comment. The initial '//' is already eaten. |
123 `--------------------------------------------------------------*/
127 "\n" STRING_GROW (); BEGIN sc_context;
128 {splice} STRING_GROW ();
132 /*--------------------------------------------.
133 | Scanning user-code characters and strings. |
134 `--------------------------------------------*/
136 <SC_CHARACTER,SC_STRING>
138 {splice}|\\{splice}. STRING_GROW ();
143 "'" STRING_GROW (); BEGIN sc_context;
148 "\"" STRING_GROW (); BEGIN sc_context;
152 <SC_RULE_ACTION,SC_SYMBOL_ACTION>
154 "'" STRING_GROW (); BEGIN SC_CHARACTER;
155 "\"" STRING_GROW (); BEGIN SC_STRING;
156 "/"{splice}"*" STRING_GROW (); BEGIN SC_COMMENT;
157 "/"{splice}"/" STRING_GROW (); BEGIN SC_LINE_COMMENT;
160 complain (loc, Wother, _("stray '%s'"), yytext);
161 obstack_escape (&obstack_for_string, yytext);
167 "$"("<"{tag}">")?{ref} {
168 ref_tail_fields = NULL;
169 handle_action_dollar (self->rule, yytext, loc);
171 obstack_sgrow (&obstack_for_string, ref_tail_fields);
174 ref_tail_fields = NULL;
175 handle_action_at (self->rule, yytext, loc);
177 obstack_sgrow (&obstack_for_string, ref_tail_fields);
183 "$"("<"{tag}">")?"$" {
184 const char *type_name = NULL;
185 fetch_type_name (yytext + 1, &type_name, loc)[-1] = 0;
186 obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar(");
187 obstack_quote (&obstack_for_string, type_name);
188 obstack_sgrow (&obstack_for_string, ")[");
189 self->is_value_used = true;
192 obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
193 muscle_percent_define_ensure("locations", *loc, true);
200 /* Escape M4 quoting characters in C code. */
201 [$@\[\]] obstack_escape (&obstack_for_string, yytext);
203 /* By default, grow the string obstack with the input. */
206 /* End of processing. */
207 <<EOF>> STRING_FINISH (); return last_string;
213 is_dot_or_dash (char ch)
215 return ch == '.' || ch == '-';
219 contains_dot_or_dash (const char* p)
221 return !!strpbrk (p, ".-");
224 /* Defines a variant of a symbolic name resolution. */
227 /* Index in symbol list. */
230 /* Matched symbol id and loc. */
234 /* Hiding named reference. */
235 named_ref* hidden_by;
237 /* Error flags. May contain zero (no errors) or
238 a combination of VARIANT_* values. */
242 /* Set when the variant refers to a symbol hidden
243 by an explicit symbol reference. */
244 #define VARIANT_HIDDEN (1 << 0)
246 /* Set when the variant refers to a symbol containing
247 dots or dashes. Will require explicit bracketing. */
248 #define VARIANT_BAD_BRACKETING (1 << 1)
250 /* Set when the variant refers to a symbol which is
251 not visible from current midrule. */
252 #define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2)
254 static variant *variant_table = NULL;
255 static int variant_table_size = 0;
256 static int variant_count = 0;
259 variant_table_grow (void)
262 if (variant_count > variant_table_size)
264 while (variant_count > variant_table_size)
265 variant_table_size = 2 * variant_table_size + 3;
266 variant_table = xnrealloc (variant_table, variant_table_size,
267 sizeof *variant_table);
269 return &variant_table[variant_count - 1];
273 variant_table_free (void)
275 free (variant_table);
276 variant_table = NULL;
277 variant_table_size = variant_count = 0;
281 find_prefix_end (char const *prefix, char const *cp, char const *end)
283 for (; *prefix && cp != end; ++prefix, ++cp)
287 return *prefix ? NULL : cp;
291 variant_add (uniqstr id, location id_loc, int symbol_index,
292 char const *cp, char const *cp_end, bool explicit_bracketing)
294 char const *prefix_end = find_prefix_end (id, cp, cp_end);
296 (prefix_end == cp_end ||
297 (!explicit_bracketing && is_dot_or_dash (*prefix_end))))
299 variant *r = variant_table_grow ();
300 r->symbol_index = symbol_index;
312 get_at_spec(int symbol_index)
314 static char at_buf[20];
315 if (symbol_index == 0)
316 strcpy (at_buf, "$$");
318 snprintf (at_buf, sizeof at_buf, "$%u", symbol_index);
323 show_sub_message (warnings warning,
324 const char* cp, bool explicit_bracketing,
325 int midrule_rhs_index, char dollar_or_at,
328 const char *at_spec = get_at_spec (var->symbol_index);
331 subcomplain (&var->loc, warning,
332 _("refers to: %c%s at %s"), dollar_or_at,
341 id = var->hidden_by->id;
342 id_loc = var->hidden_by->loc;
350 const char *tail = explicit_bracketing ? "" : cp + strlen (var->id);
352 /* Create the explanation message. */
353 static struct obstack msg_buf;
354 obstack_init (&msg_buf);
356 obstack_printf (&msg_buf, _("possibly meant: %c"), dollar_or_at);
357 if (contains_dot_or_dash (id))
358 obstack_printf (&msg_buf, "[%s]", id);
360 obstack_sgrow (&msg_buf, id);
361 obstack_sgrow (&msg_buf, tail);
363 if (var->err & VARIANT_HIDDEN)
365 obstack_printf (&msg_buf, _(", hiding %c"), dollar_or_at);
366 if (contains_dot_or_dash (var->id))
367 obstack_printf (&msg_buf, "[%s]", var->id);
369 obstack_sgrow (&msg_buf, var->id);
370 obstack_sgrow (&msg_buf, tail);
373 obstack_printf (&msg_buf, _(" at %s"), at_spec);
375 if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
376 obstack_printf (&msg_buf,
377 _(", cannot be accessed from midrule action at $%d"),
380 subcomplain (&id_loc, warning, "%s",
381 obstack_finish0 (&msg_buf));
382 obstack_free (&msg_buf, 0);
387 show_sub_messages (warnings warning,
388 const char* cp, bool explicit_bracketing,
389 int midrule_rhs_index, char dollar_or_at)
391 for (int i = 0; i < variant_count; ++i)
392 show_sub_message (warning | silent,
393 cp, explicit_bracketing,
394 midrule_rhs_index, dollar_or_at,
398 /* Returned from "parse_ref" when the reference
400 #define INVALID_REF (INT_MIN)
402 /* Returned from "parse_ref" when the reference
403 points to LHS ($$) of the current rule or midrule. */
404 #define LHS_REF (INT_MIN + 1)
406 /* Parse a positional reference in RULE. */
408 parse_positional_ref (char *cp, int rule_length,
409 char *text, const location *text_loc)
411 long num = strtol (cp, &cp, 10);
412 if (1 - INT_MAX + rule_length <= num && num <= rule_length)
416 complain (text_loc, complaint, _("integer out of range: %s"),
423 /* Parse named or positional reference. In case of positional
424 references, can return negative values for $-n "deep" stack
427 parse_ref (char *cp, symbol_list *rule, int rule_length,
428 int midrule_rhs_index, char *text, const location *text_loc,
434 if (c_isdigit (*cp) || (*cp == '-' && c_isdigit (cp[1])))
435 return parse_positional_ref (cp, rule_length, text, text_loc);
437 bool const explicit_bracketing = *cp == '[';
439 if (explicit_bracketing)
442 ref_tail_fields = strpbrk (cp, ".-");
444 char const *cp_end = strchr (cp, explicit_bracketing ? ']' : '\0');
446 /* Add all relevant variants. */
451 for (symbol_index = 0, l = rule; !symbol_list_null (l);
452 ++symbol_index, l = l->next)
454 if (l->content_type != SYMLIST_SYMBOL)
458 = variant_add (l->content.sym->tag, l->sym_loc,
459 symbol_index, cp, cp_end, explicit_bracketing);
460 if (var && l->named_ref)
461 var->hidden_by = l->named_ref;
464 variant_add (l->named_ref->id, l->named_ref->loc,
465 symbol_index, cp, cp_end, explicit_bracketing);
470 int valid_variants = 0;
471 int valid_variant_index = 0;
472 for (int i = 0; i < variant_count; ++i)
474 variant *var = &variant_table[i];
475 int symbol_index = var->symbol_index;
477 /* Check visibility from midrule actions. */
478 if (midrule_rhs_index != 0
479 && (symbol_index == 0 || midrule_rhs_index < symbol_index))
480 var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
482 /* Check correct bracketing. */
483 if (!explicit_bracketing && contains_dot_or_dash (var->id))
484 var->err |= VARIANT_BAD_BRACKETING;
486 /* Check using of hidden symbols. */
488 var->err |= VARIANT_HIDDEN;
492 valid_variant_index = i;
497 switch (valid_variants)
501 int len = (explicit_bracketing || !ref_tail_fields) ?
502 cp_end - cp : ref_tail_fields - cp;
504 complain (text_loc, complaint,
505 _("invalid reference: %s"), quote (text));
508 location sym_loc = *text_loc;
509 sym_loc.start.column += 1;
510 sym_loc.end = sym_loc.start;
511 subcomplain (&sym_loc, complaint,
512 _("syntax error after '%c', expecting integer, "
513 "letter, '_', '[', or '$'"),
516 else if (midrule_rhs_index)
517 subcomplain (&rule->rhs_loc, complaint,
518 _("symbol not found in production before $%d: "
520 midrule_rhs_index, len, cp);
522 subcomplain (&rule->rhs_loc, complaint,
523 _("symbol not found in production: %.*s"),
526 if (variant_count > 0)
527 show_sub_messages (complaint,
528 cp, explicit_bracketing, midrule_rhs_index,
534 if (variant_count > 1)
536 complain (text_loc, Wother,
537 _("misleading reference: %s"), quote (text));
538 show_sub_messages (Wother,
539 cp, explicit_bracketing, midrule_rhs_index,
544 variant_table[valid_variant_index].symbol_index;
545 return (symbol_index == midrule_rhs_index) ? LHS_REF : symbol_index;
551 complain (text_loc, complaint,
552 _("ambiguous reference: %s"), quote (text));
553 show_sub_messages (complaint,
554 cp, explicit_bracketing, midrule_rhs_index,
561 /* Keeps track of the maximum number of semantic values to the left of
562 a handle (those referenced by $0, $-1, etc.) are required by the
563 semantic actions of this grammar. */
564 int max_left_semantic_context = 0;
567 /* If CP points to a typename (i.e., <.*?>), set TYPE_NAME to its
568 beginning (i.e., after the opening "<", and return the pointer
569 immediately after it. */
573 fetch_type_name (char *cp, char const **type_name,
574 const location *dollar_loc)
579 /* Series of non-'>' or "->". */
580 while (*cp != '>' || cp[-1] == '-')
583 /* The '>' symbol will be later replaced by '\0'. Original
584 'text' is needed for error messages. */
586 if (untyped_var_seen)
587 complain (dollar_loc, complaint,
588 _("explicit type given in untyped grammar"));
594 /*------------------------------------------------------------------.
595 | TEXT is pointing to a wannabee semantic value (i.e., a '$'). |
597 | Possible inputs: $[<TYPENAME>]($|INTEGER) |
599 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
600 `------------------------------------------------------------------*/
603 handle_action_dollar (symbol_list *rule, char *text, const location *dollar_loc)
605 symbol_list *effective_rule;
606 int effective_rule_length;
608 if (rule->midrule_parent_rule)
610 effective_rule = rule->midrule_parent_rule;
611 effective_rule_length = rule->midrule_parent_rhs_index - 1;
615 effective_rule = rule;
616 effective_rule_length = symbol_list_length (rule->next);
619 /* The type name if explicit, otherwise left null. */
620 char const *type_name = NULL;
621 char *cp = fetch_type_name (text + 1, &type_name, dollar_loc);
622 int n = parse_ref (cp, effective_rule, effective_rule_length,
623 rule->midrule_parent_rhs_index, text, dollar_loc, '$');
624 /* End type_name. Don't do it earlier: parse_ref depends on TEXT. */
635 symbol_list *sym = symbol_list_n_get (rule, 0);
637 && !sym->content.sym->content->type_name)
639 if (union_seen || tag_seen)
641 if (rule->midrule_parent_rule)
642 complain (dollar_loc, complaint,
643 _("$$ for the midrule at $%d of %s"
644 " has no declared type"),
645 rule->midrule_parent_rhs_index,
646 quote (effective_rule->content.sym->tag));
648 complain (dollar_loc, complaint,
649 _("$$ of %s has no declared type"),
650 quote (rule->content.sym->tag));
653 untyped_var_seen = true;
656 obstack_printf (&obstack_for_string, "]b4_lhs_value(orig %d, ",
657 sym->content.sym->content->number);
658 obstack_quote (&obstack_for_string, type_name);
659 obstack_sgrow (&obstack_for_string, ")[");
660 rule->action_props.is_value_used = true;
664 /* Reference to a RHS symbol. */
667 if (max_left_semantic_context < 1 - n)
668 max_left_semantic_context = 1 - n;
669 symbol_list *sym = 0 < n ? symbol_list_n_get (effective_rule, n) : NULL;
671 && (!sym || !sym->content.sym->content->type_name))
673 if (union_seen || tag_seen)
674 complain (dollar_loc, complaint,
675 _("$%s of %s has no declared type"), cp,
676 quote (effective_rule->content.sym->tag));
678 untyped_var_seen = true;
681 obstack_printf (&obstack_for_string,
682 "]b4_rhs_value(%d, %d, ",
683 effective_rule_length, n);
685 obstack_printf (&obstack_for_string, "%s%d, ",
686 sym->content.sym->content->class == nterm_sym ? "orig " : "",
687 sym->content.sym->content->number);
689 obstack_sgrow (&obstack_for_string, "[], ");
691 obstack_quote (&obstack_for_string, type_name);
692 obstack_sgrow (&obstack_for_string, ")[");
695 if (muscle_percent_define_ifdef ("api.value.automove")
696 && sym->action_props.is_value_used)
697 complain (dollar_loc, Wother,
698 _("multiple occurrences of $%d with api.value.automove"),
700 sym->action_props.is_value_used = true;
708 /*------------------------------------------------------.
709 | TEXT is a location token (i.e., a '@...'). Output to |
710 | OBSTACK_FOR_STRING a reference to this location. |
711 `------------------------------------------------------*/
714 handle_action_at (symbol_list *rule, char *text, const location *at_loc)
716 symbol_list *effective_rule;
717 int effective_rule_length;
719 if (rule->midrule_parent_rule)
721 effective_rule = rule->midrule_parent_rule;
722 effective_rule_length = rule->midrule_parent_rhs_index - 1;
726 effective_rule = rule;
727 effective_rule_length = symbol_list_length (rule->next);
730 muscle_percent_define_ensure ("locations", *at_loc, true);
732 int n = parse_ref (text + 1, effective_rule, effective_rule_length,
733 rule->midrule_parent_rhs_index, text, at_loc, '@');
740 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
744 obstack_printf (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
745 effective_rule_length, n);
751 /*-------------------------.
752 | Initialize the scanner. |
753 `-------------------------*/
755 /* Translate the '$...' and '@...' in \a self, in the context \a
756 sc_context (SC_RULE_ACTION, SC_SYMBOL_ACTION, INITIAL). */
759 translate_action (code_props *self, int sc_context)
761 loc->start = loc->end = self->location.start;
762 yy_switch_to_buffer (yy_scan_string (self->code));
763 char *res = code_lex (self, sc_context);
764 yy_delete_buffer (YY_CURRENT_BUFFER);
768 /*------------------------------------------------------------------------.
769 | Implementation of the public interface as documented in "scan-code.h". |
770 `------------------------------------------------------------------------*/
773 code_props_none_init (code_props *self)
775 *self = code_props_none;
778 code_props code_props_none = CODE_PROPS_NONE_INIT;
781 code_props_plain_init (code_props *self, char const *code,
784 code_props_none_init (self);
785 self->kind = CODE_PROPS_PLAIN;
787 self->location = code_loc;
791 code_props_symbol_action_init (code_props *self, char const *code,
794 code_props_none_init (self);
795 self->kind = CODE_PROPS_SYMBOL_ACTION;
797 self->location = code_loc;
801 code_props_rule_action_init (code_props *self, char const *code,
802 location code_loc, symbol_list *rule,
803 named_ref *name, uniqstr type,
806 code_props_none_init (self);
807 self->kind = CODE_PROPS_RULE_ACTION;
809 self->location = code_loc;
811 self->named_ref = name;
813 self->is_predicate = is_predicate;
817 code_props_translate_code (code_props *self)
821 case CODE_PROPS_NONE:
823 case CODE_PROPS_PLAIN:
824 self->code = translate_action (self, INITIAL);
826 case CODE_PROPS_SYMBOL_ACTION:
827 self->code = translate_action (self, SC_SYMBOL_ACTION);
829 case CODE_PROPS_RULE_ACTION:
830 self->code = translate_action (self, SC_RULE_ACTION);
836 code_scanner_last_string_free (void)
842 code_scanner_init (void)
844 obstack_init (&obstack_for_string);
849 code_scanner_free (void)
851 obstack_free (&obstack_for_string, 0);
852 variant_table_free ();
854 /* Reclaim Flex's buffers. */