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