]> git.saurik.com Git - bison.git/blob - data/location.cc
Complete ChangeLog.
[bison.git] / data / location.cc
1 m4_divert(-1)
2
3 # C++ skeleton for Bison
4
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6
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.
11
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.
16
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
21
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])
28 [
29 /* As a special exception, you may create a larger work that contains
30 part or all of the Bison parser skeleton and distribute that work
31 under terms of your choice, so long as that work isn't itself a
32 parser generator using the skeleton or a modified version thereof
33 as a parser skeleton. Alternatively, if you modify or redistribute
34 the parser skeleton itself, you may (at your option) remove this
35 special exception, which will cause the skeleton and the resulting
36 Bison output files to be licensed under the GNU General Public
37 License without this special exception.
38
39 This special exception was added by the Free Software Foundation in
40 version 2.2 of Bison. */
41
42 /**
43 ** \file position.hh
44 ** Define the ]b4_namespace[::position class.
45 */
46
47 #ifndef BISON_POSITION_HH
48 # define BISON_POSITION_HH
49
50 # include <iostream>
51 # include <string>
52
53 namespace ]b4_namespace[
54 {
55 /// Abstract a position.
56 class position
57 {
58 public:
59 ]m4_ifdef([b4_location_constructors], [
60 /// Construct a position.
61 position ()
62 : filename (0), line (1), column (0)
63 {
64 }
65
66 ])[
67 /// Initialization.
68 inline void initialize (]b4_filename_type[* fn)
69 {
70 filename = fn;
71 line = 1;
72 column = 0;
73 }
74
75 /** \name Line and Column related manipulators
76 ** \{ */
77 public:
78 /// (line related) Advance to the COUNT next lines.
79 inline void lines (int count = 1)
80 {
81 column = 0;
82 line += count;
83 }
84
85 /// (column related) Advance to the COUNT next columns.
86 inline void columns (int count = 1)
87 {
88 int leftmost = 0;
89 int current = column;
90 if (leftmost <= current + count)
91 column += count;
92 else
93 column = 0;
94 }
95 /** \} */
96
97 public:
98 /// File name to which this position refers.
99 ]b4_filename_type[* filename;
100 /// Current line number.
101 unsigned int line;
102 /// Current column number.
103 unsigned int column;
104 };
105
106 /// Add and assign a position.
107 inline const position&
108 operator+= (position& res, const int width)
109 {
110 res.columns (width);
111 return res;
112 }
113
114 /// Add two position objects.
115 inline const position
116 operator+ (const position& begin, const int width)
117 {
118 position res = begin;
119 return res += width;
120 }
121
122 /// Add and assign a position.
123 inline const position&
124 operator-= (position& res, const int width)
125 {
126 return res += -width;
127 }
128
129 /// Add two position objects.
130 inline const position
131 operator- (const position& begin, const int width)
132 {
133 return begin + -width;
134 }
135
136 /** \brief Intercept output stream redirection.
137 ** \param ostr the destination output stream
138 ** \param pos a reference to the position to redirect
139 */
140 inline std::ostream&
141 operator<< (std::ostream& ostr, const position& pos)
142 {
143 if (pos.filename)
144 ostr << *pos.filename << ':';
145 return ostr << pos.line << '.' << pos.column;
146 }
147
148 }
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])
153 [
154 /* As a special exception, you may create a larger work that contains
155 part or all of the Bison parser skeleton and distribute that work
156 under terms of your choice, so long as that work isn't itself a
157 parser generator using the skeleton or a modified version thereof
158 as a parser skeleton. Alternatively, if you modify or redistribute
159 the parser skeleton itself, you may (at your option) remove this
160 special exception, which will cause the skeleton and the resulting
161 Bison output files to be licensed under the GNU General Public
162 License without this special exception.
163
164 This special exception was added by the Free Software Foundation in
165 version 2.2 of Bison. */
166
167 /**
168 ** \file location.hh
169 ** Define the ]b4_namespace[::location class.
170 */
171
172 #ifndef BISON_LOCATION_HH
173 # define BISON_LOCATION_HH
174
175 # include <iostream>
176 # include <string>
177 # include "position.hh"
178
179 namespace ]b4_namespace[
180 {
181
182 /// Abstract a location.
183 class location
184 {
185 public:
186 ]m4_ifdef([b4_location_constructors], [
187 /// Construct a location.
188 location ()
189 : begin (), end ()
190 {
191 }
192
193 ])[
194 /// Initialization.
195 inline void initialize (]b4_filename_type[* fn)
196 {
197 begin.initialize (fn);
198 end = begin;
199 }
200
201 /** \name Line and Column related manipulators
202 ** \{ */
203 public:
204 /// Reset initial location to final location.
205 inline void step ()
206 {
207 begin = end;
208 }
209
210 /// Extend the current location to the COUNT next columns.
211 inline void columns (unsigned int count = 1)
212 {
213 end += count;
214 }
215
216 /// Extend the current location to the COUNT next lines.
217 inline void lines (unsigned int count = 1)
218 {
219 end.lines (count);
220 }
221 /** \} */
222
223
224 public:
225 /// Beginning of the located region.
226 position begin;
227 /// End of the located region.
228 position end;
229 };
230
231 /// Join two location objects to create a location.
232 inline const location operator+ (const location& begin, const location& end)
233 {
234 location res = begin;
235 res.end = end.end;
236 return res;
237 }
238
239 /// Add two location objects.
240 inline const location operator+ (const location& begin, unsigned int width)
241 {
242 location res = begin;
243 res.columns (width);
244 return res;
245 }
246
247 /// Add and assign a location.
248 inline location& operator+= (location& res, unsigned int width)
249 {
250 res.columns (width);
251 return res;
252 }
253
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]
278 m4_divert(-1)
279 m4_changecom([#])