]>
Commit | Line | Data |
---|---|---|
1 | # C++ skeleton for Bison | |
2 | ||
3 | # Copyright (C) 2002-2015 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-2015]) | |
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_NULLPTR, | |
31 | unsigned l = ]b4_location_initial_line[u, | |
32 | unsigned c = ]b4_location_initial_column[u) | |
33 | : filename (f) | |
34 | , line (l) | |
35 | , column (c) | |
36 | {} | |
37 | ||
38 | ]])[ | |
39 | /// Initialization. | |
40 | void initialize (]b4_percent_define_get([[filename_type]])[* fn = YY_NULLPTR, | |
41 | unsigned l = ]b4_location_initial_line[u, | |
42 | unsigned c = ]b4_location_initial_column[u) | |
43 | { | |
44 | filename = fn; | |
45 | line = l; | |
46 | column = c; | |
47 | } | |
48 | ||
49 | /** \name Line and Column related manipulators | |
50 | ** \{ */ | |
51 | /// (line related) Advance to the COUNT next lines. | |
52 | void lines (int count = 1) | |
53 | { | |
54 | if (count) | |
55 | { | |
56 | column = ]b4_location_initial_column[u; | |
57 | line = add_ (line, count, ]b4_location_initial_line[); | |
58 | } | |
59 | } | |
60 | ||
61 | /// (column related) Advance to the COUNT next columns. | |
62 | void columns (int count = 1) | |
63 | { | |
64 | column = add_ (column, count, ]b4_location_initial_column[); | |
65 | } | |
66 | /** \} */ | |
67 | ||
68 | /// File name to which this position refers. | |
69 | ]b4_percent_define_get([[filename_type]])[* filename; | |
70 | /// Current line number. | |
71 | unsigned line; | |
72 | /// Current column number. | |
73 | unsigned column; | |
74 | ||
75 | private: | |
76 | /// Compute max(min, lhs+rhs) (provided min <= lhs). | |
77 | static unsigned add_ (unsigned lhs, int rhs, unsigned min) | |
78 | { | |
79 | return (0 < rhs || -static_cast<unsigned>(rhs) < lhs | |
80 | ? rhs + lhs | |
81 | : min); | |
82 | } | |
83 | }; | |
84 | ||
85 | /// Add \a width columns, in place. | |
86 | inline position& | |
87 | operator+= (position& res, int width) | |
88 | { | |
89 | res.columns (width); | |
90 | return res; | |
91 | } | |
92 | ||
93 | /// Add \a width columns. | |
94 | inline position | |
95 | operator+ (position res, int width) | |
96 | { | |
97 | return res += width; | |
98 | } | |
99 | ||
100 | /// Subtract \a width columns, in place. | |
101 | inline position& | |
102 | operator-= (position& res, int width) | |
103 | { | |
104 | return res += -width; | |
105 | } | |
106 | ||
107 | /// Subtract \a width columns. | |
108 | inline position | |
109 | operator- (position res, int width) | |
110 | { | |
111 | return res -= width; | |
112 | } | |
113 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ | |
114 | /// Compare two position objects. | |
115 | inline bool | |
116 | operator== (const position& pos1, const position& pos2) | |
117 | { | |
118 | return (pos1.line == pos2.line | |
119 | && pos1.column == pos2.column | |
120 | && (pos1.filename == pos2.filename | |
121 | || (pos1.filename && pos2.filename | |
122 | && *pos1.filename == *pos2.filename))); | |
123 | } | |
124 | ||
125 | /// Compare two position objects. | |
126 | inline bool | |
127 | operator!= (const position& pos1, const position& pos2) | |
128 | { | |
129 | return !(pos1 == pos2); | |
130 | } | |
131 | ]])[ | |
132 | /** \brief Intercept output stream redirection. | |
133 | ** \param ostr the destination output stream | |
134 | ** \param pos a reference to the position to redirect | |
135 | */ | |
136 | template <typename YYChar> | |
137 | inline std::basic_ostream<YYChar>& | |
138 | operator<< (std::basic_ostream<YYChar>& ostr, const position& pos) | |
139 | { | |
140 | if (pos.filename) | |
141 | ostr << *pos.filename << ':'; | |
142 | return ostr << pos.line << '.' << pos.column; | |
143 | } | |
144 | ]]) | |
145 | ||
146 | ||
147 | # b4_location_define | |
148 | # ------------------ | |
149 | m4_define([b4_location_define], | |
150 | [[ /// Abstract a location. | |
151 | class location | |
152 | { | |
153 | public: | |
154 | ]m4_ifdef([b4_location_constructors], [ | |
155 | /// Construct a location from \a b to \a e. | |
156 | location (const position& b, const position& e) | |
157 | : begin (b) | |
158 | , end (e) | |
159 | {} | |
160 | ||
161 | /// Construct a 0-width location in \a p. | |
162 | explicit location (const position& p = position ()) | |
163 | : begin (p) | |
164 | , end (p) | |
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 l = ]b4_location_initial_line[u, | |
170 | unsigned c = ]b4_location_initial_column[u) | |
171 | : begin (f, l, c) | |
172 | , end (f, l, c) | |
173 | {} | |
174 | ||
175 | ])[ | |
176 | /// Initialization. | |
177 | void initialize (]b4_percent_define_get([[filename_type]])[* f = YY_NULLPTR, | |
178 | unsigned l = ]b4_location_initial_line[u, | |
179 | unsigned c = ]b4_location_initial_column[u) | |
180 | { | |
181 | begin.initialize (f, l, c); | |
182 | end = begin; | |
183 | } | |
184 | ||
185 | /** \name Line and Column related manipulators | |
186 | ** \{ */ | |
187 | public: | |
188 | /// Reset initial location to final location. | |
189 | void step () | |
190 | { | |
191 | begin = end; | |
192 | } | |
193 | ||
194 | /// Extend the current location to the COUNT next columns. | |
195 | void columns (int count = 1) | |
196 | { | |
197 | end += count; | |
198 | } | |
199 | ||
200 | /// Extend the current location to the COUNT next lines. | |
201 | void lines (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 locations, in place. | |
216 | inline location& operator+= (location& res, const location& end) | |
217 | { | |
218 | res.end = end.end; | |
219 | return res; | |
220 | } | |
221 | ||
222 | /// Join two locations. | |
223 | inline location operator+ (location res, const location& end) | |
224 | { | |
225 | return res += end; | |
226 | } | |
227 | ||
228 | /// Add \a width columns to the end position, in place. | |
229 | inline location& operator+= (location& res, int width) | |
230 | { | |
231 | res.columns (width); | |
232 | return res; | |
233 | } | |
234 | ||
235 | /// Add \a width columns to the end position. | |
236 | inline location operator+ (location res, int width) | |
237 | { | |
238 | return res += width; | |
239 | } | |
240 | ||
241 | /// Subtract \a width columns to the end position, in place. | |
242 | inline location& operator-= (location& res, int width) | |
243 | { | |
244 | return res += -width; | |
245 | } | |
246 | ||
247 | /// Subtract \a width columns to the end position. | |
248 | inline location operator- (location res, int width) | |
249 | { | |
250 | return res -= width; | |
251 | } | |
252 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ | |
253 | /// Compare two location objects. | |
254 | inline bool | |
255 | operator== (const location& loc1, const location& loc2) | |
256 | { | |
257 | return loc1.begin == loc2.begin && loc1.end == loc2.end; | |
258 | } | |
259 | ||
260 | /// Compare two location objects. | |
261 | inline bool | |
262 | operator!= (const location& loc1, const location& loc2) | |
263 | { | |
264 | return !(loc1 == loc2); | |
265 | } | |
266 | ]])[ | |
267 | /** \brief Intercept output stream redirection. | |
268 | ** \param ostr the destination output stream | |
269 | ** \param loc a reference to the location to redirect | |
270 | ** | |
271 | ** Avoid duplicate information. | |
272 | */ | |
273 | template <typename YYChar> | |
274 | inline std::basic_ostream<YYChar>& | |
275 | operator<< (std::basic_ostream<YYChar>& ostr, const location& loc) | |
276 | { | |
277 | unsigned end_col = 0 < loc.end.column ? loc.end.column - 1 : 0; | |
278 | ostr << loc.begin; | |
279 | if (loc.end.filename | |
280 | && (!loc.begin.filename | |
281 | || *loc.begin.filename != *loc.end.filename)) | |
282 | ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col; | |
283 | else if (loc.begin.line < loc.end.line) | |
284 | ostr << '-' << loc.end.line << '.' << end_col; | |
285 | else if (loc.begin.column < end_col) | |
286 | ostr << '-' << end_col; | |
287 | return ostr; | |
288 | } | |
289 | ]]) | |
290 | ||
291 | ||
292 | b4_defines_if([ | |
293 | b4_output_begin([b4_dir_prefix[]position.hh]) | |
294 | b4_copyright([Positions for Bison parsers in C++])[ | |
295 | ||
296 | /** | |
297 | ** \file ]b4_dir_prefix[position.hh | |
298 | ** Define the ]b4_namespace_ref[::position class. | |
299 | */ | |
300 | ||
301 | ]b4_cpp_guard_open([b4_dir_prefix[]position.hh])[ | |
302 | ||
303 | # include <algorithm> // std::max | |
304 | # include <iostream> | |
305 | # include <string> | |
306 | ||
307 | ]b4_null_define[ | |
308 | ||
309 | ]b4_namespace_open[ | |
310 | ]b4_position_define[ | |
311 | ]b4_namespace_close[ | |
312 | ]b4_cpp_guard_close([b4_dir_prefix[]position.hh]) | |
313 | b4_output_end() | |
314 | ||
315 | ||
316 | b4_output_begin([b4_dir_prefix[]location.hh]) | |
317 | b4_copyright([Locations for Bison parsers in C++])[ | |
318 | ||
319 | /** | |
320 | ** \file ]b4_dir_prefix[location.hh | |
321 | ** Define the ]b4_namespace_ref[::location class. | |
322 | */ | |
323 | ||
324 | ]b4_cpp_guard_open([b4_dir_prefix[]location.hh])[ | |
325 | ||
326 | # include "position.hh" | |
327 | ||
328 | ]b4_namespace_open[ | |
329 | ]b4_location_define[ | |
330 | ]b4_namespace_close[ | |
331 | ]b4_cpp_guard_close([b4_dir_prefix[]location.hh]) | |
332 | b4_output_end() | |
333 | ]) | |
334 | ||
335 | ||
336 | m4_popdef([b4_copyright_years]) |