1 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 # Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
19 m4_pushdef([b4_copyright_years
],
20 [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009])
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
++])[
30 ** Define the ]b4_namespace_ref[::position class.
33 #ifndef BISON_POSITION_HH
34 # define BISON_POSITION_HH
41 /// Abstract a position.
45 ]m4_ifdef([b4_location_constructors
], [[
46 /// Construct a position.
47 explicit position (]b4_percent_define_get([[filename_type]])[* f
= 0,
48 unsigned int l
= ]b4_location_initial_line
[u
,
49 unsigned int c
= ]b4_location_initial_column
[u
)
58 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
61 line
= ]b4_location_initial_line
[u
;
62 column
= ]b4_location_initial_column
[u
;
65 /** \name Line and Column related manipulators
67 /// (line related) Advance to the COUNT next lines.
68 /// Set current column to initial value.
69 inline void lines (int count
= 1)
71 column
= ]b4_location_initial_column
[u
;
75 /// (column related) Advance to the COUNT next columns.
76 inline void columns (int count
= 1)
78 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
82 /// File name to which this position refers.
83 ]b4_percent_define_get([[filename_type]])[* filename
;
84 /// Current line number.
86 /// Current column number.
90 /// Add and assign a position.
91 inline const position
&
92 operator+= (position
& res
, const int width
)
98 /// Add two position objects.
100 operator+ (const position
& begin
, const int width
)
102 position res
= begin
;
106 /// Add and assign a position.
107 inline const position
&
108 operator-= (position
& res
, const int width
)
110 return res
+= -width
;
113 /// Add two position objects.
114 inline const position
115 operator- (const position
& begin
, const int width
)
117 return begin
+ -width
;
119 ]b4_percent_define_flag_if([[define_location_comparison]], [[
120 /// Compare two position objects.
122 operator== (const position
& pos1
, const position
& pos2
)
124 return (pos1
.line
== pos2
.line
125 && pos1
.column
== pos2
.column
126 && (pos1
.filename
== pos2
.filename
127 || (pos1
.filename
&& pos2
.filename
128 && *pos1
.filename
== *pos2
.filename
)));
131 /// Compare two position objects.
133 operator!= (const position
& pos1
, const position
& pos2
)
135 return !(pos1
== pos2
);
138 /** \brief Intercept output stream redirection.
139 ** \param ostr the destination output stream
140 ** \param pos a reference to the position to redirect
143 operator<< (std::ostream
& ostr
, const position
& pos
)
146 ostr
<< *pos
.filename
<< ':';
147 return ostr
<< pos
.line
<< '.' << pos
.column
;
151 #endif // not BISON_POSITION_HH]
152 @
output(b4_dir_prefix
[]location
.hh@
)@
153 b4_copyright([Locations
for Bison parsers in C
++])[
157 ** Define the ]b4_namespace_ref[::location class.
160 #ifndef BISON_LOCATION_HH
161 # define BISON_LOCATION_HH
165 # include "position.hh"
169 /// Abstract a location.
173 ]m4_ifdef([b4_location_constructors
], [
174 /// Construct a location from \a b to \a e.
175 location (const position
& b
, const position
& e
)
181 /// Construct a 0-width location in \a p.
182 explicit location (const position
& p
= position ())
188 /// Construct a 0-width location in \a f, \a l, \a c.
189 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
190 unsigned int l
= ]b4_location_initial_line
[u
,
191 unsigned int c
= ]b4_location_initial_column
[u
)
199 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn
)
201 begin
.initialize (fn
);
205 /** \name Line and Column related manipulators
208 /// Reset initial location to final location.
214 /// Extend the current location to the COUNT next columns.
215 inline void columns (unsigned int count
= 1)
220 /// Extend the current location to the COUNT next lines.
221 inline void lines (unsigned int count
= 1)
229 /// Beginning of the located region.
231 /// End of the located region.
235 /// Join two location objects to create a location.
236 inline const location
operator+ (const location
& begin
, const location
& end
)
238 location res
= begin
;
243 /// Add two location objects.
244 inline const location
operator+ (const location
& begin
, unsigned int width
)
246 location res
= begin
;
251 /// Add and assign a location.
252 inline location
& operator+= (location
& res
, unsigned int width
)
257 ]b4_percent_define_flag_if([[define_location_comparison]], [[
258 /// Compare two location objects.
260 operator== (const location
& loc1
, const location
& loc2
)
262 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
265 /// Compare two location objects.
267 operator!= (const location
& loc1
, const location
& loc2
)
269 return !(loc1
== loc2
);
272 /** \brief Intercept output stream redirection.
273 ** \param ostr the destination output stream
274 ** \param loc a reference to the location to redirect
276 ** Avoid duplicate information.
278 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
280 position last
= loc
.end
- 1;
283 && (!loc
.begin
.filename
284 || *loc
.begin
.filename
!= *last
.filename
))
286 else if (loc
.begin
.line
!= last
.line
)
287 ostr
<< '-' << last
.line
<< '.' << last
.column
;
288 else if (loc
.begin
.column
!= last
.column
)
289 ostr
<< '-' << last
.column
;
295 #endif // not BISON_LOCATION_HH]
297 m4_popdef([b4_copyright_years
])dnl