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