Extract include directive matcher code into separated methods to improve readability

This commit is contained in:
Jan 2021-02-10 15:29:54 +01:00
parent d876bc5e25
commit f91c7f6afc
2 changed files with 36 additions and 24 deletions

View File

@ -9,14 +9,12 @@ IncludingStreamProxy::IncludingStreamProxy(IParserLineStream* stream)
{ {
} }
bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const bool IncludingStreamProxy::FindIncludeDirective(const ParserLine& line, unsigned& includeDirectivePosition)
{ {
unsigned includeDirectivePos = 0; includeDirectivePosition = 0;
auto hasIncludeDirective = false; for (; includeDirectivePosition < line.m_line.size() - INCLUDE_DIRECTIVE_MINIMUM_TOTAL_LENGTH; includeDirectivePosition++)
for (; includeDirectivePos < line.m_line.size() - INCLUDE_DIRECTIVE_MINIMUM_TOTAL_LENGTH; includeDirectivePos++)
{ {
const auto c = line.m_line[includeDirectivePos]; const auto c = line.m_line[includeDirectivePosition];
if (isspace(c)) if (isspace(c))
continue; continue;
@ -24,19 +22,20 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const
if (c != '#') if (c != '#')
return false; return false;
if (line.m_line.compare(includeDirectivePos + 1, INCLUDE_DIRECTIVE_LENGTH, INCLUDE_DIRECTIVE) != 0) if (line.m_line.compare(includeDirectivePosition + 1, INCLUDE_DIRECTIVE_LENGTH, INCLUDE_DIRECTIVE) != 0)
{ {
return false; return false;
} }
hasIncludeDirective = true; return true;
break;
} }
if (!hasIncludeDirective) return false;
return false; }
auto currentPos = includeDirectivePos + INCLUDE_DIRECTIVE_LENGTH + 1; bool IncludingStreamProxy::ExtractIncludeFilename(const ParserLine& line, const unsigned includeDirectivePosition, unsigned& filenameStartPosition, unsigned& filenameEndPosition)
{
auto currentPos = includeDirectivePosition;
bool isDoubleQuotes; bool isDoubleQuotes;
if (line.m_line[currentPos] == '"') if (line.m_line[currentPos] == '"')
isDoubleQuotes = true; isDoubleQuotes = true;
@ -45,9 +44,8 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const
else else
throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR); throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR);
const auto filenameStart = ++currentPos; filenameStartPosition = ++currentPos;
unsigned filenameEnd = 0; filenameEndPosition = 0;
auto filenameHasEnd = false;
for (; currentPos < line.m_line.size(); currentPos++) for (; currentPos < line.m_line.size(); currentPos++)
{ {
@ -55,24 +53,35 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const
if (c == '"') if (c == '"')
{ {
if(!isDoubleQuotes) if (!isDoubleQuotes)
throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), ""); throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), "");
filenameEnd = currentPos; filenameEndPosition = currentPos;
filenameHasEnd = true; return true;
break;
} }
if (c == '>') if (c == '>')
{ {
if (isDoubleQuotes) if (isDoubleQuotes)
throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR); throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR);
filenameEnd = currentPos; filenameEndPosition = currentPos;
filenameHasEnd = true; return true;
break;
} }
} }
if(!filenameHasEnd) return false;
}
bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const
{
unsigned includeDirectivePos;
if (!FindIncludeDirective(line, includeDirectivePos))
return false;
const auto currentPos = includeDirectivePos + INCLUDE_DIRECTIVE_LENGTH + 1;
unsigned filenameStart, filenameEnd;
if(!ExtractIncludeFilename(line, currentPos, filenameStart, filenameEnd))
throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR); throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR);
if(filenameEnd <= filenameStart) if(filenameEnd <= filenameStart)

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Utils/ClassUtils.h"
#include "Parsing/IParserLineStream.h" #include "Parsing/IParserLineStream.h"
class IncludingStreamProxy final : public IParserLineStream class IncludingStreamProxy final : public IParserLineStream
@ -11,7 +12,9 @@ class IncludingStreamProxy final : public IParserLineStream
IParserLineStream* m_stream; IParserLineStream* m_stream;
bool MatchIncludeDirective(const ParserLine& line) const; _NODISCARD static bool FindIncludeDirective(const ParserLine& line, unsigned& includeDirectivePosition);
_NODISCARD static bool ExtractIncludeFilename(const ParserLine& line, unsigned includeDirectivePosition, unsigned& filenameStartPosition, unsigned& filenameEndPosition);
_NODISCARD bool MatchIncludeDirective(const ParserLine& line) const;
public: public:
explicit IncludingStreamProxy(IParserLineStream* stream); explicit IncludingStreamProxy(IParserLineStream* stream);