1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "SimpleLexer.h"
35 #include "ObjectiveCTypeQualifiers.h"
41 using namespace CPlusPlus;
43 SimpleLexer::SimpleLexer()
46 _qtMocRunEnabled(true),
52 SimpleLexer::~SimpleLexer()
55 bool SimpleLexer::qtMocRunEnabled() const
57 return _qtMocRunEnabled;
60 void SimpleLexer::setQtMocRunEnabled(bool enabled)
62 _qtMocRunEnabled = enabled;
65 bool SimpleLexer::objCEnabled() const
70 void SimpleLexer::setObjCEnabled(bool onoff)
75 bool SimpleLexer::skipComments() const
80 void SimpleLexer::setSkipComments(bool skipComments)
82 _skipComments = skipComments;
85 bool SimpleLexer::endedJoined() const
90 QList<Token> SimpleLexer::operator()(const QString &text, int state)
94 const QByteArray bytes = text.toLatin1();
95 const char *firstChar = bytes.constData();
96 const char *lastChar = firstChar + bytes.size();
98 Lexer lex(firstChar, lastChar);
99 lex.setQtMocRunEnabled(_qtMocRunEnabled);
100 lex.setObjCEnabled(_objCEnabled);
101 lex.setStartWithNewline(true);
102 lex.setObjCEnabled(_objCEnabled);
105 lex.setScanCommentTokens(true);
108 lex.setState(state & 0xff);
110 bool inPreproc = false;
115 if (tk.is(T_EOF_SYMBOL)) {
116 _endedJoined = tk.joined();
120 QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength());
121 lex.setScanAngleStringLiteralTokens(false);
123 if (tk.f.newline && tk.is(T_POUND))
125 else if (inPreproc && tokens.size() == 1 && tk.is(T_IDENTIFIER) &&
126 spell == QLatin1String("include"))
127 lex.setScanAngleStringLiteralTokens(true);
128 else if (_objCEnabled
129 && inPreproc && tokens.size() == 1 && tk.is(T_IDENTIFIER) &&
130 spell == QLatin1String("import"))
131 lex.setScanAngleStringLiteralTokens(true);
136 _lastState = lex.state();
140 int SimpleLexer::tokenAt(const QList<Token> &tokens, unsigned offset)
142 for (int index = tokens.size() - 1; index >= 0; --index) {
143 const Token &tk = tokens.at(index);
144 if (tk.begin() <= offset && tk.end() >= offset)
151 Token SimpleLexer::tokenAt(const QString &text,
154 bool qtMocRunEnabled)
156 SimpleLexer tokenize;
157 tokenize.setQtMocRunEnabled(qtMocRunEnabled);
158 const QList<Token> tokens = tokenize(text, state);
159 const int tokenIdx = tokenAt(tokens, offset);
160 return (tokenIdx == -1) ? Token() : tokens.at(tokenIdx);
163 int SimpleLexer::tokenBefore(const QList<Token> &tokens, unsigned offset)
165 for (int index = tokens.size() - 1; index >= 0; --index) {
166 const Token &tk = tokens.at(index);
167 if (tk.begin() <= offset)