SimpleLexer.cpp 4 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 66 67 68
bool SimpleToken::isComment() const
{
    return _kind == T_COMMENT || _kind == T_DOXY_COMMENT;
}

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

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

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

SimpleLexer::~SimpleLexer()
{ }

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

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

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

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

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

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

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
131
    lex.setObjCEnabled(_objCEnabled);
132
    lex.setStartWithNewline(true);
con's avatar
con committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    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;

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

152
        if (tk.f.newline && tk.is(T_POUND))
con's avatar
con committed
153 154 155 156 157 158 159 160 161 162 163 164 165
            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;
}