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 2 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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # We do want M4 expansion after # for CPP macros.
24 @
output(b4_dir_prefix
[]position
.hh@
)
25 b4_copyright([Positions
for Bison parsers in C
++],
26 [2002, 2003, 2004, 2005, 2006])[
30 ** Define the ]b4_percent_define_get([[namespace]])[::position class.
33 #ifndef BISON_POSITION_HH
34 # define BISON_POSITION_HH
40 namespace ]b4_percent_define_get([[namespace]])[
42 /// Abstract a position.
46 ]m4_ifdef([b4_location_constructors
], [
47 /// Construct a position.
49 : filename (0), line (]b4_location_initial_line
[), column (]b4_location_initial_column
[)
55 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
58 line
= ]b4_location_initial_line
[;
59 column
= ]b4_location_initial_column
[;
62 /** \name Line and Column related manipulators
65 /// (line related) Advance to the COUNT next lines.
66 inline void lines (int count
= 1)
68 column
= ]b4_location_initial_column
[;
72 /// (column related) Advance to the COUNT next columns.
73 inline void columns (int count
= 1)
75 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
80 /// File name to which this position refers.
81 ]b4_percent_define_get([[filename_type]])[* filename
;
82 /// Current line number.
84 /// Current column number.
88 /// Add and assign a position.
89 inline const position
&
90 operator+= (position
& res
, const int width
)
96 /// Add two position objects.
98 operator+ (const position
& begin
, const int width
)
100 position res
= begin
;
104 /// Add and assign a position.
105 inline const position
&
106 operator-= (position
& res
, const int width
)
108 return res
+= -width
;
111 /// Add two position objects.
112 inline const position
113 operator- (const position
& begin
, const int width
)
115 return begin
+ -width
;
117 ]b4_percent_define_flag_if([[define_location_comparison]], [[
118 /// Compare two position objects.
120 operator== (const position
& pos1
, const position
& pos2
)
123 (pos1
.filename
== pos2
.filename
124 || pos1
.filename
&& pos2
.filename
&& *pos1
.filename
== *pos2
.filename
)
125 && pos1
.line
== pos2
.line
&& pos1
.column
== pos2
.column
;
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 #endif // not BISON_POSITION_HH]
149 @
output(b4_dir_prefix
[]location
.hh@
)
150 b4_copyright([Locations
for Bison parsers in C
++],
151 [2002, 2003, 2004, 2005, 2006])[
155 ** Define the ]b4_percent_define_get([[namespace]])[::location class.
158 #ifndef BISON_LOCATION_HH
159 # define BISON_LOCATION_HH
163 # include "position.hh"
165 namespace ]b4_percent_define_get([[namespace]])[
168 /// Abstract a location.
172 ]m4_ifdef([b4_location_constructors
], [
173 /// Construct a location.
181 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
183 begin
.initialize (fn
);
187 /** \name Line and Column related manipulators
190 /// Reset initial location to final location.
196 /// Extend the current location to the COUNT next columns.
197 inline void columns (unsigned int count
= 1)
202 /// Extend the current location to the COUNT next lines.
203 inline void lines (unsigned int count
= 1)
211 /// Beginning of the located region.
213 /// End of the located region.
217 /// Join two location objects to create a location.
218 inline const location
operator+ (const location
& begin
, const location
& end
)
220 location res
= begin
;
225 /// Add two location objects.
226 inline const location
operator+ (const location
& begin
, unsigned int width
)
228 location res
= begin
;
233 /// Add and assign a location.
234 inline location
& operator+= (location
& res
, unsigned int width
)
239 ]b4_percent_define_flag_if([[define_location_comparison]], [[
240 /// Compare two location objects.
242 operator== (const location
& loc1
, const location
& loc2
)
244 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
247 /// Compare two location objects.
249 operator!= (const location
& loc1
, const location
& loc2
)
251 return !(loc1
== loc2
);
254 /** \brief Intercept output stream redirection.
255 ** \param ostr the destination output stream
256 ** \param loc a reference to the location to redirect
258 ** Avoid duplicate information.
260 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
262 position last
= loc
.end
- 1;
265 && (!loc
.begin
.filename
266 || *loc
.begin
.filename
!= *last
.filename
))
268 else if (loc
.begin
.line
!= last
.line
)
269 ostr
<< '-' << last
.line
<< '.' << last
.column
;
270 else if (loc
.begin
.column
!= last
.column
)
271 ostr
<< '-' << last
.column
;
277 #endif // not BISON_LOCATION_HH]