]> git.saurik.com Git - bison.git/blame - data/location.cc
c++: style: use "unsigned", not "unsigned int"
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1# C++ skeleton for Bison
2
3209eb1c 3# Copyright (C) 2002-2015 Free Software Foundation, Inc.
21667f64 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
21667f64 6# it under the terms of the GNU General Public License as published by
f16b0819 7# the Free Software Foundation, either version 3 of the License, or
21667f64 8# (at your option) any later version.
f16b0819 9#
21667f64
AD
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.
f16b0819 14#
21667f64 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
21667f64 17
a9ce3f54 18m4_pushdef([b4_copyright_years],
3209eb1c 19 [2002-2015])
a9ce3f54 20
93549bcd
AD
21# b4_position_define
22# ------------------
23# Define class position.
24m4_define([b4_position_define],
25[[ /// Abstract a position.
21667f64
AD
26 class position
27 {
75ae8299 28 public:]m4_ifdef([b4_location_constructors], [[
21667f64 29 /// Construct a position.
8d0b7cef 30 explicit position (]b4_percent_define_get([[filename_type]])[* f = YY_NULLPTR,
0d40b364
AD
31 unsigned l = ]b4_location_initial_line[u,
32 unsigned c = ]b4_location_initial_column[u)
0634493c
AD
33 : filename (f)
34 , line (l)
35 , column (c)
b809770e 36 {}
21667f64 37
0634493c 38]])[
4626a15d 39 /// Initialization.
8d0b7cef 40 void initialize (]b4_percent_define_get([[filename_type]])[* fn = YY_NULLPTR,
0d40b364
AD
41 unsigned l = ]b4_location_initial_line[u,
42 unsigned c = ]b4_location_initial_column[u)
4626a15d
AD
43 {
44 filename = fn;
936c88d1
AD
45 line = l;
46 column = c;
4626a15d 47 }
21667f64
AD
48
49 /** \name Line and Column related manipulators
50 ** \{ */
21667f64 51 /// (line related) Advance to the COUNT next lines.
2a1bd0d7 52 void lines (int count = 1)
21667f64 53 {
75ae8299
AD
54 if (count)
55 {
56 column = ]b4_location_initial_column[u;
7ba01e11 57 line = add_ (line, count, ]b4_location_initial_line[);
75ae8299 58 }
21667f64
AD
59 }
60
61 /// (column related) Advance to the COUNT next columns.
2a1bd0d7 62 void columns (int count = 1)
21667f64 63 {
7ba01e11 64 column = add_ (column, count, ]b4_location_initial_column[);
21667f64
AD
65 }
66 /** \} */
67
21667f64 68 /// File name to which this position refers.
a4e25e1d 69 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64 70 /// Current line number.
0d40b364 71 unsigned line;
21667f64 72 /// Current column number.
0d40b364 73 unsigned column;
7ba01e11
AD
74
75 private:
76 /// Compute max(min, lhs+rhs) (provided min <= lhs).
0d40b364 77 static unsigned add_ (unsigned lhs, int rhs, unsigned min)
7ba01e11 78 {
0d40b364 79 return (0 < rhs || -static_cast<unsigned>(rhs) < lhs
7ba01e11
AD
80 ? rhs + lhs
81 : min);
82 }
21667f64
AD
83 };
84
56351d4c 85 /// Add \a width columns, in place.
0e1ccdfa 86 inline position&
75ae8299 87 operator+= (position& res, int width)
21667f64
AD
88 {
89 res.columns (width);
90 return res;
91 }
92
56351d4c 93 /// Add \a width columns.
75ae8299
AD
94 inline position
95 operator+ (position res, int width)
21667f64 96 {
21667f64
AD
97 return res += width;
98 }
99
56351d4c 100 /// Subtract \a width columns, in place.
0e1ccdfa 101 inline position&
75ae8299 102 operator-= (position& res, int width)
21667f64
AD
103 {
104 return res += -width;
105 }
106
56351d4c 107 /// Subtract \a width columns.
75ae8299
AD
108 inline position
109 operator- (position res, int width)
21667f64 110 {
75ae8299 111 return res -= width;
21667f64 112 }
592d0b1e 113]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
114 /// Compare two position objects.
115 inline bool
116 operator== (const position& pos1, const position& pos2)
117 {
56f772e9
AD
118 return (pos1.line == pos2.line
119 && pos1.column == pos2.column
120 && (pos1.filename == pos2.filename
121 || (pos1.filename && pos2.filename
122 && *pos1.filename == *pos2.filename)));
31b2b07e 123 }
21667f64 124
31b2b07e
JD
125 /// Compare two position objects.
126 inline bool
127 operator!= (const position& pos1, const position& pos2)
128 {
129 return !(pos1 == pos2);
130 }
131]])[
21667f64
AD
132 /** \brief Intercept output stream redirection.
133 ** \param ostr the destination output stream
134 ** \param pos a reference to the position to redirect
135 */
7ae57e2a
AD
136 template <typename YYChar>
137 inline std::basic_ostream<YYChar>&
138 operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
21667f64
AD
139 {
140 if (pos.filename)
141 ostr << *pos.filename << ':';
142 return ostr << pos.line << '.' << pos.column;
143 }
93549bcd 144]])
21667f64 145
21667f64 146
93549bcd
AD
147# b4_location_define
148# ------------------
149m4_define([b4_location_define],
150[[ /// Abstract a location.
21667f64
AD
151 class location
152 {
21667f64 153 public:
4626a15d 154]m4_ifdef([b4_location_constructors], [
0634493c
AD
155 /// Construct a location from \a b to \a e.
156 location (const position& b, const position& e)
157 : begin (b)
158 , end (e)
b809770e 159 {}
0634493c
AD
160
161 /// Construct a 0-width location in \a p.
162 explicit location (const position& p = position ())
163 : begin (p)
164 , end (p)
b809770e 165 {}
0634493c
AD
166
167 /// Construct a 0-width location in \a f, \a l, \a c.
168 explicit location (]b4_percent_define_get([[filename_type]])[* f,
0d40b364
AD
169 unsigned l = ]b4_location_initial_line[u,
170 unsigned c = ]b4_location_initial_column[u)
0634493c
AD
171 : begin (f, l, c)
172 , end (f, l, c)
b809770e 173 {}
21667f64 174
4626a15d
AD
175])[
176 /// Initialization.
8d0b7cef 177 void initialize (]b4_percent_define_get([[filename_type]])[* f = YY_NULLPTR,
0d40b364
AD
178 unsigned l = ]b4_location_initial_line[u,
179 unsigned c = ]b4_location_initial_column[u)
4626a15d 180 {
936c88d1 181 begin.initialize (f, l, c);
4626a15d
AD
182 end = begin;
183 }
21667f64
AD
184
185 /** \name Line and Column related manipulators
186 ** \{ */
187 public:
188 /// Reset initial location to final location.
2a1bd0d7 189 void step ()
21667f64
AD
190 {
191 begin = end;
192 }
193
194 /// Extend the current location to the COUNT next columns.
75ae8299 195 void columns (int count = 1)
21667f64
AD
196 {
197 end += count;
198 }
199
200 /// Extend the current location to the COUNT next lines.
75ae8299 201 void lines (int count = 1)
21667f64
AD
202 {
203 end.lines (count);
204 }
205 /** \} */
206
207
208 public:
209 /// Beginning of the located region.
210 position begin;
211 /// End of the located region.
212 position end;
213 };
214
56351d4c
AD
215 /// Join two locations, in place.
216 inline location& operator+= (location& res, const location& end)
21667f64 217 {
21667f64
AD
218 res.end = end.end;
219 return res;
220 }
221
56351d4c
AD
222 /// Join two locations.
223 inline location operator+ (location res, const location& end)
224 {
225 return res += end;
226 }
227
228 /// Add \a width columns to the end position, in place.
75ae8299 229 inline location& operator+= (location& res, int width)
21667f64 230 {
21667f64
AD
231 res.columns (width);
232 return res;
233 }
234
56351d4c 235 /// Add \a width columns to the end position.
75ae8299 236 inline location operator+ (location res, int width)
21667f64 237 {
75ae8299
AD
238 return res += width;
239 }
240
56351d4c 241 /// Subtract \a width columns to the end position, in place.
75ae8299
AD
242 inline location& operator-= (location& res, int width)
243 {
244 return res += -width;
245 }
246
56351d4c
AD
247 /// Subtract \a width columns to the end position.
248 inline location operator- (location res, int width)
75ae8299 249 {
56351d4c 250 return res -= width;
21667f64 251 }
592d0b1e 252]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
253 /// Compare two location objects.
254 inline bool
255 operator== (const location& loc1, const location& loc2)
256 {
257 return loc1.begin == loc2.begin && loc1.end == loc2.end;
258 }
21667f64 259
31b2b07e
JD
260 /// Compare two location objects.
261 inline bool
262 operator!= (const location& loc1, const location& loc2)
263 {
264 return !(loc1 == loc2);
265 }
266]])[
21667f64
AD
267 /** \brief Intercept output stream redirection.
268 ** \param ostr the destination output stream
269 ** \param loc a reference to the location to redirect
270 **
271 ** Avoid duplicate information.
272 */
7ae57e2a
AD
273 template <typename YYChar>
274 inline std::basic_ostream<YYChar>&
275 operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
21667f64 276 {
0d40b364 277 unsigned end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
b702ec61 278 ostr << loc.begin;
aedcb6c0 279 if (loc.end.filename
e9690142 280 && (!loc.begin.filename
aedcb6c0
AD
281 || *loc.begin.filename != *loc.end.filename))
282 ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
283 else if (loc.begin.line < loc.end.line)
284 ostr << '-' << loc.end.line << '.' << end_col;
285 else if (loc.begin.column < end_col)
286 ostr << '-' << end_col;
21667f64
AD
287 return ostr;
288 }
93549bcd 289]])
21667f64 290
93549bcd 291
93549bcd 292b4_defines_if([
064e42b0 293b4_output_begin([b4_dir_prefix[]position.hh])
93549bcd
AD
294b4_copyright([Positions for Bison parsers in C++])[
295
296/**
297 ** \file ]b4_dir_prefix[position.hh
298 ** Define the ]b4_namespace_ref[::position class.
299 */
300
301]b4_cpp_guard_open([b4_dir_prefix[]position.hh])[
302
303# include <algorithm> // std::max
2b45240d 304# include <iostream>
93549bcd
AD
305# include <string>
306
307]b4_null_define[
308
309]b4_namespace_open[
310]b4_position_define[
793fbca5 311]b4_namespace_close[
93549bcd 312]b4_cpp_guard_close([b4_dir_prefix[]position.hh])
064e42b0
AD
313b4_output_end()
314
315
316b4_output_begin([b4_dir_prefix[]location.hh])
93549bcd
AD
317b4_copyright([Locations for Bison parsers in C++])[
318
319/**
320 ** \file ]b4_dir_prefix[location.hh
321 ** Define the ]b4_namespace_ref[::location class.
322 */
323
324]b4_cpp_guard_open([b4_dir_prefix[]location.hh])[
21667f64 325
93549bcd
AD
326# include "position.hh"
327
328]b4_namespace_open[
329]b4_location_define[
330]b4_namespace_close[
22172d47 331]b4_cpp_guard_close([b4_dir_prefix[]location.hh])
1c7ec959 332b4_output_end()
064e42b0
AD
333])
334
335
336m4_popdef([b4_copyright_years])