OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / cplusplus / SimpleLexer.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "SimpleLexer.h"
34
35 #include "ObjectiveCTypeQualifiers.h"
36
37 #include <Lexer.h>
38 #include <Token.h>
39 #include <QtDebug>
40
41 using namespace CPlusPlus;
42
43 SimpleLexer::SimpleLexer()
44     : _lastState(0),
45       _skipComments(false),
46       _qtMocRunEnabled(true),
47       _objCEnabled(false),
48       _endedJoined(false)
49 {
50 }
51
52 SimpleLexer::~SimpleLexer()
53 { }
54
55 bool SimpleLexer::qtMocRunEnabled() const
56 {
57     return _qtMocRunEnabled;
58 }
59
60 void SimpleLexer::setQtMocRunEnabled(bool enabled)
61 {
62     _qtMocRunEnabled = enabled;
63 }
64
65 bool SimpleLexer::objCEnabled() const
66 {
67     return _objCEnabled;
68 }
69
70 void SimpleLexer::setObjCEnabled(bool onoff)
71 {
72     _objCEnabled = onoff;
73 }
74
75 bool SimpleLexer::skipComments() const
76 {
77     return _skipComments;
78 }
79
80 void SimpleLexer::setSkipComments(bool skipComments)
81 {
82     _skipComments = skipComments;
83 }
84
85 bool SimpleLexer::endedJoined() const
86 {
87     return _endedJoined;
88 }
89
90 QList<Token> SimpleLexer::operator()(const QString &text, int state)
91 {
92     QList<Token> tokens;
93
94     const QByteArray bytes = text.toLatin1();
95     const char *firstChar = bytes.constData();
96     const char *lastChar = firstChar + bytes.size();
97
98     Lexer lex(firstChar, lastChar);
99     lex.setQtMocRunEnabled(_qtMocRunEnabled);
100     lex.setObjCEnabled(_objCEnabled);
101     lex.setStartWithNewline(true);
102     lex.setObjCEnabled(_objCEnabled);
103
104     if (! _skipComments)
105         lex.setScanCommentTokens(true);
106
107     if (state != -1)
108         lex.setState(state & 0xff);
109
110     bool inPreproc = false;
111
112     for (;;) {
113         Token tk;
114         lex(&tk);
115         if (tk.is(T_EOF_SYMBOL)) {
116             _endedJoined = tk.joined();
117             break;
118         }
119
120         QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength());
121         lex.setScanAngleStringLiteralTokens(false);
122
123         if (tk.f.newline && tk.is(T_POUND))
124             inPreproc = true;
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);
132
133         tokens.append(tk);
134     }
135
136     _lastState = lex.state();
137     return tokens;
138 }
139
140 int SimpleLexer::tokenAt(const QList<Token> &tokens, unsigned offset)
141 {
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)
145             return index;
146     }
147
148     return -1;
149 }
150
151 Token SimpleLexer::tokenAt(const QString &text,
152                            unsigned offset,
153                            int state,
154                            bool qtMocRunEnabled)
155 {
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);
161 }
162
163 int SimpleLexer::tokenBefore(const QList<Token> &tokens, unsigned offset)
164 {
165     for (int index = tokens.size() - 1; index >= 0; --index) {
166         const Token &tk = tokens.at(index);
167         if (tk.begin() <= offset)
168             return index;
169     }
170
171     return -1;
172 }