]> git.saurik.com Git - bison.git/blame - data/location.cc
* src/output.c (output_skeleton): Don't hard wire the inclusion of
[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
8bb4c753 28b4_copyright([Position class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
21667f64
AD
29
30/**
31 ** \file position.hh
4626a15d 32 ** Define the yy::position class.
21667f64
AD
33 */
34
35#ifndef BISON_POSITION_HH
36# define BISON_POSITION_HH
37
38# include <iostream>
39# include <string>
40
41namespace yy
42{
43 /// Abstract a position.
44 class position
45 {
21667f64 46 public:
4626a15d 47]m4_ifdef([b4_location_constructors], [
21667f64 48 /// Construct a position.
4626a15d
AD
49 position ()
50 : filename (0), line (1), column (0)
21667f64
AD
51 {
52 }
21667f64 53
4626a15d
AD
54])[
55 /// Initialization.
56 inline void initialize (]b4_filename_type[* fn)
57 {
58 filename = fn;
59 line = 1;
60 column = 0;
61 }
21667f64
AD
62
63 /** \name Line and Column related manipulators
64 ** \{ */
65 public:
66 /// (line related) Advance to the COUNT next lines.
67 inline void lines (int count = 1)
68 {
4f6e011e 69 column = 0;
21667f64
AD
70 line += count;
71 }
72
73 /// (column related) Advance to the COUNT next columns.
74 inline void columns (int count = 1)
75 {
4f6e011e 76 int leftmost = 0;
21667f64
AD
77 int current = column;
78 if (leftmost <= current + count)
79 column += count;
80 else
4f6e011e 81 column = 0;
21667f64
AD
82 }
83 /** \} */
84
85 public:
86 /// File name to which this position refers.
87 ]b4_filename_type[* filename;
88 /// Current line number.
89 unsigned int line;
90 /// Current column number.
91 unsigned int column;
92 };
93
94 /// Add and assign a position.
95 inline const position&
96 operator+= (position& res, const int width)
97 {
98 res.columns (width);
99 return res;
100 }
101
102 /// Add two position objects.
103 inline const position
104 operator+ (const position& begin, const int width)
105 {
106 position res = begin;
107 return res += width;
108 }
109
110 /// Add and assign a position.
111 inline const position&
112 operator-= (position& res, const int width)
113 {
114 return res += -width;
115 }
116
117 /// Add two position objects.
118 inline const position
119 operator- (const position& begin, const int width)
120 {
121 return begin + -width;
122 }
123
124 /** \brief Intercept output stream redirection.
125 ** \param ostr the destination output stream
126 ** \param pos a reference to the position to redirect
127 */
128 inline std::ostream&
129 operator<< (std::ostream& ostr, const position& pos)
130 {
131 if (pos.filename)
132 ostr << *pos.filename << ':';
133 return ostr << pos.line << '.' << pos.column;
134 }
135
136}
137#endif // not BISON_POSITION_HH]
138@output b4_dir_prefix[]location.hh
139b4_copyright([Location class for Bison C++ parsers], [2002, 2003, 2004, 2005])[
140
141/**
142 ** \file location.hh
4626a15d 143 ** Define the yy::location class.
21667f64
AD
144 */
145
146#ifndef BISON_LOCATION_HH
147# define BISON_LOCATION_HH
148
149# include <iostream>
150# include <string>
151# include "position.hh"
152
153namespace yy
154{
155
156 /// Abstract a location.
157 class location
158 {
21667f64 159 public:
4626a15d 160]m4_ifdef([b4_location_constructors], [
21667f64 161 /// Construct a location.
4626a15d
AD
162 location ()
163 : begin (), end ()
21667f64
AD
164 {
165 }
21667f64 166
4626a15d
AD
167])[
168 /// Initialization.
169 inline void initialize (]b4_filename_type[* fn)
170 {
171 begin.initialize (fn);
172 end = begin;
173 }
21667f64
AD
174
175 /** \name Line and Column related manipulators
176 ** \{ */
177 public:
178 /// Reset initial location to final location.
179 inline void step ()
180 {
181 begin = end;
182 }
183
184 /// Extend the current location to the COUNT next columns.
185 inline void columns (unsigned int count = 1)
186 {
187 end += count;
188 }
189
190 /// Extend the current location to the COUNT next lines.
191 inline void lines (unsigned int count = 1)
192 {
193 end.lines (count);
194 }
195 /** \} */
196
197
198 public:
199 /// Beginning of the located region.
200 position begin;
201 /// End of the located region.
202 position end;
203 };
204
205 /// Join two location objects to create a location.
206 inline const location operator+ (const location& begin, const location& end)
207 {
208 location res = begin;
209 res.end = end.end;
210 return res;
211 }
212
213 /// Add two location objects.
214 inline const location operator+ (const location& begin, unsigned int width)
215 {
216 location res = begin;
217 res.columns (width);
218 return res;
219 }
220
221 /// Add and assign a location.
222 inline location& operator+= (location& res, unsigned int width)
223 {
224 res.columns (width);
225 return res;
226 }
227
228 /** \brief Intercept output stream redirection.
229 ** \param ostr the destination output stream
230 ** \param loc a reference to the location to redirect
231 **
232 ** Avoid duplicate information.
233 */
234 inline std::ostream& operator<< (std::ostream& ostr, const location& loc)
235 {
236 position last = loc.end - 1;
237 ostr << loc.begin;
238 if (last.filename
239 && (!loc.begin.filename
240 || *loc.begin.filename != *last.filename))
241 ostr << '-' << last;
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;
246 return ostr;
247 }
248
249}
250
251#endif // not BISON_LOCATION_HH]
252m4_divert(-1)
253m4_changecom([#])