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