]> git.saurik.com Git - bison.git/blame - data/location.cc
Formatting changes.
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1# C++ skeleton for Bison
2
59c544c2 3# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
279cabb6 4# Inc.
21667f64 5
f16b0819 6# This program is free software: you can redistribute it and/or modify
21667f64 7# it under the terms of the GNU General Public License as published by
f16b0819 8# the Free Software Foundation, either version 3 of the License, or
21667f64 9# (at your option) any later version.
f16b0819 10#
21667f64
AD
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
f16b0819 15#
21667f64 16# You should have received a copy of the GNU General Public License
f16b0819 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
21667f64 18
a9ce3f54
AD
19m4_pushdef([b4_copyright_years],
20 [2002, 2003, 2004, 2005, 2006, 2007, 2008])
21
21667f64
AD
22# We do want M4 expansion after # for CPP macros.
23m4_changecom()
08af01c2 24m4_divert_push(0)dnl
59c544c2 25@output(b4_dir_prefix[]position.hh@)@
a9ce3f54 26b4_copyright([Positions for Bison parsers in C++])[
21667f64
AD
27
28/**
29 ** \file position.hh
793fbca5 30 ** Define the ]b4_namespace_ref[::position class.
21667f64
AD
31 */
32
33#ifndef BISON_POSITION_HH
34# define BISON_POSITION_HH
35
36# include <iostream>
37# include <string>
cd48d21d 38# include <algorithm>
21667f64 39
793fbca5 40]b4_namespace_open[
21667f64
AD
41 /// Abstract a position.
42 class position
43 {
21667f64 44 public:
4626a15d 45]m4_ifdef([b4_location_constructors], [
21667f64 46 /// Construct a position.
4626a15d 47 position ()
cd48d21d 48 : filename (0), line (]b4_location_initial_line[), column (]b4_location_initial_column[)
21667f64
AD
49 {
50 }
21667f64 51
4626a15d
AD
52])[
53 /// Initialization.
a4e25e1d 54 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
55 {
56 filename = fn;
cd48d21d
AD
57 line = ]b4_location_initial_line[;
58 column = ]b4_location_initial_column[;
4626a15d 59 }
21667f64
AD
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 {
cd48d21d 67 column = ]b4_location_initial_column[;
21667f64
AD
68 line += count;
69 }
70
71 /// (column related) Advance to the COUNT next columns.
72 inline void columns (int count = 1)
73 {
cd48d21d 74 column = std::max (]b4_location_initial_column[u, column + count);
21667f64
AD
75 }
76 /** \} */
77
78 public:
79 /// File name to which this position refers.
a4e25e1d 80 ]b4_percent_define_get([[filename_type]])[* filename;
21667f64
AD
81 /// Current line number.
82 unsigned int line;
83 /// Current column number.
84 unsigned int column;
85 };
86
87 /// Add and assign a position.
88 inline const position&
89 operator+= (position& res, const int width)
90 {
91 res.columns (width);
92 return res;
93 }
94
95 /// Add two position objects.
96 inline const position
97 operator+ (const position& begin, const int width)
98 {
99 position res = begin;
100 return res += width;
101 }
102
103 /// Add and assign a position.
104 inline const position&
105 operator-= (position& res, const int width)
106 {
107 return res += -width;
108 }
109
110 /// Add two position objects.
111 inline const position
112 operator- (const position& begin, const int width)
113 {
114 return begin + -width;
115 }
592d0b1e 116]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
117 /// Compare two position objects.
118 inline bool
119 operator== (const position& pos1, const position& pos2)
120 {
121 return
122 (pos1.filename == pos2.filename
123 || pos1.filename && pos2.filename && *pos1.filename == *pos2.filename)
124 && pos1.line == pos2.line && pos1.column == pos2.column;
125 }
21667f64 126
31b2b07e
JD
127 /// Compare two position objects.
128 inline bool
129 operator!= (const position& pos1, const position& pos2)
130 {
131 return !(pos1 == pos2);
132 }
133]])[
21667f64
AD
134 /** \brief Intercept output stream redirection.
135 ** \param ostr the destination output stream
136 ** \param pos a reference to the position to redirect
137 */
138 inline std::ostream&
139 operator<< (std::ostream& ostr, const position& pos)
140 {
141 if (pos.filename)
142 ostr << *pos.filename << ':';
143 return ostr << pos.line << '.' << pos.column;
144 }
145
793fbca5 146]b4_namespace_close[
21667f64 147#endif // not BISON_POSITION_HH]
59c544c2 148@output(b4_dir_prefix[]location.hh@)@
a9ce3f54 149b4_copyright([Locations for Bison parsers in C++])[
21667f64
AD
150
151/**
152 ** \file location.hh
793fbca5 153 ** Define the ]b4_namespace_ref[::location class.
21667f64
AD
154 */
155
156#ifndef BISON_LOCATION_HH
157# define BISON_LOCATION_HH
158
159# include <iostream>
160# include <string>
161# include "position.hh"
162
793fbca5 163]b4_namespace_open[
21667f64
AD
164
165 /// Abstract a location.
166 class location
167 {
21667f64 168 public:
4626a15d 169]m4_ifdef([b4_location_constructors], [
21667f64 170 /// Construct a location.
4626a15d
AD
171 location ()
172 : begin (), end ()
21667f64
AD
173 {
174 }
21667f64 175
4626a15d
AD
176])[
177 /// Initialization.
a4e25e1d 178 inline void initialize (]b4_percent_define_get([[filename_type]])[* fn)
4626a15d
AD
179 {
180 begin.initialize (fn);
181 end = begin;
182 }
21667f64
AD
183
184 /** \name Line and Column related manipulators
185 ** \{ */
186 public:
187 /// Reset initial location to final location.
188 inline void step ()
189 {
190 begin = end;
191 }
192
193 /// Extend the current location to the COUNT next columns.
194 inline void columns (unsigned int count = 1)
195 {
196 end += count;
197 }
198
199 /// Extend the current location to the COUNT next lines.
200 inline void lines (unsigned int count = 1)
201 {
202 end.lines (count);
203 }
204 /** \} */
205
206
207 public:
208 /// Beginning of the located region.
209 position begin;
210 /// End of the located region.
211 position end;
212 };
213
214 /// Join two location objects to create a location.
215 inline const location operator+ (const location& begin, const location& end)
216 {
217 location res = begin;
218 res.end = end.end;
219 return res;
220 }
221
222 /// Add two location objects.
223 inline const location operator+ (const location& begin, unsigned int width)
224 {
225 location res = begin;
226 res.columns (width);
227 return res;
228 }
229
230 /// Add and assign a location.
231 inline location& operator+= (location& res, unsigned int width)
232 {
233 res.columns (width);
234 return res;
235 }
592d0b1e 236]b4_percent_define_flag_if([[define_location_comparison]], [[
31b2b07e
JD
237 /// Compare two location objects.
238 inline bool
239 operator== (const location& loc1, const location& loc2)
240 {
241 return loc1.begin == loc2.begin && loc1.end == loc2.end;
242 }
21667f64 243
31b2b07e
JD
244 /// Compare two location objects.
245 inline bool
246 operator!= (const location& loc1, const location& loc2)
247 {
248 return !(loc1 == loc2);
249 }
250]])[
21667f64
AD
251 /** \brief Intercept output stream redirection.
252 ** \param ostr the destination output stream
253 ** \param loc a reference to the location to redirect
254 **
255 ** Avoid duplicate information.
256 */
257 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
258 {
259 position last = loc.end - 1;
260 ostr << loc.begin;
261 if (last.filename
262 && (!loc.begin.filename
263 || *loc.begin.filename != *last.filename))
264 ostr << '-' << last;
265 else if (loc.begin.line != last.line)
266 ostr << '-' << last.line << '.' << last.column;
267 else if (loc.begin.column != last.column)
268 ostr << '-' << last.column;
269 return ostr;
270 }
271
793fbca5 272]b4_namespace_close[
21667f64
AD
273
274#endif // not BISON_LOCATION_HH]
08af01c2 275m4_divert_pop(0)
a9ce3f54 276m4_popdef([b4_copyright_years])dnl
21667f64 277m4_changecom([#])