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

Last change on this file since 348 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