3 # C++ skeleton for Bison
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6 # Free Software Foundation, Inc.
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 m4_include(b4_pkgdatadir/[c.m4])
23 ## ---------------- ##
25 ## ---------------- ##
27 b4_percent_define_default([[parser_class_name]], [[parser]])
28 b4_percent_define_default([[location_type]], [[location]])
29 b4_percent_define_default([[filename_type]], [[std::string]])
30 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
32 b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
33 b4_percent_define_default([[define_location_comparison]],
34 [m4_if(b4_percent_define_get([[filename_type]]),
35 [std::string], [[true]], [[false]])])
43 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
46 # Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
47 # would compile as an absolute reference with `parser' in the global namespace.
48 # b4_namespace_open would open an anonymous namespace and thus establish
49 # internal linkage. This would compile. However, it's cryptic, and internal
50 # linkage for the parser would be specified in all translation units that
51 # include the header, which is always generated. If we ever need to permit
52 # internal linkage somehow, surely we can find a cleaner approach.
53 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
54 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
55 [[namespace reference is empty]])])
57 # Instead of assuming the C++ compiler will do it, Bison should reject any
58 # invalid b4_namepsace_ref that would be converted to a valid
59 # b4_namespace_open. The problem is that Bison doesn't always output
60 # b4_namespace_ref to uncommented code but should reserve the ability to do so
61 # in future releases without risking breaking any existing user grammars.
62 # Specifically, don't allow empty names as b4_namespace_open would just convert
63 # those into anonymous namespaces, and that might tempt some users.
64 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
65 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
66 [[namespace reference has consecutive "::"]])])
67 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
68 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
69 [[namespace reference has a trailing "::"]])])
71 m4_define([b4_namespace_open],
72 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
73 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
74 [^\(.\)[ ]*::], [\1])),
75 [::], [ { namespace ])[ {]])])
77 m4_define([b4_namespace_close],
78 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
79 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
80 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
82 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
85 # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
86 # -----------------------------------------------------
87 # Output the definition of the tokens as enums.
88 m4_define([b4_token_enums],
91 m4_map_sep([ b4_token_enum], [,
98 ## ----------------- ##
99 ## Semantic Values. ##
100 ## ----------------- ##
102 # b4_semantic_type_declare
103 # ------------------------
104 # Declare semantic_type.
105 m4_define([b4_semantic_type_declare],
106 [ /// Symbol semantic values.
108 [ union semantic_type
111 [m4_if(b4_tag_seen_flag, 0,
112 [[ typedef int semantic_type;]],
113 [[ typedef YYSTYPE semantic_type;]])])])
116 # b4_public_types_declare
117 # -----------------------
118 # Define the public types: token, semantic value, location, and so forth.
119 # Depending on %define token_lex, may be output in the header or source file.
120 m4_define([b4_public_types_declare],
122 ]b4_semantic_type_declare[
124 typedef YYSTYPE semantic_type;
125 #endif]b4_locations_if([
126 /// Symbol locations.
127 typedef b4_percent_define_get([[location_type]]) location_type;])[
129 /// Syntax errors thrown from user actions.
130 struct syntax_error : std::runtime_error
132 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
133 location_type location;])[
139 ]b4_token_enums(b4_tokens)[
143 typedef token::yytokentype token_type;
145 /// A complete symbol, with its type.
146 template <typename Exact>
147 struct symbol_base_type
149 /// Default constructor.
150 inline symbol_base_type ();
152 /// Constructor.]b4_locations_if([
153 inline symbol_base_type (const location_type& l)])[;
154 inline symbol_base_type (]b4_args(
155 [const semantic_type& v],
156 b4_locations_if([const location_type& l]))[);
158 /// Return this with its exact type.
159 const Exact& self () const;
162 /// Return the type of this symbol.
163 int type_get () const;
165 /// The semantic value.
166 semantic_type value;]b4_locations_if([
169 location_type location;])[
172 /// External form of a symbol: its type and attributes.
173 struct symbol_type : symbol_base_type<symbol_type>
175 /// The parent class.
176 typedef symbol_base_type<symbol_type> super_type;
178 /// Default constructor.
179 inline symbol_type ();
182 inline symbol_type (]b4_args([int t],
183 [const semantic_type& v],
184 b4_locations_if([const location_type& l]))[);
186 inline symbol_type (]b4_args([int t],
187 b4_locations_if([const location_type& l]))[);
192 /// Return the type corresponding to this state.
193 inline int type_get_ () const;
196 inline token_type token () const;
198 ]b4_symbol_constructor_declare])
201 # b4_public_types_define
202 # ----------------------
203 # Provide the implementation needed by the public types.
204 m4_define([b4_public_types_define],
206 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
207 : std::runtime_error (m)]b4_locations_if([
212 template <typename Exact>
214 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
215 : value()]b4_locations_if([
220 template <typename Exact>
222 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
228 template <typename Exact>
230 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
231 [const semantic_type& v],
232 b4_locations_if([const location_type& l]))[)
233 : value(v)]b4_locations_if([
238 template <typename Exact>
241 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
243 return static_cast<const Exact&>(*this);
246 template <typename Exact>
249 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
251 return static_cast<Exact&>(*this);
254 template <typename Exact>
257 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
259 return self ().type_get_ ();
264 ]b4_parser_class_name[::symbol_type::symbol_type ()
271 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
273 b4_locations_if([const location_type& l]))[)
274 : super_type (]b4_locations_if([l])[)
280 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
282 [const semantic_type& v],
283 b4_locations_if([const location_type& l]))[)
284 : super_type (v]b4_locations_if([, l])[)
291 ]b4_parser_class_name[::symbol_type::type_get_ () const
297 ]b4_parser_class_name[::token_type
298 ]b4_parser_class_name[::symbol_type::token () const
300 // YYTOKNUM[NUM] -- (External) token number corresponding to the
301 // (internal) symbol number NUM (which must be that of a token). */
303 const ]b4_int_type_for([b4_toknum])[
308 return static_cast<token_type> (yytoken_number_[type]);
311 b4_symbol_constructor_define])
314 # b4_symbol_constructor_declare
315 # b4_symbol_constructor_define
316 # -----------------------------
317 # Declare/define symbol constructors for all the value types.
318 # Use at class-level. Redefined in variant.hh.
319 m4_define([b4_symbol_constructor_declare], [])
320 m4_define([b4_symbol_constructor_define], [])
323 # b4_yytranslate_define
324 # ---------------------
325 # Define yytranslate_. Sometimes used in the header file,
326 # sometimes in the cc file.
327 m4_define([b4_yytranslate_define],
328 [[ // Symbol number corresponding to token number t.
329 ]b4_parser_class_name[::token_number_type
330 ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
334 const token_number_type
339 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
340 const token_number_type undef_token_ = ]b4_undef_token_number[;
342 if (static_cast<int>(t) <= yyeof_)
344 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
345 return translate_table[t];
352 # b4_lhs_value([TYPE])
353 # --------------------
354 # Expansion of $<TYPE>$.
355 m4_define([b4_lhs_value],
356 [b4_symbol_value([yyval], [$1])])
359 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
360 # --------------------------------------
361 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
363 m4_define([b4_rhs_value],
364 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
370 m4_define([b4_lhs_location],
374 # b4_rhs_location(RULE-LENGTH, NUM)
375 # ---------------------------------
376 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
378 m4_define([b4_rhs_location],
379 [(yylocation_stack_@{($1) - ($2)@})])
382 # b4_parse_param_decl
383 # -------------------
384 # Extra formal arguments of the constructor.
385 # Change the parameter names from "foo" into "foo_yyarg", so that
386 # there is no collision bw the user chosen attribute name, and the
387 # argument name in the constructor.
388 m4_define([b4_parse_param_decl],
389 [m4_ifset([b4_parse_param],
390 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
392 m4_define([b4_parse_param_decl_1],
397 # b4_parse_param_cons
398 # -------------------
399 # Extra initialisations of the constructor.
400 m4_define([b4_parse_param_cons],
401 [m4_ifset([b4_parse_param],
403 b4_cc_constructor_calls(b4_parse_param)])])
404 m4_define([b4_cc_constructor_calls],
405 [m4_map_sep([b4_cc_constructor_call], [,
407 m4_define([b4_cc_constructor_call],
410 # b4_parse_param_vars
411 # -------------------
412 # Extra instance variables.
413 m4_define([b4_parse_param_vars],
414 [m4_ifset([b4_parse_param],
416 /* User arguments. */
417 b4_cc_var_decls(b4_parse_param)])])
418 m4_define([b4_cc_var_decls],
419 [m4_map_sep([b4_cc_var_decl], [
421 m4_define([b4_cc_var_decl],