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