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