From f91c7f6afc250e237633fad74462ed7acde75c6c Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 10 Feb 2021 15:29:54 +0100 Subject: [PATCH] Extract include directive matcher code into separated methods to improve readability --- .../Parsing/Impl/IncludingStreamProxy.cpp | 55 +++++++++++-------- .../Parsing/Impl/IncludingStreamProxy.h | 5 +- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.cpp b/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.cpp index 49fb7427..b006bb1e 100644 --- a/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.cpp +++ b/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.cpp @@ -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; - auto hasIncludeDirective = false; - - for (; includeDirectivePos < line.m_line.size() - INCLUDE_DIRECTIVE_MINIMUM_TOTAL_LENGTH; includeDirectivePos++) + includeDirectivePosition = 0; + for (; includeDirectivePosition < line.m_line.size() - INCLUDE_DIRECTIVE_MINIMUM_TOTAL_LENGTH; includeDirectivePosition++) { - const auto c = line.m_line[includeDirectivePos]; + const auto c = line.m_line[includeDirectivePosition]; if (isspace(c)) continue; @@ -24,19 +22,20 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const if (c != '#') 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; } - hasIncludeDirective = true; - break; + return true; } - 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; if (line.m_line[currentPos] == '"') isDoubleQuotes = true; @@ -45,9 +44,8 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const else throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR); - const auto filenameStart = ++currentPos; - unsigned filenameEnd = 0; - auto filenameHasEnd = false; + filenameStartPosition = ++currentPos; + filenameEndPosition = 0; for (; currentPos < line.m_line.size(); currentPos++) { @@ -55,24 +53,35 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const if (c == '"') { - if(!isDoubleQuotes) + if (!isDoubleQuotes) throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), ""); - filenameEnd = currentPos; - filenameHasEnd = true; - break; + filenameEndPosition = currentPos; + return true; } if (c == '>') { if (isDoubleQuotes) throw ParsingException(TokenPos(line.m_filename, line.m_line_number, currentPos), INCLUDE_QUOTES_ERROR); - filenameEnd = currentPos; - filenameHasEnd = true; - break; + filenameEndPosition = currentPos; + return true; } } - 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); if(filenameEnd <= filenameStart) diff --git a/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.h b/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.h index 26bdb5a6..9bbba8f8 100644 --- a/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.h +++ b/src/ZoneCodeGeneratorNew/Parsing/Impl/IncludingStreamProxy.h @@ -1,5 +1,6 @@ #pragma once +#include "Utils/ClassUtils.h" #include "Parsing/IParserLineStream.h" class IncludingStreamProxy final : public IParserLineStream @@ -11,7 +12,9 @@ class IncludingStreamProxy final : public IParserLineStream 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: explicit IncludingStreamProxy(IParserLineStream* stream);