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