1 # C++ skeleton for Bison
3 # Copyright (C) 2002-2007, 2009-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 # We do want M4 expansion after # for CPP macros.
21 @
output(b4_dir_prefix
[]position
.hh@
)@
22 b4_copyright([Positions
for Bison parsers in C
++],
23 [2002-2007, 2009-2012])[
26 ** \file ]b4_dir_prefix[position.hh
27 ** Define the ]b4_namespace_ref[::position class.
30 ]b4_cpp_guard_open([b4_dir_prefix
[]position
.hh
])[
32 # include <algorithm> // std::max
39 /// Abstract a position.
43 ]m4_ifdef([b4_location_constructors
], [[
44 /// Construct a position.
45 explicit position (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
46 unsigned int l
= ]b4_location_initial_line
[u
,
47 unsigned int c
= ]b4_location_initial_column
[u
)
56 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
57 unsigned int l
= ]b4_location_initial_line
[u
,
58 unsigned int c
= ]b4_location_initial_column
[u
)
65 /** \name Line and Column related manipulators
67 /// (line related) Advance to the COUNT next lines.
68 void lines (int count
= 1)
70 column
= ]b4_location_initial_column
[u
;
74 /// (column related) Advance to the COUNT next columns.
75 void columns (int count
= 1)
77 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
81 /// File name to which this position refers.
82 ]b4_percent_define_get([[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 ]b4_percent_define_flag_if([[define_location_comparison]], [[
119 /// Compare two position objects.
121 operator== (const position
& pos1
, const position
& pos2
)
123 return (pos1
.line
== pos2
.line
124 && pos1
.column
== pos2
.column
125 && (pos1
.filename
== pos2
.filename
126 || (pos1
.filename
&& pos2
.filename
127 && *pos1
.filename
== *pos2
.filename
)));
130 /// Compare two position objects.
132 operator!= (const position
& pos1
, const position
& pos2
)
134 return !(pos1
== pos2
);
137 /** \brief Intercept output stream redirection.
138 ** \param ostr the destination output stream
139 ** \param pos a reference to the position to redirect
142 operator<< (std::ostream
& ostr
, const position
& pos
)
145 ostr
<< *pos
.filename
<< ':';
146 return ostr
<< pos
.line
<< '.' << pos
.column
;
150 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
151 @
output(b4_dir_prefix
[]location
.hh@
)@
152 b4_copyright([Locations
for Bison parsers in C
++],
153 [2002-2007, 2009-2012])[
156 ** \file ]b4_dir_prefix[location.hh
157 ** Define the ]b4_namespace_ref[::location class.
160 ]b4_cpp_guard_open([b4_dir_prefix
[]location
.hh
])[
162 # include "position.hh"
166 /// Abstract a location.
170 ]m4_ifdef([b4_location_constructors
], [
171 /// Construct a location from \a b to \a e.
172 location (const position
& b
, const position
& e
)
178 /// Construct a 0-width location in \a p.
179 explicit location (const position
& p
= position ())
185 /// Construct a 0-width location in \a f, \a l, \a c.
186 explicit location (]b4_percent_define_get([[filename_type]])[* f
,
187 unsigned int l
= ]b4_location_initial_line
[u
,
188 unsigned int c
= ]b4_location_initial_column
[u
)
196 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
197 unsigned int l
= ]b4_location_initial_line
[u
,
198 unsigned int c
= ]b4_location_initial_column
[u
)
200 begin
.initialize (f
, l
, c
);
204 /** \name Line and Column related manipulators
207 /// Reset initial location to final location.
213 /// Extend the current location to the COUNT next columns.
214 void columns (unsigned int count
= 1)
219 /// Extend the current location to the COUNT next lines.
220 void lines (unsigned int count
= 1)
228 /// Beginning of the located region.
230 /// End of the located region.
234 /// Join two location objects to create a location.
235 inline const location
operator+ (const location
& begin
, const location
& end
)
237 location res
= begin
;
242 /// Add two location objects.
243 inline const location
operator+ (const location
& begin
, unsigned int width
)
245 location res
= begin
;
250 /// Add and assign a location.
251 inline location
& operator+= (location
& res
, unsigned int width
)
256 ]b4_percent_define_flag_if([[define_location_comparison]], [[
257 /// Compare two location objects.
259 operator== (const location
& loc1
, const location
& loc2
)
261 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
264 /// Compare two location objects.
266 operator!= (const location
& loc1
, const location
& loc2
)
268 return !(loc1
== loc2
);
271 /** \brief Intercept output stream redirection.
272 ** \param ostr the destination output stream
273 ** \param loc a reference to the location to redirect
275 ** Avoid duplicate information.
277 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
279 position last
= loc
.end
- 1;
282 && (!loc
.begin
.filename
283 || *loc
.begin
.filename
!= *last
.filename
))
285 else if (loc
.begin
.line
!= last
.line
)
286 ostr
<< '-' << last
.line
<< '.' << last
.column
;
287 else if (loc
.begin
.column
!= last
.column
)
288 ostr
<< '-' << last
.column
;
294 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])