3 # C++ skeleton for Bison
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # We do want M4 expansion after # for CPP macros.
25 @output b4_dir_prefix
[]position
.hh
26 b4_copyright([Positions
for Bison parsers in C
++],
27 [2002, 2003, 2004, 2005, 2006])[
31 ** Define the ]b4_namespace[::position class.
34 #ifndef BISON_POSITION_HH
35 # define BISON_POSITION_HH
40 namespace ]b4_namespace
[
42 /// Abstract a position.
46 ]m4_ifdef([b4_location_constructors
], [
47 /// Construct a position.
49 : filename (0), line (1), column (0)
55 inline void initialize (]b4_filename_type
[* fn
)
62 /** \name Line and Column related manipulators
65 /// (line related) Advance to the COUNT next lines.
66 inline void lines (int count
= 1)
72 /// (column related) Advance to the COUNT next columns.
73 inline void columns (int count
= 1)
77 if (leftmost
<= current
+ count
)
85 /// File name to which this position refers.
86 ]b4_filename_type
[* filename
;
87 /// Current line number.
89 /// Current column number.
93 /// Add and assign a position.
94 inline const position
&
95 operator+= (position
& res
, const int width
)
101 /// Add two position objects.
102 inline const position
103 operator+ (const position
& begin
, const int width
)
105 position res
= begin
;
109 /// Add and assign a position.
110 inline const position
&
111 operator-= (position
& res
, const int width
)
113 return res
+= -width
;
116 /// Add two position objects.
117 inline const position
118 operator- (const position
& begin
, const int width
)
120 return begin
+ -width
;
122 ]m4_if(b4_define_location_comparison
, [1], [[
123 /// Compare two position objects.
125 operator== (const position
& pos1
, const position
& pos2
)
128 (pos1
.filename
== pos2
.filename
129 || pos1
.filename
&& pos2
.filename
&& *pos1
.filename
== *pos2
.filename
)
130 && pos1
.line
== pos2
.line
&& pos1
.column
== pos2
.column
;
133 /// Compare two position objects.
135 operator!= (const position
& pos1
, const position
& pos2
)
137 return !(pos1
== pos2
);
140 /** \brief Intercept output stream redirection.
141 ** \param ostr the destination output stream
142 ** \param pos a reference to the position to redirect
145 operator<< (std::ostream
& ostr
, const position
& pos
)
148 ostr
<< *pos
.filename
<< ':';
149 return ostr
<< pos
.line
<< '.' << pos
.column
;
153 #endif // not BISON_POSITION_HH]
154 @output b4_dir_prefix
[]location
.hh
155 b4_copyright([Locations
for Bison parsers in C
++],
156 [2002, 2003, 2004, 2005, 2006])[
160 ** Define the ]b4_namespace[::location class.
163 #ifndef BISON_LOCATION_HH
164 # define BISON_LOCATION_HH
168 # include "position.hh"
170 namespace ]b4_namespace
[
173 /// Abstract a location.
177 ]m4_ifdef([b4_location_constructors
], [
178 /// Construct a location.
186 inline void initialize (]b4_filename_type
[* fn
)
188 begin
.initialize (fn
);
192 /** \name Line and Column related manipulators
195 /// Reset initial location to final location.
201 /// Extend the current location to the COUNT next columns.
202 inline void columns (unsigned int count
= 1)
207 /// Extend the current location to the COUNT next lines.
208 inline void lines (unsigned int count
= 1)
216 /// Beginning of the located region.
218 /// End of the located region.
222 /// Join two location objects to create a location.
223 inline const location
operator+ (const location
& begin
, const location
& end
)
225 location res
= begin
;
230 /// Add two location objects.
231 inline const location
operator+ (const location
& begin
, unsigned int width
)
233 location res
= begin
;
238 /// Add and assign a location.
239 inline location
& operator+= (location
& res
, unsigned int width
)
244 ]m4_if(b4_define_location_comparison
, [1], [[
245 /// Compare two location objects.
247 operator== (const location
& loc1
, const location
& loc2
)
249 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
252 /// Compare two location objects.
254 operator!= (const location
& loc1
, const location
& loc2
)
256 return !(loc1
== loc2
);
259 /** \brief Intercept output stream redirection.
260 ** \param ostr the destination output stream
261 ** \param loc a reference to the location to redirect
263 ** Avoid duplicate information.
265 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
267 position last
= loc
.end
- 1;
270 && (!loc
.begin
.filename
271 || *loc
.begin
.filename
!= *last
.filename
))
273 else if (loc
.begin
.line
!= last
.line
)
274 ostr
<< '-' << last
.line
<< '.' << last
.column
;
275 else if (loc
.begin
.column
!= last
.column
)
276 ostr
<< '-' << last
.column
;
282 #endif // not BISON_LOCATION_HH]