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
++])[
28 ** \file ]b4_dir_prefix[position.hh
29 ** Define the ]b4_namespace_ref[::position class.
32 ]b4_cpp_guard_open([b4_dir_prefix
[]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
= YY_NULL
,
48 unsigned int l
= ]b4_location_initial_line
[u
,
49 unsigned int c
= ]b4_location_initial_column
[u
)
58 void initialize (]b4_percent_define_get([[filename_type]])[* fn
= YY_NULL
,
59 unsigned int l
= ]b4_location_initial_line
[u
,
60 unsigned int c
= ]b4_location_initial_column
[u
)
67 /** \name Line and Column related manipulators
69 /// (line related) Advance to the COUNT next lines.
70 void lines (int count
= 1)
72 column
= ]b4_location_initial_column
[u
;
76 /// (column related) Advance to the COUNT next columns.
77 void columns (int count
= 1)
79 column
= std::max (]b4_location_initial_column
[u
, column
+ count
);
83 /// File name to which this position refers.
84 ]b4_percent_define_get([[filename_type]])[* filename
;
85 /// Current line number.
87 /// Current column number.
91 /// Add and assign a position.
92 inline const position
&
93 operator+= (position
& res
, const int width
)
99 /// Add two position objects.
100 inline const position
101 operator+ (const position
& begin
, const int width
)
103 position res
= begin
;
107 /// Add and assign a position.
108 inline const position
&
109 operator-= (position
& res
, const int width
)
111 return res
+= -width
;
114 /// Add two position objects.
115 inline const position
116 operator- (const position
& begin
, const int width
)
118 return begin
+ -width
;
120 ]b4_percent_define_flag_if([[define_location_comparison]], [[
121 /// Compare two position objects.
123 operator== (const position
& pos1
, const position
& pos2
)
125 return (pos1
.line
== pos2
.line
126 && pos1
.column
== pos2
.column
127 && (pos1
.filename
== pos2
.filename
128 || (pos1
.filename
&& pos2
.filename
129 && *pos1
.filename
== *pos2
.filename
)));
132 /// Compare two position objects.
134 operator!= (const position
& pos1
, const position
& pos2
)
136 return !(pos1
== pos2
);
139 /** \brief Intercept output stream redirection.
140 ** \param ostr the destination output stream
141 ** \param pos a reference to the position to redirect
144 operator<< (std::ostream
& ostr
, const position
& pos
)
147 ostr
<< *pos
.filename
<< ':';
148 return ostr
<< pos
.line
<< '.' << pos
.column
;
152 ]b4_cpp_guard_close([b4_dir_prefix
[]position
.hh
])
153 @
output(b4_dir_prefix
[]location
.hh@
)@
154 b4_copyright([Locations
for Bison parsers in C
++])[
157 ** \file ]b4_dir_prefix[location.hh
158 ** Define the ]b4_namespace_ref[::location class.
161 ]b4_cpp_guard_open([b4_dir_prefix
[]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 void initialize (]b4_percent_define_get([[filename_type]])[* f
= YY_NULL
,
200 unsigned int l
= ]b4_location_initial_line
[u
,
201 unsigned int c
= ]b4_location_initial_column
[u
)
203 begin
.initialize (f
, l
, c
);
207 /** \name Line and Column related manipulators
210 /// Reset initial location to final location.
216 /// Extend the current location to the COUNT next columns.
217 void columns (unsigned int count
= 1)
222 /// Extend the current location to the COUNT next lines.
223 void lines (unsigned int count
= 1)
231 /// Beginning of the located region.
233 /// End of the located region.
237 /// Join two location objects to create a location.
238 inline const location
operator+ (const location
& begin
, const location
& end
)
240 location res
= begin
;
245 /// Add two location objects.
246 inline const location
operator+ (const location
& begin
, unsigned int width
)
248 location res
= begin
;
253 /// Add and assign a location.
254 inline location
& operator+= (location
& res
, unsigned int width
)
259 ]b4_percent_define_flag_if([[define_location_comparison]], [[
260 /// Compare two location objects.
262 operator== (const location
& loc1
, const location
& loc2
)
264 return loc1
.begin
== loc2
.begin
&& loc1
.end
== loc2
.end
;
267 /// Compare two location objects.
269 operator!= (const location
& loc1
, const location
& loc2
)
271 return !(loc1
== loc2
);
274 /** \brief Intercept output stream redirection.
275 ** \param ostr the destination output stream
276 ** \param loc a reference to the location to redirect
278 ** Avoid duplicate information.
280 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
282 position last
= loc
.end
- 1;
285 && (!loc
.begin
.filename
286 || *loc
.begin
.filename
!= *last
.filename
))
288 else if (loc
.begin
.line
!= last
.line
)
289 ostr
<< '-' << last
.line
<< '.' << last
.column
;
290 else if (loc
.begin
.column
!= last
.column
)
291 ostr
<< '-' << last
.column
;
297 ]b4_cpp_guard_close([b4_dir_prefix
[]location
.hh
])
299 m4_popdef([b4_copyright_years
])dnl