| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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 | #ifndef TOKENENGINE_H
|
|---|
| 43 | #define TOKENENGINE_H
|
|---|
| 44 |
|
|---|
| 45 | #include <QByteArray>
|
|---|
| 46 | #include <QVector>
|
|---|
| 47 | #include <QString>
|
|---|
| 48 | #include <QSharedData>
|
|---|
| 49 | #include <QSharedDataPointer>
|
|---|
| 50 | #include <QMap>
|
|---|
| 51 |
|
|---|
| 52 | QT_BEGIN_NAMESPACE
|
|---|
| 53 |
|
|---|
| 54 | namespace TokenEngine {
|
|---|
| 55 |
|
|---|
| 56 | class TokenContainer;
|
|---|
| 57 | /*
|
|---|
| 58 | A token is defined as a start-position and a length. Since the actual text
|
|---|
| 59 | storage is not reffered to here, Token needs to be used together with
|
|---|
| 60 | a TokenContainer in order to be useful.
|
|---|
| 61 | */
|
|---|
| 62 | class Token
|
|---|
| 63 | {
|
|---|
| 64 | public:
|
|---|
| 65 | Token()
|
|---|
| 66 | :start(0), length(0) {}
|
|---|
| 67 | Token(int p_start, int p_lenght)
|
|---|
| 68 | :start(p_start), length(p_lenght) {}
|
|---|
| 69 | int start;
|
|---|
| 70 | int length;
|
|---|
| 71 | };
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | Each TokenContainer has a TypeInfo object with meta-information.
|
|---|
| 75 | */
|
|---|
| 76 | class FileInfo;
|
|---|
| 77 | class GeneratedInfo;
|
|---|
| 78 | class TypeInfo
|
|---|
| 79 | {
|
|---|
| 80 | public:
|
|---|
| 81 | virtual ~TypeInfo() {};
|
|---|
| 82 | virtual FileInfo *toFileInfo() const {return 0;}
|
|---|
| 83 | virtual GeneratedInfo *toGeneratedInfo() const {return 0;}
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | MetaInfo for containers that contains tokens from a file
|
|---|
| 88 | */
|
|---|
| 89 | class FileInfo: public TypeInfo
|
|---|
| 90 | {
|
|---|
| 91 | public:
|
|---|
| 92 | FileInfo *toFileInfo() const
|
|---|
| 93 | {return const_cast<FileInfo *>(this);}
|
|---|
| 94 |
|
|---|
| 95 | QString filename;
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | MetaInfo for containers that contains generated tokens.
|
|---|
| 100 | */
|
|---|
| 101 | class GeneratedInfo: public TypeInfo
|
|---|
| 102 | {
|
|---|
| 103 | public:
|
|---|
| 104 | GeneratedInfo *toGeneratedInfo() const
|
|---|
| 105 | {return const_cast<GeneratedInfo *>(this);}
|
|---|
| 106 |
|
|---|
| 107 | //preprocessor tree pointer?
|
|---|
| 108 | };
|
|---|
| 109 |
|
|---|
| 110 | class TokenAttributes
|
|---|
| 111 | {
|
|---|
| 112 | public:
|
|---|
| 113 | void addAttribute(const QByteArray &name, const QByteArray &value);
|
|---|
| 114 | QByteArray attribute(const QByteArray &name) const;
|
|---|
| 115 | void addAttribute(const int index, const QByteArray &name, const QByteArray &value);
|
|---|
| 116 | QByteArray attribute(const int index, const QByteArray &name) const;
|
|---|
| 117 |
|
|---|
| 118 | private:
|
|---|
| 119 | inline QByteArray makeKeyText(const int index, const QByteArray &name) const;
|
|---|
| 120 | QMap<QByteArray, QByteArray> attributes;
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /*
|
|---|
| 125 | A TokenSequence that stores text and tokens referencing
|
|---|
| 126 | that text.
|
|---|
| 127 | */
|
|---|
| 128 | class TokenContainerData : public QSharedData
|
|---|
| 129 | {
|
|---|
| 130 | public:
|
|---|
| 131 | TokenContainerData()
|
|---|
| 132 | : typeInfo(0)
|
|---|
| 133 | {tokenAttributes = new TokenAttributes();}
|
|---|
| 134 | ~TokenContainerData()
|
|---|
| 135 | {delete tokenAttributes; delete typeInfo; }
|
|---|
| 136 | QByteArray text;
|
|---|
| 137 | QVector<Token> tokens;
|
|---|
| 138 | TypeInfo *typeInfo;
|
|---|
| 139 | TokenAttributes *tokenAttributes;
|
|---|
| 140 | };
|
|---|
| 141 | class TokenTempRef;
|
|---|
| 142 | class TokenContainer
|
|---|
| 143 | {
|
|---|
| 144 | public:
|
|---|
| 145 | TokenContainer();
|
|---|
| 146 | TokenContainer(QByteArray text, QVector<Token> tokens, TypeInfo *typeInfo = 0);
|
|---|
| 147 | int count() const;
|
|---|
| 148 | QByteArray text(const int index) const;
|
|---|
| 149 | QByteArray tempText(const int index) const;
|
|---|
| 150 | QByteArray fullText() const;
|
|---|
| 151 | TokenContainer tokenContainer(const int index) const;
|
|---|
| 152 | inline int containerIndex(const int index) const
|
|---|
| 153 | { return index; }
|
|---|
| 154 | Token token(const int index) const;
|
|---|
| 155 | TypeInfo *typeInfo();
|
|---|
| 156 | TokenAttributes *tokenAttributes();
|
|---|
| 157 | const TokenAttributes *tokenAttributes() const;
|
|---|
| 158 | int line(int index) const;
|
|---|
| 159 | int column(int index) const;
|
|---|
| 160 | TokenTempRef tokenTempRef(const int index) const;
|
|---|
| 161 | private:
|
|---|
| 162 | const QByteArray &textRef()
|
|---|
| 163 | { return d->text; }
|
|---|
| 164 | QExplicitlySharedDataPointer<TokenContainerData> d;
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | /*
|
|---|
| 168 | A reference to a single token in a container
|
|---|
| 169 | */
|
|---|
| 170 | class TokenRef
|
|---|
| 171 | {
|
|---|
| 172 | public:
|
|---|
| 173 | TokenRef(): m_index(-1) {}
|
|---|
| 174 | TokenRef(TokenContainer tokenContainer, int containerIndex)
|
|---|
| 175 | : m_tokenContainer(tokenContainer), m_index(containerIndex) {}
|
|---|
| 176 | inline int count() const
|
|---|
| 177 | { return m_index == -1 ? 0 : 1; }
|
|---|
| 178 | inline QByteArray text(const int index = 0) const
|
|---|
| 179 | { Q_UNUSED(index); return m_tokenContainer.text(m_index); }
|
|---|
| 180 | inline QByteArray tempText(const int index) const
|
|---|
| 181 | { Q_UNUSED(index); return m_tokenContainer.tempText(m_index); }
|
|---|
| 182 | inline QByteArray fullText() const
|
|---|
| 183 | { return text(); }
|
|---|
| 184 | inline TokenContainer tokenContainer(const int index = 0) const
|
|---|
| 185 | { Q_UNUSED(index); return m_tokenContainer; }
|
|---|
| 186 | inline int containerIndex(const int index = 0) const
|
|---|
| 187 | { Q_UNUSED(index); return m_index; }
|
|---|
| 188 | private:
|
|---|
| 189 | TokenContainer m_tokenContainer;
|
|---|
| 190 | int m_index;
|
|---|
| 191 | };
|
|---|
| 192 |
|
|---|
| 193 | /*
|
|---|
| 194 | A temporary reference to a single token in a container. This reference does
|
|---|
| 195 | not increase the refcount on the TokenContainer.
|
|---|
| 196 | */
|
|---|
| 197 | class TokenTempRef
|
|---|
| 198 | {
|
|---|
| 199 | public:
|
|---|
| 200 | TokenTempRef(const char *text, int length)
|
|---|
| 201 | : m_text(text), m_length(length) {}
|
|---|
| 202 | inline const char *constData() const
|
|---|
| 203 | { return m_text; }
|
|---|
| 204 | inline int length() const
|
|---|
| 205 | { return m_length; }
|
|---|
| 206 | char at(int index) const
|
|---|
| 207 | { Q_ASSERT(index < m_length); return m_text[index]; }
|
|---|
| 208 | private:
|
|---|
| 209 | const char *m_text;
|
|---|
| 210 | int m_length;
|
|---|
| 211 | };
|
|---|
| 212 |
|
|---|
| 213 | /*
|
|---|
| 214 | Contains a selected range from a TokenSequence.
|
|---|
| 215 | */
|
|---|
| 216 | class TokenSection
|
|---|
| 217 | {
|
|---|
| 218 | public:
|
|---|
| 219 | TokenSection() : m_start(0), m_count(0) {}
|
|---|
| 220 | TokenSection(TokenContainer tokenContainer,
|
|---|
| 221 | const int start, const int count)
|
|---|
| 222 | :m_tokenContainer(tokenContainer), m_start(start), m_count(count) {}
|
|---|
| 223 |
|
|---|
| 224 | inline int count() const
|
|---|
| 225 | { return m_count; }
|
|---|
| 226 | inline QByteArray text(const int index) const
|
|---|
| 227 | {
|
|---|
| 228 | const int cIndex = containerIndex(index);
|
|---|
| 229 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 230 | return m_tokenContainer.text(cIndex);
|
|---|
| 231 | }
|
|---|
| 232 | inline QByteArray tempText(const int index) const
|
|---|
| 233 | {
|
|---|
| 234 | const int cIndex = containerIndex(index);
|
|---|
| 235 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 236 | return m_tokenContainer.tempText(cIndex);
|
|---|
| 237 | }
|
|---|
| 238 | QByteArray fullText() const;
|
|---|
| 239 | inline TokenContainer tokenContainer(const int index = 0) const
|
|---|
| 240 | { Q_UNUSED(index); return m_tokenContainer; }
|
|---|
| 241 | inline int containerIndex(const int index) const
|
|---|
| 242 | { return m_start + index; }
|
|---|
| 243 | TokenTempRef tokenTempRef(const int index) const
|
|---|
| 244 | {
|
|---|
| 245 | const int cIndex = containerIndex(index);
|
|---|
| 246 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 247 | return m_tokenContainer.tokenTempRef(cIndex);
|
|---|
| 248 | }
|
|---|
| 249 | private:
|
|---|
| 250 | TokenContainer m_tokenContainer;
|
|---|
| 251 | int m_start;
|
|---|
| 252 | int m_count;
|
|---|
| 253 | };
|
|---|
| 254 |
|
|---|
| 255 | /*
|
|---|
| 256 | A list of tokens from a tokenContainer
|
|---|
| 257 | */
|
|---|
| 258 | class TokenList
|
|---|
| 259 | {
|
|---|
| 260 | public:
|
|---|
| 261 | TokenList() {};
|
|---|
| 262 | TokenList(TokenContainer tokenContainer, QVector<int> tokenList)
|
|---|
| 263 | :m_tokenContainer(tokenContainer), m_tokenList(tokenList) {}
|
|---|
| 264 | inline int count() const
|
|---|
| 265 | { return m_tokenList.count(); }
|
|---|
| 266 | inline QByteArray text(const int index) const
|
|---|
| 267 | {
|
|---|
| 268 | const int cIndex = containerIndex(index);
|
|---|
| 269 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 270 | return m_tokenContainer.text(cIndex);
|
|---|
| 271 | }
|
|---|
| 272 | inline QByteArray tempText(const int index) const
|
|---|
| 273 | {
|
|---|
| 274 | const int cIndex = containerIndex(index);
|
|---|
| 275 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 276 | return m_tokenContainer.tempText(cIndex);
|
|---|
| 277 | }
|
|---|
| 278 | QByteArray fullText() const;
|
|---|
| 279 | inline TokenContainer tokenContainer(const int index) const
|
|---|
| 280 | { Q_UNUSED(index); return m_tokenContainer; }
|
|---|
| 281 | inline int containerIndex(const int index) const
|
|---|
| 282 | { return m_tokenList.at(index); }
|
|---|
| 283 | Token token(const int index) const
|
|---|
| 284 | {
|
|---|
| 285 | const int cIndex = containerIndex(index);
|
|---|
| 286 | Q_ASSERT(cIndex < m_tokenContainer.count());
|
|---|
| 287 | return m_tokenContainer.token(cIndex);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | private:
|
|---|
| 291 | TokenContainer m_tokenContainer;
|
|---|
| 292 | QVector<int> m_tokenList;
|
|---|
| 293 | };
|
|---|
| 294 |
|
|---|
| 295 | /*
|
|---|
| 296 | Combines a list of TokenSequences into one TokenSectionSequence
|
|---|
| 297 | */
|
|---|
| 298 | class TokenSectionSequenceIterator;
|
|---|
| 299 | class TokenSectionSequence
|
|---|
| 300 | {
|
|---|
| 301 | public:
|
|---|
| 302 | TokenSectionSequence() :m_count(0) {};
|
|---|
| 303 | TokenSectionSequence(QVector<TokenSection> tokenSections);
|
|---|
| 304 |
|
|---|
| 305 | QByteArray fullText() const;
|
|---|
| 306 | int count() const;
|
|---|
| 307 | QVector<TokenSection> tokenSections() const;
|
|---|
| 308 |
|
|---|
| 309 | //random access interface, access time is linear on the number of sections
|
|---|
| 310 | QByteArray text(const int index) const;
|
|---|
| 311 | QByteArray tempText(const int index) const;
|
|---|
| 312 | TokenContainer tokenContainer(const int index) const;
|
|---|
| 313 | int containerIndex(const int index) const;
|
|---|
| 314 |
|
|---|
| 315 | protected:
|
|---|
| 316 | int findSection(const int index) const;
|
|---|
| 317 | int calculateInternalIndex(const int index, const int sectionIndex) const;
|
|---|
| 318 | private:
|
|---|
| 319 | QVector<TokenSection> m_tokenSections;
|
|---|
| 320 | QVector<int> m_startIndexes;
|
|---|
| 321 | int m_count;
|
|---|
| 322 | friend class TokenSectionSequenceIterator;
|
|---|
| 323 | };
|
|---|
| 324 |
|
|---|
| 325 | //sequental access interface, constant access time.
|
|---|
| 326 | class TokenSectionSequenceIterator
|
|---|
| 327 | {
|
|---|
| 328 | public:
|
|---|
| 329 | TokenSectionSequenceIterator(const TokenSectionSequence &tokenSectionSequence);
|
|---|
| 330 | void reset();
|
|---|
| 331 | bool nextToken();
|
|---|
| 332 | QByteArray text() const;
|
|---|
| 333 | QByteArray tempText() const;
|
|---|
| 334 | TokenContainer tokenContainer() const;
|
|---|
| 335 | int containerIndex() const;
|
|---|
| 336 | TokenTempRef tokenTempRef() const;
|
|---|
| 337 | private:
|
|---|
| 338 | int m_currentSection;
|
|---|
| 339 | int m_currentToken; // token index in currentTokenSequence;
|
|---|
| 340 | const int m_numSections;
|
|---|
| 341 | const TokenSectionSequence &m_tokenSectionSequence;
|
|---|
| 342 | };
|
|---|
| 343 |
|
|---|
| 344 | template <typename TokenSequence>
|
|---|
| 345 | QByteArray getText(TokenSequence tokenSequence)
|
|---|
| 346 | {
|
|---|
| 347 | QByteArray text;
|
|---|
| 348 | for (int t = 0; t<tokenSequence.count(); ++t) {
|
|---|
| 349 | text += tokenSequence.text(t);
|
|---|
| 350 | }
|
|---|
| 351 | return text;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /*
|
|---|
| 355 | Append the text and the tokens from the range [startToken, startToken + numTokens>
|
|---|
| 356 | to text and tokenList.
|
|---|
| 357 | */
|
|---|
| 358 | template <typename TokenSequenceType>
|
|---|
| 359 | void copy(QByteArray &text, QVector<TokenEngine::Token> &tokenList, const TokenSequenceType &tokenSequence, int startToken, int numTokens)
|
|---|
| 360 | {
|
|---|
| 361 | const int endToken = startToken + numTokens;
|
|---|
| 362 | int textIndex = text.count();
|
|---|
| 363 | for(int t = startToken; t < endToken; ++t) {
|
|---|
| 364 | const QByteArray tokenText = tokenSequence.text(t);
|
|---|
| 365 | const int tokenLength = tokenText.count();
|
|---|
| 366 | TokenEngine::Token token(textIndex, tokenLength);
|
|---|
| 367 | tokenList.append(token);
|
|---|
| 368 | text += tokenSequence.text(t);
|
|---|
| 369 | textIndex += tokenText.count();
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | /*
|
|---|
| 374 | Copy a the range [startToken, startToken + numTokens> from a tokenSequence to a new
|
|---|
| 375 | TokenConrtainer.
|
|---|
| 376 | */
|
|---|
| 377 | template <typename TokenSequenceType>
|
|---|
| 378 | TokenContainer copy(const TokenSequenceType &tokenSequence, int startToken, int numTokens)
|
|---|
| 379 | {
|
|---|
| 380 | QByteArray containerText;
|
|---|
| 381 | QVector<Token> tokens;
|
|---|
| 382 | tokens.reserve(numTokens);
|
|---|
| 383 | TokenEngine::copy(containerText, tokens, tokenSequence, startToken, numTokens);
|
|---|
| 384 | return TokenContainer(containerText, tokens);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | } //namespace TokenEngine
|
|---|
| 388 |
|
|---|
| 389 | QT_END_NAMESPACE
|
|---|
| 390 |
|
|---|
| 391 | #endif
|
|---|