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