]> git.saurik.com Git - bison.git/blame - data/location.cc
* NEWS: Reword %destructor vs YYABORT etc.
[bison.git] / data / location.cc
CommitLineData
21667f64
AD
1m4_divert(-1)
2
3# C++ skeleton for Bison
4
5# Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6
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.
11
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.
16
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
20# 02110-1301 USA
21
22m4_include(b4_pkgdatadir/[c++.m4])
23
24# We do want M4 expansion after # for CPP macros.
25m4_changecom()
26m4_divert(0)dnl
27@output b4_dir_prefix[]position.hh
af3412cd
PE
28b4_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. */
21667f64
AD
33
34/**
35 ** \file position.hh
4626a15d 36 ** Define the yy::position class.
21667f64
AD
37 */
38
39#ifndef BISON_POSITION_HH
40# define BISON_POSITION_HH
41
42# include <iostream>
43# include <string>
44
45namespace yy
46{
47 /// Abstract a position.
48 class position
49 {
21667f64 50 public:
4626a15d 51]m4_ifdef([b4_location_constructors], [
21667f64 52 /// Construct a position.
4626a15d
AD
53 position ()
54 : filename (0), line (1), column (0)
21667f64
AD
55 {
56 }
21667f64 57
4626a15d
AD
58])[
59 /// Initialization.
60 inline void initialize (]b4_filename_type[* fn)
61 {
62 filename = fn;
63 line = 1;
64 column = 0;
65 }
21667f64
AD
66
67 /** \name Line and Column related manipulators
68 ** \{ */
69 public:
70 /// (line related) Advance to the COUNT next lines.
71 inline void lines (int count = 1)
72 {
4f6e011e 73 column = 0;
21667f64
AD
74 line += count;
75 }
76
77 /// (column related) Advance to the COUNT next columns.
78 inline void columns (int count = 1)
79 {
4f6e011e 80 int leftmost = 0;
21667f64
AD
81 int current = column;
82 if (leftmost <= current + count)
83 column += count;
84 else
4f6e011e 85 column = 0;
21667f64
AD
86 }
87 /** \} */
88
89 public:
90 /// File name to which this position refers.
91 ]b4_filename_type[* filename;
92 /// Current line number.
93 unsigned int line;
94 /// Current column number.
95 unsigned int column;
96 };
97
98 /// Add and assign a position.
99 inline const position&
100 operator+= (position& res, const int width)
101 {
102 res.columns (width);
103 return res;
104 }
105
106 /// Add two position objects.
107 inline const position
108 operator+ (const position& begin, const int width)
109 {
110 position res = begin;
111 return res += width;
112 }
113
114 /// Add and assign a position.
115 inline const position&
116 operator-= (position& res, const int width)
117 {
118 return res += -width;
119 }
120
121 /// Add two position objects.
122 inline const position
123 operator- (const position& begin, const int width)
124 {
125 return begin + -width;
126 }
127
128 /** \brief Intercept output stream redirection.
129 ** \param ostr the destination output stream
130 ** \param pos a reference to the position to redirect
131 */
132 inline std::ostream&
133 operator<< (std::ostream& ostr, const position& pos)
134 {
135 if (pos.filename)
136 ostr << *pos.filename << ':';
137 return ostr << pos.line << '.' << pos.column;
138 }
139
140}
141#endif // not BISON_POSITION_HH]
142@output b4_dir_prefix[]location.hh
143b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
144
145/**
146 ** \file location.hh
4626a15d 147 ** Define the yy::location class.
21667f64
AD
148 */
149
150#ifndef BISON_LOCATION_HH
151# define BISON_LOCATION_HH
152
153# include <iostream>
154# include <string>
155# include "position.hh"
156
157namespace yy
158{
159
160 /// Abstract a location.
161 class location
162 {
21667f64 163 public:
4626a15d 164]m4_ifdef([b4_location_constructors], [
21667f64 165 /// Construct a location.
4626a15d
AD
166 location ()
167 : begin (), end ()
21667f64
AD
168 {
169 }
21667f64 170
4626a15d
AD
171])[
172 /// Initialization.
173 inline void initialize (]b4_filename_type[* fn)
174 {
175 begin.initialize (fn);
176 end = begin;
177 }
21667f64
AD
178
179 /** \name Line and Column related manipulators
180 ** \{ */
181 public:
182 /// Reset initial location to final location.
183 inline void step ()
184 {
185 begin = end;
186 }
187
188 /// Extend the current location to the COUNT next columns.
189 inline void columns (unsigned int count = 1)
190 {
191 end += count;
192 }
193
194 /// Extend the current location to the COUNT next lines.
195 inline void lines (unsigned int count = 1)
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 }
231
232 /** \brief Intercept output stream redirection.
233 ** \param ostr the destination output stream
234 ** \param loc a reference to the location to redirect
235 **
236 ** Avoid duplicate information.
237 */
238 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
239 {
240 position last = loc.end - 1;
241 ostr << loc.begin;
242 if (last.filename
243 && (!loc.begin.filename
244 || *loc.begin.filename != *last.filename))
245 ostr << '-' << last;
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;
250 return ostr;
251 }
252
253}
254
255#endif // not BISON_LOCATION_HH]
256m4_divert(-1)
257m4_changecom([#])