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