source: trunk/tools/porting/src/rpp.h@ 500

Last change on this file since 500 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 26.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5** Copyright (C) 2001-2004 Roberto Raggi
6**
7** This file is part of the qt3to4 porting application of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain
25** additional rights. These rights are described in the Nokia Qt LGPL
26** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
27** package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37** If you are unsure which license is appropriate for your use, please
38** contact the sales department at [email protected].
39** $QT_END_LICENSE$
40**
41****************************************************************************/
42
43#ifndef RPP_H
44#define RPP_H
45
46#include "tokenengine.h"
47#include "rpplexer.h"
48#include "tokens.h"
49#include "smallobject.h"
50#include <QHash>
51#include <QStringList>
52#include <QFile>
53#include <QByteArray>
54#include <QDir>
55#include <QMultiMap>
56#include <ctype.h>
57
58QT_BEGIN_NAMESPACE
59
60namespace Rpp
61{
62
63struct Item;
64struct ItemComposite;
65
66struct Source;
67
68struct Directive;
69struct EmptyDirective;
70struct ErrorDirective;
71struct PragmaDirective;
72struct IncludeDirective;
73struct ConditionalDirective;
74struct DefineDirective;
75struct UndefDirective;
76struct LineDirective;
77struct NonDirective;
78
79struct IfSection;
80struct IfLikeDirective;
81struct IfDirective;
82struct ElifDirective;
83struct IfdefLikeDirective;
84struct IfdefDirective;
85struct IfndefDirective;
86struct ElseDirective;
87struct EndifDirective;
88
89struct Text;
90struct Token;
91struct TokenComposite;
92struct IdToken;
93struct NonIdToken;
94struct PastingToken;
95struct LineComment;
96struct MultiLineComment;
97struct WhiteSpace;
98
99struct MacroDefinition;
100struct MacroFunctionDefinition;
101struct MacroParameters;
102struct MacroParameter;
103
104struct Expression;
105struct UnaryExpression;
106struct BinaryExpression;
107struct ConditionalExpression;
108
109struct StringLiteral;
110struct IntLiteral;
111struct MacroReference;
112struct MacroFunctionReference;
113struct MacroArguments;
114struct MacroArgument;
115
116struct Item
117{
118 virtual ~Item() {}
119
120 virtual Item *parent() const = 0;
121
122 virtual ItemComposite *toItemComposite() const
123 { return 0; }
124
125 virtual Item *toItem() const
126 { return const_cast<Item *>(this); }
127
128 virtual Directive *toDirective() const
129 { return 0; }
130
131 virtual Text *toText() const
132 { return 0; }
133
134 virtual Token *toToken() const
135 { return 0; }
136
137 virtual Source *toSource() const
138 { return 0; }
139
140 virtual Expression *toExpression() const
141 { return 0; }
142
143 virtual IfSection *toIfSection() const
144 { return 0; }
145
146 // Text returns the original text for an item, e.g.
147 // the way it is found in the source
148 virtual TokenEngine::TokenSection text() const
149 { return TokenEngine::TokenSection(); }
150
151protected:
152 //using the default constructor for an item is
153 //only allowded for subclasses.
154 Item() {};
155};
156
157struct ItemComposite
158{
159 virtual ~ItemComposite() {}
160 virtual int count() const = 0;
161 virtual Item *item(int index) const = 0;
162 virtual void add(Item *item) = 0;
163/*
164 Classes that inherit ItemComposite must implement this
165 function themselves
166 virtual ItemComposite *toItemComposite() const
167 { return const_cast<ItemComposite *>(this); }
168*/
169};
170
171struct Directive: public Item
172{
173 inline Directive(Item *parent = 0)
174 : m_parent(parent), m_numLines(0) {}
175
176 virtual Item *parent() const
177 { return m_parent; }
178
179 inline void setParent(Item *parent)
180 { m_parent = parent;}
181
182 void setNumLines(const int numLines)
183 {m_numLines = numLines;}
184
185 virtual Directive *toDirective() const
186 { return const_cast<Directive *>(this); }
187
188 virtual EmptyDirective *toEmptyDirective() const
189 { return 0; }
190
191 virtual ErrorDirective *toErrorDirective() const
192 { return 0; }
193
194 virtual PragmaDirective *toPragmaDirective() const
195 { return 0; }
196
197 virtual IncludeDirective *toIncludeDirective() const
198 { return 0; }
199
200 virtual ConditionalDirective *toConditionalDirective() const
201 { return 0; }
202
203 virtual DefineDirective *toDefineDirective() const
204 { return 0; }
205
206 virtual UndefDirective *toUndefDirective() const
207 { return 0; }
208
209 virtual LineDirective *toLineDirective() const
210 { return 0; }
211
212 virtual NonDirective *toNonDirective() const
213 { return 0; }
214
215 void setTokenSection(TokenEngine::TokenSection section)
216 { m_tokenSection = section; }
217
218 TokenEngine::TokenSection text() const
219 { return m_tokenSection; }
220
221protected:
222 Item *m_parent;
223 int m_numLines;
224 TokenEngine::TokenSection m_tokenSection;
225};
226
227
228struct Token: public Item
229{
230 inline Token(Item *parent = 0)
231 : m_tokenIndex(0), m_parent(parent) {}
232
233 virtual Item *parent() const
234 { return m_parent; }
235
236 virtual MacroArguments *toMacroArguments() const
237 { return 0; }
238
239 virtual IdToken *toIdToken() const
240 { return 0; }
241
242 virtual NonIdToken *toNonIdToken() const
243 { return 0; }
244
245 virtual LineComment *toLineComment() const
246 { return 0; }
247
248 virtual MultiLineComment *toMultiLineComment() const
249 { return 0; }
250
251 virtual WhiteSpace *toWhiteSpace() const
252 { return 0; }
253
254 virtual Token *toToken() const
255 { return const_cast<Token *>(this); }
256
257 void setToken(int tokenIndex)
258 { m_tokenIndex = tokenIndex;}
259
260 int index() const
261 { return m_tokenIndex; }
262
263protected:
264 int m_tokenIndex;
265 Item *m_parent;
266};
267
268struct Text: public Item
269{
270 inline Text(Item *parent = 0)
271 : m_parent(parent) {}
272
273 virtual Text *toText() const
274 { return const_cast<Text *>(this); }
275
276 virtual Item *parent() const
277 { return m_parent; }
278
279 void setTokenSection(TokenEngine::TokenSection tokenSection)
280 {m_tokenSection = tokenSection; }
281
282 TokenEngine::TokenSection text() const
283 { return m_tokenSection; }
284
285 QVector<TokenEngine::TokenSection> cleanedText() const
286 { return m_cleanedSection; }
287
288 void setTokens( const QVector<Token *> &tokens )
289 { m_tokens = tokens; }
290
291 void addToken(Token *token)
292 {m_tokens.append(token);}
293
294 Token *token(int index) const
295 {return m_tokens.at(index);}
296
297 inline int count() const
298 {return m_tokens.count();}
299
300 QVector<Token *> tokenList() const
301 { return m_tokens; }
302
303protected:
304 Item *m_parent;
305 TokenEngine::TokenSection m_tokenSection; // all tokens
306 QVector<TokenEngine::TokenSection> m_cleanedSection; //comments removed
307 QVector<Token *> m_tokens;
308};
309
310struct IdToken: public Token
311{
312 inline IdToken(Item *parent = 0)
313 : Token(parent) {}
314
315 virtual IdToken *toIdToken() const
316 { return const_cast<IdToken *>(this); }
317};
318
319struct NonIdToken: public Token
320{
321 inline NonIdToken(Item *parent = 0)
322 : Token(parent) {}
323
324 virtual NonIdToken *toNonIdToken() const
325 { return const_cast< NonIdToken *>(this); }
326};
327
328struct LineComment : public NonIdToken
329{
330 inline LineComment(Item *parent = 0)
331 : NonIdToken(parent) {}
332
333 virtual LineComment *toLineComment() const
334 { return const_cast< LineComment *>(this); }
335};
336
337struct MultiLineComment: public NonIdToken
338{
339 inline MultiLineComment(Item *parent = 0)
340 : NonIdToken(parent) {}
341
342 virtual MultiLineComment *toMultiLineComment() const
343 { return const_cast< MultiLineComment *>(this); }
344protected:
345};
346
347struct WhiteSpace: public NonIdToken
348{
349 inline WhiteSpace(Item *parent = 0)
350 : NonIdToken(parent) {}
351
352 virtual WhiteSpace *toWhiteSpace() const
353 { return const_cast<WhiteSpace *>(this); }
354};
355
356struct Source: public Item, public ItemComposite
357{
358 Source(Item *parent = 0)
359 :m_parent(parent) {}
360
361 virtual Source *toSource() const
362 { return const_cast<Source *>(this); }
363
364 ItemComposite *toItemComposite() const
365 { return const_cast<Source *>(this); }
366
367 virtual int count() const
368 { return m_items.count(); }
369
370 virtual Item *item(int index) const
371 { return m_items.at(index); }
372
373 inline QString fileName() const
374 { return m_fileName; }
375
376 void setFileName(const QString &fileName);
377
378 virtual Item *parent() const
379 { return m_parent; }
380
381 inline void add(Item *item)
382 { m_items.append(item); }
383
384private:
385 Item *m_parent;
386 QVector<Item *> m_items;
387 QString m_fileName;
388};
389
390struct EmptyDirective: public Directive
391{
392 EmptyDirective(Item *item)
393 : Directive(item) {}
394
395 virtual EmptyDirective *toEmptyDirective() const
396 { return const_cast<EmptyDirective *>(this); }
397};
398
399struct ErrorDirective: public Directive
400{
401 ErrorDirective(Item *item)
402 : Directive(item) {}
403
404 virtual ErrorDirective *toErrorDirective() const
405 { return const_cast<ErrorDirective *>(this); }
406};
407
408struct PragmaDirective: public Directive
409{
410 PragmaDirective(Item *item)
411 : Directive(item) {}
412
413 virtual PragmaDirective *toPragmaDirective() const
414 { return const_cast<PragmaDirective *>(this); }
415};
416
417struct IncludeDirective: public Directive
418{
419 enum IncludeType {QuoteInclude, AngleBracketInclude};
420
421 IncludeDirective(Item *item)
422 : Directive(item), m_includeType(QuoteInclude) {}
423
424 IncludeDirective() : Directive() {}
425
426 virtual IncludeDirective *toIncludeDirective() const
427 { return const_cast<IncludeDirective *>(this); }
428
429 void setFilenameTokens(const TokenEngine::TokenList &filenameTokens)
430 { m_filenameTokens = filenameTokens; }
431
432 TokenEngine::TokenList filenameTokens() const
433 { return m_filenameTokens; }
434
435 void setFilename(const QByteArray &filename)
436 { m_filename = filename; }
437
438 QByteArray filename() const
439 { return m_filename;}
440
441 void setIncludeType(IncludeType includeType)
442 { m_includeType = includeType; }
443
444 IncludeType includeType() const
445 { return m_includeType; }
446private:
447 TokenEngine::TokenList m_filenameTokens;
448 QByteArray m_filename;
449 IncludeType m_includeType;
450};
451
452struct ConditionalDirective: public Directive, public ItemComposite
453{
454 inline ConditionalDirective(Item *parent = 0)
455 :Directive(parent) {}
456
457 virtual ConditionalDirective *toConditionalDirective() const
458 { return const_cast<ConditionalDirective *>(this); }
459
460 ItemComposite *toItemComposite() const
461 { return const_cast<ConditionalDirective *>(this); }
462
463 virtual IfDirective *toIfDirective() const
464 { return 0; }
465
466 virtual IfdefDirective *toIfdefDirective() const
467 { return 0; }
468
469 virtual IfndefDirective *toIfndefDirective() const
470 { return 0; }
471
472 virtual ElifDirective *toElifDirective() const
473 { return 0; }
474
475 virtual ElseDirective *toElseDirective() const
476 { return 0; }
477
478 int count() const
479 { return m_items.count(); }
480
481 Item *item(int index) const
482 { return m_items.at(index); }
483
484 void add(Item *item)
485 { m_items.append(item); }
486protected:
487 QVector<Item *> m_items;
488};
489
490struct IfSection: public Item, public ItemComposite
491{
492 IfSection(Item *parent)
493 :m_parent(parent), m_ifGroup(0), m_elseGroup(0), m_endifLine(0) {}
494
495 IfSection *toIfSection() const
496 { return const_cast<IfSection *>(this); }
497
498 ItemComposite *toItemComposite() const
499 { return const_cast<IfSection *>(this); }
500
501 void setParent(Item *parent)
502 { m_parent = parent; }
503
504 Item *parent() const
505 { return m_parent; }
506
507 void setIfGroup(ConditionalDirective *ifGroup)
508 { m_ifGroup = ifGroup; m_items.append(ifGroup); }
509
510 ConditionalDirective *ifGroup() const
511 { return m_ifGroup; }
512
513 void addElifGroup(ConditionalDirective *elifGroup)
514 { m_elifGroups.append(elifGroup); m_items.append(elifGroup); }
515
516 QVector<ConditionalDirective *> elifGroups() const
517 { return m_elifGroups; }
518
519 void setElseGroup(ConditionalDirective *elseGroup)
520 { m_elseGroup = elseGroup; m_items.append(elseGroup); }
521
522 ConditionalDirective *elseGroup() const
523 { return m_elseGroup; }
524
525 void setEndifLine(Directive *endifLine)
526 { m_endifLine = endifLine; m_items.append(endifLine); }
527
528 Directive *endifLine() const
529 { return m_endifLine; }
530
531 int count() const
532 { return m_items.count(); }
533
534 Item *item(int index) const
535 { return m_items.at(index);}
536
537 private:
538 void add(Item *item)
539 { Q_UNUSED(item); }
540
541 Item *m_parent;
542 QVector<Item *> m_items;
543 ConditionalDirective *m_ifGroup;
544 QVector<ConditionalDirective *> m_elifGroups;
545 ConditionalDirective *m_elseGroup;
546 Directive *m_endifLine;
547};
548
549struct Expression: public Item
550{
551 enum Operator
552 {
553 LtEqOp = 300,
554 GtEqOp,
555 LtOp,
556 GtOp,
557 EqOp,
558 NotEqOp,
559 OrOp,
560 AndOp,
561 LShiftOp,
562 RShiftOp
563 };
564
565 inline Expression(Item *parent = 0)
566 : m_parent(parent) {}
567
568 inline Expression *parentExpression() const
569 { return m_parent ? m_parent->toExpression() : 0; }
570
571 virtual Item *parent() const
572 { return m_parent; }
573
574 virtual Expression *toExpression() const
575 { return const_cast<Expression *>(this); }
576
577 virtual UnaryExpression *toUnaryExpression() const
578 { return 0; }
579
580 virtual BinaryExpression *toBinaryExpression() const
581 { return 0; }
582
583 virtual StringLiteral *toStringLiteral() const