mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 08:05:45 +00:00
Make DefinesProxy go over a line with defines substitution multiple times until no further substitution has been done
This commit is contained in:
parent
bba55706bf
commit
9816d01ac2
@ -26,7 +26,7 @@ DefinesStreamProxy::Define::Define(std::string name, std::string value)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string DefinesStreamProxy::Define::Render(std::vector<std::string>& parameterValues)
|
std::string DefinesStreamProxy::Define::Render(const std::vector<std::string>& parameterValues)
|
||||||
{
|
{
|
||||||
if (parameterValues.empty() || m_parameter_positions.empty())
|
if (parameterValues.empty() || m_parameter_positions.empty())
|
||||||
return m_value;
|
return m_value;
|
||||||
@ -52,7 +52,7 @@ std::string DefinesStreamProxy::Define::Render(std::vector<std::string>& paramet
|
|||||||
return str.str();
|
return str.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefinesStreamProxy::Define::IdentifyParameters(std::vector<std::string>& parameterNames)
|
void DefinesStreamProxy::Define::IdentifyParameters(const std::vector<std::string>& parameterNames)
|
||||||
{
|
{
|
||||||
if (parameterNames.empty())
|
if (parameterNames.empty())
|
||||||
return;
|
return;
|
||||||
@ -168,7 +168,7 @@ bool DefinesStreamProxy::MatchDefineDirective(const ParserLine& line, unsigned d
|
|||||||
|
|
||||||
const auto name = line.m_line.substr(nameStartPos, directivePosition - nameStartPos);
|
const auto name = line.m_line.substr(nameStartPos, directivePosition - nameStartPos);
|
||||||
|
|
||||||
auto parameters = MatchDefineParameters(line, directivePosition);
|
const auto parameters = MatchDefineParameters(line, directivePosition);
|
||||||
|
|
||||||
std::string value;
|
std::string value;
|
||||||
if (directivePosition < line.m_line.size())
|
if (directivePosition < line.m_line.size())
|
||||||
@ -346,13 +346,21 @@ void DefinesStreamProxy::ExtractParametersFromDefineUsage(const ParserLine& line
|
|||||||
|
|
||||||
void DefinesStreamProxy::ExpandDefines(ParserLine& line)
|
void DefinesStreamProxy::ExpandDefines(ParserLine& line)
|
||||||
{
|
{
|
||||||
|
bool usesDefines;
|
||||||
|
auto defineIterations = 0u;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (defineIterations > MAX_DEFINE_ITERATIONS)
|
||||||
|
throw ParsingException(CreatePos(line, 1), "Potential define loop? Exceeded max define iterations of " + std::to_string(MAX_DEFINE_ITERATIONS) + " iterations.");
|
||||||
|
|
||||||
|
usesDefines = false;
|
||||||
|
|
||||||
auto wordStart = 0u;
|
auto wordStart = 0u;
|
||||||
auto lastWordEnd = 0u;
|
auto lastWordEnd = 0u;
|
||||||
auto inWord = false;
|
auto inWord = false;
|
||||||
Define* value;
|
Define* value;
|
||||||
|
|
||||||
std::ostringstream str;
|
std::ostringstream str;
|
||||||
auto usesDefines = false;
|
|
||||||
|
|
||||||
for (auto i = 0u; i < line.m_line.size(); i++)
|
for (auto i = 0u; i < line.m_line.size(); i++)
|
||||||
{
|
{
|
||||||
@ -395,7 +403,7 @@ void DefinesStreamProxy::ExpandDefines(ParserLine& line)
|
|||||||
{
|
{
|
||||||
if (FindDefineForWord(line, wordStart, line.m_line.size(), value))
|
if (FindDefineForWord(line, wordStart, line.m_line.size(), value))
|
||||||
{
|
{
|
||||||
std::vector<std::string> parameterValues;
|
const std::vector<std::string> parameterValues;
|
||||||
const auto defineValue = value->Render(parameterValues);
|
const auto defineValue = value->Render(parameterValues);
|
||||||
|
|
||||||
if (!usesDefines)
|
if (!usesDefines)
|
||||||
@ -417,6 +425,9 @@ void DefinesStreamProxy::ExpandDefines(ParserLine& line)
|
|||||||
str << std::string(line.m_line, lastWordEnd, line.m_line.size() - lastWordEnd);
|
str << std::string(line.m_line, lastWordEnd, line.m_line.size() - lastWordEnd);
|
||||||
line.m_line = str.str();
|
line.m_line = str.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineIterations++;
|
||||||
|
} while (usesDefines);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefinesStreamProxy::AddDefine(Define define)
|
void DefinesStreamProxy::AddDefine(Define define)
|
||||||
|
@ -15,6 +15,8 @@ class DefinesStreamProxy final : public AbstractDirectiveStreamProxy
|
|||||||
static constexpr const char* ELSE_DIRECTIVE = "else";
|
static constexpr const char* ELSE_DIRECTIVE = "else";
|
||||||
static constexpr const char* ENDIF_DIRECTIVE = "endif";
|
static constexpr const char* ENDIF_DIRECTIVE = "endif";
|
||||||
|
|
||||||
|
static constexpr auto MAX_DEFINE_ITERATIONS = 128u;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
class DefineParameterPosition
|
class DefineParameterPosition
|
||||||
{
|
{
|
||||||
@ -35,8 +37,8 @@ public:
|
|||||||
|
|
||||||
Define();
|
Define();
|
||||||
Define(std::string name, std::string value);
|
Define(std::string name, std::string value);
|
||||||
void IdentifyParameters(std::vector<std::string>& parameterNames);
|
void IdentifyParameters(const std::vector<std::string>& parameterNames);
|
||||||
std::string Render(std::vector<std::string>& parameterValues);
|
std::string Render(const std::vector<std::string>& parameterValues);
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -45,7 +47,7 @@ private:
|
|||||||
std::stack<bool> m_modes;
|
std::stack<bool> m_modes;
|
||||||
unsigned m_ignore_depth;
|
unsigned m_ignore_depth;
|
||||||
|
|
||||||
std::vector<std::string> MatchDefineParameters(const ParserLine& line, unsigned& parameterPosition);
|
static std::vector<std::string> MatchDefineParameters(const ParserLine& line, unsigned& parameterPosition);
|
||||||
_NODISCARD bool MatchDefineDirective(const ParserLine& line, unsigned directivePosition);
|
_NODISCARD bool MatchDefineDirective(const ParserLine& line, unsigned directivePosition);
|
||||||
_NODISCARD bool MatchUndefDirective(const ParserLine& line, unsigned directivePosition);
|
_NODISCARD bool MatchUndefDirective(const ParserLine& line, unsigned directivePosition);
|
||||||
_NODISCARD bool MatchIfdefDirective(const ParserLine& line, unsigned directivePosition);
|
_NODISCARD bool MatchIfdefDirective(const ParserLine& line, unsigned directivePosition);
|
||||||
@ -53,7 +55,7 @@ private:
|
|||||||
_NODISCARD bool MatchEndifDirective(const ParserLine& line, unsigned directivePosition);
|
_NODISCARD bool MatchEndifDirective(const ParserLine& line, unsigned directivePosition);
|
||||||
_NODISCARD bool MatchDirectives(const ParserLine& line);
|
_NODISCARD bool MatchDirectives(const ParserLine& line);
|
||||||
|
|
||||||
void ExtractParametersFromDefineUsage(const ParserLine& line, unsigned parameterStart, unsigned& parameterEnd, std::vector<std::string>& parameterValues);
|
static void ExtractParametersFromDefineUsage(const ParserLine& line, unsigned parameterStart, unsigned& parameterEnd, std::vector<std::string>& parameterValues);
|
||||||
bool FindDefineForWord(const ParserLine& line, unsigned wordStart, unsigned wordEnd, Define*& value);
|
bool FindDefineForWord(const ParserLine& line, unsigned wordStart, unsigned wordEnd, Define*& value);
|
||||||
void ExpandDefines(ParserLine& line);
|
void ExpandDefines(ParserLine& line);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user