]> git.saurik.com Git - bison.git/blame - data/location.cc
Back out previous patch.
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1# C++ skeleton for Bison
2
279cabb6
JD
3# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
4# Inc.
21667f64
AD
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
21667f64
AD
21# We do want M4 expansion after # for CPP macros.
22m4_changecom()
08af01c2
JD
23m4_divert_push(0)dnl
24@output(b4_dir_prefix[]position.hh@)
193d7c70 25b4_copyright([Positions for Bison parsers in C++],
6e93d810 26 [2002, 2003, 2004, 2005, 2006])[
21667f64
AD
27
28/**
29 ** \file position.hh
a4e25e1d 30 ** Define the ]b4_percent_define_get([[namespace]])[::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
a4e25e1d 40namespace ]b4_percent_define_get([[namespace]])[
21667f64
AD
41{
42 /// Abstract a position.
43 class position
44 {
21667f64 45 public:
4626a15d 46]m4_ifdef([b4_location_constructors], [
21667f64 47 /// Construct a position.
4626a15d 48 position ()
cd48d21d 49 : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[)
21667f64
AD
50 {
51 }
21667f64 52
4626a15d
AD
53])[
54 /// Initialization.
a4e25e1d 55 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
56 {
57 filename = fn;
cd48d21d
AD
58 line = ]b4_location_initial_line[;
59 column = ]b4_location_initial_column[;
4626a15d 60 }
21667f64
AD
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 {
cd48d21d 68 column = ]b4_location_initial_column[;
21667f64
AD
69 line += count;
70 }
71
72 /// (column related) Advance to the COUNT next columns.
73 inline void columns (int count = 1)
74 {
cd48d21d 75 column = std::max (]b4_location_initial_column[u, column + count);
21667f64
AD
76 }
77 /** \} */
78
79 public:
80 /// File name to which this position refers.
a4e25e1d 81 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64
AD
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 }
a4e25e1d 117]m4_if(b4_percent_define_get([[define_location_comparison]]), [1], [[
31b2b07e
JD
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 }
21667f64 127
31b2b07e
JD
128 /// Compare two position objects.
129 inline bool
130 operator!= (const position& pos1, const position& pos2)
131 {
132 return !(pos1 == pos2);
133 }
134]])[
21667f64
AD
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]
08af01c2 149@output(b4_dir_prefix[]location.hh@)
193d7c70 150b4_copyright([Locations for Bison parsers in C++],
6e93d810 151 [2002, 2003, 2004, 2005, 2006])[
21667f64
AD
152
153/**
154 ** \file location.hh
a4e25e1d 155 ** Define the ]b4_percent_define_get([[namespace]])[::location class.
21667f64
AD
156 */
157
158#ifndef BISON_LOCATION_HH
159# define BISON_LOCATION_HH
160
161# include <iostream>
162# include <string>
163# include "position.hh"
164
a4e25e1d 165namespace ]b4_percent_define_get([[namespace]])[
21667f64
AD
166{
167
168 /// Abstract a location.
169 class location
170 {
21667f64 171 public:
4626a15d 172]m4_ifdef([b4_location_constructors], [
21667f64 173 /// Construct a location.
4626a15d
AD
174 location ()
175 : begin (), end ()
21667f64
AD
176 {
177 }
21667f64 178
4626a15d
AD
179])[
180 /// Initialization.
a4e25e1d 181 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
182 {
183 begin.initialize (fn);
184 end = begin;
185 }
21667f64
AD
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 }
a4e25e1d 239]m4_if(b4_percent_define_get([[define_location_comparison]]), [1], [[
31b2b07e
JD
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 }
21667f64 246
31b2b07e
JD
247 /// Compare two location objects.
248 inline bool
249 operator!= (const location& loc1, const location& loc2)
250 {
251 return !(loc1 == loc2);
252 }
253]])[
21667f64
AD
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]
08af01c2 278m4_divert_pop(0)
21667f64 279m4_changecom([#])