]> git.saurik.com Git - bison.git/blame - data/location.cc
Merge remote-tracking branch 'origin/maint'
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1# C++ skeleton for Bison
2
34136e65 3# Copyright (C) 2002-2012 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],
34136e65 19 [2002-2012])
a9ce3f54 20
21667f64
AD
21# We do want M4 expansion after # for CPP macros.
22m4_changecom()
08af01c2 23m4_divert_push(0)dnl
59c544c2 24@output(b4_dir_prefix[]position.hh@)@
a9ce3f54 25b4_copyright([Positions for Bison parsers in C++])[
21667f64
AD
26
27/**
03f1b545 28 ** \file ]b4_dir_prefix[position.hh
793fbca5 29 ** Define the ]b4_namespace_ref[::position class.
21667f64
AD
30 */
31
22172d47 32]b4_cpp_guard_open([b4_dir_prefix[]position.hh])[
21667f64
AD
33
34# include <iostream>
35# include <string>
cd48d21d 36# include <algorithm>
21667f64 37
ef51bfa7
AD
38]b4_null_define[
39
793fbca5 40]b4_namespace_open[
21667f64
AD
41 /// Abstract a position.
42 class position
43 {
21667f64 44 public:
0634493c 45]m4_ifdef([b4_location_constructors], [[
21667f64 46 /// Construct a position.
2091988f 47 explicit position (]b4_percent_define_get([[filename_type]])[* f = YY_NULL,
0634493c
AD
48 unsigned int l = ]b4_location_initial_line[u,
49 unsigned int c = ]b4_location_initial_column[u)
50 : filename (f)
51 , line (l)
52 , column (c)
21667f64
AD
53 {
54 }
21667f64 55
0634493c 56]])[
4626a15d 57 /// Initialization.
936c88d1
AD
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)
4626a15d
AD
61 {
62 filename = fn;
936c88d1
AD
63 line = l;
64 column = c;
4626a15d 65 }
21667f64
AD
66
67 /** \name Line and Column related manipulators
68 ** \{ */
21667f64 69 /// (line related) Advance to the COUNT next lines.
2a1bd0d7 70 void lines (int count = 1)
21667f64 71 {
0634493c 72 column = ]b4_location_initial_column[u;
21667f64
AD
73 line += count;
74 }
75
76 /// (column related) Advance to the COUNT next columns.
2a1bd0d7 77 void columns (int count = 1)
21667f64 78 {
cd48d21d 79 column = std::max (]b4_location_initial_column[u, column + count);
21667f64
AD
80 }
81 /** \} */
82
21667f64 83 /// File name to which this position refers.
a4e25e1d 84 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64
AD
85 /// Current line number.
86 unsigned int line;
87 /// Current column number.
88 unsigned int column;
89 };
90
91 /// Add and assign a position.
92 inline const position&
93 operator+= (position& res, const int width)
94 {
95 res.columns (width);
96 return res;
97 }
98
99 /// Add two position objects.
100 inline const position
101 operator+ (const position& begin, const int width)
102 {
103 position res = begin;
104 return res += width;
105 }
106
107 /// Add and assign a position.
108 inline const position&
109 operator-= (position& res, const int width)
110 {
111 return res += -width;
112 }
113
114 /// Add two position objects.
115 inline const position
116 operator- (const position& begin, const int width)
117 {
118 return begin + -width;
119 }
592d0b1e 120]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
121 /// Compare two position objects.
122 inline bool
123 operator== (const position& pos1, const position& pos2)
124 {
56f772e9
AD
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)));
31b2b07e 130 }
21667f64 131
31b2b07e
JD
132 /// Compare two position objects.
133 inline bool
134 operator!= (const position& pos1, const position& pos2)
135 {
136 return !(pos1 == pos2);
137 }
138]])[
21667f64
AD
139 /** \brief Intercept output stream redirection.
140 ** \param ostr the destination output stream
141 ** \param pos a reference to the position to redirect
142 */
143 inline std::ostream&
144 operator<< (std::ostream& ostr, const position& pos)
145 {
146 if (pos.filename)
147 ostr << *pos.filename << ':';
148 return ostr << pos.line << '.' << pos.column;
149 }
150
793fbca5 151]b4_namespace_close[
22172d47 152]b4_cpp_guard_close([b4_dir_prefix[]position.hh])
59c544c2 153@output(b4_dir_prefix[]location.hh@)@
a9ce3f54 154b4_copyright([Locations for Bison parsers in C++])[
21667f64
AD
155
156/**
03f1b545 157 ** \file ]b4_dir_prefix[location.hh
793fbca5 158 ** Define the ]b4_namespace_ref[::location class.
21667f64
AD
159 */
160
22172d47 161]b4_cpp_guard_open([b4_dir_prefix[]location.hh])[
21667f64
AD
162
163# include <iostream>
164# include <string>
165# include "position.hh"
166
793fbca5 167]b4_namespace_open[
21667f64
AD
168
169 /// Abstract a location.
170 class location
171 {
21667f64 172 public:
4626a15d 173]m4_ifdef([b4_location_constructors], [
0634493c
AD
174 /// Construct a location from \a b to \a e.
175 location (const position& b, const position& e)
176 : begin (b)
177 , end (e)
178 {
179 }
180
181 /// Construct a 0-width location in \a p.
182 explicit location (const position& p = position ())
183 : begin (p)
184 , end (p)
185 {
186 }
187
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)
192 : begin (f, l, c)
193 , end (f, l, c)
21667f64
AD
194 {
195 }
21667f64 196
4626a15d
AD
197])[
198 /// Initialization.
936c88d1
AD
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)
4626a15d 202 {
936c88d1 203 begin.initialize (f, l, c);
4626a15d
AD
204 end = begin;
205 }
21667f64
AD
206
207 /** \name Line and Column related manipulators
208 ** \{ */
209 public:
210 /// Reset initial location to final location.
2a1bd0d7 211 void step ()
21667f64
AD
212 {
213 begin = end;
214 }
215
216 /// Extend the current location to the COUNT next columns.
2a1bd0d7 217 void columns (unsigned int count = 1)
21667f64
AD
218 {
219 end += count;
220 }
221
222 /// Extend the current location to the COUNT next lines.
2a1bd0d7 223 void lines (unsigned int count = 1)
21667f64
AD
224 {
225 end.lines (count);
226 }
227 /** \} */
228
229
230 public:
231 /// Beginning of the located region.
232 position begin;
233 /// End of the located region.
234 position end;
235 };
236
237 /// Join two location objects to create a location.
238 inline const location operator+ (const location& begin, const location& end)
239 {
240 location res = begin;
241 res.end = end.end;
242 return res;
243 }
244
245 /// Add two location objects.
246 inline const location operator+ (const location& begin, unsigned int width)
247 {
248 location res = begin;
249 res.columns (width);
250 return res;
251 }
252
253 /// Add and assign a location.
254 inline location& operator+= (location& res, unsigned int width)
255 {
256 res.columns (width);
257 return res;
258 }
592d0b1e 259]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
260 /// Compare two location objects.
261 inline bool
262 operator== (const location& loc1, const location& loc2)
263 {
264 return loc1.begin == loc2.begin && loc1.end == loc2.end;
265 }
21667f64 266
31b2b07e
JD
267 /// Compare two location objects.
268 inline bool
269 operator!= (const location& loc1, const location& loc2)
270 {
271 return !(loc1 == loc2);
272 }
273]])[
21667f64
AD
274 /** \brief Intercept output stream redirection.
275 ** \param ostr the destination output stream
276 ** \param loc a reference to the location to redirect
277 **
278 ** Avoid duplicate information.
279 */
280 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
281 {
282 position last = loc.end - 1;
283 ostr << loc.begin;
284 if (last.filename
e9690142
JD
285 && (!loc.begin.filename
286 || *loc.begin.filename != *last.filename))
21667f64
AD
287 ostr << '-' << last;
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;
292 return ostr;
293 }
294
793fbca5 295]b4_namespace_close[
21667f64 296
22172d47 297]b4_cpp_guard_close([b4_dir_prefix[]location.hh])
08af01c2 298m4_divert_pop(0)
a9ce3f54 299m4_popdef([b4_copyright_years])dnl
21667f64 300m4_changecom([#])