Introduced QmlJSScanner::scanComments/setScanComments(onoff).

This commit is contained in:
Roberto Raggi
2010-01-29 15:05:22 +01:00
parent fbbc27be32
commit 04161a4ced
2 changed files with 29 additions and 11 deletions

View File

@@ -77,7 +77,8 @@ const _Tp *end(const _Tp (&a)[N])
} }
QmlJSScanner::QmlJSScanner() QmlJSScanner::QmlJSScanner()
: m_state(0) : _state(0),
_scanComments(true)
{ {
} }
@@ -85,6 +86,16 @@ QmlJSScanner::~QmlJSScanner()
{ {
} }
bool QmlJSScanner::scanComments() const
{
return _scanComments;
}
void QmlJSScanner::setScanComments(bool scanComments)
{
_scanComments = scanComments;
}
static bool isIdentifierChar(QChar ch) static bool isIdentifierChar(QChar ch)
{ {
switch (ch.unicode()) { switch (ch.unicode()) {
@@ -116,14 +127,14 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
MultiLineComment = 1 MultiLineComment = 1
}; };
m_state = startState; _state = startState;
QList<Token> tokens; QList<Token> tokens;
// ### handle multi line comment state. // ### handle multi line comment state.
int index = 0; int index = 0;
if (m_state == MultiLineComment) { if (_state == MultiLineComment) {
const int start = index; const int start = index;
while (index < text.length()) { while (index < text.length()) {
const QChar ch = text.at(index); const QChar ch = text.at(index);
@@ -132,7 +143,7 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
la = text.at(index + 1); la = text.at(index + 1);
if (ch == QLatin1Char('*') && la == QLatin1Char('/')) { if (ch == QLatin1Char('*') && la == QLatin1Char('/')) {
m_state = Normal; _state = Normal;
index += 2; index += 2;
break; break;
} else { } else {
@@ -140,6 +151,7 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
} }
} }
if (_scanComments)
tokens.append(Token(start, index - start, Token::Comment)); tokens.append(Token(start, index - start, Token::Comment));
} }
@@ -153,12 +165,13 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
switch (ch.unicode()) { switch (ch.unicode()) {
case '/': case '/':
if (la == QLatin1Char('/')) { if (la == QLatin1Char('/')) {
if (_scanComments)
tokens.append(Token(index, text.length() - index, Token::Comment)); tokens.append(Token(index, text.length() - index, Token::Comment));
index = text.length(); index = text.length();
} else if (la == QLatin1Char('*')) { } else if (la == QLatin1Char('*')) {
const int start = index; const int start = index;
index += 2; index += 2;
m_state = MultiLineComment; _state = MultiLineComment;
while (index < text.length()) { while (index < text.length()) {
const QChar ch = text.at(index); const QChar ch = text.at(index);
QChar la; QChar la;
@@ -166,13 +179,14 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
la = text.at(index + 1); la = text.at(index + 1);
if (ch == QLatin1Char('*') && la == QLatin1Char('/')) { if (ch == QLatin1Char('*') && la == QLatin1Char('/')) {
m_state = Normal; _state = Normal;
index += 2; index += 2;
break; break;
} else { } else {
++index; ++index;
} }
} }
if (_scanComments)
tokens.append(Token(start, index - start, Token::Comment)); tokens.append(Token(start, index - start, Token::Comment));
} else { } else {
tokens.append(Token(index++, 1, Token::Delimiter)); tokens.append(Token(index++, 1, Token::Delimiter));
@@ -285,7 +299,7 @@ QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
int QmlJSScanner::state() const int QmlJSScanner::state() const
{ {
return m_state; return _state;
} }
bool QmlJSScanner::isKeyword(const QString &text) const bool QmlJSScanner::isKeyword(const QString &text) const

View File

@@ -80,13 +80,17 @@ public:
QmlJSScanner(); QmlJSScanner();
virtual ~QmlJSScanner(); virtual ~QmlJSScanner();
bool scanComments() const;
void setScanComments(bool scanComments);
QList<Token> operator()(const QString &text, int startState = 0); QList<Token> operator()(const QString &text, int startState = 0);
int state() const; int state() const;
bool isKeyword(const QString &text) const; bool isKeyword(const QString &text) const;
private: private:
int m_state; int _state;
bool _scanComments: 1;
}; };
} // namespace QmlJS } // namespace QmlJS