Skip to content

Commit 431f4ee

Browse files
Parsing tools: added RefinePreprocessorDirective function
1 parent 1210c85 commit 431f4ee

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

Common/interface/ParsingTools.hpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1084,6 +1084,45 @@ std::string GetTokenContext(const TokenIterType& Start,
10841084
return Ctx.str();
10851085
}
10861086

1087+
/// Extracts the preprocessor directive from the given range
1088+
template <typename InteratorType>
1089+
std::string RefinePreprocessorDirective(const InteratorType& Start, const InteratorType& End) noexcept
1090+
{
1091+
// # /* Comment */ define
1092+
// ^
1093+
// Pos
1094+
if (Start == End || *Start != '#')
1095+
return "";
1096+
1097+
const auto DirectiveStart = SkipDelimitersAndComments(Start + 1, End, " \t", SKIP_COMMENT_FLAG_MULTILINE);
1098+
// # /* Comment */ define
1099+
// ^
1100+
// DirectiveStart
1101+
1102+
const auto DirectiveEnd = SkipIdentifier(DirectiveStart, End);
1103+
// # /* Comment */ define
1104+
// ^
1105+
// Pos
1106+
1107+
return std::string{DirectiveStart, DirectiveEnd};
1108+
}
1109+
1110+
inline std::string RefinePreprocessorDirective(const std::string& Str) noexcept
1111+
{
1112+
return RefinePreprocessorDirective(Str.begin(), Str.end());
1113+
}
1114+
1115+
inline std::string RefinePreprocessorDirective(const char* Str, size_t Len = 0) noexcept
1116+
{
1117+
if (Str == nullptr)
1118+
return "";
1119+
1120+
if (Len == 0)
1121+
Len = strlen(Str);
1122+
1123+
return RefinePreprocessorDirective(Str, Str + Len);
1124+
}
1125+
10871126
} // namespace Parsing
10881127

10891128
} // namespace Diligent

Tests/DiligentCoreTest/src/Common/ParsingToolsTest.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1341,4 +1341,32 @@ TEST(Common_ParsingTools, GetTokenContext)
13411341
}
13421342
}
13431343

1344+
1345+
TEST(Common_ParsingTools, RefinePreprocessorDirective)
1346+
{
1347+
auto TestRefine = [](const std::string& Str, const char* RefStr) {
1348+
EXPECT_STREQ(RefinePreprocessorDirective(Str).c_str(), RefStr);
1349+
EXPECT_STREQ(RefinePreprocessorDirective(Str.c_str()).c_str(), RefStr);
1350+
EXPECT_STREQ(RefinePreprocessorDirective(Str.c_str(), Str.length()).c_str(), RefStr);
1351+
};
1352+
1353+
TestRefine("", "");
1354+
TestRefine(" ", "");
1355+
TestRefine("#", "");
1356+
TestRefine("# ", "");
1357+
TestRefine("# \t", "");
1358+
TestRefine("# \n", "");
1359+
TestRefine("# /*Comment*/", "");
1360+
1361+
TestRefine("define", "");
1362+
TestRefine("#\ndefine", "");
1363+
TestRefine("# // define", "");
1364+
TestRefine(",define", "");
1365+
1366+
TestRefine("#define", "define");
1367+
TestRefine("# define", "define");
1368+
TestRefine("# \t define", "define");
1369+
TestRefine("# \t /* Comment*/ define", "define");
1370+
}
1371+
13441372
} // namespace

0 commit comments

Comments
 (0)