]>
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: |
4626a15d | 44 | ]m4_ifdef([b4_location_constructors], [ |
21667f64 | 45 | /// Construct a position. |
4626a15d | 46 | position () |
ef51bfa7 | 47 | : filename (YY_NULL), line (]b4_location_initial_line[), column (]b4_location_initial_column[) |
21667f64 AD |
48 | { |
49 | } | |
21667f64 | 50 | |
4626a15d AD |
51 | ])[ |
52 | /// Initialization. | |
ef51bfa7 | 53 | inline void initialize (]b4_percent_define_get([[filename_type]])[* fn = YY_NULL) |
4626a15d AD |
54 | { |
55 | filename = fn; | |
cd48d21d AD |
56 | line = ]b4_location_initial_line[; |
57 | column = ]b4_location_initial_column[; | |
4626a15d | 58 | } |
21667f64 AD |
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 | { | |
cd48d21d | 66 | column = ]b4_location_initial_column[; |
21667f64 AD |
67 | line += count; |
68 | } | |
69 | ||
70 | /// (column related) Advance to the COUNT next columns. | |
71 | inline void columns (int count = 1) | |
72 | { | |
cd48d21d | 73 | column = std::max (]b4_location_initial_column[u, column + count); |
21667f64 AD |
74 | } |
75 | /** \} */ | |
76 | ||
77 | public: | |
78 | /// File name to which this position refers. | |
a4e25e1d | 79 | ]b4_percent_define_get([[filename_type]])[* filename; |
21667f64 AD |
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 | } | |
592d0b1e | 115 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ |
31b2b07e JD |
116 | /// Compare two position objects. |
117 | inline bool | |
118 | operator== (const position& pos1, const position& pos2) | |
119 | { | |
b328890a AD |
120 | return (pos1.line == pos2.line |
121 | && pos1.column == pos2.column | |
122 | && (pos1.filename == pos2.filename | |
123 | || (pos1.filename && pos2.filename | |
124 | && *pos1.filename == *pos2.filename))); | |
31b2b07e | 125 | } |
21667f64 | 126 | |
31b2b07e JD |
127 | /// Compare two position objects. |
128 | inline bool | |
129 | operator!= (const position& pos1, const position& pos2) | |
130 | { | |
131 | return !(pos1 == pos2); | |
132 | } | |
133 | ]])[ | |
21667f64 AD |
134 | /** \brief Intercept output stream redirection. |
135 | ** \param ostr the destination output stream | |
136 | ** \param pos a reference to the position to redirect | |
137 | */ | |
138 | inline std::ostream& | |
139 | operator<< (std::ostream& ostr, const position& pos) | |
140 | { | |
141 | if (pos.filename) | |
142 | ostr << *pos.filename << ':'; | |
143 | return ostr << pos.line << '.' << pos.column; | |
144 | } | |
145 | ||
793fbca5 | 146 | ]b4_namespace_close[ |
21667f64 | 147 | #endif // not BISON_POSITION_HH] |
c19178bf | 148 | @output(b4_dir_prefix[]location.hh@)@ |
193d7c70 | 149 | b4_copyright([Locations for Bison parsers in C++], |
ea0a7676 | 150 | [2002-2007, 2009-2011])[ |
21667f64 AD |
151 | |
152 | /** | |
153 | ** \file location.hh | |
793fbca5 | 154 | ** Define the ]b4_namespace_ref[::location class. |
21667f64 AD |
155 | */ |
156 | ||
157 | #ifndef BISON_LOCATION_HH | |
158 | # define BISON_LOCATION_HH | |
159 | ||
160 | # include <iostream> | |
161 | # include <string> | |
162 | # include "position.hh" | |
163 | ||
793fbca5 | 164 | ]b4_namespace_open[ |
21667f64 AD |
165 | |
166 | /// Abstract a location. | |
167 | class location | |
168 | { | |
21667f64 | 169 | public: |
4626a15d | 170 | ]m4_ifdef([b4_location_constructors], [ |
21667f64 | 171 | /// Construct a location. |
4626a15d AD |
172 | location () |
173 | : begin (), end () | |
21667f64 AD |
174 | { |
175 | } | |
21667f64 | 176 | |
4626a15d AD |
177 | ])[ |
178 | /// Initialization. | |
ef51bfa7 | 179 | inline void initialize (]b4_percent_define_get([[filename_type]])[* fn = YY_NULL) |
4626a15d AD |
180 | { |
181 | begin.initialize (fn); | |
182 | end = begin; | |
183 | } | |
21667f64 AD |
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 | } | |
592d0b1e | 237 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ |
31b2b07e JD |
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 | } | |
21667f64 | 244 | |
31b2b07e JD |
245 | /// Compare two location objects. |
246 | inline bool | |
247 | operator!= (const location& loc1, const location& loc2) | |
248 | { | |
249 | return !(loc1 == loc2); | |
250 | } | |
251 | ]])[ | |
21667f64 AD |
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 | ||
793fbca5 | 273 | ]b4_namespace_close[ |
21667f64 AD |
274 | |
275 | #endif // not BISON_LOCATION_HH] | |
08af01c2 | 276 | m4_divert_pop(0) |
21667f64 | 277 | m4_changecom([#]) |