]>
Commit | Line | Data |
---|---|---|
08af01c2 | 1 | -*- Autoconf -*- |
cf147260 AD |
2 | |
3 | # C++ skeleton for Bison | |
4 | ||
e141f4d4 | 5 | # Copyright (C) 2002-2010 Free Software Foundation, Inc. |
cf147260 | 6 | |
f16b0819 | 7 | # This program is free software: you can redistribute it and/or modify |
cf147260 | 8 | # it under the terms of the GNU General Public License as published by |
f16b0819 | 9 | # the Free Software Foundation, either version 3 of the License, or |
cf147260 | 10 | # (at your option) any later version. |
f16b0819 | 11 | # |
cf147260 AD |
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. | |
f16b0819 | 16 | # |
cf147260 | 17 | # You should have received a copy of the GNU General Public License |
f16b0819 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
cf147260 | 19 | |
3953ed88 AD |
20 | m4_include(b4_pkgdatadir/[c.m4]) |
21 | ||
cf147260 AD |
22 | ## ---------------- ## |
23 | ## Default values. ## | |
24 | ## ---------------- ## | |
25 | ||
a4e25e1d | 26 | b4_percent_define_default([[parser_class_name]], [[parser]]) |
7789b6e3 AD |
27 | |
28 | # Don't do that so that we remember whether we're using a user | |
29 | # request, or the default value. | |
30 | # | |
f38ae16f | 31 | # b4_percent_define_default([[location_type]], [[location]]) |
7789b6e3 | 32 | |
a4e25e1d | 33 | b4_percent_define_default([[filename_type]], [[std::string]]) |
171ad99d | 34 | b4_percent_define_default([[api.namespace]], m4_defn([b4_prefix])) |
67501061 | 35 | |
c1d19e10 | 36 | b4_percent_define_default([[global_tokens_and_yystype]], [[false]]) |
a4e25e1d JD |
37 | b4_percent_define_default([[define_location_comparison]], |
38 | [m4_if(b4_percent_define_get([[filename_type]]), | |
c1d19e10 | 39 | [std::string], [[true]], [[false]])]) |
cf147260 AD |
40 | |
41 | ||
67501061 | 42 | |
793fbca5 JD |
43 | ## ----------- ## |
44 | ## Namespace. ## | |
45 | ## ----------- ## | |
46 | ||
67501061 AD |
47 | m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])]) |
48 | ||
793fbca5 JD |
49 | |
50 | # Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it | |
51 | # would compile as an absolute reference with `parser' in the global namespace. | |
52 | # b4_namespace_open would open an anonymous namespace and thus establish | |
53 | # internal linkage. This would compile. However, it's cryptic, and internal | |
54 | # linkage for the parser would be specified in all translation units that | |
55 | # include the header, which is always generated. If we ever need to permit | |
56 | # internal linkage somehow, surely we can find a cleaner approach. | |
57 | m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [], | |
67501061 | 58 | [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), |
793fbca5 JD |
59 | [[namespace reference is empty]])]) |
60 | ||
61 | # Instead of assuming the C++ compiler will do it, Bison should reject any | |
62 | # invalid b4_namepsace_ref that would be converted to a valid | |
63 | # b4_namespace_open. The problem is that Bison doesn't always output | |
64 | # b4_namespace_ref to uncommented code but should reserve the ability to do so | |
65 | # in future releases without risking breaking any existing user grammars. | |
66 | # Specifically, don't allow empty names as b4_namespace_open would just convert | |
67 | # those into anonymous namespaces, and that might tempt some users. | |
68 | m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [], | |
67501061 | 69 | [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), |
793fbca5 JD |
70 | [[namespace reference has consecutive "::"]])]) |
71 | m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [], | |
67501061 | 72 | [b4_complain_at(b4_percent_define_get_loc([[api.namespace]]), |
793fbca5 JD |
73 | [[namespace reference has a trailing "::"]])]) |
74 | ||
75 | m4_define([b4_namespace_open], | |
67501061 | 76 | [b4_user_code([b4_percent_define_get_syncline([[api.namespace]]) |
793fbca5 JD |
77 | [namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref), |
78 | [^\(.\)[ ]*::], [\1])), | |
79 | [::], [ { namespace ])[ {]])]) | |
80 | ||
81 | m4_define([b4_namespace_close], | |
67501061 | 82 | [b4_user_code([b4_percent_define_get_syncline([[api.namespace]]) |
4977e0a7 | 83 | m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]), |
b987342b | 84 | [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*], |
793fbca5 | 85 | [\1])), |
b987342b | 86 | [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])]) |
793fbca5 JD |
87 | |
88 | ||
cf147260 AD |
89 | # b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) |
90 | # ----------------------------------------------------- | |
c095d689 | 91 | # Output the definition of the tokens as enums. |
cf147260 | 92 | m4_define([b4_token_enums], |
cf147260 AD |
93 | [/* Tokens. */ |
94 | enum yytokentype { | |
95 | m4_map_sep([ b4_token_enum], [, | |
96 | ], | |
97 | [$@]) | |
98 | }; | |
c095d689 | 99 | ]) |
cf147260 AD |
100 | |
101 | ||
102 | ## ----------------- ## | |
103 | ## Semantic Values. ## | |
104 | ## ----------------- ## | |
105 | ||
b9e4eb5b AD |
106 | # b4_semantic_type_declare |
107 | # ------------------------ | |
108 | # Declare semantic_type. | |
109 | m4_define([b4_semantic_type_declare], | |
110 | [ /// Symbol semantic values. | |
111 | m4_ifdef([b4_stype], | |
112 | [ union semantic_type | |
113 | {b4_user_stype | |
114 | };], | |
115 | [m4_if(b4_tag_seen_flag, 0, | |
116 | [[ typedef int semantic_type;]], | |
117 | [[ typedef YYSTYPE semantic_type;]])])]) | |
118 | ||
119 | ||
4f84717d AD |
120 | # b4_public_types_declare |
121 | # ----------------------- | |
122 | # Define the public types: token, semantic value, location, and so forth. | |
1d6b689b | 123 | # Depending on %define token_lex, may be output in the header or source file. |
4f84717d AD |
124 | m4_define([b4_public_types_declare], |
125 | [[#ifndef YYSTYPE | |
126 | ]b4_semantic_type_declare[ | |
127 | #else | |
128 | typedef YYSTYPE semantic_type; | |
129 | #endif]b4_locations_if([ | |
130 | /// Symbol locations. | |
7789b6e3 AD |
131 | typedef b4_percent_define_get([[location_type]], |
132 | [[location]]) location_type;])[ | |
1d6b689b | 133 | |
ff601366 AD |
134 | /// Syntax errors thrown from user actions. |
135 | struct syntax_error : std::runtime_error | |
136 | { | |
137 | syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m);]b4_locations_if([ | |
138 | location_type location;])[ | |
139 | }; | |
140 | ||
4f84717d AD |
141 | /// Tokens. |
142 | struct token | |
143 | { | |
144 | ]b4_token_enums(b4_tokens)[ | |
145 | }; | |
1d6b689b | 146 | |
4f84717d AD |
147 | /// Token type. |
148 | typedef token::yytokentype token_type; | |
1d6b689b AD |
149 | |
150 | /// A complete symbol, with its type. | |
151 | template <typename Exact> | |
152 | struct symbol_base_type | |
153 | { | |
154 | /// Default constructor. | |
155 | inline symbol_base_type (); | |
156 | ||
157 | /// Constructor.]b4_locations_if([ | |
158 | inline symbol_base_type (const location_type& l)])[; | |
159 | inline symbol_base_type (]b4_args( | |
160 | [const semantic_type& v], | |
161 | b4_locations_if([const location_type& l]))[); | |
162 | ||
163 | /// Return this with its exact type. | |
164 | const Exact& self () const; | |
165 | Exact& self (); | |
166 | ||
167 | /// Return the type of this symbol. | |
168 | int type_get () const; | |
169 | ||
170 | /// The semantic value. | |
171 | semantic_type value;]b4_locations_if([ | |
172 | ||
173 | /// The location. | |
174 | location_type location;])[ | |
175 | }; | |
176 | ||
177 | /// External form of a symbol: its type and attributes. | |
178 | struct symbol_type : symbol_base_type<symbol_type> | |
179 | { | |
180 | /// The parent class. | |
181 | typedef symbol_base_type<symbol_type> super_type; | |
182 | ||
183 | /// Default constructor. | |
184 | inline symbol_type (); | |
185 | ||
2b08bceb AD |
186 | /// Constructor for tokens with semantic value. |
187 | inline symbol_type (]b4_args([token_type t], | |
1d6b689b AD |
188 | [const semantic_type& v], |
189 | b4_locations_if([const location_type& l]))[); | |
190 | ||
2b08bceb AD |
191 | /// Constructor for valueless tokens. |
192 | inline symbol_type (]b4_args([token_type t], | |
1d6b689b AD |
193 | b4_locations_if([const location_type& l]))[); |
194 | ||
195 | /// The symbol type. | |
196 | int type; | |
197 | ||
2b08bceb | 198 | /// The symbol type. |
1d6b689b AD |
199 | inline int type_get_ () const; |
200 | ||
2b08bceb | 201 | /// The token. |
1d6b689b AD |
202 | inline token_type token () const; |
203 | }; | |
0623bacc | 204 | ]b4_symbol_constructor_declare]) |
4f84717d AD |
205 | |
206 | ||
1d6b689b AD |
207 | # b4_public_types_define |
208 | # ---------------------- | |
209 | # Provide the implementation needed by the public types. | |
210 | m4_define([b4_public_types_define], | |
ff601366 AD |
211 | [[ inline |
212 | ]b4_parser_class_name[::syntax_error::syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m) | |
213 | : std::runtime_error (m)]b4_locations_if([ | |
214 | , location (l)])[ | |
215 | {} | |
216 | ||
217 | // symbol_base_type. | |
1d6b689b | 218 | template <typename Exact> |
ff601366 | 219 | inline |
1d6b689b AD |
220 | ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type () |
221 | : value()]b4_locations_if([ | |
222 | , location()])[ | |
223 | { | |
224 | }]b4_locations_if([[ | |
225 | ||
226 | template <typename Exact> | |
29660062 | 227 | inline |
1d6b689b AD |
228 | ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l) |
229 | : value() | |
230 | , location(l) | |
231 | { | |
232 | }]])[ | |
233 | ||
234 | template <typename Exact> | |
29660062 | 235 | inline |
1d6b689b AD |
236 | ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args( |
237 | [const semantic_type& v], | |
238 | b4_locations_if([const location_type& l]))[) | |
239 | : value(v)]b4_locations_if([ | |
240 | , location(l)])[ | |
241 | { | |
242 | } | |
243 | ||
244 | template <typename Exact> | |
29660062 | 245 | inline |
1d6b689b AD |
246 | const Exact& |
247 | ]b4_parser_class_name[::symbol_base_type<Exact>::self () const | |
248 | { | |
249 | return static_cast<const Exact&>(*this); | |
250 | } | |
251 | ||
252 | template <typename Exact> | |
29660062 | 253 | inline |
1d6b689b AD |
254 | Exact& |
255 | ]b4_parser_class_name[::symbol_base_type<Exact>::self () | |
256 | { | |
257 | return static_cast<Exact&>(*this); | |
258 | } | |
259 | ||
260 | template <typename Exact> | |
29660062 | 261 | inline |
1d6b689b AD |
262 | int |
263 | ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const | |
264 | { | |
265 | return self ().type_get_ (); | |
266 | } | |
267 | ||
268 | // symbol_type. | |
29660062 | 269 | inline |
1d6b689b AD |
270 | ]b4_parser_class_name[::symbol_type::symbol_type () |
271 | : super_type () | |
272 | , type () | |
273 | { | |
274 | } | |
275 | ||
29660062 | 276 | inline |
1d6b689b | 277 | ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( |
2b08bceb | 278 | [token_type t], |
1d6b689b AD |
279 | b4_locations_if([const location_type& l]))[) |
280 | : super_type (]b4_locations_if([l])[) | |
2b08bceb | 281 | , type (yytranslate_ (t)) |
1d6b689b AD |
282 | { |
283 | } | |
284 | ||
29660062 | 285 | inline |
1d6b689b | 286 | ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args( |
2b08bceb | 287 | [token_type t], |
1d6b689b AD |
288 | [const semantic_type& v], |
289 | b4_locations_if([const location_type& l]))[) | |
290 | : super_type (v]b4_locations_if([, l])[) | |
2b08bceb | 291 | , type (yytranslate_ (t)) |
1d6b689b AD |
292 | { |
293 | } | |
294 | ||
29660062 | 295 | inline |
1d6b689b AD |
296 | int |
297 | ]b4_parser_class_name[::symbol_type::type_get_ () const | |
298 | { | |
299 | return type; | |
300 | } | |
301 | ]b4_lex_symbol_if([[ | |
29660062 | 302 | inline |
1d6b689b AD |
303 | ]b4_parser_class_name[::token_type |
304 | ]b4_parser_class_name[::symbol_type::token () const | |
305 | { | |
306 | // YYTOKNUM[NUM] -- (External) token number corresponding to the | |
307 | // (internal) symbol number NUM (which must be that of a token). */ | |
308 | static | |
309 | const ]b4_int_type_for([b4_toknum])[ | |
310 | yytoken_number_[] = | |
311 | { | |
312 | ]b4_toknum[ | |
313 | }; | |
314 | return static_cast<token_type> (yytoken_number_[type]); | |
315 | } | |
0623bacc AD |
316 | ]])[]dnl |
317 | b4_symbol_constructor_define]) | |
318 | ||
319 | ||
320 | # b4_symbol_constructor_declare | |
321 | # b4_symbol_constructor_define | |
322 | # ----------------------------- | |
323 | # Declare/define symbol constructors for all the value types. | |
324 | # Use at class-level. Redefined in variant.hh. | |
325 | m4_define([b4_symbol_constructor_declare], []) | |
326 | m4_define([b4_symbol_constructor_define], []) | |
327 | ||
1d6b689b | 328 | |
cbf25ce7 AD |
329 | # b4_yytranslate_define |
330 | # --------------------- | |
331 | # Define yytranslate_. Sometimes used in the header file, | |
332 | # sometimes in the cc file. | |
333 | m4_define([b4_yytranslate_define], | |
334 | [[ // Symbol number corresponding to token number t. | |
335 | ]b4_parser_class_name[::token_number_type | |
336 | ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type], | |
337 | [int])[ t) | |
338 | { | |
339 | static | |
340 | const token_number_type | |
341 | translate_table[] = | |
342 | { | |
343 | ]b4_translate[ | |
344 | }; | |
345 | const unsigned int user_token_number_max_ = ]b4_user_token_number_max[; | |
346 | const token_number_type undef_token_ = ]b4_undef_token_number[; | |
347 | ||
348 | if (static_cast<int>(t) <= yyeof_) | |
349 | return yyeof_; | |
350 | else if (static_cast<unsigned int> (t) <= user_token_number_max_) | |
351 | return translate_table[t]; | |
352 | else | |
353 | return undef_token_; | |
354 | } | |
355 | ]]) | |
356 | ||
1d6b689b | 357 | |
cf147260 AD |
358 | # b4_lhs_value([TYPE]) |
359 | # -------------------- | |
360 | # Expansion of $<TYPE>$. | |
361 | m4_define([b4_lhs_value], | |
1fa5d8bb | 362 | [b4_symbol_value([yyval], [$1])]) |
cf147260 AD |
363 | |
364 | ||
365 | # b4_rhs_value(RULE-LENGTH, NUM, [TYPE]) | |
366 | # -------------------------------------- | |
367 | # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH | |
368 | # symbols on RHS. | |
369 | m4_define([b4_rhs_value], | |
1fa5d8bb AD |
370 | [b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])]) |
371 | ||
cf147260 AD |
372 | |
373 | # b4_lhs_location() | |
374 | # ----------------- | |
375 | # Expansion of @$. | |
376 | m4_define([b4_lhs_location], | |
377 | [(yyloc)]) | |
378 | ||
379 | ||
380 | # b4_rhs_location(RULE-LENGTH, NUM) | |
381 | # --------------------------------- | |
382 | # Expansion of @NUM, where the current rule has RULE-LENGTH symbols | |
383 | # on RHS. | |
384 | m4_define([b4_rhs_location], | |
d1ff7a7c | 385 | [(yylocation_stack_@{($1) - ($2)@})]) |
cf147260 AD |
386 | |
387 | ||
388 | # b4_parse_param_decl | |
389 | # ------------------- | |
390 | # Extra formal arguments of the constructor. | |
391 | # Change the parameter names from "foo" into "foo_yyarg", so that | |
392 | # there is no collision bw the user chosen attribute name, and the | |
393 | # argument name in the constructor. | |
394 | m4_define([b4_parse_param_decl], | |
395 | [m4_ifset([b4_parse_param], | |
396 | [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])]) | |
397 | ||
398 | m4_define([b4_parse_param_decl_1], | |
399 | [$1_yyarg]) | |
400 | ||
401 | ||
402 | ||
403 | # b4_parse_param_cons | |
404 | # ------------------- | |
405 | # Extra initialisations of the constructor. | |
406 | m4_define([b4_parse_param_cons], | |
407 | [m4_ifset([b4_parse_param], | |
fa7b79c0 | 408 | [ |
cf147260 AD |
409 | b4_cc_constructor_calls(b4_parse_param)])]) |
410 | m4_define([b4_cc_constructor_calls], | |
411 | [m4_map_sep([b4_cc_constructor_call], [, | |
412 | ], [$@])]) | |
413 | m4_define([b4_cc_constructor_call], | |
414 | [$2 ($2_yyarg)]) | |
415 | ||
416 | # b4_parse_param_vars | |
417 | # ------------------- | |
418 | # Extra instance variables. | |
419 | m4_define([b4_parse_param_vars], | |
420 | [m4_ifset([b4_parse_param], | |
421 | [ | |
422 | /* User arguments. */ | |
423 | b4_cc_var_decls(b4_parse_param)])]) | |
424 | m4_define([b4_cc_var_decls], | |
425 | [m4_map_sep([b4_cc_var_decl], [ | |
426 | ], [$@])]) | |
427 | m4_define([b4_cc_var_decl], | |
428 | [ $1;]) |