Skip to content

Commit

Permalink
Parsing tools: added RefinePreprocessorDirective function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jan 10, 2024
1 parent 1210c85 commit 431f4ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
41 changes: 40 additions & 1 deletion Common/interface/ParsingTools.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1084,6 +1084,45 @@ std::string GetTokenContext(const TokenIterType& Start,
return Ctx.str();
}

/// Extracts the preprocessor directive from the given range
template <typename InteratorType>
std::string RefinePreprocessorDirective(const InteratorType& Start, const InteratorType& End) noexcept
{
// # /* Comment */ define
// ^
// Pos
if (Start == End || *Start != '#')
return "";

const auto DirectiveStart = SkipDelimitersAndComments(Start + 1, End, " \t", SKIP_COMMENT_FLAG_MULTILINE);
// # /* Comment */ define
// ^
// DirectiveStart

const auto DirectiveEnd = SkipIdentifier(DirectiveStart, End);
// # /* Comment */ define
// ^
// Pos

return std::string{DirectiveStart, DirectiveEnd};
}

inline std::string RefinePreprocessorDirective(const std::string& Str) noexcept
{
return RefinePreprocessorDirective(Str.begin(), Str.end());
}

inline std::string RefinePreprocessorDirective(const char* Str, size_t Len = 0) noexcept
{
if (Str == nullptr)
return "";

if (Len == 0)
Len = strlen(Str);

return RefinePreprocessorDirective(Str, Str + Len);
}

} // namespace Parsing

} // namespace Diligent
30 changes: 29 additions & 1 deletion Tests/DiligentCoreTest/src/Common/ParsingToolsTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1341,4 +1341,32 @@ TEST(Common_ParsingTools, GetTokenContext)
}
}


TEST(Common_ParsingTools, RefinePreprocessorDirective)
{
auto TestRefine = [](const std::string& Str, const char* RefStr) {
EXPECT_STREQ(RefinePreprocessorDirective(Str).c_str(), RefStr);
EXPECT_STREQ(RefinePreprocessorDirective(Str.c_str()).c_str(), RefStr);
EXPECT_STREQ(RefinePreprocessorDirective(Str.c_str(), Str.length()).c_str(), RefStr);
};

TestRefine("", "");
TestRefine(" ", "");
TestRefine("#", "");
TestRefine("# ", "");
TestRefine("# \t", "");
TestRefine("# \n", "");
TestRefine("# /*Comment*/", "");

TestRefine("define", "");
TestRefine("#\ndefine", "");
TestRefine("# // define", "");
TestRefine(",define", "");

TestRefine("#define", "define");
TestRefine("# define", "define");
TestRefine("# \t define", "define");
TestRefine("# \t /* Comment*/ define", "define");
}

} // namespace

0 comments on commit 431f4ee

Please sign in to comment.