]> git.saurik.com Git - bison.git/blame - data/location.cc
Update to GPLv3.
[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 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
21667f64
AD
19# We do want M4 expansion after # for CPP macros.
20m4_changecom()
08af01c2
JD
21m4_divert_push(0)dnl
22@output(b4_dir_prefix[]position.hh@)
193d7c70 23b4_copyright([Positions for Bison parsers in C++],
6e93d810 24 [2002, 2003, 2004, 2005, 2006])[
21667f64
AD
25
26/**
27 ** \file position.hh
a4e25e1d 28 ** Define the ]b4_percent_define_get([[namespace]])[::position class.
21667f64
AD
29 */
30
31#ifndef BISON_POSITION_HH
32# define BISON_POSITION_HH
33
34# include <iostream>
35# include <string>
cd48d21d 36# include <algorithm>
21667f64 37
a4e25e1d 38namespace ]b4_percent_define_get([[namespace]])[
21667f64
AD
39{
40 /// Abstract a position.
41 class position
42 {
21667f64 43 public:
4626a15d 44]m4_ifdef([b4_location_constructors], [
21667f64 45 /// Construct a position.
4626a15d 46 position ()
cd48d21d 47 : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[)
21667f64
AD
48 {
49 }
21667f64 50
4626a15d
AD
51])[
52 /// Initialization.
a4e25e1d 53 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
54 {
55 filename = fn;
cd48d21d
AD
56 line = ]b4_location_initial_line[;
57 column = ]b4_location_initial_column[;
4626a15d 58 }
21667f64
AD
59
60 /** \name Line and Column related manipulators
61 ** \{ */
62 public:
63 /// (line related) Advance to the COUNT next lines.
64 inline void lines (int count = 1)
65 {
cd48d21d 66 column = ]b4_location_initial_column[;
21667f64
AD
67 line += count;
68 }
69
70 /// (column related) Advance to the COUNT next columns.
71 inline void columns (int count = 1)
72 {
cd48d21d 73 column = std::max (]b4_location_initial_column[u, column + count);
21667f64
AD
74 }
75 /** \} */
76
77 public:
78 /// File name to which this position refers.
a4e25e1d 79 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64
AD
80 /// Current line number.
81 unsigned int line;
82 /// Current column number.
83 unsigned int column;
84 };
85
86 /// Add and assign a position.
87 inline const position&
88 operator+= (position& res, const int width)
89 {
90 res.columns (width);
91 return res;
92 }
93
94 /// Add two position objects.
95 inline const position
96 operator+ (const position& begin, const int width)
97 {
98 position res = begin;
99 return res += width;
100 }
101
102 /// Add and assign a position.
103 inline const position&
104 operator-= (position& res, const int width)
105 {
106 return res += -width;
107 }
108
109 /// Add two position objects.
110 inline const position
111 operator- (const position& begin, const int width)
112 {
113 return begin + -width;
114 }
592d0b1e 115]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
116 /// Compare two position objects.
117 inline bool
118 operator== (const position& pos1, const position& pos2)
119 {
120 return
121 (pos1.filename == pos2.filename
122 || pos1.filename && pos2.filename && *pos1.filename == *pos2.filename)
123 && pos1.line == pos2.line && pos1.column == pos2.column;
124 }
21667f64 125
31b2b07e
JD
126 /// Compare two position objects.
127 inline bool
128 operator!= (const position& pos1, const position& pos2)
129 {
130 return !(pos1 == pos2);
131 }
132]])[
21667f64
AD
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}
146#endif // not BISON_POSITION_HH]
08af01c2 147@output(b4_dir_prefix[]location.hh@)
193d7c70 148b4_copyright([Locations for Bison parsers in C++],
6e93d810 149 [2002, 2003, 2004, 2005, 2006])[
21667f64
AD
150
151/**
152 ** \file location.hh
a4e25e1d 153 ** Define the ]b4_percent_define_get([[namespace]])[::location class.
21667f64
AD
154 */
155
156#ifndef BISON_LOCATION_HH
157# define BISON_LOCATION_HH
158
159# include <iostream>
160# include <string>
161# include "position.hh"
162
a4e25e1d 163namespace ]b4_percent_define_get([[namespace]])[
21667f64
AD
164{
165
166 /// Abstract a location.
167 class location
168 {
21667f64 169 public:
4626a15d 170]m4_ifdef([b4_location_constructors], [
21667f64 171 /// Construct a location.
4626a15d
AD
172 location ()
173 : begin (), end ()
21667f64
AD
174 {
175 }
21667f64 176
4626a15d
AD
177])[
178 /// Initialization.
a4e25e1d 179 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
180 {
181 begin.initialize (fn);
182 end = begin;
183 }
21667f64
AD
184
185 /** \name Line and Column related manipulators
186 ** \{ */
187 public:
188 /// Reset initial location to final location.
189 inline void step ()
190 {
191 begin = end;
192 }
193
194 /// Extend the current location to the COUNT next columns.
195 inline void columns (unsigned int count = 1)
196 {
197 end += count;
198 }
199
200 /// Extend the current location to the COUNT next lines.
201 inline void lines (unsigned int count = 1)
202 {
203 end.lines (count);
204 }
205 /** \} */
206
207
208 public:
209 /// Beginning of the located region.
210 position begin;
211 /// End of the located region.
212 position end;
213 };
214
215 /// Join two location objects to create a location.
216 inline const location operator+ (const location& begin, const location& end)
217 {
218 location res = begin;
219 res.end = end.end;
220 return res;
221 }
222
223 /// Add two location objects.
224 inline const location operator+ (const location& begin, unsigned int width)
225 {
226 location res = begin;
227 res.columns (width);
228 return res;
229 }
230
231 /// Add and assign a location.
232 inline location& operator+= (location& res, unsigned int width)
233 {
234 res.columns (width);
235 return res;
236 }
592d0b1e 237]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
238 /// Compare two location objects.
239 inline bool
240 operator== (const location& loc1, const location& loc2)
241 {
242 return loc1.begin == loc2.begin && loc1.end == loc2.end;
243 }
21667f64 244
31b2b07e
JD
245 /// Compare two location objects.
246 inline bool
247 operator!= (const location& loc1, const location& loc2)
248 {
249 return !(loc1 == loc2);
250 }
251]])[
21667f64
AD
252 /** \brief Intercept output stream redirection.
253 ** \param ostr the destination output stream
254 ** \param loc a reference to the location to redirect
255 **
256 ** Avoid duplicate information.
257 */
258 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
259 {
260 position last = loc.end - 1;
261 ostr << loc.begin;
262 if (last.filename
263 && (!loc.begin.filename
264 || *loc.begin.filename != *last.filename))
265 ostr << '-' << last;
266 else if (loc.begin.line != last.line)
267 ostr << '-' << last.line << '.' << last.column;
268 else if (loc.begin.column != last.column)
269 ostr << '-' << last.column;
270 return ostr;
271 }
272
273}
274
275#endif // not BISON_LOCATION_HH]
08af01c2 276m4_divert_pop(0)
21667f64 277m4_changecom([#])