X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/0623bacc05d3cdcbac1776cf33798ee1022a6e60..22b081deb7ab1d0ec7a279e5213fa7fdc2c85207:/data/c%20%20.m4/bison.git/blobdiff_plain/0623bacc05d3cdcbac1776cf33798ee1022a6e60..22b081deb7ab1d0ec7a279e5213fa7fdc2c85207:/data/c++.m4 diff --git a/data/c++.m4 b/data/c++.m4 index 9ec967ad..f1468c70 100644 --- a/data/c++.m4 +++ b/data/c++.m4 @@ -2,8 +2,7 @@ # C++ skeleton for Bison -# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 -# Free Software Foundation, Inc. +# Copyright (C) 2002-2012 Free Software Foundation, Inc. # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -24,22 +23,29 @@ m4_include(b4_pkgdatadir/[c.m4]) ## Default values. ## ## ---------------- ## -# Default parser class name. b4_percent_define_default([[parser_class_name]], [[parser]]) -b4_percent_define_default([[location_type]], [[location]]) + +# Don't do that so that we remember whether we're using a user +# request, or the default value. +# +# b4_percent_define_default([[location_type]], [[location]]) + b4_percent_define_default([[filename_type]], [[std::string]]) -b4_percent_define_default([[namespace]], m4_defn([b4_prefix])) +b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix])) + b4_percent_define_default([[global_tokens_and_yystype]], [[false]]) b4_percent_define_default([[define_location_comparison]], [m4_if(b4_percent_define_get([[filename_type]]), [std::string], [[true]], [[false]])]) + ## ----------- ## ## Namespace. ## ## ----------- ## -m4_define([b4_namespace_ref], [b4_percent_define_get([[namespace]])]) +m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])]) + # Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it # would compile as an absolute reference with `parser' in the global namespace. @@ -49,7 +55,7 @@ m4_define([b4_namespace_ref], [b4_percent_define_get([[namespace]])]) # include the header, which is always generated. If we ever need to permit # internal linkage somehow, surely we can find a cleaner approach. m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [], -[b4_complain_at(b4_percent_define_get_loc([[namespace]]), +[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), [[namespace reference is empty]])]) # Instead of assuming the C++ compiler will do it, Bison should reject any @@ -60,24 +66,24 @@ m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [], # Specifically, don't allow empty names as b4_namespace_open would just convert # those into anonymous namespaces, and that might tempt some users. m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [], -[b4_complain_at(b4_percent_define_get_loc([[namespace]]), +[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), [[namespace reference has consecutive "::"]])]) m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [], -[b4_complain_at(b4_percent_define_get_loc([[namespace]]), +[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), [[namespace reference has a trailing "::"]])]) m4_define([b4_namespace_open], -[b4_user_code([b4_percent_define_get_syncline([[namespace]]) +[b4_user_code([b4_percent_define_get_syncline([[api.namespace]]) [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref), [^\(.\)[ ]*::], [\1])), [::], [ { namespace ])[ {]])]) m4_define([b4_namespace_close], -[b4_user_code([b4_percent_define_get_syncline([[namespace]]) -m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref), - [^\(.\)[ ]*\(::\)?\([^][:]\|:[^][:]\)*], +[b4_user_code([b4_percent_define_get_syncline([[api.namespace]]) +m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]), + [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*], [\1])), - [::\([^][:]\|:[^][:]\)*], [} ])[} // ]b4_namespace_ref])]) + [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])]) # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) @@ -93,6 +99,8 @@ m4_map_sep([ b4_token_enum], [, ]) + + ## ----------------- ## ## Semantic Values. ## ## ----------------- ## @@ -122,7 +130,15 @@ m4_define([b4_public_types_declare], typedef YYSTYPE semantic_type; #endif]b4_locations_if([ /// Symbol locations. - typedef b4_percent_define_get([[location_type]]) location_type;])[ + typedef b4_percent_define_get([[location_type]], + [[location]]) location_type;])[ + + /// Syntax errors thrown from user actions. + struct syntax_error : std::runtime_error + { + syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([ + location_type location;])[ + }; /// Tokens. struct token @@ -169,21 +185,22 @@ m4_define([b4_public_types_declare], /// Default constructor. inline symbol_type (); - /// Constructor. - inline symbol_type (]b4_args([int t], + /// Constructor for tokens with semantic value. + inline symbol_type (]b4_args([token_type t], [const semantic_type& v], b4_locations_if([const location_type& l]))[); - inline symbol_type (]b4_args([int t], + /// Constructor for valueless tokens. + inline symbol_type (]b4_args([token_type t], b4_locations_if([const location_type& l]))[); /// The symbol type. int type; - /// Return the type corresponding to this state. + /// The symbol type. inline int type_get_ () const; - /// Its token. + /// The token. inline token_type token () const; }; ]b4_symbol_constructor_declare]) @@ -193,8 +210,15 @@ m4_define([b4_public_types_declare], # ---------------------- # Provide the implementation needed by the public types. m4_define([b4_public_types_define], -[[ // symbol_base_type. +[[ inline + ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m) + : std::runtime_error (m)]b4_locations_if([ + , location (l)])[ + {} + + // symbol_base_type. template + inline ]b4_parser_class_name[::symbol_base_type::symbol_base_type () : value()]b4_locations_if([ , location()])[ @@ -202,6 +226,7 @@ m4_define([b4_public_types_define], }]b4_locations_if([[ template + inline ]b4_parser_class_name[::symbol_base_type::symbol_base_type (const location_type& l) : value() , location(l) @@ -209,6 +234,7 @@ m4_define([b4_public_types_define], }]])[ template + inline ]b4_parser_class_name[::symbol_base_type::symbol_base_type (]b4_args( [const semantic_type& v], b4_locations_if([const location_type& l]))[) @@ -218,6 +244,7 @@ m4_define([b4_public_types_define], } template + inline const Exact& ]b4_parser_class_name[::symbol_base_type::self () const { @@ -225,6 +252,7 @@ m4_define([b4_public_types_define], } template + inline Exact& ]b4_parser_class_name[::symbol_base_type::self () { @@ -232,6 +260,7 @@ m4_define([b4_public_types_define], } template + inline int ]b4_parser_class_name[::symbol_base_type::type_get () const { @@ -239,35 +268,40 @@ m4_define([b4_public_types_define], } // symbol_type. + inline ]b4_parser_class_name[::symbol_type::symbol_type () : super_type () , type () { } + inline ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( - [int t], + [token_type t], b4_locations_if([const location_type& l]))[) : super_type (]b4_locations_if([l])[) - , type (t) + , type (yytranslate_ (t)) { } + inline ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( - [int t], + [token_type t], [const semantic_type& v], b4_locations_if([const location_type& l]))[) : super_type (v]b4_locations_if([, l])[) - , type (t) + , type (yytranslate_ (t)) { } + inline int ]b4_parser_class_name[::symbol_type::type_get_ () const { return type; } ]b4_lex_symbol_if([[ + inline ]b4_parser_class_name[::token_type ]b4_parser_class_name[::symbol_type::token () const { @@ -294,6 +328,34 @@ m4_define([b4_symbol_constructor_declare], []) m4_define([b4_symbol_constructor_define], []) +# b4_yytranslate_define +# --------------------- +# Define yytranslate_. Sometimes used in the header file, +# sometimes in the cc file. +m4_define([b4_yytranslate_define], +[[ // 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(t) <= yyeof_) + return yyeof_; + else if (static_cast (t) <= user_token_number_max_) + return translate_table[t]; + else + return undef_token_; + } +]]) + # b4_lhs_value([TYPE]) # -------------------- @@ -345,24 +407,24 @@ m4_define([b4_parse_param_decl_1], # Extra initialisations of the constructor. m4_define([b4_parse_param_cons], [m4_ifset([b4_parse_param], - [ + [ b4_cc_constructor_calls(b4_parse_param)])]) m4_define([b4_cc_constructor_calls], - [m4_map_sep([b4_cc_constructor_call], [, + [m4_map_sep([b4_cc_constructor_call], [, ], [$@])]) m4_define([b4_cc_constructor_call], - [$2 ($2_yyarg)]) + [$2 ($2_yyarg)]) # b4_parse_param_vars # ------------------- # Extra instance variables. m4_define([b4_parse_param_vars], [m4_ifset([b4_parse_param], - [ + [ /* User arguments. */ b4_cc_var_decls(b4_parse_param)])]) m4_define([b4_cc_var_decls], - [m4_map_sep([b4_cc_var_decl], [ + [m4_map_sep([b4_cc_var_decl], [ ], [$@])]) m4_define([b4_cc_var_decl], - [ $1;]) + [ $1;])