1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** 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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "SimpleLexer.h"
36 #include "ObjectiveCTypeQualifiers.h"
42 using namespace CPlusPlus;
44 SimpleLexer::SimpleLexer()
47 _qtMocRunEnabled(true),
53 SimpleLexer::~SimpleLexer()
56 bool SimpleLexer::qtMocRunEnabled() const
58 return _qtMocRunEnabled;
61 void SimpleLexer::setQtMocRunEnabled(bool enabled)
63 _qtMocRunEnabled = enabled;
66 bool SimpleLexer::objCEnabled() const
71 void SimpleLexer::setObjCEnabled(bool onoff)
76 bool SimpleLexer::skipComments() const
81 void SimpleLexer::setSkipComments(bool skipComments)
83 _skipComments = skipComments;
86 bool SimpleLexer::endedJoined() const
91 QList<Token> SimpleLexer::operator()(const QString &text, int state)
95 const QByteArray bytes = text.toLatin1();
96 const char *firstChar = bytes.constData();
97 const char *lastChar = firstChar + bytes.size();
99 Lexer lex(firstChar, lastChar);
100 lex.setQtMocRunEnabled(_qtMocRunEnabled);
101 lex.setObjCEnabled(_objCEnabled);
102 lex.setStartWithNewline(true);
103 lex.setObjCEnabled(_objCEnabled);
106 lex.setScanCommentTokens(true);
109 lex.setState(state & 0xff);
111 bool inPreproc = false;
116 if (tk.is(T_EOF_SYMBOL)) {
117 _endedJoined = tk.joined();
121 QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength());
122 lex.setScanAngleStringLiteralTokens(false);
124 if (tk.f.newline && tk.is(T_POUND))
126 else if (inPreproc && tokens.size() == 1 && tk.is(T_IDENTIFIER) &&
127 spell == QLatin1String("include"))
128 lex.setScanAngleStringLiteralTokens(true);
129 else if (_objCEnabled
130 && inPreproc && tokens.size() == 1 && tk.is(T_IDENTIFIER) &&
131 spell == QLatin1String("import"))
132 lex.setScanAngleStringLiteralTokens(true);
137 _lastState = lex.state();
141 int SimpleLexer::tokenAt(const QList<Token> &tokens, unsigned offset)
143 for (int index = tokens.size() - 1; index >= 0; --index) {
144 const Token &tk = tokens.at(index);
145 if (tk.begin() <= offset && tk.end() >= offset)
152 Token SimpleLexer::tokenAt(const QString &text,
155 bool qtMocRunEnabled)
157 SimpleLexer tokenize;
158 tokenize.setQtMocRunEnabled(qtMocRunEnabled);
159 const QList<Token> tokens = tokenize(text, state);
160 const int tokenIdx = tokenAt(tokens, offset);
161 return (tokenIdx == -1) ? Token() : tokens.at(tokenIdx);
164 int SimpleLexer::tokenBefore(const QList<Token> &tokens, unsigned offset)
166 for (int index = tokens.size() - 1; index >= 0; --index) {
167 const Token &tk = tokens.at(index);
168 if (tk.begin() <= offset)