1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the porting application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
28
#include "tokenengine.h"
30
namespace TokenEngine {
33
Add a container-global attribute.
35
void TokenAttributes::addAttribute(const QByteArray &name, const QByteArray &value)
37
const QByteArray keyText = makeKeyText(-1, name);
38
attributes.insert(keyText, value);
42
Retrieve container-global attribute.
44
QByteArray TokenAttributes::attribute(const QByteArray &name) const
46
const QByteArray keyText = makeKeyText(-1, name);
47
return attributes.value(keyText);
51
Add an attribute. Note: Don't use names starting with a number, that will
54
void TokenAttributes::addAttribute(const int index, const QByteArray &name, const QByteArray &value)
56
const QByteArray keyText = makeKeyText(index, name);
57
attributes.insert(keyText, value);
60
QByteArray TokenAttributes::attribute(const int index, const QByteArray &name) const
62
const QByteArray keyText = makeKeyText(index, name);
63
return attributes.value(keyText);
66
QByteArray TokenAttributes::makeKeyText(const int index, const QByteArray &name) const
69
return indexText.setNum(index) + name;
74
Construnct an empty TokenContainer.
76
TokenContainer::TokenContainer()
78
d = new TokenContainerData();
83
Construnct a TokenContainer from data
85
TokenContainer::TokenContainer(QByteArray text, QVector<Token> tokens, TypeInfo *typeInfo)
87
d = new TokenContainerData();
91
d->typeInfo = new TypeInfo();
93
d->typeInfo = typeInfo;
96
int TokenContainer::count() const
98
return d->tokens.count();
101
QByteArray TokenContainer::text(const int index) const
103
Token token = d->tokens.at(index);
104
return d->text.mid(token.start, token.length);
107
QByteArray TokenContainer::tempText(const int index) const
109
Token token = d->tokens.at(index);
110
return QByteArray::fromRawData(d->text.constData() + token.start, token.length);
113
QByteArray TokenContainer::fullText() const
118
TokenContainer TokenContainer::tokenContainer(const int index) const
124
Token TokenContainer::token(const int index) const
126
return d->tokens.at(index);
129
TypeInfo *TokenContainer::typeInfo()
134
TokenAttributes *TokenContainer::tokenAttributes()
136
return d->tokenAttributes;
139
const TokenAttributes *TokenContainer::tokenAttributes() const
141
return d->tokenAttributes;
145
Returns the line number for the given index.
147
int TokenContainer::line(int index) const
149
//This algorithm is a bit more complicated than it should be,
150
//since we have to search for newlines inside comments.
151
//(Comments are tokenized as one token)
152
QByteArray contents = fullText();
155
for(int t=0; t < index; ++t) {
156
int tokenLength = d->tokens.at(t).length;
157
if((tokenLength == 1) && (text(t) == "\n")) {
159
} else if(tokenLength > 2) {// 3 is the minimum size for a comment (// + newline)
160
int newpos = d->tokens.at(t).length;
161
for(int p = pos; p < newpos; ++p) {
162
if(contents[p] == '\n')
166
pos += d->tokens.at(t).length;
172
Returns the column number for the given index.
174
int TokenContainer::column(int index) const
176
//Scan backwards, find \n.
180
if((d->tokens.at(t).length == 1) && (text(t) == "\n"))
182
chars += d->tokens.at(t).length;
188
TokenTempRef TokenContainer::tokenTempRef(const int index) const
190
Q_ASSERT(index < d->tokens.count());
191
const Token token = d->tokens.at(index);
192
return TokenTempRef(d->text.constData() + token.start, token.length);
195
QByteArray TokenSection::fullText() const
198
for (int t = m_start; t < m_start + m_count; ++t) {
199
text += m_tokenContainer.text(t);
204
QByteArray TokenList::fullText() const
207
for (int t = 0; t < m_tokenList.count(); ++t) {
208
text += m_tokenContainer.text(m_tokenList.at(t));
213
TokenSectionSequence::TokenSectionSequence(QVector<TokenSection> tokenSections)
214
:m_tokenSections(tokenSections), m_count(0)
216
for(int s = 0; s < m_tokenSections.count(); ++s) {
217
m_startIndexes.append(m_count);
218
m_count += m_tokenSections.at(s).count();
222
QByteArray TokenSectionSequence::fullText() const
225
foreach(TokenSection section, m_tokenSections) {
226
text += section.fullText();
231
int TokenSectionSequence::count() const
236
QVector<TokenSection> TokenSectionSequence::tokenSections() const
238
return m_tokenSections;
241
QByteArray TokenSectionSequence::text(const int index) const
243
const int sectionIndex = findSection(index);
244
const int sectionInternalIndex = calculateInternalIndex(index, sectionIndex);
245
return m_tokenSections.at(sectionIndex).text(sectionInternalIndex);
248
QByteArray TokenSectionSequence::tempText(const int index) const
250
const int sectionIndex = findSection(index);
251
const int sectionInternalIndex = calculateInternalIndex(index, sectionIndex);
252
return m_tokenSections.at(sectionIndex).tempText(sectionInternalIndex);
256
TokenContainer TokenSectionSequence::tokenContainer(const int index) const
258
const int sectionIndex = findSection(index);
259
const int sectionInternalIndex = calculateInternalIndex(index, sectionIndex);
260
return m_tokenSections.at(sectionIndex).tokenContainer(sectionInternalIndex);
263
int TokenSectionSequence::containerIndex(const int index) const
265
const int sectionIndex = findSection(index);
266
const int sectionInternalIndex = calculateInternalIndex(index, sectionIndex);
267
return m_tokenSections.at(sectionIndex).containerIndex(sectionInternalIndex);
270
int TokenSectionSequence::findSection(const int index) const
274
//Here we do a linear search through all collections in the list,
275
//which could turn out to be to slow.
276
while(!found && c < m_tokenSections.count()) {
277
const int sectionEnd = m_startIndexes.at(c)
278
+ m_tokenSections.at(c).count();
279
if (index < sectionEnd)
289
Q_ASSERT(c < m_tokenSections.count());
293
int TokenSectionSequence::calculateInternalIndex(const int index, const int sectionIndex) const
295
const int sectionInternalIndex =
296
index - m_startIndexes.at(sectionIndex);
297
Q_ASSERT(sectionInternalIndex < m_tokenSections.at(sectionIndex).count());
298
return sectionInternalIndex;
302
TokenSectionSequenceIterator::TokenSectionSequenceIterator(const TokenSectionSequence &tokenSectionSequence)
305
,m_numSections(tokenSectionSequence.m_tokenSections.count())
306
,m_tokenSectionSequence(tokenSectionSequence)
311
Resets the token iterator.
313
void TokenSectionSequenceIterator::reset()
315
m_currentSection = 0;
320
Advances the token iterator.
322
bool TokenSectionSequenceIterator::nextToken()
324
if(m_tokenSectionSequence.m_tokenSections.isEmpty())
327
//check of we are past the end of the current section
328
if(m_currentToken < m_tokenSectionSequence.m_tokenSections.at(m_currentSection).count())
330
//Advance to the next non-empty section.
334
//return false if no more sections
335
if(m_currentSection >= m_numSections)
337
//skip empty sections
338
} while(m_tokenSectionSequence.m_tokenSections.at(m_currentSection).count() == 0);
344
Returns the text for the current token;
346
QByteArray TokenSectionSequenceIterator::text() const
348
return m_tokenSectionSequence.m_tokenSections.at(m_currentSection).text(m_currentToken);
352
Returns the text for the current token as a temporary QByteArray;
354
QByteArray TokenSectionSequenceIterator::tempText() const
356
return m_tokenSectionSequence.m_tokenSections.at(m_currentSection).tempText(m_currentToken);
360
Returns the container for the current token;
362
TokenContainer TokenSectionSequenceIterator::tokenContainer() const
364
return m_tokenSectionSequence.m_tokenSections.at(m_currentSection).tokenContainer(m_currentToken);
368
Returns the containerIndex for the current token;
370
int TokenSectionSequenceIterator::containerIndex() const
372
return m_tokenSectionSequence.m_tokenSections.at(m_currentSection).containerIndex(m_currentToken);
376
Returns a temporary token reference for the current token for the current token;
378
TokenTempRef TokenSectionSequenceIterator::tokenTempRef() const
380
return m_tokenSectionSequence.m_tokenSections.at(m_currentSection).tokenTempRef(m_currentToken);
384
} //namespace TokenEngine