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