]> git.saurik.com Git - bison.git/blobdiff - data/lalr1.cc
Instead of using make_symbol<TOK_FOO>, generate make_FOO for each token type.
[bison.git] / data / lalr1.cc
index 10669ff49736e50036e642f4b45b0078bd8e7ef7..c3678887386728d64805d2ac7217748f958a86b2 100644 (file)
 
 m4_include(b4_pkgdatadir/[c++.m4])
 
 
 m4_include(b4_pkgdatadir/[c++.m4])
 
+# b4_subtract(LHS, RHS)
+# ---------------------
+# Evaluate LHS - RHS if they are integer literals, otherwise expand
+# to (LHS) - (RHS).
+m4_define([b4_subtract],
+[m4_bmatch([$1$2], [^[0123456789]*$],
+           [m4_eval([$1 - $2])],
+           [($1) - ($2)])])
+
 
 # b4_args(ARG1, ...)
 # _b4_args(ARG1, ...)
 
 # b4_args(ARG1, ...)
 # _b4_args(ARG1, ...)
@@ -107,7 +116,7 @@ m4_define([b4_lhs_location],
 # Return the data corresponding to the symbol #NUM, where the current
 # rule has RULE-LENGTH symbols on RHS.
 m4_define([b4_rhs_data],
 # Return the data corresponding to the symbol #NUM, where the current
 # rule has RULE-LENGTH symbols on RHS.
 m4_define([b4_rhs_data],
-          [yystack_@{($1) - ($2)@}])
+          [yystack_@{b4_subtract($@)@}])
 
 
 # b4_rhs_state(RULE-LENGTH, NUM)
 
 
 # b4_rhs_state(RULE-LENGTH, NUM)
@@ -134,12 +143,23 @@ m4_define([b4_rhs_location],
           [b4_rhs_data([$1], [$2]).location])
 
 
           [b4_rhs_data([$1], [$2]).location])
 
 
+# b4_symbol_(NUM, FIELD)
+# ----------------------
+# Recover a FIELD about symbol #NUM.  Thanks to m4_indir, fails if
+# undefined.
+m4_define([b4_symbol_],
+[m4_indir([b4_symbol($1, $2)])])
+
+
 # b4_symbol(NUM, FIELD)
 # ---------------------
 # Recover a FIELD about symbol #NUM.  Thanks to m4_indir, fails if
 # b4_symbol(NUM, FIELD)
 # ---------------------
 # Recover a FIELD about symbol #NUM.  Thanks to m4_indir, fails if
-# undefined.
+# undefined.  If FIELD = id, prepend the prefix.
 m4_define([b4_symbol],
 m4_define([b4_symbol],
-[m4_indir([b4_symbol($1, $2)])])
+[m4_case([$2],
+         [id],    [m4_do([b4_percent_define_get([token.prefix])],
+                         [b4_symbol_([$1], [id])])],
+         [b4_symbol_($@)])])
 
 
 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
 
 
 # b4_symbol_if(NUM, FIELD, IF-TRUE, IF-FALSE)
@@ -195,66 +215,40 @@ m4_define([b4_type_action_],
 ])])
 
 
 ])])
 
 
-# b4_symbol_constructor_declaration_(SYMBOL-NUMBERS)
-# ----------------------------------------------------
+# b4_symbol_constructor_declaration_(SYMBOL-NUMBER)
+# -------------------------------------------------
 # Declare the overloaded version of make_symbol for the (common) type of
 # these SYMBOL-NUMBERS.  Use at class-level.
 m4_define([b4_symbol_constructor_declaration_],
 # Declare the overloaded version of make_symbol for the (common) type of
 # these SYMBOL-NUMBERS.  Use at class-level.
 m4_define([b4_symbol_constructor_declaration_],
-[    template <token_type>
-    static inline symbol_type
-    make_symbol (b4_args(b4_symbol_if([$1], [has_type],
-                                      [const b4_symbol([$1], [type])& v]),
-                         b4_locations_if([const location_type& l])));
+[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
+[    static inline
+    symbol_type
+    make_[]b4_symbol_([$1], [id]) (dnl
+b4_args(b4_symbol_if([$1], [has_type],
+                     [const b4_symbol([$1], [type])& v]),
+        b4_locations_if([const location_type& l])));
+
+])])])
 
 
-])
 
 # b4_symbol_constructor_declarations
 # ----------------------------------
 
 # b4_symbol_constructor_declarations
 # ----------------------------------
-# Declare the overloaded versions of make_symbol for all the value types.
+# Declare symbol constructors for all the value types.
 # Use at class-level.
 m4_define([b4_symbol_constructor_declarations],
 [b4_variant_if([
 # Use at class-level.
 m4_define([b4_symbol_constructor_declarations],
 [b4_variant_if([
-    // Declaration of make_symbol for each value type.
-m4_map([b4_symbol_constructor_declaration_], m4_defn([b4_type_names]))])])
-
-
-
-# b4_symbol_constructor_specialization_(SYMBOL-NUMBER)
-# ----------------------------------------------------
-# Declare the specialization of make_symbol for this each SYMBOL-NUMBER.
-# Specializations cannot be declared at class-level, this must be done
-# at namespace-level.
-m4_define([b4_symbol_constructor_specialization_],
-[b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
-[  template <>
-  inline
-  b4_parser_class_name::symbol_type
-  b4_parser_class_name::make_symbol <b4_parser_class_name::token::b4_symbol([$1], [id])> (dnl
-b4_args(b4_symbol_if([$1], [has_type],
-                     [const b4_symbol([$1], [type])& v]),
-        b4_locations_if([const b4_parser_class_name::location_type& l])));
-])])])
-
-# b4_symbol_constructor_specializations
-# -------------------------------------
-# Declare specializations of make_symbol.
-m4_define([b4_symbol_constructor_specializations],
-[b4_variant_if([
-  // Specializations of make_symbol for each symbol type.
-m4_map([b4_symbol_constructor_specialization_],
-       m4_defn([b4_symbol_numbers]))])dnl
-])
+    // Symbol constructors declarations.
+m4_map([b4_symbol_constructor_declaration_], m4_defn([b4_symbol_numbers]))])])
 
 
 
 # b4_symbol_constructor_definition_(SYMBOL-NUMBER)
 # ------------------------------------------------
 
 
 
 # b4_symbol_constructor_definition_(SYMBOL-NUMBER)
 # ------------------------------------------------
-# Define make_symbol for this SYMBOL-NUMBER.
+# Define symbol constructor for this SYMBOL-NUMBER.
 m4_define([b4_symbol_constructor_definition_],
 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
 m4_define([b4_symbol_constructor_definition_],
 [b4_symbol_if([$1], [is_token], [b4_symbol_if([$1], [has_id],
-[  template <>
-  b4_parser_class_name::symbol_type
-  b4_parser_class_name::make_symbol <b4_parser_class_name::token::b4_symbol([$1], [id])> (dnl
+[  b4_parser_class_name::symbol_type
+  b4_parser_class_name::make_[]b4_symbol_([$1], [id]) (dnl
 b4_args(b4_symbol_if([$1], [has_type],
                      [const b4_symbol([$1], [type])& v]),
         b4_locations_if([const location_type& l])))
 b4_args(b4_symbol_if([$1], [has_type],
                      [const b4_symbol([$1], [type])& v]),
         b4_locations_if([const location_type& l])))
@@ -267,11 +261,101 @@ b4_args(b4_symbol_if([$1], [has_type],
 ])])])
 
 
 ])])])
 
 
-# b4_symbol_constructor_declarations
+# b4_symbol_constructor_definitions
 # ----------------------------------
 # Define the overloaded versions of make_symbol for all the value types.
 m4_define([b4_symbol_constructor_definitions],
 # ----------------------------------
 # Define the overloaded versions of make_symbol for all the value types.
 m4_define([b4_symbol_constructor_definitions],
-[b4_variant_if(
+[[  // symbol_base_type.
+  template <typename Exact>
+  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
+    : value()]b4_locations_if([
+    , location()])[
+  {
+  }]b4_locations_if([[
+
+  template <typename Exact>
+  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
+    : value()
+    , location(l)
+  {
+  }]])[
+
+  template <typename Exact>
+  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
+          [const semantic_type& v],
+          b4_locations_if([const location_type& l]))[)
+    : value(v)]b4_locations_if([
+    , location(l)])[
+  {
+  }
+
+  template <typename Exact>
+  const Exact&
+  ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
+  {
+    return static_cast<const Exact&>(*this);
+  }
+
+  template <typename Exact>
+  Exact&
+  ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
+  {
+    return static_cast<Exact&>(*this);
+  }
+
+  template <typename Exact>
+  int
+  ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
+  {
+    return self ().type_get_ ();
+  }
+
+  // symbol_type.
+  ]b4_parser_class_name[::symbol_type::symbol_type ()
+    : super_type ()
+    , type ()
+  {
+  }
+
+  ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
+                [int t],
+                b4_locations_if([const location_type& l]))[)
+    : super_type (]b4_locations_if([l])[)
+    , type (t)
+  {
+  }
+
+  ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
+                 [int t],
+                 [const semantic_type& v],
+                 b4_locations_if([const location_type& l]))[)
+    : super_type (v]b4_locations_if([, l])[)
+    , type (t)
+  {
+  }
+
+  int
+  ]b4_parser_class_name[::symbol_type::type_get_ () const
+  {
+    return type;
+  }
+]b4_lex_symbol_if([[
+  ]b4_parser_class_name[::token_type
+  ]b4_parser_class_name[::symbol_type::token () const
+  {
+    // YYTOKNUM[NUM] -- (External) token number corresponding to the
+    // (internal) symbol number NUM (which must be that of a token).  */
+    static
+    const ]b4_int_type_for([b4_toknum])[
+    yytoken_number_[] =
+    {
+  ]b4_toknum[
+    };
+    return static_cast<token_type> (yytoken_number_[type]);
+  }
+]])[
+
+]b4_variant_if(
 [  // Implementation of make_symbol for each symbol type.
 m4_map([b4_symbol_constructor_definition_], m4_defn([b4_symbol_numbers]))])])
 
 [  // Implementation of make_symbol for each symbol type.
 m4_map([b4_symbol_constructor_definition_], m4_defn([b4_symbol_numbers]))])])
 
@@ -330,6 +414,35 @@ m4_map([b4_char_sizeof_], [$@])dnl
 ])])
 
 
 ])])
 
 
+# b4_yytranslate_definition
+# -------------------------
+# Define yytranslate_.  Sometimes we want it in the header file,
+# sometimes the cc file suffices.
+m4_define([b4_yytranslate_definition],
+[[  // Symbol number corresponding to token number t.
+  ]b4_parser_class_name[::token_number_type
+  ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
+                                                          [int])[ t)
+  {
+    static
+    const token_number_type
+    translate_table[] =
+    {
+]b4_translate[
+    };
+    const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
+    const token_number_type undef_token_ = ]b4_undef_token_number[;
+
+    if (static_cast<int>(t) <= yyeof_)
+      return yyeof_;
+    else if (static_cast<unsigned int> (t) <= user_token_number_max_)
+      return translate_table[t];
+    else
+      return undef_token_;
+  }
+]])
+
+
 m4_pushdef([b4_copyright_years],
            [2002, 2003, 2004, 2005, 2006, 2007, 2008])
 
 m4_pushdef([b4_copyright_years],
            [2002, 2003, 2004, 2005, 2006, 2007, 2008])
 
@@ -622,8 +735,6 @@ m4_ifdef([b4_stype],
 #if YYDEBUG
     /// For each rule, its source line number.
     static const ]b4_int_type_for([b4_rline])[ yyrline_[];
 #if YYDEBUG
     /// For each rule, its source line number.
     static const ]b4_int_type_for([b4_rline])[ yyrline_[];
-    /// For each scanner token number, its symbol number.
-    static const ]b4_int_type_for([b4_toknum])[ yytoken_number_[];
     /// Report on the debug stream that the rule \a r is going to be reduced.
     virtual void yy_reduce_print_ (int r);
     /// Print the state stack on the debug stream.
     /// Report on the debug stream that the rule \a r is going to be reduced.
     virtual void yy_reduce_print_ (int r);
     /// Print the state stack on the debug stream.
@@ -635,7 +746,7 @@ m4_ifdef([b4_stype],
 #endif
 
     /// Convert a scanner token number \a t to a symbol number.
 #endif
 
     /// Convert a scanner token number \a t to a symbol number.
-    static inline token_number_type yytranslate_ (int t);
+    static inline token_number_type yytranslate_ (]b4_lex_symbol_if([token_type], [int])[ t);
 
     /// A complete symbol, with its type.
     template <typename Exact>
 
     /// A complete symbol, with its type.
     template <typename Exact>
@@ -704,6 +815,9 @@ m4_ifdef([b4_stype],
 
       /// Return the type corresponding to this state.
       inline int type_get_ () const;
 
       /// Return the type corresponding to this state.
       inline int type_get_ () const;
+
+      /// Its token.
+      inline token_type token () const;
     };
 
 ]b4_symbol_constructor_declarations[
     };
 
 ]b4_symbol_constructor_declarations[
@@ -769,7 +883,9 @@ m4_ifdef([b4_stype],
 
 ]b4_parse_param_vars[
   };
 
 ]b4_parse_param_vars[
   };
-]b4_symbol_constructor_specializations[
+
+]b4_lex_symbol_if([b4_yytranslate_definition])[
+]b4_lex_symbol_if([b4_symbol_constructor_definitions])[
 ]b4_namespace_close[
 
 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
 ]b4_namespace_close[
 
 ]b4_percent_define_flag_if([[global_tokens_and_yystype]],
@@ -919,82 +1035,7 @@ b4_percent_code_get[]dnl
   | Symbol types.  |
   `---------------*/
 
   | Symbol types.  |
   `---------------*/
 
-  // symbol_base_type.
-  template <typename Exact>
-  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
-    : value()]b4_locations_if([
-    , location()])[
-  {
-  }]b4_locations_if([[
-
-  template <typename Exact>
-  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
-    : value()
-    , location(l)
-  {
-  }]])[
-
-  template <typename Exact>
-  ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
-          [const semantic_type& v],
-          b4_locations_if([const location_type& l]))[)
-    : value(v)]b4_locations_if([
-    , location(l)])[
-  {
-  }
-
-  template <typename Exact>
-  const Exact&
-  ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
-  {
-    return static_cast<const Exact&>(*this);
-  }
-
-  template <typename Exact>
-  Exact&
-  ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
-  {
-    return static_cast<Exact&>(*this);
-  }
-
-  template <typename Exact>
-  int
-  ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
-  {
-    return self ().type_get_ ();
-  }
-
-  // symbol_type.
-  ]b4_parser_class_name[::symbol_type::symbol_type ()
-    : super_type ()
-    , type ()
-  {
-  }
-
-  ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
-                [int t],
-                b4_locations_if([const location_type& l]))[)
-    : super_type (]b4_locations_if([l])[)
-    , type (t)
-  {
-  }
-
-  ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
-                 [int t],
-                 [const semantic_type& v],
-                 b4_locations_if([const location_type& l]))[)
-    : super_type (v]b4_locations_if([, l])[)
-    , type (t)
-  {
-  }
-
-  int
-  ]b4_parser_class_name[::symbol_type::type_get_ () const
-  {
-    return type;
-  }
-
-]b4_symbol_constructor_definitions[
+]b4_lex_symbol_if([], [b4_symbol_constructor_definitions])[
 
   // stack_symbol_type.
   ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type ()
 
   // stack_symbol_type.
   ]b4_parser_class_name[::stack_symbol_type::stack_symbol_type ()
@@ -1197,7 +1238,8 @@ m4_popdef([b4_at_dollar])])dnl
       {
         YYCDEBUG << "Reading a token: ";
 ]b4_lex_symbol_if(
       {
         YYCDEBUG << "Reading a token: ";
 ]b4_lex_symbol_if(
-[        yyla = yylex();],
+[        yyla = b4_c_function_call([yylex], [symbol_type],
+                                   m4_ifdef([b4_lex_param], b4_lex_param));],
 [        yyla.type = yytranslate_ (b4_c_function_call([yylex], [int],
                                     [[YYSTYPE*], [&yyla.value]][]dnl
 b4_locations_if([, [[location*], [&yyla.location]]])dnl
 [        yyla.type = yytranslate_ (b4_c_function_call([yylex], [int],
                                     [[YYSTYPE*], [&yyla.value]][]dnl
 b4_locations_if([, [[location*], [&yyla.location]]])dnl
@@ -1529,12 +1571,6 @@ b4_error_verbose_if([int yystate, int yytoken],
      symbol of state STATE-NUM.  */
   ]b4_table_define([stos], [b4_stos])[;
 
      symbol of state STATE-NUM.  */
   ]b4_table_define([stos], [b4_stos])[;
 
-#if YYDEBUG
-  /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
-     to YYLEX-NUM.  */
-  ]b4_table_define([token_number], [b4_toknum])[;
-#endif
-
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
   ]b4_table_define([r1], [b4_r1])[;
 
   /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
   ]b4_table_define([r1], [b4_r1])[;
 
@@ -1584,27 +1620,7 @@ b4_error_verbose_if([int yystate, int yytoken],
   }
 #endif // YYDEBUG
 
   }
 #endif // YYDEBUG
 
-  /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX.  */
-  ]b4_parser_class_name[::token_number_type
-  ]b4_parser_class_name[::yytranslate_ (int t)
-  {
-    static
-    const token_number_type
-    translate_table[] =
-    {
-]b4_translate[
-    };
-    const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
-    const token_number_type undef_token_ = ]b4_undef_token_number[;
-
-    if (t <= yyeof_)
-      return yyeof_;
-    else if (static_cast<unsigned int> (t) <= user_token_number_max_)
-      return translate_table[t];
-    else
-      return undef_token_;
-  }
-
+]b4_lex_symbol_if([], [b4_yytranslate_definition])[
 ]b4_namespace_close[
 
 ]b4_epilogue[]dnl
 ]b4_namespace_close[
 
 ]b4_epilogue[]dnl