SimpleLexer.cpp 4.07 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
con's avatar
con committed
29
30

#include "SimpleLexer.h"
hjk's avatar
hjk committed
31

con's avatar
con committed
32
33
34
35
36
37
#include <Lexer.h>
#include <Token.h>
#include <QtDebug>

using namespace CPlusPlus;

38
39
40
41
42
43
44
45
46
47
48
SimpleToken::SimpleToken(const Token &token, const QStringRef &text)
    : _kind(token.f.kind)
    , _flags(0)
    , _position(token.begin())
    , _length(token.f.length)
    , _text(text)
{
    f._whitespace = token.f.whitespace;
    f._newline = token.f.newline;
}

con's avatar
con committed
49
bool SimpleToken::isLiteral() const
hjk's avatar
hjk committed
50
51
52
{
    return _kind >= T_FIRST_LITERAL && _kind <= T_LAST_LITERAL;
}
con's avatar
con committed
53
54

bool SimpleToken::isOperator() const
hjk's avatar
hjk committed
55
56
57
{
    return _kind >= T_FIRST_OPERATOR && _kind <= T_LAST_OPERATOR;
}
con's avatar
con committed
58
59

bool SimpleToken::isKeyword() const
hjk's avatar
hjk committed
60
61
62
{
    return _kind >= T_FIRST_KEYWORD && _kind < T_FIRST_QT_KEYWORD;
}
con's avatar
con committed
63

64
65
bool SimpleToken::isComment() const
{
66
67
    return _kind == T_COMMENT || _kind == T_DOXY_COMMENT ||
            _kind == T_CPP_COMMENT || _kind == T_CPP_DOXY_COMMENT;
68
69
}

70
71
bool SimpleToken::isObjCAtKeyword() const
{
72
    return _kind >= T_FIRST_OBJC_AT_KEYWORD && _kind <= T_LAST_OBJC_AT_KEYWORD;
73
74
}

75
76
77
78
79
const char *SimpleToken::name() const
{
    return Token::name(_kind);
}

con's avatar
con committed
80
81
82
SimpleLexer::SimpleLexer()
    : _lastState(0),
      _skipComments(false),
83
      _qtMocRunEnabled(true),
Roberto Raggi's avatar
Roberto Raggi committed
84
      _objCEnabled(false)
Roberto Raggi's avatar
Roberto Raggi committed
85
86
87
{
    setObjCEnabled(true);
}
con's avatar
con committed
88
89
90
91
92

SimpleLexer::~SimpleLexer()
{ }

bool SimpleLexer::qtMocRunEnabled() const
hjk's avatar
hjk committed
93
94
95
{
    return _qtMocRunEnabled;
}
con's avatar
con committed
96
97

void SimpleLexer::setQtMocRunEnabled(bool enabled)
hjk's avatar
hjk committed
98
99
100
{
    _qtMocRunEnabled = enabled;
}
con's avatar
con committed
101

Roberto Raggi's avatar
Roberto Raggi committed
102
bool SimpleLexer::objCEnabled() const
103
{
Roberto Raggi's avatar
Roberto Raggi committed
104
    return _objCEnabled;
105
106
}

Roberto Raggi's avatar
Roberto Raggi committed
107
void SimpleLexer::setObjCEnabled(bool onoff)
108
{
Roberto Raggi's avatar
Roberto Raggi committed
109
    _objCEnabled = onoff;
110
111
}

con's avatar
con committed
112
bool SimpleLexer::skipComments() const
hjk's avatar
hjk committed
113
114
115
{
    return _skipComments;
}
con's avatar
con committed
116
117

void SimpleLexer::setSkipComments(bool skipComments)
hjk's avatar
hjk committed
118
119
120
{
    _skipComments = skipComments;
}
con's avatar
con committed
121
122
123
124
125
126
127
128
129
130
131

QList<SimpleToken> SimpleLexer::operator()(const QString &text, int state)
{
    QList<SimpleToken> tokens;

    const QByteArray bytes = text.toLatin1();
    const char *firstChar = bytes.constData();
    const char *lastChar = firstChar + bytes.size();

    Lexer lex(firstChar, lastChar);
    lex.setQtMocRunEnabled(_qtMocRunEnabled);
Roberto Raggi's avatar
Roberto Raggi committed
132
    lex.setObjCEnabled(_objCEnabled);
133
    lex.setStartWithNewline(true);
con's avatar
con committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    if (! _skipComments)
        lex.setScanCommentTokens(true);

    if (state != -1)
        lex.setState(state & 0xff);

    bool inPreproc = false;

    for (;;) {
        Token tk;
        lex(&tk);
        if (tk.is(T_EOF_SYMBOL))
            break;

149
150
        QStringRef spell = text.midRef(lex.tokenOffset(), lex.tokenLength());
        SimpleToken simpleTk(tk, spell);
con's avatar
con committed
151
152
        lex.setScanAngleStringLiteralTokens(false);

153
        if (tk.f.newline && tk.is(T_POUND))
con's avatar
con committed
154
155
156
157
158
159
160
161
162
163
164
165
166
            inPreproc = true;
        else if (inPreproc && tokens.size() == 1 && simpleTk.is(T_IDENTIFIER) &&
                 simpleTk.text() == QLatin1String("include"))
            lex.setScanAngleStringLiteralTokens(true);

        tokens.append(simpleTk);
    }

    _lastState = lex.state();
    return tokens;
}