]> git.saurik.com Git - bison.git/blob - data/location.cc
muscle: enforce definition syntax for keyword variables
[bison.git] / data / location.cc
1 # C++ skeleton for Bison
2
3 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
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.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 m4_pushdef([b4_copyright_years],
19 [2002-2013])
20
21 # b4_position_define
22 # ------------------
23 # Define class position.
24 m4_define([b4_position_define],
25 [[ /// Abstract a position.
26 class position
27 {
28 public:]m4_ifdef([b4_location_constructors], [[
29 /// Construct a position.
30 explicit position (]b4_percent_define_get([[filename_type]])[* f = YY_NULL,
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)
36 {
37 }
38
39 ]])[
40 /// Initialization.
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)
44 {
45 filename = fn;
46 line = l;
47 column = c;
48 }
49
50 /** \name Line and Column related manipulators
51 ** \{ */
52 /// (line related) Advance to the COUNT next lines.
53 void lines (int count = 1)
54 {
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 }
63 }
64
65 /// (column related) Advance to the COUNT next columns.
66 void columns (int count = 1)
67 {
68 column =
69 0 < count || -count < column
70 ? column + count
71 : ]b4_location_initial_column[;
72 }
73 /** \} */
74
75 /// File name to which this position refers.
76 ]b4_percent_define_get([[filename_type]])[* filename;
77 /// Current line number.
78 unsigned int line;
79 /// Current column number.
80 unsigned int column;
81 };
82
83 /// Add and assign a position.
84 inline position&
85 operator+= (position& res, int width)
86 {
87 res.columns (width);
88 return res;
89 }
90
91 /// Add two position objects.
92 inline position
93 operator+ (position res, int width)
94 {
95 return res += width;
96 }
97
98 /// Add and assign a position.
99 inline position&
100 operator-= (position& res, int width)
101 {
102 return res += -width;
103 }
104
105 /// Add two position objects.
106 inline position
107 operator- (position res, int width)
108 {
109 return res -= width;
110 }
111 ]b4_percent_define_flag_if([[define_location_comparison]], [[
112 /// Compare two position objects.
113 inline bool
114 operator== (const position& pos1, const position& pos2)
115 {
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)));
121 }
122
123 /// Compare two position objects.
124 inline bool
125 operator!= (const position& pos1, const position& pos2)
126 {
127 return !(pos1 == pos2);
128 }
129 ]])[
130 /** \brief Intercept output stream redirection.
131 ** \param ostr the destination output stream
132 ** \param pos a reference to the position to redirect
133 */
134 template <typename YYChar>
135 inline std::basic_ostream<YYChar>&
136 operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
137 {
138 if (pos.filename)
139 ostr << *pos.filename << ':';
140 return ostr << pos.line << '.' << pos.column;
141 }
142 ]])
143
144
145 # b4_location_define
146 # ------------------
147 m4_define([b4_location_define],
148 [[ /// Abstract a location.
149 class location
150 {
151 public:
152 ]m4_ifdef([b4_location_constructors], [
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)
173 {
174 }
175
176 ])[
177 /// Initialization.
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)
181 {
182 begin.initialize (f, l, c);
183 end = begin;
184 }
185
186 /** \name Line and Column related manipulators
187 ** \{ */
188 public:
189 /// Reset initial location to final location.
190 void step ()
191 {
192 begin = end;
193 }
194
195 /// Extend the current location to the COUNT next columns.
196 void columns (int count = 1)
197 {
198 end += count;
199 }
200
201 /// Extend the current location to the COUNT next lines.
202 void lines (int count = 1)
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.
217 inline location operator+ (location res, const location& end)
218 {
219 res.end = end.end;
220 return res;
221 }
222
223 /// Change end position in place.
224 inline location& operator+= (location& res, int width)
225 {
226 res.columns (width);
227 return res;
228 }
229
230 /// Change end position.
231 inline location operator+ (location res, int width)
232 {
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;
246 }
247 ]b4_percent_define_flag_if([[define_location_comparison]], [[
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 }
254
255 /// Compare two location objects.
256 inline bool
257 operator!= (const location& loc1, const location& loc2)
258 {
259 return !(loc1 == loc2);
260 }
261 ]])[
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 */
268 template <typename YYChar>
269 inline std::basic_ostream<YYChar>&
270 operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
271 {
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
276 && (!loc.begin.filename
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;
283 return ostr;
284 }
285 ]])
286
287
288 b4_defines_if([
289 b4_output_begin([b4_dir_prefix[]position.hh])
290 b4_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
300 # include <iostream>
301 # include <string>
302
303 ]b4_null_define[
304
305 ]b4_namespace_open[
306 ]b4_position_define[
307 ]b4_namespace_close[
308 ]b4_cpp_guard_close([b4_dir_prefix[]position.hh])
309 b4_output_end()
310
311
312 b4_output_begin([b4_dir_prefix[]location.hh])
313 b4_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])[
321
322 # include "position.hh"
323
324 ]b4_namespace_open[
325 ]b4_location_define[
326 ]b4_namespace_close[
327 ]b4_cpp_guard_close([b4_dir_prefix[]location.hh])
328 b4_output_end()
329 ])
330
331
332 m4_popdef([b4_copyright_years])