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