Commit a309b3cf authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

C++: Store token kind as lexer state



... when needed

Change-Id: I32a1649c87e1fa42da80eff5003b2f5714062064
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 25ab1199
......@@ -32,7 +32,7 @@ using namespace CPlusPlus;
Lexer::Lexer(TranslationUnit *unit)
: _translationUnit(unit),
_control(unit->control()),
_state(State_Default),
_state(T_EOF_SYMBOL),
_flags(0),
_currentLine(1)
{
......@@ -44,7 +44,7 @@ Lexer::Lexer(TranslationUnit *unit)
Lexer::Lexer(const char *firstChar, const char *lastChar)
: _translationUnit(0),
_control(0),
_state(State_Default),
_state(T_EOF_SYMBOL),
_flags(0),
_currentLine(1)
{
......@@ -145,7 +145,9 @@ void Lexer::scan_helper(Token *tok)
_tokenStart = _currentChar;
tok->offset = _currentChar - _firstChar;
if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
switch (_state) {
case T_COMMENT:
case T_DOXY_COMMENT: {
const int originalState = _state;
if (! _yychar) {
......@@ -160,7 +162,7 @@ void Lexer::scan_helper(Token *tok)
yyinp();
if (_yychar == '/') {
yyinp();
_state = State_Default;
_state = T_EOF_SYMBOL;
break;
}
}
......@@ -169,12 +171,10 @@ void Lexer::scan_helper(Token *tok)
if (! f._scanCommentTokens)
goto _Lagain;
else if (originalState == State_MultiLineComment)
tok->f.kind = T_COMMENT;
else
tok->f.kind = T_DOXY_COMMENT;
tok->f.kind = originalState;
return; // done
}
}
if (! _yychar) {
tok->f.kind = T_EOF_SYMBOL;
......@@ -374,7 +374,7 @@ void Lexer::scan_helper(Token *tok)
} else if (_yychar == '*') {
yyinp();
bool doxy = false;
Kind commentKind = T_COMMENT;
if (_yychar == '*' || _yychar == '!') {
const char ch = _yychar;
......@@ -388,7 +388,7 @@ void Lexer::scan_helper(Token *tok)
yyinp();
if (! _yychar || std::isspace(_yychar))
doxy = true;
commentKind = T_DOXY_COMMENT;
}
while (_yychar) {
......@@ -405,12 +405,12 @@ void Lexer::scan_helper(Token *tok)
if (_yychar)
yyinp();
else
_state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment;
_state = commentKind;
if (! f._scanCommentTokens)
goto _Lagain;
tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
tok->f.kind = commentKind;
} else if (_yychar == '=') {
yyinp();
......
......@@ -32,12 +32,6 @@ class CPLUSPLUS_EXPORT Lexer
void operator =(const Lexer &other);
public:
enum State {
State_Default,
State_MultiLineComment,
State_MultiLineDoxyComment
};
Lexer(TranslationUnit *unit);
Lexer(const char *firstChar, const char *lastChar);
~Lexer();
......
......@@ -66,7 +66,7 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
void CppHighlighter::highlightBlock(const QString &text)
{
const int previousState = previousBlockState();
int state = 0, initialBraceDepth = 0;
int state = T_EOF_SYMBOL, initialBraceDepth = 0;
if (previousState != -1) {
state = previousState & 0xff;
initialBraceDepth = previousState >> 8;
......@@ -96,9 +96,9 @@ void CppHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState);
BaseTextDocumentLayout::clearParentheses(currentBlock());
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));
else if (initialState == Lexer::State_MultiLineDoxyComment)
else if (initialState == T_DOXY_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
else
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
......
......@@ -504,7 +504,7 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
leave();
continue;
} else if (m_tokenIndex == m_tokens.size() - 1
&& lexerState == Lexer::State_Default) {
&& lexerState == T_EOF_SYMBOL) {
leave();
} else if (m_tokenIndex == 0 && m_currentToken.isComment()) {
// to allow enter/leave to update the indentDepth
......@@ -571,8 +571,8 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block)
if (topState != multiline_comment_start
&& topState != multiline_comment_cont
&& (lexerState == Lexer::State_MultiLineComment
|| lexerState == Lexer::State_MultiLineDoxyComment)) {
&& (lexerState == T_COMMENT
|| lexerState == T_DOXY_COMMENT)) {
enter(multiline_comment_start);
}
......@@ -1612,7 +1612,7 @@ void QtStyleCodeFormatter::adjustIndent(const QList<CPlusPlus::Token> &tokens, i
if ((topState.type == multiline_comment_cont
|| topState.type == multiline_comment_start)
&& (kind == T_COMMENT || kind == T_DOXY_COMMENT)
&& (lexerState == Lexer::State_Default
&& (lexerState == T_EOF_SYMBOL
|| tokens.size() != 1)) {
if (*indentDepth >= m_tabSettings.m_indentSize)
*indentDepth -= m_tabSettings.m_indentSize;
......
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