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