]>
Commit | Line | Data |
---|---|---|
1 | # C++ skeleton for Bison | |
2 | ||
3 | # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009 | |
4 | # Free Software Foundation, 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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | # We do want M4 expansion after # for CPP macros. | |
20 | m4_changecom() | |
21 | m4_divert_push(0)dnl | |
22 | @output(b4_dir_prefix[]position.hh@)@ | |
23 | b4_copyright([Positions for Bison parsers in C++], | |
24 | [2002, 2003, 2004, 2005, 2006, 2007, 2009])[ | |
25 | ||
26 | /** | |
27 | ** \file position.hh | |
28 | ** Define the ]b4_namespace_ref[::position class. | |
29 | */ | |
30 | ||
31 | #ifndef BISON_POSITION_HH | |
32 | # define BISON_POSITION_HH | |
33 | ||
34 | # include <iostream> | |
35 | # include <string> | |
36 | # include <algorithm> | |
37 | ||
38 | ]b4_namespace_open[ | |
39 | /// Abstract a position. | |
40 | class position | |
41 | { | |
42 | public: | |
43 | ]m4_ifdef([b4_location_constructors], [ | |
44 | /// Construct a position. | |
45 | position () | |
46 | : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[) | |
47 | { | |
48 | } | |
49 | ||
50 | ])[ | |
51 | /// Initialization. | |
52 | inline void initialize (]b4_percent_define_get([[filename_type]])[* fn) | |
53 | { | |
54 | filename = fn; | |
55 | line = ]b4_location_initial_line[; | |
56 | column = ]b4_location_initial_column[; | |
57 | } | |
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 | { | |
65 | column = ]b4_location_initial_column[; | |
66 | line += count; | |
67 | } | |
68 | ||
69 | /// (column related) Advance to the COUNT next columns. | |
70 | inline void columns (int count = 1) | |
71 | { | |
72 | column = std::max (]b4_location_initial_column[u, column + count); | |
73 | } | |
74 | /** \} */ | |
75 | ||
76 | public: | |
77 | /// File name to which this position refers. | |
78 | ]b4_percent_define_get([[filename_type]])[* filename; | |
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 | } | |
114 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ | |
115 | /// Compare two position objects. | |
116 | inline bool | |
117 | operator== (const position& pos1, const position& pos2) | |
118 | { | |
119 | return (pos1.line == pos2.line | |
120 | && pos1.column == pos2.column | |
121 | && (pos1.filename == pos2.filename | |
122 | || (pos1.filename && pos2.filename | |
123 | && *pos1.filename == *pos2.filename))); | |
124 | } | |
125 | ||
126 | /// Compare two position objects. | |
127 | inline bool | |
128 | operator!= (const position& pos1, const position& pos2) | |
129 | { | |
130 | return !(pos1 == pos2); | |
131 | } | |
132 | ]])[ | |
133 | /** \brief Intercept output stream redirection. | |
134 | ** \param ostr the destination output stream | |
135 | ** \param pos a reference to the position to redirect | |
136 | */ | |
137 | inline std::ostream& | |
138 | operator<< (std::ostream& ostr, const position& pos) | |
139 | { | |
140 | if (pos.filename) | |
141 | ostr << *pos.filename << ':'; | |
142 | return ostr << pos.line << '.' << pos.column; | |
143 | } | |
144 | ||
145 | ]b4_namespace_close[ | |
146 | #endif // not BISON_POSITION_HH] | |
147 | @output(b4_dir_prefix[]location.hh@)@ | |
148 | b4_copyright([Locations for Bison parsers in C++], | |
149 | [2002, 2003, 2004, 2005, 2006, 2007, 2009])[ | |
150 | ||
151 | /** | |
152 | ** \file location.hh | |
153 | ** Define the ]b4_namespace_ref[::location class. | |
154 | */ | |
155 | ||
156 | #ifndef BISON_LOCATION_HH | |
157 | # define BISON_LOCATION_HH | |
158 | ||
159 | # include <iostream> | |
160 | # include <string> | |
161 | # include "position.hh" | |
162 | ||
163 | ]b4_namespace_open[ | |
164 | ||
165 | /// Abstract a location. | |
166 | class location | |
167 | { | |
168 | public: | |
169 | ]m4_ifdef([b4_location_constructors], [ | |
170 | /// Construct a location. | |
171 | location () | |
172 | : begin (), end () | |
173 | { | |
174 | } | |
175 | ||
176 | ])[ | |
177 | /// Initialization. | |
178 | inline void initialize (]b4_percent_define_get([[filename_type]])[* fn) | |
179 | { | |
180 | begin.initialize (fn); | |
181 | end = begin; | |
182 | } | |
183 | ||
184 | /** \name Line and Column related manipulators | |
185 | ** \{ */ | |
186 | public: | |
187 | /// Reset initial location to final location. | |
188 | inline void step () | |
189 | { | |
190 | begin = end; | |
191 | } | |
192 | ||
193 | /// Extend the current location to the COUNT next columns. | |
194 | inline void columns (unsigned int count = 1) | |
195 | { | |
196 | end += count; | |
197 | } | |
198 | ||
199 | /// Extend the current location to the COUNT next lines. | |
200 | inline void lines (unsigned int count = 1) | |
201 | { | |
202 | end.lines (count); | |
203 | } | |
204 | /** \} */ | |
205 | ||
206 | ||
207 | public: | |
208 | /// Beginning of the located region. | |
209 | position begin; | |
210 | /// End of the located region. | |
211 | position end; | |
212 | }; | |
213 | ||
214 | /// Join two location objects to create a location. | |
215 | inline const location operator+ (const location& begin, const location& end) | |
216 | { | |
217 | location res = begin; | |
218 | res.end = end.end; | |
219 | return res; | |
220 | } | |
221 | ||
222 | /// Add two location objects. | |
223 | inline const location operator+ (const location& begin, unsigned int width) | |
224 | { | |
225 | location res = begin; | |
226 | res.columns (width); | |
227 | return res; | |
228 | } | |
229 | ||
230 | /// Add and assign a location. | |
231 | inline location& operator+= (location& res, unsigned int width) | |
232 | { | |
233 | res.columns (width); | |
234 | return res; | |
235 | } | |
236 | ]b4_percent_define_flag_if([[define_location_comparison]], [[ | |
237 | /// Compare two location objects. | |
238 | inline bool | |
239 | operator== (const location& loc1, const location& loc2) | |
240 | { | |
241 | return loc1.begin == loc2.begin && loc1.end == loc2.end; | |
242 | } | |
243 | ||
244 | /// Compare two location objects. | |
245 | inline bool | |
246 | operator!= (const location& loc1, const location& loc2) | |
247 | { | |
248 | return !(loc1 == loc2); | |
249 | } | |
250 | ]])[ | |
251 | /** \brief Intercept output stream redirection. | |
252 | ** \param ostr the destination output stream | |
253 | ** \param loc a reference to the location to redirect | |
254 | ** | |
255 | ** Avoid duplicate information. | |
256 | */ | |
257 | inline std::ostream& operator<< (std::ostream& ostr, const location& loc) | |
258 | { | |
259 | position last = loc.end - 1; | |
260 | ostr << loc.begin; | |
261 | if (last.filename | |
262 | && (!loc.begin.filename | |
263 | || *loc.begin.filename != *last.filename)) | |
264 | ostr << '-' << last; | |
265 | else if (loc.begin.line != last.line) | |
266 | ostr << '-' << last.line << '.' << last.column; | |
267 | else if (loc.begin.column != last.column) | |
268 | ostr << '-' << last.column; | |
269 | return ostr; | |
270 | } | |
271 | ||
272 | ]b4_namespace_close[ | |
273 | ||
274 | #endif // not BISON_LOCATION_HH] | |
275 | m4_divert_pop(0) | |
276 | m4_changecom([#]) |