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 l
= ]b4_location_initial_line
[u
,
32 unsigned c
= ]b4_location_initial_column
[u
)
40 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULLPTR
,
41 unsigned l
= ]b4_location_initial_line
[u
,
42 unsigned c
= ]b4_location_initial_column
[u
)
49 /** \name Line and Column related manipulators
51 /// (line related) Advance to the COUNT next lines.
52 void lines (int count
= 1)
56 column
= ]b4_location_initial_column
[u
;
57 line
= add_ (line
, count
, ]b4_location_initial_line
[);
61 /// (column related) Advance to the COUNT next columns.
62 void columns (int count
= 1)
64 column
= add_ (column
, count
, ]b4_location_initial_column
[);
68 /// File name to which this position refers.
69 ]b4_percent_define_get([[filename_type]])[* filename
;
70 /// Current line number.
72 /// Current column number.
76 /// Compute max(min, lhs+rhs) (provided min <= lhs).
77 static unsigned add_ (unsigned lhs
, int rhs
, unsigned min
)
79 return (0 < rhs
|| -static_cast<unsigned>(rhs
) < lhs
85 /// Add \a width columns, in place.
87 operator+= (position
& res
, int width
)
93 /// Add \a width columns.
95 operator+ (position res
, int width
)
100 /// Subtract \a width columns, in place.
102 operator-= (position
& res
, int width
)
104 return res
+= -width
;
107 /// Subtract \a width columns.
109 operator- (position res
, int width
)
113 ]b4_percent_define_flag_if([[define_location_comparison]], [[
114 /// Compare two position objects.
116 operator== (const position
& pos1
, const position
& pos2
)
118 return (pos1
.line
== pos2
.line
119 && pos1
.column
== pos2
.column
120 && (pos1
.filename
== pos2
.filename
121 || (pos1
.filename
&& pos2
.filename
122 && *pos1
.filename
== *pos2
.filename
)));
125 /// Compare two position objects.
127 operator!= (const position
& pos1
, const position
& pos2
)
129 return !(pos1
== pos2
);
132 /** \brief Intercept output stream redirection.
133 ** \param ostr the destination output stream
134 ** \param pos a reference to the position to redirect
136 template <typename YYChar
>
137 inline std::basic_ostream
<YYChar
>&
138 operator<< (std::basic_ostream
<YYChar
>& ostr
, const position
& pos
)
141 ostr
<< *pos
.filename
<< ':';
142 return ostr
<< pos
.line
<< '.' << pos
.column
;
149 m4_define([b4_location_define
],
150 [[ /// Abstract a location.
154 ]m4_ifdef([b4_location_constructors
], [
155 /// Construct a location from \a b to \a e.
156 location (const position
& b
, const position
& e
)
161 /// Construct a 0-width location in \a p.
162 explicit location (const position
& p
= position ())
167 /// Construct a 0-width location in \a f, \a l, \a c.
168 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
169 unsigned l
= ]b4_location_initial_line
[u
,
170 unsigned c
= ]b4_location_initial_column
[u
)
177 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULLPTR
,
178 unsigned l
= ]b4_location_initial_line
[u
,
179 unsigned c
= ]b4_location_initial_column
[u
)
181 begin
.initialize (f
, l
, c
);
185 /** \name Line and Column related manipulators
188 /// Reset initial location to final location.
194 /// Extend the current location to the COUNT next columns.
195 void columns (int count
= 1)
200 /// Extend the current location to the COUNT next lines.
201 void lines (int count
= 1)
209 /// Beginning of the located region.
211 /// End of the located region.
215 /// Join two locations, in place.
216 inline location
& operator+= (location
& res
, const location
& end
)
222 /// Join two locations.
223 inline location
operator+ (location res
, const location
& end
)
228 /// Add \a width columns to the end position, in place.
229 inline location
& operator+= (location
& res
, int width
)
235 /// Add \a width columns to the end position.
236 inline location
operator+ (location res
, int width
)
241 /// Subtract \a width columns to the end position, in place.
242 inline location
& operator-= (location
& res
, int width
)
244 return res
+= -width
;
247 /// Subtract \a width columns to the end position.
248 inline location
operator- (location res
, int width
)
252 ]b4_percent_define_flag_if([[define_location_comparison]], [[
253 /// Compare two location objects.
255 operator== (const location
& loc1
, const location
& loc2
)
257 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
260 /// Compare two location objects.
262 operator!= (const location
& loc1
, const location
& loc2
)
264 return !(loc1
== loc2
);
267 /** \brief Intercept output stream redirection.
268 ** \param ostr the destination output stream
269 ** \param loc a reference to the location to redirect
271 ** Avoid duplicate information.
273 template <typename YYChar
>
274 inline std::basic_ostream
<YYChar
>&
275 operator<< (std::basic_ostream
<YYChar
>& ostr
, const location
& loc
)
277 unsigned end_col
= 0 < loc
.end
.column
? loc
.end
.column
- 1 : 0;
280 && (!loc
.begin
.filename
281 || *loc
.begin
.filename
!= *loc
.end
.filename
))
282 ostr
<< '-' << loc
.end
.filename
<< ':' << loc
.end
.line
<< '.' << end_col
;
283 else if (loc
.begin
.line
< loc
.end
.line
)
284 ostr
<< '-' << loc
.end
.line
<< '.' << end_col
;
285 else if (loc
.begin
.column
< end_col
)
286 ostr
<< '-' << end_col
;
293 b4_output_begin([b4_dir_prefix
[]position
.hh
])
294 b4_copyright([Positions
for Bison parsers in C
++])[
297 ** \file ]b4_dir_prefix[position.hh
298 ** Define the ]b4_namespace_ref[::position class.
301 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
303 # include <algorithm> // std::max
312 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
316 b4_output_begin([b4_dir_prefix
[]location
.hh
])
317 b4_copyright([Locations
for Bison parsers in C
++])[
320 ** \file ]b4_dir_prefix[location.hh
321 ** Define the ]b4_namespace_ref[::location class.
324 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
326 # include "position.hh"
331 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])
336 m4_popdef([b4_copyright_years
])