OSDN Git Service

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