]> git.saurik.com Git - bison.git/blob - data/location.cc
Have glr.cc pass (some of) the calc.at tests.
[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([Position class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
27
28 /**
29 ** \file position.hh
30 ** Define the yy::position class.
31 */
32
33 #ifndef BISON_POSITION_HH
34 # define BISON_POSITION_HH
35
36 # include <iostream>
37 # include <string>
38
39 namespace yy
40 {
41 /// Abstract a position.
42 class position
43 {
44 public:
45 ]m4_ifdef([b4_location_constructors], [
46 /// Construct a position.
47 position ()
48 : filename (0), line (1), column (0)
49 {
50 }
51
52 ])[
53 /// Initialization.
54 inline void initialize (]b4_filename_type[* fn)
55 {
56 filename = fn;
57 line = 1;
58 column = 0;
59 }
60
61 /** \name Line and Column related manipulators
62 ** \{ */
63 public:
64 /// (line related) Advance to the COUNT next lines.
65 inline void lines (int count = 1)
66 {
67 column = 0;
68 line += count;
69 }
70
71 /// (column related) Advance to the COUNT next columns.
72 inline void columns (int count = 1)
73 {
74 int leftmost = 0;
75 int current = column;
76 if (leftmost <= current + count)
77 column += count;
78 else
79 column = 0;
80 }
81 /** \} */
82
83 public:
84 /// File name to which this position refers.
85 ]b4_filename_type[* filename;
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 }
121
122 /** \brief Intercept output stream redirection.
123 ** \param ostr the destination output stream
124 ** \param pos a reference to the position to redirect
125 */
126 inline std::ostream&
127 operator<< (std::ostream& ostr, const position& pos)
128 {
129 if (pos.filename)
130 ostr << *pos.filename << ':';
131 return ostr << pos.line << '.' << pos.column;
132 }
133
134 }
135 #endif // not BISON_POSITION_HH]
136 @output b4_dir_prefix[]location.hh
137 b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
138
139 /**
140 ** \file location.hh
141 ** Define the yy::location class.
142 */
143
144 #ifndef BISON_LOCATION_HH
145 # define BISON_LOCATION_HH
146
147 # include <iostream>
148 # include <string>
149 # include "position.hh"
150
151 namespace yy
152 {
153
154 /// Abstract a location.
155 class location
156 {
157 public:
158 ]m4_ifdef([b4_location_constructors], [
159 /// Construct a location.
160 location ()
161 : begin (), end ()
162 {
163 }
164
165 ])[
166 /// Initialization.
167 inline void initialize (]b4_filename_type[* fn)
168 {
169 begin.initialize (fn);
170 end = begin;
171 }
172
173 /** \name Line and Column related manipulators
174 ** \{ */
175 public:
176 /// Reset initial location to final location.
177 inline void step ()
178 {
179 begin = end;
180 }
181
182 /// Extend the current location to the COUNT next columns.
183 inline void columns (unsigned int count = 1)
184 {
185 end += count;
186 }
187
188 /// Extend the current location to the COUNT next lines.
189 inline void lines (unsigned int count = 1)
190 {
191 end.lines (count);
192 }
193 /** \} */
194
195
196 public:
197 /// Beginning of the located region.
198 position begin;
199 /// End of the located region.
200 position end;
201 };
202
203 /// Join two location objects to create a location.
204 inline const location operator+ (const location& begin, const location& end)
205 {
206 location res = begin;
207 res.end = end.end;
208 return res;
209 }
210
211 /// Add two location objects.
212 inline const location operator+ (const location& begin, unsigned int width)
213 {
214 location res = begin;
215 res.columns (width);
216 return res;
217 }
218
219 /// Add and assign a location.
220 inline location& operator+= (location& res, unsigned int width)
221 {
222 res.columns (width);
223 return res;
224 }
225
226 /** \brief Intercept output stream redirection.
227 ** \param ostr the destination output stream
228 ** \param loc a reference to the location to redirect
229 **
230 ** Avoid duplicate information.
231 */
232 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
233 {
234 position last = loc.end - 1;
235 ostr << loc.begin;
236 if (last.filename
237 && (!loc.begin.filename
238 || *loc.begin.filename != *last.filename))
239 ostr << '-' << last;
240 else if (loc.begin.line != last.line)
241 ostr << '-' << last.line << '.' << last.column;
242 else if (loc.begin.column != last.column)
243 ostr << '-' << last.column;
244 return ostr;
245 }
246
247 }
248
249 #endif // not BISON_LOCATION_HH]
250 m4_divert(-1)
251 m4_changecom([#])