~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/porting/src/tokenstreamadapter.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
** Copyright (C) 2001-2004 Roberto Raggi
 
5
**
 
6
** This file is part of the porting application of the Qt Toolkit.
 
7
**
 
8
** This file may be distributed under the terms of the Q Public License
 
9
** as defined by Trolltech AS of Norway and appearing in the file
 
10
** LICENSE.QPL included in the packaging of this file.
 
11
**
 
12
** This file may be distributed and/or modified under the terms of the
 
13
** GNU General Public License version 2 as published by the Free Software
 
14
** Foundation and appearing in the file LICENSE.GPL included in the
 
15
** packaging of this file.
 
16
**
 
17
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
18
**   information about Qt Commercial License Agreements.
 
19
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
20
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
21
**
 
22
** Contact info@trolltech.com if any conditions of this licensing are
 
23
** not clear to you.
 
24
**
 
25
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
26
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
27
**
 
28
****************************************************************************/
 
29
#ifndef TOKENSTREAMADAPTER_H
 
30
#define TOKENSTREAMADAPTER_H
 
31
 
 
32
#include <QVector>
 
33
 
 
34
#include "tokenengine.h"
 
35
#include "tokens.h"
 
36
 
 
37
namespace TokenStreamAdapter {
 
38
struct TokenStream
 
39
{
 
40
    TokenStream(TokenEngine::TokenSectionSequence translationUnit, QVector<Type> tokenKindList)
 
41
    :m_translationUnit(translationUnit),
 
42
     m_tokenKindList(tokenKindList),
 
43
     m_cursor(0),
 
44
     m_numTokens(tokenKindList.count())
 
45
    {
 
46
        Q_ASSERT(translationUnit.count() == m_numTokens);
 
47
 
 
48
        // Copy out the container and containerIndex for each token so we can have
 
49
        // constant time random access to it.
 
50
        TokenEngine::TokenSectionSequenceIterator it(translationUnit);
 
51
        while(it.nextToken()) {
 
52
            m_tokenContainers.append(it.tokenContainer());
 
53
            m_containerIndices.append(it.containerIndex());
 
54
        }
 
55
    }
 
56
 
 
57
    bool isHidden(int index) const
 
58
    {
 
59
       if(index >= m_numTokens)
 
60
            return false;
 
61
        ::Type type = m_tokenKindList.at(index);
 
62
        return (type == Token_whitespaces || type == 10 /*newline*/ ||
 
63
                type == Token_comment || type == Token_preproc );
 
64
    }
 
65
 
 
66
    inline int lookAhead(int n = 0) const
 
67
    {
 
68
        if(m_cursor + n >= m_numTokens)
 
69
            return 0;
 
70
        return m_tokenKindList.at(m_cursor + n);
 
71
    }
 
72
 
 
73
    inline int currentToken() const
 
74
    { return lookAhead(); }
 
75
 
 
76
    inline QByteArray currentTokenText() const
 
77
    {
 
78
        return tokenText(m_cursor);
 
79
    }
 
80
 
 
81
    inline TokenEngine::TokenContainer tokenContainer(int index = 0) const
 
82
    {
 
83
        if (index < m_numTokens)
 
84
            return m_tokenContainers.at(index);
 
85
        else
 
86
            return TokenEngine::TokenContainer();
 
87
    }
 
88
 
 
89
    inline int containerIndex(int index = 0) const
 
90
    {
 
91
        if (index < m_numTokens)
 
92
            return m_containerIndices.at(index);
 
93
        else
 
94
            return -1;
 
95
    }
 
96
 
 
97
    inline QByteArray tokenText(int index = 0) const
 
98
    {
 
99
        if (index <  m_numTokens) {
 
100
            const TokenEngine::TokenContainer container = tokenContainer(index);
 
101
            const int cIndex = containerIndex(index);
 
102
            return container.text(cIndex);
 
103
        } else {
 
104
            return QByteArray();
 
105
        }
 
106
    }
 
107
 
 
108
    inline void rewind(int index)
 
109
    { m_cursor = index; }
 
110
 
 
111
    inline int cursor() const
 
112
    { return m_cursor; }
 
113
 
 
114
    inline void nextToken()
 
115
    { ++m_cursor; }
 
116
 
 
117
    inline bool tokenAtEnd()
 
118
    { return m_cursor >= m_numTokens; }
 
119
 
 
120
    TokenEngine::TokenSectionSequence tokenSections() const
 
121
    { return m_translationUnit;  }
 
122
 
 
123
private:
 
124
   TokenEngine::TokenSectionSequence m_translationUnit;
 
125
   QVector<Type> m_tokenKindList;
 
126
   QList<TokenEngine::TokenContainer> m_tokenContainers;
 
127
   QList<int> m_containerIndices;
 
128
   int m_cursor;
 
129
   int m_numTokens;
 
130
};
 
131
 
 
132
} //namespace TokenStreamAdapter
 
133
 
 
134
#endif