1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2013 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.
28 public:]m4_ifdef([b4_location_constructors
], [[
29 /// Construct a position.
30 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
31 unsigned int l
= ]b4_location_initial_line
[u
,
32 unsigned int c
= ]b4_location_initial_column
[u
)
41 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
42 unsigned int l
= ]b4_location_initial_line
[u
,
43 unsigned int c
= ]b4_location_initial_column
[u
)
50 /** \name Line and Column related manipulators
52 /// (line related) Advance to the COUNT next lines.
53 void lines (int count
= 1)
57 column
= ]b4_location_initial_column
[u
;
59 0 < count
|| -count
< line
61 : ]b4_location_initial_line
[;
65 /// (column related) Advance to the COUNT next columns.
66 void columns (int count
= 1)
69 0 < count
|| -count
< column
71 : ]b4_location_initial_column
[;
75 /// File name to which this position refers.
76 ]b4_percent_define_get([[filename_type]])[* filename
;
77 /// Current line number.
79 /// Current column number.
83 /// Add and assign a position.
85 operator+= (position
& res
, int width
)
91 /// Add two position objects.
93 operator+ (position res
, int width
)
98 /// Add and assign a position.
100 operator-= (position
& res
, int width
)
102 return res
+= -width
;
105 /// Add two position objects.
107 operator- (position res
, int width
)
111 ]b4_percent_define_flag_if([[define_location_comparison]], [[
112 /// Compare two position objects.
114 operator== (const position
& pos1
, const position
& pos2
)
116 return (pos1
.line
== pos2
.line
117 && pos1
.column
== pos2
.column
118 && (pos1
.filename
== pos2
.filename
119 || (pos1
.filename
&& pos2
.filename
120 && *pos1
.filename
== *pos2
.filename
)));
123 /// Compare two position objects.
125 operator!= (const position
& pos1
, const position
& pos2
)
127 return !(pos1
== pos2
);
130 /** \brief Intercept output stream redirection.
131 ** \param ostr the destination output stream
132 ** \param pos a reference to the position to redirect
134 template <typename YYChar
>
135 inline std::basic_ostream
<YYChar
>&
136 operator<< (std::basic_ostream
<YYChar
>& ostr
, const position
& pos
)
139 ostr
<< *pos
.filename
<< ':';
140 return ostr
<< pos
.line
<< '.' << pos
.column
;
147 m4_define([b4_location_define
],
148 [[ /// Abstract a location.
152 ]m4_ifdef([b4_location_constructors
], [
153 /// Construct a location from \a b to \a e.
154 location (const position
& b
, const position
& e
)
160 /// Construct a 0-width location in \a p.
161 explicit location (const position
& p
= position ())
167 /// Construct a 0-width location in \a f, \a l, \a c.
168 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
169 unsigned int l
= ]b4_location_initial_line
[u
,
170 unsigned int c
= ]b4_location_initial_column
[u
)
178 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
179 unsigned int l
= ]b4_location_initial_line
[u
,
180 unsigned int c
= ]b4_location_initial_column
[u
)
182 begin
.initialize (f
, l
, c
);
186 /** \name Line and Column related manipulators
189 /// Reset initial location to final location.
195 /// Extend the current location to the COUNT next columns.
196 void columns (int count
= 1)
201 /// Extend the current location to the COUNT next lines.
202 void lines (int count
= 1)
210 /// Beginning of the located region.
212 /// End of the located region.
216 /// Join two location objects to create a location.
217 inline location
operator+ (location res
, const location
& end
)
223 /// Change end position in place.
224 inline location
& operator+= (location
& res
, int width
)
230 /// Change end position.
231 inline location
operator+ (location res
, int width
)
236 /// Change end position in place.
237 inline location
& operator-= (location
& res
, int width
)
239 return res
+= -width
;
242 /// Change end position.
243 inline location
operator- (const location
& begin
, int width
)
245 return begin
+ -width
;
247 ]b4_percent_define_flag_if([[define_location_comparison]], [[
248 /// Compare two location objects.
250 operator== (const location
& loc1
, const location
& loc2
)
252 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
255 /// Compare two location objects.
257 operator!= (const location
& loc1
, const location
& loc2
)
259 return !(loc1
== loc2
);
262 /** \brief Intercept output stream redirection.
263 ** \param ostr the destination output stream
264 ** \param loc a reference to the location to redirect
266 ** Avoid duplicate information.
268 template <typename YYChar
>
269 inline std::basic_ostream
<YYChar
>&
270 operator<< (std::basic_ostream
<YYChar
>& ostr
, const location
& loc
)
272 unsigned int end_col
= 0 < loc
.end
.column
? loc
.end
.column
- 1 : 0;
273 ostr
<< loc
.begin
// << "(" << loc.end << ") "
276 && (!loc
.begin
.filename
277 || *loc
.begin
.filename
!= *loc
.end
.filename
))
278 ostr
<< '-' << loc
.end
.filename
<< ':' << loc
.end
.line
<< '.' << end_col
;
279 else if (loc
.begin
.line
< loc
.end
.line
)
280 ostr
<< '-' << loc
.end
.line
<< '.' << end_col
;
281 else if (loc
.begin
.column
< end_col
)
282 ostr
<< '-' << end_col
;
289 b4_output_begin([b4_dir_prefix
[]position
.hh
])
290 b4_copyright([Positions
for Bison parsers in C
++])[
293 ** \file ]b4_dir_prefix[position.hh
294 ** Define the ]b4_namespace_ref[::position class.
297 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
299 # include <algorithm> // std::max
308 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
312 b4_output_begin([b4_dir_prefix
[]location
.hh
])
313 b4_copyright([Locations
for Bison parsers in C
++])[
316 ** \file ]b4_dir_prefix[location.hh
317 ** Define the ]b4_namespace_ref[::location class.
320 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
322 # include "position.hh"
327 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])
332 m4_popdef([b4_copyright_years
])