* src/symtab.h (struct semantic_type): Remove the tag 'semantic_type',
[bison.git] / data / location.cc
blob2d32bf77bf8f8c62ebaadd07a935f2d8d712e8bf
1 m4_divert(-1)
3 # C++ skeleton for Bison
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # 02110-1301 USA
22 # We do want M4 expansion after # for CPP macros.
23 m4_changecom()
24 m4_divert(0)dnl
25 @output b4_dir_prefix[]position.hh
26 b4_copyright([Positions for Bison parsers in C++],
27 [2002, 2003, 2004, 2005, 2006])[
29 /**
30 ** \file position.hh
31 ** Define the ]b4_namespace[::position class.
34 #ifndef BISON_POSITION_HH
35 # define BISON_POSITION_HH
37 # include <iostream>
38 # include <string>
39 # include <algorithm>
41 namespace ]b4_namespace[
43 /// Abstract a position.
44 class position
46 public:
47 ]m4_ifdef([b4_location_constructors], [
48 /// Construct a position.
49 position ()
50 : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[)
54 ])[
55 /// Initialization.
56 inline void initialize (]b4_filename_type[* fn)
58 filename = fn;
59 line = ]b4_location_initial_line[;
60 column = ]b4_location_initial_column[;
63 /** \name Line and Column related manipulators
64 ** \{ */
65 public:
66 /// (line related) Advance to the COUNT next lines.
67 inline void lines (int count = 1)
69 column = ]b4_location_initial_column[;
70 line += count;
73 /// (column related) Advance to the COUNT next columns.
74 inline void columns (int count = 1)
76 column = std::max (]b4_location_initial_column[u, column + count);
78 /** \} */
80 public:
81 /// File name to which this position refers.
82 ]b4_filename_type[* filename;
83 /// Current line number.
84 unsigned int line;
85 /// Current column number.
86 unsigned int column;
89 /// Add and assign a position.
90 inline const position&
91 operator+= (position& res, const int width)
93 res.columns (width);
94 return res;
97 /// Add two position objects.
98 inline const position
99 operator+ (const position& begin, const int width)
101 position res = begin;
102 return res += width;
105 /// Add and assign a position.
106 inline const position&
107 operator-= (position& res, const int width)
109 return res += -width;
112 /// Add two position objects.
113 inline const position
114 operator- (const position& begin, const int width)
116 return begin + -width;
118 ]m4_if(b4_define_location_comparison, [1], [[
119 /// Compare two position objects.
120 inline bool
121 operator== (const position& pos1, const position& pos2)
123 return
124 (pos1.filename == pos2.filename
125 || pos1.filename && pos2.filename && *pos1.filename == *pos2.filename)
126 && pos1.line == pos2.line && pos1.column == pos2.column;
129 /// Compare two position objects.
130 inline bool
131 operator!= (const position& pos1, const position& pos2)
133 return !(pos1 == pos2);
135 ]])[
136 /** \brief Intercept output stream redirection.
137 ** \param ostr the destination output stream
138 ** \param pos a reference to the position to redirect
140 inline std::ostream&
141 operator<< (std::ostream& ostr, const position& pos)
143 if (pos.filename)
144 ostr << *pos.filename << ':';
145 return ostr << pos.line << '.' << pos.column;
149 #endif // not BISON_POSITION_HH]
150 @output b4_dir_prefix[]location.hh
151 b4_copyright([Locations for Bison parsers in C++],
152 [2002, 2003, 2004, 2005, 2006])[
155 ** \file location.hh
156 ** Define the ]b4_namespace[::location class.
159 #ifndef BISON_LOCATION_HH
160 # define BISON_LOCATION_HH
162 # include <iostream>
163 # include <string>
164 # include "position.hh"
166 namespace ]b4_namespace[
169 /// Abstract a location.
170 class location
172 public:
173 ]m4_ifdef([b4_location_constructors], [
174 /// Construct a location.
175 location ()
176 : begin (), end ()
181 /// Initialization.
182 inline void initialize (]b4_filename_type[* fn)
184 begin.initialize (fn);
185 end = begin;
188 /** \name Line and Column related manipulators
189 ** \{ */
190 public:
191 /// Reset initial location to final location.
192 inline void step ()
194 begin = end;
197 /// Extend the current location to the COUNT next columns.
198 inline void columns (unsigned int count = 1)
200 end += count;
203 /// Extend the current location to the COUNT next lines.
204 inline void lines (unsigned int count = 1)
206 end.lines (count);
208 /** \} */
211 public:
212 /// Beginning of the located region.
213 position begin;
214 /// End of the located region.
215 position end;
218 /// Join two location objects to create a location.
219 inline const location operator+ (const location& begin, const location& end)
221 location res = begin;
222 res.end = end.end;
223 return res;
226 /// Add two location objects.
227 inline const location operator+ (const location& begin, unsigned int width)
229 location res = begin;
230 res.columns (width);
231 return res;
234 /// Add and assign a location.
235 inline location& operator+= (location& res, unsigned int width)
237 res.columns (width);
238 return res;
240 ]m4_if(b4_define_location_comparison, [1], [[
241 /// Compare two location objects.
242 inline bool
243 operator== (const location& loc1, const location& loc2)
245 return loc1.begin == loc2.begin && loc1.end == loc2.end;
248 /// Compare two location objects.
249 inline bool
250 operator!= (const location& loc1, const location& loc2)
252 return !(loc1 == loc2);
254 ]])[
255 /** \brief Intercept output stream redirection.
256 ** \param ostr the destination output stream
257 ** \param loc a reference to the location to redirect
259 ** Avoid duplicate information.
261 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
263 position last = loc.end - 1;
264 ostr << loc.begin;
265 if (last.filename
266 && (!loc.begin.filename
267 || *loc.begin.filename != *last.filename))
268 ostr << '-' << last;
269 else if (loc.begin.line != last.line)
270 ostr << '-' << last.line << '.' << last.column;
271 else if (loc.begin.column != last.column)
272 ostr << '-' << last.column;
273 return ostr;
278 #endif // not BISON_LOCATION_HH]
279 m4_divert(-1)
280 m4_changecom([#])