Mercurial > projects > qtd
annotate generator/parser/rpp/pp-macro-expander.h @ 52:09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
author | eldar |
---|---|
date | Mon, 18 May 2009 15:23:28 +0000 |
parents | e78566595089 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
3 ** Copyright (C) 1992-2009 Nokia. All rights reserved. |
1 | 4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org> |
5 ** | |
6 ** This file is part of Qt Jambi. | |
7 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
8 ** Commercial Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
9 Licensees holding valid Qt Commercial licenses may use this file in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
10 accordance with the Qt Commercial License Agreement provided with the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
11 Software or, alternatively, in accordance with the terms contained in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
12 a written agreement between you and Nokia. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
13 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
14 GNU Lesser General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
15 Alternatively, this file may be used under the terms of the GNU Lesser |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
16 General Public License version 2.1 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
17 Foundation and appearing in the file LICENSE.LGPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
18 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
19 ensure the GNU Lesser General Public License version 2.1 requirements |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
20 will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
21 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
22 In addition, as a special exception, Nokia gives you certain |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
23 additional rights. These rights are described in the Nokia Qt LGPL |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
24 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
25 package. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
26 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
27 GNU General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
28 Alternatively, this file may be used under the terms of the GNU |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
29 General Public License version 3.0 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
30 Foundation and appearing in the file LICENSE.GPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
31 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
32 ensure the GNU General Public License version 3.0 requirements will be |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
33 met: http://www.gnu.org/copyleft/gpl.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
34 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
35 If you are unsure which license is appropriate for your use, please |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
36 contact the sales department at qt-sales@nokia.com. |
1 | 37 |
38 ** | |
39 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
40 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
41 ** | |
42 ****************************************************************************/ | |
43 | |
44 #ifndef PP_MACRO_EXPANDER_H | |
45 #define PP_MACRO_EXPANDER_H | |
46 | |
47 namespace rpp { | |
48 | |
49 struct pp_frame | |
50 { | |
51 pp_macro *expanding_macro; | |
52 std::vector<std::string> *actuals; | |
53 | |
54 pp_frame (pp_macro *__expanding_macro, std::vector<std::string> *__actuals): | |
55 expanding_macro (__expanding_macro), actuals (__actuals) {} | |
56 }; | |
57 | |
58 class pp_macro_expander | |
59 { | |
60 pp_environment &env; | |
61 pp_frame *frame; | |
62 | |
63 pp_skip_number skip_number; | |
64 pp_skip_identifier skip_identifier; | |
65 pp_skip_string_literal skip_string_literal; | |
66 pp_skip_char_literal skip_char_literal; | |
67 pp_skip_argument skip_argument; | |
68 pp_skip_comment_or_divop skip_comment_or_divop; | |
69 pp_skip_blanks skip_blanks; | |
70 pp_skip_whitespaces skip_whitespaces; | |
71 | |
72 std::string const *resolve_formal (pp_fast_string const *__name) | |
73 { | |
74 assert (__name != 0); | |
75 | |
76 if (! frame) | |
77 return 0; | |
78 | |
79 assert (frame->expanding_macro != 0); | |
80 | |
81 std::vector<pp_fast_string const *> const formals = frame->expanding_macro->formals; | |
82 for (std::size_t index = 0; index < formals.size(); ++index) | |
83 { | |
84 pp_fast_string const *formal = formals[index]; | |
85 | |
86 if (*formal != *__name) | |
87 continue; | |
88 | |
89 else if (frame->actuals && index < frame->actuals->size()) | |
90 return &(*frame->actuals)[index]; | |
91 | |
92 else | |
93 assert (0); // internal error? | |
94 } | |
95 | |
96 return 0; | |
97 } | |
98 | |
99 public: // attributes | |
100 int lines; | |
101 int generated_lines; | |
102 | |
103 public: | |
104 pp_macro_expander (pp_environment &__env, pp_frame *__frame = 0): | |
105 env (__env), frame (__frame), lines (0), generated_lines (0) {} | |
106 | |
107 template <typename _InputIterator, typename _OutputIterator> | |
108 _InputIterator operator () (_InputIterator __first, _InputIterator __last, _OutputIterator __result) | |
109 { | |
110 generated_lines = 0; | |
111 __first = skip_blanks (__first, __last); | |
112 lines = skip_blanks.lines; | |
113 | |
114 while (__first != __last) | |
115 { | |
116 if (*__first == '\n') | |
117 { | |
118 *__result++ = *__first; | |
119 ++lines; | |
120 | |
121 __first = skip_blanks (++__first, __last); | |
122 lines += skip_blanks.lines; | |
123 | |
124 if (__first != __last && *__first == '#') | |
125 break; | |
126 } | |
127 else if (*__first == '#') | |
128 { | |
129 __first = skip_blanks (++__first, __last); | |
130 lines += skip_blanks.lines; | |
131 | |
132 _InputIterator end_id = skip_identifier (__first, __last); | |
133 | |
134 // ### rewrite: not safe | |
135 char name_buffer[512], *cp = name_buffer; | |
136 std::copy (__first, end_id, cp); | |
137 std::size_t name_size = end_id - __first; | |
138 name_buffer[name_size] = '\0'; | |
139 | |
140 pp_fast_string fast_name (name_buffer, name_size); | |
141 | |
142 if (std::string const *actual = resolve_formal (&fast_name)) | |
143 { | |
144 *__result++ = '\"'; | |
145 | |
146 for (std::string::const_iterator it = skip_whitespaces (actual->begin (), actual->end ()); | |
147 it != actual->end (); ++it) | |
148 { | |
149 if (*it == '"') | |
150 { | |
151 *__result++ = '\\'; | |
152 *__result++ = *it; | |
153 } | |
154 | |
155 else if (*it == '\n') | |
156 { | |
157 *__result++ = '"'; | |
158 *__result++ = '\n'; | |
159 *__result++ = '"'; | |
160 } | |
161 | |
162 else | |
163 *__result++ = *it; | |
164 } | |
165 | |
166 *__result++ = '\"'; | |
167 __first = end_id; | |
168 } | |
169 else | |
170 *__result++ = '#'; // ### warning message? | |
171 } | |
172 else if (*__first == '\"') | |
173 { | |
174 _InputIterator next_pos = skip_string_literal (__first, __last); | |
175 lines += skip_string_literal.lines; | |
176 std::copy (__first, next_pos, __result); | |
177 __first = next_pos; | |
178 } | |
179 else if (*__first == '\'') | |
180 { | |
181 _InputIterator next_pos = skip_char_literal (__first, __last); | |
182 lines += skip_char_literal.lines; | |
183 std::copy (__first, next_pos, __result); | |
184 __first = next_pos; | |
185 } | |
186 else if (_PP_internal::comment_p (__first, __last)) | |
187 { | |
188 __first = skip_comment_or_divop (__first, __last); | |
189 int n = skip_comment_or_divop.lines; | |
190 lines += n; | |
191 | |
192 while (n-- > 0) | |
193 *__result++ = '\n'; | |
194 } | |
195 else if (pp_isspace (*__first)) | |
196 { | |
197 for (; __first != __last; ++__first) | |
198 { | |
199 if (*__first == '\n' || !pp_isspace (*__first)) | |
200 break; | |
201 } | |
202 | |
203 *__result = ' '; | |
204 } | |
205 else if (pp_isdigit (*__first)) | |
206 { | |
207 _InputIterator next_pos = skip_number (__first, __last); | |
208 lines += skip_number.lines; | |
209 std::copy (__first, next_pos, __result); | |
210 __first = next_pos; | |
211 } | |
212 else if (pp_isalpha (*__first) || *__first == '_') | |
213 { | |
214 _InputIterator name_begin = __first; | |
215 _InputIterator name_end = skip_identifier (__first, __last); | |
216 __first = name_end; // advance | |
217 | |
218 // search for the paste token | |
219 _InputIterator next = skip_blanks (__first, __last); | |
220 if (next != __last && *next == '#') | |
221 { | |
222 ++next; | |
223 if (next != __last && *next == '#') | |
224 __first = skip_blanks(++next, __last); | |
225 } | |
226 | |
227 // ### rewrite: not safe | |
228 | |
229 std::ptrdiff_t name_size; | |
230 #if defined(__SUNPRO_CC) | |
231 std::distance (name_begin, name_end, name_size); | |
232 #else | |
233 name_size = std::distance (name_begin, name_end); | |
234 #endif | |
235 assert (name_size >= 0 && name_size < 512); | |
236 | |
237 char name_buffer[512], *cp = name_buffer; | |
238 std::size_t __size = name_end - name_begin; | |
239 std::copy (name_begin, name_end, cp); | |
240 name_buffer[__size] = '\0'; | |
241 | |
242 pp_fast_string fast_name (name_buffer, name_size); | |
243 | |
244 if (std::string const *actual = resolve_formal (&fast_name)) | |
245 { | |
246 std::copy (actual->begin (), actual->end (), __result); | |
247 continue; | |
248 } | |
249 | |
250 static bool hide_next = false; // ### remove me | |
251 | |
252 pp_macro *macro = env.resolve (name_buffer, name_size); | |
253 if (! macro || macro->hidden || hide_next) | |
254 { | |
255 hide_next = ! strcmp (name_buffer, "defined"); | |
256 | |
257 if (__size == 8 && name_buffer [0] == '_' && name_buffer [1] == '_') | |
258 { | |
259 if (! strcmp (name_buffer, "__LINE__")) | |
260 { | |
261 char buf [16]; | |
262 char *end = buf + pp_snprintf (buf, 16, "%d", env.current_line + lines); | |
263 | |
264 std::copy (&buf [0], end, __result); | |
265 continue; | |
266 } | |
267 | |
268 else if (! strcmp (name_buffer, "__FILE__")) | |
269 { | |
270 __result++ = '"'; | |
271 std::copy (env.current_file.begin (), env.current_file.end (), __result); // ### quote | |
272 __result++ = '"'; | |
273 continue; | |
274 } | |
275 } | |
276 | |
277 std::copy (name_begin, name_end, __result); | |
278 continue; | |
279 } | |
280 | |
281 if (! macro->function_like) | |
282 { | |
283 pp_macro *m = 0; | |
284 | |
285 if (macro->definition) | |
286 { | |
287 macro->hidden = true; | |
288 | |
289 std::string __tmp; | |
290 __tmp.reserve (256); | |
291 | |
292 pp_macro_expander expand_macro (env); | |
293 expand_macro (macro->definition->begin (), macro->definition->end (), std::back_inserter (__tmp)); | |
294 generated_lines += expand_macro.lines; | |
295 | |
296 if (! __tmp.empty ()) | |
297 { | |
298 std::string::iterator __begin_id = skip_whitespaces (__tmp.begin (), __tmp.end ()); | |
299 std::string::iterator __end_id = skip_identifier (__begin_id, __tmp.end ()); | |
300 | |
301 if (__end_id == __tmp.end ()) | |
302 { | |
303 std::string __id; | |
304 __id.assign (__begin_id, __end_id); | |
305 | |
306 std::size_t x; | |
307 #if defined(__SUNPRO_CC) | |
308 std::distance (__begin_id, __end_id, x); | |
309 #else | |
310 x = std::distance (__begin_id, __end_id); | |
311 #endif | |
312 m = env.resolve (__id.c_str (), x); | |
313 } | |
314 | |
315 if (! m) | |
316 std::copy (__tmp.begin (), __tmp.end (), __result); | |
317 } | |
318 | |
319 macro->hidden = false; | |
320 } | |
321 | |
322 if (! m) | |
323 continue; | |
324 | |
325 macro = m; | |
326 } | |
327 | |
328 // function like macro | |
329 _InputIterator arg_it = skip_whitespaces (__first, __last); | |
330 | |
331 if (arg_it == __last || *arg_it != '(') | |
332 { | |
333 std::copy (name_begin, name_end, __result); | |
334 lines += skip_whitespaces.lines; | |
335 __first = arg_it; | |
336 continue; | |
337 } | |
338 | |
339 std::vector<std::string> actuals; | |
340 actuals.reserve (5); | |
341 ++arg_it; // skip '(' | |
342 | |
343 pp_macro_expander expand_actual (env, frame); | |
344 | |
345 _InputIterator arg_end = skip_argument_variadics (actuals, macro, arg_it, __last); | |
346 if (arg_it != arg_end) | |
347 { | |
348 std::string actual (arg_it, arg_end); | |
349 actuals.resize (actuals.size() + 1); | |
350 actuals.back ().reserve (255); | |
351 expand_actual (actual.begin (), actual.end(), std::back_inserter (actuals.back())); | |
352 arg_it = arg_end; | |
353 } | |
354 | |
355 while (arg_it != __last && *arg_end == ',') | |
356 { | |
357 ++arg_it; // skip ',' | |
358 | |
359 arg_end = skip_argument_variadics (actuals, macro, arg_it, __last); | |
360 std::string actual (arg_it, arg_end); | |
361 actuals.resize (actuals.size() + 1); | |
362 actuals.back ().reserve (255); | |
363 expand_actual (actual.begin (), actual.end(), std::back_inserter (actuals.back())); | |
364 arg_it = arg_end; | |
365 } | |
366 | |
367 assert (arg_it != __last && *arg_it == ')'); | |
368 | |
369 ++arg_it; // skip ')' | |
370 __first = arg_it; | |
371 | |
372 #if 0 // ### enable me | |
373 assert ((macro->variadics && macro->formals.size () >= actuals.size ()) | |
374 || macro->formals.size() == actuals.size()); | |
375 #endif | |
376 | |
377 pp_frame frame (macro, &actuals); | |
378 pp_macro_expander expand_macro (env, &frame); | |
379 macro->hidden = true; | |
380 expand_macro (macro->definition->begin (), macro->definition->end (), __result); | |
381 macro->hidden = false; | |
382 generated_lines += expand_macro.lines; | |
383 } | |
384 else | |
385 *__result++ = *__first++; | |
386 } | |
387 | |
388 return __first; | |
389 } | |
390 | |
391 template <typename _InputIterator> | |
392 _InputIterator skip_argument_variadics (std::vector<std::string> const &__actuals, pp_macro *__macro, | |
393 _InputIterator __first, _InputIterator __last) | |
394 { | |
395 _InputIterator arg_end = skip_argument (__first, __last); | |
396 | |
397 while (__macro->variadics && __first != arg_end && arg_end != __last && *arg_end == ',' | |
398 && (__actuals.size () + 1) == __macro->formals.size ()) | |
399 { | |
400 arg_end = skip_argument (++arg_end, __last); | |
401 } | |
402 | |
403 return arg_end; | |
404 } | |
405 }; | |
406 | |
407 } // namespace rpp | |
408 | |
409 #endif // PP_MACRO_EXPANDER_H | |
410 | |
411 // kate: space-indent on; indent-width 2; replace-tabs on; |