1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
|
|
4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
|
|
5 **
|
|
6 ** This file is part of Qt Jambi.
|
|
7 **
|
|
8 ** * Commercial Usage
|
|
9 * Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 * accordance with the Qt Commercial License Agreement provided with the
|
|
11 * Software or, alternatively, in accordance with the terms contained in
|
|
12 * a written agreement between you and Nokia.
|
|
13 *
|
|
14 *
|
|
15 * GNU General Public License Usage
|
|
16 * Alternatively, this file may be used under the terms of the GNU
|
|
17 * General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 * Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 * the packaging of this file. Please review the following information
|
|
20 * to ensure GNU General Public Licensing requirements will be met:
|
|
21 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 * exception, Nokia gives you certain additional rights. These rights
|
|
24 * are described in the Nokia Qt GPL Exception version 1.2, included in
|
|
25 * the file GPL_EXCEPTION.txt in this package.
|
|
26 *
|
|
27 * Qt for Windows(R) Licensees
|
|
28 * As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 * Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 * right for the Qt/Eclipse Integration to link to functionality
|
|
31 * provided by Qt Designer and its related libraries.
|
|
32 *
|
|
33 *
|
|
34 * If you are unsure which license is appropriate for your use, please
|
|
35 * contact the sales department at qt-sales@nokia.com.
|
|
36
|
|
37 **
|
|
38 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
39 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
40 **
|
|
41 ****************************************************************************/
|
|
42
|
|
43 #ifndef PP_SCANNER_H
|
|
44 #define PP_SCANNER_H
|
|
45
|
|
46 namespace rpp {
|
|
47
|
|
48 struct pp_skip_blanks
|
|
49 {
|
|
50 int lines;
|
|
51
|
|
52 template <typename _InputIterator>
|
|
53 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
54 {
|
|
55 lines = 0;
|
|
56
|
|
57 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
58 {
|
|
59 if (*__first == '\\')
|
|
60 {
|
|
61 _InputIterator __begin = __first;
|
|
62 ++__begin;
|
|
63
|
|
64 if (__begin != __last && *__begin == '\n')
|
|
65 ++__first;
|
|
66 else
|
|
67 break;
|
|
68 }
|
|
69 else if (*__first == '\n' || !pp_isspace (*__first))
|
|
70 break;
|
|
71 }
|
|
72
|
|
73 return __first;
|
|
74 }
|
|
75 };
|
|
76
|
|
77 struct pp_skip_whitespaces
|
|
78 {
|
|
79 int lines;
|
|
80
|
|
81 template <typename _InputIterator>
|
|
82 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
83 {
|
|
84 lines = 0;
|
|
85
|
|
86 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
87 {
|
|
88 if (! pp_isspace (*__first))
|
|
89 break;
|
|
90 }
|
|
91
|
|
92 return __first;
|
|
93 }
|
|
94 };
|
|
95
|
|
96 struct pp_skip_comment_or_divop
|
|
97 {
|
|
98 int lines;
|
|
99
|
|
100 template <typename _InputIterator>
|
|
101 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
102 {
|
|
103 enum {
|
|
104 MAYBE_BEGIN,
|
|
105 BEGIN,
|
|
106 MAYBE_END,
|
|
107 END,
|
|
108 IN_COMMENT,
|
|
109 IN_CXX_COMMENT
|
|
110 } state (MAYBE_BEGIN);
|
|
111
|
|
112 lines = 0;
|
|
113
|
|
114 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
115 {
|
|
116 switch (state)
|
|
117 {
|
|
118 default:
|
|
119 assert (0);
|
|
120 break;
|
|
121
|
|
122 case MAYBE_BEGIN:
|
|
123 if (*__first != '/')
|
|
124 return __first;
|
|
125
|
|
126 state = BEGIN;
|
|
127 break;
|
|
128
|
|
129 case BEGIN:
|
|
130 if (*__first == '*')
|
|
131 state = IN_COMMENT;
|
|
132 else if (*__first == '/')
|
|
133 state = IN_CXX_COMMENT;
|
|
134 else
|
|
135 return __first;
|
|
136 break;
|
|
137
|
|
138 case IN_COMMENT:
|
|
139 if (*__first == '*')
|
|
140 state = MAYBE_END;
|
|
141 break;
|
|
142
|
|
143 case IN_CXX_COMMENT:
|
|
144 if (*__first == '\n')
|
|
145 return __first;
|
|
146 break;
|
|
147
|
|
148 case MAYBE_END:
|
|
149 if (*__first == '/')
|
|
150 state = END;
|
|
151 else if (*__first != '*')
|
|
152 state = IN_COMMENT;
|
|
153 break;
|
|
154
|
|
155 case END:
|
|
156 return __first;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 return __first;
|
|
161 }
|
|
162 };
|
|
163
|
|
164 struct pp_skip_identifier
|
|
165 {
|
|
166 int lines;
|
|
167
|
|
168 template <typename _InputIterator>
|
|
169 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
170 {
|
|
171 lines = 0;
|
|
172
|
|
173 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
174 {
|
|
175 if (! pp_isalnum (*__first) && *__first != '_')
|
|
176 break;
|
|
177 }
|
|
178
|
|
179 return __first;
|
|
180 }
|
|
181 };
|
|
182
|
|
183 struct pp_skip_number
|
|
184 {
|
|
185 int lines;
|
|
186
|
|
187 template <typename _InputIterator>
|
|
188 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
189 {
|
|
190 lines = 0;
|
|
191
|
|
192 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
193 {
|
|
194 if (! pp_isalnum (*__first) && *__first != '.')
|
|
195 break;
|
|
196 }
|
|
197
|
|
198 return __first;
|
|
199 }
|
|
200 };
|
|
201
|
|
202 struct pp_skip_string_literal
|
|
203 {
|
|
204 int lines;
|
|
205
|
|
206 template <typename _InputIterator>
|
|
207 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
208 {
|
|
209 enum {
|
|
210 BEGIN,
|
|
211 IN_STRING,
|
|
212 QUOTE,
|
|
213 END
|
|
214 } state (BEGIN);
|
|
215
|
|
216 lines = 0;
|
|
217
|
|
218 for (; __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
219 {
|
|
220 switch (state)
|
|
221 {
|
|
222 default:
|
|
223 assert (0);
|
|
224 break;
|
|
225
|
|
226 case BEGIN:
|
|
227 if (*__first != '\"')
|
|
228 return __first;
|
|
229 state = IN_STRING;
|
|
230 break;
|
|
231
|
|
232 case IN_STRING:
|
|
233 assert (*__first != '\n');
|
|
234
|
|
235 if (*__first == '\"')
|
|
236 state = END;
|
|
237 else if (*__first == '\\')
|
|
238 state = QUOTE;
|
|
239 break;
|
|
240
|
|
241 case QUOTE:
|
|
242 state = IN_STRING;
|
|
243 break;
|
|
244
|
|
245 case END:
|
|
246 return __first;
|
|
247 }
|
|
248 }
|
|
249
|
|
250 return __first;
|
|
251 }
|
|
252 };
|
|
253
|
|
254 struct pp_skip_char_literal
|
|
255 {
|
|
256 int lines;
|
|
257
|
|
258 template <typename _InputIterator>
|
|
259 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
260 {
|
|
261 enum {
|
|
262 BEGIN,
|
|
263 IN_STRING,
|
|
264 QUOTE,
|
|
265 END
|
|
266 } state (BEGIN);
|
|
267
|
|
268 lines = 0;
|
|
269
|
|
270 for (; state != END && __first != __last; lines += (*__first != '\n' ? 0 : 1), ++__first)
|
|
271 {
|
|
272 switch (state)
|
|
273 {
|
|
274 default:
|
|
275 assert (0);
|
|
276 break;
|
|
277
|
|
278 case BEGIN:
|
|
279 if (*__first != '\'')
|
|
280 return __first;
|
|
281 state = IN_STRING;
|
|
282 break;
|
|
283
|
|
284 case IN_STRING:
|
|
285 assert (*__first != '\n');
|
|
286
|
|
287 if (*__first == '\'')
|
|
288 state = END;
|
|
289 else if (*__first == '\\')
|
|
290 state = QUOTE;
|
|
291 break;
|
|
292
|
|
293 case QUOTE:
|
|
294 state = IN_STRING;
|
|
295 break;
|
|
296 }
|
|
297 }
|
|
298
|
|
299 return __first;
|
|
300 }
|
|
301 };
|
|
302
|
|
303 struct pp_skip_argument
|
|
304 {
|
|
305 pp_skip_identifier skip_number;
|
|
306 pp_skip_identifier skip_identifier;
|
|
307 pp_skip_string_literal skip_string_literal;
|
|
308 pp_skip_char_literal skip_char_literal;
|
|
309 pp_skip_comment_or_divop skip_comment_or_divop;
|
|
310 int lines;
|
|
311
|
|
312 template <typename _InputIterator>
|
|
313 _InputIterator operator () (_InputIterator __first, _InputIterator __last)
|
|
314 {
|
|
315 int depth = 0;
|
|
316 lines = 0;
|
|
317
|
|
318 while (__first != __last)
|
|
319 {
|
|
320 if (!depth && (*__first == ')' || *__first == ','))
|
|
321 break;
|
|
322 else if (*__first == '(')
|
|
323 ++depth, ++__first;
|
|
324 else if (*__first == ')')
|
|
325 --depth, ++__first;
|
|
326 else if (*__first == '\"')
|
|
327 {
|
|
328 __first = skip_string_literal (__first, __last);
|
|
329 lines += skip_string_literal.lines;
|
|
330 }
|
|
331 else if (*__first == '\'')
|
|
332 {
|
|
333 __first = skip_char_literal (__first, __last);
|
|
334 lines += skip_char_literal.lines;
|
|
335 }
|
|
336 else if (*__first == '/')
|
|
337 {
|
|
338 __first = skip_comment_or_divop (__first, __last);
|
|
339 lines += skip_comment_or_divop.lines;
|
|
340 }
|
|
341 else if (pp_isalpha (*__first) || *__first == '_')
|
|
342 {
|
|
343 __first = skip_identifier (__first, __last);
|
|
344 lines += skip_identifier.lines;
|
|
345 }
|
|
346 else if (pp_isdigit (*__first))
|
|
347 {
|
|
348 __first = skip_number (__first, __last);
|
|
349 lines += skip_number.lines;
|
|
350 }
|
|
351 else if (*__first == '\n')
|
|
352 {
|
|
353 ++__first;
|
|
354 ++lines;
|
|
355 }
|
|
356 else
|
|
357 ++__first;
|
|
358 }
|
|
359
|
|
360 return __first;
|
|
361 }
|
|
362 };
|
|
363
|
|
364 } // namespace rpp
|
|
365
|
|
366 #endif // PP_SCANNER_H
|
|
367
|
|
368 // kate: space-indent on; indent-width 2; replace-tabs on;
|