]>
git.saurik.com Git - bison.git/blob - data/location.cc
869320e799a8d97667445ba2444293292f6c3664
3 # C++ skeleton for Bison
5 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
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.
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.
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
22 # We do want M4 expansion after # for CPP macros.
25 @output b4_dir_prefix
[]position
.hh
26 b4_copyright([Positions
for Bison parsers in C
++],
27 [2002, 2003, 2004, 2005, 2006])[
31 ** Define the ]b4_namespace[::position class.
34 #ifndef BISON_POSITION_HH
35 # define BISON_POSITION_HH
40 namespace ]b4_namespace
[
42 /// Abstract a position.
46 ]m4_ifdef([b4_location_constructors
], [
47 /// Construct a position.
49 : filename (0), line (1), column (0)
55 inline void initialize (]b4_filename_type
[* fn
)
62 /** \name Line and Column related manipulators
65 /// (line related) Advance to the COUNT next lines.
66 inline void lines (int count
= 1)
72 /// (column related) Advance to the COUNT next columns.
73 inline void columns (int count
= 1)
77 if (leftmost
<= current
+ count
)
85 /// File name to which this position refers.
86 ]b4_filename_type
[* filename
;
87 /// Current line number.
89 /// Current column number.
93 /// Add and assign a position.
94 inline const position
&
95 operator+= (position
& res
, const int width
)
101 /// Add two position objects.
102 inline const position
103 operator+ (const position
& begin
, const int width
)
105 position res
= begin
;
109 /// Add and assign a position.
110 inline const position
&
111 operator-= (position
& res
, const int width
)
113 return res
+= -width
;
116 /// Add two position objects.
117 inline const position
118 operator- (const position
& begin
, const int width
)
120 return begin
+ -width
;
123 /** \brief Intercept output stream redirection.
124 ** \param ostr the destination output stream
125 ** \param pos a reference to the position to redirect
128 operator<< (std::ostream
& ostr
, const position
& pos
)
131 ostr
<< *pos
.filename
<< ':';
132 return ostr
<< pos
.line
<< '.' << pos
.column
;
136 #endif // not BISON_POSITION_HH]
137 @output b4_dir_prefix
[]location
.hh
138 b4_copyright([Locations
for Bison parsers in C
++],
139 [2002, 2003, 2004, 2005, 2006])[
143 ** Define the ]b4_namespace[::location class.
146 #ifndef BISON_LOCATION_HH
147 # define BISON_LOCATION_HH
151 # include "position.hh"
153 namespace ]b4_namespace
[
156 /// Abstract a location.
160 ]m4_ifdef([b4_location_constructors
], [
161 /// Construct a location.
169 inline void initialize (]b4_filename_type
[* fn
)
171 begin
.initialize (fn
);
175 /** \name Line and Column related manipulators
178 /// Reset initial location to final location.
184 /// Extend the current location to the COUNT next columns.
185 inline void columns (unsigned int count
= 1)
190 /// Extend the current location to the COUNT next lines.
191 inline void lines (unsigned int count
= 1)
199 /// Beginning of the located region.
201 /// End of the located region.
205 /// Join two location objects to create a location.
206 inline const location
operator+ (const location
& begin
, const location
& end
)
208 location res
= begin
;
213 /// Add two location objects.
214 inline const location
operator+ (const location
& begin
, unsigned int width
)
216 location res
= begin
;
221 /// Add and assign a location.
222 inline location
& operator+= (location
& res
, unsigned int width
)
228 /** \brief Intercept output stream redirection.
229 ** \param ostr the destination output stream
230 ** \param loc a reference to the location to redirect
232 ** Avoid duplicate information.
234 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
236 position last
= loc
.end
- 1;
239 && (!loc
.begin
.filename
240 || *loc
.begin
.filename
!= *last
.filename
))
242 else if (loc
.begin
.line
!= last
.line
)
243 ostr
<< '-' << last
.line
<< '.' << last
.column
;
244 else if (loc
.begin
.column
!= last
.column
)
245 ostr
<< '-' << last
.column
;
251 #endif // not BISON_LOCATION_HH]