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/>.
19 b4_output_begin([b4_dir_prefix
[]position
.hh
])
20 b4_copyright([Positions
for Bison parsers in C
++],
21 [2002-2007, 2009-2012])[
24 ** \file ]b4_dir_prefix[position.hh
25 ** Define the ]b4_namespace_ref[::position class.
28 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
30 # include <algorithm> // std::max
37 /// Abstract a position.
41 ]m4_ifdef([b4_location_constructors
], [[
42 /// Construct a position.
43 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
44 unsigned int l
= ]b4_location_initial_line
[u
,
45 unsigned int c
= ]b4_location_initial_column
[u
)
54 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
55 unsigned int l
= ]b4_location_initial_line
[u
,
56 unsigned int c
= ]b4_location_initial_column
[u
)
63 /** \name Line and Column related manipulators
65 /// (line related) Advance to the COUNT next lines.
66 void lines (int count
= 1)
68 column
= ]b4_location_initial_column
[u
;
72 /// (column related) Advance to the COUNT next columns.
73 void columns (int count
= 1)
75 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
79 /// File name to which this position refers.
80 ]b4_percent_define_get([[filename_type]])[* filename
;
81 /// Current line number.
83 /// Current column number.
87 /// Add and assign a position.
89 operator+= (position
& res
, const int width
)
95 /// Add two position objects.
97 operator+ (const position
& begin
, const int width
)
103 /// Add and assign a position.
105 operator-= (position
& res
, const int width
)
107 return res
+= -width
;
110 /// Add two position objects.
111 inline const position
112 operator- (const position
& begin
, const int width
)
114 return begin
+ -width
;
116 ]b4_percent_define_flag_if([[define_location_comparison]], [[
117 /// Compare two position objects.
119 operator== (const position
& pos1
, const position
& pos2
)
121 return (pos1
.line
== pos2
.line
122 && pos1
.column
== pos2
.column
123 && (pos1
.filename
== pos2
.filename
124 || (pos1
.filename
&& pos2
.filename
125 && *pos1
.filename
== *pos2
.filename
)));
128 /// Compare two position objects.
130 operator!= (const position
& pos1
, const position
& pos2
)
132 return !(pos1
== pos2
);
135 /** \brief Intercept output stream redirection.
136 ** \param ostr the destination output stream
137 ** \param pos a reference to the position to redirect
140 operator<< (std::ostream
& ostr
, const position
& pos
)
143 ostr
<< *pos
.filename
<< ':';
144 return ostr
<< pos
.line
<< '.' << pos
.column
;
148 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
152 b4_output_begin([b4_dir_prefix
[]location
.hh
])
153 b4_copyright([Locations
for Bison parsers in C
++],
154 [2002-2007, 2009-2012])[
157 ** \file ]b4_dir_prefix[location.hh
158 ** Define the ]b4_namespace_ref[::location class.
161 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
163 # include "position.hh"
167 /// Abstract a location.
171 ]m4_ifdef([b4_location_constructors
], [
172 /// Construct a location from \a b to \a e.
173 location (const position
& b
, const position
& e
)
179 /// Construct a 0-width location in \a p.
180 explicit location (const position
& p
= position ())
186 /// Construct a 0-width location in \a f, \a l, \a c.
187 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
188 unsigned int l
= ]b4_location_initial_line
[u
,
189 unsigned int c
= ]b4_location_initial_column
[u
)
197 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
198 unsigned int l
= ]b4_location_initial_line
[u
,
199 unsigned int c
= ]b4_location_initial_column
[u
)
201 begin
.initialize (f
, l
, c
);
205 /** \name Line and Column related manipulators
208 /// Reset initial location to final location.
214 /// Extend the current location to the COUNT next columns.
215 void columns (unsigned int count
= 1)
220 /// Extend the current location to the COUNT next lines.
221 void lines (unsigned int count
= 1)
229 /// Beginning of the located region.
231 /// End of the located region.
235 /// Join two location objects to create a location.
236 inline const location
operator+ (const location
& begin
, const location
& end
)
238 location res
= begin
;
243 /// Add two location objects.
244 inline const location
operator+ (const location
& begin
, unsigned int width
)
246 location res
= begin
;
251 /// Add and assign a location.
252 inline location
& operator+= (location
& res
, unsigned int width
)
257 ]b4_percent_define_flag_if([[define_location_comparison]], [[
258 /// Compare two location objects.
260 operator== (const location
& loc1
, const location
& loc2
)
262 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
265 /// Compare two location objects.
267 operator!= (const location
& loc1
, const location
& loc2
)
269 return !(loc1
== loc2
);
272 /** \brief Intercept output stream redirection.
273 ** \param ostr the destination output stream
274 ** \param loc a reference to the location to redirect
276 ** Avoid duplicate information.
278 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
280 position last
= loc
.end
- 1;
283 && (!loc
.begin
.filename
284 || *loc
.begin
.filename
!= *last
.filename
))
286 else if (loc
.begin
.line
!= last
.line
)
287 ostr
<< '-' << last
.line
<< '.' << last
.column
;
288 else if (loc
.begin
.column
!= last
.column
)
289 ostr
<< '-' << last
.column
;
295 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])