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