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