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