C++: Store token kind as lexer state

... when needed

Change-Id: I32a1649c87e1fa42da80eff5003b2f5714062064
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
Orgad Shaneh
2014-01-18 19:51:57 +02:00
committed by Orgad Shaneh
parent 25ab1199cd
commit a309b3cfe6
4 changed files with 19 additions and 25 deletions

View File

@@ -32,7 +32,7 @@ using namespace CPlusPlus;
Lexer::Lexer(TranslationUnit *unit) Lexer::Lexer(TranslationUnit *unit)
: _translationUnit(unit), : _translationUnit(unit),
_control(unit->control()), _control(unit->control()),
_state(State_Default), _state(T_EOF_SYMBOL),
_flags(0), _flags(0),
_currentLine(1) _currentLine(1)
{ {
@@ -44,7 +44,7 @@ Lexer::Lexer(TranslationUnit *unit)
Lexer::Lexer(const char *firstChar, const char *lastChar) Lexer::Lexer(const char *firstChar, const char *lastChar)
: _translationUnit(0), : _translationUnit(0),
_control(0), _control(0),
_state(State_Default), _state(T_EOF_SYMBOL),
_flags(0), _flags(0),
_currentLine(1) _currentLine(1)
{ {
@@ -145,7 +145,9 @@ void Lexer::scan_helper(Token *tok)
_tokenStart = _currentChar; _tokenStart = _currentChar;
tok->offset = _currentChar - _firstChar; tok->offset = _currentChar - _firstChar;
if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) { switch (_state) {
case T_COMMENT:
case T_DOXY_COMMENT: {
const int originalState = _state; const int originalState = _state;
if (! _yychar) { if (! _yychar) {
@@ -160,7 +162,7 @@ void Lexer::scan_helper(Token *tok)
yyinp(); yyinp();
if (_yychar == '/') { if (_yychar == '/') {
yyinp(); yyinp();
_state = State_Default; _state = T_EOF_SYMBOL;
break; break;
} }
} }
@@ -169,12 +171,10 @@ void Lexer::scan_helper(Token *tok)
if (! f._scanCommentTokens) if (! f._scanCommentTokens)
goto _Lagain; goto _Lagain;
else if (originalState == State_MultiLineComment) tok->f.kind = originalState;
tok->f.kind = T_COMMENT;
else
tok->f.kind = T_DOXY_COMMENT;
return; // done return; // done
} }
}
if (! _yychar) { if (! _yychar) {
tok->f.kind = T_EOF_SYMBOL; tok->f.kind = T_EOF_SYMBOL;
@@ -374,7 +374,7 @@ void Lexer::scan_helper(Token *tok)
} else if (_yychar == '*') { } else if (_yychar == '*') {
yyinp(); yyinp();
bool doxy = false; Kind commentKind = T_COMMENT;
if (_yychar == '*' || _yychar == '!') { if (_yychar == '*' || _yychar == '!') {
const char ch = _yychar; const char ch = _yychar;
@@ -388,7 +388,7 @@ void Lexer::scan_helper(Token *tok)
yyinp(); yyinp();
if (! _yychar || std::isspace(_yychar)) if (! _yychar || std::isspace(_yychar))
doxy = true; commentKind = T_DOXY_COMMENT;
} }
while (_yychar) { while (_yychar) {
@@ -405,12 +405,12 @@ void Lexer::scan_helper(Token *tok)
if (_yychar) if (_yychar)
yyinp(); yyinp();
else else
_state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment; _state = commentKind;
if (! f._scanCommentTokens) if (! f._scanCommentTokens)
goto _Lagain; goto _Lagain;
tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT; tok->f.kind = commentKind;
} else if (_yychar == '=') { } else if (_yychar == '=') {
yyinp(); yyinp();

View File

@@ -32,12 +32,6 @@ class CPLUSPLUS_EXPORT Lexer
void operator =(const Lexer &other); void operator =(const Lexer &other);
public: public:
enum State {
State_Default,
State_MultiLineComment,
State_MultiLineDoxyComment
};
Lexer(TranslationUnit *unit); Lexer(TranslationUnit *unit);
Lexer(const char *firstChar, const char *lastChar); Lexer(const char *firstChar, const char *lastChar);
~Lexer(); ~Lexer();

View File

@@ -66,7 +66,7 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
void CppHighlighter::highlightBlock(const QString &text) void CppHighlighter::highlightBlock(const QString &text)
{ {
const int previousState = previousBlockState(); const int previousState = previousBlockState();
int state = 0, initialBraceDepth = 0; int state = T_EOF_SYMBOL, initialBraceDepth = 0;
if (previousState != -1) { if (previousState != -1) {
state = previousState & 0xff; state = previousState & 0xff;
initialBraceDepth = previousState >> 8; initialBraceDepth = previousState >> 8;
@@ -96,9 +96,9 @@ void CppHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState); setCurrentBlockState(previousState);
BaseTextDocumentLayout::clearParentheses(currentBlock()); BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace if (text.length()) {// the empty line can still contain whitespace
if (initialState == Lexer::State_MultiLineComment) if (initialState == T_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat)); highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
else if (initialState == Lexer::State_MultiLineDoxyComment) else if (initialState == T_DOXY_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat)); highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
else else
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace)); setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));

View File

@@ -504,7 +504,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
leave(); leave();
continue; continue;
} else if (m_tokenIndex == m_tokens.size() - 1 } else if (m_tokenIndex == m_tokens.size() - 1
&& lexerState == Lexer::State_Default) { && lexerState == T_EOF_SYMBOL) {
leave(); leave();
} else if (m_tokenIndex == 0 && m_currentToken.isComment()) { } else if (m_tokenIndex == 0 && m_currentToken.isComment()) {
// to allow enter/leave to update the indentDepth // to allow enter/leave to update the indentDepth
@@ -571,8 +571,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
if (topState != multiline_comment_start if (topState != multiline_comment_start
&& topState != multiline_comment_cont && topState != multiline_comment_cont
&& (lexerState == Lexer::State_MultiLineComment && (lexerState == T_COMMENT
|| lexerState == Lexer::State_MultiLineDoxyComment)) { || lexerState == T_DOXY_COMMENT)) {
enter(multiline_comment_start); enter(multiline_comment_start);
} }
@@ -1612,7 +1612,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
if ((topState.type == multiline_comment_cont if ((topState.type == multiline_comment_cont
|| topState.type == multiline_comment_start) || topState.type == multiline_comment_start)
&& (kind == T_COMMENT || kind == T_DOXY_COMMENT) && (kind == T_COMMENT || kind == T_DOXY_COMMENT)
&& (lexerState == Lexer::State_Default && (lexerState == T_EOF_SYMBOL
|| tokens.size() != 1)) { || tokens.size() != 1)) {
if (*indentDepth >= m_tabSettings.m_indentSize) if (*indentDepth >= m_tabSettings.m_indentSize)
*indentDepth -= m_tabSettings.m_indentSize; *indentDepth -= m_tabSettings.m_indentSize;