]> git.saurik.com Git - bison.git/blame_incremental - data/c++.m4
gnulib: update
[bison.git] / data / c++.m4
... / ...
CommitLineData
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.
21b4_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
25m4_include(b4_pkgdatadir/[c.m4])
26
27# b4_comment(TEXT, [PREFIX])
28# --------------------------
29# Put TEXT in comment. Prefix all the output lines with PREFIX.
30m4_define([b4_comment],
31[b4_comment_([$1], [$2// ], [$2// ])])
32
33## -------- ##
34## Checks. ##
35## -------- ##
36
37b4_percent_define_check_kind([api.namespace], [code], [deprecated])
38b4_percent_define_check_kind([parser_class_name], [code], [deprecated])
39
40## ---------------- ##
41## Default values. ##
42## ---------------- ##
43
44b4_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
51b4_percent_define_default([[filename_type]], [[std::string]])
52# Make it a warning for those who used betas of Bison 3.0.
53b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix]))
54
55b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
56b4_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
66m4_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.
76m4_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.
87m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
88[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
89 [[namespace reference has consecutive "::"]])])
90m4_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
94m4_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
100m4_define([b4_namespace_close],
101[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
102m4_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.
111m4_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.
132m4_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.
151m4_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 /// Symbol type: an internal symbol number.
178 typedef int symbol_number_type;
179
180 /// The symbol type number to denote an empty symbol.
181 enum { empty_symbol = -2 };
182
183 /// Internal symbol number for tokens (subsumed by symbol_number_type).
184 typedef ]b4_int_type_for([b4_translate])[ token_number_type;
185
186 /// A complete symbol.
187 ///
188 /// Expects its Base type to provide access to the symbol type
189 /// via type_get().
190 ///
191 /// Provide access to semantic value]b4_locations_if([ and location])[.
192 template <typename Base>
193 struct basic_symbol : Base
194 {
195 /// Alias to Base.
196 typedef Base super_type;
197
198 /// Default constructor.
199 basic_symbol ();
200
201 /// Copy constructor.
202 basic_symbol (const basic_symbol& other);
203]b4_variant_if([[
204 /// Constructor for valueless symbols, and symbols from each type.
205]b4_type_foreach([b4_basic_symbol_constructor_declare])], [[
206 /// Constructor for valueless symbols.
207 basic_symbol (typename Base::kind_type t]b4_locations_if([,
208 const location_type& l])[);]])[
209
210 /// Constructor for symbols with semantic value.
211 basic_symbol (typename Base::kind_type t,
212 const semantic_type& v]b4_locations_if([,
213 const location_type& l])[);
214
215 /// Destroy the symbol.
216 ~basic_symbol ();
217
218 /// Destroy contents, and record that is empty.
219 void clear ();
220
221 /// Whether empty.
222 bool empty () const;
223
224 /// Destructive move, \a s is emptied into this.
225 void move (basic_symbol& s);
226
227 /// The semantic value.
228 semantic_type value;]b4_locations_if([
229
230 /// The location.
231 location_type location;])[
232
233 private:
234 /// Assignment operator.
235 basic_symbol& operator= (const basic_symbol& other);
236 };
237
238 /// Type access provider for token (enum) based symbols.
239 struct by_type
240 {
241 /// Default constructor.
242 by_type ();
243
244 /// Copy constructor.
245 by_type (const by_type& other);
246
247 /// The symbol type as needed by the constructor.
248 typedef token_type kind_type;
249
250 /// Constructor from (external) token numbers.
251 by_type (kind_type t);
252
253 /// Record that this symbol is empty.
254 void clear ();
255
256 /// Steal the symbol type from \a that.
257 void move (by_type& that);
258
259 /// The (internal) type number (corresponding to \a type).
260 /// \a empty when empty.
261 symbol_number_type type_get () const;
262
263 /// The token.
264 token_type token () const;
265
266 /// The symbol type.
267 /// \a empty_symbol when empty.
268 /// An int, not token_number_type, to be able to store empty_symbol.
269 int type;
270 };
271
272 /// "External" symbols: returned by the scanner.
273 typedef basic_symbol<by_type> symbol_type;
274
275]b4_symbol_constructor_declare])
276
277
278# b4_public_types_define
279# ----------------------
280# Provide the implementation needed by the public types.
281m4_define([b4_public_types_define],
282[[ inline
283 ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m)
284 : std::runtime_error (m)]b4_locations_if([
285 , location (l)])[
286 {}
287
288 // basic_symbol.
289 template <typename Base>
290 inline
291 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol ()
292 : value ()
293 {}
294
295 template <typename Base>
296 inline
297 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (const basic_symbol& other)
298 : Base (other)
299 , value ()]b4_locations_if([
300 , location (other.location)])[
301 {
302 ]b4_variant_if([b4_symbol_variant([other.type_get ()], [value], [copy],
303 [other.value])],
304 [value = other.value;])[
305 }
306
307 template <typename Base>
308 inline
309 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
310 [typename Base::kind_type t],
311 [const semantic_type& v],
312 b4_locations_if([const location_type& l]))[)
313 : Base (t)
314 , value (]b4_variant_if([], [v])[)]b4_locations_if([
315 , location (l)])[
316 {]b4_variant_if([[
317 (void) v;
318 ]b4_symbol_variant([this->type_get ()], [value], [copy], [v])])[}
319
320]b4_variant_if([[
321 // Implementation of basic_symbol constructor for each type.
322]b4_type_foreach([b4_basic_symbol_constructor_define])], [[
323 /// Constructor for valueless symbols.
324 template <typename Base>
325 inline
326 ]b4_parser_class_name[::basic_symbol<Base>::basic_symbol (]b4_join(
327 [typename Base::kind_type t],
328 b4_locations_if([const location_type& l]))[)
329 : Base (t)
330 , value ()]b4_locations_if([
331 , location (l)])[
332 {}]])[
333
334 template <typename Base>
335 inline
336 ]b4_parser_class_name[::basic_symbol<Base>::~basic_symbol ()
337 {
338 clear ();
339 }
340
341 template <typename Base>
342 inline
343 void
344 ]b4_parser_class_name[::basic_symbol<Base>::clear ()
345 {]b4_variant_if([[
346 // User destructor.
347 symbol_number_type yytype = this->type_get ();
348 basic_symbol<Base>& yysym = *this;
349 (void) yysym;
350 switch (yytype)
351 {
352]b4_symbol_foreach([b4_symbol_destructor])dnl
353[ default:
354 break;
355 }
356
357 // Type destructor.
358 ]b4_symbol_variant([[yytype]], [[value]], [[template destroy]])])[
359 Base::clear ();
360 }
361
362 template <typename Base>
363 inline
364 bool
365 ]b4_parser_class_name[::basic_symbol<Base>::empty () const
366 {
367 return Base::type_get () == empty_symbol;
368 }
369
370 template <typename Base>
371 inline
372 void
373 ]b4_parser_class_name[::basic_symbol<Base>::move (basic_symbol& s)
374 {
375 super_type::move(s);
376 ]b4_variant_if([b4_symbol_variant([this->type_get ()], [value], [move],
377 [s.value])],
378 [value = s.value;])[]b4_locations_if([
379 location = s.location;])[
380 }
381
382 // by_type.
383 inline
384 ]b4_parser_class_name[::by_type::by_type ()
385 : type (empty_symbol)
386 {}
387
388 inline
389 ]b4_parser_class_name[::by_type::by_type (const by_type& other)
390 : type (other.type)
391 {}
392
393 inline
394 ]b4_parser_class_name[::by_type::by_type (token_type t)
395 : type (yytranslate_ (t))
396 {}
397
398 inline
399 void
400 ]b4_parser_class_name[::by_type::clear ()
401 {
402 type = empty_symbol;
403 }
404
405 inline
406 void
407 ]b4_parser_class_name[::by_type::move (by_type& that)
408 {
409 type = that.type;
410 that.clear ();
411 }
412
413 inline
414 int
415 ]b4_parser_class_name[::by_type::type_get () const
416 {
417 return type;
418 }
419]b4_token_ctor_if([[
420 inline
421 ]b4_parser_class_name[::token_type
422 ]b4_parser_class_name[::by_type::token () const
423 {
424 // YYTOKNUM[NUM] -- (External) token number corresponding to the
425 // (internal) symbol number NUM (which must be that of a token). */
426 static
427 const ]b4_int_type_for([b4_toknum])[
428 yytoken_number_[] =
429 {
430 ]b4_toknum[
431 };
432 return static_cast<token_type> (yytoken_number_[type]);
433 }
434]])[]dnl
435b4_symbol_constructor_define])
436
437
438# b4_symbol_constructor_declare
439# b4_symbol_constructor_define
440# -----------------------------
441# Declare/define symbol constructors for all the value types.
442# Use at class-level. Redefined in variant.hh.
443m4_define([b4_symbol_constructor_declare], [])
444m4_define([b4_symbol_constructor_define], [])
445
446
447# b4_yytranslate_define
448# ---------------------
449# Define yytranslate_. Sometimes used in the header file,
450# sometimes in the cc file.
451m4_define([b4_yytranslate_define],
452[[ // Symbol number corresponding to token number t.
453 inline
454 ]b4_parser_class_name[::token_number_type
455 ]b4_parser_class_name[::yytranslate_ (]b4_token_ctor_if([token_type],
456 [int])[ t)
457 {
458 static
459 const token_number_type
460 translate_table[] =
461 {
462]b4_translate[
463 };
464 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
465 const token_number_type undef_token_ = ]b4_undef_token_number[;
466
467 if (static_cast<int>(t) <= yyeof_)
468 return yyeof_;
469 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
470 return translate_table[t];
471 else
472 return undef_token_;
473 }
474]])
475
476
477# b4_lhs_value([TYPE])
478# --------------------
479# Expansion of $<TYPE>$.
480m4_define([b4_lhs_value],
481[b4_symbol_value([yyval], [$1])])
482
483
484# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
485# --------------------------------------
486# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
487# symbols on RHS.
488m4_define([b4_rhs_value],
489[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
490
491
492# b4_lhs_location()
493# -----------------
494# Expansion of @$.
495m4_define([b4_lhs_location],
496[(yyloc)])
497
498
499# b4_rhs_location(RULE-LENGTH, NUM)
500# ---------------------------------
501# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
502# on RHS.
503m4_define([b4_rhs_location],
504[(yylocation_stack_@{($1) - ($2)@})])
505
506
507# b4_parse_param_decl
508# -------------------
509# Extra formal arguments of the constructor.
510# Change the parameter names from "foo" into "foo_yyarg", so that
511# there is no collision bw the user chosen attribute name, and the
512# argument name in the constructor.
513m4_define([b4_parse_param_decl],
514[m4_ifset([b4_parse_param],
515 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
516
517m4_define([b4_parse_param_decl_1],
518[$1_yyarg])
519
520
521
522# b4_parse_param_cons
523# -------------------
524# Extra initialisations of the constructor.
525m4_define([b4_parse_param_cons],
526 [m4_ifset([b4_parse_param],
527 [
528 b4_cc_constructor_calls(b4_parse_param)])])
529m4_define([b4_cc_constructor_calls],
530 [m4_map_sep([b4_cc_constructor_call], [,
531 ], [$@])])
532m4_define([b4_cc_constructor_call],
533 [$2 ($2_yyarg)])
534
535# b4_parse_param_vars
536# -------------------
537# Extra instance variables.
538m4_define([b4_parse_param_vars],
539 [m4_ifset([b4_parse_param],
540 [
541 // User arguments.
542b4_cc_var_decls(b4_parse_param)])])
543m4_define([b4_cc_var_decls],
544 [m4_map_sep([b4_cc_var_decl], [
545], [$@])])
546m4_define([b4_cc_var_decl],
547 [ $1;])
548
549
550## ---------##
551## Values. ##
552## ---------##
553
554# b4_yylloc_default_define
555# ------------------------
556# Define YYLLOC_DEFAULT.
557m4_define([b4_yylloc_default_define],
558[[/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
559 If N is 0, then set CURRENT to the empty location which ends
560 the previous symbol: RHS[0] (always defined). */
561
562# ifndef YYLLOC_DEFAULT
563# define YYLLOC_DEFAULT(Current, Rhs, N) \
564 do \
565 if (N) \
566 { \
567 (Current).begin = YYRHSLOC (Rhs, 1).begin; \
568 (Current).end = YYRHSLOC (Rhs, N).end; \
569 } \
570 else \
571 { \
572 (Current).begin = (Current).end = YYRHSLOC (Rhs, 0).end; \
573 } \
574 while (/*CONSTCOND*/ false)
575# endif
576]])
577
578## -------- ##
579## Checks. ##
580## -------- ##
581
582b4_token_ctor_if([b4_variant_if([],
583 [b4_fatal_at(b4_percent_define_get_loc(api.token.constructor),
584 [cannot use '%s' without '%s'],
585 [%define api.token.constructor],
586 [%define api.value.type variant]))])])