1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2007, 2009-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 # We do want M4 expansion after # for CPP macros.
21 @
output(b4_dir_prefix
[]position
.hh@
)@
22 b4_copyright([Positions
for Bison parsers in C
++],
23 [2002-2007, 2009-2012])[
27 ** Define the ]b4_namespace_ref[::position class.
30 #ifndef BISON_POSITION_HH
31 # define BISON_POSITION_HH
40 /// Abstract a position.
44 ]m4_ifdef([b4_location_constructors
], [
45 /// Construct a position.
47 : filename (YY_NULL
), line (]b4_location_initial_line
[), column (]b4_location_initial_column
[)
53 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
)
56 line
= ]b4_location_initial_line
[;
57 column
= ]b4_location_initial_column
[;
60 /** \name Line and Column related manipulators
63 /// (line related) Advance to the COUNT next lines.
64 inline void lines (int count
= 1)
66 column
= ]b4_location_initial_column
[;
70 /// (column related) Advance to the COUNT next columns.
71 inline void columns (int count
= 1)
73 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
78 /// File name to which this position refers.
79 ]b4_percent_define_get([[filename_type]])[* filename
;
80 /// Current line number.
82 /// Current column number.
86 /// Add and assign a position.
87 inline const position
&
88 operator+= (position
& res
, const int width
)
94 /// Add two position objects.
96 operator+ (const position
& begin
, const int width
)
102 /// Add and assign a position.
103 inline const position
&
104 operator-= (position
& res
, const int width
)
106 return res
+= -width
;
109 /// Add two position objects.
110 inline const position
111 operator- (const position
& begin
, const int width
)
113 return begin
+ -width
;
115 ]b4_percent_define_flag_if([[define_location_comparison]], [[
116 /// Compare two position objects.
118 operator== (const position
& pos1
, const position
& pos2
)
120 return (pos1
.line
== pos2
.line
121 && pos1
.column
== pos2
.column
122 && (pos1
.filename
== pos2
.filename
123 || (pos1
.filename
&& pos2
.filename
124 && *pos1
.filename
== *pos2
.filename
)));
127 /// Compare two position objects.
129 operator!= (const position
& pos1
, const position
& pos2
)
131 return !(pos1
== pos2
);
134 /** \brief Intercept output stream redirection.
135 ** \param ostr the destination output stream
136 ** \param pos a reference to the position to redirect
139 operator<< (std::ostream
& ostr
, const position
& pos
)
142 ostr
<< *pos
.filename
<< ':';
143 return ostr
<< pos
.line
<< '.' << pos
.column
;
147 #endif // not BISON_POSITION_HH]
148 @
output(b4_dir_prefix
[]location
.hh@
)@
149 b4_copyright([Locations
for Bison parsers in C
++],
150 [2002-2007, 2009-2011])[
154 ** Define the ]b4_namespace_ref[::location class.
157 #ifndef BISON_LOCATION_HH
158 # define BISON_LOCATION_HH
162 # include "position.hh"
166 /// Abstract a location.
170 ]m4_ifdef([b4_location_constructors
], [
171 /// Construct a location.
179 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
)
181 begin
.initialize (fn
);
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 inline void columns (unsigned int count
= 1)
200 /// Extend the current location to the COUNT next lines.
201 inline void lines (unsigned int count
= 1)
209 /// Beginning of the located region.
211 /// End of the located region.
215 /// Join two location objects to create a location.
216 inline const location
operator+ (const location
& begin
, const location
& end
)
218 location res
= begin
;
223 /// Add two location objects.
224 inline const location
operator+ (const location
& begin
, unsigned int width
)
226 location res
= begin
;
231 /// Add and assign a location.
232 inline location
& operator+= (location
& res
, unsigned int width
)
237 ]b4_percent_define_flag_if([[define_location_comparison]], [[
238 /// Compare two location objects.
240 operator== (const location
& loc1
, const location
& loc2
)
242 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
245 /// Compare two location objects.
247 operator!= (const location
& loc1
, const location
& loc2
)
249 return !(loc1
== loc2
);
252 /** \brief Intercept output stream redirection.
253 ** \param ostr the destination output stream
254 ** \param loc a reference to the location to redirect
256 ** Avoid duplicate information.
258 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
260 position last
= loc
.end
- 1;
263 && (!loc
.begin
.filename
264 || *loc
.begin
.filename
!= *last
.filename
))
266 else if (loc
.begin
.line
!= last
.line
)
267 ostr
<< '-' << last
.line
<< '.' << last
.column
;
268 else if (loc
.begin
.column
!= last
.column
)
269 ostr
<< '-' << last
.column
;
275 #endif // not BISON_LOCATION_HH]