]>
git.saurik.com Git - bison.git/blob - data/location.cc
3 # C++ skeleton for Bison
5 # Copyright (C) 2002, 2003, 2004, 2005 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 m4_include(b4_pkgdatadir
/[c
++.m4
])
24 # We do want M4 expansion after # for CPP macros.
27 @output b4_dir_prefix
[]position
.hh
28 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004, 2005])dnl
29 [/* As a special exception, when this parser skeleton is copied by
30 Bison into a Bison output file, you may use that output file
31 without restriction. This special exception was added by the Free
32 Software Foundation for C++ parsers in version 2.2 of Bison. */
36 ** Define the yy::position class.
39 #ifndef BISON_POSITION_HH
40 # define BISON_POSITION_HH
47 /// Abstract a position.
51 ]m4_ifdef([b4_location_constructors
], [
52 /// Construct a position.
54 : filename (0), line (1), column (0)
60 inline void initialize (]b4_filename_type
[* fn
)
67 /** \name Line and Column related manipulators
70 /// (line related) Advance to the COUNT next lines.
71 inline void lines (int count
= 1)
77 /// (column related) Advance to the COUNT next columns.
78 inline void columns (int count
= 1)
82 if (leftmost
<= current
+ count
)
90 /// File name to which this position refers.
91 ]b4_filename_type
[* filename
;
92 /// Current line number.
94 /// Current column number.
98 /// Add and assign a position.
99 inline const position
&
100 operator+= (position
& res
, const int width
)
106 /// Add two position objects.
107 inline const position
108 operator+ (const position
& begin
, const int width
)
110 position res
= begin
;
114 /// Add and assign a position.
115 inline const position
&
116 operator-= (position
& res
, const int width
)
118 return res
+= -width
;
121 /// Add two position objects.
122 inline const position
123 operator- (const position
& begin
, const int width
)
125 return begin
+ -width
;
128 /** \brief Intercept output stream redirection.
129 ** \param ostr the destination output stream
130 ** \param pos a reference to the position to redirect
133 operator<< (std::ostream
& ostr
, const position
& pos
)
136 ostr
<< *pos
.filename
<< ':';
137 return ostr
<< pos
.line
<< '.' << pos
.column
;
141 #endif // not BISON_POSITION_HH]
142 @output b4_dir_prefix
[]location
.hh
143 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004, 2005])[
147 ** Define the yy::location class.
150 #ifndef BISON_LOCATION_HH
151 # define BISON_LOCATION_HH
155 # include "position.hh"
160 /// Abstract a location.
164 ]m4_ifdef([b4_location_constructors
], [
165 /// Construct a location.
173 inline void initialize (]b4_filename_type
[* fn
)
175 begin
.initialize (fn
);
179 /** \name Line and Column related manipulators
182 /// Reset initial location to final location.
188 /// Extend the current location to the COUNT next columns.
189 inline void columns (unsigned int count
= 1)
194 /// Extend the current location to the COUNT next lines.
195 inline void lines (unsigned int count
= 1)
203 /// Beginning of the located region.
205 /// End of the located region.
209 /// Join two location objects to create a location.
210 inline const location
operator+ (const location
& begin
, const location
& end
)
212 location res
= begin
;
217 /// Add two location objects.
218 inline const location
operator+ (const location
& begin
, unsigned int width
)
220 location res
= begin
;
225 /// Add and assign a location.
226 inline location
& operator+= (location
& res
, unsigned int width
)
232 /** \brief Intercept output stream redirection.
233 ** \param ostr the destination output stream
234 ** \param loc a reference to the location to redirect
236 ** Avoid duplicate information.
238 inline std::ostream
& operator<< (std::ostream
& ostr
, const location
& loc
)
240 position last
= loc
.end
- 1;
243 && (!loc
.begin
.filename
244 || *loc
.begin
.filename
!= *last
.filename
))
246 else if (loc
.begin
.line
!= last
.line
)
247 ostr
<< '-' << last
.line
<< '.' << last
.column
;
248 else if (loc
.begin
.column
!= last
.column
)
249 ostr
<< '-' << last
.column
;
255 #endif // not BISON_LOCATION_HH]