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
41 namespace ]b4_namespace
[
43 /// Abstract a position.
47 ]m4_ifdef([b4_location_constructors
], [
48 /// Construct a position.
50 : filename (0), line (]b4_location_initial_line
[), column (]b4_location_initial_column
[)
56 inline void initialize (]b4_filename_type
[* fn
)
59 line
= ]b4_location_initial_line
[;
60 column
= ]b4_location_initial_column
[;
63 /** \name Line and Column related manipulators
66 /// (line related) Advance to the COUNT next lines.
67 inline void lines (int count
= 1)
69 column
= ]b4_location_initial_column
[;
73 /// (column related) Advance to the COUNT next columns.
74 inline void columns (int count
= 1)
76 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
81 /// File name to which this position refers.
82 ]b4_filename_type
[* filename
;
83 /// Current line number.
85 /// Current column number.
89 /// Add and assign a position.
90 inline const position
&
91 operator+= (position
& res
, const int width
)
97 /// Add two position objects.
99 operator+ (const position
& begin
, const int width
)
101 position res
= begin
;
105 /// Add and assign a position.
106 inline const position
&
107 operator-= (position
& res
, const int width
)
109 return res
+= -width
;
112 /// Add two position objects.
113 inline const position
114 operator- (const position
& begin
, const int width
)
116 return begin
+ -width
;
118 ]m4_if(b4_define_location_comparison
, [1], [[
119 /// Compare two position objects.
121 operator== (const position
& pos1
, const position
& pos2
)
124 (pos1
.filename
== pos2
.filename
125 || pos1
.filename
&& pos2
.filename
&& *pos1
.filename
== *pos2
.filename
)
126 && pos1
.line
== pos2
.line
&& pos1
.column
== pos2
.column
;
129 /// Compare two position objects.
131 operator!= (const position
& pos1
, const position
& pos2
)
133 return !(pos1
== pos2
);
136 /** \brief Intercept output stream redirection.
137 ** \param ostr the destination output stream
138 ** \param pos a reference to the position to redirect
141 operator<< (std::ostream
& ostr
, const position
& pos
)
144 ostr
<< *pos
.filename
<< ':';
145 return ostr
<< pos
.line
<< '.' << pos
.column
;
149 #endif // not BISON_POSITION_HH]
150 @output b4_dir_prefix
[]location
.hh
151 b4_copyright([Locations
for Bison parsers in C
++],
152 [2002, 2003, 2004, 2005, 2006])[
156 ** Define the ]b4_namespace[::location class.
159 #ifndef BISON_LOCATION_HH
160 # define BISON_LOCATION_HH
164 # include "position.hh"
166 namespace ]b4_namespace
[
169 /// Abstract a location.
173 ]m4_ifdef([b4_location_constructors
], [
174 /// Construct a location.
182 inline void initialize (]b4_filename_type
[* fn
)
184 begin
.initialize (fn
);
188 /** \name Line and Column related manipulators
191 /// Reset initial location to final location.
197 /// Extend the current location to the COUNT next columns.
198 inline void columns (unsigned int count
= 1)
203 /// Extend the current location to the COUNT next lines.
204 inline void lines (unsigned int count
= 1)
212 /// Beginning of the located region.
214 /// End of the located region.
218 /// Join two location objects to create a location.
219 inline const location
operator+ (const location
& begin
, const location
& end
)
221 location res
= begin
;
226 /// Add two location objects.
227 inline const location
operator+ (const location
& begin
, unsigned int width
)
229 location res
= begin
;
234 /// Add and assign a location.
235 inline location
& operator+= (location
& res
, unsigned int width
)
240 ]m4_if(b4_define_location_comparison
, [1], [[
241 /// Compare two location objects.
243 operator== (const location
& loc1
, const location
& loc2
)
245 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
248 /// Compare two location objects.
250 operator!= (const location
& loc1
, const location
& loc2
)
252 return !(loc1
== loc2
);
255 /** \brief Intercept output stream redirection.
256 ** \param ostr the destination output stream
257 ** \param loc a reference to the location to redirect
259 ** Avoid duplicate information.
261 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
263 position last
= loc
.end
- 1;
266 && (!loc
.begin
.filename
267 || *loc
.begin
.filename
!= *last
.filename
))
269 else if (loc
.begin
.line
!= last
.line
)
270 ostr
<< '-' << last
.line
<< '.' << last
.column
;
271 else if (loc
.begin
.column
!= last
.column
)
272 ostr
<< '-' << last
.column
;
278 #endif // not BISON_LOCATION_HH]