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