]> git.saurik.com Git - bison.git/blame - data/location.cc
diagnostics: no longer pretty-print rules in error messages, carets suffice
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1# C++ skeleton for Bison
2
7d6bad19 3# Copyright (C) 2002-2013 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],
7d6bad19 19 [2002-2013])
a9ce3f54 20
93549bcd
AD
21# b4_position_define
22# ------------------
23# Define class position.
24m4_define([b4_position_define],
25[[ /// Abstract a position.
21667f64
AD
26 class position
27 {
21667f64 28 public:
0634493c 29]m4_ifdef([b4_location_constructors], [[
21667f64 30 /// Construct a position.
2091988f 31 explicit position (]b4_percent_define_get([[filename_type]])[* f = YY_NULL,
0634493c
AD
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)
21667f64
AD
37 {
38 }
21667f64 39
0634493c 40]])[
4626a15d 41 /// Initialization.
936c88d1
AD
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)
4626a15d
AD
45 {
46 filename = fn;
936c88d1
AD
47 line = l;
48 column = c;
4626a15d 49 }
21667f64
AD
50
51 /** \name Line and Column related manipulators
52 ** \{ */
21667f64 53 /// (line related) Advance to the COUNT next lines.
2a1bd0d7 54 void lines (int count = 1)
21667f64 55 {
0634493c 56 column = ]b4_location_initial_column[u;
21667f64
AD
57 line += count;
58 }
59
60 /// (column related) Advance to the COUNT next columns.
2a1bd0d7 61 void columns (int count = 1)
21667f64 62 {
cd48d21d 63 column = std::max (]b4_location_initial_column[u, column + count);
21667f64
AD
64 }
65 /** \} */
66
21667f64 67 /// File name to which this position refers.
a4e25e1d 68 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64
AD
69 /// Current line number.
70 unsigned int line;
71 /// Current column number.
72 unsigned int column;
73 };
74
75 /// Add and assign a position.
0e1ccdfa 76 inline position&
21667f64
AD
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.
0e1ccdfa 92 inline position&
21667f64
AD
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 }
592d0b1e 104]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
105 /// Compare two position objects.
106 inline bool
107 operator== (const position& pos1, const position& pos2)
108 {
56f772e9
AD
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)));
31b2b07e 114 }
21667f64 115
31b2b07e
JD
116 /// Compare two position objects.
117 inline bool
118 operator!= (const position& pos1, const position& pos2)
119 {
120 return !(pos1 == pos2);
121 }
122]])[
21667f64
AD
123 /** \brief Intercept output stream redirection.
124 ** \param ostr the destination output stream
125 ** \param pos a reference to the position to redirect
126 */
7ae57e2a
AD
127 template <typename YYChar>
128 inline std::basic_ostream<YYChar>&
129 operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
21667f64
AD
130 {
131 if (pos.filename)
132 ostr << *pos.filename << ':';
133 return ostr << pos.line << '.' << pos.column;
134 }
93549bcd 135]])
21667f64 136
21667f64 137
93549bcd
AD
138# b4_location_define
139# ------------------
140m4_define([b4_location_define],
141[[ /// Abstract a location.
21667f64
AD
142 class location
143 {
21667f64 144 public:
4626a15d 145]m4_ifdef([b4_location_constructors], [
0634493c
AD
146 /// Construct a location from \a b to \a e.
147 location (const position& b, const position& e)
148 : begin (b)
149 , end (e)
150 {
151 }
152
153 /// Construct a 0-width location in \a p.
154 explicit location (const position& p = position ())
155 : begin (p)
156 , end (p)
157 {
158 }
159
160 /// Construct a 0-width location in \a f, \a l, \a c.
161 explicit location (]b4_percent_define_get([[filename_type]])[* f,
162 unsigned int l = ]b4_location_initial_line[u,
163 unsigned int c = ]b4_location_initial_column[u)
164 : begin (f, l, c)
165 , end (f, l, c)
21667f64
AD
166 {
167 }
21667f64 168
4626a15d
AD
169])[
170 /// Initialization.
936c88d1
AD
171 void initialize (]b4_percent_define_get([[filename_type]])[* f = YY_NULL,
172 unsigned int l = ]b4_location_initial_line[u,
173 unsigned int c = ]b4_location_initial_column[u)
4626a15d 174 {
936c88d1 175 begin.initialize (f, l, c);
4626a15d
AD
176 end = begin;
177 }
21667f64
AD
178
179 /** \name Line and Column related manipulators
180 ** \{ */
181 public:
182 /// Reset initial location to final location.
2a1bd0d7 183 void step ()
21667f64
AD
184 {
185 begin = end;
186 }
187
188 /// Extend the current location to the COUNT next columns.
2a1bd0d7 189 void columns (unsigned int count = 1)
21667f64
AD
190 {
191 end += count;
192 }
193
194 /// Extend the current location to the COUNT next lines.
2a1bd0d7 195 void lines (unsigned int count = 1)
21667f64
AD
196 {
197 end.lines (count);
198 }
199 /** \} */
200
201
202 public:
203 /// Beginning of the located region.
204 position begin;
205 /// End of the located region.
206 position end;
207 };
208
209 /// Join two location objects to create a location.
210 inline const location operator+ (const location& begin, const location& end)
211 {
212 location res = begin;
213 res.end = end.end;
214 return res;
215 }
216
217 /// Add two location objects.
218 inline const location operator+ (const location& begin, unsigned int width)
219 {
220 location res = begin;
221 res.columns (width);
222 return res;
223 }
224
225 /// Add and assign a location.
226 inline location& operator+= (location& res, unsigned int width)
227 {
228 res.columns (width);
229 return res;
230 }
592d0b1e 231]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
232 /// Compare two location objects.
233 inline bool
234 operator== (const location& loc1, const location& loc2)
235 {
236 return loc1.begin == loc2.begin && loc1.end == loc2.end;
237 }
21667f64 238
31b2b07e
JD
239 /// Compare two location objects.
240 inline bool
241 operator!= (const location& loc1, const location& loc2)
242 {
243 return !(loc1 == loc2);
244 }
245]])[
21667f64
AD
246 /** \brief Intercept output stream redirection.
247 ** \param ostr the destination output stream
248 ** \param loc a reference to the location to redirect
249 **
250 ** Avoid duplicate information.
251 */
7ae57e2a
AD
252 template <typename YYChar>
253 inline std::basic_ostream<YYChar>&
254 operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
21667f64 255 {
aedcb6c0
AD
256 unsigned int end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;
257 ostr << loc.begin// << "(" << loc.end << ") "
258;
259 if (loc.end.filename
e9690142 260 && (!loc.begin.filename
aedcb6c0
AD
261 || *loc.begin.filename != *loc.end.filename))
262 ostr << '-' << loc.end.filename << ':' << loc.end.line << '.' << end_col;
263 else if (loc.begin.line < loc.end.line)
264 ostr << '-' << loc.end.line << '.' << end_col;
265 else if (loc.begin.column < end_col)
266 ostr << '-' << end_col;
21667f64
AD
267 return ostr;
268 }
93549bcd 269]])
21667f64 270
93549bcd 271
93549bcd 272b4_defines_if([
064e42b0 273b4_output_begin([b4_dir_prefix[]position.hh])
93549bcd
AD
274b4_copyright([Positions for Bison parsers in C++])[
275
276/**
277 ** \file ]b4_dir_prefix[position.hh
278 ** Define the ]b4_namespace_ref[::position class.
279 */
280
281]b4_cpp_guard_open([b4_dir_prefix[]position.hh])[
282
283# include <algorithm> // std::max
2b45240d 284# include <iostream>
93549bcd
AD
285# include <string>
286
287]b4_null_define[
288
289]b4_namespace_open[
290]b4_position_define[
793fbca5 291]b4_namespace_close[
93549bcd 292]b4_cpp_guard_close([b4_dir_prefix[]position.hh])
064e42b0
AD
293b4_output_end()
294
295
296b4_output_begin([b4_dir_prefix[]location.hh])
93549bcd
AD
297b4_copyright([Locations for Bison parsers in C++])[
298
299/**
300 ** \file ]b4_dir_prefix[location.hh
301 ** Define the ]b4_namespace_ref[::location class.
302 */
303
304]b4_cpp_guard_open([b4_dir_prefix[]location.hh])[
21667f64 305
93549bcd
AD
306# include "position.hh"
307
308]b4_namespace_open[
309]b4_location_define[
310]b4_namespace_close[
22172d47 311]b4_cpp_guard_close([b4_dir_prefix[]location.hh])
1c7ec959 312b4_output_end()
064e42b0
AD
313])
314
315
316m4_popdef([b4_copyright_years])