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