source: trunk/tools/porting/src/tokenstreamadapter.h@ 315

Last change on this file since 315 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5** Copyright (C) 2001-2004 Roberto Raggi
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
25** additional rights. These rights are described in the Nokia Qt LGPL
26** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
27** package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37** If you are unsure which license is appropriate for your use, please
38** contact the sales department at [email protected].
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
51QT_BEGIN_NAMESPACE
52
53namespace TokenStreamAdapter {
54struct 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
139private:
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
150QT_END_NAMESPACE
151
152#endif
Note: See TracBrowser for help on using the repository browser.