1 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # We do want M4 expansion after # for CPP macros.
22 @
output(b4_dir_prefix
[]position
.hh@
)
23 b4_copyright([Positions
for Bison parsers in C
++],
24 [2002, 2003, 2004, 2005, 2006])[
28 ** Define the ]b4_percent_define_get([[namespace]])[::position class.
31 #ifndef BISON_POSITION_HH
32 # define BISON_POSITION_HH
38 namespace ]b4_percent_define_get([[namespace]])[
40 /// Abstract a position.
44 ]m4_ifdef([b4_location_constructors
], [
45 /// Construct a position.
47 : filename (0), line (]b4_location_initial_line
[), column (]b4_location_initial_column
[)
53 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
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
)
121 (pos1
.filename
== pos2
.filename
122 || pos1
.filename
&& pos2
.filename
&& *pos1
.filename
== *pos2
.filename
)
123 && pos1
.line
== pos2
.line
&& pos1
.column
== pos2
.column
;
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
138 operator<< (std::ostream
& ostr
, const position
& pos
)
141 ostr
<< *pos
.filename
<< ':';
142 return ostr
<< pos
.line
<< '.' << pos
.column
;
146 #endif // not BISON_POSITION_HH]
147 @
output(b4_dir_prefix
[]location
.hh@
)
148 b4_copyright([Locations
for Bison parsers in C
++],
149 [2002, 2003, 2004, 2005, 2006])[
153 ** Define the ]b4_percent_define_get([[namespace]])[::location class.
156 #ifndef BISON_LOCATION_HH
157 # define BISON_LOCATION_HH
161 # include "position.hh"
163 namespace ]b4_percent_define_get([[namespace]])[
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
)
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]