Merge topic 'add-subparsers'

aaeffdfe6b cmArgumentParser: Refactor to allow for nested parsers
18f818f556 cmArgumentParser: Move parser state into dedicated struct

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !10963
This commit is contained in:
Brad King
2025-07-24 13:36:58 +00:00
committed by Kitware Robot
3 changed files with 234 additions and 63 deletions

View File

@@ -56,8 +56,8 @@ auto PositionActionMap::Find(std::size_t pos) const -> const_iterator
void Instance::Bind(std::function<Continue(cm::string_view)> f,
ExpectAtLeast expect)
{
this->KeywordValueFunc = std::move(f);
this->KeywordValuesExpected = expect.Count;
this->GetState().KeywordValueFunc = std::move(f);
this->GetState().KeywordValuesExpected = expect.Count;
}
void Instance::Bind(bool& val)
@@ -81,7 +81,7 @@ void Instance::Bind(NonEmpty<std::string>& val)
this->Bind(
[this, &val](cm::string_view arg) -> Continue {
if (arg.empty() && this->ParseResults) {
this->ParseResults->AddKeywordError(this->Keyword,
this->ParseResults->AddKeywordError(this->GetState().Keyword,
" empty string not allowed\n");
}
val = std::string(arg);
@@ -132,48 +132,50 @@ void Instance::Bind(std::vector<std::vector<std::string>>& multiVal)
ExpectAtLeast{ 0 });
}
void Instance::Consume(std::size_t pos, cm::string_view arg)
void Instance::Consume(cm::string_view arg)
{
auto const it = this->Bindings.Keywords.Find(arg);
if (it != this->Bindings.Keywords.end()) {
ParserState& state = this->GetState();
auto const it = state.Bindings.Keywords.Find(arg);
if (it != state.Bindings.Keywords.end()) {
this->FinishKeyword();
this->Keyword = it->first;
this->KeywordValuesSeen = 0;
this->DoneWithPositional = true;
if (this->Bindings.ParsedKeyword) {
this->Bindings.ParsedKeyword(*this, it->first);
state.Keyword = it->first;
state.KeywordValuesSeen = 0;
state.DoneWithPositional = true;
if (state.Bindings.ParsedKeyword) {
state.Bindings.ParsedKeyword(*this, it->first);
}
it->second(*this);
return;
}
if (!this->DoneWithPositional) {
auto const pit = this->Bindings.Positions.Find(pos);
if (pit != this->Bindings.Positions.end()) {
pit->second(*this, pos, arg);
if (!state.DoneWithPositional) {
auto const pit = state.Bindings.Positions.Find(state.Pos);
if (pit != state.Bindings.Positions.end()) {
pit->second(*this, state.Pos, arg);
return;
}
if (this->Bindings.TrailingArgs) {
this->Keyword = ""_s;
this->KeywordValuesSeen = 0;
this->DoneWithPositional = true;
this->Bindings.TrailingArgs(*this);
if (!this->KeywordValueFunc) {
if (state.Bindings.TrailingArgs) {
state.Keyword = ""_s;
state.KeywordValuesSeen = 0;
state.DoneWithPositional = true;
state.Bindings.TrailingArgs(*this);
if (!state.KeywordValueFunc) {
return;
}
}
}
if (this->KeywordValueFunc) {
switch (this->KeywordValueFunc(arg)) {
if (state.KeywordValueFunc) {
switch (state.KeywordValueFunc(arg)) {
case Continue::Yes:
break;
case Continue::No:
this->KeywordValueFunc = nullptr;
state.KeywordValueFunc = nullptr;
break;
}
++this->KeywordValuesSeen;
++state.KeywordValuesSeen;
return;
}
@@ -184,16 +186,18 @@ void Instance::Consume(std::size_t pos, cm::string_view arg)
void Instance::FinishKeyword()
{
if (!this->DoneWithPositional) {
ParserState const& state = this->GetState();
if (!state.DoneWithPositional) {
return;
}
if (this->KeywordValuesSeen < this->KeywordValuesExpected) {
if (state.KeywordValuesSeen < state.KeywordValuesExpected) {
if (this->ParseResults) {
this->ParseResults->AddKeywordError(this->Keyword,
this->ParseResults->AddKeywordError(state.Keyword,
" missing required value\n");
}
if (this->Bindings.KeywordMissingValue) {
this->Bindings.KeywordMissingValue(*this, this->Keyword);
if (state.Bindings.KeywordMissingValue) {
state.Bindings.KeywordMissingValue(*this, state.Keyword);
}
}
}

View File

@@ -165,16 +165,49 @@ public:
}
};
class ParserState
{
public:
cm::string_view Keyword;
std::size_t Pos = 0;
std::size_t KeywordValuesSeen = 0;
std::size_t KeywordValuesExpected = 0;
std::function<Continue(cm::string_view)> KeywordValueFunc = nullptr;
ActionMap const& Bindings;
void* Result = nullptr;
bool DoneWithPositional = false;
ParserState(ActionMap const& bindings, void* result)
: Bindings(bindings)
, Result(result)
{
}
};
class Instance
{
public:
Instance(ActionMap const& bindings, ParseResult* parseResult,
std::vector<std::string>* unparsedArguments, void* result = nullptr)
: Bindings(bindings)
, ParseResults(parseResult)
: ParseResults(parseResult)
, UnparsedArguments(unparsedArguments)
, Result(result)
{
PushState(bindings, result);
}
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);
@@ -185,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)
@@ -217,27 +251,43 @@ 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) {
this->Consume(pos++, arg);
for (size_t j = 0; j < FindKeywordOwnerLevel(arg); ++j) {
this->PopState();
}
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:
ActionMap const& Bindings;
std::vector<ParserState> Stack;
ParseResult* ParseResults = nullptr;
std::vector<std::string>* UnparsedArguments = nullptr;
void* Result = nullptr;
cm::string_view Keyword;
std::size_t KeywordValuesSeen = 0;
std::size_t KeywordValuesExpected = 0;
std::function<Continue(cm::string_view)> KeywordValueFunc;
bool DoneWithPositional = false;
void Consume(std::size_t pos, cm::string_view arg);
void Consume(cm::string_view arg);
void FinishKeyword();
template <typename Result>
@@ -250,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>,
@@ -259,7 +311,7 @@ public:
cmArgumentParser& Bind(cm::static_string_view name, T member)
{
this->Base::Bind(name, [member](Instance& instance) {
instance.Bind(static_cast<Result*>(instance.Result)->*member);
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member);
});
return *this;
}
@@ -269,7 +321,7 @@ public:
T Result::*member, U Result::*context)
{
this->Base::Bind(name, [member, context](Instance& instance) {
auto* result = static_cast<Result*>(instance.Result);
auto* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(result->*member, result->*context);
});
return *this;
@@ -280,7 +332,7 @@ public:
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [member, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.Result);
Result* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(
[result, member](cm::string_view arg) -> Continue {
return (result->*member)(arg);
@@ -296,8 +348,8 @@ public:
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [member, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.Result);
cm::string_view keyword = instance.Keyword;
Result* result = static_cast<Result*>(instance.GetState().Result);
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[result, member, keyword](cm::string_view arg) -> Continue {
return (result->*member)(keyword, arg);
@@ -312,7 +364,7 @@ public:
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.Result);
Result* result = static_cast<Result*>(instance.GetState().Result);
instance.Bind(
[result, &f](cm::string_view arg) -> Continue {
return f(*result, arg);
@@ -328,8 +380,8 @@ public:
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
Result* result = static_cast<Result*>(instance.Result);
cm::string_view keyword = instance.Keyword;
Result* result = static_cast<Result*>(instance.GetState().Result);
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[result, keyword, &f](cm::string_view arg) -> Continue {
return f(*result, keyword, arg);
@@ -345,7 +397,7 @@ public:
this->Base::Bind(
position,
[member](Instance& instance, std::size_t, cm::string_view arg) {
Result* result = static_cast<Result*>(instance.Result);
Result* result = static_cast<Result*>(instance.GetState().Result);
result->*member = arg;
});
return *this;
@@ -356,7 +408,8 @@ public:
{
this->Base::BindParsedKeyword(
[member](Instance& instance, cm::string_view arg) {
(static_cast<Result*>(instance.Result)->*member).emplace_back(arg);
(static_cast<Result*>(instance.GetState().Result)->*member)
.emplace_back(arg);
});
return *this;
}
@@ -368,11 +421,44 @@ public:
cmArgumentParser& BindTrailingArgs(T member)
{
this->Base::BindTrailingArgs([member](Instance& instance) {
instance.Bind(static_cast<Result*>(instance.Result)->*member);
instance.Bind(static_cast<Result*>(instance.GetState().Result)->*member);
});
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,
@@ -405,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)
{
@@ -429,7 +517,7 @@ public:
ExpectAtLeast expect = { 1 })
{
this->Base::Bind(name, [f, expect](Instance& instance) {
cm::string_view keyword = instance.Keyword;
cm::string_view keyword = instance.GetState().Keyword;
instance.Bind(
[keyword, &f](cm::string_view arg) -> Continue {
return f(keyword, arg);
@@ -463,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,