Commit 60ad6c47 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Moved QmlJSScanner::Token in the QmlJS namespace.

parent 1bc838fd
......@@ -62,60 +62,60 @@ void QScriptHighlighter::highlightBlock(const QString &text)
QTextCharFormat emptyFormat;
int lastEnd = 0;
const QList<QmlJSScanner::Token> tokens = m_scanner.tokens();
const QList<Token> tokens = m_scanner.tokens();
for (int i = 0; i < tokens.size(); ++i) {
const QmlJSScanner::Token token = tokens.at(i);
const Token token = tokens.at(i);
if (token.offset != lastEnd)
setFormat(lastEnd, token.offset - lastEnd, m_formats[VisualWhitespace]);
switch (token.kind) {
case QmlJSScanner::Token::Keyword:
case Token::Keyword:
setFormat(token.offset, token.length, m_formats[KeywordFormat]);
break;
case QmlJSScanner::Token::String:
case Token::String:
highlightWhitespace(token, text, StringFormat);
break;
case QmlJSScanner::Token::Comment:
case Token::Comment:
highlightWhitespace(token, text, CommentFormat);
break;
case QmlJSScanner::Token::Number:
case Token::Number:
highlightWhitespace(token, text, NumberFormat);
break;
case QmlJSScanner::Token::LeftParenthesis:
case Token::LeftParenthesis:
onOpeningParenthesis('(', token.offset);
break;
case QmlJSScanner::Token::RightParenthesis:
case Token::RightParenthesis:
onClosingParenthesis(')', token.offset);
break;
case QmlJSScanner::Token::LeftBrace:
case Token::LeftBrace:
onOpeningParenthesis('{', token.offset);
break;
case QmlJSScanner::Token::RightBrace:
case Token::RightBrace:
onClosingParenthesis('}', token.offset);
break;
case QmlJSScanner::Token::LeftBracket:
case Token::LeftBracket:
onOpeningParenthesis('[', token.offset);
break;
case QmlJSScanner::Token::RightBracket:
case Token::RightBracket:
onClosingParenthesis(']', token.offset);
break;
case QmlJSScanner::Token::Identifier:
if (m_duiEnabled && (i + 1 != tokens.size()) && tokens.at(i + 1).kind == QmlJSScanner::Token::Colon) {
case Token::Identifier:
if (m_duiEnabled && (i + 1 != tokens.size()) && tokens.at(i + 1).kind == Token::Colon) {
int j = i;
for (; j != -1; --j) {
const QmlJSScanner::Token &tok = tokens.at(j);
if (tok.is(QmlJSScanner::Token::Dot) || tok.is(QmlJSScanner::Token::Identifier)) {
const Token &tok = tokens.at(j);
if (tok.is(Token::Dot) || tok.is(Token::Identifier)) {
setFormat(tok.offset, tok.length, m_formats[LabelFormat]);
} else {
break;
......@@ -131,15 +131,15 @@ void QScriptHighlighter::highlightBlock(const QString &text)
}
break;
case QmlJSScanner::Token::Colon:
if (m_duiEnabled && i > 0 && tokens.at(i - 1).kind == QmlJSScanner::Token::Identifier)
case Token::Colon:
if (m_duiEnabled && i > 0 && tokens.at(i - 1).kind == Token::Identifier)
setFormat(token.offset, token.length, m_formats[LabelFormat]);
else
setFormat(token.offset, token.length, emptyFormat);
break;
case QmlJSScanner::Token::Operator:
case QmlJSScanner::Token::Dot:
case Token::Operator:
case Token::Dot:
setFormat(token.offset, token.length, emptyFormat);
break;
......@@ -247,7 +247,7 @@ void QScriptHighlighter::onOpeningParenthesis(QChar, int) {}
void QScriptHighlighter::onClosingParenthesis(QChar, int) {}
void QScriptHighlighter::onBlockEnd(int state, int) { return setCurrentBlockState(state); }
void QScriptHighlighter::highlightWhitespace(const QmlJSScanner::Token &token, const QString &text, int nonWhitespaceFormat)
void QScriptHighlighter::highlightWhitespace(const Token &token, const QString &text, int nonWhitespaceFormat)
{
const QTextCharFormat normalFormat = m_formats[nonWhitespaceFormat];
const QTextCharFormat visualSpaceFormat = m_formats[VisualWhitespace];
......
......@@ -69,7 +69,7 @@ protected:
// sets the enriched user state, or simply calls setCurrentBlockState(state);
virtual void onBlockEnd(int state, int firstNonSpace);
virtual void highlightWhitespace(const QmlJSScanner::Token &token, const QString &text, int nonWhitespaceFormat);
virtual void highlightWhitespace(const Token &token, const QString &text, int nonWhitespaceFormat);
protected:
QmlJSScanner m_scanner;
......
......@@ -215,14 +215,14 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
yyLinizerState.tokens = scanner(t, startState);
QString trimmed;
int previousTokenEnd = 0;
foreach (const QmlJSScanner::Token &token, yyLinizerState.tokens) {
foreach (const Token &token, yyLinizerState.tokens) {
trimmed.append(t.midRef(previousTokenEnd, token.begin() - previousTokenEnd));
if (token.is(QmlJSScanner::Token::String)) {
if (token.is(Token::String)) {
for (int i = 0; i < token.length; ++i)
trimmed.append(QLatin1Char('X'));
} else if (token.is(QmlJSScanner::Token::Comment)) {
} else if (token.is(Token::Comment)) {
for (int i = 0; i < token.length; ++i)
trimmed.append(QLatin1Char(' '));
......@@ -235,43 +235,43 @@ QString QmlJSIndenter::trimmedCodeLine(const QString &t)
int index = yyLinizerState.tokens.size() - 1;
for (; index != -1; --index) {
const QmlJSScanner::Token &token = yyLinizerState.tokens.at(index);
if (token.isNot(QmlJSScanner::Token::Comment))
const Token &token = yyLinizerState.tokens.at(index);
if (token.isNot(Token::Comment))
break;
}
bool isBinding = false;
foreach (const QmlJSScanner::Token &token, yyLinizerState.tokens) {
if (token.is(QmlJSScanner::Token::Colon)) {
foreach (const Token &token, yyLinizerState.tokens) {
if (token.is(Token::Colon)) {
isBinding = true;
break;
}
}
if (index != -1) {
const QmlJSScanner::Token &last = yyLinizerState.tokens.at(index);
const Token &last = yyLinizerState.tokens.at(index);
switch (last.kind) {
case QmlJSScanner::Token::LeftParenthesis:
case QmlJSScanner::Token::LeftBrace:
case QmlJSScanner::Token::Semicolon:
case QmlJSScanner::Token::Operator:
case Token::LeftParenthesis:
case Token::LeftBrace:
case Token::Semicolon:
case Token::Operator:
break;
case QmlJSScanner::Token::RightParenthesis:
case QmlJSScanner::Token::RightBrace:
case Token::RightParenthesis:
case Token::RightBrace:
if (isBinding)
trimmed.append(QLatin1Char(';'));
break;
case QmlJSScanner::Token::Colon:
case QmlJSScanner::Token::LeftBracket:
case QmlJSScanner::Token::RightBracket:
case Token::Colon:
case Token::LeftBracket:
case Token::RightBracket:
trimmed.append(QLatin1Char(';'));
break;
case QmlJSScanner::Token::Identifier:
case QmlJSScanner::Token::Keyword:
case Token::Identifier:
case Token::Keyword:
if (t.midRef(last.offset, last.length) != QLatin1String("else"))
trimmed.append(QLatin1Char(';'));
break;
......@@ -309,16 +309,16 @@ bool QmlJSIndenter::okay(QChar typedIn, QChar okayCh) const
return typedIn == QChar() || typedIn == okayCh;
}
QmlJSScanner::Token QmlJSIndenter::lastToken() const
Token QmlJSIndenter::lastToken() const
{
for (int index = yyLinizerState.tokens.size() - 1; index != -1; --index) {
const QmlJSScanner::Token &token = yyLinizerState.tokens.at(index);
const Token &token = yyLinizerState.tokens.at(index);
if (token.isNot(QmlJSScanner::Token::Comment))
if (token.isNot(Token::Comment))
return token;
}
return QmlJSScanner::Token();
return Token();
}
/*
......@@ -485,52 +485,52 @@ bool QmlJSIndenter::matchBracelessControlStatement()
int delimDepth = 0;
if (! yyLinizerState.tokens.isEmpty()) {
QmlJSScanner::Token tk = lastToken();
Token tk = lastToken();
if (tk.is(QmlJSScanner::Token::Identifier) &&
if (tk.is(Token::Identifier) &&
yyLinizerState.line.midRef(tk.offset, tk.length) == QLatin1String("else"))
return true;
else if (tk.isNot(QmlJSScanner::Token::RightParenthesis))
else if (tk.isNot(Token::RightParenthesis))
return false;
}
for (int i = 0; i < SmallRoof; i++) {
for (int tokenIndex = yyLinizerState.tokens.size() - 1; tokenIndex != -1; --tokenIndex) {
const QmlJSScanner::Token &token = yyLinizerState.tokens.at(tokenIndex);
const Token &token = yyLinizerState.tokens.at(tokenIndex);
switch (token.kind) {
default:
break;
case QmlJSScanner::Token::Comment:
case Token::Comment:
// skip comments
break;
case QmlJSScanner::Token::RightParenthesis:
case Token::RightParenthesis:
++delimDepth;
break;
case QmlJSScanner::Token::LeftBrace:
case QmlJSScanner::Token::RightBrace:
case QmlJSScanner::Token::Semicolon:
case Token::LeftBrace:
case Token::RightBrace:
case Token::Semicolon:
/*
We met a statement separator, but not where we
expected it. What follows is probably a weird
continuation line. Be careful with ';' in for,
though.
*/
if (token.kind != QmlJSScanner::Token::Semicolon || delimDepth == 0)
if (token.kind != Token::Semicolon || delimDepth == 0)
return false;
case QmlJSScanner::Token::LeftParenthesis:
case Token::LeftParenthesis:
--delimDepth;
if (delimDepth == 0 && tokenIndex > 0) {
const QmlJSScanner::Token &tk = yyLinizerState.tokens.at(tokenIndex - 1);
const Token &tk = yyLinizerState.tokens.at(tokenIndex - 1);
if (tk.is(QmlJSScanner::Token::Identifier)) {
if (tk.is(Token::Identifier)) {
const QStringRef tokenText = yyLinizerState.line.midRef(tk.offset, tk.length);
/*
......
......@@ -84,7 +84,7 @@ private:
int indentForContinuationLine();
int indentForStandaloneLine();
QmlJSScanner::Token lastToken() const;
Token lastToken() const;
private:
int ppHardwareTabSize;
......@@ -105,7 +105,7 @@ private:
bool leftBraceFollows;
bool pendingRightBrace;
QString line;
QList<QmlJSScanner::Token> tokens;
QList<Token> tokens;
QTextBlock iter;
};
......
......@@ -48,7 +48,7 @@ void QmlJSScanner::reset()
m_tokens.clear();
}
QList<QmlJSScanner::Token> QmlJSScanner::operator()(const QString &text, int startState)
QList<Token> QmlJSScanner::operator()(const QString &text, int startState)
{
reset();
......
......@@ -38,52 +38,54 @@
namespace QmlJS {
class QMLJS_EXPORT QmlJSScanner
class QMLJS_EXPORT Token
{
public:
struct Token {
enum Kind {
EndOfFile,
Keyword,
Identifier,
String,
Comment,
Number,
LeftParenthesis,
RightParenthesis,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Operator,
Semicolon,
Colon,
Comma,
Dot
};
int offset;
int length;
Kind kind;
inline Token(): offset(0), length(0), kind(EndOfFile) {}
inline Token(int o, int l, Kind k): offset(o), length(l), kind(k) {}
inline int begin() const { return offset; }
inline int end() const { return offset + length; }
inline bool is(int k) const { return k == kind; }
inline bool isNot(int k) const { return k != kind; }
enum Kind {
EndOfFile,
Keyword,
Identifier,
String,
Comment,
Number,
LeftParenthesis,
RightParenthesis,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Operator,
Semicolon,
Colon,
Comma,
Dot
};
inline Token(): offset(0), length(0), kind(EndOfFile) {}
inline Token(int o, int l, Kind k): offset(o), length(l), kind(k) {}
inline int begin() const { return offset; }
inline int end() const { return offset + length; }
inline bool is(int k) const { return k == kind; }
inline bool isNot(int k) const { return k != kind; }
public:
int offset;
int length;
Kind kind;
};
class QMLJS_EXPORT QmlJSScanner
{
public:
QmlJSScanner();
virtual ~QmlJSScanner();
void setKeywords(const QSet<QString> keywords)
{ m_keywords = keywords;; }
{ m_keywords = keywords; }
void reset();
QList<QmlJSScanner::Token> operator()(const QString &text, int startState = 0);
QList<Token> operator()(const QString &text, int startState = 0);
int endState() const
{ return m_endState; }
......@@ -91,7 +93,7 @@ public:
int firstNonSpace() const
{ return m_firstNonSpace; }
QList<QmlJSScanner::Token> tokens() const
QList<Token> tokens() const
{ return m_tokens; }
private:
......@@ -113,7 +115,7 @@ private:
QSet<QString> m_keywords;
int m_endState;
int m_firstNonSpace;
QList<QmlJSScanner::Token> m_tokens;
QList<Token> m_tokens;
};
} // namespace QmlJS
......
......@@ -678,13 +678,13 @@ bool QmlJSTextEditor::isElectricCharacter(const QChar &ch) const
return false;
}
bool QmlJSTextEditor::isClosingBrace(const QList<QmlJSScanner::Token> &tokens) const
bool QmlJSTextEditor::isClosingBrace(const QList<Token> &tokens) const
{
if (tokens.size() == 1) {
const QmlJSScanner::Token firstToken = tokens.first();
const Token firstToken = tokens.first();
return firstToken.is(QmlJSScanner::Token::RightBrace) || firstToken.is(QmlJSScanner::Token::RightBracket);
return firstToken.is(Token::RightBrace) || firstToken.is(Token::RightBracket);
}
return false;
......@@ -856,31 +856,31 @@ bool QmlJSTextEditor::contextAllowsAutoParentheses(const QTextCursor &cursor, co
const int blockState = blockStartState(cursor.block());
QmlJSScanner tokenize;
const QList<QmlJSScanner::Token> tokens = tokenize(blockText, blockState);
const QList<Token> tokens = tokenize(blockText, blockState);
const int pos = cursor.columnNumber();
int tokenIndex = 0;
for (; tokenIndex < tokens.size(); ++tokenIndex) {
const QmlJSScanner::Token &token = tokens.at(tokenIndex);
const Token &token = tokens.at(tokenIndex);
if (pos >= token.begin()) {
if (pos < token.end())
break;
else if (pos == token.end() && (token.is(QmlJSScanner::Token::Comment) ||
token.is(QmlJSScanner::Token::String)))
else if (pos == token.end() && (token.is(Token::Comment) ||
token.is(Token::String)))
break;
}
}
if (tokenIndex != tokens.size()) {
const QmlJSScanner::Token &token = tokens.at(tokenIndex);
const Token &token = tokens.at(tokenIndex);
switch (token.kind) {
case QmlJSScanner::Token::Comment:
case Token::Comment:
return false;
case QmlJSScanner::Token::String: {
case Token::String: {
const QStringRef tokenText = blockText.midRef(token.offset, token.length);
const QChar quote = tokenText.at(0);
......
......@@ -145,7 +145,7 @@ protected:
private:
virtual bool isElectricCharacter(const QChar &ch) const;
virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
bool isClosingBrace(const QList<QmlJS::QmlJSScanner::Token> &tokens) const;
bool isClosingBrace(const QList<QmlJS::Token> &tokens) const;
QString wordUnderCursor() const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment