mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-14 02:08:27 +08:00
cmArgumentParser: Refactor to allow for nested parsers
This commit is contained in:

committed by
taylor.sasser

parent
18f818f556
commit
aaeffdfe6b
@@ -186,11 +186,11 @@ void Instance::Consume(cm::string_view arg)
|
||||
|
||||
void Instance::FinishKeyword()
|
||||
{
|
||||
ParserState& state = this->GetState();
|
||||
|
||||
if (state.DoneWithPositional) {
|
||||
ParserState const& state = this->GetState();
|
||||
if (!state.DoneWithPositional) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.KeywordValuesSeen < state.KeywordValuesExpected) {
|
||||
if (this->ParseResults) {
|
||||
this->ParseResults->AddKeywordError(state.Keyword,
|
||||
|
@@ -190,14 +190,25 @@ class Instance
|
||||
public:
|
||||
Instance(ActionMap const& bindings, ParseResult* parseResult,
|
||||
std::vector<std::string>* unparsedArguments, void* result = nullptr)
|
||||
|
||||
: ParseState(bindings, result)
|
||||
, ParseResults(parseResult)
|
||||
: ParseResults(parseResult)
|
||||
, UnparsedArguments(unparsedArguments)
|
||||
{
|
||||
PushState(bindings, result);
|
||||
}
|
||||
|
||||
ParserState& GetState() { return ParseState; }
|
||||
ParserState& GetState() { return this->Stack.back(); }
|
||||
|
||||
void PushState(ActionMap const& bindings, void* result)
|
||||
{
|
||||
this->Stack.emplace_back(bindings, result);
|
||||
}
|
||||
|
||||
void PopState()
|
||||
{
|
||||
if (!this->Stack.empty()) {
|
||||
this->Stack.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
void Bind(std::function<Continue(cm::string_view)> f, ExpectAtLeast expect);
|
||||
void Bind(bool& val);
|
||||
@@ -207,6 +218,7 @@ public:
|
||||
void Bind(MaybeEmpty<std::vector<std::string>>& val);
|
||||
void Bind(NonEmpty<std::vector<std::string>>& val);
|
||||
void Bind(std::vector<std::vector<std::string>>& val);
|
||||
|
||||
template <typename U>
|
||||
void Bind(NonEmpty<std::vector<std::pair<std::string, U>>>& val,
|
||||
U const& context)
|
||||
@@ -239,20 +251,39 @@ public:
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
void Parse(Range const& args, std::size_t pos = 0)
|
||||
void Parse(Range& args, std::size_t const pos = 0)
|
||||
{
|
||||
GetState().Pos = pos;
|
||||
for (cm::string_view arg : args) {
|
||||
for (size_t j = 0; j < FindKeywordOwnerLevel(arg); ++j) {
|
||||
this->PopState();
|
||||
}
|
||||
|
||||
for (cm::string_view const arg : args) {
|
||||
this->Consume(arg);
|
||||
GetState().Pos++;
|
||||
}
|
||||
|
||||
this->FinishKeyword();
|
||||
|
||||
while (this->Stack.size() > 1) {
|
||||
this->FinishKeyword();
|
||||
this->PopState();
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t FindKeywordOwnerLevel(cm::string_view arg) const
|
||||
{
|
||||
for (std::size_t i = Stack.size(); i--;) {
|
||||
if (this->Stack[i].Bindings.Keywords.Find(arg) !=
|
||||
this->Stack[i].Bindings.Keywords.end()) {
|
||||
return (this->Stack.size() - 1) - i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
ParserState ParseState;
|
||||
std::vector<ParserState> Stack;
|
||||
ParseResult* ParseResults = nullptr;
|
||||
std::vector<std::string>* UnparsedArguments = nullptr;
|
||||
|
||||
@@ -269,6 +300,8 @@ template <typename Result>
|
||||
class cmArgumentParser : private ArgumentParser::Base
|
||||
{
|
||||
public:
|
||||
using Base::Bindings;
|
||||
|
||||
// I *think* this function could be made `constexpr` when the code is
|
||||
// compiled as C++20. This would allow building a parser at compile time.
|
||||
template <typename T, typename cT = cm::member_pointer_class_t<T>,
|
||||
@@ -393,6 +426,39 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
cmArgumentParser& BindSubParser(cm::static_string_view name,
|
||||
cmArgumentParser<T>& parser,
|
||||
cm::optional<T> U::*member)
|
||||
{
|
||||
|
||||
this->Base::Bind(name, [name, parser, member](Instance& instance) {
|
||||
auto* parentResult = static_cast<Result*>(instance.GetState().Result);
|
||||
auto& childResult = parentResult->*member;
|
||||
childResult.emplace(T());
|
||||
instance.Bind(nullptr, ExpectAtLeast{ 0 });
|
||||
instance.PushState(parser.Bindings, &(*childResult));
|
||||
instance.Consume(name);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
cmArgumentParser& BindSubParser(cm::static_string_view name,
|
||||
cmArgumentParser<T>& parser, T U::*member)
|
||||
{
|
||||
this->Base::Bind(name, [name, parser, member](Instance& instance) {
|
||||
auto* parentResult = static_cast<Result*>(instance.GetState().Result);
|
||||
auto* childResult = &(parentResult->*member);
|
||||
instance.Bind(nullptr, ExpectAtLeast{ 1 });
|
||||
instance.PushState(parser.Bindings, childResult);
|
||||
instance.Consume(name);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
bool Parse(Result& result, Range const& args,
|
||||
std::vector<std::string>* unparsedArguments,
|
||||
@@ -425,6 +491,8 @@ template <>
|
||||
class cmArgumentParser<void> : private ArgumentParser::Base
|
||||
{
|
||||
public:
|
||||
using Base::Bindings;
|
||||
|
||||
template <typename T>
|
||||
cmArgumentParser& Bind(cm::static_string_view name, T& ref)
|
||||
{
|
||||
@@ -483,6 +551,32 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
cmArgumentParser& BindSubParser(cm::static_string_view name,
|
||||
cmArgumentParser<T>& parser,
|
||||
cm::optional<U>& subResult)
|
||||
{
|
||||
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) {
|
||||
subResult.emplace(U());
|
||||
instance.Bind(nullptr, ExpectAtLeast{ 0 });
|
||||
instance.PushState(parser.Bindings, &(*subResult));
|
||||
instance.Consume(name);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
cmArgumentParser& BindSubParser(cm::static_string_view name,
|
||||
cmArgumentParser<T>& parser, U& subResult)
|
||||
{
|
||||
this->Base::Bind(name, [name, parser, &subResult](Instance& instance) {
|
||||
instance.Bind(nullptr, ExpectAtLeast{ 1 });
|
||||
instance.PushState(parser.Bindings, &subResult);
|
||||
instance.Consume(name);
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Range>
|
||||
ParseResult Parse(Range const& args,
|
||||
std::vector<std::string>* unparsedArguments,
|
||||
|
@@ -16,9 +16,17 @@
|
||||
#include "testCommon.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct Result : public ArgumentParser::ParseResult
|
||||
struct Result : ArgumentParser::ParseResult
|
||||
{
|
||||
struct SubResult : ParseResult
|
||||
{
|
||||
ArgumentParser::NonEmpty<std::string> SubCommand;
|
||||
bool SubOption = false;
|
||||
ArgumentParser::NonEmpty<std::string> SubString;
|
||||
ArgumentParser::NonEmpty<std::vector<std::string>> SubList;
|
||||
std::vector<cm::string_view> ParsedKeywords;
|
||||
};
|
||||
|
||||
bool Option1 = false;
|
||||
bool Option2 = false;
|
||||
|
||||
@@ -83,6 +91,9 @@ struct Result : public ArgumentParser::ParseResult
|
||||
: ArgumentParser::Continue::No;
|
||||
}
|
||||
|
||||
cm::optional<SubResult> Sub;
|
||||
ArgumentParser::NonEmpty<std::string> Parent;
|
||||
|
||||
ArgumentParser::Maybe<std::string> UnboundMaybe{ 'u', 'n', 'b', 'o',
|
||||
'u', 'n', 'd' };
|
||||
ArgumentParser::MaybeEmpty<std::vector<std::string>> UnboundMaybeEmpty{
|
||||
@@ -131,7 +142,12 @@ std::initializer_list<cm::string_view> const args = {
|
||||
"FUNC_3", "foo", "bar", // callback with list arg ...
|
||||
"FUNC_4a", "foo", "ign4", // callback with keyword-dependent arg count
|
||||
"FUNC_4b", "bar", "zot", // callback with keyword-dependent arg count
|
||||
/* clang-format on */
|
||||
"SUB_CMD", "foo", // switch to subparser and set Sub::SUB_CMD to foo
|
||||
"SUB_OPTION", // subparser option
|
||||
"SUB_STRING", "sub_value", // subparser string
|
||||
"SUB_LIST", "a", "b", "c", // subparser list
|
||||
// Return to main parser (simulate another main option if needed)
|
||||
"PARENT", "value",
|
||||
};
|
||||
|
||||
struct ResultTrailingPos : public ArgumentParser::ParseResult
|
||||
@@ -189,8 +205,15 @@ bool verifyResult(Result const& result,
|
||||
"FUNC_3",
|
||||
"FUNC_4a",
|
||||
"FUNC_4b",
|
||||
"SUB_CMD",
|
||||
"PARENT",
|
||||
/* clang-format on */
|
||||
};
|
||||
|
||||
static std::vector<cm::string_view> subParsedKeywords = {
|
||||
"SUB_CMD", "SUB_OPTION", "SUB_STRING", "SUB_LIST"
|
||||
};
|
||||
|
||||
static std::map<std::string, std::vector<std::string>> const func2map = {
|
||||
{ "FUNC_2a", { "foo" } }, { "FUNC_2b", { "bar", "zot" } }
|
||||
};
|
||||
@@ -257,6 +280,14 @@ bool verifyResult(Result const& result,
|
||||
ASSERT_TRUE(result.UnboundNonEmpty == unbound);
|
||||
ASSERT_TRUE(result.UnboundNonEmptyStr == "unbound");
|
||||
|
||||
ASSERT_TRUE(result.Sub->SubCommand == "foo");
|
||||
ASSERT_TRUE(result.Sub->SubOption);
|
||||
ASSERT_TRUE(result.Sub->SubString == "sub_value");
|
||||
ASSERT_TRUE(result.Sub->SubList ==
|
||||
std::vector<std::string>({ "a", "b", "c" }));
|
||||
ASSERT_TRUE(result.Parent == "value");
|
||||
|
||||
ASSERT_TRUE(result.Sub->ParsedKeywords == subParsedKeywords);
|
||||
ASSERT_TRUE(result.ParsedKeywords == parsedKeywords);
|
||||
|
||||
ASSERT_TRUE(result.GetKeywordErrors().size() == keywordErrors.size());
|
||||
@@ -292,6 +323,8 @@ bool verifyResult(ResultTrailingPos const& result,
|
||||
bool testArgumentParserDynamic()
|
||||
{
|
||||
Result result;
|
||||
result.Sub = Result::SubResult();
|
||||
|
||||
std::vector<std::string> unparsedArguments;
|
||||
|
||||
std::function<ArgumentParser::Continue(cm::string_view, cm::string_view)>
|
||||
@@ -305,6 +338,13 @@ bool testArgumentParserDynamic()
|
||||
ASSERT_TRUE(parserDynamic.HasKeyword("OPTION_1"_s));
|
||||
ASSERT_TRUE(!parserDynamic.HasKeyword("NOT_AN_OPTION"_s));
|
||||
|
||||
auto subParser = cmArgumentParser<void>{}
|
||||
.Bind("SUB_CMD"_s, result.Sub->SubCommand)
|
||||
.Bind("SUB_OPTION"_s, result.Sub->SubOption)
|
||||
.Bind("SUB_STRING"_s, result.Sub->SubString)
|
||||
.Bind("SUB_LIST"_s, result.Sub->SubList)
|
||||
.BindParsedKeywords(result.Sub->ParsedKeywords);
|
||||
|
||||
static_cast<ArgumentParser::ParseResult&>(result) =
|
||||
cmArgumentParser<void>{}
|
||||
.Bind(0, result.Pos0)
|
||||
@@ -349,6 +389,8 @@ bool testArgumentParserDynamic()
|
||||
})
|
||||
.Bind("FUNC_4a"_s, func4)
|
||||
.Bind("FUNC_4b"_s, func4)
|
||||
.BindSubParser("SUB_CMD"_s, subParser, result.Sub)
|
||||
.Bind("PARENT"_s, result.Parent)
|
||||
.BindParsedKeywords(result.ParsedKeywords)
|
||||
.Parse(args, &unparsedArguments);
|
||||
|
||||
@@ -378,7 +420,17 @@ static auto const parserStaticFunc4 =
|
||||
};
|
||||
|
||||
#define BIND_ALL(name, resultType) \
|
||||
static auto const name = \
|
||||
/* Define the sub-parser first */ \
|
||||
static auto sub##name = \
|
||||
cmArgumentParser<resultType::SubResult>{} \
|
||||
.Bind("SUB_CMD"_s, &resultType::SubResult::SubCommand) \
|
||||
.Bind("SUB_OPTION"_s, &resultType::SubResult::SubOption) \
|
||||
.Bind("SUB_STRING"_s, &resultType::SubResult::SubString) \
|
||||
.Bind("SUB_LIST"_s, &resultType::SubResult::SubList) \
|
||||
.BindParsedKeywords(&resultType::SubResult::ParsedKeywords); \
|
||||
\
|
||||
/* Define the main parser, which uses the sub-parser */ \
|
||||
static const auto name = \
|
||||
cmArgumentParser<resultType>{} \
|
||||
.Bind(0, &resultType::Pos0) \
|
||||
.Bind(1, &resultType::Pos1) \
|
||||
@@ -411,7 +463,9 @@ static auto const parserStaticFunc4 =
|
||||
-> ArgumentParser::Continue { return result.Func3(arg); }) \
|
||||
.Bind("FUNC_4a"_s, parserStaticFunc4) \
|
||||
.Bind("FUNC_4b"_s, parserStaticFunc4) \
|
||||
.BindParsedKeywords(&resultType::ParsedKeywords)
|
||||
.BindSubParser("SUB_CMD"_s, sub##name, &resultType::Sub) \
|
||||
.Bind("PARENT"_s, &resultType::Parent) \
|
||||
.BindParsedKeywords(&resultType::ParsedKeywords);
|
||||
|
||||
BIND_ALL(parserStatic, Result);
|
||||
BIND_ALL(parserDerivedStatic, Derived);
|
||||
@@ -484,11 +538,10 @@ bool testArgumentParserTypes()
|
||||
|
||||
ArgumentParser::NonEmpty<std::vector<std::string>> nonEmptyVecStr;
|
||||
nonEmptyVecStr = std::vector<std::string>{ "" };
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
}
|
||||
|
||||
int testArgumentParser(int /*unused*/, char* /*unused*/[])
|
||||
{
|
||||
|
Reference in New Issue
Block a user