c++: use more explicit types than int

* data/c++.m4 (b4_public_types_declare): Declare token_number_type soon.
Introduce symbol_number_type (wider than token_number_type).
Clarify the requirement that kind_type from by_state and by_type
denote the _input_ type (required by the constructor), not the stored type.
Use symbol_number_type and token_number_type where appropriate, instead
of int.
* data/lalr1.cc: Adjust to these changes.
Propagate "symbol_number_type".
Invoke "type_get ()" instead of read "type" directly.
This commit is contained in:
Akim Demaille
2013-01-29 08:16:15 +01:00
parent 7fc7df7a38
commit 26f95f5f35
2 changed files with 36 additions and 32 deletions

View File

@@ -154,9 +154,15 @@ m4_define([b4_public_types_declare],
]b4_token_enums[
};
/// Token type.
/// (External) token type, as returned by yylex.
typedef token::yytokentype token_type;
/// Internal symbol number.
typedef int symbol_number_type;
/// Internal symbol number for tokens (subsumed by symbol_number_type).
typedef ]b4_int_type_for([b4_translate])[ token_number_type;
/// A complete symbol.
///
/// Expects its Base type to provide access to the symbol type
@@ -209,27 +215,26 @@ m4_define([b4_public_types_declare],
/// Copy constructor.
by_type (const by_type& other);
/// Constructor.
///
by_type (token_type t);
/// The symbol type as needed by the constructor.
typedef token_type kind_type;
/// Steal the type of \a that.
/// Constructor from (external) token numbers.
by_type (kind_type t);
/// Steal the symbol type from \a that.
void move (by_type& that);
/// The symbol type.
///
/// The (internal) type number (corresponding to \a type).
/// -1 when this symbol is empty.
int type;
/// The type (corresponding to \a type).
/// -1 when this symbol is empty.
int type_get () const;
symbol_number_type type_get () const;
/// The token.
token_type token () const;
/// The type used to store the symbol type.
typedef token_type kind_type;
/// The symbol type.
///
/// -1 when this symbol is empty.
token_number_type type;
};
/// "External" symbols: returned by the scanner.
@@ -296,7 +301,7 @@ m4_define([b4_public_types_define],
]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
{]b4_variant_if([[
// User destructor.
int yytype = this->type_get ();
symbol_number_type yytype = this->type_get ();
switch (yytype)
{
]b4_symbol_foreach([b4_symbol_destructor])dnl

View File

@@ -223,8 +223,6 @@ b4_location_define])])[
/// \param yyvalue the value to check
static bool yy_table_value_is_error_ (int yyvalue);
/// Internal symbol numbers.
typedef ]b4_int_type_for([b4_translate])[ token_number_type;
static const ]b4_int_type(b4_pact_ninf, b4_pact_ninf)[ yypact_ninf_;
static const ]b4_int_type(b4_table_ninf, b4_table_ninf)[ yytable_ninf_;
@@ -273,23 +271,24 @@ b4_location_define])])[
/// Default constructor.
by_state ();
/// The symbol type as needed by the constructor.
typedef state_type kind_type;
/// Constructor.
by_state (state_type s);
by_state (kind_type s);
/// Copy constructor.
by_state (const by_state& other);
/// Steal the symbol type from \a that.
void move (by_state& that);
/// The (internal) type number (corresponding to \a state).
/// -1 when empty.
symbol_number_type type_get () const;
/// The state.
state_type state;
/// The type (corresponding to \a state).
/// -1 when empty.
int type_get () const;
/// The type used to store the symbol type.
typedef state_type kind_type;
};
/// "Internal" symbol: element of the stack.
@@ -546,7 +545,7 @@ m4_if(b4_prefix, [yy], [],
{}
inline
int
]b4_parser_class_name[::symbol_number_type
]b4_parser_class_name[::by_state::type_get () const
{
return state == -1 ? -1 : yystos_[state];
@@ -590,7 +589,7 @@ m4_if(b4_prefix, [yy], [],
YY_SYMBOL_PRINT (yymsg, yysym);]b4_variant_if([], [
// User destructor.
int yytype = yysym.type_get ();
symbol_number_type yytype = yysym.type_get ();
switch (yytype)
{
]b4_symbol_foreach([b4_symbol_destructor])dnl
@@ -607,7 +606,7 @@ m4_if(b4_prefix, [yy], [],
{
std::ostream& yyoutput = yyo;
YYUSE (yyoutput);
int yytype = yysym.type_get ();
symbol_number_type yytype = yysym.type_get ();
yyo << (yytype < yyntokens_ ? "token" : "nterm")
<< ' ' << yytname_[yytype] << " ("]b4_locations_if([
<< yysym.location << ": "])[;
@@ -778,8 +777,8 @@ b4_dollar_popdef])[]dnl
/* If the proper action on seeing token YYLA.TYPE is to reduce or
to detect an error, take that action. */
yyn += yyla.type;
if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.type)
yyn += yyla.type_get ();
if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yyla.type_get ())
goto yydefault;
// Reduce or error.
@@ -875,7 +874,7 @@ b4_dollar_popdef])[]dnl
++yynerrs_;
error (]b4_join(b4_locations_if([yyla.location]),
[[yysyntax_error_ (yystack_[0].state,
yyempty ? yyempty_ : yyla.type)]])[);
yyempty ? yyempty_ : yyla.type_get ())]])[);
}
]b4_locations_if([[
@@ -886,7 +885,7 @@ b4_dollar_popdef])[]dnl
error, discard it. */
// Return failure if at end of input.
if (yyla.type == yyeof_)
if (yyla.type_get () == yyeof_)
YYABORT;
else if (!yyempty)
{