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