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.
46 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
47 unsigned int l
= ]b4_location_initial_line
[u
,
48 unsigned int c
= ]b4_location_initial_column
[u
)
57 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
58 unsigned int l
= ]b4_location_initial_line
[u
,
59 unsigned int c
= ]b4_location_initial_column
[u
)
66 /** \name Line and Column related manipulators
68 /// (line related) Advance to the COUNT next lines.
69 void lines (int count
= 1)
71 column
= ]b4_location_initial_column
[u
;
75 /// (column related) Advance to the COUNT next columns.
76 void columns (int count
= 1)
78 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
82 /// File name to which this position refers.
83 ]b4_percent_define_get([[filename_type]])[* filename
;
84 /// Current line number.
86 /// Current column number.
90 /// Add and assign a position.
91 inline const position
&
92 operator+= (position
& res
, const int width
)
98 /// Add two position objects.
100 operator+ (const position
& begin
, const int width
)
102 position res
= begin
;
106 /// Add and assign a position.
107 inline const position
&
108 operator-= (position
& res
, const int width
)
110 return res
+= -width
;
113 /// Add two position objects.
114 inline const position
115 operator- (const position
& begin
, const int width
)
117 return begin
+ -width
;
119 ]b4_percent_define_flag_if([[define_location_comparison]], [[
120 /// Compare two position objects.
122 operator== (const position
& pos1
, const position
& pos2
)
124 return (pos1
.line
== pos2
.line
125 && pos1
.column
== pos2
.column
126 && (pos1
.filename
== pos2
.filename
127 || (pos1
.filename
&& pos2
.filename
128 && *pos1
.filename
== *pos2
.filename
)));
131 /// Compare two position objects.
133 operator!= (const position
& pos1
, const position
& pos2
)
135 return !(pos1
== pos2
);
138 /** \brief Intercept output stream redirection.
139 ** \param ostr the destination output stream
140 ** \param pos a reference to the position to redirect
143 operator<< (std::ostream
& ostr
, const position
& pos
)
146 ostr
<< *pos
.filename
<< ':';
147 return ostr
<< pos
.line
<< '.' << pos
.column
;
151 #endif // not BISON_POSITION_HH]
152 @
output(b4_dir_prefix
[]location
.hh@
)@
153 b4_copyright([Locations
for Bison parsers in C
++],
154 [2002-2007, 2009-2012])[
158 ** Define the ]b4_namespace_ref[::location class.
161 #ifndef BISON_LOCATION_HH
162 # define BISON_LOCATION_HH
166 # include "position.hh"
170 /// Abstract a location.
174 ]m4_ifdef([b4_location_constructors
], [
175 /// Construct a location from \a b to \a e.
176 location (const position
& b
, const position
& e
)
182 /// Construct a 0-width location in \a p.
183 explicit location (const position
& p
= position ())
189 /// Construct a 0-width location in \a f, \a l, \a c.
190 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
191 unsigned int l
= ]b4_location_initial_line
[u
,
192 unsigned int c
= ]b4_location_initial_column
[u
)
200 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
201 unsigned int l
= ]b4_location_initial_line
[u
,
202 unsigned int c
= ]b4_location_initial_column
[u
)
204 begin
.initialize (f
, l
, c
);
208 /** \name Line and Column related manipulators
211 /// Reset initial location to final location.
217 /// Extend the current location to the COUNT next columns.
218 void columns (unsigned int count
= 1)
223 /// Extend the current location to the COUNT next lines.
224 void lines (unsigned int count
= 1)
232 /// Beginning of the located region.
234 /// End of the located region.
238 /// Join two location objects to create a location.
239 inline const location
operator+ (const location
& begin
, const location
& end
)
241 location res
= begin
;
246 /// Add two location objects.
247 inline const location
operator+ (const location
& begin
, unsigned int width
)
249 location res
= begin
;
254 /// Add and assign a location.
255 inline location
& operator+= (location
& res
, unsigned int width
)
260 ]b4_percent_define_flag_if([[define_location_comparison]], [[
261 /// Compare two location objects.
263 operator== (const location
& loc1
, const location
& loc2
)
265 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
268 /// Compare two location objects.
270 operator!= (const location
& loc1
, const location
& loc2
)
272 return !(loc1
== loc2
);
275 /** \brief Intercept output stream redirection.
276 ** \param ostr the destination output stream
277 ** \param loc a reference to the location to redirect
279 ** Avoid duplicate information.
281 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
283 position last
= loc
.end
- 1;
286 && (!loc
.begin
.filename
287 || *loc
.begin
.filename
!= *last
.filename
))
289 else if (loc
.begin
.line
!= last
.line
)
290 ostr
<< '-' << last
.line
<< '.' << last
.column
;
291 else if (loc
.begin
.column
!= last
.column
)
292 ostr
<< '-' << last
.column
;
298 #endif // not BISON_LOCATION_HH]