]>
Commit | Line | Data |
---|---|---|
21667f64 AD |
1 | # C++ skeleton for Bison |
2 | ||
8f7e3cf9 | 3 | # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
21667f64 AD |
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 2 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, write to the Free Software | |
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | # 02110-1301 USA | |
19 | ||
21667f64 AD |
20 | # We do want M4 expansion after # for CPP macros. |
21 | m4_changecom() | |
08af01c2 JD |
22 | m4_divert_push(0)dnl |
23 | @output(b4_dir_prefix[]position.hh@) | |
193d7c70 | 24 | b4_copyright([Positions for Bison parsers in C++], |
6e93d810 | 25 | [2002, 2003, 2004, 2005, 2006])[ |
21667f64 AD |
26 | |
27 | /** | |
28 | ** \file position.hh | |
6afc30cc | 29 | ** Define the ]b4_get_percent_define([[namespace]])[::position class. |
21667f64 AD |
30 | */ |
31 | ||
32 | #ifndef BISON_POSITION_HH | |
33 | # define BISON_POSITION_HH | |
34 | ||
35 | # include <iostream> | |
36 | # include <string> | |
cd48d21d | 37 | # include <algorithm> |
21667f64 | 38 | |
6afc30cc | 39 | namespace ]b4_get_percent_define([[namespace]])[ |
21667f64 AD |
40 | { |
41 | /// Abstract a position. | |
42 | class position | |
43 | { | |
21667f64 | 44 | public: |
4626a15d | 45 | ]m4_ifdef([b4_location_constructors], [ |
21667f64 | 46 | /// Construct a position. |
4626a15d | 47 | position () |
cd48d21d | 48 | : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[) |
21667f64 AD |
49 | { |
50 | } | |
21667f64 | 51 | |
4626a15d AD |
52 | ])[ |
53 | /// Initialization. | |
6afc30cc | 54 | inline void initialize (]b4_get_percent_define([[filename_type]])[* fn) |
4626a15d AD |
55 | { |
56 | filename = fn; | |
cd48d21d AD |
57 | line = ]b4_location_initial_line[; |
58 | column = ]b4_location_initial_column[; | |
4626a15d | 59 | } |
21667f64 AD |
60 | |
61 | /** \name Line and Column related manipulators | |
62 | ** \{ */ | |
63 | public: | |
64 | /// (line related) Advance to the COUNT next lines. | |
65 | inline void lines (int count = 1) | |
66 | { | |
cd48d21d | 67 | column = ]b4_location_initial_column[; |
21667f64 AD |
68 | line += count; |
69 | } | |
70 | ||
71 | /// (column related) Advance to the COUNT next columns. | |
72 | inline void columns (int count = 1) | |
73 | { | |
cd48d21d | 74 | column = std::max (]b4_location_initial_column[u, column + count); |
21667f64 AD |
75 | } |
76 | /** \} */ | |
77 | ||
78 | public: | |
79 | /// File name to which this position refers. | |
6afc30cc | 80 | ]b4_get_percent_define([[filename_type]])[* filename; |
21667f64 AD |
81 | /// Current line number. |
82 | unsigned int line; | |
83 | /// Current column number. | |
84 | unsigned int column; | |
85 | }; | |
86 | ||
87 | /// Add and assign a position. | |
88 | inline const position& | |
89 | operator+= (position& res, const int width) | |
90 | { | |
91 | res.columns (width); | |
92 | return res; | |
93 | } | |
94 | ||
95 | /// Add two position objects. | |
96 | inline const position | |
97 | operator+ (const position& begin, const int width) | |
98 | { | |
99 | position res = begin; | |
100 | return res += width; | |
101 | } | |
102 | ||
103 | /// Add and assign a position. | |
104 | inline const position& | |
105 | operator-= (position& res, const int width) | |
106 | { | |
107 | return res += -width; | |
108 | } | |
109 | ||
110 | /// Add two position objects. | |
111 | inline const position | |
112 | operator- (const position& begin, const int width) | |
113 | { | |
114 | return begin + -width; | |
115 | } | |
6afc30cc | 116 | ]m4_if(b4_get_percent_define([[define_location_comparison]]), [1], [[ |
31b2b07e JD |
117 | /// Compare two position objects. |
118 | inline bool | |
119 | operator== (const position& pos1, const position& pos2) | |
120 | { | |
121 | return | |
122 | (pos1.filename == pos2.filename | |
123 | || pos1.filename && pos2.filename && *pos1.filename == *pos2.filename) | |
124 | && pos1.line == pos2.line && pos1.column == pos2.column; | |
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 | ||
146 | } | |
147 | #endif // not BISON_POSITION_HH] | |
08af01c2 | 148 | @output(b4_dir_prefix[]location.hh@) |
193d7c70 | 149 | b4_copyright([Locations for Bison parsers in C++], |
6e93d810 | 150 | [2002, 2003, 2004, 2005, 2006])[ |
21667f64 AD |
151 | |
152 | /** | |
153 | ** \file location.hh | |
6afc30cc | 154 | ** Define the ]b4_get_percent_define([[namespace]])[::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 | ||
6afc30cc | 164 | namespace ]b4_get_percent_define([[namespace]])[ |
21667f64 AD |
165 | { |
166 | ||
167 | /// Abstract a location. | |
168 | class location | |
169 | { | |
21667f64 | 170 | public: |
4626a15d | 171 | ]m4_ifdef([b4_location_constructors], [ |
21667f64 | 172 | /// Construct a location. |
4626a15d AD |
173 | location () |
174 | : begin (), end () | |
21667f64 AD |
175 | { |
176 | } | |
21667f64 | 177 | |
4626a15d AD |
178 | ])[ |
179 | /// Initialization. | |
6afc30cc | 180 | inline void initialize (]b4_get_percent_define([[filename_type]])[* fn) |
4626a15d AD |
181 | { |
182 | begin.initialize (fn); | |
183 | end = begin; | |
184 | } | |
21667f64 AD |
185 | |
186 | /** \name Line and Column related manipulators | |
187 | ** \{ */ | |
188 | public: | |
189 | /// Reset initial location to final location. | |
190 | inline void step () | |
191 | { | |
192 | begin = end; | |
193 | } | |
194 | ||
195 | /// Extend the current location to the COUNT next columns. | |
196 | inline void columns (unsigned int count = 1) | |
197 | { | |
198 | end += count; | |
199 | } | |
200 | ||
201 | /// Extend the current location to the COUNT next lines. | |
202 | inline void lines (unsigned int count = 1) | |
203 | { | |
204 | end.lines (count); | |
205 | } | |
206 | /** \} */ | |
207 | ||
208 | ||
209 | public: | |
210 | /// Beginning of the located region. | |
211 | position begin; | |
212 | /// End of the located region. | |
213 | position end; | |
214 | }; | |
215 | ||
216 | /// Join two location objects to create a location. | |
217 | inline const location operator+ (const location& begin, const location& end) | |
218 | { | |
219 | location res = begin; | |
220 | res.end = end.end; | |
221 | return res; | |
222 | } | |
223 | ||
224 | /// Add two location objects. | |
225 | inline const location operator+ (const location& begin, unsigned int width) | |
226 | { | |
227 | location res = begin; | |
228 | res.columns (width); | |
229 | return res; | |
230 | } | |
231 | ||
232 | /// Add and assign a location. | |
233 | inline location& operator+= (location& res, unsigned int width) | |
234 | { | |
235 | res.columns (width); | |
236 | return res; | |
237 | } | |
6afc30cc | 238 | ]m4_if(b4_get_percent_define([[define_location_comparison]]), [1], [[ |
31b2b07e JD |
239 | /// Compare two location objects. |
240 | inline bool | |
241 | operator== (const location& loc1, const location& loc2) | |
242 | { | |
243 | return loc1.begin == loc2.begin && loc1.end == loc2.end; | |
244 | } | |
21667f64 | 245 | |
31b2b07e JD |
246 | /// Compare two location objects. |
247 | inline bool | |
248 | operator!= (const location& loc1, const location& loc2) | |
249 | { | |
250 | return !(loc1 == loc2); | |
251 | } | |
252 | ]])[ | |
21667f64 AD |
253 | /** \brief Intercept output stream redirection. |
254 | ** \param ostr the destination output stream | |
255 | ** \param loc a reference to the location to redirect | |
256 | ** | |
257 | ** Avoid duplicate information. | |
258 | */ | |
259 | inline std::ostream& operator<< (std::ostream& ostr, const location& loc) | |
260 | { | |
261 | position last = loc.end - 1; | |
262 | ostr << loc.begin; | |
263 | if (last.filename | |
264 | && (!loc.begin.filename | |
265 | || *loc.begin.filename != *last.filename)) | |
266 | ostr << '-' << last; | |
267 | else if (loc.begin.line != last.line) | |
268 | ostr << '-' << last.line << '.' << last.column; | |
269 | else if (loc.begin.column != last.column) | |
270 | ostr << '-' << last.column; | |
271 | return ostr; | |
272 | } | |
273 | ||
274 | } | |
275 | ||
276 | #endif // not BISON_LOCATION_HH] | |
08af01c2 | 277 | m4_divert_pop(0) |
21667f64 | 278 | m4_changecom([#]) |