1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2012 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.
29 ]m4_ifdef([b4_location_constructors
], [[
30 /// Construct a position.
31 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
32 unsigned int l
= ]b4_location_initial_line
[u
,
33 unsigned int c
= ]b4_location_initial_column
[u
)
42 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
43 unsigned int l
= ]b4_location_initial_line
[u
,
44 unsigned int c
= ]b4_location_initial_column
[u
)
51 /** \name Line and Column related manipulators
53 /// (line related) Advance to the COUNT next lines.
54 void lines (int count
= 1)
56 column
= ]b4_location_initial_column
[u
;
60 /// (column related) Advance to the COUNT next columns.
61 void columns (int count
= 1)
63 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
67 /// File name to which this position refers.
68 ]b4_percent_define_get([[filename_type]])[* filename
;
69 /// Current line number.
71 /// Current column number.
75 /// Add and assign a position.
77 operator+= (position
& res
, const int width
)
83 /// Add two position objects.
85 operator+ (const position
& begin
, const int width
)
91 /// Add and assign a position.
93 operator-= (position
& res
, const int width
)
98 /// Add two position objects.
100 operator- (const position
& begin
, const int width
)
102 return begin
+ -width
;
104 ]b4_percent_define_flag_if([[define_location_comparison]], [[
105 /// Compare two position objects.
107 operator== (const position
& pos1
, const position
& pos2
)
109 return (pos1
.line
== pos2
.line
110 && pos1
.column
== pos2
.column
111 && (pos1
.filename
== pos2
.filename
112 || (pos1
.filename
&& pos2
.filename
113 && *pos1
.filename
== *pos2
.filename
)));
116 /// Compare two position objects.
118 operator!= (const position
& pos1
, const position
& pos2
)
120 return !(pos1
== pos2
);
123 /** \brief Intercept output stream redirection.
124 ** \param ostr the destination output stream
125 ** \param pos a reference to the position to redirect
127 template <typename YYChar
>
128 inline std::basic_ostream
<YYChar
>&
129 operator<< (std::basic_ostream
<YYChar
>& ostr
, const position
& pos
)
132 ostr
<< *pos
.filename
<< ':';
133 return ostr
<< pos
.line
<< '.' << pos
.column
;
140 m4_define([b4_location_define
],
141 [[ /// Abstract a location.
145 ]m4_ifdef([b4_location_constructors
], [
146 /// Construct a location from \a b to \a e.
147 location (const position
& b
, const position
& e
)
153 /// Construct a 0-width location in \a p.
154 explicit location (const position
& p
= position ())
160 /// Construct a 0-width location in \a f, \a l, \a c.
161 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
162 unsigned int l
= ]b4_location_initial_line
[u
,
163 unsigned int c
= ]b4_location_initial_column
[u
)
171 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
172 unsigned int l
= ]b4_location_initial_line
[u
,
173 unsigned int c
= ]b4_location_initial_column
[u
)
175 begin
.initialize (f
, l
, c
);
179 /** \name Line and Column related manipulators
182 /// Reset initial location to final location.
188 /// Extend the current location to the COUNT next columns.
189 void columns (unsigned int count
= 1)
194 /// Extend the current location to the COUNT next lines.
195 void lines (unsigned int count
= 1)
203 /// Beginning of the located region.
205 /// End of the located region.
209 /// Join two location objects to create a location.
210 inline const location
operator+ (const location
& begin
, const location
& end
)
212 location res
= begin
;
217 /// Add two location objects.
218 inline const location
operator+ (const location
& begin
, unsigned int width
)
220 location res
= begin
;
225 /// Add and assign a location.
226 inline location
& operator+= (location
& res
, unsigned int width
)
231 ]b4_percent_define_flag_if([[define_location_comparison]], [[
232 /// Compare two location objects.
234 operator== (const location
& loc1
, const location
& loc2
)
236 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
239 /// Compare two location objects.
241 operator!= (const location
& loc1
, const location
& loc2
)
243 return !(loc1
== loc2
);
246 /** \brief Intercept output stream redirection.
247 ** \param ostr the destination output stream
248 ** \param loc a reference to the location to redirect
250 ** Avoid duplicate information.
252 template <typename YYChar
>
253 inline std::basic_ostream
<YYChar
>&
254 operator<< (std::basic_ostream
<YYChar
>& ostr
, const location
& loc
)
256 position last
= loc
.end
- 1;
259 && (!loc
.begin
.filename
260 || *loc
.begin
.filename
!= *last
.filename
))
262 else if (loc
.begin
.line
!= last
.line
)
263 ostr
<< '-' << last
.line
<< '.' << last
.column
;
264 else if (loc
.begin
.column
!= last
.column
)
265 ostr
<< '-' << last
.column
;
271 # We do want M4 expansion after # for CPP macros.
274 b4_output_begin([b4_dir_prefix
[]position
.hh
])
275 b4_copyright([Positions
for Bison parsers in C
++])[
278 ** \file ]b4_dir_prefix[position.hh
279 ** Define the ]b4_namespace_ref[::position class.
282 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
284 # include <algorithm> // std::max
293 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
297 b4_output_begin([b4_dir_prefix
[]location
.hh
])
298 b4_copyright([Locations
for Bison parsers in C
++])[
301 ** \file ]b4_dir_prefix[location.hh
302 ** Define the ]b4_namespace_ref[::location class.
305 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
307 # include "position.hh"
312 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])
317 m4_popdef([b4_copyright_years
])