3 # C++ skeleton for Bison
5 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
20 m4_include(b4_pkgdatadir/[c.m4])
22 # b4_comment(TEXT, [PREFIX])
23 # --------------------------
24 # Put TEXT in comment. Prefix all the output lines with PREFIX.
25 m4_define([b4_comment],
26 [b4_comment_([$1], [$2// ], [$2// ])])
29 ## ---------------- ##
31 ## ---------------- ##
33 b4_percent_define_default([[parser_class_name]], [[parser]])
35 # Don't do that so that we remember whether we're using a user
36 # request, or the default value.
38 # b4_percent_define_default([[api.location.type]], [[location]])
40 b4_percent_define_default([[filename_type]], [[std::string]])
41 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
43 b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
44 b4_percent_define_default([[define_location_comparison]],
45 [m4_if(b4_percent_define_get([[filename_type]]),
46 [std::string], [[true]], [[false]])])
54 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
57 # Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
58 # would compile as an absolute reference with `parser' in the global namespace.
59 # b4_namespace_open would open an anonymous namespace and thus establish
60 # internal linkage. This would compile. However, it's cryptic, and internal
61 # linkage for the parser would be specified in all translation units that
62 # include the header, which is always generated. If we ever need to permit
63 # internal linkage somehow, surely we can find a cleaner approach.
64 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
65 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
66 [[namespace reference is empty]])])
68 # Instead of assuming the C++ compiler will do it, Bison should reject any
69 # invalid b4_namepsace_ref that would be converted to a valid
70 # b4_namespace_open. The problem is that Bison doesn't always output
71 # b4_namespace_ref to uncommented code but should reserve the ability to do so
72 # in future releases without risking breaking any existing user grammars.
73 # Specifically, don't allow empty names as b4_namespace_open would just convert
74 # those into anonymous namespaces, and that might tempt some users.
75 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
76 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
77 [[namespace reference has consecutive "::"]])])
78 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
79 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
80 [[namespace reference has a trailing "::"]])])
82 m4_define([b4_namespace_open],
83 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
84 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
85 [^\(.\)[ ]*::], [\1])),
86 [::], [ { namespace ])[ {]])])
88 m4_define([b4_namespace_close],
89 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
90 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
91 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
93 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
98 # Output the definition of the tokens as enums.
99 m4_define([b4_token_enums],
104 b4_symbol_map([b4_token_enum]))[
111 ## ----------------- ##
112 ## Semantic Values. ##
113 ## ----------------- ##
115 # b4_semantic_type_declare
116 # ------------------------
117 # Declare semantic_type.
118 m4_define([b4_semantic_type_declare],
119 [ /// Symbol semantic values.
121 [ union semantic_type
125 [m4_if(b4_tag_seen_flag, 0,
126 [[ typedef int semantic_type;]],
127 [[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
130 # b4_public_types_declare
131 # -----------------------
132 # Define the public types: token, semantic value, location, and so forth.
133 # Depending on %define token_lex, may be output in the header or source file.
134 m4_define([b4_public_types_declare],
135 [[#ifndef ]b4_api_PREFIX[STYPE
136 ]b4_semantic_type_declare[
138 typedef ]b4_api_PREFIX[STYPE semantic_type;
139 #endif]b4_locations_if([
140 /// Symbol locations.
141 typedef b4_percent_define_get([[api.location.type]],
142 [[location]]) location_type;])[
144 /// Syntax errors thrown from user actions.
145 struct syntax_error : std::runtime_error
147 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
148 location_type location;])[
158 typedef token::yytokentype token_type;
160 /// A complete symbol, with its type.
161 template <typename Exact>
162 struct symbol_base_type
164 /// Default constructor.
165 inline symbol_base_type ();
167 /// Constructor.]b4_locations_if([
168 inline symbol_base_type (const location_type& l);])[
169 inline symbol_base_type (]b4_join(
170 [const semantic_type& v],
171 b4_locations_if([const location_type& l]))[);
173 /// Return this with its exact type.
174 const Exact& self () const;
177 /// Return the type of this symbol.
178 int type_get () const;
180 /// The semantic value.
181 semantic_type value;]b4_locations_if([
184 location_type location;])[
187 /// External form of a symbol: its type and attributes.
188 struct symbol_type : symbol_base_type<symbol_type>
190 /// The parent class.
191 typedef symbol_base_type<symbol_type> super_type;
193 /// Default constructor.
194 inline symbol_type ();
196 /// Destructive move, \a s is emptied.
197 inline void move (symbol_type& s);
199 /// Constructor for tokens with semantic value.
200 inline symbol_type (]b4_join([token_type t],
201 [const semantic_type& v],
202 b4_locations_if([const location_type& l]))[);
204 /// Constructor for valueless tokens.
205 inline symbol_type (]b4_join([token_type t],
206 b4_locations_if([const location_type& l]))[);
212 inline int type_get_ () const;
215 inline token_type token () const;
217 ]b4_symbol_constructor_declare])
220 # b4_public_types_define
221 # ----------------------
222 # Provide the implementation needed by the public types.
223 m4_define([b4_public_types_define],
225 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
226 : std::runtime_error (m)]b4_locations_if([
231 template <typename Exact>
233 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
234 : value()]b4_locations_if([
239 template <typename Exact>
241 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
247 template <typename Exact>
249 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_join(
250 [const semantic_type& v],
251 b4_locations_if([const location_type& l]))[)
252 : value(v)]b4_locations_if([
257 template <typename Exact>
260 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
262 return static_cast<const Exact&>(*this);
265 template <typename Exact>
268 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
270 return static_cast<Exact&>(*this);
273 template <typename Exact>
276 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
278 return self ().type_get_ ();
283 ]b4_parser_class_name[::symbol_type::symbol_type ()
290 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
292 b4_locations_if([const location_type& l]))[)
293 : super_type (]b4_locations_if([l])[)
294 , type (yytranslate_ (t))
299 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_join(
301 [const semantic_type& v],
302 b4_locations_if([const location_type& l]))[)
303 : super_type (v]b4_locations_if([, l])[)
304 , type (yytranslate_ (t))
310 ]b4_parser_class_name[::symbol_type::move (symbol_type& s)
312 ]b4_variant_if([b4_symbol_variant([[s.type]], [value], [build], [s.value])],
314 type = s.type;]b4_locations_if([
315 location = s.location;])[
320 ]b4_parser_class_name[::symbol_type::type_get_ () const
326 ]b4_parser_class_name[::token_type
327 ]b4_parser_class_name[::symbol_type::token () const
329 // YYTOKNUM[NUM] -- (External) token number corresponding to the
330 // (internal) symbol number NUM (which must be that of a token). */
332 const ]b4_int_type_for([b4_toknum])[
337 return static_cast<token_type> (yytoken_number_[type]);
340 b4_symbol_constructor_define])
343 # b4_symbol_constructor_declare
344 # b4_symbol_constructor_define
345 # -----------------------------
346 # Declare/define symbol constructors for all the value types.
347 # Use at class-level. Redefined in variant.hh.
348 m4_define([b4_symbol_constructor_declare], [])
349 m4_define([b4_symbol_constructor_define], [])
352 # b4_yytranslate_define
353 # ---------------------
354 # Define yytranslate_. Sometimes used in the header file,
355 # sometimes in the cc file.
356 m4_define([b4_yytranslate_define],
357 [[ // Symbol number corresponding to token number t.
358 ]b4_parser_class_name[::token_number_type
359 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
363 const token_number_type
368 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
369 const token_number_type undef_token_ = ]b4_undef_token_number[;
371 if (static_cast<int>(t) <= yyeof_)
373 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
374 return translate_table[t];
381 # b4_lhs_value([TYPE])
382 # --------------------
383 # Expansion of $<TYPE>$.
384 m4_define([b4_lhs_value],
385 [b4_symbol_value([yyval], [$1])])
388 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
389 # --------------------------------------
390 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
392 m4_define([b4_rhs_value],
393 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
399 m4_define([b4_lhs_location],
403 # b4_rhs_location(RULE-LENGTH, NUM)
404 # ---------------------------------
405 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
407 m4_define([b4_rhs_location],
408 [(yylocation_stack_@{($1) - ($2)@})])
411 # b4_parse_param_decl
412 # -------------------
413 # Extra formal arguments of the constructor.
414 # Change the parameter names from "foo" into "foo_yyarg", so that
415 # there is no collision bw the user chosen attribute name, and the
416 # argument name in the constructor.
417 m4_define([b4_parse_param_decl],
418 [m4_ifset([b4_parse_param],
419 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
421 m4_define([b4_parse_param_decl_1],
426 # b4_parse_param_cons
427 # -------------------
428 # Extra initialisations of the constructor.
429 m4_define([b4_parse_param_cons],
430 [m4_ifset([b4_parse_param],
432 b4_cc_constructor_calls(b4_parse_param)])])
433 m4_define([b4_cc_constructor_calls],
434 [m4_map_sep([b4_cc_constructor_call], [,
436 m4_define([b4_cc_constructor_call],
439 # b4_parse_param_vars
440 # -------------------
441 # Extra instance variables.
442 m4_define([b4_parse_param_vars],
443 [m4_ifset([b4_parse_param],
446 b4_cc_var_decls(b4_parse_param)])])
447 m4_define([b4_cc_var_decls],
448 [m4_map_sep([b4_cc_var_decl], [
450 m4_define([b4_cc_var_decl],
458 # b4_yylloc_default_define
459 # ------------------------
460 # Define YYLLOC_DEFAULT.
461 m4_define([b4_yylloc_default_define],
462 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
463 If N is 0, then set CURRENT to the empty location which ends
464 the previous symbol: RHS[0] (always defined). */
466 # ifndef YYLLOC_DEFAULT
467 # define YYLLOC_DEFAULT(Current, Rhs, N) \
471 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
472 (Current).end = YYRHSLOC (Rhs, N).end; \
476 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
478 while (/*CONSTCOND*/ false)