Skip to content

Commit db89af1

Browse files
committed
Refactor code duplicates
1 parent 8d30918 commit db89af1

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

source/tcppLibrary.hpp

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,25 @@ namespace tcpp
948948
return false;
949949
}
950950

951+
952+
/*!
953+
\return The method eats all whitespace tokens (TT_SPACE, TT_COMMENTARY) and returns current token as a result
954+
*/
955+
956+
template <typename TAction>
957+
static TToken TrySkipWhitespaceTokensSequence(TAction getNextToken, const TToken& initialToken)
958+
{
959+
TToken currToken = initialToken;
960+
961+
while (currToken.mType == E_TOKEN_TYPE::SPACE)
962+
{
963+
currToken = getNextToken();
964+
}
965+
966+
return currToken;
967+
}
968+
969+
951970
std::string Lexer::_requestSourceLine() TCPP_NOEXCEPT
952971
{
953972
IInputStream* pCurrInputStream = _getActiveStream();
@@ -1222,8 +1241,7 @@ namespace tcpp
12221241
processedStr.erase(processedStr.length() - 1);
12231242
}
12241243

1225-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE); // \note skip space tokens
1226-
1244+
currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
12271245
appendString(currToken.mRawView);
12281246
break;
12291247
case E_TOKEN_TYPE::STRINGIZE_OP:
@@ -1288,9 +1306,7 @@ namespace tcpp
12881306

12891307
auto extractValue = [this](TMacroDesc& desc, Lexer& lexer)
12901308
{
1291-
TToken currToken;
1292-
1293-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE); // \note skip space tokens
1309+
TToken currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
12941310

12951311
if (currToken.mType != E_TOKEN_TYPE::NEWLINE)
12961312
{
@@ -1329,6 +1345,7 @@ namespace tcpp
13291345
};
13301346

13311347
currToken = mpLexer->GetNextToken();
1348+
13321349
switch (currToken.mType)
13331350
{
13341351
case E_TOKEN_TYPE::SPACE: // object like macro
@@ -1343,22 +1360,22 @@ namespace tcpp
13431360
// \note parse arguments
13441361
while (true)
13451362
{
1346-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE); // \note skip space tokens
1363+
currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
13471364

13481365
switch (currToken.mType)
13491366
{
1350-
case E_TOKEN_TYPE::IDENTIFIER:
1351-
macroDesc.mArgsNames.push_back(currToken.mRawView);
1352-
break;
1353-
case E_TOKEN_TYPE::ELLIPSIS:
1354-
macroDesc.mArgsNames.push_back("__VA_ARGS__");
1355-
macroDesc.mVariadic = true;
1356-
break;
1357-
default:
1358-
mOnErrorCallback({ E_ERROR_TYPE::UNEXPECTED_TOKEN, mpLexer->GetCurrLineIndex() });
1367+
case E_TOKEN_TYPE::IDENTIFIER:
1368+
macroDesc.mArgsNames.push_back(currToken.mRawView);
1369+
break;
1370+
case E_TOKEN_TYPE::ELLIPSIS:
1371+
macroDesc.mArgsNames.push_back("__VA_ARGS__");
1372+
macroDesc.mVariadic = true;
1373+
break;
1374+
default:
1375+
mOnErrorCallback({ E_ERROR_TYPE::UNEXPECTED_TOKEN, mpLexer->GetCurrLineIndex() });
13591376
}
13601377

1361-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE);
1378+
currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
13621379
if (macroDesc.mVariadic)
13631380
{
13641381
_expect(E_TOKEN_TYPE::CLOSE_BRACKET, currToken.mType);
@@ -1443,13 +1460,11 @@ namespace tcpp
14431460
mContextStack.push_back(macroDesc.mName);
14441461

14451462
// \note function like macro's case
1446-
auto currToken = getNextTokenCallback();
1463+
auto currToken = TrySkipWhitespaceTokensSequence(getNextTokenCallback, getNextTokenCallback());
14471464

1448-
while (currToken.mType == E_TOKEN_TYPE::SPACE) { currToken = getNextTokenCallback(); } // \note skip space tokens
1449-
14501465
if (E_TOKEN_TYPE::OPEN_BRACKET != currToken.mType)
14511466
{
1452-
return { TToken { E_TOKEN_TYPE::BLOB, macroDesc.mName }, currToken }; // \note Function like macro without brackets are is not expanded
1467+
return { TToken { E_TOKEN_TYPE::BLOB, macroDesc.mName }, currToken }; // \note Function like macro without brackets is not expanded
14531468
}
14541469

14551470
_expect(E_TOKEN_TYPE::OPEN_BRACKET, currToken.mType);
@@ -1493,22 +1508,22 @@ namespace tcpp
14931508
++currNestingLevel;
14941509
}
14951510

1496-
while ((currToken = getNextTokenCallback()).mType == E_TOKEN_TYPE::SPACE);
1511+
currToken = TrySkipWhitespaceTokensSequence(getNextTokenCallback, getNextTokenCallback());
14971512

14981513
while ((currToken.mType != E_TOKEN_TYPE::COMMA &&
14991514
currToken.mType != E_TOKEN_TYPE::NEWLINE &&
15001515
currToken.mType != E_TOKEN_TYPE::CLOSE_BRACKET) || currNestingLevel)
15011516
{
15021517
switch (currToken.mType)
15031518
{
1504-
case E_TOKEN_TYPE::OPEN_BRACKET:
1505-
++currNestingLevel;
1506-
break;
1507-
case E_TOKEN_TYPE::CLOSE_BRACKET:
1508-
--currNestingLevel;
1509-
break;
1510-
default:
1511-
break;
1519+
case E_TOKEN_TYPE::OPEN_BRACKET:
1520+
++currNestingLevel;
1521+
break;
1522+
case E_TOKEN_TYPE::CLOSE_BRACKET:
1523+
--currNestingLevel;
1524+
break;
1525+
default:
1526+
break;
15121527
}
15131528

15141529
currArgTokens.push_back(currToken);
@@ -1607,9 +1622,7 @@ namespace tcpp
16071622
return;
16081623
}
16091624

1610-
TToken currToken;
1611-
1612-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE); // \note skip space tokens
1625+
TToken currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
16131626

16141627
if (currToken.mType != E_TOKEN_TYPE::LESS && currToken.mType != E_TOKEN_TYPE::QUOTES)
16151628
{
@@ -1640,7 +1653,7 @@ namespace tcpp
16401653
path.append(currToken.mRawView);
16411654
}
16421655

1643-
while ((currToken = mpLexer->GetNextToken()).mType == E_TOKEN_TYPE::SPACE);
1656+
currToken = TrySkipWhitespaceTokensSequence([this] { return mpLexer->GetNextToken(); }, mpLexer->GetNextToken());
16441657

16451658
if (E_TOKEN_TYPE::NEWLINE != currToken.mType && E_TOKEN_TYPE::END != currToken.mType)
16461659
{

0 commit comments

Comments
 (0)