]> git.saurik.com Git - bison.git/blob - data/location.cc
Add comparison operators for C++ location classes. Discussed at
[bison.git] / data / location.cc
1 m4_divert(-1)
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301 USA
21
22 # We do want M4 expansion after # for CPP macros.
23 m4_changecom()
24 m4_divert(0)dnl
25 @output b4_dir_prefix[]position.hh
26 b4_copyright([Positions for Bison parsers in C++],
27 [2002, 2003, 2004, 2005, 2006])[
28
29 /**
30 ** \file position.hh
31 ** Define the ]b4_namespace[::position class.
32 */
33
34 #ifndef BISON_POSITION_HH
35 # define BISON_POSITION_HH
36
37 # include <iostream>
38 # include <string>
39
40 namespace ]b4_namespace[
41 {
42 /// Abstract a position.
43 class position
44 {
45 public:
46 ]m4_ifdef([b4_location_constructors], [
47 /// Construct a position.
48 position ()
49 : filename (0), line (1), column (0)
50 {
51 }
52
53 ])[
54 /// Initialization.
55 inline void initialize (]b4_filename_type[* fn)
56 {
57 filename = fn;
58 line = 1;
59 column = 0;
60 }
61
62 /** \name Line and Column related manipulators
63 ** \{ */
64 public:
65 /// (line related) Advance to the COUNT next lines.
66 inline void lines (int count = 1)
67 {
68 column = 0;
69 line += count;
70 }
71
72 /// (column related) Advance to the COUNT next columns.
73 inline void columns (int count = 1)
74 {
75 int leftmost = 0;
76 int current = column;
77 if (leftmost <= current + count)
78 column += count;
79 else
80 column = 0;
81 }
82 /** \} */
83
84 public:
85 /// File name to which this position refers.
86 ]b4_filename_type[* filename;
87 /// Current line number.
88 unsigned int line;
89 /// Current column number.
90 unsigned int column;
91 };
92
93 /// Add and assign a position.
94 inline const position&
95 operator+= (position& res, const int width)
96 {
97 res.columns (width);
98 return res;
99 }
100
101 /// Add two position objects.
102 inline const position
103 operator+ (const position& begin, const int width)
104 {
105 position res = begin;
106 return res += width;
107 }
108
109 /// Add and assign a position.
110 inline const position&
111 operator-= (position& res, const int width)
112 {
113 return res += -width;
114 }
115
116 /// Add two position objects.
117 inline const position
118 operator- (const position& begin, const int width)
119 {
120 return begin + -width;
121 }
122 ]m4_if(b4_define_location_comparison, [1], [[
123 /// Compare two position objects.
124 inline bool
125 operator== (const position& pos1, const position& pos2)
126 {
127 return
128 (pos1.filename == pos2.filename
129 || pos1.filename && pos2.filename && *pos1.filename == *pos2.filename)
130 && pos1.line == pos2.line && pos1.column == pos2.column;
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 }
153 #endif // not BISON_POSITION_HH]
154 @output b4_dir_prefix[]location.hh
155 b4_copyright([Locations for Bison parsers in C++],
156 [2002, 2003, 2004, 2005, 2006])[
157
158 /**
159 ** \file location.hh
160 ** Define the ]b4_namespace[::location class.
161 */
162
163 #ifndef BISON_LOCATION_HH
164 # define BISON_LOCATION_HH
165
166 # include <iostream>
167 # include <string>
168 # include "position.hh"
169
170 namespace ]b4_namespace[
171 {
172
173 /// Abstract a location.
174 class location
175 {
176 public:
177 ]m4_ifdef([b4_location_constructors], [
178 /// Construct a location.
179 location ()
180 : begin (), end ()
181 {
182 }
183
184 ])[
185 /// Initialization.
186 inline void initialize (]b4_filename_type[* fn)
187 {
188 begin.initialize (fn);
189 end = begin;
190 }
191
192 /** \name Line and Column related manipulators
193 ** \{ */
194 public:
195 /// Reset initial location to final location.
196 inline void step ()
197 {
198 begin = end;
199 }
200
201 /// Extend the current location to the COUNT next columns.
202 inline void columns (unsigned int count = 1)
203 {
204 end += count;
205 }
206
207 /// Extend the current location to the COUNT next lines.
208 inline void lines (unsigned int count = 1)
209 {
210 end.lines (count);
211 }
212 /** \} */
213
214
215 public:
216 /// Beginning of the located region.
217 position begin;
218 /// End of the located region.
219 position end;
220 };
221
222 /// Join two location objects to create a location.
223 inline const location operator+ (const location& begin, const location& end)
224 {
225 location res = begin;
226 res.end = end.end;
227 return res;
228 }
229
230 /// Add two location objects.
231 inline const location operator+ (const location& begin, unsigned int width)
232 {
233 location res = begin;
234 res.columns (width);
235 return res;
236 }
237
238 /// Add and assign a location.
239 inline location& operator+= (location& res, unsigned int width)
240 {
241 res.columns (width);
242 return res;
243 }
244 ]m4_if(b4_define_location_comparison, [1], [[
245 /// Compare two location objects.
246 inline bool
247 operator== (const location& loc1, const location& loc2)
248 {
249 return loc1.begin == loc2.begin && loc1.end == loc2.end;
250 }
251
252 /// Compare two location objects.
253 inline bool
254 operator!= (const location& loc1, const location& loc2)
255 {
256 return !(loc1 == loc2);
257 }
258 ]])[
259 /** \brief Intercept output stream redirection.
260 ** \param ostr the destination output stream
261 ** \param loc a reference to the location to redirect
262 **
263 ** Avoid duplicate information.
264 */
265 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
266 {
267 position last = loc.end - 1;
268 ostr << loc.begin;
269 if (last.filename
270 && (!loc.begin.filename
271 || *loc.begin.filename != *last.filename))
272 ostr << '-' << last;
273 else if (loc.begin.line != last.line)
274 ostr << '-' << last.line << '.' << last.column;
275 else if (loc.begin.column != last.column)
276 ostr << '-' << last.column;
277 return ostr;
278 }
279
280 }
281
282 #endif // not BISON_LOCATION_HH]
283 m4_divert(-1)
284 m4_changecom([#])