1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2007, 2009-2011 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-2011])[
27 ** Define the ]b4_namespace_ref[::position class.
30 #ifndef BISON_POSITION_HH
31 # define BISON_POSITION_HH
38 /// Abstract a position.
42 ]m4_ifdef([b4_location_constructors
], [
43 /// Construct a position.
45 : filename (0), line (]b4_location_initial_line
[), column (]b4_location_initial_column
[)
51 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
54 line
= ]b4_location_initial_line
[;
55 column
= ]b4_location_initial_column
[;
58 /** \name Line and Column related manipulators
61 /// (line related) Advance to the COUNT next lines.
62 inline void lines (int count
= 1)
64 column
= ]b4_location_initial_column
[;
68 /// (column related) Advance to the COUNT next columns.
69 inline void columns (int count
= 1)
71 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
76 /// File name to which this position refers.
77 ]b4_percent_define_get([[filename_type]])[* filename
;
78 /// Current line number.
80 /// Current column number.
84 /// Add and assign a position.
85 inline const position
&
86 operator+= (position
& res
, const int width
)
92 /// Add two position objects.
94 operator+ (const position
& begin
, const int width
)
100 /// Add and assign a position.
101 inline const position
&
102 operator-= (position
& res
, const int width
)
104 return res
+= -width
;
107 /// Add two position objects.
108 inline const position
109 operator- (const position
& begin
, const int width
)
111 return begin
+ -width
;
113 ]b4_percent_define_flag_if([[define_location_comparison]], [[
114 /// Compare two position objects.
116 operator== (const position
& pos1
, const position
& pos2
)
118 return (pos1
.line
== pos2
.line
119 && pos1
.column
== pos2
.column
120 && (pos1
.filename
== pos2
.filename
121 || (pos1
.filename
&& pos2
.filename
122 && *pos1
.filename
== *pos2
.filename
)));
125 /// Compare two position objects.
127 operator!= (const position
& pos1
, const position
& pos2
)
129 return !(pos1
== pos2
);
132 /** \brief Intercept output stream redirection.
133 ** \param ostr the destination output stream
134 ** \param pos a reference to the position to redirect
137 operator<< (std::ostream
& ostr
, const position
& pos
)
140 ostr
<< *pos
.filename
<< ':';
141 return ostr
<< pos
.line
<< '.' << pos
.column
;
145 #endif // not BISON_POSITION_HH]
146 @
output(b4_dir_prefix
[]location
.hh@
)@
147 b4_copyright([Locations
for Bison parsers in C
++],
148 [2002-2007, 2009-2011])[
152 ** Define the ]b4_namespace_ref[::location class.
155 #ifndef BISON_LOCATION_HH
156 # define BISON_LOCATION_HH
160 # include "position.hh"
164 /// Abstract a location.
168 ]m4_ifdef([b4_location_constructors
], [
169 /// Construct a location.
177 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
179 begin
.initialize (fn
);
183 /** \name Line and Column related manipulators
186 /// Reset initial location to final location.
192 /// Extend the current location to the COUNT next columns.
193 inline void columns (unsigned int count
= 1)
198 /// Extend the current location to the COUNT next lines.
199 inline void lines (unsigned int count
= 1)
207 /// Beginning of the located region.
209 /// End of the located region.
213 /// Join two location objects to create a location.
214 inline const location
operator+ (const location
& begin
, const location
& end
)
216 location res
= begin
;
221 /// Add two location objects.
222 inline const location
operator+ (const location
& begin
, unsigned int width
)
224 location res
= begin
;
229 /// Add and assign a location.
230 inline location
& operator+= (location
& res
, unsigned int width
)
235 ]b4_percent_define_flag_if([[define_location_comparison]], [[
236 /// Compare two location objects.
238 operator== (const location
& loc1
, const location
& loc2
)
240 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
243 /// Compare two location objects.
245 operator!= (const location
& loc1
, const location
& loc2
)
247 return !(loc1
== loc2
);
250 /** \brief Intercept output stream redirection.
251 ** \param ostr the destination output stream
252 ** \param loc a reference to the location to redirect
254 ** Avoid duplicate information.
256 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
258 position last
= loc
.end
- 1;
261 && (!loc
.begin
.filename
262 || *loc
.begin
.filename
!= *last
.filename
))
264 else if (loc
.begin
.line
!= last
.line
)
265 ostr
<< '-' << last
.line
<< '.' << last
.column
;
266 else if (loc
.begin
.column
!= last
.column
)
267 ostr
<< '-' << last
.column
;
273 #endif // not BISON_LOCATION_HH]