gnulib: update
[bison.git] / data / skeletons / glr.cc
blob7181402f3dbe443d348f4e538bcca930765406df
1 # C++ GLR skeleton for Bison
3 # Copyright (C) 2002-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 # This skeleton produces a C++ class that encapsulates a C glr parser.
20 # This is in order to reduce the maintenance burden. The glr.c
21 # skeleton is clean and pure enough so that there are no real
22 # problems. The C++ interface is the same as that of lalr1.cc. In
23 # fact, glr.c can replace yacc.c without the user noticing any
24 # difference, and similarly for glr.cc replacing lalr1.cc.
26 # The passing of parse-params
28 # The additional arguments are stored as members of the parser
29 # object, yyparser. The C routines need to carry yyparser
30 # throughout the C parser; that's easy: make yyparser an
31 # additional parse-param. But because the C++ skeleton needs to
32 # know the "real" original parse-param, we save them
33 # (b4_parse_param_orig). Note that b4_parse_param is overquoted
34 # (and c.m4 strips one level of quotes). This is a PITA, and
35 # explains why there are so many levels of quotes.
37 # The locations
39 # We use location.cc just like lalr1.cc, but because glr.c stores
40 # the locations in a union, the position and location classes
41 # must not have a constructor. Therefore, contrary to lalr1.cc, we
42 # must not define "b4_location_constructors". As a consequence the
43 # user must initialize the first positions (in particular the
44 # filename member).
46 # We require a pure interface.
47 m4_define([b4_pure_flag], [1])
49 m4_include(b4_skeletonsdir/[c++.m4])
50 b4_bison_locations_if([m4_include(b4_skeletonsdir/[location.cc])])
52 m4_define([b4_parser_class],
53 [b4_percent_define_get([[api.parser.class]])])
55 # Save the parse parameters.
56 m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
58 # b4_parse_param_wrap
59 # -------------------
60 # New ones.
61 m4_ifset([b4_parse_param],
62 [m4_define([b4_parse_param_wrap],
63 [[b4_namespace_ref::b4_parser_class[& yyparser], [[yyparser]]],]
64 m4_defn([b4_parse_param]))],
65 [m4_define([b4_parse_param_wrap],
66 [[b4_namespace_ref::b4_parser_class[& yyparser], [[yyparser]]]])
70 # b4_yy_symbol_print_define
71 # -------------------------
72 # Bypass the default implementation to generate the "yy_symbol_print"
73 # and "yy_symbol_value_print" functions.
74 m4_define([b4_yy_symbol_print_define],
75 [[/*--------------------.
76 | Print this symbol. |
77 `--------------------*/
79 static void
80 yy_symbol_print (FILE *, ]b4_namespace_ref::b4_parser_class[::symbol_kind_type yytoken,
81 const ]b4_namespace_ref::b4_parser_class[::value_type *yyvaluep]b4_locations_if([[,
82 const ]b4_namespace_ref::b4_parser_class[::location_type *yylocationp]])[]b4_user_formals[)
84 ]b4_parse_param_use[]dnl
85 [ yyparser.yy_symbol_print_ (yytoken, yyvaluep]b4_locations_if([, yylocationp])[);
87 ]])[
89 # Hijack the initial action to initialize the locations.
90 ]b4_bison_locations_if([m4_define([b4_initial_action],
91 [yylloc.initialize ();]m4_ifdef([b4_initial_action], [
92 m4_defn([b4_initial_action])]))])[
94 # Hijack the post prologue to declare yyerror.
95 ]m4_append([b4_post_prologue],
96 [b4_syncline([@oline@], [@ofile@])dnl
97 [static void
98 yyerror (]b4_locations_if([[const ]b4_namespace_ref::b4_parser_class[::location_type *yylocationp,
99 ]])[]m4_ifset([b4_parse_param], [b4_formals(b4_parse_param),
100 ])[const char* msg);]])[
102 # Inserted before the epilogue to define implementations (yyerror, parser member
103 # functions etc.).
104 ]m4_define([b4_glr_cc_pre_epilogue],
105 [b4_syncline([@oline@], [@ofile@])dnl
107 /*------------------.
108 | Report an error. |
109 `------------------*/
111 static void
112 yyerror (]b4_locations_if([[const ]b4_namespace_ref::b4_parser_class[::location_type *yylocationp,
113 ]])[]m4_ifset([b4_parse_param], [b4_formals(b4_parse_param),
114 ])[const char* msg)
116 ]b4_parse_param_use[]dnl
117 [ yyparser.error (]b4_locations_if([[*yylocationp, ]])[msg);
121 ]b4_namespace_open[
122 ]dnl In this section, the parse params are the original parse_params.
123 m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
124 [ /// Build a parser object.
125 ]b4_parser_class::b4_parser_class[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
126 :])[
127 #if ]b4_api_PREFIX[DEBUG
128 ]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
129 #endif]b4_parse_param_cons[
132 ]b4_parser_class::~b4_parser_class[ ()
135 ]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW
139 ]b4_parser_class[::operator() ()
141 return parse ();
145 ]b4_parser_class[::parse ()
147 return ::yy_parse_impl (*this]b4_user_args[);
150 #if ]b4_api_PREFIX[DEBUG
151 /*--------------------.
152 | Print this symbol. |
153 `--------------------*/
155 void
156 ]b4_parser_class[::yy_symbol_value_print_ (symbol_kind_type yykind,
157 const value_type* yyvaluep]b4_locations_if([[,
158 const location_type* yylocationp]])[) const
159 {]b4_locations_if([[
160 YY_USE (yylocationp);]])[
161 YY_USE (yyvaluep);
162 std::ostream& yyo = debug_stream ();
163 std::ostream& yyoutput = yyo;
164 YY_USE (yyoutput);
165 ]b4_symbol_actions([printer])[
169 void
170 ]b4_parser_class[::yy_symbol_print_ (symbol_kind_type yykind,
171 const value_type* yyvaluep]b4_locations_if([[,
172 const location_type* yylocationp]])[) const
174 *yycdebug_ << (yykind < YYNTOKENS ? "token" : "nterm")
175 << ' ' << yysymbol_name (yykind) << " ("]b4_locations_if([[
176 << *yylocationp << ": "]])[;
177 yy_symbol_value_print_ (yykind, yyvaluep]b4_locations_if([[, yylocationp]])[);
178 *yycdebug_ << ')';
181 std::ostream&
182 ]b4_parser_class[::debug_stream () const
184 return *yycdebug_;
187 void
188 ]b4_parser_class[::set_debug_stream (std::ostream& o)
190 yycdebug_ = &o;
194 ]b4_parser_class[::debug_level_type
195 ]b4_parser_class[::debug_level () const
197 return yydebug;
200 void
201 ]b4_parser_class[::set_debug_level (debug_level_type l)
203 // Actually, it is yydebug which is really used.
204 yydebug = l;
207 #endif
208 ]m4_popdef([b4_parse_param])dnl
209 b4_namespace_close[]dnl
213 m4_define([b4_define_symbol_kind],
214 [m4_format([#define %-15s %s],
215 b4_symbol($][1, kind_base),
216 b4_namespace_ref[::]b4_parser_class[::symbol_kind::]b4_symbol($1, kind_base))
219 # b4_glr_cc_setup
220 # ---------------
221 # Setup redirections for glr.c: Map the names used in c.m4 to the ones used
222 # in c++.m4.
223 m4_define([b4_glr_cc_setup],
224 [[]b4_attribute_define[
225 ]b4_null_define[
227 // This skeleton is based on C, yet compiles it as C++.
228 // So expect warnings about C style casts.
229 #if defined __clang__ && 306 <= __clang_major__ * 100 + __clang_minor__
230 # pragma clang diagnostic ignored "-Wold-style-cast"
231 #elif defined __GNUC__ && 406 <= __GNUC__ * 100 + __GNUC_MINOR__
232 # pragma GCC diagnostic ignored "-Wold-style-cast"
233 #endif
235 // On MacOS, PTRDIFF_MAX is defined as long long, which Clang's
236 // -pedantic reports as being a C++11 extension.
237 #if defined __APPLE__ && YY_CPLUSPLUS < 201103L \
238 && defined __clang__ && 4 <= __clang_major__
239 # pragma clang diagnostic ignored "-Wc++11-long-long"
240 #endif
242 #undef ]b4_symbol(empty, [id])[
243 #define ]b4_symbol(empty, [id])[ ]b4_namespace_ref[::]b4_parser_class[::token::]b4_symbol(empty, [id])[
244 #undef ]b4_symbol(eof, [id])[
245 #define ]b4_symbol(eof, [id])[ ]b4_namespace_ref[::]b4_parser_class[::token::]b4_symbol(eof, [id])[
246 #undef ]b4_symbol(error, [id])[
247 #define ]b4_symbol(error, [id])[ ]b4_namespace_ref[::]b4_parser_class[::token::]b4_symbol(error, [id])[
249 #ifndef ]b4_api_PREFIX[STYPE
250 # define ]b4_api_PREFIX[STYPE ]b4_namespace_ref[::]b4_parser_class[::value_type
251 #endif
252 #ifndef ]b4_api_PREFIX[LTYPE
253 # define ]b4_api_PREFIX[LTYPE ]b4_namespace_ref[::]b4_parser_class[::location_type
254 #endif
256 typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind_type yysymbol_kind_t;
258 // Expose C++ symbol kinds to C.
259 ]b4_define_symbol_kind(-2)dnl
260 b4_symbol_foreach([b4_define_symbol_kind])])[
264 m4_define([b4_undef_symbol_kind],
265 [[#undef ]b4_symbol($1, kind_base)[
269 # b4_glr_cc_cleanup
270 # -----------------
271 # Remove redirections for glr.c.
272 m4_define([b4_glr_cc_cleanup],
273 [[#undef ]b4_symbol(empty, [id])[
274 #undef ]b4_symbol(eof, [id])[
275 #undef ]b4_symbol(error, [id])[
277 ]b4_undef_symbol_kind(-2)dnl
278 b4_symbol_foreach([b4_undef_symbol_kind])dnl
282 # b4_shared_declarations(hh|cc)
283 # -----------------------------
284 # Declaration that might either go into the header (if --header, $1 = hh)
285 # or in the implementation file.
286 m4_define([b4_shared_declarations],
287 [m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
288 b4_percent_code_get([[requires]])[
289 #include <iostream>
290 #include <stdexcept>
291 #include <string>
293 ]b4_cxx_portability[
294 ]m4_ifdef([b4_location_include],
295 [[# include ]b4_location_include])[
296 ]b4_variant_if([b4_variant_includes])[
298 // Whether we are compiled with exception support.
299 #ifndef YY_EXCEPTIONS
300 # if defined __GNUC__ && !defined __EXCEPTIONS
301 # define YY_EXCEPTIONS 0
302 # else
303 # define YY_EXCEPTIONS 1
304 # endif
305 #endif
307 ]b4_YYDEBUG_define[
309 ]b4_namespace_open[
311 ]b4_bison_locations_if([m4_ifndef([b4_location_file],
312 [b4_location_define])])[
314 /// A Bison parser.
315 class ]b4_parser_class[
317 public:
318 ]b4_public_types_declare[
320 /// Build a parser object.
321 ]b4_parser_class[ (]b4_parse_param_decl[);
322 virtual ~]b4_parser_class[ ();
324 /// Parse. An alias for parse ().
325 /// \returns 0 iff parsing succeeded.
326 int operator() ();
328 /// Parse.
329 /// \returns 0 iff parsing succeeded.
330 virtual int parse ();
332 #if ]b4_api_PREFIX[DEBUG
333 /// The current debugging stream.
334 std::ostream& debug_stream () const;
335 /// Set the current debugging stream.
336 void set_debug_stream (std::ostream &);
338 /// Type for debugging levels.
339 typedef int debug_level_type;
340 /// The current debugging level.
341 debug_level_type debug_level () const;
342 /// Set the current debugging level.
343 void set_debug_level (debug_level_type l);
344 #endif
346 /// Report a syntax error.]b4_locations_if([[
347 /// \param loc where the syntax error is found.]])[
348 /// \param msg a description of the syntax error.
349 virtual void error (]b4_locations_if([[const location_type& loc, ]])[const std::string& msg);
351 # if ]b4_api_PREFIX[DEBUG
352 public:
353 /// \brief Report a symbol value on the debug stream.
354 /// \param yykind The symbol kind.
355 /// \param yyvaluep Its semantic value.]b4_locations_if([[
356 /// \param yylocationp Its location.]])[
357 virtual void yy_symbol_value_print_ (symbol_kind_type yykind,
358 const value_type* yyvaluep]b4_locations_if([[,
359 const location_type* yylocationp]])[) const;
360 /// \brief Report a symbol on the debug stream.
361 /// \param yykind The symbol kind.
362 /// \param yyvaluep Its semantic value.]b4_locations_if([[
363 /// \param yylocationp Its location.]])[
364 virtual void yy_symbol_print_ (symbol_kind_type yykind,
365 const value_type* yyvaluep]b4_locations_if([[,
366 const location_type* yylocationp]])[) const;
367 private:
368 /// Debug stream.
369 std::ostream* yycdebug_;
370 #endif
372 ]b4_parse_param_vars[
375 ]b4_namespace_close[
377 ]b4_percent_code_get([[provides]])[
378 ]m4_popdef([b4_parse_param])dnl
381 ]b4_header_if(
382 [b4_output_begin([b4_spec_header_file])
383 b4_copyright([Skeleton interface for Bison GLR parsers in C++],
384 [2002-2015, 2018-2021])[
385 // C++ GLR parser skeleton written by Akim Demaille.
387 ]b4_disclaimer[
388 ]b4_cpp_guard_open([b4_spec_mapped_header_file])[
389 ]b4_shared_declarations[
390 ]b4_cpp_guard_close([b4_spec_mapped_header_file])[
391 ]b4_output_end])
393 # Let glr.c (and b4_shared_declarations) believe that the user
394 # arguments include the parser itself.
395 m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_wrap]))
396 m4_include(b4_skeletonsdir/[glr.c])
397 m4_popdef([b4_parse_param])