]> git.saurik.com Git - bison.git/blob - data/c++.m4
package: fix a reporter's name
[bison.git] / data / c++.m4
1 -*- Autoconf -*-
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002-2015 Free Software Foundation, Inc.
6
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.
11 #
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.
16 #
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/>.
19
20 # Sanity checks, before defaults installed by c.m4.
21 b4_percent_define_ifdef([[api.value.union.name]],
22 [b4_complain_at(b4_percent_define_get_loc([[api.value.union.name]]),
23 [named %union is invalid in C++])])
24
25 m4_include(b4_pkgdatadir/[c.m4])
26
27 # b4_comment(TEXT, [PREFIX])
28 # --------------------------
29 # Put TEXT in comment. Prefix all the output lines with PREFIX.
30 m4_define([b4_comment],
31 [b4_comment_([$1], [$2// ], [$2// ])])
32
33 ## -------- ##
34 ## Checks. ##
35 ## -------- ##
36
37 b4_percent_define_check_kind([api.namespace], [code], [deprecated])
38 b4_percent_define_check_kind([parser_class_name], [code], [deprecated])
39
40 ## ---------------- ##
41 ## Default values. ##
42 ## ---------------- ##
43
44 b4_percent_define_default([[parser_class_name]], [[parser]])
45
46 # Don't do that so that we remember whether we're using a user
47 # request, or the default value.
48 #
49 # b4_percent_define_default([[api.location.type]], [[location]])
50
51 b4_percent_define_default([[filename_type]], [[std::string]])
52 # Make it a warning for those who used betas of Bison 3.0.
53 b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
54
55 b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
56 b4_percent_define_default([[define_location_comparison]],
57 [m4_if(b4_percent_define_get([[filename_type]]),
58 [std::string], [[true]], [[false]])])
59
60
61
62 ## ----------- ##
63 ## Namespace. ##
64 ## ----------- ##
65
66 m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
67
68
69 # Don't permit an empty b4_namespace_ref. Any '::parser::foo' appended to it
70 # would compile as an absolute reference with 'parser' in the global namespace.
71 # b4_namespace_open would open an anonymous namespace and thus establish
72 # internal linkage. This would compile. However, it's cryptic, and internal
73 # linkage for the parser would be specified in all translation units that
74 # include the header, which is always generated. If we ever need to permit
75 # internal linkage somehow, surely we can find a cleaner approach.
76 m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
77 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
78 [[namespace reference is empty]])])
79
80 # Instead of assuming the C++ compiler will do it, Bison should reject any
81 # invalid b4_namepsace_ref that would be converted to a valid
82 # b4_namespace_open. The problem is that Bison doesn't always output
83 # b4_namespace_ref to uncommented code but should reserve the ability to do so
84 # in future releases without risking breaking any existing user grammars.
85 # Specifically, don't allow empty names as b4_namespace_open would just convert
86 # those into anonymous namespaces, and that might tempt some users.
87 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
88 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
89 [[namespace reference has consecutive "::"]])])
90 m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
91 [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
92 [[namespace reference has a trailing "::"]])])
93
94 m4_define([b4_namespace_open],
95 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
96 [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
97 [^\(.\)[ ]*::], [\1])),
98 [::], [ { namespace ])[ {]])])
99
100 m4_define([b4_namespace_close],
101 [b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
102 m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
103 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
104 [\1])),
105 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
106
107
108 # b4_token_enums
109 # --------------
110 # Output the definition of the tokens as enums.
111 m4_define([b4_token_enums],
112 [[enum yytokentype
113 {
114 ]m4_join([,
115 ],
116 b4_symbol_map([b4_token_enum]))[
117 };]dnl
118 ])
119
120
121
122
123 ## ----------------- ##
124 ## Semantic Values. ##
125 ## ----------------- ##
126
127
128
129 # b4_value_type_declare
130 # ---------------------
131 # Declare semantic_type.
132 m4_define([b4_value_type_declare],
133 [b4_value_type_setup[]dnl
134 [ /// Symbol semantic values.
135 ]m4_bmatch(b4_percent_define_get_kind([[api.value.type]]),
136 [code],
137 [[ typedef ]b4_percent_define_get([[api.value.type]])[ semantic_type;]],
138 [m4_bmatch(b4_percent_define_get([[api.value.type]]),
139 [union\|union-directive],
140 [[ union semantic_type
141 {
142 ]b4_user_union_members[
143 };]])])dnl
144 ])
145
146
147 # b4_public_types_declare
148 # -----------------------
149 # Define the public types: token, semantic value, location, and so forth.
150 # Depending on %define token_lex, may be output in the header or source file.
151 m4_define([b4_public_types_declare],
152 [[#ifndef ]b4_api_PREFIX[STYPE
153 ]b4_value_type_declare[
154 #else
155 typedef ]b4_api_PREFIX[STYPE semantic_type;
156 #endif]b4_locations_if([
157 /// Symbol locations.
158 typedef b4_percent_define_get([[api.location.type]],
159 [[location]]) location_type;])[
160
161 /// Syntax errors thrown from user actions.
162 struct syntax_error : std::runtime_error
163 {
164 syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([
165 location_type location;])[
166 };
167
168 /// Tokens.
169 struct token
170 {
171 ]b4_token_enums[
172 };
173
174 /// (External) token type, as returned by yylex.
175 typedef token::yytokentype token_type;
176
177 /// Internal symbol number.
178 typedef int symbol_number_type;
179
180 /// Internal symbol number for tokens (subsumed by symbol_number_type).
181 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
182
183 /// A complete symbol.
184 ///
185 /// Expects its Base type to provide access to the symbol type
186 /// via type_get().
187 ///
188 /// Provide access to semantic value]b4_locations_if([ and location])[.
189 template <typename Base>
190 struct basic_symbol : Base
191 {
192 /// Alias to Base.
193 typedef Base super_type;
194
195 /// Default constructor.
196 basic_symbol ();
197
198 /// Copy constructor.
199 basic_symbol (const basic_symbol& other);
200 ]b4_variant_if([[
201 /// Constructor for valueless symbols, and symbols from each type.
202 ]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
203 /// Constructor for valueless symbols.
204 basic_symbol (typename Base::kind_type t]b4_locations_if([,
205 const location_type& l])[);]])[
206
207 /// Constructor for symbols with semantic value.
208 basic_symbol (typename Base::kind_type t,
209 const semantic_type& v]b4_locations_if([,
210 const location_type& l])[);
211
212 ~basic_symbol ();
213
214 /// Destructive move, \a s is emptied into this.
215 void move (basic_symbol& s);
216
217 /// The semantic value.
218 semantic_type value;]b4_locations_if([
219
220 /// The location.
221 location_type location;])[
222
223 private:
224 /// Assignment operator.
225 basic_symbol& operator= (const basic_symbol& other);
226 };
227
228 /// Type access provider for token (enum) based symbols.
229 struct by_type
230 {
231 /// Default constructor.
232 by_type ();
233
234 /// Copy constructor.
235 by_type (const by_type& other);
236
237 /// The symbol type as needed by the constructor.
238 typedef token_type kind_type;
239
240 /// Constructor from (external) token numbers.
241 by_type (kind_type t);
242
243 /// Steal the symbol type from \a that.
244 void move (by_type& that);
245
246 /// The (internal) type number (corresponding to \a type).
247 /// -1 when this symbol is empty.
248 symbol_number_type type_get () const;
249
250 /// The token.
251 token_type token () const;
252
253 enum { empty = 0 };
254
255 /// The symbol type.
256 /// -1 when this symbol is empty.
257 token_number_type type;
258 };
259
260 /// "External" symbols: returned by the scanner.
261 typedef basic_symbol<by_type> symbol_type;
262
263 ]b4_symbol_constructor_declare])
264
265
266 # b4_public_types_define
267 # ----------------------
268 # Provide the implementation needed by the public types.
269 m4_define([b4_public_types_define],
270 [[ inline
271 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
272 : std::runtime_error (m)]b4_locations_if([
273 , location (l)])[
274 {}
275
276 // basic_symbol.
277 template <typename Base>
278 inline
279 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
280 : value ()
281 {}
282
283 template <typename Base>
284 inline
285 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
286 : Base (other)
287 , value ()]b4_locations_if([
288 , location (other.location)])[
289 {
290 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
291 [other.value])],
292 [value = other.value;])[
293 }
294
295
296 template <typename Base>
297 inline
298 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
299 [typename Base::kind_type t],
300 [const semantic_type& v],
301 b4_locations_if([const location_type& l]))[)
302 : Base (t)
303 , value (]b4_variant_if([], [v])[)]b4_locations_if([
304 , location (l)])[
305 {]b4_variant_if([[
306 (void) v;
307 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
308
309 ]b4_variant_if([[
310 // Implementation of basic_symbol constructor for each type.
311 ]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
312 /// Constructor for valueless symbols.
313 template <typename Base>
314 inline
315 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
316 [typename Base::kind_type t],
317 b4_locations_if([const location_type& l]))[)
318 : Base (t)
319 , value ()]b4_locations_if([
320 , location (l)])[
321 {}]])[
322
323 template <typename Base>
324 inline
325 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
326 {]b4_variant_if([[
327 // User destructor.
328 symbol_number_type yytype = this->type_get ();
329 switch (yytype)
330 {
331 ]b4_symbol_foreach([b4_symbol_destructor])dnl
332 [ default:
333 break;
334 }
335
336 // Type destructor.
337 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
338 }
339
340 template <typename Base>
341 inline
342 void
343 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
344 {
345 super_type::move(s);
346 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
347 [s.value])],
348 [value = s.value;])[]b4_locations_if([
349 location = s.location;])[
350 }
351
352 // by_type.
353 inline
354 ]b4_parser_class_name[::by_type::by_type ()
355 : type (empty)
356 {}
357
358 inline
359 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
360 : type (other.type)
361 {}
362
363 inline
364 ]b4_parser_class_name[::by_type::by_type (token_type t)
365 : type (yytranslate_ (t))
366 {}
367
368 inline
369 void
370 ]b4_parser_class_name[::by_type::move (by_type& that)
371 {
372 type = that.type;
373 that.type = empty;
374 }
375
376 inline
377 int
378 ]b4_parser_class_name[::by_type::type_get () const
379 {
380 return type;
381 }
382 ]b4_token_ctor_if([[
383 inline
384 ]b4_parser_class_name[::token_type
385 ]b4_parser_class_name[::by_type::token () const
386 {
387 // YYTOKNUM[NUM] -- (External) token number corresponding to the
388 // (internal) symbol number NUM (which must be that of a token). */
389 static
390 const ]b4_int_type_for([b4_toknum])[
391 yytoken_number_[] =
392 {
393 ]b4_toknum[
394 };
395 return static_cast<token_type> (yytoken_number_[type]);
396 }
397 ]])[]dnl
398 b4_symbol_constructor_define])
399
400
401 # b4_symbol_constructor_declare
402 # b4_symbol_constructor_define
403 # -----------------------------
404 # Declare/define symbol constructors for all the value types.
405 # Use at class-level. Redefined in variant.hh.
406 m4_define([b4_symbol_constructor_declare], [])
407 m4_define([b4_symbol_constructor_define], [])
408
409
410 # b4_yytranslate_define
411 # ---------------------
412 # Define yytranslate_. Sometimes used in the header file,
413 # sometimes in the cc file.
414 m4_define([b4_yytranslate_define],
415 [[ // Symbol number corresponding to token number t.
416 inline
417 ]b4_parser_class_name[::token_number_type
418 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
419 [int])[ t)
420 {
421 static
422 const token_number_type
423 translate_table[] =
424 {
425 ]b4_translate[
426 };
427 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
428 const token_number_type undef_token_ = ]b4_undef_token_number[;
429
430 if (static_cast<int>(t) <= yyeof_)
431 return yyeof_;
432 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
433 return translate_table[t];
434 else
435 return undef_token_;
436 }
437 ]])
438
439
440 # b4_lhs_value([TYPE])
441 # --------------------
442 # Expansion of $<TYPE>$.
443 m4_define([b4_lhs_value],
444 [b4_symbol_value([yyval], [$1])])
445
446
447 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
448 # --------------------------------------
449 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
450 # symbols on RHS.
451 m4_define([b4_rhs_value],
452 [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
453
454
455 # b4_lhs_location()
456 # -----------------
457 # Expansion of @$.
458 m4_define([b4_lhs_location],
459 [(yyloc)])
460
461
462 # b4_rhs_location(RULE-LENGTH, NUM)
463 # ---------------------------------
464 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
465 # on RHS.
466 m4_define([b4_rhs_location],
467 [(yylocation_stack_@{($1) - ($2)@})])
468
469
470 # b4_parse_param_decl
471 # -------------------
472 # Extra formal arguments of the constructor.
473 # Change the parameter names from "foo" into "foo_yyarg", so that
474 # there is no collision bw the user chosen attribute name, and the
475 # argument name in the constructor.
476 m4_define([b4_parse_param_decl],
477 [m4_ifset([b4_parse_param],
478 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
479
480 m4_define([b4_parse_param_decl_1],
481 [$1_yyarg])
482
483
484
485 # b4_parse_param_cons
486 # -------------------
487 # Extra initialisations of the constructor.
488 m4_define([b4_parse_param_cons],
489 [m4_ifset([b4_parse_param],
490 [
491 b4_cc_constructor_calls(b4_parse_param)])])
492 m4_define([b4_cc_constructor_calls],
493 [m4_map_sep([b4_cc_constructor_call], [,
494 ], [$@])])
495 m4_define([b4_cc_constructor_call],
496 [$2 ($2_yyarg)])
497
498 # b4_parse_param_vars
499 # -------------------
500 # Extra instance variables.
501 m4_define([b4_parse_param_vars],
502 [m4_ifset([b4_parse_param],
503 [
504 // User arguments.
505 b4_cc_var_decls(b4_parse_param)])])
506 m4_define([b4_cc_var_decls],
507 [m4_map_sep([b4_cc_var_decl], [
508 ], [$@])])
509 m4_define([b4_cc_var_decl],
510 [ $1;])
511
512
513 ## ---------##
514 ## Values. ##
515 ## ---------##
516
517 # b4_yylloc_default_define
518 # ------------------------
519 # Define YYLLOC_DEFAULT.
520 m4_define([b4_yylloc_default_define],
521 [[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
522 If N is 0, then set CURRENT to the empty location which ends
523 the previous symbol: RHS[0] (always defined). */
524
525 # ifndef YYLLOC_DEFAULT
526 # define YYLLOC_DEFAULT(Current, Rhs, N) \
527 do \
528 if (N) \
529 { \
530 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
531 (Current).end = YYRHSLOC (Rhs, N).end; \
532 } \
533 else \
534 { \
535 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
536 } \
537 while (/*CONSTCOND*/ false)
538 # endif
539 ]])
540
541 ## -------- ##
542 ## Checks. ##
543 ## -------- ##
544
545 b4_token_ctor_if([b4_variant_if([],
546 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
547 [cannot use '%s' without '%s'],
548 [%define api.token.constructor],
549 [%define api.value.type variant]))])])