2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-01-04 11:39:56 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-01-04 11:39:56 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-01-04 11:39:56 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-01-04 11:39:56 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-01-04 11:39:56 +01:00
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
#include <qmljs/qmljsscanner.h>
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-18 14:12:04 +01:00
|
|
|
using namespace QmlJS;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-29 11:20:42 +01:00
|
|
|
namespace {
|
|
|
|
|
QString js_keywords[] = {
|
|
|
|
|
QLatin1String("break"),
|
|
|
|
|
QString::fromLatin1("case"),
|
|
|
|
|
QString::fromLatin1("catch"),
|
|
|
|
|
QString::fromLatin1("continue"),
|
|
|
|
|
QString::fromLatin1("debugger"),
|
|
|
|
|
QString::fromLatin1("default"),
|
|
|
|
|
QString::fromLatin1("delete"),
|
|
|
|
|
QString::fromLatin1("do"),
|
|
|
|
|
QString::fromLatin1("else"),
|
|
|
|
|
QString::fromLatin1("finally"),
|
|
|
|
|
QString::fromLatin1("for"),
|
|
|
|
|
QString::fromLatin1("function"),
|
|
|
|
|
QString::fromLatin1("if"),
|
|
|
|
|
QString::fromLatin1("in"),
|
|
|
|
|
QString::fromLatin1("instanceof"),
|
|
|
|
|
QString::fromLatin1("new"),
|
|
|
|
|
QString::fromLatin1("return"),
|
|
|
|
|
QString::fromLatin1("switch"),
|
|
|
|
|
QString::fromLatin1("this"),
|
|
|
|
|
QString::fromLatin1("throw"),
|
|
|
|
|
QString::fromLatin1("try"),
|
|
|
|
|
QString::fromLatin1("typeof"),
|
|
|
|
|
QString::fromLatin1("var"),
|
|
|
|
|
QString::fromLatin1("void"),
|
|
|
|
|
QString::fromLatin1("while"),
|
|
|
|
|
QString::fromLatin1("with")
|
|
|
|
|
};
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
template <typename _Tp, int N>
|
|
|
|
|
const _Tp *begin(const _Tp (&a)[N])
|
|
|
|
|
{
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename _Tp, int N>
|
|
|
|
|
const _Tp *end(const _Tp (&a)[N])
|
|
|
|
|
{
|
|
|
|
|
return a + N;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
Scanner::Scanner()
|
2010-05-27 11:26:20 +02:00
|
|
|
: _state(Normal),
|
2010-01-29 15:05:22 +01:00
|
|
|
_scanComments(true)
|
2009-09-28 14:49:39 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
Scanner::~Scanner()
|
2010-01-28 13:12:52 +01:00
|
|
|
{
|
|
|
|
|
}
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
bool Scanner::scanComments() const
|
2010-01-29 15:05:22 +01:00
|
|
|
{
|
|
|
|
|
return _scanComments;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
void Scanner::setScanComments(bool scanComments)
|
2010-01-29 15:05:22 +01:00
|
|
|
{
|
|
|
|
|
_scanComments = scanComments;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
static bool isIdentifierChar(QChar ch)
|
2009-09-28 14:49:39 +02:00
|
|
|
{
|
2010-01-28 13:12:52 +01:00
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '$': case '_':
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return ch.isLetterOrNumber();
|
|
|
|
|
}
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
static bool isNumberChar(QChar ch)
|
2009-09-28 14:49:39 +02:00
|
|
|
{
|
2010-01-28 13:12:52 +01:00
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '.':
|
|
|
|
|
case 'e':
|
|
|
|
|
case 'E': // ### more...
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return ch.isLetterOrNumber();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2011-05-20 14:48:05 +02:00
|
|
|
static int findRegExpEnd(const QString &text, int start)
|
|
|
|
|
{
|
|
|
|
|
if (start >= text.size() || text.at(start) != QLatin1Char('/'))
|
|
|
|
|
return start;
|
|
|
|
|
|
|
|
|
|
// find the second /
|
|
|
|
|
int index = start + 1;
|
|
|
|
|
for (; index < text.length(); ++index) {
|
|
|
|
|
const QChar ch = text.at(index);
|
|
|
|
|
|
|
|
|
|
if (ch == QLatin1Char('\\')) {
|
|
|
|
|
++index;
|
|
|
|
|
} else if (ch == QLatin1Char('[')) {
|
|
|
|
|
// find closing ]
|
|
|
|
|
for (; index < text.length(); ++index) {
|
|
|
|
|
const QChar ch2 = text.at(index);
|
|
|
|
|
if (ch2 == QLatin1Char('\\')) {
|
|
|
|
|
++index;
|
|
|
|
|
} else if (ch2 == QLatin1Char(']'))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (index >= text.size())
|
|
|
|
|
return text.size();
|
|
|
|
|
} else if (ch == QLatin1Char('/'))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (index >= text.size())
|
|
|
|
|
return text.size();
|
|
|
|
|
++index;
|
|
|
|
|
|
|
|
|
|
// find end of reg exp flags
|
|
|
|
|
for (; index < text.size(); ++index) {
|
|
|
|
|
const QChar ch = text.at(index);
|
|
|
|
|
if (!isIdentifierChar(ch))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline int multiLineState(int state)
|
|
|
|
|
{
|
2011-06-03 08:49:40 +02:00
|
|
|
return state & Scanner::MultiLineMask;
|
2011-05-20 14:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void setMultiLineState(int *state, int s)
|
|
|
|
|
{
|
2011-06-03 08:49:40 +02:00
|
|
|
*state = s | (*state & ~Scanner::MultiLineMask);
|
2011-05-20 14:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool regexpMayFollow(int state)
|
|
|
|
|
{
|
2011-06-03 08:49:40 +02:00
|
|
|
return state & Scanner::RegexpMayFollow;
|
2011-05-20 14:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void setRegexpMayFollow(int *state, bool on)
|
|
|
|
|
{
|
2011-06-03 08:49:40 +02:00
|
|
|
*state = (on ? Scanner::RegexpMayFollow : 0) | (*state & ~Scanner::RegexpMayFollow);
|
2011-05-20 14:48:05 +02:00
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
QList<Token> Scanner::operator()(const QString &text, int startState)
|
2010-01-28 13:12:52 +01:00
|
|
|
{
|
2010-01-29 15:05:22 +01:00
|
|
|
_state = startState;
|
2010-01-28 13:12:52 +01:00
|
|
|
QList<Token> tokens;
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
2011-05-20 14:48:05 +02:00
|
|
|
if (multiLineState(_state) == MultiLineComment) {
|
2010-07-08 14:16:58 +02:00
|
|
|
int start = -1;
|
2010-01-28 13:12:52 +01:00
|
|
|
while (index < text.length()) {
|
|
|
|
|
const QChar ch = text.at(index);
|
2010-07-08 14:16:58 +02:00
|
|
|
|
|
|
|
|
if (start == -1 && !ch.isSpace())
|
|
|
|
|
start = index;
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
QChar la;
|
|
|
|
|
if (index + 1 < text.length())
|
|
|
|
|
la = text.at(index + 1);
|
|
|
|
|
|
|
|
|
|
if (ch == QLatin1Char('*') && la == QLatin1Char('/')) {
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, Normal);
|
2010-01-28 13:12:52 +01:00
|
|
|
index += 2;
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-07-08 14:31:07 +02:00
|
|
|
if (_scanComments && start != -1)
|
2010-01-29 15:05:22 +01:00
|
|
|
tokens.append(Token(start, index - start, Token::Comment));
|
2011-05-20 14:48:05 +02:00
|
|
|
} else if (multiLineState(_state) == MultiLineStringDQuote || multiLineState(_state) == MultiLineStringSQuote) {
|
2010-11-29 14:00:43 +10:00
|
|
|
const QChar quote = (_state == MultiLineStringDQuote ? QLatin1Char('"') : QLatin1Char('\''));
|
|
|
|
|
const int start = index;
|
|
|
|
|
while (index < text.length()) {
|
|
|
|
|
const QChar ch = text.at(index);
|
|
|
|
|
|
|
|
|
|
if (ch == quote)
|
|
|
|
|
break;
|
|
|
|
|
else if (index + 1 < text.length() && ch == QLatin1Char('\\'))
|
|
|
|
|
index += 2;
|
|
|
|
|
else
|
|
|
|
|
++index;
|
|
|
|
|
}
|
|
|
|
|
if (index < text.length()) {
|
|
|
|
|
++index;
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, Normal);
|
2010-11-29 14:00:43 +10:00
|
|
|
}
|
|
|
|
|
if (start < index)
|
|
|
|
|
tokens.append(Token(start, index - start, Token::String));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
while (index < text.length()) {
|
|
|
|
|
const QChar ch = text.at(index);
|
|
|
|
|
|
|
|
|
|
QChar la; // lookahead char
|
|
|
|
|
if (index + 1 < text.length())
|
|
|
|
|
la = text.at(index + 1);
|
|
|
|
|
|
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
|
case '/':
|
|
|
|
|
if (la == QLatin1Char('/')) {
|
2010-01-29 15:05:22 +01:00
|
|
|
if (_scanComments)
|
|
|
|
|
tokens.append(Token(index, text.length() - index, Token::Comment));
|
2010-01-28 13:12:52 +01:00
|
|
|
index = text.length();
|
|
|
|
|
} else if (la == QLatin1Char('*')) {
|
|
|
|
|
const int start = index;
|
|
|
|
|
index += 2;
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, MultiLineComment);
|
2010-01-28 13:12:52 +01:00
|
|
|
while (index < text.length()) {
|
|
|
|
|
const QChar ch = text.at(index);
|
|
|
|
|
QChar la;
|
|
|
|
|
if (index + 1 < text.length())
|
|
|
|
|
la = text.at(index + 1);
|
|
|
|
|
|
|
|
|
|
if (ch == QLatin1Char('*') && la == QLatin1Char('/')) {
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, Normal);
|
2010-01-28 13:12:52 +01:00
|
|
|
index += 2;
|
|
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
} else {
|
2010-01-28 13:12:52 +01:00
|
|
|
++index;
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-29 15:05:22 +01:00
|
|
|
if (_scanComments)
|
|
|
|
|
tokens.append(Token(start, index - start, Token::Comment));
|
2011-05-20 14:48:05 +02:00
|
|
|
} else if (regexpMayFollow(_state)) {
|
|
|
|
|
const int end = findRegExpEnd(text, index);
|
|
|
|
|
tokens.append(Token(index, end - index, Token::RegExp));
|
|
|
|
|
index = end;
|
|
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
} else {
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Delimiter));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '\'':
|
|
|
|
|
case '"': {
|
|
|
|
|
const QChar quote = ch;
|
|
|
|
|
const int start = index;
|
|
|
|
|
++index;
|
|
|
|
|
while (index < text.length()) {
|
|
|
|
|
const QChar ch = text.at(index);
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
if (ch == quote)
|
|
|
|
|
break;
|
|
|
|
|
else if (index + 1 < text.length() && ch == QLatin1Char('\\'))
|
|
|
|
|
index += 2;
|
|
|
|
|
else
|
|
|
|
|
++index;
|
|
|
|
|
}
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
if (index < text.length()) {
|
|
|
|
|
++index;
|
|
|
|
|
// good one
|
|
|
|
|
} else {
|
2010-11-29 14:00:43 +10:00
|
|
|
if (quote.unicode() == '"')
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, MultiLineStringDQuote);
|
2010-11-29 14:00:43 +10:00
|
|
|
else
|
2011-05-20 14:48:05 +02:00
|
|
|
setMultiLineState(&_state, MultiLineStringSQuote);
|
2010-01-28 13:12:52 +01:00
|
|
|
}
|
2009-10-07 16:31:08 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
tokens.append(Token(start, index - start, Token::String));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
} break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '.':
|
|
|
|
|
if (la.isDigit()) {
|
|
|
|
|
const int start = index;
|
|
|
|
|
do {
|
|
|
|
|
++index;
|
|
|
|
|
} while (index < text.length() && isNumberChar(text.at(index)));
|
|
|
|
|
tokens.append(Token(start, index - start, Token::Number));
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2010-01-28 13:12:52 +01:00
|
|
|
tokens.append(Token(index++, 1, Token::Dot));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '(':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::LeftParenthesis));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case ')':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::RightParenthesis));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2009-09-28 14:49:39 +02:00
|
|
|
break;
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '[':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::LeftBracket));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case ']':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::RightBracket));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '{':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::LeftBrace));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-11-16 12:38:47 +01:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case '}':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::RightBrace));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case ';':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Semicolon));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
2009-09-28 14:49:39 +02:00
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
case ':':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Colon));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Comma));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
break;
|
|
|
|
|
|
2010-08-10 12:05:19 +02:00
|
|
|
case '+':
|
|
|
|
|
case '-':
|
|
|
|
|
if (la == ch) {
|
|
|
|
|
tokens.append(Token(index, 2, Token::Delimiter));
|
|
|
|
|
index += 2;
|
|
|
|
|
} else {
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Delimiter));
|
|
|
|
|
}
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-08-10 12:05:19 +02:00
|
|
|
break;
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
default:
|
2010-01-29 09:48:21 +01:00
|
|
|
if (ch.isSpace()) {
|
|
|
|
|
do {
|
|
|
|
|
++index;
|
|
|
|
|
} while (index < text.length() && text.at(index).isSpace());
|
|
|
|
|
} else if (ch.isNumber()) {
|
2010-01-28 13:12:52 +01:00
|
|
|
const int start = index;
|
|
|
|
|
do {
|
|
|
|
|
++index;
|
|
|
|
|
} while (index < text.length() && isNumberChar(text.at(index)));
|
|
|
|
|
tokens.append(Token(start, index - start, Token::Number));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
} else if (ch.isLetter() || ch == QLatin1Char('_') || ch == QLatin1Char('$')) {
|
|
|
|
|
const int start = index;
|
|
|
|
|
do {
|
|
|
|
|
++index;
|
|
|
|
|
} while (index < text.length() && isIdentifierChar(text.at(index)));
|
|
|
|
|
|
|
|
|
|
if (isKeyword(text.mid(start, index - start)))
|
|
|
|
|
tokens.append(Token(start, index - start, Token::Keyword)); // ### fixme
|
|
|
|
|
else
|
|
|
|
|
tokens.append(Token(start, index - start, Token::Identifier));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, false);
|
2010-01-28 13:12:52 +01:00
|
|
|
} else {
|
|
|
|
|
tokens.append(Token(index++, 1, Token::Delimiter));
|
2011-05-20 14:48:05 +02:00
|
|
|
setRegexpMayFollow(&_state, true);
|
2010-01-28 13:12:52 +01:00
|
|
|
}
|
|
|
|
|
} // end of switch
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-28 13:12:52 +01:00
|
|
|
return tokens;
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
int Scanner::state() const
|
2010-01-29 11:20:42 +01:00
|
|
|
{
|
2010-01-29 15:05:22 +01:00
|
|
|
return _state;
|
2010-01-29 11:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-15 12:56:03 +01:00
|
|
|
bool Scanner::isKeyword(const QString &text) const
|
2009-09-28 14:49:39 +02:00
|
|
|
{
|
2010-01-29 11:20:42 +01:00
|
|
|
if (qBinaryFind(begin(js_keywords), end(js_keywords), text) != end(js_keywords))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
2009-09-28 14:49:39 +02:00
|
|
|
}
|
2010-03-26 13:50:51 +01:00
|
|
|
|
|
|
|
|
QStringList Scanner::keywords()
|
|
|
|
|
{
|
|
|
|
|
static QStringList words;
|
|
|
|
|
if (words.isEmpty()) {
|
|
|
|
|
for (const QString *word = begin(js_keywords); word != end(js_keywords); ++word)
|
|
|
|
|
words.append(*word);
|
|
|
|
|
}
|
|
|
|
|
return words;
|
|
|
|
|
}
|