]> git.saurik.com Git - bison.git/blobdiff - src/scan-code.l
Fix bug that mistakes braced code in a declaration in the rules section
[bison.git] / src / scan-code.l
index 70b250bc78c8bd1394bc6a1d40645edf7d524618..4e464a7a8002f82ee5aecd30966730be8a6525aa 100644 (file)
 %option prefix="code_" outfile="lex.yy.c"
 
 %{
+#include <config.h>
+#include "system.h"
+
 /* Work around a bug in flex 2.5.31.  See Debian bug 333231
    <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
 #undef code_wrap
 #define code_wrap() 1
 
 #define FLEX_PREFIX(Id) code_ ## Id
+#include "complain.h"
 #include "flex-scanner.h"
 #include "reader.h"
 #include "getargs.h"
 
 /* The current calling start condition: SC_RULE_ACTION or
    SC_SYMBOL_ACTION. */
-# define YY_DECL const char *code_lex (int sc_context)
+# define YY_DECL const char *code_lex (int sc_context, symbol_list *rule)
 YY_DECL;
 
 #define YY_USER_ACTION  location_compute (loc, &loc->end, yytext, yyleng);
 
-static void handle_action_dollar (char *cp, location loc);
-static void handle_action_at (char *cp, location loc);
+static void handle_action_dollar (symbol_list *rule, char *cp,
+                                 location dollar_loc);
+static void handle_action_at (symbol_list *rule, char *cp, location at_loc);
 static location the_location;
 static location *loc = &the_location;
-
-/* The rule being processed. */
-symbol_list *current_rule;
 %}
  /* C and C++ comments in code. */
 %x SC_COMMENT SC_LINE_COMMENT
@@ -75,6 +77,9 @@ splice         (\\[ \f\t\v]*\n)*
 %%
 
 %{
+  /* Nesting level of the current code in braces.  */
+  int braces_level IF_LINT (= 0);
+
   /* This scanner is special: it is invoked only once, henceforth
      is expected to return only once.  This initialization is
      therefore done once per action to translate. */
@@ -146,8 +151,8 @@ splice       (\\[ \f\t\v]*\n)*
 
 <SC_RULE_ACTION>
 {
-  "$"("<"{tag}">")?(-?[0-9]+|"$")   handle_action_dollar (yytext, *loc);
-  "@"(-?[0-9]+|"$")                handle_action_at (yytext, *loc);
+  "$"("<"{tag}">")?(-?[0-9]+|"$")   handle_action_dollar (rule, yytext, *loc);
+  "@"(-?[0-9]+|"$")                handle_action_at (rule, yytext, *loc);
 
   "$"  {
     warn_at (*loc, _("stray `$'"));
@@ -157,6 +162,30 @@ splice      (\\[ \f\t\v]*\n)*
     warn_at (*loc, _("stray `@'"));
     obstack_sgrow (&obstack_for_string, "@@");
   }
+
+  "{"  STRING_GROW; ++braces_level;
+  "}"  {
+    bool outer_brace = --braces_level < 0;
+
+    /* As an undocumented Bison extension, append `;' before the last
+       brace in braced code, so that the user code can omit trailing
+       `;'.  But do not append `;' if emulating Yacc, since Yacc does
+       not append one.
+
+       FIXME: Bison should warn if a semicolon seems to be necessary
+       here, and should omit the semicolon if it seems unnecessary
+       (e.g., after ';', '{', or '}', each followed by comments or
+       white space).  Such a warning shouldn't depend on --yacc; it
+       should depend on a new --pedantic option, which would cause
+       Bison to warn if it detects an extension to POSIX.  --pedantic
+       should also diagnose other Bison extensions like %yacc.
+       Perhaps there should also be a GCC-style --pedantic-errors
+       option, so that such warnings are diagnosed as errors.  */
+    if (outer_brace && ! yacc_flag)
+      obstack_1grow (&obstack_for_string, ';');
+
+    STRING_GROW;
+  }
 }
 
 <SC_SYMBOL_ACTION>
@@ -207,11 +236,11 @@ int max_left_semantic_context = 0;
 `------------------------------------------------------------------*/
 
 static void
-handle_action_dollar (char *text, location loc)
+handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
 {
   const char *type_name = NULL;
   char *cp = text + 1;
-  int rule_length = symbol_list_length (current_rule->next);
+  int rule_length = symbol_list_length (rule->next);
 
   /* Get the type name if explicit. */
   if (*cp == '<')
@@ -226,15 +255,15 @@ handle_action_dollar (char *text, location loc)
   if (*cp == '$')
     {
       if (!type_name)
-       type_name = symbol_list_n_type_name_get (current_rule, loc, 0);
+       type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0);
       if (!type_name && typed)
-       complain_at (loc, _("$$ of `%s' has no declared type"),
-                    current_rule->sym->tag);
+       complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
+                    rule->sym->tag);
       if (!type_name)
        type_name = "";
       obstack_fgrow1 (&obstack_for_string,
                      "]b4_lhs_value([%s])[", type_name);
-      current_rule->used = true;
+      rule->used = true;
     }
   else
     {
@@ -247,19 +276,20 @@ handle_action_dollar (char *text, location loc)
          if (1-n > max_left_semantic_context)
            max_left_semantic_context = 1-n;
          if (!type_name && n > 0)
-           type_name = symbol_list_n_type_name_get (current_rule, loc, n);
+           type_name =
+             symbol_list_n_type_name_get (rule, dollar_loc, n);
          if (!type_name && typed)
-           complain_at (loc, _("$%d of `%s' has no declared type"),
-                        n, current_rule->sym->tag);
+           complain_at (dollar_loc, _("$%d of `%s' has no declared type"),
+                        n, rule->sym->tag);
          if (!type_name)
            type_name = "";
          obstack_fgrow3 (&obstack_for_string,
                          "]b4_rhs_value(%d, %d, [%s])[",
                          rule_length, n, type_name);
-         symbol_list_n_used_set (current_rule, n, true);
+         symbol_list_n_used_set (rule, n, true);
        }
       else
-       complain_at (loc, _("integer out of range: %s"), quote (text));
+       complain_at (dollar_loc, _("integer out of range: %s"), quote (text));
     }
 }
 
@@ -270,10 +300,10 @@ handle_action_dollar (char *text, location loc)
 `------------------------------------------------------*/
 
 static void
-handle_action_at (char *text, location loc)
+handle_action_at (symbol_list *rule, char *text, location at_loc)
 {
   char *cp = text + 1;
-  int rule_length = symbol_list_length (current_rule->next);
+  int rule_length = symbol_list_length (rule->next);
   locations_flag = true;
 
   if (*cp == '$')
@@ -291,7 +321,7 @@ handle_action_at (char *text, location loc)
                          rule_length, n);
        }
       else
-       complain_at (loc, _("integer out of range: %s"), quote (text));
+       complain_at (at_loc, _("integer out of range: %s"), quote (text));
     }
 }
 
@@ -305,7 +335,7 @@ handle_action_at (char *text, location loc)
    INITIAL), the processing is different.  */
 
 static const char *
-translate_action (int sc_context, const char *a, location l)
+translate_action (int sc_context, symbol_list *rule, const char *a, location l)
 {
   const char *res;
   static bool initialized = false;
@@ -320,29 +350,28 @@ translate_action (int sc_context, const char *a, location l)
 
   loc->start = loc->end = l.start;
   yy_switch_to_buffer (yy_scan_string (a));
-  res = code_lex (sc_context);
+  res = code_lex (sc_context, rule);
   yy_delete_buffer (YY_CURRENT_BUFFER);
 
   return res;
 }
 
 const char *
-translate_rule_action (symbol_list *r, const char *a, location l)
+translate_rule_action (symbol_list *rule, const char *a, location l)
 {
-  current_rule = r;
-  return translate_action (SC_RULE_ACTION, a, l);
+  return translate_action (SC_RULE_ACTION, rule, a, l);
 }
 
 const char *
 translate_symbol_action (const char *a, location l)
 {
-  return translate_action (SC_SYMBOL_ACTION, a, l);
+  return translate_action (SC_SYMBOL_ACTION, NULL, a, l);
 }
 
 const char *
 translate_code (const char *a, location l)
 {
-  return translate_action (INITIAL, a, l);
+  return translate_action (INITIAL, NULL, a, l);
 }
 
 /*-----------------------------------------------.