]>
git.saurik.com Git - bison.git/blob - data/location.cc
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])
29 /* As a special exception, you may create a larger work that contains
30 part or all of the Bison parser skeleton and distribute that work
31 under terms of your choice, so long as that work isn't itself a
32 parser generator using the skeleton or a modified version thereof
33 as a parser skeleton. Alternatively, if you modify or redistribute
34 the parser skeleton itself, you may (at your option) remove this
35 special exception, which will cause the skeleton and the resulting
36 Bison output files to be licensed under the GNU General Public
37 License without this special exception.
39 This special exception was added by the Free Software Foundation in
40 version 2.2 of Bison. */
44 ** Define the ]b4_namespace[::position class.
47 #ifndef BISON_POSITION_HH
48 # define BISON_POSITION_HH
53 namespace ]b4_namespace
[
55 /// Abstract a position.
59 ]m4_ifdef([b4_location_constructors
], [
60 /// Construct a position.
62 : filename (0), line (1), column (0)
68 inline void initialize (]b4_filename_type
[* fn
)
75 /** \name Line and Column related manipulators
78 /// (line related) Advance to the COUNT next lines.
79 inline void lines (int count
= 1)
85 /// (column related) Advance to the COUNT next columns.
86 inline void columns (int count
= 1)
90 if (leftmost
<= current
+ count
)
98 /// File name to which this position refers.
99 ]b4_filename_type
[* filename
;
100 /// Current line number.
102 /// Current column number.
106 /// Add and assign a position.
107 inline const position
&
108 operator+= (position
& res
, const int width
)
114 /// Add two position objects.
115 inline const position
116 operator+ (const position
& begin
, const int width
)
118 position res
= begin
;
122 /// Add and assign a position.
123 inline const position
&
124 operator-= (position
& res
, const int width
)
126 return res
+= -width
;
129 /// Add two position objects.
130 inline const position
131 operator- (const position
& begin
, const int width
)
133 return begin
+ -width
;
136 /** \brief Intercept output stream redirection.
137 ** \param ostr the destination output stream
138 ** \param pos a reference to the position to redirect
141 operator<< (std::ostream
& ostr
, const position
& pos
)
144 ostr
<< *pos
.filename
<< ':';
145 return ostr
<< pos
.line
<< '.' << pos
.column
;
149 #endif // not BISON_POSITION_HH]
150 @output b4_dir_prefix
[]location
.hh
151 b4_copyright([Locations
for Bison parsers in C
++],
152 [2002, 2003, 2004, 2005, 2006])
154 /* As a special exception, you may create a larger work that contains
155 part or all of the Bison parser skeleton and distribute that work
156 under terms of your choice, so long as that work isn't itself a
157 parser generator using the skeleton or a modified version thereof
158 as a parser skeleton. Alternatively, if you modify or redistribute
159 the parser skeleton itself, you may (at your option) remove this
160 special exception, which will cause the skeleton and the resulting
161 Bison output files to be licensed under the GNU General Public
162 License without this special exception.
164 This special exception was added by the Free Software Foundation in
165 version 2.2 of Bison. */
169 ** Define the ]b4_namespace[::location class.
172 #ifndef BISON_LOCATION_HH
173 # define BISON_LOCATION_HH
177 # include "position.hh"
179 namespace ]b4_namespace
[
182 /// Abstract a location.
186 ]m4_ifdef([b4_location_constructors
], [
187 /// Construct a location.
195 inline void initialize (]b4_filename_type
[* fn
)
197 begin
.initialize (fn
);
201 /** \name Line and Column related manipulators
204 /// Reset initial location to final location.
210 /// Extend the current location to the COUNT next columns.
211 inline void columns (unsigned int count
= 1)
216 /// Extend the current location to the COUNT next lines.
217 inline void lines (unsigned int count
= 1)
225 /// Beginning of the located region.
227 /// End of the located region.
231 /// Join two location objects to create a location.
232 inline const location
operator+ (const location
& begin
, const location
& end
)
234 location res
= begin
;
239 /// Add two location objects.
240 inline const location
operator+ (const location
& begin
, unsigned int width
)
242 location res
= begin
;
247 /// Add and assign a location.
248 inline location
& operator+= (location
& res
, unsigned int width
)
254 /** \brief Intercept output stream redirection.
255 ** \param ostr the destination output stream
256 ** \param loc a reference to the location to redirect
258 ** Avoid duplicate information.
260 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
262 position last
= loc
.end
- 1;
265 && (!loc
.begin
.filename
266 || *loc
.begin
.filename
!= *last
.filename
))
268 else if (loc
.begin
.line
!= last
.line
)
269 ostr
<< '-' << last
.line
<< '.' << last
.column
;
270 else if (loc
.begin
.column
!= last
.column
)
271 ostr
<< '-' << last
.column
;
277 #endif // not BISON_LOCATION_HH]