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