1 /* Bison Action Scanner                             -*- C -*-
 
   3    Copyright (C) 2006 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 2 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, write to the Free Software
 
  19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
  23 %option debug nodefault nounput noyywrap never-interactive
 
  24 %option prefix="code_" outfile="lex.yy.c"
 
  30 /* Work around a bug in flex 2.5.31.  See Debian bug 333231
 
  31    <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
 
  35 #define FLEX_PREFIX(Id) code_ ## Id
 
  37 #include "flex-scanner.h"
 
  41 #include <get-errno.h>
 
  44 #include "scan-code.h"
 
  46 /* The current calling start condition: SC_RULE_ACTION or
 
  48 # define YY_DECL const char *code_lex (int sc_context, symbol_list *rule)
 
  51 #define YY_USER_ACTION  location_compute (loc, &loc->end, yytext, yyleng);
 
  53 static void handle_action_dollar (symbol_list *rule, char *cp,
 
  55 static void handle_action_at (symbol_list *rule, char *cp, location at_loc);
 
  56 static location the_location;
 
  57 static location *loc = &the_location;
 
  59  /* C and C++ comments in code. */
 
  60 %x SC_COMMENT SC_LINE_COMMENT
 
  61  /* Strings and characters in code. */
 
  62 %x SC_STRING SC_CHARACTER
 
  63  /* Whether in a rule or symbol action.  Specifies the translation
 
  65 %x SC_RULE_ACTION SC_SYMBOL_ACTION
 
  68 /* POSIX says that a tag must be both an id and a C union member, but
 
  69    historically almost any character is allowed in a tag.  We disallow
 
  70    NUL and newline, as this simplifies our implementation.  */
 
  73 /* Zero or more instances of backslash-newline.  Following GCC, allow
 
  74    white space between the backslash and the newline.  */
 
  75 splice   (\\[ \f\t\v]*\n)*
 
  80   /* Nesting level of the current code in braces.  */
 
  81   int braces_level IF_LINT (= 0);
 
  83   /* This scanner is special: it is invoked only once, henceforth
 
  84      is expected to return only once.  This initialization is
 
  85      therefore done once per action to translate. */
 
  86   assert (sc_context == SC_SYMBOL_ACTION
 
  87           || sc_context == SC_RULE_ACTION
 
  88           || sc_context == INITIAL);
 
  92   /*------------------------------------------------------------.
 
  93   | Scanning a C comment.  The initial `/ *' is already eaten.  |
 
  94   `------------------------------------------------------------*/
 
  98   "*"{splice}"/"  STRING_GROW; BEGIN sc_context;
 
 102   /*--------------------------------------------------------------.
 
 103   | Scanning a line comment.  The initial `//' is already eaten.  |
 
 104   `--------------------------------------------------------------*/
 
 108   "\n"           STRING_GROW; BEGIN sc_context;
 
 109   {splice}       STRING_GROW;
 
 113   /*--------------------------------------------.
 
 114   | Scanning user-code characters and strings.  |
 
 115   `--------------------------------------------*/
 
 117 <SC_CHARACTER,SC_STRING>
 
 119   {splice}|\\{splice}.  STRING_GROW;
 
 124   "'"           STRING_GROW; BEGIN sc_context;
 
 129   "\""          STRING_GROW; BEGIN sc_context;
 
 133 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
 
 148     BEGIN SC_LINE_COMMENT;
 
 154   "$"("<"{tag}">")?(-?[0-9]+|"$")   handle_action_dollar (rule, yytext, *loc);
 
 155   "@"(-?[0-9]+|"$")                 handle_action_at (rule, yytext, *loc);
 
 158     warn_at (*loc, _("stray `$'"));
 
 159     obstack_sgrow (&obstack_for_string, "$][");
 
 162     warn_at (*loc, _("stray `@'"));
 
 163     obstack_sgrow (&obstack_for_string, "@@");
 
 166   "{"  STRING_GROW; ++braces_level;
 
 168     bool outer_brace = --braces_level < 0;
 
 170     /* As an undocumented Bison extension, append `;' before the last
 
 171        brace in braced code, so that the user code can omit trailing
 
 172        `;'.  But do not append `;' if emulating Yacc, since Yacc does
 
 175        FIXME: Bison should warn if a semicolon seems to be necessary
 
 176        here, and should omit the semicolon if it seems unnecessary
 
 177        (e.g., after ';', '{', or '}', each followed by comments or
 
 178        white space).  Such a warning shouldn't depend on --yacc; it
 
 179        should depend on a new --pedantic option, which would cause
 
 180        Bison to warn if it detects an extension to POSIX.  --pedantic
 
 181        should also diagnose other Bison extensions like %yacc.
 
 182        Perhaps there should also be a GCC-style --pedantic-errors
 
 183        option, so that such warnings are diagnosed as errors.  */
 
 184     if (outer_brace && ! yacc_flag)
 
 185       obstack_1grow (&obstack_for_string, ';');
 
 193   "$$"   obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
 
 194   "@$"   obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
 
 198   /*-----------------------------------------.
 
 199   | Escape M4 quoting characters in C code.  |
 
 200   `-----------------------------------------*/
 
 204   \$    obstack_sgrow (&obstack_for_string, "$][");
 
 205   \@    obstack_sgrow (&obstack_for_string, "@@");
 
 206   \[    obstack_sgrow (&obstack_for_string, "@{");
 
 207   \]    obstack_sgrow (&obstack_for_string, "@}");
 
 210   /*-----------------------------------------------------.
 
 211   | By default, grow the string obstack with the input.  |
 
 212   `-----------------------------------------------------*/
 
 216  /* End of processing. */
 
 218                    obstack_1grow (&obstack_for_string, '\0');
 
 219                    return obstack_finish (&obstack_for_string);
 
 224 /* Keeps track of the maximum number of semantic values to the left of
 
 225    a handle (those referenced by $0, $-1, etc.) are required by the
 
 226    semantic actions of this grammar. */
 
 227 int max_left_semantic_context = 0;
 
 230 /*------------------------------------------------------------------.
 
 231 | TEXT is pointing to a wannabee semantic value (i.e., a `$').      |
 
 233 | Possible inputs: $[<TYPENAME>]($|integer)                         |
 
 235 | Output to OBSTACK_FOR_STRING a reference to this semantic value.  |
 
 236 `------------------------------------------------------------------*/
 
 239 handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
 
 241   const char *type_name = NULL;
 
 243   int rule_length = symbol_list_length (rule->next);
 
 245   /* Get the type name if explicit. */
 
 258         type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0);
 
 259       if (!type_name && typed)
 
 260         complain_at (dollar_loc, _("$$ of `%s' has no declared type"),
 
 264       obstack_fgrow1 (&obstack_for_string,
 
 265                       "]b4_lhs_value([%s])[", type_name);
 
 272       num = strtol (cp, 0, 10);
 
 273       if (INT_MIN <= num && num <= rule_length && ! get_errno ())
 
 276           if (1-n > max_left_semantic_context)
 
 277             max_left_semantic_context = 1-n;
 
 278           if (!type_name && n > 0)
 
 280               symbol_list_n_type_name_get (rule, dollar_loc, n);
 
 281           if (!type_name && typed)
 
 282             complain_at (dollar_loc, _("$%d of `%s' has no declared type"),
 
 286           obstack_fgrow3 (&obstack_for_string,
 
 287                           "]b4_rhs_value(%d, %d, [%s])[",
 
 288                           rule_length, n, type_name);
 
 289           symbol_list_n_used_set (rule, n, true);
 
 292         complain_at (dollar_loc, _("integer out of range: %s"), quote (text));
 
 297 /*------------------------------------------------------.
 
 298 | TEXT is a location token (i.e., a `@...').  Output to |
 
 299 | OBSTACK_FOR_STRING a reference to this location.      |
 
 300 `------------------------------------------------------*/
 
 303 handle_action_at (symbol_list *rule, char *text, location at_loc)
 
 306   int rule_length = symbol_list_length (rule->next);
 
 307   locations_flag = true;
 
 310     obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
 
 315       num = strtol (cp, 0, 10);
 
 317       if (INT_MIN <= num && num <= rule_length && ! get_errno ())
 
 320           obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
 
 324         complain_at (at_loc, _("integer out of range: %s"), quote (text));
 
 329 /*-------------------------.
 
 330 | Initialize the scanner.  |
 
 331 `-------------------------*/
 
 333 /* Translate the dollars and ats in \a a, whose location is l.
 
 334    Depending on the \a sc_context (SC_RULE_ACTION, SC_SYMBOL_ACTION,
 
 335    INITIAL), the processing is different.  */
 
 338 translate_action (int sc_context, symbol_list *rule, const char *a, location l)
 
 341   static bool initialized = false;
 
 344       obstack_init (&obstack_for_string);
 
 345       /* The initial buffer, never used. */
 
 346       yy_delete_buffer (YY_CURRENT_BUFFER);
 
 351   loc->start = loc->end = l.start;
 
 352   yy_switch_to_buffer (yy_scan_string (a));
 
 353   res = code_lex (sc_context, rule);
 
 354   yy_delete_buffer (YY_CURRENT_BUFFER);
 
 360 translate_rule_action (symbol_list *rule, const char *a, location l)
 
 362   return translate_action (SC_RULE_ACTION, rule, a, l);
 
 366 translate_symbol_action (const char *a, location l)
 
 368   return translate_action (SC_SYMBOL_ACTION, NULL, a, l);
 
 372 translate_code (const char *a, location l)
 
 374   return translate_action (INITIAL, NULL, a, l);
 
 377 /*-----------------------------------------------.
 
 378 | Free all the memory allocated to the scanner.  |
 
 379 `-----------------------------------------------*/
 
 382 code_scanner_free (void)
 
 384   obstack_free (&obstack_for_string, 0);
 
 385   /* Reclaim Flex's buffers.  */
 
 386   yy_delete_buffer (YY_CURRENT_BUFFER);