1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2015 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 <http://www.gnu.org/licenses/>.
18 m4_pushdef([b4_copyright_years
],
23 # Define class position.
24 m4_define([b4_position_define
],
25 [[ /// Abstract a position.
28 public:]m4_ifdef([b4_location_constructors
], [[
29 /// Construct a position.
30 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULLPTR
,
31 unsigned int l
= ]b4_location_initial_line
[u
,
32 unsigned int c
= ]b4_location_initial_column
[u
)
41 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULLPTR
,
42 unsigned int l
= ]b4_location_initial_line
[u
,
43 unsigned int c
= ]b4_location_initial_column
[u
)
50 /** \name Line and Column related manipulators
52 /// (line related) Advance to the COUNT next lines.
53 void lines (int count
= 1)
57 column
= ]b4_location_initial_column
[u
;
58 line
= add_ (line
, count
, ]b4_location_initial_line
[);
62 /// (column related) Advance to the COUNT next columns.
63 void columns (int count
= 1)
65 column
= add_ (column
, count
, ]b4_location_initial_column
[);
69 /// File name to which this position refers.
70 ]b4_percent_define_get([[filename_type]])[* filename
;
71 /// Current line number.
73 /// Current column number.
77 /// Compute max(min, lhs+rhs) (provided min <= lhs).
78 static unsigned int add_ (unsigned int lhs
, int rhs
, unsigned int min
)
80 return (0 < rhs
|| -static_cast<unsigned int>(rhs
) < lhs
86 /// Add \a width columns, in place.
88 operator+= (position
& res
, int width
)
94 /// Add \a width columns.
96 operator+ (position res
, int width
)
101 /// Subtract \a width columns, in place.
103 operator-= (position
& res
, int width
)
105 return res
+= -width
;
108 /// Subtract \a width columns.
110 operator- (position res
, int width
)
114 ]b4_percent_define_flag_if([[define_location_comparison]], [[
115 /// Compare two position objects.
117 operator== (const position
& pos1
, const position
& pos2
)
119 return (pos1
.line
== pos2
.line
120 && pos1
.column
== pos2
.column
121 && (pos1
.filename
== pos2
.filename
122 || (pos1
.filename
&& pos2
.filename
123 && *pos1
.filename
== *pos2
.filename
)));
126 /// Compare two position objects.
128 operator!= (const position
& pos1
, const position
& pos2
)
130 return !(pos1
== pos2
);
133 /** \brief Intercept output stream redirection.
134 ** \param ostr the destination output stream
135 ** \param pos a reference to the position to redirect
137 template <typename YYChar
>
138 inline std::basic_ostream
<YYChar
>&
139 operator<< (std::basic_ostream
<YYChar
>& ostr
, const position
& pos
)
142 ostr
<< *pos
.filename
<< ':';
143 return ostr
<< pos
.line
<< '.' << pos
.column
;
150 m4_define([b4_location_define
],
151 [[ /// Abstract a location.
155 ]m4_ifdef([b4_location_constructors
], [
156 /// Construct a location from \a b to \a e.
157 location (const position
& b
, const position
& e
)
163 /// Construct a 0-width location in \a p.
164 explicit location (const position
& p
= position ())
170 /// Construct a 0-width location in \a f, \a l, \a c.
171 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
172 unsigned int l
= ]b4_location_initial_line
[u
,
173 unsigned int c
= ]b4_location_initial_column
[u
)
181 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULLPTR
,
182 unsigned int l
= ]b4_location_initial_line
[u
,
183 unsigned int c
= ]b4_location_initial_column
[u
)
185 begin
.initialize (f
, l
, c
);
189 /** \name Line and Column related manipulators
192 /// Reset initial location to final location.
198 /// Extend the current location to the COUNT next columns.
199 void columns (int count
= 1)
204 /// Extend the current location to the COUNT next lines.
205 void lines (int count
= 1)
213 /// Beginning of the located region.
215 /// End of the located region.
219 /// Join two locations, in place.
220 inline location
& operator+= (location
& res
, const location
& end
)
226 /// Join two locations.
227 inline location
operator+ (location res
, const location
& end
)
232 /// Add \a width columns to the end position, in place.
233 inline location
& operator+= (location
& res
, int width
)
239 /// Add \a width columns to the end position.
240 inline location
operator+ (location res
, int width
)
245 /// Subtract \a width columns to the end position, in place.
246 inline location
& operator-= (location
& res
, int width
)
248 return res
+= -width
;
251 /// Subtract \a width columns to the end position.
252 inline location
operator- (location res
, int width
)
256 ]b4_percent_define_flag_if([[define_location_comparison]], [[
257 /// Compare two location objects.
259 operator== (const location
& loc1
, const location
& loc2
)
261 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
264 /// Compare two location objects.
266 operator!= (const location
& loc1
, const location
& loc2
)
268 return !(loc1
== loc2
);
271 /** \brief Intercept output stream redirection.
272 ** \param ostr the destination output stream
273 ** \param loc a reference to the location to redirect
275 ** Avoid duplicate information.
277 template <typename YYChar
>
278 inline std::basic_ostream
<YYChar
>&
279 operator<< (std::basic_ostream
<YYChar
>& ostr
, const location
& loc
)
281 unsigned int end_col
= 0 < loc
.end
.column
? loc
.end
.column
- 1 : 0;
284 && (!loc
.begin
.filename
285 || *loc
.begin
.filename
!= *loc
.end
.filename
))
286 ostr
<< '-' << loc
.end
.filename
<< ':' << loc
.end
.line
<< '.' << end_col
;
287 else if (loc
.begin
.line
< loc
.end
.line
)
288 ostr
<< '-' << loc
.end
.line
<< '.' << end_col
;
289 else if (loc
.begin
.column
< end_col
)
290 ostr
<< '-' << end_col
;
297 b4_output_begin([b4_dir_prefix
[]position
.hh
])
298 b4_copyright([Positions
for Bison parsers in C
++])[
301 ** \file ]b4_dir_prefix[position.hh
302 ** Define the ]b4_namespace_ref[::position class.
305 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
307 # include <algorithm> // std::max
316 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
320 b4_output_begin([b4_dir_prefix
[]location
.hh
])
321 b4_copyright([Locations
for Bison parsers in C
++])[
324 ** \file ]b4_dir_prefix[location.hh
325 ** Define the ]b4_namespace_ref[::location class.
328 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
330 # include "position.hh"
335 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])
340 m4_popdef([b4_copyright_years
])