source: trunk/tools/porting/src/tokenreplacements.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "tokenreplacements.h"
43#include "logger.h"
44#include "portingrules.h"
45
46QT_BEGIN_NAMESPACE
47using namespace TokenEngine;
48
49void addLogSourceEntry(const QString &text, const TokenContainer &tokenContainer, const int index)
50{
51 Logger *logger = Logger::instance();
52 int line = tokenContainer.line(index);
53 int col = tokenContainer.column(index);
54 SourcePointLogEntry *logEntry =
55 new SourcePointLogEntry(QLatin1String("Info"), QLatin1String("Porting"),
56 logger->globalState.value(QLatin1String("currentFileName")),
57 line, col, text);
58 logger->addEntry(logEntry);
59}
60
61void addLogWarning(const QString &text)
62{
63 Logger::instance()->addEntry(new PlainLogEntry(QLatin1String("Warning"), QLatin1String("Porting"), text));
64}
65
66QualifiedNameParser::QualifiedNameParser(const TokenContainer &tokenContainer, const int tokenIndex)
67:tokenContainer(tokenContainer)
68,currentIndex(tokenIndex)
69{
70 Q_ASSERT(isValidIndex(currentIndex));
71}
72
73bool QualifiedNameParser::isPartOfQualifiedName()
74{
75 return ((nextScopeToken(Left) != -1) || (nextScopeToken(Right) != -1));
76}
77
78
79bool QualifiedNameParser::isValidIndex(int index)
80{
81 return (index < tokenContainer.count() && index >= 0);
82}
83
84/*
85 A qualifier is a the leftmost or middle part of a qualified name
86*/
87bool QualifiedNameParser::isQualifier()
88{
89 return (nextScopeToken(Right) != -1);
90}
91
92/*
93 A name is a the rightmost part of a qualified name.
94*/
95bool QualifiedNameParser::isName()
96{
97 return (nextScopeToken(Left) != -1);
98}
99
100/*
101 Peek for a qualifier or name in the given direction
102*/
103int QualifiedNameParser::peek(Direction direction)
104{
105 return nextScopeToken(direction);
106}
107
108/*
109 Look for a qualifier or name in the given direction,update
110 current position if found.
111*/
112int QualifiedNameParser::move(Direction direction)
113{
114 int tokenIndex = nextScopeToken(direction);
115 if(tokenIndex != -1)
116 currentIndex = tokenIndex;
117 return tokenIndex;
118}
119
120/*
121 Looks for "::" starting at currentIndex, returns the token index
122 for it if found. If the first non-whitespace token found is something else,
123 -1 is returned.
124*/
125int QualifiedNameParser::findScopeOperator(Direction direction)
126{
127 int tokenIndex = currentIndex;
128 QByteArray tokenText;
129 //loop until we get a token containg text or we pass the beginning/end of the source
130 tokenIndex += direction;
131 while(tokenText.isEmpty() && isValidIndex(tokenIndex)) {
132 tokenText = tokenContainer.text(tokenIndex).trimmed();
133 if(tokenText==QByteArray("::"))
134 return tokenIndex;
135 tokenIndex += direction;
136 }
137 return -1;
138}
139/*
140 Walks a qualified name. Returns the token index
141 for the next identifer in the qualified name, or -1 if its not found.
142*/
143int QualifiedNameParser::nextScopeToken(Direction direction)
144{
145 int tokenIndex = findScopeOperator(direction);
146 if (tokenIndex == -1)
147 return -1;
148 QByteArray tokenText;
149 //loop until we get a token containg text or we pass the start of the source
150 tokenIndex += direction;
151 while(tokenText.isEmpty() && isValidIndex(tokenIndex)) {
152 tokenText = tokenContainer.text(tokenIndex).trimmed();
153 tokenIndex += direction;
154 }
155 return tokenIndex - direction;
156}
157
158/////////////////////
159GenericTokenReplacement::GenericTokenReplacement(QByteArray oldToken, QByteArray newToken)
160:oldToken(oldToken)
161,newToken(newToken)
162{}
163
164QByteArray GenericTokenReplacement::getReplaceKey()
165{
166 return QByteArray(oldToken);
167}