mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
Extract include directive matcher code into separated methods to improve readability
This commit is contained in:
parent
d876bc5e25
commit
f91c7f6afc
@ -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;
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
@ -57,22 +55,33 @@ bool IncludingStreamProxy::MatchIncludeDirective(const ParserLine& line) const
|
||||
{
|
||||
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)
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user