tools/porting/src/tokenstreamadapter.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2001-2004 Roberto Raggi
       
     4 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     5 ** All rights reserved.
       
     6 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     7 **
       
     8 ** This file is part of the qt3to4 porting application of the Qt Toolkit.
       
     9 **
       
    10 ** $QT_BEGIN_LICENSE:LGPL$
       
    11 ** No Commercial Usage
       
    12 ** This file contains pre-release code and may not be distributed.
       
    13 ** You may use this file in accordance with the terms and conditions
       
    14 ** contained in the Technology Preview License Agreement accompanying
       
    15 ** this package.
       
    16 **
       
    17 ** GNU Lesser General Public License Usage
       
    18 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    19 ** General Public License version 2.1 as published by the Free Software
       
    20 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    21 ** packaging of this file.  Please review the following information to
       
    22 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    24 **
       
    25 ** In addition, as a special exception, Nokia gives you certain additional
       
    26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    28 **
       
    29 ** If you have questions regarding the use of this file, please contact
       
    30 ** Nokia at qt-info@nokia.com.
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 **
       
    39 ** $QT_END_LICENSE$
       
    40 **
       
    41 ****************************************************************************/
       
    42 
       
    43 #ifndef TOKENSTREAMADAPTER_H
       
    44 #define TOKENSTREAMADAPTER_H
       
    45 
       
    46 #include "tokenengine.h"
       
    47 #include "tokens.h"
       
    48 
       
    49 #include <QVector>
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 
       
    53 namespace TokenStreamAdapter {
       
    54 struct TokenStream
       
    55 {
       
    56     TokenStream(TokenEngine::TokenSectionSequence translationUnit, QVector<Type> tokenKindList)
       
    57     :m_translationUnit(translationUnit),
       
    58      m_tokenKindList(tokenKindList),
       
    59      m_cursor(0),
       
    60      m_numTokens(tokenKindList.count())
       
    61     {
       
    62         Q_ASSERT(translationUnit.count() == m_numTokens);
       
    63 
       
    64         // Copy out the container and containerIndex for each token so we can have
       
    65         // constant time random access to it.
       
    66         TokenEngine::TokenSectionSequenceIterator it(translationUnit);
       
    67         while(it.nextToken()) {
       
    68             m_tokenContainers.append(it.tokenContainer());
       
    69             m_containerIndices.append(it.containerIndex());
       
    70         }
       
    71     }
       
    72 
       
    73     bool isHidden(int index) const
       
    74     {
       
    75        if(index >= m_numTokens)
       
    76             return false;
       
    77         QT_PREPEND_NAMESPACE(Type) type = m_tokenKindList.at(index);
       
    78         return (type == Token_whitespaces || type == 10 /*newline*/ ||
       
    79                 type == Token_comment || type == Token_preproc );
       
    80     }
       
    81 
       
    82     inline int lookAhead(int n = 0) const
       
    83     {
       
    84         if(m_cursor + n >= m_numTokens)
       
    85             return 0;
       
    86         return m_tokenKindList.at(m_cursor + n);
       
    87     }
       
    88 
       
    89     inline int currentToken() const
       
    90     { return lookAhead(); }
       
    91 
       
    92     inline QByteArray currentTokenText() const
       
    93     {
       
    94         return tokenText(m_cursor);
       
    95     }
       
    96 
       
    97     inline TokenEngine::TokenContainer tokenContainer(int index = 0) const
       
    98     {
       
    99         if (index < m_numTokens)
       
   100             return m_tokenContainers.at(index);
       
   101         else
       
   102             return TokenEngine::TokenContainer();
       
   103     }
       
   104 
       
   105     inline int containerIndex(int index = 0) const
       
   106     {
       
   107         if (index < m_numTokens)
       
   108             return m_containerIndices.at(index);
       
   109         else
       
   110             return -1;
       
   111     }
       
   112 
       
   113     inline QByteArray tokenText(int index = 0) const
       
   114     {
       
   115         if (index <  m_numTokens) {
       
   116             const TokenEngine::TokenContainer container = tokenContainer(index);
       
   117             const int cIndex = containerIndex(index);
       
   118             return container.text(cIndex);
       
   119         } else {
       
   120             return QByteArray();
       
   121         }
       
   122     }
       
   123 
       
   124     inline void rewind(int index)
       
   125     { m_cursor = index; }
       
   126 
       
   127     inline int cursor() const
       
   128     { return m_cursor; }
       
   129 
       
   130     inline void nextToken()
       
   131     { ++m_cursor; }
       
   132 
       
   133     inline bool tokenAtEnd()
       
   134     { return m_cursor >= m_numTokens; }
       
   135 
       
   136     TokenEngine::TokenSectionSequence tokenSections() const
       
   137     { return m_translationUnit;  }
       
   138 
       
   139 private:
       
   140    TokenEngine::TokenSectionSequence m_translationUnit;
       
   141    QVector<Type> m_tokenKindList;
       
   142    QList<TokenEngine::TokenContainer> m_tokenContainers;
       
   143    QList<int> m_containerIndices;
       
   144    int m_cursor;
       
   145    int m_numTokens;
       
   146 };
       
   147 
       
   148 } //namespace TokenStreamAdapter
       
   149 
       
   150 QT_END_NAMESPACE
       
   151 
       
   152 #endif