1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2012 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 m4_pushdef([b4_copyright_years
],
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
++])[
29 ** Define the ]b4_namespace_ref[::position class.
32 #ifndef BISON_POSITION_HH
33 # define BISON_POSITION_HH
42 /// Abstract a position.
46 ]m4_ifdef([b4_location_constructors
], [[
47 /// Construct a position.
48 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
49 unsigned int l
= ]b4_location_initial_line
[u
,
50 unsigned int c
= ]b4_location_initial_column
[u
)
59 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
60 unsigned int l
= ]b4_location_initial_line
[u
,
61 unsigned int c
= ]b4_location_initial_column
[u
)
68 /** \name Line and Column related manipulators
70 /// (line related) Advance to the COUNT next lines.
71 void lines (int count
= 1)
73 column
= ]b4_location_initial_column
[u
;
77 /// (column related) Advance to the COUNT next columns.
78 void columns (int count
= 1)
80 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
84 /// File name to which this position refers.
85 ]b4_percent_define_get([[filename_type]])[* filename
;
86 /// Current line number.
88 /// Current column number.
92 /// Add and assign a position.
93 inline const position
&
94 operator+= (position
& res
, const int width
)
100 /// Add two position objects.
101 inline const position
102 operator+ (const position
& begin
, const int width
)
104 position res
= begin
;
108 /// Add and assign a position.
109 inline const position
&
110 operator-= (position
& res
, const int width
)
112 return res
+= -width
;
115 /// Add two position objects.
116 inline const position
117 operator- (const position
& begin
, const int width
)
119 return begin
+ -width
;
121 ]b4_percent_define_flag_if([[define_location_comparison]], [[
122 /// Compare two position objects.
124 operator== (const position
& pos1
, const position
& pos2
)
126 return (pos1
.line
== pos2
.line
127 && pos1
.column
== pos2
.column
128 && (pos1
.filename
== pos2
.filename
129 || (pos1
.filename
&& pos2
.filename
130 && *pos1
.filename
== *pos2
.filename
)));
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
++])[
159 ** Define the ]b4_namespace_ref[::location class.
162 #ifndef BISON_LOCATION_HH
163 # define BISON_LOCATION_HH
167 # include "position.hh"
171 /// Abstract a location.
175 ]m4_ifdef([b4_location_constructors
], [
176 /// Construct a location from \a b to \a e.
177 location (const position
& b
, const position
& e
)
183 /// Construct a 0-width location in \a p.
184 explicit location (const position
& p
= position ())
190 /// Construct a 0-width location in \a f, \a l, \a c.
191 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
192 unsigned int l
= ]b4_location_initial_line
[u
,
193 unsigned int c
= ]b4_location_initial_column
[u
)
201 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
202 unsigned int l
= ]b4_location_initial_line
[u
,
203 unsigned int c
= ]b4_location_initial_column
[u
)
205 begin
.initialize (f
, l
, c
);
209 /** \name Line and Column related manipulators
212 /// Reset initial location to final location.
218 /// Extend the current location to the COUNT next columns.
219 void columns (unsigned int count
= 1)
224 /// Extend the current location to the COUNT next lines.
225 void lines (unsigned int count
= 1)
233 /// Beginning of the located region.
235 /// End of the located region.
239 /// Join two location objects to create a location.
240 inline const location
operator+ (const location
& begin
, const location
& end
)
242 location res
= begin
;
247 /// Add two location objects.
248 inline const location
operator+ (const location
& begin
, unsigned int width
)
250 location res
= begin
;
255 /// Add and assign a location.
256 inline location
& operator+= (location
& res
, unsigned int width
)
261 ]b4_percent_define_flag_if([[define_location_comparison]], [[
262 /// Compare two location objects.
264 operator== (const location
& loc1
, const location
& loc2
)
266 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
269 /// Compare two location objects.
271 operator!= (const location
& loc1
, const location
& loc2
)
273 return !(loc1
== loc2
);
276 /** \brief Intercept output stream redirection.
277 ** \param ostr the destination output stream
278 ** \param loc a reference to the location to redirect
280 ** Avoid duplicate information.
282 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
284 position last
= loc
.end
- 1;
287 && (!loc
.begin
.filename
288 || *loc
.begin
.filename
!= *last
.filename
))
290 else if (loc
.begin
.line
!= last
.line
)
291 ostr
<< '-' << last
.line
<< '.' << last
.column
;
292 else if (loc
.begin
.column
!= last
.column
)
293 ostr
<< '-' << last
.column
;
299 #endif // not BISON_LOCATION_HH]
301 m4_popdef([b4_copyright_years
])dnl