3 # C++ skeleton for Bison
5 # Copyright (C) 2002-2013 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 ## ----------------- ##
117 # b4_value_type_declare
118 # ---------------------
119 # Declare semantic_type.
120 m4_define([b4_value_type_declare],
121 [ /// Symbol semantic values.
122 m4_ifdef([b4_union_members],
123 [ union semantic_type
125 b4_user_union_members
127 [m4_if(b4_tag_seen_flag, 0,
128 [[ typedef int semantic_type;]],
129 [[ typedef ]b4_api_PREFIX[STYPE semantic_type;]])])])
132 # b4_public_types_declare
133 # -----------------------
134 # Define the public types: token, semantic value, location, and so forth.
135 # Depending on %define token_lex, may be output in the header or source file.
136 m4_define([b4_public_types_declare],
137 [[#ifndef ]b4_api_PREFIX[STYPE
138 ]b4_value_type_declare[
140 typedef ]b4_api_PREFIX[STYPE semantic_type;
141 #endif]b4_locations_if([
142 /// Symbol locations.
143 typedef b4_percent_define_get([[api.location.type]],
144 [[location]]) location_type;])[
146 /// Syntax errors thrown from user actions.
147 struct syntax_error : std::runtime_error
149 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
150 location_type location;])[
159 /// (External) token type, as returned by yylex.
160 typedef token::yytokentype token_type;
162 /// Internal symbol number.
163 typedef int symbol_number_type;
165 /// Internal symbol number for tokens (subsumed by symbol_number_type).
166 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
168 /// A complete symbol.
170 /// Expects its Base type to provide access to the symbol type
173 /// Provide access to semantic value]b4_locations_if([ and location])[.
174 template <typename Base>
175 struct basic_symbol : Base
178 typedef Base super_type;
180 /// Default constructor.
183 /// Copy constructor.
184 basic_symbol (const basic_symbol& other);
186 /// Constructor for valueless symbols, and symbols from each type.
187 ]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
188 /// Constructor for valueless symbols.
189 basic_symbol (typename Base::kind_type t]b4_locations_if([,
190 const location_type& l])[);]])[
192 /// Constructor for symbols with semantic value.
193 basic_symbol (typename Base::kind_type t,
194 const semantic_type& v]b4_locations_if([,
195 const location_type& l])[);
199 /// Destructive move, \a s is emptied into this.
200 void move (basic_symbol& s);
202 /// The semantic value.
203 semantic_type value;]b4_locations_if([
206 location_type location;])[
209 /// Assignment operator.
210 basic_symbol& operator= (const basic_symbol& other);
213 /// Type access provider for token (enum) based symbols.
216 /// Default constructor.
219 /// Copy constructor.
220 by_type (const by_type& other);
222 /// The symbol type as needed by the constructor.
223 typedef token_type kind_type;
225 /// Constructor from (external) token numbers.
226 by_type (kind_type t);
228 /// Steal the symbol type from \a that.
229 void move (by_type& that);
231 /// The (internal) type number (corresponding to \a type).
232 /// -1 when this symbol is empty.
233 symbol_number_type type_get () const;
236 token_type token () const;
241 /// -1 when this symbol is empty.
242 token_number_type type;
245 /// "External" symbols: returned by the scanner.
246 typedef basic_symbol<by_type> symbol_type;
248 ]b4_symbol_constructor_declare])
251 # b4_public_types_define
252 # ----------------------
253 # Provide the implementation needed by the public types.
254 m4_define([b4_public_types_define],
256 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
257 : std::runtime_error (m)]b4_locations_if([
262 template <typename Base>
264 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
268 template <typename Base>
270 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
272 , value ()]b4_locations_if([
273 , location (other.location)])[
275 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
277 [value = other.value;])[
281 template <typename Base>
283 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
284 [typename Base::kind_type t],
285 [const semantic_type& v],
286 b4_locations_if([const location_type& l]))[)
288 , value (]b4_variant_if([], [v])[)]b4_locations_if([
292 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
295 // Implementation of basic_symbol constructor for each type.
296 ]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
297 /// Constructor for valueless symbols.
298 template <typename Base>
300 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
301 [typename Base::kind_type t],
302 b4_locations_if([const location_type& l]))[)
304 , value ()]b4_locations_if([
308 template <typename Base>
310 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
313 symbol_number_type yytype = this->type_get ();
316 ]b4_symbol_foreach([b4_symbol_destructor])dnl
322 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
325 template <typename Base>
328 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
331 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
333 [value = s.value;])[]b4_locations_if([
334 location = s.location;])[
339 ]b4_parser_class_name[::by_type::by_type ()
344 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
349 ]b4_parser_class_name[::by_type::by_type (token_type t)
350 : type (yytranslate_ (t))
355 ]b4_parser_class_name[::by_type::move (by_type& that)
363 ]b4_parser_class_name[::by_type::type_get () const
369 ]b4_parser_class_name[::token_type
370 ]b4_parser_class_name[::by_type::token () const
372 // YYTOKNUM[NUM] -- (External) token number corresponding to the
373 // (internal) symbol number NUM (which must be that of a token). */
375 const ]b4_int_type_for([b4_toknum])[
380 return static_cast<token_type> (yytoken_number_[type]);
383 b4_symbol_constructor_define])
386 # b4_symbol_constructor_declare
387 # b4_symbol_constructor_define
388 # -----------------------------
389 # Declare/define symbol constructors for all the value types.
390 # Use at class-level. Redefined in variant.hh.
391 m4_define([b4_symbol_constructor_declare], [])
392 m4_define([b4_symbol_constructor_define], [])
395 # b4_yytranslate_define
396 # ---------------------
397 # Define yytranslate_. Sometimes used in the header file,
398 # sometimes in the cc file.
399 m4_define([b4_yytranslate_define],
400 [[ // Symbol number corresponding to token number t.
402 ]b4_parser_class_name[::token_number_type
403 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
407 const token_number_type
412 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
413 const token_number_type undef_token_ = ]b4_undef_token_number[;
415 if (static_cast<int>(t) <= yyeof_)
417 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
418 return translate_table[t];
425 # b4_lhs_value([TYPE])
426 # --------------------
427 # Expansion of $<TYPE>$.
428 m4_define([b4_lhs_value],
429 [b4_symbol_value([yyval], [$1])])
432 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
433 # --------------------------------------
434 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
436 m4_define([b4_rhs_value],
437 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
443 m4_define([b4_lhs_location],
447 # b4_rhs_location(RULE-LENGTH, NUM)
448 # ---------------------------------
449 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
451 m4_define([b4_rhs_location],
452 [(yylocation_stack_@{($1) - ($2)@})])
455 # b4_parse_param_decl
456 # -------------------
457 # Extra formal arguments of the constructor.
458 # Change the parameter names from "foo" into "foo_yyarg", so that
459 # there is no collision bw the user chosen attribute name, and the
460 # argument name in the constructor.
461 m4_define([b4_parse_param_decl],
462 [m4_ifset([b4_parse_param],
463 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
465 m4_define([b4_parse_param_decl_1],
470 # b4_parse_param_cons
471 # -------------------
472 # Extra initialisations of the constructor.
473 m4_define([b4_parse_param_cons],
474 [m4_ifset([b4_parse_param],
476 b4_cc_constructor_calls(b4_parse_param)])])
477 m4_define([b4_cc_constructor_calls],
478 [m4_map_sep([b4_cc_constructor_call], [,
480 m4_define([b4_cc_constructor_call],
483 # b4_parse_param_vars
484 # -------------------
485 # Extra instance variables.
486 m4_define([b4_parse_param_vars],
487 [m4_ifset([b4_parse_param],
490 b4_cc_var_decls(b4_parse_param)])])
491 m4_define([b4_cc_var_decls],
492 [m4_map_sep([b4_cc_var_decl], [
494 m4_define([b4_cc_var_decl],
502 # b4_yylloc_default_define
503 # ------------------------
504 # Define YYLLOC_DEFAULT.
505 m4_define([b4_yylloc_default_define],
506 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
507 If N is 0, then set CURRENT to the empty location which ends
508 the previous symbol: RHS[0] (always defined). */
510 # ifndef YYLLOC_DEFAULT
511 # define YYLLOC_DEFAULT(Current, Rhs, N) \
515 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
516 (Current).end = YYRHSLOC (Rhs, N).end; \
520 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
522 while (/*CONSTCOND*/ false)
530 b4_token_ctor_if([b4_variant_if([],
531 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
532 [cannot use '%s' without '%s'],
533 [%define api.token.constructor],
534 [%define api.value.type variant]))])])